Created
December 19, 2018 22:18
-
-
Save rpdelaney/b8d2181ef34f50f4dce8b941df03db27 to your computer and use it in GitHub Desktop.
python3 custom map object
This file contains 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 python3 | |
# | |
# from: https://stackoverflow.com/questions/4014621/a-python-class-that-acts-like-dict | |
class Mapping(dict): | |
def __setitem__(self, key, item): | |
self.__dict__[key] = item | |
def __getitem__(self, key): | |
return self.__dict__[key] | |
def __repr__(self): | |
return repr(self.__dict__) | |
def __len__(self): | |
return len(self.__dict__) | |
def __delitem__(self, key): | |
del self.__dict__[key] | |
def clear(self): | |
return self.__dict__.clear() | |
def copy(self): | |
return self.__dict__.copy() | |
def has_key(self, k): | |
return k in self.__dict__ | |
def update(self, *args, **kwargs): | |
return self.__dict__.update(*args, **kwargs) | |
def keys(self): | |
return self.__dict__.keys() | |
def values(self): | |
return self.__dict__.values() | |
def items(self): | |
return self.__dict__.items() | |
def pop(self, *args): | |
return self.__dict__.pop(*args) | |
def __cmp__(self, dict_): | |
return self.__cmp__(self.__dict__, dict_) | |
def __contains__(self, item): | |
return item in self.__dict__ | |
def __iter__(self): | |
return iter(self.__dict__) | |
def __unicode__(self): | |
return unicode(repr(self.__dict__)) | |
o = Mapping() | |
o.foo = "bar" | |
o['lumberjack'] = 'foo' | |
o.update({'a': 'b'}, c=44) | |
print 'lumberjack' in o | |
print o | |
# In [187]: run mapping.py | |
# True | |
# {'a': 'b', 'lumberjack': 'foo', 'foo': 'bar', 'c': 44} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment