Last active
November 24, 2016 06:47
-
-
Save christabor/a08762ac6e9b56215113a27f43827839 to your computer and use it in GitHub Desktop.
kinda sorta numpy style array and also 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 SuperDict(): | |
def __init__(self, **kwargs): | |
self.items = dict(**kwargs) | |
def __getitem__(self, key): | |
if isinstance(key, tuple): | |
_curr = self.items | |
for num in key: | |
_curr = _curr[num] | |
return _curr | |
return self.items[key] | |
class SuperList(): | |
def __init__(self, args): | |
self.items = list(args) | |
def __getitem__(self, key): | |
if isinstance(key, tuple): | |
_curr = self.items | |
for num in key: | |
_curr = _curr[num] | |
return _curr | |
return self.items[key] | |
if __name__ == '__main__': | |
sup = SuperList([ | |
[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3], [1, 2, 3]]]) | |
res = sup[1, 2, 2] | |
print('res', res) | |
sup = SuperDict(foo1=dict(bar1=dict(foo2='bar2'))) | |
res = sup['foo1', 'bar1', 'foo2'] | |
print('res', res) | |
assert res == 'bar2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment