bcdict.bcdict#

Functions

apply(f, *args, **kwargs)

Apply callable on each element of some dicts.

bootstrap(keys, f, *args, **kwargs)

Call f for for every key.

bootstrap_arg(keys, f, *args, **kwargs)

Same as bootstrap(), but pass key as positional argument.

bootstrap_kwarg(keys, f, *args, argname, ...)

Same as bootstrap(), but pass key as keyword argument.

to_list(*args)

Convert a list of dicts to a dict of lists.

Classes

BCDict(*args[, ipython_safe])

Dictionary with broadcast support.

Documentation

bcdict.bcdict.to_list(*args: dict) dict[source]#

Convert a list of dicts to a dict of lists.

bcdict.bcdict.apply(f: Callable, *args: Any, **kwargs: Any) bcdict.bcdict.BCDict[bcdict.bcdict.K, Any][source]#

Apply callable on each element of some dicts.

The first argument that is a BCDict serves as reference. f is called for each of its elements.

args and kwargs are passed to f and broadcast if applicable.

If there is no BCDict in the args or kwargs, a ValueError is raised.

Parameters
  • f (Callable) – function or callable that is called

  • args (BCDict | Any) – positional arguments passed to f

  • kwargs (BCDict | Any) – keyword arguments passed to f

Returns

return_value – a broadcast dictionary with the same keys as the first BCDict in the arguments. Its values are the return values from the respective call to f.

Return type

BCDict

Examples

>>> d = BCDict({"A": 2, "B": 3})
>>> factor = BCDict({"A": 4, "B": 5})
>>> f = lambda x1, x2, x3: x1 * x2 + x3
>>> apply(f, d, factor, 1)
BCDict({'A': 9, 'B': 16})

# 2 * 4 + 1 = 9 # 3 * 5 + 1 = 16

bcdict.bcdict.bootstrap(keys: list[str], f: Callable, *args, **kwargs)[source]#

Call f for for every key.

args and kwargs are passed to f and broadcast if applicable.

The result is a BCDcict with an entry for each element of keys and the respective return value of f as values.

The keys are not passed to f, but only used as dictionary keys.

bcdict.bcdict.bootstrap_arg(keys: list[str], f: Callable, *args, **kwargs)[source]#

Same as bootstrap(), but pass key as positional argument.

When calling f for a key, the key is passed as the first positional argument.

bcdict.bcdict.bootstrap_kwarg(keys: list[str], f: Callable, *args, argname: str, **kwargs)[source]#

Same as bootstrap(), but pass key as keyword argument.

When calling f for a key, the key is passed as argument with name argname.

class bcdict.bcdict.BCDict(*args: Any, ipython_safe: bool = True, **kwargs: Any)[source]#

Bases: dict, Generic[bcdict.bcdict.K, bcdict.bcdict.V]

Dictionary with broadcast support.

Allows to apply functions to all its elements, or retrieve attributes of all its elements.

Parameters
  • ipython_safe (bool, optional) – whether to use some black magic to prevent lengthy formatter errors in jupyter notebook or lab. Can be problematic if you want to access attributes in the dictionary’s values that start with “_ipython_” or “_repr_”. Then you need to use the .a accessor or disdable ipython_safe. Default: True

  • *args – forwarded to dict

  • **kwargs – forwarded to dict

Examples

>>> d = BCDict({"a": "hello", "b": "world!"})
>>> d
BCDict({'a': 'hello', 'b': 'world!'})

Regular element access:

>>> d['a']
'hello'

Regular element assignments

>>> d['a'] = "Hello"
>>> d
BCDict({'a': 'Hello', 'b': 'world!'})

Calling functions:

>>> d.upper()
BCDict({'a': 'HELLO', 'b': 'WORLD!'})

Slicing:

>>> d[1:3]
BCDict({'a': 'el', 'b': 'or'})

Applying functions:

>>> d.pipe(len)
BCDict({'a': 5, 'b': 6})

When there is a conflict between an attribute in the values and an attribute in BCDict, use the attribute accessor explicitly:

>>> d.a.upper()
BCDict({'a': 'HELLO', 'b': 'WORLD!'})

Slicing with conflicting keys:

>>> n = BCDict({1:"hello", 2: "world"})
>>> n[1]
'hello'
>>> n.a[1]
BCDict({1: 'e', 2: 'o'})
class DictAccessor(data: dict[K, V])[source]#

Bases: object

Internal helper class.

This is what BCDict.a returns.

__init__(data: dict[K, V])[source]#
__getattr__(item: str) bcdict.bcdict.BCDict[bcdict.bcdict.K, Any][source]#
__setattr__(item: str, value: Any) None[source]#

Implement setattr(self, name, value).

__getitem__(item: Any) bcdict.bcdict.BCDict[bcdict.bcdict.K, Any][source]#
__setitem__(item: str, value: Any) None[source]#
__init__(*args: Any, ipython_safe: bool = True, **kwargs: Any)[source]#
__call__(*args: Any, **kwargs: Any) bcdict.bcdict.BCDict[bcdict.bcdict.K, Any][source]#

Call each element of the dictionary with args and kwargs.

args and kwargs are broadcasted if applicable.

property broadcast: bcdict.bcdict.BCDict.DictAccessor#

Attribute access. Use this to get an attribute of each value in the dictionary which has the same name as an attribute of the BCDict class.

property a: bcdict.bcdict.BCDict.DictAccessor#

Shorthand version of broadcast property.

__getitem__(item: Any) V | BCDict[K, Any][source]#

Slice function.

When item is a key of the BCDict, return the respective value.

Else, if item is a dictionary with the same keys as this BCDict, then slice each value of this BCDict with the corresponding element of item, and return the result as a new BCDict.

Else, slice each value in the dictionary with item and return a new dict.

To slice each value with an item that is also in this dictionary, use the item() function instead.

__getattr__(item: str) Any[source]#
__setattr__(key, value) None[source]#

Implement setattr(self, name, value).

pipe(f: Callable, *args: Any, **kwargs: Any) bcdict.bcdict.BCDict[bcdict.bcdict.K, Any][source]#

Apply callable on each element of the dict.

args and kwargs are passed to f and broadcasted if applicable.

__add__(other: dict | Any) BCDict[source]#
__mul__(other: dict | Any) BCDict[source]#
__matmul__(other: dict | Any) BCDict[source]#
__sub__(other: dict | Any) BCDict[source]#
__mod__(other: dict | Any) BCDict[source]#
__truediv__(other: dict | Any) BCDict[source]#
__floordiv__(other: dict | Any) BCDict[source]#
__pow__(power: dict | Any) BCDict[source]#
__lshift__(other: dict | Any) BCDict[source]#
__rshift__(other: dict | Any) BCDict[source]#
__lt__(other: dict | Any) BCDict[source]#

Return self<value.

__le__(other: dict | Any) BCDict[source]#

Return self<=value.

__gt__(other: dict | Any) BCDict[source]#

Return self>value.

__ge__(other: dict | Any) BCDict[source]#

Return self>=value.

eq(other: dict | Any) BCDict[source]#

Element-wise equality with broadcast support.

ne(other: dict | Any) BCDict[source]#

Element-wise inequality with broadcast support.

unpack()[source]#

Convert BCDict of tuples into tuple of BCDict.

__repr__()[source]#

Return repr(self).