Util Data

Various helper functions for manipulating python data structures and values

Run doctests with

python -m doctest -v dffml/util/data.py

dffml.util.data.explore_directories(path_dict: dict)[source]

Recursively explores any path binded to a key in path_dict

Examples

>>> import pathlib
>>> import tempfile
>>> from dffml import explore_directories
>>>
>>> with tempfile.TemporaryDirectory() as root:
...     # Setup directories for example
...     STRUCTURE = '''
...     root
...     |
...     +--- deadbeef
...     |    |
...     |    +--- file1.txt
...     |    +--- colosseum
...     |         |
...     |         +--- battle.rst
...     +--- face
...          |
...          +--- file2.jpg
...     '''
...     # The writes will produce the 0's in the output (for doctest)
...     pathlib.Path(root, "deadbeef").mkdir()
...     pathlib.Path(root, "deadbeef", "file1.txt").write_text("")
...     pathlib.Path(root, "deadbeef", "colosseum").mkdir()
...     pathlib.Path(root, "deadbeef", "colosseum", "battle.rst").write_text("")
...     pathlib.Path(root, "face").mkdir()
...     pathlib.Path(root, "face", "file2.jpg").write_text("")
...     # Explore directories
...     root = explore_directories(path_dict={
...         "root": root
...     })["root"]
...     # Check that everything was found
...     bool("deadbeef" in root)
...     bool("colosseum" in root["deadbeef"])
...     bool("battle" in root["deadbeef"]["colosseum"])
...     bool("face" in root)
...     bool("file2" in root["face"])
0
0
0
True
True
True
True
True
dffml.util.data.export(obj)[source]

Convert a value from a Python object into something that is just primitives, so things that could be printed withough formatting issues or sent to json.dumps().

It works on typing.NamedTuple(), dataclasses.dataclass(), and anything that implements an export method that returns a dict.

Examples

>>> from dffml import export
>>> from typing import NamedTuple
>>>
>>> class MyType(NamedTuple):
...     data: int
>>>
>>> export(MyType(data=42))
{'data': 42}
>>>
>>> class MyBiggerType:
...     def export(self):
...         return {"feed": "face", "sub": MyType(data=42)}
>>>
>>> export(MyBiggerType())
{'feed': 'face', 'sub': {'data': 42}}
dffml.util.data.export_dict(**kwargs)[source]

Return the dict given as kwargs but first recurse into each element and call its export or _asdict function if it is not a serializable type.

dffml.util.data.ignore_args(func)[source]

Decorator to call the decorated function without any arguments passed to it.

async dffml.util.data.nested_apply(target: dict, func: Callable)[source]

Applies func recursively to all non dict types in target

dffml.util.data.parser_helper(value)[source]

Calls checks if value is string and if it is it converts it to a bool if the string is a string representation of common boolean value (on, off, true, false, yes, no). Otherwise it tries to call ast.literal_eval(), if that doesn’t succeed the string value is returned.

Examples

>>> from dffml import parser_helper
>>>
>>> parser_helper("on")
True
>>> parser_helper("[1, 2, 3]")
[1, 2, 3]
>>> parser_helper("1,2,3")
(1, 2, 3)
>>> parser_helper("hello")
'hello'
>>> parser_helper("'on'")
'on'
>>> parser_helper("on,off,feed,face,42")
[True, False, 'feed', 'face', 42]
>>> parser_helper("list,")
['list']
dffml.util.data.traverse_config_get(target, *args)[source]

Examples

>>> from dffml import traverse_config_get
>>>
>>> traverse_config_get({
...     "level": {
...         "plugin": None,
...         "config": {
...             "one": {
...                 "plugin": 1,
...                 "config": {},
...             },
...         },
...     },
... }, "level", "one")
1
dffml.util.data.traverse_config_set(target, *args)[source]

Examples

>>> from dffml import traverse_config_set
>>>
>>> traverse_config_set({
...     "level": {
...         "plugin": None,
...         "config": {
...             "one": {
...                 "plugin": 1,
...                 "config": {},
...             },
...         },
...     },
... }, "level", "one", 42)
{'level': {'plugin': None, 'config': {'one': {'plugin': 42, 'config': {}}}}}
dffml.util.data.traverse_get(target, *args)[source]

Travel down through a dict

Examples

>>> from dffml import traverse_get
>>>
>>> traverse_get({"one": {"two": 3}}, "one", "two")
3
>>> traverse_get({"one": {"two": 3}}, "one.two")
3
>>> traverse_get({"one.two": {"three": 4}}, "'one.two'.three")
4
dffml.util.data.traverse_set(target, *args, value)[source]

Examples

>>> from dffml import traverse_set
>>>
>>> d = {"one": {"two": 3}}
>>> traverse_set(d,"one.two", value = "Three")
>>> d["one"]["two"]
'Three'