Datastructures

Invenio special data structures.

class invenio.utils.datastructures.DotableDict

Make nested python dictionaries accessable using dot notation.

Example:

>>> dotable = DotableDict({'a': [{'b': 3, 'c': 5}]})
>>> dotable.a
...  [{'b': 3, 'c': 5}]
class invenio.utils.datastructures.LaziestDict(function=<type 'dict'>)

Even lazier dictionary (maybe the laziest).

It does not have content and when a key is accessed it tries to evaluate only this key.

Example:

def reader_discover(key):
    from werkzeug.utils import import_string
    return import_string(
        'invenio.jsonalchemy.jsonext.readers%sreader:reader' % (key)
    )

laziest_dict = LaziestDict(reader_discover)

laziest_dict['json']
# It will give you the JsonReader class
class invenio.utils.datastructures.LazyDict(function=<type 'dict'>)

Lazy dictionary that evaluates its content when it is first accessed.

Example:

def my_dict():
    from werkzeug.utils import import_string
    return {'foo': import_string('foo')}

lazy_dict = LazyDict(my_dict)
# at this point the internal dictionary is empty
lazy_dict['foo']
class invenio.utils.datastructures.SmartDict(d=None)

This dictionary allows to do some ‘smart queries’ to its content.

Example:

>>> d = SmartDict()
>>> d['foo'] = {'a': 'world', 'b':'hello'}
>>> d['a'] = [ {'b':1}, {'b':2}, {'b':3} ]
>>> d['a']
[ {'b':1}, {'b':2}, {'b':3} ]
>>> d['a[0]']
{'b':1}
>>> d['a.b']
[1,2,3]
>>> d['a[1:]']
[{'b':2}, {'b':3}]

Note

You can’t use the reserved words ‘.’, ‘[‘, ‘]’ like a key.

>>> d['.']
>>> d[']']
>>> d['.a']

It is also not recommended initialize SmartDict with keys from within the list of reserved words.

>>> d = SmartDict({'a': 3, 'b': {'.': 5}})
get(key, default=None)

Return value for given key or default value.

has_key(key)

Return True if key is in dictionary.

iteritems()

Proxy to dict.iteritems.

iterkeys()

Proxy to dict.iterkeys.

itervalues()

Proxy to dict.itervalues.

set(key, value, extend=False, **kwargs)

Extended standard set function.

update(E, **F)

Proxy dict update method.

invenio.utils.datastructures.flatten_multidict(multidict)

Return flattened dictionary from MultiDict.