<commit_before>import os
import sys
from unittest import TestCase
from pecan import configuration
from pecan import conf as _runtime_conf

class TestConf(TestCase):

    def setUp(self):
        test_config_d = os.path.join(os.path.dirname(__file__), 'test_config')

        if test_config_d not in sys.path:
            sys.path.append(test_config_d)

    def test_update_config_fail_identifier(self):
        """Fail when naming does not pass correctness"""
        bad_dict = {'bad name':'value'}
        self.assertRaises(ValueError, configuration.Config, bad_dict)

    def test_update_set_config(self):
        """Update an empty configuration with the default values"""

        conf = configuration.initconf()
        conf.update_with_module('config')

        self.assertTrue(conf.app.debug)
        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, 'myproject/templates')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '1.1.1.1')
        self.assertEqual(conf.server.port, '8081')

    def test_update_set_default_config(self):
        """Update an empty configuration with the default values"""

        conf = configuration.initconf()
        conf.update_with_module('empty')

        self.assertFalse(conf.app.debug)
        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, '')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '0.0.0.0')
        self.assertEqual(conf.server.port, '8080')

    def test_update_force_dict(self):
        """Update an empty configuration with the default values"""
        conf = configuration.initconf()
        conf.update_with_module('forcedict')

        self.assertFalse(conf.app.debug)
        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, '')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '0.0.0.0')
        self.assertEqual(conf.server.port, '8080')

        self.assertTrue(isinstance(conf.beaker, dict))
        self.assertEqual(conf.beaker['session.key'], 'key')
        self.assertEqual(conf.beaker['session.type'], 'cookie')
        self.assertEqual(conf.beaker['session.validate_key'], '1a971a7df182df3e1dec0af7c6913ec7')
        self.assertEqual(conf.beaker.get('__force_dict__'), None)

    def test_update_config_with_dict(self):
        conf = configuration.initconf()
        d = {'attr':True}
        conf['attr'] = d
        self.assertTrue(conf.attr.attr)

    def test_config_dirname(self):
        from pecan import default_config
        conf = configuration.initconf()
        conf['path'] = '%(confdir)s'
        self.assertEqual(conf.path, os.path.dirname(default_config.__file__))

    def test_config_repr(self):
        conf = configuration.Config({'a':1})
        self.assertEqual(repr(conf),"Config({'a': 1})")

    def test_config_from_dict(self):
        conf = configuration.conf_from_dict({})
        conf['path'] = '%(confdir)s'
        self.assertTrue(os.path.samefile(conf['path'], os.getcwd()))

    def test_config_from_file(self):
        path = os.path.join(os.path.dirname(__file__), 'test_config', 'config.py')
        conf = configuration.conf_from_file(path)
        self.assertTrue(conf.app.debug)

    def test_config_illegal_ids(self):
        conf = configuration.Config({})
        conf.update_with_module('bad.module_and_underscore')
        self.assertEqual([], list(conf))

    def test_config_bad_module(self):
        conf = configuration.Config({})
        self.assertRaises(ImportError, conf.update_with_module, 'doesnotexist')
        self.assertRaises(ImportError, conf.update_with_module, 'bad.doesnotexists')
        self.assertRaises(ImportError, conf.update_with_module, 'bad.bad.doesnotexist')
        self.assertRaises(SyntaxError, conf.update_with_module, 'bad.syntaxerror')
        self.assertRaises(ImportError, conf.update_with_module, 'bad.importerror')

    def test_config_set_from_file(self):
        path = os.path.join(os.path.dirname(__file__), 'test_config', 'empty.py')
        configuration.set_config(path)
        assert list(_runtime_conf.server) == list(configuration.initconf().server)

    def test_config_dir(self):
        if sys.version_info >= (2, 6):
            conf = configuration.Config({})
            self.assertEqual([], dir(conf))
            conf = configuration.Config({'a':1})
            self.assertEqual(['a'], dir(conf))

    def test_config_bad_key(self):
        conf = configuration.Config({'a': 1})
        assert conf.a == 1
        self.assertRaises(AttributeError, getattr, conf, 'b')

    def test_config_as_dict(self):
        conf = configuration.initconf()

        assert isinstance(conf, configuration.Config)

        as_dict = conf.as_dict()

        assert isinstance(as_dict, dict)
        assert as_dict['server']['host']         == '0.0.0.0'
        assert as_dict['server']['port']         == '8080'
        assert as_dict['app']['debug']           == False
        assert as_dict['app']['errors']          == {}
        assert as_dict['app']['force_canonical'] == True
        assert as_dict['app']['modules']         == []
        assert as_dict['app']['root']            == None
        assert as_dict['app']['static_root']     == 'public'
        assert as_dict['app']['template_path']   == ''

    def test_config_as_dict_nested(self):
        """have more than one level nesting and convert to dict"""
        conf = configuration.initconf()
        nested = {'one':{'two':2}}
        conf['nested'] = nested

        as_dict = conf.as_dict()

        assert isinstance(as_dict, dict)
        assert as_dict['server']['host']         == '0.0.0.0'
        assert as_dict['server']['port']         == '8080'
        assert as_dict['app']['debug']           == False
        assert as_dict['app']['errors']          == {}
        assert as_dict['app']['force_canonical'] == True
        assert as_dict['app']['modules']         == []
        assert as_dict['app']['root']            == None
        assert as_dict['app']['static_root']     == 'public'
        assert as_dict['app']['template_path']   == ''
        assert as_dict['nested']['one']['two']   == 2

    def test_config_as_dict_prefixed(self):
        """Add a prefix for keys"""
        conf = configuration.initconf()

        assert isinstance(conf, configuration.Config)

        as_dict = conf.as_dict('prefix_')

        assert isinstance(as_dict, dict)
        assert as_dict['prefix_server']['prefix_host']         == '0.0.0.0'
        assert as_dict['prefix_server']['prefix_port']         == '8080'
        assert as_dict['prefix_app']['prefix_debug']           == False
        assert as_dict['prefix_app']['prefix_errors']          == {}
        assert as_dict['prefix_app']['prefix_force_canonical'] == True
        assert as_dict['prefix_app']['prefix_modules']         == []
        assert as_dict['prefix_app']['prefix_root']            == None
        assert as_dict['prefix_app']['prefix_static_root']     == 'public'
        assert as_dict['prefix_app']['prefix_template_path']   == ''

<commit_msg>add tests to support the new get method in the config object<commit_after>import os
import sys
from unittest import TestCase
from pecan import configuration
from pecan import conf as _runtime_conf

class TestConf(TestCase):

    def setUp(self):
        test_config_d = os.path.join(os.path.dirname(__file__), 'test_config')

        if test_config_d not in sys.path:
            sys.path.append(test_config_d)

    def test_update_config_fail_identifier(self):
        """Fail when naming does not pass correctness"""
        bad_dict = {'bad name':'value'}
        self.assertRaises(ValueError, configuration.Config, bad_dict)

    def test_update_set_config(self):
        """Update an empty configuration with the default values"""

        conf = configuration.initconf()
        conf.update_with_module('config')

        self.assertTrue(conf.app.debug)
        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, 'myproject/templates')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '1.1.1.1')
        self.assertEqual(conf.server.port, '8081')

    def test_update_set_default_config(self):
        """Update an empty configuration with the default values"""

        conf = configuration.initconf()
        conf.update_with_module('empty')

        self.assertFalse(conf.app.debug)
        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, '')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '0.0.0.0')
        self.assertEqual(conf.server.port, '8080')

    def test_update_force_dict(self):
        """Update an empty configuration with the default values"""
        conf = configuration.initconf()
        conf.update_with_module('forcedict')

        self.assertFalse(conf.app.debug)
        self.assertEqual(conf.app.root, None)
        self.assertEqual(conf.app.template_path, '')
        self.assertEqual(conf.app.static_root, 'public')

        self.assertEqual(conf.server.host, '0.0.0.0')
        self.assertEqual(conf.server.port, '8080')

        self.assertTrue(isinstance(conf.beaker, dict))
        self.assertEqual(conf.beaker['session.key'], 'key')
        self.assertEqual(conf.beaker['session.type'], 'cookie')
        self.assertEqual(conf.beaker['session.validate_key'], '1a971a7df182df3e1dec0af7c6913ec7')
        self.assertEqual(conf.beaker.get('__force_dict__'), None)

    def test_update_config_with_dict(self):
        conf = configuration.initconf()
        d = {'attr':True}
        conf['attr'] = d
        self.assertTrue(conf.attr.attr)

    def test_config_dirname(self):
        from pecan import default_config
        conf = configuration.initconf()
        conf['path'] = '%(confdir)s'
        self.assertEqual(conf.path, os.path.dirname(default_config.__file__))

    def test_config_repr(self):
        conf = configuration.Config({'a':1})
        self.assertEqual(repr(conf),"Config({'a': 1})")

    def test_config_from_dict(self):
        conf = configuration.conf_from_dict({})
        conf['path'] = '%(confdir)s'
        self.assertTrue(os.path.samefile(conf['path'], os.getcwd()))

    def test_config_from_file(self):
        path = os.path.join(os.path.dirname(__file__), 'test_config', 'config.py')
        conf = configuration.conf_from_file(path)
        self.assertTrue(conf.app.debug)

    def test_config_illegal_ids(self):
        conf = configuration.Config({})
        conf.update_with_module('bad.module_and_underscore')
        self.assertEqual([], list(conf))

    def test_config_bad_module(self):
        conf = configuration.Config({})
        self.assertRaises(ImportError, conf.update_with_module, 'doesnotexist')
        self.assertRaises(ImportError, conf.update_with_module, 'bad.doesnotexists')
        self.assertRaises(ImportError, conf.update_with_module, 'bad.bad.doesnotexist')
        self.assertRaises(SyntaxError, conf.update_with_module, 'bad.syntaxerror')
        self.assertRaises(ImportError, conf.update_with_module, 'bad.importerror')

    def test_config_set_from_file(self):
        path = os.path.join(os.path.dirname(__file__), 'test_config', 'empty.py')
        configuration.set_config(path)
        assert list(_runtime_conf.server) == list(configuration.initconf().server)

    def test_config_dir(self):
        if sys.version_info >= (2, 6):
            conf = configuration.Config({})
            self.assertEqual([], dir(conf))
            conf = configuration.Config({'a':1})
            self.assertEqual(['a'], dir(conf))

    def test_config_bad_key(self):
        conf = configuration.Config({'a': 1})
        assert conf.a == 1
        self.assertRaises(AttributeError, getattr, conf, 'b')

    def test_config_get_valid_key(self):
        conf = configuration.Config({'a': 1})
        assert conf.get('a') == 1

    def test_config_get_invalid_key_is_none(self):
        conf = configuration.Config({'a': 1})
        assert conf.get('b') is None

    def test_config_as_dict(self):
        conf = configuration.initconf()

        assert isinstance(conf, configuration.Config)

        as_dict = conf.as_dict()

        assert isinstance(as_dict, dict)
        assert as_dict['server']['host']         == '0.0.0.0'
        assert as_dict['server']['port']         == '8080'
        assert as_dict['app']['debug']           == False
        assert as_dict['app']['errors']          == {}
        assert as_dict['app']['force_canonical'] == True
        assert as_dict['app']['modules']         == []
        assert as_dict['app']['root']            == None
        assert as_dict['app']['static_root']     == 'public'
        assert as_dict['app']['template_path']   == ''

    def test_config_as_dict_nested(self):
        """have more than one level nesting and convert to dict"""
        conf = configuration.initconf()
        nested = {'one':{'two':2}}
        conf['nested'] = nested

        as_dict = conf.as_dict()

        assert isinstance(as_dict, dict)
        assert as_dict['server']['host']         == '0.0.0.0'
        assert as_dict['server']['port']         == '8080'
        assert as_dict['app']['debug']           == False
        assert as_dict['app']['errors']          == {}
        assert as_dict['app']['force_canonical'] == True
        assert as_dict['app']['modules']         == []
        assert as_dict['app']['root']            == None
        assert as_dict['app']['static_root']     == 'public'
        assert as_dict['app']['template_path']   == ''
        assert as_dict['nested']['one']['two']   == 2

    def test_config_as_dict_prefixed(self):
        """Add a prefix for keys"""
        conf = configuration.initconf()

        assert isinstance(conf, configuration.Config)

        as_dict = conf.as_dict('prefix_')

        assert isinstance(as_dict, dict)
        assert as_dict['prefix_server']['prefix_host']         == '0.0.0.0'
        assert as_dict['prefix_server']['prefix_port']         == '8080'
        assert as_dict['prefix_app']['prefix_debug']           == False
        assert as_dict['prefix_app']['prefix_errors']          == {}
        assert as_dict['prefix_app']['prefix_force_canonical'] == True
        assert as_dict['prefix_app']['prefix_modules']         == []
        assert as_dict['prefix_app']['prefix_root']            == None
        assert as_dict['prefix_app']['prefix_static_root']     == 'public'
        assert as_dict['prefix_app']['prefix_template_path']   == ''

<|endoftext|>


<commit_before>from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema
from jsonschema import Draft4Validator

from . import BaseTest, UserSchema

class TestDumpSchema(BaseTest):

    def _validate_schema(self, schema):
        '''
        raises jsonschema.exceptions.SchemaError
        '''
        Draft4Validator.check_schema(schema)

    def test_dump_schema(self):
        schema = UserSchema()
        json_schema = JSONSchema()
        dumped = json_schema.dump(schema).data
        self._validate_schema(dumped)
        self.assertGreater(len(schema.fields), 1)
        for field_name, field in schema.fields.items():
            self.assertIn(field_name, dumped['properties'])

    def test_default(self):
        schema = UserSchema()
        json_schema = JSONSchema()
        dumped = json_schema.dump(schema).data
        self._validate_schema(dumped)
        self.assertEqual(dumped['properties']['id']['default'], 'no-id')

    def test_unknown_typed_field(self):

        class Colour(fields.Field):

            def _jsonschema_type_mapping(self):
                return {
                    'type': 'string',
                }

            def _serialize(self, value, attr, obj):
                r, g, b = value
                r = hex(r)[2:]
                g = hex(g)[2:]
                b = hex(b)[2:]
                return '#' + r + g + b

        class UserSchema(Schema):
            name = fields.String(required=True)
            favourite_colour = Colour()

        schema = UserSchema()
        json_schema = JSONSchema()
        dumped = json_schema.dump(schema).data
        self.assertEqual(dumped['properties']['favourite_colour'],
                        {'type': 'string'})
<commit_msg>100% coverage<commit_after>from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema
from jsonschema import Draft4Validator

from . import BaseTest, UserSchema

class TestDumpSchema(BaseTest):

    def _validate_schema(self, schema):
        '''
        raises jsonschema.exceptions.SchemaError
        '''
        Draft4Validator.check_schema(schema)

    def test_dump_schema(self):
        schema = UserSchema()
        json_schema = JSONSchema()
        dumped = json_schema.dump(schema).data
        self._validate_schema(dumped)
        self.assertGreater(len(schema.fields), 1)
        for field_name, field in schema.fields.items():
            self.assertIn(field_name, dumped['properties'])

    def test_default(self):
        schema = UserSchema()
        json_schema = JSONSchema()
        dumped = json_schema.dump(schema).data
        self._validate_schema(dumped)
        self.assertEqual(dumped['properties']['id']['default'], 'no-id')

    def test_unknown_typed_field_throws_valueerror(self):

        class Invalid(fields.Field):
            def _serialize(self, value, attr, obj):
                return value

        class UserSchema(Schema):
            favourite_colour = Invalid()

        schema = UserSchema()
        json_schema = JSONSchema()
        with self.assertRaises(ValueError):
            dumped = json_schema.dump(schema).data

    def test_unknown_typed_field(self):

        class Colour(fields.Field):

            def _jsonschema_type_mapping(self):
                return {
                    'type': 'string',
                }

            def _serialize(self, value, attr, obj):
                r, g, b = value
                r = hex(r)[2:]
                g = hex(g)[2:]
                b = hex(b)[2:]
                return '#' + r + g + b

        class UserSchema(Schema):
            name = fields.String(required=True)
            favourite_colour = Colour()

        schema = UserSchema()
        json_schema = JSONSchema()
        dumped = json_schema.dump(schema).data
        self.assertEqual(dumped['properties']['favourite_colour'],
                        {'type': 'string'})
<|endoftext|>


<commit_before>import pytest

@pytest.mark.xfail
class TestFile(object):
    def test_context_manager(self, tmpdir, registry_container):
        assert 0
    def test_create(self, tmpdir, registry_container):
        assert 0
    def test_close(self, tmpdir, registry_container):
        assert 0

<commit_msg>Another test for file wrapping<commit_after>import pytest

from h5preserve import H5PreserveFile, new_registry_list

class TestFile(object):
    @pytest.mark.xfail
    def test_context_manager(self, tmpdir, registry_container):
        assert 0
    @pytest.mark.xfail
    def test_create(self, tmpdir, registry_container):
        assert 0
    @pytest.mark.xfail
    def test_close(self, tmpdir, registry_container):
        assert 0

    def test_roundtrip(self, h5py_file):
        assert h5py_file == H5PreserveFile(
            h5py_file, new_registry_list()
        ).h5py_file

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from flask import Blueprint, request, jsonify
from flask import current_app as app
from app.utils import handle_error
from app.search.models import DataPackageQuery

search_blueprint = Blueprint('search', __name__, url_prefix='/api/search')

@search_blueprint.route("/package", methods=["GET"])
def search_packages():
    """
        DPR data package search operation.
        This API is responsible for searching data package
        ---
        tags:
            - search
        parameters:
            - in: query
              name: q
              type: string
              required: true
              description: search query string e.g. q=query publisher=pub
        responses:
            500:
                description: Internal Server Error
            200:
                description: Success Message
                schema:
                    id: search_package_success
                    properties:
                        total_count:
                            type: integer
                            description: Total datapackage count
                        items:
                            type: list
                            properties:
                                type: object
        """
    try:
        q = request.args.get('q').strip()
        if q:
            result = DataPackageQuery(query_string=q).get_data()
        else:
            result = DataPackageQuery(query_string=q).get_data(50)

        return jsonify(dict(items=result, total_count=len(result)))
    except Exception as e:
        app.logger.error(e)
        return handle_error('GENERIC_ERROR', e.message, 500)
<commit_msg>[search][s]:  added limit for search api results to 20<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from flask import Blueprint, request, jsonify
from flask import current_app as app
from app.utils import handle_error
from app.search.models import DataPackageQuery

search_blueprint = Blueprint('search', __name__, url_prefix='/api/search')

@search_blueprint.route("/package", methods=["GET"])
def search_packages():
    """
        DPR data package search operation.
        This API is responsible for searching data package
        ---
        tags:
            - search
        parameters:
            - in: query
              name: q
              type: string
              required: true
              description: search query string e.g. q=query publisher=pub
        responses:
            500:
                description: Internal Server Error
            200:
                description: Success Message
                schema:
                    id: search_package_success
                    properties:
                        total_count:
                            type: integer
                            description: Total datapackage count
                        items:
                            type: list
                            properties:
                                type: object
        """
    try:
        q = request.args.get('q').strip()
        result = DataPackageQuery(query_string=q).get_data(20)

        return jsonify(dict(items=result, total_count=len(result)))
    except Exception as e:
        app.logger.error(e)
        return handle_error('GENERIC_ERROR', e.message, 500)
<|endoftext|>


<commit_before>from __future__ import absolute_import
import os
import sys
import glob
from contextlib import contextmanager

PYTHON_VERSION = sys.version_info[0]

from numpy.testing import assert_allclose
import numpy as np
from sklearn.externals import joblib
import subprocess as sp
from gala import imio, classify, features, agglo, evaluate as ev
from six.moves import map

@contextmanager
def tar_extract(fn):
    sp.call(['tar', '-xzf', fn + '.tar.gz'])
    ext_fn = os.path.basename(fn)
    yield ext_fn
    os.remove(ext_fn)
    for sub_fn in glob.glob(ext_fn + '_*'):
        os.remove(sub_fn)

rundir = os.path.dirname(__file__)

# load example data

train_list = ['example-data/train-gt.lzf.h5', 'example-data/train-p1.lzf.h5',
              'example-data/train-p4.lzf.h5', 'example-data/train-ws.lzf.h5']
train_list = [os.path.join(rundir, fn) for fn in train_list]
gt_train, pr_train, p4_train, ws_train = map(imio.read_h5_stack, train_list)
test_list = ['example-data/test-gt.lzf.h5', 'example-data/test-p1.lzf.h5',
             'example-data/test-p4.lzf.h5', 'example-data/test-ws.lzf.h5']
test_list = [os.path.join(rundir, fn) for fn in test_list]
gt_test, pr_test, p4_test, ws_test = map(imio.read_h5_stack, test_list)

# prepare feature manager
fm = features.moments.Manager()
fh = features.histogram.Manager()
fc = features.base.Composite(children=[fm, fh])

### helper functions

def load_training_data(fn):
    io = np.load(fn)
    return io['X'], io['y']

def save_training_data(fn, X, y):
    np.savez(fn, X=X, y=y)

def train_and_save_classifier(training_data_file, filename,
                              classifier_kind='random forest'):
    X, y = load_training_data(training_data_file)
    cl = classify.get_classifier(classifier_kind)
    cl.fit(X, y)
    classify.save_classifier(cl, filename, use_joblib=False)

### tests

def test_generate_examples_1_channel():
    g_train = agglo.Rag(ws_train, pr_train, feature_manager=fc)
    np.random.RandomState(0)
    (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc, random_state=0)[0]
    X_expected, y_expected = load_training_data(
            os.path.join(rundir, 'example-data/train-set-1.npz'))
    assert_allclose(X, X_expected, atol=1e-6)
    assert_allclose(y, y_expected, atol=1e-6)

def test_segment_with_classifer_1_channel():
    if PYTHON_VERSION == 2:
        rf = classify.load_classifier(
            os.path.join(rundir, 'example-data/rf-1.joblib'))
    else:
        fn = os.path.join(rundir, 'example-data/rf1-py3.joblib')
        with tar_extract(fn) as fn:
            rf = joblib.load(fn)
    learned_policy = agglo.classifier_probability(fc, rf)
    g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    #imio.write_h5_stack(seg_test, 'example-data/test-seg-1.lzf.h5')
    seg_expected = imio.read_h5_stack(
        os.path.join(rundir, 'example-data/test-seg-1.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)

def test_generate_examples_4_channel():
    g_train = agglo.Rag(ws_train, p4_train, feature_manager=fc)
    np.random.RandomState(0)
    (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc, random_state=0)[0]
    X_expected, y_expected = load_training_data(
            os.path.join(rundir, 'example-data/train-set-4.npz'))
    assert_allclose(X, X_expected, atol=1e-6)
    assert_allclose(y, y_expected, atol=1e-6)

def test_segment_with_classifier_4_channel():
    if PYTHON_VERSION == 2:
        rf = classify.load_classifier(
            os.path.join(rundir, 'example-data/rf-4.joblib'))
    else:
        fn = os.path.join(rundir, 'example-data/rf4-py3.joblib')
        with tar_extract(fn) as fn:
            rf = joblib.load(fn)
    learned_policy = agglo.classifier_probability(fc, rf)
    g_test = agglo.Rag(ws_test, p4_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    seg_expected = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg-4.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)

def test_split_vi():
    seg_test1 = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg1.lzf.h5'))
    seg_test4 = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg4.lzf.h5'))
    result = np.vstack((
        ev.split_vi(ws_test, gt_test),
        ev.split_vi(seg_test1, gt_test),
        ev.split_vi(seg_test4, gt_test)
        ))
    expected = np.load(os.path.join(rundir, 'example-data/vi-results.npy'))
    assert_allclose(result, expected, atol=1e-6)

if __name__ == '__main__':
    np.random.RandomState(0)
    from numpy import testing
    testing.run_module_suite()
<commit_msg>Fix loading of training data<commit_after>from __future__ import absolute_import
import os
import sys
import glob
from contextlib import contextmanager

PYTHON_VERSION = sys.version_info[0]

from numpy.testing import assert_allclose
import numpy as np
from sklearn.externals import joblib
import subprocess as sp
from gala import imio, classify, features, agglo, evaluate as ev
from six.moves import map

@contextmanager
def tar_extract(fn):
    sp.call(['tar', '-xzf', fn + '.tar.gz'])
    ext_fn = os.path.basename(fn)
    yield ext_fn
    os.remove(ext_fn)
    for sub_fn in glob.glob(ext_fn + '_*'):
        os.remove(sub_fn)

rundir = os.path.dirname(__file__)

# load example data

train_list = ['example-data/train-gt.lzf.h5', 'example-data/train-p1.lzf.h5',
              'example-data/train-p4.lzf.h5', 'example-data/train-ws.lzf.h5']
train_list = [os.path.join(rundir, fn) for fn in train_list]
gt_train, pr_train, p4_train, ws_train = map(imio.read_h5_stack, train_list)
test_list = ['example-data/test-gt.lzf.h5', 'example-data/test-p1.lzf.h5',
             'example-data/test-p4.lzf.h5', 'example-data/test-ws.lzf.h5']
test_list = [os.path.join(rundir, fn) for fn in test_list]
gt_test, pr_test, p4_test, ws_test = map(imio.read_h5_stack, test_list)

# prepare feature manager
fm = features.moments.Manager()
fh = features.histogram.Manager()
fc = features.base.Composite(children=[fm, fh])

### helper functions

def load_training_data(fn):
    io = np.load(fn)
    X, y = io['X'], io['y']
    if y.ndim > 1:
        y = y[:, 0]
    return X, y

def save_training_data(fn, X, y):
    np.savez(fn, X=X, y=y)

def train_and_save_classifier(training_data_file, filename,
                              classifier_kind='random forest'):
    X, y = load_training_data(training_data_file)
    cl = classify.get_classifier(classifier_kind)
    cl.fit(X, y)
    classify.save_classifier(cl, filename, use_joblib=False)

### tests

def test_generate_examples_1_channel():
    g_train = agglo.Rag(ws_train, pr_train, feature_manager=fc)
    np.random.RandomState(0)
    (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc, random_state=0)[0]
    X_expected, y_expected = load_training_data(
            os.path.join(rundir, 'example-data/train-set-1.npz'))
    assert_allclose(X, X_expected, atol=1e-6)
    assert_allclose(y, y_expected, atol=1e-6)

def test_segment_with_classifer_1_channel():
    if PYTHON_VERSION == 2:
        rf = classify.load_classifier(
            os.path.join(rundir, 'example-data/rf-1.joblib'))
    else:
        fn = os.path.join(rundir, 'example-data/rf1-py3.joblib')
        with tar_extract(fn) as fn:
            rf = joblib.load(fn)
    learned_policy = agglo.classifier_probability(fc, rf)
    g_test = agglo.Rag(ws_test, pr_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    #imio.write_h5_stack(seg_test, 'example-data/test-seg-1.lzf.h5')
    seg_expected = imio.read_h5_stack(
        os.path.join(rundir, 'example-data/test-seg-1.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)

def test_generate_examples_4_channel():
    g_train = agglo.Rag(ws_train, p4_train, feature_manager=fc)
    np.random.RandomState(0)
    (X, y, w, merges) = g_train.learn_agglomerate(gt_train, fc, random_state=0)[0]
    X_expected, y_expected = load_training_data(
            os.path.join(rundir, 'example-data/train-set-4.npz'))
    assert_allclose(X, X_expected, atol=1e-6)
    assert_allclose(y, y_expected, atol=1e-6)

def test_segment_with_classifier_4_channel():
    if PYTHON_VERSION == 2:
        rf = classify.load_classifier(
            os.path.join(rundir, 'example-data/rf-4.joblib'))
    else:
        fn = os.path.join(rundir, 'example-data/rf4-py3.joblib')
        with tar_extract(fn) as fn:
            rf = joblib.load(fn)
    learned_policy = agglo.classifier_probability(fc, rf)
    g_test = agglo.Rag(ws_test, p4_test, learned_policy, feature_manager=fc)
    g_test.agglomerate(0.5)
    seg_test = g_test.get_segmentation()
    seg_expected = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg-4.lzf.h5'))
    assert_allclose(ev.vi(seg_test, seg_expected), 0.0)

def test_split_vi():
    seg_test1 = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg1.lzf.h5'))
    seg_test4 = imio.read_h5_stack(
            os.path.join(rundir, 'example-data/test-seg4.lzf.h5'))
    result = np.vstack((
        ev.split_vi(ws_test, gt_test),
        ev.split_vi(seg_test1, gt_test),
        ev.split_vi(seg_test4, gt_test)
        ))
    expected = np.load(os.path.join(rundir, 'example-data/vi-results.npy'))
    assert_allclose(result, expected, atol=1e-6)

if __name__ == '__main__':
    np.random.RandomState(0)
    from numpy import testing
    testing.run_module_suite()
<|endoftext|>


<commit_before>from __future__ import unicode_literals

import unittest

import mock

from mopidy_alarmclock import http

class HttpTest(unittest.TestCase):

    @mock.patch('mopidy_alarmclock.http.tornado')
    def test_SetAlarmRequestHandler(self, x):
        config = mock.Mock()
        core = mock.Mock()
        alarm_manager = mock.Mock()
        msg_store = mock.Mock()

        handler = http.SetAlarmRequestHandler()
        handler.initialize(config, core, alarm_manager, msg_store)
        handler.get_argument = mock.Mock()
        handler.get_argument.side_effect = lambda v: {'playlist': 'Playlist URI', 'time': '8:00', 'random': '1', 'volume': '81', 'incsec': '23'}[v]
        handler.post()

        self.assertEqual(alarm_manager.set_alarm.call_count, 1)
        self.assertEqual(alarm_manager.set_alarm.call_args[0][1], core.playlists.lookup('Playlist URI').get())
        self.assertEqual(alarm_manager.set_alarm.call_args[0][2], True)
        self.assertEqual(alarm_manager.set_alarm.call_args[0][3], 81)
        self.assertEqual(alarm_manager.set_alarm.call_args[0][4], 23)

    # TODO Use Tornado unit testing
    # TODO Write more (granular + comprehensive) tests
<commit_msg>http tests: implementing<commit_after>from __future__ import unicode_literals

import unittest

import mock

from mopidy_alarmclock import http

class HttpTest(unittest.TestCase):

    def test_SetAlarmRequestHandler(self):
        config = mock.Mock()
        core = mock.Mock()
        alarm_manager = mock.Mock()
        msg_store = mock.Mock()

        patcher = mock.patch.object(http.SetAlarmRequestHandler, '__bases__', (mock.Mock,))
        with patcher:
            with patcher:
            handler = http.SetAlarmRequestHandler()

        handler.initialize(config, core, alarm_manager, msg_store)
        handler.get_argument = mock.Mock()
        handler.get_argument.side_effect = lambda v: {'playlist': 'Playlist URI', 'time': '8:00', 'random': '1', 'volume': '81', 'incsec': '23'}[v]
        handler.post()

        self.assertEqual(alarm_manager.set_alarm.call_count, 1)
        self.assertEqual(alarm_manager.set_alarm.call_args[0][1], core.playlists.lookup('Playlist URI').get())
        self.assertEqual(alarm_manager.set_alarm.call_args[0][2], True)
        self.assertEqual(alarm_manager.set_alarm.call_args[0][3], 81)
        self.assertEqual(alarm_manager.set_alarm.call_args[0][4], 23)

    # TODO Use Tornado unit testing
    # TODO Write more (granular + comprehensive) tests
<|endoftext|>


<commit_before>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Basic Google Account and User (Model) query functions.
"""

__authors__ = [
  '"Chen Lunpeng" <forever.clp@gmail.com>',
  '"Todd Larsen" <tlarsen@google.com>',
  ]

from google.appengine.api import users

from soc.logic import models

import soc.models.user
import soc.logic.models.user

def isDeveloper(account=None):
  """Returns True if a Google Account is a Developer with special privileges.

  Since it only works on the current logged-in user, if account matches the
  current logged-in Google Account, the App Engine Users API function
  user.is_current_user_admin() is checked.  If that returns False, or
  account is not the currently logged-in user, the is_developer property of
  the User entity corresponding to the Google Account is checked next.

  This solves the "chicken-and-egg" problem of no User entity having its
  is_developer property set, but no one being able to set it.

  Args:
    account: a Google Account (users.User) object; if not supplied,
      the current logged-in user is checked
  """

  # Get the currently logged in user
  current = users.get_current_user()

  if not (account or current):
    # no Google Account was supplied or is logged in, so an unspecified
    # User is definitely *not* a Developer
    return False

  if (((not account) or (account == current))
      and users.is_current_user_admin()):
    # no account supplied, or current logged-in user, and that user is in the
    # Administration->Developers list in the App Engine console
    return True

  if not account:
    account = current

  user = models.user.logic.getForFields({'account': account}, unique=True)

  if not user:
    # no User entity for this Google Account, and account is not the
    # currently logged-in user, so there is no conclusive way to check the
    # Administration->Developers list in the App Engine console
    return False

  return user.is_developer

def isAccountAvailable(new_account,
                       existing_user=None, existing_key_name=None):
  """Returns True if Google Account is available for use by existing User.

  Args:
    new_account: a Google Account (users.User) object with a (possibly)
      new email
    existing_user: an existing User entity; default is None, in which case
      existing_key_name is used to look up the User entity
    existing_key_name: the key_name of an existing User entity, used
      when existing_user is not supplied; default is None
  """
  if not existing_user:
    existing_user = models.user.logic.getFromKeyName(existing_key_name)

  if existing_user:
    old_email = existing_user.account.email()
  else:
    old_email = None

  if new_account.email().lower() == old_email.lower():
    # "new" email is same as existing User wanting it, so it is "available"
    return True
  # else: "new" email truly is new to the existing User, so keep checking

  if not models.user.logic.getForFields({'account': new_account},
                                        unique=True):
    # new email address also does not belong to any other User,
    # so it is available
    return True

  # email does not already belong to this User, but to some other User
  return False
<commit_msg>Allows a user object to be passed to accounts.isDeveloper<commit_after>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Basic Google Account and User (Model) query functions.
"""

__authors__ = [
  '"Chen Lunpeng" <forever.clp@gmail.com>',
  '"Todd Larsen" <tlarsen@google.com>',
  ]

from google.appengine.api import users

from soc.logic import models

import soc.models.user
import soc.logic.models.user

def isDeveloper(account=None, user=None):
  """Returns True if a Google Account is a Developer with special privileges.

  Since it only works on the current logged-in user, if account matches the
  current logged-in Google Account, the App Engine Users API function
  user.is_current_user_admin() is checked.  If that returns False, or
  account is not the currently logged-in user, the is_developer property of
  the User entity corresponding to the Google Account is checked next.

  This solves the "chicken-and-egg" problem of no User entity having its
  is_developer property set, but no one being able to set it.

  Args:
    account: a Google Account (users.User) object; if not supplied,
      the current logged-in user is checked
  """

  if user and (not account):
    account = user.account

  # Get the currently logged in user
  current = users.get_current_user()

  if not (account or current):
    # no Google Account was supplied or is logged in, so an unspecified
    # User is definitely *not* a Developer
    return False

  if (((not account) or (account == current))
      and users.is_current_user_admin()):
    # no account supplied, or current logged-in user, and that user is in the
    # Administration->Developers list in the App Engine console
    return True

  if not account:
    account = current

  if not user:
    user = models.user.logic.getForFields({'account': account}, unique=True)

  if not user:
    # no User entity for this Google Account, and account is not the
    # currently logged-in user, so there is no conclusive way to check the
    # Administration->Developers list in the App Engine console
    return False

  return user.is_developer

def isAccountAvailable(new_account,
                       existing_user=None, existing_key_name=None):
  """Returns True if Google Account is available for use by existing User.

  Args:
    new_account: a Google Account (users.User) object with a (possibly)
      new email
    existing_user: an existing User entity; default is None, in which case
      existing_key_name is used to look up the User entity
    existing_key_name: the key_name of an existing User entity, used
      when existing_user is not supplied; default is None
  """
  if not existing_user:
    existing_user = models.user.logic.getFromKeyName(existing_key_name)

  if existing_user:
    old_email = existing_user.account.email()
  else:
    old_email = None

  if new_account.email().lower() == old_email.lower():
    # "new" email is same as existing User wanting it, so it is "available"
    return True
  # else: "new" email truly is new to the existing User, so keep checking

  if not models.user.logic.getForFields({'account': new_account},
                                        unique=True):
    # new email address also does not belong to any other User,
    # so it is available
    return True

  # email does not already belong to this User, but to some other User
  return False
<|endoftext|>


<commit_before>import pytest
from knot_a_rumor.knot import Knot
from knot_a_rumor.story import Story, Scene
from os import getcwd
from os.path import join

@pytest.mark.usefixtures("prepare_knot")
class TestKnot:
    @pytest.fixture()
    def prepare_knot(self):
        self.path = join(getcwd(), "tests", "fixtures", "library")
        self.library = Knot(self.path)

    def set_location(self, player_state, x, y):
        player_state["location"]["x"] = x
        player_state["location"]["y"] = y
        return player_state

    def test_knot_will_accept_story_path(self):
        assert self.library.path == self.path

    def test_knot_will_list_available_stories(self):
        stories = self.library.stories()
        assert "basic" in stories
        assert "test" in stories

    def test_knot_get_story_returns_story(self):
        story = self.library.get_story("basic")
        assert type(story) is Story

    def test_build_story_path_includes_story_name(self):
        path = self.library.build_story_path("wonky")
        assert "wonky" in path

    def test_init_story_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        assert type(player_state) is dict

    def test_play_accepts_and_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        assert type(player_state) is dict

    def test_narrate_returns_a_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        description = self.library.narrate(player_state)
        assert "This is a basic description of a basic scene" == description

    def test_narrate_returns_a_positional_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        player_state = self.set_location(player_state, 3, 2)
        description = self.library.narrate(player_state)
        assert "You are at the upper right." == description

    def test_player_state_contains_the_current_scene(self):
        player_state = self.library.init_story("basic")
        assert "flourescent" == player_state["current_scene"]

    def test_player_state_contains_location(self):
        player_state = self.library.init_story("basic")
        assert "location" in player_state.keys()
        assert "x" in player_state["location"].keys()
        assert "y" in player_state["location"].keys()

    def test_player_state_contains_the_current_story(self):
        player_state = self.library.init_story("basic")
        assert "basic" == player_state["story"]

    def test_player_state_contains_the_number_of_turns_played(self):
        player_state = self.library.init_story("basic")
        assert 0 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 1 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 2 == player_state["turn"]

    def test_load_scene_returns_scene(self):
        player_state = self.library.init_story("basic")
        assert type(self.library.load_scene(player_state)) == Scene

    def test_can_return_scene_map(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        scene_map = self.library.scene_map(player_state)
        assert "####" in scene_map

    def test_play_will_update_the_starting_location(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.play(player_state)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x != old_x
        assert new_y != old_y

    def test_move_will_move_a_player_north(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "n")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x
        assert new_y == old_y + 1

    def test_move_will_move_a_player_south(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 2

        player_state = self.library.move(player_state, "s")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 3
        assert new_y == 1

    def test_move_will_move_a_player_east(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 1
        assert new_y == old_y

    def test_move_will_move_a_player_west(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 0

        player_state = self.library.move(player_state, "w")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 2
        assert new_y == 0

    def test_move_accepts_an_optional_repetiton(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e", 2)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 2
        assert new_y == old_y

    def test_player_can_look_to_see_items_on_map(self):
        state = self.library.init_story("basic")
        state = self.library.play(state)
        scene_map = self.library.scene_map(state)
        assert "#@d#" not in scene_map
        assert 1 not in state["seen"]

        state, seen = self.library.look(state)
        assert "dog" in seen
        assert 1 in state["seen"]

        scene_map = self.library.scene_map(state)
        assert "#@d#" in scene_map

<commit_msg>Adds test fix for Python 2.7<commit_after>import pytest
from knot_a_rumor.knot import Knot
from knot_a_rumor.story import Story, Scene
from os import getcwd
from os.path import join

@pytest.mark.usefixtures("prepare_knot")
class TestKnot:
    @pytest.fixture()
    def prepare_knot(self):
        self.path = join(getcwd(), "tests", "fixtures", "library")
        self.library = Knot(self.path)

    def set_location(self, player_state, x, y):
        player_state["location"]["x"] = x
        player_state["location"]["y"] = y
        return player_state

    def test_knot_will_accept_story_path(self):
        assert self.library.path == self.path

    def test_knot_will_list_available_stories(self):
        stories = self.library.stories()
        assert "basic" in stories
        assert "test" in stories

    def test_knot_get_story_returns_story(self):
        story = self.library.get_story("basic")
        assert isinstance(story, Story)

    def test_build_story_path_includes_story_name(self):
        path = self.library.build_story_path("wonky")
        assert "wonky" in path

    def test_init_story_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        assert type(player_state) is dict

    def test_play_accepts_and_returns_a_dict(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        assert type(player_state) is dict

    def test_narrate_returns_a_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        description = self.library.narrate(player_state)
        assert "This is a basic description of a basic scene" == description

    def test_narrate_returns_a_positional_description_of_the_scene(self):
        player_state = self.library.init_story("basic")
        player_state = self.set_location(player_state, 3, 2)
        description = self.library.narrate(player_state)
        assert "You are at the upper right." == description

    def test_player_state_contains_the_current_scene(self):
        player_state = self.library.init_story("basic")
        assert "flourescent" == player_state["current_scene"]

    def test_player_state_contains_location(self):
        player_state = self.library.init_story("basic")
        assert "location" in player_state.keys()
        assert "x" in player_state["location"].keys()
        assert "y" in player_state["location"].keys()

    def test_player_state_contains_the_current_story(self):
        player_state = self.library.init_story("basic")
        assert "basic" == player_state["story"]

    def test_player_state_contains_the_number_of_turns_played(self):
        player_state = self.library.init_story("basic")
        assert 0 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 1 == player_state["turn"]
        player_state = self.library.move(player_state, "e")
        assert 2 == player_state["turn"]

    def test_load_scene_returns_scene(self):
        player_state = self.library.init_story("basic")
        assert isinstance(self.library.load_scene(player_state), Scene)

    def test_can_return_scene_map(self):
        player_state = self.library.init_story("basic")
        player_state = self.library.play(player_state)
        scene_map = self.library.scene_map(player_state)
        assert "####" in scene_map

    def test_play_will_update_the_starting_location(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.play(player_state)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x != old_x
        assert new_y != old_y

    def test_move_will_move_a_player_north(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "n")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x
        assert new_y == old_y + 1

    def test_move_will_move_a_player_south(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 2

        player_state = self.library.move(player_state, "s")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 3
        assert new_y == 1

    def test_move_will_move_a_player_east(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 1
        assert new_y == old_y

    def test_move_will_move_a_player_west(self):
        player_state = self.library.init_story("basic")
        player_state["location"]["x"] = 3
        player_state["location"]["y"] = 0

        player_state = self.library.move(player_state, "w")
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == 2
        assert new_y == 0

    def test_move_accepts_an_optional_repetiton(self):
        player_state = self.library.init_story("basic")
        old_x = player_state["location"]["x"]
        old_y = player_state["location"]["y"]

        player_state = self.library.move(player_state, "e", 2)
        new_x = player_state["location"]["x"]
        new_y = player_state["location"]["y"]

        assert new_x == old_x + 2
        assert new_y == old_y

    def test_player_can_look_to_see_items_on_map(self):
        state = self.library.init_story("basic")
        state = self.library.play(state)
        scene_map = self.library.scene_map(state)
        assert "#@d#" not in scene_map
        assert 1 not in state["seen"]

        state, seen = self.library.look(state)
        assert "dog" in seen
        assert 1 in state["seen"]

        scene_map = self.library.scene_map(state)
        assert "#@d#" in scene_map

<|endoftext|>


<commit_before>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generic cleaning methods
"""

__authors__ = [
    '"Todd Larsen" <tlarsen@google.com>',
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
    ]

from google.appengine.api import users

from django import forms

from soc.logic import validate
from soc.logic.models import user as user_logic

def clean_link_id(self):
  # convert to lowercase for user comfort
  link_id = self.cleaned_data.get('link_id').lower()
  if not validate.isLinkIdFormatValid(link_id):
    raise forms.ValidationError("This link ID is in wrong format.")
  return link_id

def clean_existing_user(field_name):
  """Check if the field_name field is a valid user.
  """

  def wrapped(self):
    link_id = self.cleaned_data.get(field_name).lower()

    if not validate.isLinkIdFormatValid(link_id):
      raise forms.ValidationError("This link ID is in wrong format.")

    user_entity = user_logic.logic.getForFields({'link_id' : link_id}, unique=True)

    if not user_entity:
      # user does not exist
      raise forms.ValidationError("This user does not exist.")

    return user_entity
  return wrapped

def clean_user_not_exist(field_name):
  """Check if the field_name value is a valid link_id and a user with the
     link id does not exist.
  """

  def wrapped(self):
    link_id = self.cleaned_data.get(field_name).lower()

    if not validate.isLinkIdFormatValid(link_id):
      raise forms.ValidationError("This link ID is in wrong format.")

    user_entity = user_logic.logic.getForFields({'link_id' : link_id}, unique=True)

    if user_entity:
      # user exists already
      raise forms.ValidationError("There is already a user with this link id.")

    return link_id
  return wrapped

def clean_users_not_same(field_name):
  """Check if the field_name field is a valid user and is not
     equal to the current user.
  """

  def wrapped(self):

    clean_user_field = clean_existing_user(field_name)
    user_entity = clean_user_field(self)

    current_user_entity = user_logic.logic.getForCurrentAccount()

    if user_entity.key() == current_user_entity.key():
      # users are equal
      raise forms.ValidationError("You cannot enter yourself here")

    return user_entity
  return wrapped

def clean_user_account(field_name):
  """Returns the User with the given field_name value.
  """
  def wrapped(self):
    email_adress = self.cleaned_data.get(field_name).lower()

    # get the user account for this email
    user_account = users.User(email_adress)

    return user_account
  return wrapped

def clean_user_account_not_in_use(field_name):
  """Check if the field_name value contains an email
     address that hasn't been used for an existing account.
  """

  def wrapped(self):
    email_adress = self.cleaned_data.get(field_name).lower()

    # get the user account for this email and check if it's in use
    user_account = users.User(email_adress)

    fields = {'account' : user_account}
    user_entity = user_logic.logic.getForFields(fields, unique=True)

    if user_entity or user_logic.logic.isFormerAccount(user_account):
      raise forms.ValidationError("There is already a user with this email adress.")

    return user_account
  return wrapped

def clean_feed_url(self):
  feed_url = self.cleaned_data.get('feed_url')

  if feed_url == '':
    # feed url not supplied (which is OK), so do not try to validate it
    return None

  if not validate.isFeedURLValid(feed_url):
    raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')

  return feed_url

def clean_url(field_name):
  """Clean method for cleaning a field belonging to a LinkProperty.
  """

  def wrapped(self):

    value = self.cleaned_data.get(field_name)

    # LinkProperty does not accept the empty string so we must return None
    if not value or value == u'':
      return None

    # call the Django URLField cleaning method to properly clean/validate this field
    return forms.URLField.clean(self.fields[field_name], value)
  return wrapped

def validate_user_edit(link_id_field, account_field):
  """Clean method for cleaning user edit form.

  Raises ValidationError if:
    -Another User has the given email address as account
    -Another User has the given email address in it's FormerAccounts list
  """
  def wrapper(self):
    cleaned_data = self.cleaned_data

    link_id = cleaned_data.get(link_id_field)
    user_account = cleaned_data.get(account_field)

    # if both fields were valid do this check
    if link_id and user_account:
      # get the user from the link_id in the form
      fields = {'link_id': link_id}
      user_entity = user_logic.logic.getForFields(fields, unique=True)

      former_accounts = user_entity.former_accounts

      # if it's not the user's current account or one of his former accounts
      if (user_entity.account != user_account  and
          user_account not in former_accounts):

        # get the user having the given account
        fields = {'account': user_account}
        user_from_account_entity = user_logic.logic.getForFields(fields, unique=True)

        # if there is a user with the given account or it's a former account
        if user_from_account_entity or user_logic.logic.isFormerAccount(user_account):
          # raise an error because this email address can't be used
            raise forms.ValidationError("There is already a user with this email adress.")

    return cleaned_data
  return wrapper

<commit_msg>Style fixes in soc.logic.cleaning module.<commit_after>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generic cleaning methods.
"""

__authors__ = [
    '"Todd Larsen" <tlarsen@google.com>',
    '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    '"Lennard de Rijk" <ljvderijk@gmail.com>',
    ]

from google.appengine.api import users

from django import forms

from soc.logic import validate
from soc.logic.models import user as user_logic

def clean_link_id(self):
  # convert to lowercase for user comfort
  link_id = self.cleaned_data.get('link_id').lower()
  if not validate.isLinkIdFormatValid(link_id):
    raise forms.ValidationError("This link ID is in wrong format.")
  return link_id

def clean_existing_user(field_name):
  """Check if the field_name field is a valid user.
  """

  def wrapped(self):
    link_id = self.cleaned_data.get(field_name).lower()

    if not validate.isLinkIdFormatValid(link_id):
      raise forms.ValidationError("This link ID is in wrong format.")

    user_entity = user_logic.logic.getForFields({'link_id': link_id},
        unique=True)

    if not user_entity:
      # user does not exist
      raise forms.ValidationError("This user does not exist.")

    return user_entity
  return wrapped

def clean_user_not_exist(field_name):
  """Check if the field_name value is a valid link_id and a user with the
     link id does not exist.
  """

  def wrapped(self):
    link_id = self.cleaned_data.get(field_name).lower()

    if not validate.isLinkIdFormatValid(link_id):
      raise forms.ValidationError("This link ID is in wrong format.")

    user_entity = user_logic.logic.getForFields({'link_id': link_id},
        unique=True)

    if user_entity:
      # user exists already
      raise forms.ValidationError("There is already a user with this link id.")

    return link_id
  return wrapped

def clean_users_not_same(field_name):
  """Check if the field_name field is a valid user and is not
     equal to the current user.
  """

  def wrapped(self):

    clean_user_field = clean_existing_user(field_name)
    user_entity = clean_user_field(self)

    current_user_entity = user_logic.logic.getForCurrentAccount()

    if user_entity.key() == current_user_entity.key():
      # users are equal
      raise forms.ValidationError("You cannot enter yourself here.")

    return user_entity
  return wrapped

def clean_user_account(field_name):
  """Returns the User with the given field_name value.
  """
  def wrapped(self):
    email_adress = self.cleaned_data.get(field_name).lower()

    # get the user account for this email
    user_account = users.User(email_adress)

    return user_account
  return wrapped

def clean_user_account_not_in_use(field_name):
  """Check if the field_name value contains an email
     address that hasn't been used for an existing account.
  """

  def wrapped(self):
    email_adress = self.cleaned_data.get(field_name).lower()

    # get the user account for this email and check if it's in use
    user_account = users.User(email_adress)

    fields = {'account': user_account}
    user_entity = user_logic.logic.getForFields(fields, unique=True)

    if user_entity or user_logic.logic.isFormerAccount(user_account):
      raise forms.ValidationError("There is already a user with this email adress.")

    return user_account
  return wrapped

def clean_feed_url(self):
  feed_url = self.cleaned_data.get('feed_url')

  if feed_url == '':
    # feed url not supplied (which is OK), so do not try to validate it
    return None

  if not validate.isFeedURLValid(feed_url):
    raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.')

  return feed_url

def clean_url(field_name):
  """Clean method for cleaning a field belonging to a LinkProperty.
  """

  def wrapped(self):

    value = self.cleaned_data.get(field_name)

    # LinkProperty does not accept the empty string so we must return None
    if not value or value == u'':
      return None

    # call the Django URLField cleaning method to properly clean/validate this field
    return forms.URLField.clean(self.fields[field_name], value)
  return wrapped

def validate_user_edit(link_id_field, account_field):
  """Clean method for cleaning user edit form.

  Raises ValidationError if:
    -Another User has the given email address as account
    -Another User has the given email address in it's FormerAccounts list
  """
  def wrapper(self):
    cleaned_data = self.cleaned_data

    link_id = cleaned_data.get(link_id_field)
    user_account = cleaned_data.get(account_field)

    # if both fields were valid do this check
    if link_id and user_account:
      # get the user from the link_id in the form
      fields = {'link_id': link_id}
      user_entity = user_logic.logic.getForFields(fields, unique=True)

      former_accounts = user_entity.former_accounts

      # if it's not the user's current account or one of his former accounts
      if (user_entity.account != user_account  and
          user_account not in former_accounts):

        # get the user having the given account
        fields = {'account': user_account}
        user_from_account_entity = user_logic.logic.getForFields(fields,
            unique=True)

        # if there is a user with the given account or it's a former account
        if user_from_account_entity or user_logic.logic.isFormerAccount(user_account):
          # raise an error because this email address can't be used
          raise forms.ValidationError("There is already a user with this email adress.")

    return cleaned_data
  return wrapper

<|endoftext|>


<commit_before>#SPARQL_BAMS_Store_Persist_Example.py
#This program is used to open up BAMS data, persist it to a store for speed up of querying

#For Parsing
import rdflib
from rdflib import plugin

#for getting the length of the files
import os

#for working with tempfiles
import os.path as op
import tempfile

#For Unzipping
import zipfile
from StringIO import StringIO

plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')

zipdata = StringIO()

# open the file using a relative path
r = open("../Data/BAMS1.zip")

# zipdata is a buffer holding the contents of the zip file in memory
zipdata.write(r.read())

print("~40 seconds for zip to open...")

#myzipfile opens the contents of the zip file as an object that knows how to unzip
myzipfile = zipfile.ZipFile(zipdata)

#grab the contents out of myzipfile by name
foofile = myzipfile.open('bams_ontology_2013-07-10_03-20-00.xml')

print("loading up the BAMS file in memory...")

#Get a Graph object using a Sleepycat persistent store
g = rdflib.Graph('Sleepycat',identifier='BAMS')

# first time create the store
#  put the store in a temp directory so it doesn't get confused with stuff we should commit
tempStore = op.join( tempfile.gettempdir(), 'myRDF_BAMS_Store')
g.open(tempStore, create = True)

#pull in the BAMS RDF document, parse, and store.
result = g.parse(file=myzipfile.open('bams_ontology_2013-07-10_03-20-00.xml'), format="application/rdf+xml")

foofile.close()

# when done!
g.close()

print("Graph stored to disk")<commit_msg>Storing RDF to a backend that persists and querying from it<commit_after>#SPARQL_BAMS_Store_Persist_Example.py
#This program is used to open up BAMS data, persist it to a store for speed up of querying

#For Parsing
import rdflib
from rdflib import plugin

#for getting the length of the files
import os

#for working with tempfiles
import os.path as op
import tempfile

#For Unzipping
import zipfile
from StringIO import StringIO

plugin.register(
    'sparql', rdflib.query.Processor,
    'rdfextras.sparql.processor', 'Processor')
plugin.register(
    'sparql', rdflib.query.Result,
    'rdfextras.sparql.query', 'SPARQLQueryResult')

zipdata = StringIO()

# open the file using a relative path
#r = open("../Data/BAMS1.zip")

# adding the BAMS Thesaurus instead of the more limited set of data:
r = open("../Data/bams_thesaurus_2013-09-24_17-12-40.xml.zip")

# zipdata is a buffer holding the contents of the zip file in memory
zipdata.write(r.read())

print("~40 seconds for zip to open...")

#myzipfile opens the contents of the zip file as an object that knows how to unzip
myzipfile = zipfile.ZipFile(zipdata)

#grab the contents out of myzipfile by name
#foofile = myzipfile.open('bams_ontology_2013-07-10_03-20-00.xml')

#changing the foofile to be the file we upen above^^^^^ in r = open()....etc.
foofile = myzipfile.open('bams_thesaurus_2013-09-24_17-12-40.xml')

print("loading up the BAMS file in memory...")

#Get a Graph object using a Sleepycat persistent store
g = rdflib.Graph('Sleepycat',identifier='BAMS')

# first time create the store
#  put the store in a temp directory so it doesn't get confused with stuff we should commit
tempStore = op.join( tempfile.gettempdir(), 'myRDF_BAMS_Store')
g.open(tempStore, create = True)

#pull in the BAMS RDF document, parse, and store.
result = g.parse(file=myzipfile.open('bams_ontology_2013-07-10_03-20-00.xml'), format="application/rdf+xml")

foofile.close()

# when done!
g.close()

print("Graph stored to disk")<|endoftext|>


<commit_before>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Functions for composing Model entity key names.
"""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
  ]

import soc.models.document
import soc.models.site_settings
import soc.models.user

def nameSiteSettings(path):
  """Returns a SiteSettings key name constructed from a supplied path."""
  return '%(type)s:%(path)s' % {
      'type': soc.models.site_settings.SiteSettings.__name__,
      'path': path,
      }

def nameDocument(path, link_name):
  """Returns a Document key name constructed from a path and link name."""
  return '%(type)s:%(path)s/%(link_name)s' % {
      'type': soc.models.document.Document.__name__,
      'path': path,
      'link_name': link_name,
      }

def nameUser(email):
  """Returns a User key name constructed from a supplied email address."""
  return '%(type)s:%(email)s' % {
      'type': soc.models.user.User.__name__,
      'email': email,
      }

<commit_msg>Add key naming function for Sponsor, School, Organization, Club key_names.<commit_after>#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Functions for composing Model entity key names.
"""

__authors__ = [
  '"Todd Larsen" <tlarsen@google.com>',
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
  ]

import soc.models.club
import soc.models.group
import soc.models.organization
import soc.models.school
import soc.models.sponsor
import soc.models.document
import soc.models.site_settings
import soc.models.user

def nameSiteSettings(path):
  """Returns a SiteSettings key name constructed from a supplied path."""
  return '%(type)s:%(path)s' % {
      'type': soc.models.site_settings.SiteSettings.__name__,
      'path': path,
      }

def nameDocument(path, link_name):
  """Returns a Document key name constructed from a path and link name."""
  return '%(type)s:%(path)s/%(link_name)s' % {
      'type': soc.models.document.Document.__name__,
      'path': path,
      'link_name': link_name,
      }

def nameUser(email):
  """Returns a User key name constructed from a supplied email address."""
  return '%(type)s:%(email)s' % {
      'type': soc.models.user.User.__name__,
      'email': email,
      }

def nameSponsor(link_name):
  """Returns a Sponsor key name constructed from a supplied link name."""
  return '%(group)s/%(type)s:%(link_name)s' % {
      'group': soc.models.group.Group.__name__,
      'type': soc.models.sponsor.Sponsor.__name__,
      'link_name': link_name,
      }

def nameSchool(sponsor_ln, program_ln, link_name):
  """Returns a School key name constructed from a supplied sponsor,
     program and school link names.
  """
  return '%(group)s/%(type)s:%(sponsor)s/%(program)s/%(link_name)s' % {
      'group': soc.models.group.Group.__name__,
      'type': soc.models.school.School.__name__,
      'sponsor': sponsor_ln,
      'program': program_ln,
      'link_name': link_name,
      }

def nameOrganization(sponsor_ln, program_ln, link_name):
  """Returns a Organization key name constructed from a supplied sponsor,
     program and organization link names.
  """
  return '%(group)s/%(type)s:%(sponsor)s/%(program)s/%(link_name)s' % {
      'group': soc.models.group.Group.__name__,
      'type': soc.models.organization.Organization.__name__,
      'sponsor': sponsor_ln,
      'program': program_ln,
      'link_name': link_name,
      }

def nameClub(link_name):
  """Returns a Club key name constructed from a supplied link name."""
  return '%(group)s/%(type)s:%(link_name)s' % {
      'group': soc.models.group.Group.__name__,
      'type': soc.models.sponsor.Sponsor.__name__,
      'link_name': link_name,
      }
  <|endoftext|>