Skip to content

diot.diot

module

diot.diot

diot module

Classes
class

diot.diot.Diot(*args, **kwargs)

Bases
dict

Dictionary with dot notation

Examples

~~~python

d = Diot(a=1, b=2) d.a = 2 d['a'] = 2 d.a # 2 d['a'] # 2 d.pop('a') # 2 d.pop('x', 1) # 1 d.popitem() # ('b', 2) d.update(a=3, b=4) # {'a': 3, 'b': 4} d | {'a': 1, 'b': 2} # {'a': 1, 'b': 2} (d unchanged) d |= {'a': 1, 'b': 2} # d == {'a': 1, 'b': 2} del d.a del d['b'] d.freeze() d.a = 1 # DiotFrozenError d.unfreeze() d.a = 1 # ok d.setdefault('b', 2) 'b' in d d.copy() d.deepcopy() ~~~

Parameters
  • *args Anything that can be sent to dict construct
  • **kwargs keyword argument that can be sent to dict constructSome diot configurations can also be passed, including: diot_nest: Types to nestly convert values diot_transform: The transforms for keys diot_frozen: Whether to generate a frozen diot. True: freeze the object recursively if there are Diot objects in descendants False: Don'f freeze 'shallow': Only freeze at depth = 1
Methods
  • __contains__(name) (bool) True if the dictionary has the specified key, else False.</>
  • __delitem__(name) Delete self[key].</>
  • __getitem__(name) (any) x.getitem(y) <==> x[y]</>
  • __ior__(other) (Diot) Return self|=value.</>
  • __or__(other) (Diot) Return self|value.</>
  • __setitem__(name, value) Set self[key] to value.</>
  • accessible_keys() (iterable of str) Get the converted keys</>
  • clear() Clear the object</>
  • copy() (Diot) Shallow copy the object</>
  • freeze(frozen) Freeze the diot object</>
  • from_namespace(namespace, recursive, diot_nest, diot_transform, diot_frozen) (Diot) Get a Diot object from an argparse namespace</>
  • get(name, value) (any) Get the value of a key name</>
  • pop(name, *value) (any) Pop a key from the object and return the value. If key does notexist, return the given default value </>
  • popitem() (str, any) Pop last item from the object</>
  • setdefault(name, value) (any) Set a default value to a key</>
  • thaw(recursive) A context manager for temporarily change the diot</>
  • to_dict() (dict(str: any)) Turn the Box and sub Boxes back into a nativepython dictionary. </>
  • to_json(filename, encoding, errors, **json_kwargs) (str, optional) Convert to a json string or save it to json file</>
  • to_toml(filename, encoding, errors) (str, optional) Convert to a toml string or save it to toml file</>
  • to_yaml(filename, default_flow_style, encoding, errors, **yaml_kwargs) (str, optional) Convert to a yaml string or save it to yaml file</>
  • unfreeze(recursive) Unfreeze the diot object</>
  • update(*value, **kwargs) Update the object. Shortcut: |=</>
  • update_recursively(*value, **kwargs) Update the object. Shortcut: |=</>
classmethod

from_namespace(namespace, recursive=True, diot_nest=True, diot_transform='safe', diot_frozen=False)

Get a Diot object from an argparse namespace

Example

~~~python

from argparse import Namespace Diot.from_namespace(Namespace(a=1, b=2)) ~~~

Parameters
  • namespace (Namespace) The namespace object
  • recursive (bool, optional) Do it recursively?
  • diot_nest (Union(bool, iterable of type), optional) Types to nestly convert values
  • diot_transform (Union(callable(str: str), str), optional) The transforms for keys
  • diot_frozen (bool or str, optional) Whether to generate a frozen diot.
    • - True: freeze the object recursively if there are Diot objects
    in descendants
    • - False: Don'f freeze
    • - shallow: Only freeze at depth = 1
  • diot_missing How to deal with missing keys when accessing them
    • - An exception class or object to raise
    • - None to return None
    • - A custom function with first argument the key and second
        the diot object.
Returns (Diot)

The converted diot object.

method

__setitem__(name, value)

Set self[key] to value.

method

__getitem__(name) → any

x.getitem(y) <==> x[y]

method

pop(name, *value)

Pop a key from the object and return the value. If key does notexist, return the given default value

Parameters
  • name (str) The key
  • value The default value to return if the key does not exist
Returns (any)

The value corresponding to the name or the default value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

popitem()

Pop last item from the object

Returns (str, any)

A tuple of key and value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

update(*value, **kwargs)

Update the object. Shortcut: |=

Parameters
  • args args that can be sent to dict to update the object
  • kwargs kwargs that can be sent to dict to update the object
Raises
  • DiotFrozenError when try to update a frozen diot
method

update_recursively(*value, **kwargs)

Update the object. Shortcut: |=

Parameters
  • args args that can be sent to dict to update the object
  • kwargs kwargs that can be sent to dict to update the object
Raises
  • DiotFrozenError when try to update a frozen diot
method

__or__(other)Diot

Return self|value.

method

__ior__(other)Diot

Return self|=value.

method

__delitem__(name)

Delete self[key].

method

freeze(frozen='shallow')

Freeze the diot object

Parameters
  • frozen (str or bool, optional) The frozen argument indicating how to freeze:shallow: only freeze at depth=1 True: freeze recursively if there are diot objects in children False: Disable freezing
method

unfreeze(recursive=False)

Unfreeze the diot object

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
generator

thaw(recursive=False)

A context manager for temporarily change the diot

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
Yields

self, the reference to this diot.

method

setdefault(name, value)

Set a default value to a key

Parameters
  • name (str) The key name
  • value (any) The default value
Returns (any)

The existing value or the value passed in

Raises
  • DiotFrozenError when try to set default to a frozen diot
method

accessible_keys()

Get the converted keys

Returns (iterable of str)

The accessible (transformed) keys

method

get(name, value=None)

Get the value of a key name

Parameters
  • name (str) The key name
  • value (any, optional) The value to return if the key does not exist
Returns (any)

The corresponding value or the value passed in if the key doesnot exist

method

__contains__(name) → bool

True if the dictionary has the specified key, else False.

method

clear()

Clear the object

method

copy()

Shallow copy the object

Returns (Diot)

The copied object

method

to_dict()

Turn the Box and sub Boxes back into a nativepython dictionary.

Returns (dict(str: any))

The converted python dictionary

method

to_json(filename=None, encoding='utf-8', errors='strict', **json_kwargs)

Convert to a json string or save it to json file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the json to, if not given a jsonstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **json_kwargs Other kwargs for json.dumps
Returns (str, optional)

The json string with filename is not given

method

to_yaml(filename=None, default_flow_style=False, encoding='utf-8', errors='strict', **yaml_kwargs)

Convert to a yaml string or save it to yaml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the yaml to, if not given a yamlstring will be returned
  • default_flow_style (bool, optional) The default flow style for yaml dumpingSee yaml.dump
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **yaml_kwargs Other kwargs for yaml.dump
Returns (str, optional)

The yaml string with filename is not given

method

to_toml(filename=None, encoding='utf-8', errors='strict')

Convert to a toml string or save it to toml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the toml to, if not given a tomlstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
Returns (str, optional)

The toml string with filename is not given

class

diot.diot.CamelDiot(*args, **kwargs)

Bases

With camel case conversion

Parameters
  • *args Anything that can be sent to dict construct
  • **kwargs keyword argument that can be sent to dict constructSome diot configurations can also be passed, including: diot_nest: Types to nestly convert values diot_transform: The transforms for keys diot_frozen: Whether to generate a frozen diot. True: freeze the object recursively if there are Diot objects in descendants False: Don'f freeze 'shallow': Only freeze at depth = 1
Methods
  • __contains__(name) (bool) True if the dictionary has the specified key, else False.</>
  • __delitem__(name) Delete self[key].</>
  • __getitem__(name) (any) x.getitem(y) <==> x[y]</>
  • __ior__(other) (Diot) Return self|=value.</>
  • __or__(other) (Diot) Return self|value.</>
  • __setitem__(name, value) Set self[key] to value.</>
  • accessible_keys() (iterable of str) Get the converted keys</>
  • clear() Clear the object</>
  • copy() (Diot) Shallow copy the object</>
  • freeze(frozen) Freeze the diot object</>
  • from_namespace(namespace, recursive, diot_nest, diot_transform, diot_frozen) (Diot) Get a Diot object from an argparse namespace</>
  • get(name, value) (any) Get the value of a key name</>
  • pop(name, *value) (any) Pop a key from the object and return the value. If key does notexist, return the given default value </>
  • popitem() (str, any) Pop last item from the object</>
  • setdefault(name, value) (any) Set a default value to a key</>
  • thaw(recursive) A context manager for temporarily change the diot</>
  • to_dict() (dict(str: any)) Turn the Box and sub Boxes back into a nativepython dictionary. </>
  • to_json(filename, encoding, errors, **json_kwargs) (str, optional) Convert to a json string or save it to json file</>
  • to_toml(filename, encoding, errors) (str, optional) Convert to a toml string or save it to toml file</>
  • to_yaml(filename, default_flow_style, encoding, errors, **yaml_kwargs) (str, optional) Convert to a yaml string or save it to yaml file</>
  • unfreeze(recursive) Unfreeze the diot object</>
  • update(*value, **kwargs) Update the object. Shortcut: |=</>
  • update_recursively(*value, **kwargs) Update the object. Shortcut: |=</>
classmethod

from_namespace(namespace, recursive=True, diot_nest=True, diot_transform='safe', diot_frozen=False)

Get a Diot object from an argparse namespace

Example

~~~python

from argparse import Namespace Diot.from_namespace(Namespace(a=1, b=2)) ~~~

Parameters
  • namespace (Namespace) The namespace object
  • recursive (bool, optional) Do it recursively?
  • diot_nest (Union(bool, iterable of type), optional) Types to nestly convert values
  • diot_transform (Union(callable(str: str), str), optional) The transforms for keys
  • diot_frozen (bool or str, optional) Whether to generate a frozen diot.
    • - True: freeze the object recursively if there are Diot objects
    in descendants
    • - False: Don'f freeze
    • - shallow: Only freeze at depth = 1
  • diot_missing How to deal with missing keys when accessing them
    • - An exception class or object to raise
    • - None to return None
    • - A custom function with first argument the key and second
        the diot object.
Returns (Diot)

The converted diot object.

method

__setitem__(name, value)

Set self[key] to value.

method

__getitem__(name) → any

x.getitem(y) <==> x[y]

method

pop(name, *value)

Pop a key from the object and return the value. If key does notexist, return the given default value

Parameters
  • name (str) The key
Returns (any)

The value corresponding to the name or the default value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

popitem()

Pop last item from the object

Returns (str, any)

A tuple of key and value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

update(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

update_recursively(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

__or__(other)Diot

Return self|value.

method

__ior__(other)Diot

Return self|=value.

method

__delitem__(name)

Delete self[key].

method

freeze(frozen='shallow')

Freeze the diot object

Parameters
  • frozen (str or bool, optional) The frozen argument indicating how to freeze:shallow: only freeze at depth=1 True: freeze recursively if there are diot objects in children False: Disable freezing
method

unfreeze(recursive=False)

Unfreeze the diot object

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
generator

thaw(recursive=False)

A context manager for temporarily change the diot

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
Yields

self, the reference to this diot.

method

setdefault(name, value)

Set a default value to a key

Parameters
  • name (str) The key name
  • value (any) The default value
Returns (any)

The existing value or the value passed in

Raises
  • DiotFrozenError when try to set default to a frozen diot
method

accessible_keys()

Get the converted keys

Returns (iterable of str)

The accessible (transformed) keys

method

get(name, value=None)

Get the value of a key name

Parameters
  • name (str) The key name
  • value (any, optional) The value to return if the key does not exist
Returns (any)

The corresponding value or the value passed in if the key doesnot exist

method

__contains__(name) → bool

True if the dictionary has the specified key, else False.

method

clear()

Clear the object

method

copy()

Shallow copy the object

Returns (Diot)

The copied object

method

to_dict()

Turn the Box and sub Boxes back into a nativepython dictionary.

Returns (dict(str: any))

The converted python dictionary

method

to_json(filename=None, encoding='utf-8', errors='strict', **json_kwargs)

Convert to a json string or save it to json file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the json to, if not given a jsonstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **json_kwargs Other kwargs for json.dumps
Returns (str, optional)

The json string with filename is not given

method

to_yaml(filename=None, default_flow_style=False, encoding='utf-8', errors='strict', **yaml_kwargs)

Convert to a yaml string or save it to yaml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the yaml to, if not given a yamlstring will be returned
  • default_flow_style (bool, optional) The default flow style for yaml dumpingSee yaml.dump
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **yaml_kwargs Other kwargs for yaml.dump
Returns (str, optional)

The yaml string with filename is not given

method

to_toml(filename=None, encoding='utf-8', errors='strict')

Convert to a toml string or save it to toml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the toml to, if not given a tomlstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
Returns (str, optional)

The toml string with filename is not given

class

diot.diot.SnakeDiot(*args, **kwargs)

Bases

With snake case conversion

Parameters
  • *args Anything that can be sent to dict construct
  • **kwargs keyword argument that can be sent to dict constructSome diot configurations can also be passed, including: diot_nest: Types to nestly convert values diot_transform: The transforms for keys diot_frozen: Whether to generate a frozen diot. True: freeze the object recursively if there are Diot objects in descendants False: Don'f freeze 'shallow': Only freeze at depth = 1
Methods
  • __contains__(name) (bool) True if the dictionary has the specified key, else False.</>
  • __delitem__(name) Delete self[key].</>
  • __getitem__(name) (any) x.getitem(y) <==> x[y]</>
  • __ior__(other) (Diot) Return self|=value.</>
  • __or__(other) (Diot) Return self|value.</>
  • __setitem__(name, value) Set self[key] to value.</>
  • accessible_keys() (iterable of str) Get the converted keys</>
  • clear() Clear the object</>
  • copy() (Diot) Shallow copy the object</>
  • freeze(frozen) Freeze the diot object</>
  • from_namespace(namespace, recursive, diot_nest, diot_transform, diot_frozen) (Diot) Get a Diot object from an argparse namespace</>
  • get(name, value) (any) Get the value of a key name</>
  • pop(name, *value) (any) Pop a key from the object and return the value. If key does notexist, return the given default value </>
  • popitem() (str, any) Pop last item from the object</>
  • setdefault(name, value) (any) Set a default value to a key</>
  • thaw(recursive) A context manager for temporarily change the diot</>
  • to_dict() (dict(str: any)) Turn the Box and sub Boxes back into a nativepython dictionary. </>
  • to_json(filename, encoding, errors, **json_kwargs) (str, optional) Convert to a json string or save it to json file</>
  • to_toml(filename, encoding, errors) (str, optional) Convert to a toml string or save it to toml file</>
  • to_yaml(filename, default_flow_style, encoding, errors, **yaml_kwargs) (str, optional) Convert to a yaml string or save it to yaml file</>
  • unfreeze(recursive) Unfreeze the diot object</>
  • update(*value, **kwargs) Update the object. Shortcut: |=</>
  • update_recursively(*value, **kwargs) Update the object. Shortcut: |=</>
classmethod

from_namespace(namespace, recursive=True, diot_nest=True, diot_transform='safe', diot_frozen=False)

Get a Diot object from an argparse namespace

Example

~~~python

from argparse import Namespace Diot.from_namespace(Namespace(a=1, b=2)) ~~~

Parameters
  • namespace (Namespace) The namespace object
  • recursive (bool, optional) Do it recursively?
  • diot_nest (Union(bool, iterable of type), optional) Types to nestly convert values
  • diot_transform (Union(callable(str: str), str), optional) The transforms for keys
  • diot_frozen (bool or str, optional) Whether to generate a frozen diot.
    • - True: freeze the object recursively if there are Diot objects
    in descendants
    • - False: Don'f freeze
    • - shallow: Only freeze at depth = 1
  • diot_missing How to deal with missing keys when accessing them
    • - An exception class or object to raise
    • - None to return None
    • - A custom function with first argument the key and second
        the diot object.
Returns (Diot)

The converted diot object.

method

__setitem__(name, value)

Set self[key] to value.

method

__getitem__(name) → any

x.getitem(y) <==> x[y]

method

pop(name, *value)

Pop a key from the object and return the value. If key does notexist, return the given default value

Parameters
  • name (str) The key
Returns (any)

The value corresponding to the name or the default value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

popitem()

Pop last item from the object

Returns (str, any)

A tuple of key and value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

update(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

update_recursively(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

__or__(other)Diot

Return self|value.

method

__ior__(other)Diot

Return self|=value.

method

__delitem__(name)

Delete self[key].

method

freeze(frozen='shallow')

Freeze the diot object

Parameters
  • frozen (str or bool, optional) The frozen argument indicating how to freeze:shallow: only freeze at depth=1 True: freeze recursively if there are diot objects in children False: Disable freezing
method

unfreeze(recursive=False)

Unfreeze the diot object

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
generator

thaw(recursive=False)

A context manager for temporarily change the diot

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
Yields

self, the reference to this diot.

method

setdefault(name, value)

Set a default value to a key

Parameters
  • name (str) The key name
  • value (any) The default value
Returns (any)

The existing value or the value passed in

Raises
  • DiotFrozenError when try to set default to a frozen diot
method

accessible_keys()

Get the converted keys

Returns (iterable of str)

The accessible (transformed) keys

method

get(name, value=None)

Get the value of a key name

Parameters
  • name (str) The key name
  • value (any, optional) The value to return if the key does not exist
Returns (any)

The corresponding value or the value passed in if the key doesnot exist

method

__contains__(name) → bool

True if the dictionary has the specified key, else False.

method

clear()

Clear the object

method

copy()

Shallow copy the object

Returns (Diot)

The copied object

method

to_dict()

Turn the Box and sub Boxes back into a nativepython dictionary.

Returns (dict(str: any))

The converted python dictionary

method

to_json(filename=None, encoding='utf-8', errors='strict', **json_kwargs)

Convert to a json string or save it to json file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the json to, if not given a jsonstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **json_kwargs Other kwargs for json.dumps
Returns (str, optional)

The json string with filename is not given

method

to_yaml(filename=None, default_flow_style=False, encoding='utf-8', errors='strict', **yaml_kwargs)

Convert to a yaml string or save it to yaml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the yaml to, if not given a yamlstring will be returned
  • default_flow_style (bool, optional) The default flow style for yaml dumpingSee yaml.dump
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **yaml_kwargs Other kwargs for yaml.dump
Returns (str, optional)

The yaml string with filename is not given

method

to_toml(filename=None, encoding='utf-8', errors='strict')

Convert to a toml string or save it to toml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the toml to, if not given a tomlstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
Returns (str, optional)

The toml string with filename is not given

class

diot.diot.FrozenDiot(*args, **kwargs)

Bases

The frozen diot

Parameters
  • *args Anything that can be sent to dict construct
  • **kwargs keyword argument that can be sent to dict constructSome diot configurations can also be passed, including: diot_nest: Types to nestly convert values diot_transform: The transforms for keys diot_frozen: Whether to generate a frozen diot. True: freeze the object recursively if there are Diot objects in descendants False: Don'f freeze 'shallow': Only freeze at depth = 1
Methods
  • __contains__(name) (bool) True if the dictionary has the specified key, else False.</>
  • __delitem__(name) Delete self[key].</>
  • __getitem__(name) (any) x.getitem(y) <==> x[y]</>
  • __ior__(other) (Diot) Return self|=value.</>
  • __or__(other) (Diot) Return self|value.</>
  • __setitem__(name, value) Set self[key] to value.</>
  • accessible_keys() (iterable of str) Get the converted keys</>
  • clear() Clear the object</>
  • copy() (Diot) Shallow copy the object</>
  • freeze(frozen) Freeze the diot object</>
  • from_namespace(namespace, recursive, diot_nest, diot_transform, diot_frozen) (Diot) Get a Diot object from an argparse namespace</>
  • get(name, value) (any) Get the value of a key name</>
  • pop(name, *value) (any) Pop a key from the object and return the value. If key does notexist, return the given default value </>
  • popitem() (str, any) Pop last item from the object</>
  • setdefault(name, value) (any) Set a default value to a key</>
  • thaw(recursive) A context manager for temporarily change the diot</>
  • to_dict() (dict(str: any)) Turn the Box and sub Boxes back into a nativepython dictionary. </>
  • to_json(filename, encoding, errors, **json_kwargs) (str, optional) Convert to a json string or save it to json file</>
  • to_toml(filename, encoding, errors) (str, optional) Convert to a toml string or save it to toml file</>
  • to_yaml(filename, default_flow_style, encoding, errors, **yaml_kwargs) (str, optional) Convert to a yaml string or save it to yaml file</>
  • unfreeze(recursive) Unfreeze the diot object</>
  • update(*value, **kwargs) Update the object. Shortcut: |=</>
  • update_recursively(*value, **kwargs) Update the object. Shortcut: |=</>
classmethod

from_namespace(namespace, recursive=True, diot_nest=True, diot_transform='safe', diot_frozen=False)

Get a Diot object from an argparse namespace

Example

~~~python

from argparse import Namespace Diot.from_namespace(Namespace(a=1, b=2)) ~~~

Parameters
  • namespace (Namespace) The namespace object
  • recursive (bool, optional) Do it recursively?
  • diot_nest (Union(bool, iterable of type), optional) Types to nestly convert values
  • diot_transform (Union(callable(str: str), str), optional) The transforms for keys
  • diot_frozen (bool or str, optional) Whether to generate a frozen diot.
    • - True: freeze the object recursively if there are Diot objects
    in descendants
    • - False: Don'f freeze
    • - shallow: Only freeze at depth = 1
  • diot_missing How to deal with missing keys when accessing them
    • - An exception class or object to raise
    • - None to return None
    • - A custom function with first argument the key and second
        the diot object.
Returns (Diot)

The converted diot object.

method

__setitem__(name, value)

Set self[key] to value.

method

__getitem__(name) → any

x.getitem(y) <==> x[y]

method

pop(name, *value)

Pop a key from the object and return the value. If key does notexist, return the given default value

Parameters
  • name (str) The key
Returns (any)

The value corresponding to the name or the default value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

popitem()

Pop last item from the object

Returns (str, any)

A tuple of key and value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

update(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

update_recursively(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

__or__(other)Diot

Return self|value.

method

__ior__(other)Diot

Return self|=value.

method

__delitem__(name)

Delete self[key].

method

freeze(frozen='shallow')

Freeze the diot object

Parameters
  • frozen (str or bool, optional) The frozen argument indicating how to freeze:shallow: only freeze at depth=1 True: freeze recursively if there are diot objects in children False: Disable freezing
method

unfreeze(recursive=False)

Unfreeze the diot object

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
generator

thaw(recursive=False)

A context manager for temporarily change the diot

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
Yields

self, the reference to this diot.

method

setdefault(name, value)

Set a default value to a key

Parameters
  • name (str) The key name
  • value (any) The default value
Returns (any)

The existing value or the value passed in

Raises
  • DiotFrozenError when try to set default to a frozen diot
method

accessible_keys()

Get the converted keys

Returns (iterable of str)

The accessible (transformed) keys

method

get(name, value=None)

Get the value of a key name

Parameters
  • name (str) The key name
  • value (any, optional) The value to return if the key does not exist
Returns (any)

The corresponding value or the value passed in if the key doesnot exist

method

__contains__(name) → bool

True if the dictionary has the specified key, else False.

method

clear()

Clear the object

method

copy()

Shallow copy the object

Returns (Diot)

The copied object

method

to_dict()

Turn the Box and sub Boxes back into a nativepython dictionary.

Returns (dict(str: any))

The converted python dictionary

method

to_json(filename=None, encoding='utf-8', errors='strict', **json_kwargs)

Convert to a json string or save it to json file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the json to, if not given a jsonstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **json_kwargs Other kwargs for json.dumps
Returns (str, optional)

The json string with filename is not given

method

to_yaml(filename=None, default_flow_style=False, encoding='utf-8', errors='strict', **yaml_kwargs)

Convert to a yaml string or save it to yaml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the yaml to, if not given a yamlstring will be returned
  • default_flow_style (bool, optional) The default flow style for yaml dumpingSee yaml.dump
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **yaml_kwargs Other kwargs for yaml.dump
Returns (str, optional)

The yaml string with filename is not given

method

to_toml(filename=None, encoding='utf-8', errors='strict')

Convert to a toml string or save it to toml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the toml to, if not given a tomlstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
Returns (str, optional)

The toml string with filename is not given

class

diot.diot.OrderedDiot(*args, **kwargs)

Bases

With key order preserved

Parameters
  • *args Anything that can be sent to dict construct
  • **kwargs keyword argument that can be sent to dict constructSome diot configurations can also be passed, including: diot_nest: Types to nestly convert values diot_transform: The transforms for keys diot_frozen: Whether to generate a frozen diot. True: freeze the object recursively if there are Diot objects in descendants False: Don'f freeze 'shallow': Only freeze at depth = 1
Methods
  • __contains__(name) (bool) True if the dictionary has the specified key, else False.</>
  • __delitem__(name) Delete self[key].</>
  • __getitem__(name) (any) x.getitem(y) <==> x[y]</>
  • __ior__(other) (Diot) Return self|=value.</>
  • __iter__() Implement iter(self).</>
  • __or__(other) (Diot) Return self|value.</>
  • __reversed__() Return a reverse iterator over the dict keys.</>
  • __setitem__(name, value) Set self[key] to value.</>
  • accessible_keys() (iterable of str) Get the converted keys</>
  • clear() Clear the object</>
  • copy() (Diot) Shallow copy the object</>
  • copy() (OrderedDiot) Shallow copy the object</>
  • freeze(frozen) Freeze the diot object</>
  • from_namespace(namespace, recursive, diot_nest, diot_transform, diot_frozen) (Diot) Get a Diot object from an argparse namespace</>
  • get(name, value) (any) Get the value of a key name</>
  • insert(position, name, value) Insert an item to certain position</>
  • insert_after(existing_key, name, value) Insert items after the specified key</>
  • insert_before(existing_key, name, value) Insert items before the specified key</>
  • items() (iterator of (str, any)) Get the items in the order of the keys</>
  • keys() Get the keys in the order they are added</>
  • pop(name, *value) (any) Pop a key from the object and return the value. If key does notexist, return the given default value </>
  • popitem() (str, any) Pop last item from the object</>
  • setdefault(name, value) (any) Set a default value to a key</>
  • thaw(recursive) A context manager for temporarily change the diot</>
  • to_dict() (dict(str: any)) Turn the Box and sub Boxes back into a nativepython dictionary. </>
  • to_json(filename, encoding, errors, **json_kwargs) (str, optional) Convert to a json string or save it to json file</>
  • to_toml(filename, encoding, errors) (str, optional) Convert to a toml string or save it to toml file</>
  • to_yaml(filename, default_flow_style, encoding, errors, **yaml_kwargs) (str, optional) Convert to a yaml string or save it to yaml file</>
  • unfreeze(recursive) Unfreeze the diot object</>
  • update(*value, **kwargs) Update the object. Shortcut: |=</>
  • update_recursively(*value, **kwargs) Update the object. Shortcut: |=</>
  • values() Get the values in the order they are added</>
classmethod

from_namespace(namespace, recursive=True, diot_nest=True, diot_transform='safe', diot_frozen=False)

Get a Diot object from an argparse namespace

Example

~~~python

from argparse import Namespace Diot.from_namespace(Namespace(a=1, b=2)) ~~~

Parameters
  • namespace (Namespace) The namespace object
  • recursive (bool, optional) Do it recursively?
  • diot_nest (Union(bool, iterable of type), optional) Types to nestly convert values
  • diot_transform (Union(callable(str: str), str), optional) The transforms for keys
  • diot_frozen (bool or str, optional) Whether to generate a frozen diot.
    • - True: freeze the object recursively if there are Diot objects
    in descendants
    • - False: Don'f freeze
    • - shallow: Only freeze at depth = 1
  • diot_missing How to deal with missing keys when accessing them
    • - An exception class or object to raise
    • - None to return None
    • - A custom function with first argument the key and second
        the diot object.
Returns (Diot)

The converted diot object.

method

__getitem__(name) → any

x.getitem(y) <==> x[y]

method

popitem()

Pop last item from the object

Returns (str, any)

A tuple of key and value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

update(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

update_recursively(*value, **kwargs)

Update the object. Shortcut: |=

Raises
  • DiotFrozenError when try to update a frozen diot
method

__or__(other)Diot

Return self|value.

method

__ior__(other)Diot

Return self|=value.

method

freeze(frozen='shallow')

Freeze the diot object

Parameters
  • frozen (str or bool, optional) The frozen argument indicating how to freeze:shallow: only freeze at depth=1 True: freeze recursively if there are diot objects in children False: Disable freezing
method

unfreeze(recursive=False)

Unfreeze the diot object

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
generator

thaw(recursive=False)

A context manager for temporarily change the diot

Parameters
  • recursive (bool, optional) Whether unfreeze all diot objects recursively
Yields

self, the reference to this diot.

method

setdefault(name, value)

Set a default value to a key

Parameters
  • name (str) The key name
  • value (any) The default value
Returns (any)

The existing value or the value passed in

Raises
  • DiotFrozenError when try to set default to a frozen diot
method

accessible_keys()

Get the converted keys

Returns (iterable of str)

The accessible (transformed) keys

method

get(name, value=None)

Get the value of a key name

Parameters
  • name (str) The key name
  • value (any, optional) The value to return if the key does not exist
Returns (any)

The corresponding value or the value passed in if the key doesnot exist

method

__contains__(name) → bool

True if the dictionary has the specified key, else False.

method

copy()

Shallow copy the object

Returns (Diot)

The copied object

method

to_dict()

Turn the Box and sub Boxes back into a nativepython dictionary.

Returns (dict(str: any))

The converted python dictionary

method

to_json(filename=None, encoding='utf-8', errors='strict', **json_kwargs)

Convert to a json string or save it to json file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the json to, if not given a jsonstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **json_kwargs Other kwargs for json.dumps
Returns (str, optional)

The json string with filename is not given

method

to_yaml(filename=None, default_flow_style=False, encoding='utf-8', errors='strict', **yaml_kwargs)

Convert to a yaml string or save it to yaml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the yaml to, if not given a yamlstring will be returned
  • default_flow_style (bool, optional) The default flow style for yaml dumpingSee yaml.dump
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
  • **yaml_kwargs Other kwargs for yaml.dump
Returns (str, optional)

The yaml string with filename is not given

method

to_toml(filename=None, encoding='utf-8', errors='strict')

Convert to a toml string or save it to toml file

Parameters
  • filename (str, PathLike, or NoneType, optional) The filename to save the toml to, if not given a tomlstring will be returned
  • encoding (str, optional) The encoding for saving to file
  • errors (str, optional) The errors handling for saveing to fileSee python's open function
Returns (str, optional)

The toml string with filename is not given

method

__setitem__(name, value)

Set self[key] to value.

method

items()

Get the items in the order of the keys

Returns (iterator of (str, any))

The items (key-value) of the object

method

insert(position, name, value=None)

Insert an item to certain position

Parameters
  • position (int) The position where the name-value pair to be inserted
  • name (Union(str, (str, any))) The key name to be insertedIt could also be a tuple of key-value pair. In such a case, value is ignored. It could be an ordered dictionary as well
  • value (any, optional) The value to be inserted
Raises
  • ValueError when try to pass a value if name is key-value pair ora dictonary.
  • ValueError when name is a tuple but not with 2 elements
method

insert_before(existing_key, name, value=None)

Insert items before the specified key

Parameters
  • existing_key (str) The key where the new elements to be inserted before
  • name (str) The key name to be inserted
  • value (any, optional) The value to be insertedSame as name and value arguments for insert
Raises
  • KeyError when existing key does not exist
  • KeyError when name is an existing key
method

insert_after(existing_key, name, value=None)

Insert items after the specified key

Parameters
  • existing_key (str) The key where the new elements to be inserted after
  • name (str) The key name to be inserted
  • value (any, optional) The value to be insertedSame as name and value arguments for insert
Raises
  • KeyError when existing key does not exist
  • KeyError when name is an existing key
method

keys()

Get the keys in the order they are added

Returns

The keys (untransformed)

method

__iter__()

Implement iter(self).

method

values()

Get the values in the order they are added

Returns

The values of the object

method

__delitem__(name)

Delete self[key].

method

pop(name, *value)

Pop a key from the object and return the value. If key does notexist, return the given default value

Parameters
  • name (str) The key
Returns (any)

The value corresponding to the name or the default value

Raises
  • DiotFrozenError when try to pop from a frozen diot
method

__reversed__()

Return a reverse iterator over the dict keys.

method

clear()

Clear the object

method

copy()

Shallow copy the object

Returns (OrderedDiot)

The copied object