public
Last active

Provides classes for a Homogeneous Mutable Mapping (HomoDict) and Homogeneous Sequence (HomoList), which allow for quick accessing of the properties and methods of the elements, as well as searching like numpy arrays.

  • Download Gist
HomogeneousCollections.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
class HomoDict(collections.MutableMapping):
'''
A Homogeneous Mutable Mapping
Provides a class for a dictionary-like object which contains
homogeneous values. Attributes of the values can be accessed through
the attributes of HomoDict. Searching is done like numpy arrays.
Initialized from a dictionary containing values of all the same type
>>> h = HomoDict({'a':Foo(...),'b': Foo(...), 'c':Foo(..)})
The individual values of `h` can be access in identical fashion to
Dictionaries.
>>> h['key']
Assuming that `Foo` has property `prop` and function `func` ...
Access elements' properties:
>>> h.prop
Access elements' functions:
>>> h.func()
Searching:
>>> h[h.prop == value]
>>> h[h.prop < value]
Multiple search:
>>> h[set(h.prop==value1) & set( h.prop2==value2)]
Combos:
>>> h[h.prop==value].func()
'''
def __init__(self, dict_):
self.store = dict(dict_)
def __eq__(self, value):
return [k for k in self.store if self.store[k] == value ]
def __ne__(self, value):
return [k for k in self.store if self.store[k] != value ]
def __gt__(self, value):
return [k for k in self.store if self.store[k] > value ]
def __ge__(self, value):
return [k for k in self.store if self.store[k] >= value ]
def __lt__(self, value):
return [k for k in self.store if self.store[k] < value ]
def __le__(self, value):
return [k for k in self.store if self.store[k] <= value ]
def __getattr__(self, name):
return self.__class__(
{k: self.store[k].__getattribute__(name) for k in self.store})
def __getitem__(self, key):
c = self.__class__({k:self.store[k] for k in key})
if len(c) == 1:
return c.store.values()[0]
else:
return c
def __call__(self, *args, **kwargs):
return self.__class__(
{k: self.store[k](*args, **kwargs) for k in self.store})
def __setitem__(self, key, value):
self.store[key] = value
 
def __delitem__(self, key):
del self.store[key]
 
def __iter__(self):
return iter(self.store)
 
def __len__(self):
return len(self.store)
 
def __str__(self):
return pprint.pformat(self.store)
def __repr__(self):
return pprint.pformat(self.store)
 
class HomoList(collections.Sequence):
'''
A Homogeneous Sequence
Provides a class for a list-like object which contains
homogeneous values. Attributes of the values can be accessed through
the attributes of HomoList. Searching is done like numpy arrays.
Initialized from a list of all the same type
>>> h = HomoDict([Foo(...), Foo(...)])
The individual values of `h` can be access in identical fashion to
Lists.
>>> h[0]
Assuming that `Foo` has property `prop` and function `func` ...
Access elements' properties:
>>> h.prop
Access elements' functions:
>>> h.func()
Searching:
>>> h[h.prop == value]
>>> h[h.prop < value]
Multiple search:
>>> h[set(h.prop==value1) & set( h.prop2==value2)]
Combos:
>>> h[h.prop==value].func()
'''
 
def __init__(self, list_):
self.store = list(list_)
def __eq__(self, value):
return [k for k in range(len(self)) if self.store[k] == value ]
def __ne__(self, value):
return [k for k in range(len(self)) if self.store[k] != value ]
def __gt__(self, value):
return [k for k in range(len(self)) if self.store[k] > value ]
def __ge__(self, value):
return [k for k in range(len(self)) if self.store[k] >= value ]
def __lt__(self, value):
return [k for k in range(len(self)) if self.store[k] < value ]
def __le__(self, value):
return [k for k in range(len(self)) if self.store[k] <= value ]
def __getattr__(self, name):
return self.__class__(
[k.__getattribute__(name) for k in self.store])
def __getitem__(self, idx):
try:
return self.store[idx]
except(TypeError):
return self.__class__([self.store[k] for k in idx])
def __call__(self, *args, **kwargs):
return self.__class__(
[k(*args,**kwargs) for k in self.store])
def __setitem__(self, idx, value):
self.store[idx] = value
 
def __delitem__(self, idx):
del self.store[idx]
 
def __iter__(self):
return iter(self.store)
 
def __len__(self):
return len(self.store)
 
def __str__(self):
return pprint.pformat(self.store)
def __repr__(self):
return pprint.pformat(self.store)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.