Context

Context:

Context([d, formatter]) Dictionary with easy-to-read formatting.

Functions:

difference(d1, d2) Return a dictionary with items from d1 not contained in d2.
get_recursively(d, keys[, default]) Get value from a dictionary d recursively.
intersection(*dicts, **kwargs) Return a dictionary, such that each of its items are contained in all dicts (recursively).
str_to_dict(s) Create a dictionary from a dot-separated string s.
update_nested(d, other) Update dictionary d with items from other dictionary.
update_recursively(d, other) Update dictionary d with items from other dictionary.

Context

Make better output for context. Example:

>>> from lena.context import Context
>>> c = Context({"1": 1, "2": {"3": 4}})
>>> print(c) # doctest: +NORMALIZE_WHITESPACE
{
    "1": 1,
    "2": {
        "3": 4
    }
}
class Context(d={}, formatter=None)[исходный код]
Базовые классы: dict

Dictionary with easy-to-read formatting.

Initialize from a dictionary d.

Representation is defined by the formatter. That must be a callable, which should accept a dictionary and return a string. The default is json.dumps.

Совет

JSON and Python representations are different. In particular, JSON True is written lowercase true. To convert JSON back to Python, use json.loads(string).

If formatter is given but is not callable, LenaTypeError is raised.

__call__(value)[исходный код]

Convert value’s context to Context on the fly.

If the value is a (data, context) pair, convert its context part to Context. If the value doesn’t contain a context, it is created as an empty Context.

Functions

difference(d1, d2)[исходный код]

Return a dictionary with items from d1 not contained in d2.

If a key is present both in d1 and d2 but has different values, it is included into the difference.

get_recursively(d, keys, default=<object object>)[исходный код]

Get value from a dictionary d recursively.

keys can be a list of simple keys (strings), a dot-separated string or a dictionary with at most one key at each level. A string is split by dots and used as a list. A list of keys is searched in the dictionary recursively (it represents nested dictionaries). If any of them is not found, default is returned if «default» is given, otherwise LenaKeyError is raised.

Примечание

Python’s dict.get in case of a missing value returns None and never raises an error. We implement it differently, because it allows more flexibility.

If d is not a dictionary or if keys have unknown types, LenaTypeError is raised. If keys is a dictionary with more than one key at some level, LenaValueError is raised.

If keys is empty, d is returned.

Examples:

>>> context = {"output": {"latex": {"name": "x"}}}
>>> get_recursively(context, ["output", "latex", "name"], default="y")
'x'
>>> get_recursively(context, "output.latex.name")
'x'
intersection(*dicts, **kwargs)[исходный код]

Return a dictionary, such that each of its items are contained in all dicts (recursively).

dicts are several dictionaries. If dicts is empty, an empty dictionary is returned.

A keyword argument level sets maximum number of recursions. For example, if level is 0, all dicts must be equal (otherwise an empty dict is returned). If level is 1, the result contains those subdictionaries which are equal. For arbitrarily nested subdictionaries set level to -1 (default).

Example:

>>> from lena.context import intersection
>>> d1 = {1: "1", 2: {3: "3", 4: "4"}}
>>> d2 = {2: {4: "4"}}
>>> # by default level is -1, which means infinite recursion
>>> intersection(d1, d2) == d2
True
>>> intersection(d1, d2, level=0)
{}
>>> intersection(d1, d2, level=1)
{}
>>> intersection(d1, d2, level=2)
{2: {4: '4'}}

This function always returns a dictionary or its subtype (copied from dicts[0]). All values are deeply copied. No dictionary or subdictionary is changed.

If any of dicts is not a dictionary or if some kwargs are unknown, LenaTypeError is raised.

str_to_dict(s)[исходный код]

Create a dictionary from a dot-separated string s.

Dots represent nested dictionaries. s must have at least two dot-separated parts (a.b), otherwise LenaValueError is raised.

Example:

>>> str_to_dict("a.b.c d")
{'a': {'b': 'c d'}}
update_nested(d, other)[исходный код]

Update dictionary d with items from other dictionary.

other must be a dictionary of one element, which is used as a key. If d doesn’t contain the key, d is updated with other. If d contains the key, the value with that key is nested inside the copy of other at the level which doesn’t contain the key. d is updated.

If d[key] is not a dictionary or if there is not one key in other, LenaValueError is raised.

update_recursively(d, other)[исходный код]

Update dictionary d with items from other dictionary.

other can be a dot-separated string. In this case str_to_dict() is used to convert it to a dictionary.

Existing values are updated recursively, that is including nested subdictionaries. For example:

>>> d1 = {"a": 1, "b": {"c": 3}}
>>> d2 = {"b": {"d": 4}}
>>> update_recursively(d1, d2)
>>> d1 == {'a': 1, 'b': {'c': 3, 'd': 4}}
True
>>> # Usual update would have made d1["b"] = {"d": 4}, erasing "c".

Non-dictionary items from other overwrite those in d:

>>> update_recursively(d1, {"b": 2})
>>> d1 == {'a': 1, 'b': 2}
True

Both d and other must be dictionaries, otherwise LenaTypeError is raised.