Commit 6c66dbd7 authored by Jameson Graef Rollins's avatar Jameson Graef Rollins

struct: add more useful dict methods

parent 3b228698
......@@ -75,6 +75,12 @@ class Struct(object):
def __init__(self, **kwargs):
"""Arguments can pre-fill the structure
def __getitem__(self, key):
"""Get a (possibly nested) value from the struct.
......@@ -85,11 +91,33 @@ class Struct(object):
return self.__dict__[key]
def __setitem__(self, item, value):
self.__dict__[item] = value
def get(self, key, default):
"""Get a (possibly nested) value from the struct, or default.
return self[key]
except KeyError:
return default
def __setitem__(self, key, value):
self.__dict__[key] = value
def setdefault(self, key, default):
return self.__dict__.setdefault(key, default)
def items(self):
return self.__dict__.items()
def keys(self):
return self.__dict__.keys()
def values(self):
return self.__dict__.values()
def __contains__(self, key):
return key in self.__dict__
def __contains__(self, item):
return item in self.__dict__
def to_dict(self, array=False):
"""Return nested dictionary representation of Struct.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment