dicts
Utility functions for dealing with dictionaries.
Remove ¶
A sentinel class used in quacc to mark a key in a dictionary for removal.
Note: This is more robust than using None
as the sentinel value because
None
is a valid value for many keyword arguments.
Source code in quacc/utils/dicts.py
clean_dict ¶
Clean up a task document dictionary by removing all entries that are None and sorting the dictionary alphabetically by key.
Parameters:
Returns:
-
dict
–Cleaned dictionary
Source code in quacc/utils/dicts.py
finalize_dict ¶
finalize_dict(
task_doc: dict,
directory: str | Path | None,
gzip_file: bool = True,
store: Store | None = None,
) -> dict
Finalize a schema by cleaning it and storing it in a database and/or file.
Parameters:
-
task_doc
(dict
) –Dictionary representation of the task document.
-
directory
(str | Path | None
) –Directory where the results file is stored.
-
gzip_file
(bool
, default:True
) –Whether to gzip the results file.
-
store
(Store | None
, default:None
) –Maggma Store object to store the results in.
Returns:
-
dict
–Cleaned task document
Source code in quacc/utils/dicts.py
recursive_dict_merge ¶
recursive_dict_merge(
*dicts: MutableMapping[str, Any] | None,
remove_trigger: Any = Remove,
verbose: bool = False
) -> MutableMapping[str, Any]
Recursively merge several dictionaries, taking the latter in the list as higher
preference. Also removes any entries that have a value of remove_trigger
from the
final dictionary. If a None
is provided, it is assumed to be {}
.
This function should be used instead of the | operator when merging nested dictionaries,
e.g. {"a": {"b": 1}} | {"a": {"c": 2}}
will return {"a": {"c": 2}}
whereas
recursive_dict_merge({"a": {"b": 1}}, {"a": {"c": 2}})
will return {"a": {"b": 1, "c": 2}}
.
Parameters:
-
*dicts
(MutableMapping[str, Any] | None
, default:()
) –Dictionaries to merge
-
remove_trigger
(Any
, default:Remove
) –Value to that triggers removal of the entry
-
verbose
(bool
, default:False
) –Whether to log warnings when overwriting keys
Returns:
-
MutableMapping[str, Any]
–Merged dictionary
Source code in quacc/utils/dicts.py
remove_dict_entries ¶
For a given dictionary, recursively remove all items that are the remove_trigger
.
Parameters:
-
start_dict
(dict[str, Any]
) –Dictionary to clean
-
remove_trigger
(Any
) –Value to that triggers removal of the entry
Returns:
-
dict
–Cleaned dictionary
Source code in quacc/utils/dicts.py
safe_dict_copy ¶
sort_dict ¶
For a given dictionary, recursively sort all entries alphabetically by key.
Parameters:
Returns:
-
dict
–Sorted dictionary