Last active
June 28, 2018 12:19
-
-
Save RMuskovets/3162aba6500d60e46bc76a13cb00a8ea to your computer and use it in GitHub Desktop.
A Python module that includes extended dict
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
class iDict(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 __add__(self, other): | |
if type(other) == type(self.__dict__) or type(other) == type(self): | |
for key in other.keys(): | |
self[key] = other[key] | |
elif type(other) == type([]): | |
for (a, b) in other: | |
self[a] = b | |
return self | |
def __sub__(self, other): | |
if type(other) in [type(x) for x in self.keys()]: | |
del self[other] | |
elif type(other) == type(self.__dict__) or type(other) == type(self): | |
for key in other.keys(): | |
del self[key] | |
return self | |
def __lt__(self, other): | |
return len(self.keys()) < len(other.keys()) | |
def __gt__(self, other): | |
return len(self.keys()) > len(other.keys()) | |
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__)) | |
dict = iDict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment