monty.collections module

class AttrDict(*args, **kwargs)[source]

Bases: dict

Allows to access dict keys as obj.foo in addition to the traditional way obj[‘foo’]”

Example

>>> d = AttrDict(foo=1, bar=2)
>>> assert d["foo"] == d.foo
>>> d.bar = "hello"
>>> assert d.bar == "hello"
copy() → a shallow copy of D[source]
class FrozenAttrDict(*args, **kwargs)[source]

Bases: monty.collections.frozendict

A dictionary that:
  • does not permit changes.

  • Allows to access dict keys as obj.foo in addition to the traditional way obj[‘foo’]

class MongoDict(*args, **kwargs)[source]

Bases: 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
>>> m["a"]
{'b': 1}

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.

class Namespace(*args, **kwargs)[source]

Bases: dict

A dictionary that does not permit to redefine its keys.

update([E, ]**F) → None. Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

as_set(obj)[source]

Convert obj into a set, returns None if obj is None.

>>> assert as_set(None) is None and as_set(1) == set([1]) and as_set(range(1,3)) == set([1, 2])
dict2namedtuple(*args, **kwargs)[source]

Helper function to create a namedtuple from a dictionary.

Example

>>> t = dict2namedtuple(foo=1, bar="hello")
>>> assert t.foo == 1 and t.bar == "hello"
>>> t = dict2namedtuple([("foo", 1), ("bar", "hello")])
>>> assert t[0] == t.foo and t[1] == t.bar

Warning

  • The order of the items in the namedtuple is not deterministic if kwargs are used. namedtuples, however, should always be accessed by attribute hence this behaviour should not represent a serious problem.

  • Don’t use this function in code in which memory and performance are crucial since a dict is needed to instantiate the tuple!

class frozendict(*args, **kwargs)[source]

Bases: dict

A dictionary that does not permit changes. The naming violates PEP8 to be consistent with standard Python’s “frozenset” naming.

update([E, ]**F) → None. Update D from dict/iterable E and F.[source]

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

tree()[source]

A tree object, which is effectively a recursive defaultdict that adds tree as members.

Usage:

x = tree() x[‘a’][‘b’][‘c’] = 1

Returns

A tree.