udapi.core.dualdict module

DualDict is a dict with lazily synchronized string representation.

class udapi.core.dualdict.DualDict(value=None, **kwargs)[source]

Bases: MutableMapping

DualDict class serves as dict with lazily synchronized string representation.

>>> ddict = DualDict('Number=Sing|Person=1')
>>> ddict['Case'] = 'Nom'
>>> str(ddict)
'Case=Nom|Number=Sing|Person=1'
>>> ddict['NonExistent']
''

This class provides access to both * a structured (dict-based, deserialized) representation,

e.g. {‘Number’: ‘Sing’, ‘Person’: ‘1’}, and

  • a string (serialized) representation of the mapping, e.g. Number=Sing|Person=1.

There is a clever mechanism that makes sure that users can read and write both of the representations which are always kept synchronized. Moreover, the synchronization is lazy, so the serialization and deserialization is done only when needed. This speeds up scenarios where access to dict is not needed.

A value can be deleted with any of the following three ways: >>> del ddict[‘Case’] >>> ddict[‘Case’] = None >>> ddict[‘Case’] = ‘’ and it works even if the value was already missing.

clear() None.  Remove all items from D.[source]
copy()[source]

Return a deep copy of this instance.

set_mapping(value)[source]

Set the mapping from a dict or string.

If the value is None or an empty string, it is converted to storing string _ (which is the CoNLL-U way of representing an empty value). If the value is a string, it is stored as is. If the value is a dict (or any instance of collections.abc.Mapping), its copy is stored. Other types of value raise an ValueError exception.