h10n.util

An Utility module contains objects which is used by h10n internally. End users don’t need to use this module directly.

class h10n.util.ExceptionContext(obj)

An Exception Context is utility object to store context in the exception arguments. The Exception Context is used via keep_context() decorator.

classmethod extend(exception, obj)

Extends existent context of exception or add new one

class h10n.util.NamedObject

An utility base class for named objects, which is used in the exception context. See ExceptionContext doc-strings.

>>> no = NamedObject()
>>> no
<NamedObject: __empty__>
>>> no.name = 'test'
>>> no
<NamedObject: test>
class h10n.util.Namespace

Namespace is an utility object, which mimics to JavaScript object. The Namespace provides methods to manipulate attributes using subscription interface:

>>> ns = Namespace()
>>> ns.a = 1
>>> ns['a']
1
>>> ns['a'] = 2
>>> ns.a
2
>>> ns['b.c'] = 3
>>> ns.b.c
3
>>> ns.b                                     
<h10n.util.Namespace object at ...>
extend(d)

Extends namespace by attributes from dictionary or another namespace. Dot-separated keys in the dictionary becomes to nested namespace.

>>> ns = Namespace().extend({'a.b': 1, 'c': 2})
>>> ns.a.b
1
>>> ns.c
2
>>> ns.a                                 
<h10n.util.Namespace object at ...>
>>> ns_2 = Namespace().extend({'c': 3, 'd': 4, 'a.e': 5})
>>> ns.extend(ns_2)                      
<h10n.util.Namespace object at ...>
>>> ns.c
3
>>> ns.d
4
>>> ns.a.e
5
freeze()

Freeze current attributes of namespace to prevent overriding via extend.

>>> ns = Namespace().extend({'a': 1})
>>> ns.freeze()
>>> ns.extend({'a': 2}).a
1
h10n.util.keep_context(method)

Includes context into exception raised from decorated method.

>>> class Test(NamedObject):
...     def __init__(self, name):
...         self.name = name
...     @keep_context
...     def test(self):
...         raise Exception('Test exception')
>>> Test('foo').test()
Traceback (most recent call last):
...
Exception: ('Test exception', <ExceptionContext: [<Test: foo>]>)

Project Versions

Previous topic

h10n.source

Next topic

Glossary

This Page