toil.lib.expando

Module Contents

Classes

Expando

Pass inital attributes to the constructor:

MagicExpando

Use MagicExpando for chained attribute access.

class toil.lib.expando.Expando(*args, **kwargs)[source]

Bases: dict

digraph inheritancebdb030efff { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "Expando" [URL="#toil.lib.expando.Expando",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Pass inital attributes to the constructor:"]; }

Pass inital 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
copy()[source]

D.copy() -> a shallow copy of D

class toil.lib.expando.MagicExpando(*args, **kwargs)[source]

Bases: Expando

digraph inheritance5e5d4e2a43 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "Expando" [URL="#toil.lib.expando.Expando",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Pass inital attributes to the constructor:"]; "MagicExpando" [URL="#toil.lib.expando.MagicExpando",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="Use MagicExpando for chained attribute access."]; "Expando" -> "MagicExpando" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

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'}
__getattribute__(name)[source]

Return getattr(self, name).

Parameters

name (str) –