Dynamic Utility

The dynamic utility provides two abstract classes which are intended to be inherited from or being used in a stand-alone mode. Both classes provide means for dynamically creating properties, based on a given set of information.

Dynamic Class

class xobox.utils.dynamic.Dynamic(*args, **kwargs)[source]

Dynamic class generating properties from kwargs

The constructor of the Dynamic class uses all keyword arguments passed to the constructor for creating property() attributes with the same name as the keyword argument, returning the value submitted with the keyword argument.

Usage Example:

>>> from xobox.utils.dynamic import Dynamic
>>> obj = Dynamic(foo='bar')
>>> 'foo' in dir(obj)
True
>>> hasattr(obj, 'foo')
True
>>> getattr(obj, 'foo')
'bar'
>>> obj.foo
'bar'

Note

The properties are dynamically created by the constructor. After the constructor has terminated, the property set of the created object is fix (except for direct manipulation using setattr() and delattr()).

Dynamic Iterable Class

The dynamic iterable class interface is much more complex than the pure Dynamic class. It not only provides dynamically generated property() attributes, but also acts as a dict instance, allowing to access all attribute values by using their names as keys. Furthermore, attributes are dynamically created, updated or deleted when the object is manipulated via its dictionary interface.

In addition, the Dynamic class allows for registering hook methods or functions, which can be used to manipulate the item setting and deletion behaviour.

class xobox.utils.dynamic.DynamicIterable(dict=None, **kwargs)[source]

A dynamic iterable object is similar to a normal Python dictionary, except it offers all keys also as properties.

Note

Since UserDict in Python 2.x is an old-style class, this class also inherits from object to become a new style class.

Parameters:
  • dict (dict) – dictionary with initial data to be filled in
  • kwargs – keyword arguments to be transformed into dictionary data
register_hook(hook_type, method)[source]

Register a hook within the corresponding hook queue.

Parameters:
  • hook_type (str) – one of pre-set, post-set, pre-del, post-del, pre-get, post-get
  • method – reference to a method or function taking two arguments (key, value) and returning exactly this tuple (however, with different content if necessary to fulfil the hook’s purpose).

Usage example:

>>> from xobox.utils.dynamic import DynamicIterable
>>> obj = DynamicIterable(foo='bar')
>>> 'foo' in dir(obj)
True
>>> hasattr(obj, 'foo')
True
>>> getattr(obj, 'foo')
'bar'
>>> obj.foo
'bar'
>>> 'foo' in obj
True
>>> obj['foo']
'bar'
>>> hasattr(obj, 'fuu')
False
>>> obj['fuu'] = 'baz'
>>> hasattr(obj, 'fuu')
True

Hooks example:

from xobox.utils.dynamic import DynamicIterable
class MyIterable(DynamicIterable):

    def __init__(self, dict=None, **kwargs):
        self.register_hook('pre-set', my_hook)
        self.register_hook('pre-get', my_hook)
        super(MyIterable, self).__init__(dict=dict, **kwargs)

    @staticmethod
    def my_hook(key, value)
        return str(key).upper(), value

This example demonstrates how one can render the DynamicIterable’s keys case-agnostic.