Skip to content

Instantly share code, notes, and snippets.

@jfburkhart
Created October 12, 2015 20:43
Show Gist options
  • Save jfburkhart/ef8b9e4e516c543bc907 to your computer and use it in GitHub Desktop.
Save jfburkhart/ef8b9e4e516c543bc907 to your computer and use it in GitHub Desktop.
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