brainpy.tools.others.DotDict#

class brainpy.tools.others.DotDict(*args, **kwargs)[source]#

Python dictionaries with advanced dot notation access.

For example:

>>> d = DotDict({'a': 10, 'b': 20})
>>> d.a
10
>>> d['a']
10
>>> d.c  # this will raise a KeyError
KeyError: 'c'
>>> d.c = 30  # but you can assign a value to a non-existing item
>>> d.c
30

In general, all attributes will be included ad keys in the dict. For example, if you add an attribute to specify what variable names you have:

>>> d.names = ('a', 'b')

This attribute names will cause error when you treat the object as a PyTree.

>>> from jax import jit
>>> f = jit(lambda x: x)
>>> f(d)
TypeError: Argument 'a' of type <class 'str'> is not a valid JAX type.

At this moment, you can label this attribute names as not a key in the dictionary by using the syntax:

>>> d.add_attr_not_key('names')
>>> f(d)
{‘a’: DeviceArray(10, dtype=int32, weak_type=True),

‘b’: DeviceArray(20, dtype=int32, weak_type=True), ‘c’: DeviceArray(30, dtype=int32, weak_type=True)}

__init__(*args, **kwargs)[source]#

Methods

__init__(*args, **kwargs)

add_attr_not_key(*args)

Add excluded attribute when retrieving dictionary keys.

clear()

copy()

fromkeys([value])

Create a new dictionary with keys from iterable and values set to value.

get(key[, default])

Return the value for key if key is in the dictionary, else default.

items()

Retrieve all items in the dict, excluding ignored items.

keys()

Retrieve all keys in the dict, excluding ignored keys.

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

2-tuple; but raise KeyError if D is empty.

setdefault(key[, default])

Insert key with a value of default if key is not in the dictionary.

to_numpy()

Change all values to numpy arrays.

update([E, ]**F)

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values()

Retrieve all values in the dict, excluding values of ignored keys.

Attributes

attrs_not_keys