trafaret.visitor — methods to access object’s attribute/netsted key by path

This module is expirement. API and implementation are unstable. Supposed to use with Request object or something like that.

class trafaret.visitor.DeepKey(name, default=<object object>, optional=False, to_name=None, trafaret=None)

Lookup for attributes and items Path in name must be delimited by ..

>>> from trafaret import Int
>>> class A(object):
...     class B(object):
...         d = {'a': 'word'}
>>> dict((DeepKey('B.d.a') >> 'B_a').pop(A))
{'B_a': 'word'}
>>> dict((DeepKey('c.B.d.a') >> 'B_a').pop({'c': A}))
{'B_a': 'word'}
>>> dict((DeepKey('B.a') >> 'B_a').pop(A))
{'B.a': DataError(Unexistent key)}
>>> dict(DeepKey('c.B.d.a', to_name='B_a', trafaret=Int()).pop({'c': A}))
{'B_a': DataError(value can't be converted to int)}
class trafaret.visitor.Visitor(keys)

Check any object or mapping with DeepKey instances. This means that counts only existance and correctness of given paths. Visitor will not check for additional attributes etc.

trafaret.visitor.get_deep_attr(obj, keys)

Helper for DeepKey