From 00c22d465099a2709306a35faa4136678cb521b1 Mon Sep 17 00:00:00 2001 From: gmatteo Date: Sun, 26 Oct 2014 11:29:45 +0100 Subject: [PATCH] Documentation for MongoDict --- monty/collections.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/monty/collections.py b/monty/collections.py index 7f4e91523..69a6bf805 100644 --- a/monty/collections.py +++ b/monty/collections.py @@ -47,12 +47,10 @@ class AttrDict(dict): to the traditional way obj['foo']" Example: - >> d = AttrDict(foo=1, bar=2) - >> d["foo"] == d.foo - True - >> d.bar = "hello" - >> d.bar - 'hello' + >>> d = AttrDict(foo=1, bar=2) + >>> assert d["foo"] == d.foo + >>> d.bar = "hello" + >>> assert d.bar == "hello" """ def __init__(self, *args, **kwargs): super(AttrDict, self).__init__(*args, **kwargs) @@ -89,16 +87,23 @@ def __setattr__(self, name, value): class MongoDict(object): """ + This dict-like object allows one to access the entries in a nested dict as attributes. + Entries (attributes) cannot be modified. It also provides Ipython tab completion hence this object + is particularly useful if you need to analyze a nested dict interactively (e.g. documents + extracted from a MongoDB database). + >>> m = MongoDict({'a': {'b': 1}, 'x': 2}) >>> assert m.a.b == 1 and m.x == 2 >>> assert "a" in m and "b" in m.a - NB: Cannot inherit from ABC collections.Mapping because otherwise + .. note:: + + Cannot inherit from ABC collections.Mapping because otherwise dict.keys and dict.items will pollute the namespace. e.g MongoDict({"keys": 1}).keys would be the ABC dict method. """ - def __init__(self, mongo_dict): - self.__dict__["_mongo_dict_"] = mongo_dict + def __init__(self, *args, **kwargs): + self.__dict__["_mongo_dict_"] = dict(*args, **kwargs) def __repr__(self): return str(self)