chicken_turtle_util.dict

dict utilities

assign Assign one dict to the other through mutations
DefaultDict Replacement for collections.defaultdict, its default value factory takes a
invert Invert dict by swapping each value with its key
pretty_print_head Pretty print some items of a dict
chicken_turtle_util.dict.assign(destination, source)[source]

Assign one dict to the other through mutations

Roughly put, destination := source. More formally, after the call, destination == source is true and id(destination) is unchanged.

Parameters:

destination : dict

dict to assign to

source : dict

dict to assign from

Examples

>>> import chicken_turtle_util.dict as dict_
>>> destination = {1: 2, 3: 4}
>>> source = {3: 5, 6: 7}
>>> dict_.assign(destination, source)
>>> assert destination == {3: 5, 6: 7} 
class chicken_turtle_util.dict.DefaultDict[source]

Replacement for collections.defaultdict, its default value factory takes a key argument

Parameters:

default_factory : (key :: any) -> (value :: any)

Function that is called with the missing key and returns the default value to use for it

chicken_turtle_util.dict.invert(dict_)[source]

Invert dict by swapping each value with its key

Parameters:

dict_ : {hashable => hashable}

Dict to invert

Returns:

{hashable => {hashable}}

dict_ copy with key and value swapped, a multi-dict (as some keys in dict_ may have had the same value).

See also

MultiDict
A mutable multi-dict view of a {hashable => {hashable}} dict.

Notes

If your dict never has 2 keys mapped to the same value, you can convert it to a {hashable => any} dict using:

from chicken_turtle_util.multi_dict import MultiDict
inverted_dict = dict(MultiDict(inverted_dict))

Examples

>>> invert({1: 2, 3: 4})
{2: {1}, 4: {3}}
>>> invert({1: 2, 3: 2, 4: 5})
{2: {1,3}, 5: {4}}
chicken_turtle_util.dict.pretty_print_head(dict_, count=10)[source]

Pretty print some items of a dict

For an unordered dict, count arbitrary items will be printed.

Parameters:

dict_ : dict

Dict to print from

count : int, optional

Number of items to print.

Raises:

ValueError

When count < 1