Skip to content

Instantly share code, notes, and snippets.

@nzjrs
Created September 7, 2011 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzjrs/1199664 to your computer and use it in GitHub Desktop.
Save nzjrs/1199664 to your computer and use it in GitHub Desktop.
Namespaced foo.bar.baz dict wrapper
import UserDict
class DotDict(UserDict.IterableUserDict):
def __init__(self):
UserDict.IterableUserDict.__init__(self)
def __setitem__(self, key, val):
bits = key.split(".")
if bits[0] not in self.data:
self.data[bits[0]] = {}
#lilypad.name
if len(bits) == 2:
self.data[bits[0]][bits[1]] = val
#lilypad.upload.protocol
elif len(bits) == 3:
if bits[1] not in self.data[bits[0]]:
self.data[bits[0]][bits[1]] = {}
self.data[bits[0]][bits[1]][bits[2]] = val
if __name__ == "__main__":
d = DotDict()
d["lilypad.upload.protocol"] = "stk500"
d["lilypad.upload.maximum_size"]=14336
d["lilypad.name"]="LilyPad Arduino"
print d.data
print d["lilypad"]["upload"]["maximum_size"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment