toil.lib.expando¶
Classes¶
Pass initial attributes to the constructor: |
|
Use MagicExpando for chained attribute access. |
Module Contents¶
- class toil.lib.expando.Expando(*args, **kwargs)[source]¶
Bases:
dict
Pass initial attributes to the constructor:
>>> o = Expando(foo=42) >>> o.foo 42
Dynamically create new attributes:
>>> o.bar = 'hi' >>> o.bar 'hi'
Expando is a dictionary:
>>> isinstance(o,dict) True >>> o['foo'] 42
Works great with JSON:
>>> import json >>> s='{"foo":42}' >>> o = json.loads(s,object_hook=Expando) >>> o.foo 42 >>> o.bar = 'hi' >>> o.bar 'hi'
And since Expando is a dict, it serializes back to JSON just fine:
>>> json.dumps(o, sort_keys=True) '{"bar": "hi", "foo": 42}'
Attributes can be deleted, too:
>>> o = Expando(foo=42) >>> o.foo 42 >>> del o.foo >>> o.foo Traceback (most recent call last): ... AttributeError: 'Expando' object has no attribute 'foo' >>> o['foo'] Traceback (most recent call last): ... KeyError: 'foo'
>>> del o.foo Traceback (most recent call last): ... AttributeError: foo
And copied:
>>> o = Expando(foo=42) >>> p = o.copy() >>> isinstance(p,Expando) True >>> o == p True >>> o is p False
Same with MagicExpando …
>>> o = MagicExpando() >>> o.foo.bar = 42 >>> p = o.copy() >>> isinstance(p,MagicExpando) True >>> o == p True >>> o is p False
… but the copy is shallow:
>>> o.foo is p.foo True
- __slots__ = None¶
- __dict__¶
- class toil.lib.expando.MagicExpando(*args, **kwargs)[source]¶
Bases:
Expando
Use MagicExpando for chained attribute access.
The first time a missing attribute is accessed, it will be set to a new child MagicExpando.
>>> o=MagicExpando() >>> o.foo = 42 >>> o {'foo': 42} >>> o.bar.hello = 'hi' >>> o.bar {'hello': 'hi'}