Created
October 12, 2015 20:43
-
-
Save jfburkhart/ef8b9e4e516c543bc907 to your computer and use it in GitHub Desktop.
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 Structure(dict, object): | |
""" A ditionary that acts like a matlab structure. | |
""" | |
def __getattr__(self, attr): | |
# Fake a __getstate__ method that returns None | |
if attr == "__getstate__": | |
return lambda: None | |
try: | |
return self[attr][0] | |
except: | |
return self[attr] | |
def __setattr__(self, attr, value): | |
self[attr] = value | |
def __dir__(self): | |
""" necessary for Ipython tab-completion """ | |
return self.keys() | |
def set_with_dict(self, D): | |
""" set attributes with a dict """ | |
for k in D.keys(): | |
self.__setattr__(k, D[k]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment