collectionish.AttyDict

class collectionish.AttyDict(**kwargs)

Bases: dict, typing.Generic

A lightweight dictionary with dot access.

attydicts are dictionaries witch allow access to values values through dot notation. The limitation there is that their keys must be strings ( and valid python identifiers ) but sometimes this is what you want when working with json objects unpredictable json or the like. That said, if you have sturctured data you’d of course be much better off using a structured datatype.

Example

initilize a basic attydict:

>>> from collectionish import AttyDict
>>>
>>> the_sea = AttyDict(crabs=10, fish=2)
>>> the_sea
{'crabs': 10, 'fish': 2}

access stuff with dots:

>>> the_sea.crabs += 1
>>> the_sea.crabs
11

nested stuff will also have attr access:

>>> the_sea.update(submarines= {'sandwich': 0, 'actual': 1})
>>> the_sea
{'crabs': 11, 'fish': 2, 'submarines': {'sandwich': 0, 'actual': 1}}
>>> the_sea.submarines.actual
1

we handle name clashes in a similar way to pandas - Reserved names can be set using standard dict access:

>>> the_sea['pop'] = 'corn'
>>> the_sea
{'crabs': 11, 'fish': 2, 'submarines': {'sandwich': 0, 'actual': 1}, 'pop': 'corn'}
>>> the_sea.pop('pop')
'corn'
>>> the_sea
{'crabs': 11, 'fish': 2, 'submarines': {'sandwich': 0, 'actual': 1}}

and as youd expect lists containing dictionaries just work…

>>> the_sea.whales = [{'killer': 1}, {'humpback': 2}, 'mr. whale']
>>> the_sea.whales[1].humpback
2
__init__(**kwargs)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(**kwargs) Initialize self.
clear()
copy()
fromkeys Create a new dictionary with keys from iterable and values set to value.
get Return the value for key if key is in the dictionary, else default.
items()
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 Insert key with a value of default if key is not in the dictionary.
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()
clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

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

get()

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

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

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

popitem() → (k, v), remove and return some (key, value) pair as a

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

setdefault()

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

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

update([E, ]**F) → None. Update D from dict/iterable E and 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() → an object providing a view on D's values