Beispiel #1
0
 def __init__(self, allowed_variables={}, config=None):
     self._allowed_variables = allowed_variables
     self.main_path = None
     self.all_paths = set()
     self.config = config
     self.executed_time = 0
     KeyedDefaultDict.__init__(self, self._factory)
Beispiel #2
0
 def __init__(self, allowed_variables={}, config=None):
     self._allowed_variables = allowed_variables
     self.main_path = None
     self.all_paths = set()
     self.config = config
     self.executed_time = 0
     KeyedDefaultDict.__init__(self, self._factory)
    def test_simple(self):
        original = {'foo': 1, 'bar': 2}

        test = KeyedDefaultDict(lambda x: x, original)

        self.assertEqual(original, test)

        self.assertEqual(test['foo'], 1)
Beispiel #4
0
    def test_simple(self):
        original = {"foo": 1, "bar": 2}

        test = KeyedDefaultDict(lambda x: x, original)

        self.assertEqual(original, test)

        self.assertEqual(test["foo"], 1)
Beispiel #5
0
    def __setitem__(self, key, value):
        stored_type = self._validate(key, value)

        if not isinstance(value, stored_type):
            if issubclass(stored_type, ContextDerivedValue):
                value = stored_type(self, value)
            else:
                value = stored_type(value)

        return KeyedDefaultDict.__setitem__(self, key, value)
Beispiel #6
0
    def __setitem__(self, key, value):
        stored_type = self._validate(key, value)

        if not isinstance(value, stored_type):
            if issubclass(stored_type, ContextDerivedValue):
                value = stored_type(self, value)
            else:
                value = stored_type(value)

        return KeyedDefaultDict.__setitem__(self, key, value)
Beispiel #7
0
    def update(self, iterable={}, **kwargs):
        """Like dict.update(), but using the context's setitem.

        This function is transactional: if setitem fails for one of the values,
        the context is not updated at all."""
        if isinstance(iterable, dict):
            iterable = iterable.items()

        update = {}
        for key, value in itertools.chain(iterable, kwargs.items()):
            stored_type = self._validate(key, value)
            # Don't create an instance of stored_type if coercion is needed,
            # until all values are validated.
            update[key] = (value, stored_type)
        for key, (value, stored_type) in update.items():
            if not isinstance(value, stored_type):
                update[key] = stored_type(value)
            else:
                update[key] = value
        KeyedDefaultDict.update(self, update)
Beispiel #8
0
    def update(self, iterable={}, **kwargs):
        """Like dict.update(), but using the context's setitem.

        This function is transactional: if setitem fails for one of the values,
        the context is not updated at all."""
        if isinstance(iterable, dict):
            iterable = iterable.items()

        update = {}
        for key, value in itertools.chain(iterable, kwargs.items()):
            stored_type = self._validate(key, value)
            # Don't create an instance of stored_type if coercion is needed,
            # until all values are validated.
            update[key] = (value, stored_type)
        for key, (value, stored_type) in update.items():
            if not isinstance(value, stored_type):
                update[key] = stored_type(value)
            else:
                update[key] = value
        KeyedDefaultDict.update(self, update)
    def test_defaults(self):
        test = KeyedDefaultDict(lambda x: x, {'foo': 1})

        self.assertEqual(test['foo'], 1)

        self.assertEqual(test['qux'], 'qux')

        self.assertEqual(test['bar'], 'bar')

        test['foo'] = 2
        test['qux'] = None
        test['baz'] = 'foo'

        self.assertEqual(test['foo'], 2)

        self.assertEqual(test['qux'], None)

        self.assertEqual(test['baz'], 'foo')
Beispiel #10
0
    def test_defaults(self):
        test = KeyedDefaultDict(lambda x: x, {"foo": 1})

        self.assertEqual(test["foo"], 1)

        self.assertEqual(test["qux"], "qux")

        self.assertEqual(test["bar"], "bar")

        test["foo"] = 2
        test["qux"] = None
        test["baz"] = "foo"

        self.assertEqual(test["foo"], 2)

        self.assertEqual(test["qux"], None)

        self.assertEqual(test["baz"], "foo")