Created
December 22, 2011 16:21
-
-
Save turicas/1510860 to your computer and use it in GitHub Desktop.
Python class with dict-like get/set item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
class AttrDict(object): | |
def __init__(self, init=None): | |
if init is not None: | |
self.__dict__.update(init) | |
def __getitem__(self, key): | |
return self.__dict__[key] | |
def __setitem__(self, key, value): | |
self.__dict__[key] = value | |
def __delitem__(self, key): | |
del self.__dict__[key] | |
def __contains__(self, key): | |
return key in self.__dict__ | |
def __len__(self): | |
return len(self.__dict__) | |
def __repr__(self): | |
return repr(self.__dict__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import unittest | |
from attrdict import AttrDict | |
class TestAttrDict(unittest.TestCase): | |
def test_should_init_with_one_dict(self): | |
my_dict = AttrDict({'eggs': 42, 'spam': 'ham'}) | |
self.assertEquals(my_dict.eggs, 42) | |
self.assertEquals(my_dict['eggs'], 42) | |
self.assertEquals(my_dict.spam, 'ham') | |
self.assertEquals(my_dict['spam'], 'ham') | |
def test_should_not_change_values_by_inited_dict(self): | |
base = {'eggs': 42, 'spam': 'ham'} | |
my_dict = AttrDict(base) | |
base['eggs'] = 123 | |
self.assertEquals(my_dict.eggs, 42) | |
self.assertEquals(my_dict['eggs'], 42) | |
self.assertEquals(my_dict.spam, 'ham') | |
self.assertEquals(my_dict['spam'], 'ham') | |
def test_get_item(self): | |
my_dict = AttrDict() | |
my_dict.test = 123 | |
self.assertEquals(my_dict.test, 123) | |
self.assertEquals(my_dict['test'], 123) | |
def test_set_item(self): | |
my_dict = AttrDict() | |
my_dict['test'] = 123 | |
self.assertEquals(my_dict['test'], 123) | |
self.assertEquals(my_dict.test, 123) | |
def test_del_attr(self): | |
my_dict = AttrDict() | |
my_dict['test'] = 123 | |
my_dict['python'] = 42 | |
del my_dict['test'] | |
del my_dict.python | |
with self.assertRaises(KeyError): | |
temp = my_dict['test'] | |
with self.assertRaises(AttributeError): | |
temp = my_dict.python | |
def test_in_should_work_like_in_dict(self): | |
my_dict = AttrDict() | |
my_dict['test'] = 123 | |
self.assertIn('test', my_dict) | |
self.assertNotIn('bla', my_dict) | |
def test_len_should_work_like_in_dict(self): | |
my_dict = AttrDict() | |
my_dict['test'] = 123 | |
my_dict.python = 42 | |
self.assertEquals(len(my_dict), 2) | |
def test_repr(self): | |
my_dict = AttrDict() | |
my_dict['test'] = 123 | |
my_dict.python = 42 | |
self.assertEquals(repr(my_dict),"{'test': 123, 'python': 42}") | |
def test_equal(self): | |
my_dict = AttrDict({'test': 123}) | |
my_dict_2 = AttrDict({'test': 123}) | |
self.assertEquals(my_dict, my_dict_2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@yunake
s = Struct ()
s['jam'] = 'jelly' <------------------ fails , how does it act like a dictionary ?