easytree.dict#
- class easytree.dict(*args, sealed=False, frozen=False, **kwargs)#
recursive dot-styled defaultdict
- classmethod fromkeys(keys, value)#
Create a dict from a set of keys and a fixed value
- get(key, default=None)#
Get item by key, if it is exists; otherwise, return default
If key is list, recursively traverses the tree
- Parameters:
key (hashable, list[hashable]) – the key (or path of keys)
- Returns:
value
- Return type:
any
Example
>>> person = easytree.dict({"age":31, "friends":[{"firstname":"Michael"}]}) >>> person.get("age") 31 >>> person.get("address") None >>> person.get(["friends",0,"firstname"]) "Michael" >>> person.get(["friends",1,"firstname"]) None >>> person.get(["friends",0,"avatar"],"N/A") "N/A"
- pop(*args)#
Remove and return an element from a dictionary having the given key.
- Parameters:
key (any) – the key to pop
default (any) – the default value, if the key does not exist
- Raises:
KeyError – if the given key does not exist, and no default value is given
AttributeError – if the dict is frozen, or if the dict is sealed
- popitem()#
Remove and return the last item (key, value pair) inserted into the dictionary
- Raises:
KeyError – if the dict is empty
AttributeError – if the dict is frozen if the dict is sealed
- setdefault(key, default)#
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.
- Parameters:
key (any) – the key
default (any) – the default value to insert if the key does not exist
- Raises:
AttributeError – if the dict is frozen, or if the dict is sealed and the key does not exist in the dict
- update(other)#
Update the dict from keys and values of another mapping object
- Parameters:
other (mapping) – other dictionary
- Raises:
AttributeError – if the dict is frozen, or if the dict is sealed and a key of other does not exist in the dict