예제 #1
0
    def test_attr_dict_just_a_proxy(self):
        d = {'blah': 'minor', 'one': 1}
        a = AttributedDict(d)

        self.assertEqual('minor', a.blah)
        self.assertEqual(1, a.one)

        a.blah = 'shvah'
        self.assertEqual('shvah', a.blah)
        self.assertEqual('shvah', d['blah'])
예제 #2
0
    def test_attr_dict_just_a_proxy(self):
        d = {'blah': 'minor', 'one': 1}
        a = AttributedDict(d)

        self.assertEqual('minor', a.blah)
        self.assertEqual(1, a.one)

        a.blah = 'shvah'
        self.assertEqual('shvah', a.blah)
        self.assertEqual('shvah', d['blah'])
예제 #3
0
    def test_dict_proxy(self):
        d = dict(title = 'Just an Example', tags = ['test', 'python'])
        dp = AttributedDict(d)
        dp.author = dict(name = 'Alexander')
        dp.author.lastname = 'Artemenko'

        self.assertEqual('Alexander', d['author']['name'])
        self.assertEqual('Artemenko', d['author']['lastname'])

        del dp.author.lastname
        self.assertEqual(dict(name='Alexander'), dp.author)
예제 #4
0
    def test_dict_proxy(self):
        d = dict(title='Just an Example', tags=['test', 'python'])
        dp = AttributedDict(d)
        dp.author = dict(name='Alexander')
        dp.author.lastname = 'Artemenko'

        self.assertEqual('Alexander', d['author']['name'])
        self.assertEqual('Artemenko', d['author']['lastname'])

        del dp.author.lastname
        self.assertEqual(dict(name='Alexander'), dp.author)
예제 #5
0
    def test_attr_dict_returns_inner_lists_as_proxies(self):
        d = {'ddd': [{'b': 2}, {'c': 3}]}
        a = AttributedDict(d)

        a.ddd[0].b = 5
        self.assertEqual(5, a.ddd[0].b)
        self.assertEqual(5, d['ddd'][0]['b'])
예제 #6
0
    def test_attr_dict_returns_inner_dicts_as_proxies(self):
        d = {'ddd': {'b': 2, 'c': 3}}
        a = AttributedDict(d)

        a.ddd.b = 5
        self.assertEqual(5, a.ddd.b)
        self.assertEqual(5, d['ddd']['b'])
예제 #7
0
    def __getattr__(self, name):
        value = self._data.get(name, None)

        if isinstance(value, dict):
            return AttributedDict(value)

        if isinstance(value, DBRef):
            value = self.objects.dereference(value)
            self._data[name] = value

        return value
예제 #8
0
    def test_supports_iteration_by_items(self):
        d = {'ddd': {'b': 2}}
        a = AttributedDict(d)

        self.assertEqual(2, d.iteritems().next()[1]['b'])
        self.assertEqual(2, a.iteritems().next()[1].b)
예제 #9
0
    def test_supports_iteration_by_keys(self):
        d = {'ddd': {'b': 2}}
        a = AttributedDict(d)

        self.assertEqual('ddd', iter(d).next())
        self.assertEqual('ddd', iter(a).next())
예제 #10
0
    def test_supports_iteration_by_items(self):
        d = {'ddd': {'b': 2}}
        a = AttributedDict(d)

        self.assertEqual(2, d.iteritems().next()[1]['b'])
        self.assertEqual(2, a.iteritems().next()[1].b)