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'])
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)
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)
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'])
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'])
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
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)
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())