Documentation
md.python.dict component defines contracts to perform operations over
native python dict
type, and provides few useful tools out from box.
Architecture overview
Component overview
def flat(dict_: dict, initial_key: tuple = ()) -> dict: ...
def inline_index(
dict_: typing.Dict[typing.Union[typing.Hashable, typing.Sequence], typing.Any],
glue: str = '.'
) -> typing.Dict[str, typing.Any]: ...
def merge(left: dict, right: dict) -> dict: ...
Install
Usage
Operations over dictionary
Flat dictionary
md.python.dict.flat
function flats structured dictionary object, and returns
new dictionary with flatten key of tuple
type, for example:
will return:
When initial_key: tuple
argument is provided, it's value used
as prefix for resulting dictionary key by prepending it, for example:
import md.python.dict
md.python.dict.flat({
'foo': {
'bar': {
'baz': 42
}
}
}, initial_key=('4', '8'))
will return:
Inline dictionary index
def inline_index(
dict_: typing.Dict[typing.Union[typing.Hashable, typing.Sequence], typing.Any],
glue: str = '.'
) -> typing.Dict[str, typing.Any]: ...
md.python.dict.inline_index
function inlines iterable dictionary key into
scalar key (string), by joining its parts with glue: str = '.'
argument, for example:
import md.python.dict
md.python.dict.inline_index(dict_={
('foo', 'bar'): 'baz',
('foo', 'bar', 'baz'): 42
})
will retutn:
and
import md.python.dict
md.python.dict.inline_index(
dict_={
('foo', 'bar'): 'baz',
('foo', 'bar', 'baz'): 42
},
glue='-'
)
will return:
Merge dictionaries
md.python.dict.merge
function merges two dictionaries and returns new dictionary,
for example:
will return:
Warning
In some cases merge
operation leads to data overwrite, for example:
import md.python.dict
md.python.dict.merge(left={'a': ['some', 'useful', 'data']}, right={'a': None})
will return:
Notice
merge
operation does not merges types different from dict
type, for example:
import md.python.dict
md.python.dict.merge(
left={'a': ['some', 'useful', 'data']},
right={'a': ['here', 'no']}
)
will return:
Case-insensitive dictionary
md.python.dict.CaseInsensitiveDict
provides extension of standard dict
type,
that allows to access to the same value by a key not depending on it case.