scitex.dict API Reference

Dictionary utilities (DotDict, safe_merge) for the SciTeX ecosystem.

class scitex.dict.DotDict(dictionary=None)[source]

Bases: object

A dictionary-like object that allows attribute-like access (for valid identifier keys) and standard item access for all keys (including integers, etc.).

get(key, default=None)[source]
to_dict(include_private=False)[source]

Recursively converts DotDict and nested DotDict objects back to ordinary dictionaries.

Parameters:

include_private – If False, exclude keys starting with ‘_’ (default: False)

__str__()[source]

Returns a string representation, handling non-JSON-serializable objects.

__repr__()[source]

Returns a string representation suitable for debugging. Returns a nicely formatted representation that pprint can display properly. Private keys (starting with ‘_’) are hidden by default.

pformat(indent=2, width=80, depth=None, compact=False, include_private=False)[source]

Return a pretty-formatted string representation of the DotDict.

Parameters:
  • indent – Number of spaces per indentation level (default: 2)

  • width – Maximum line width (default: 80)

  • depth – Maximum depth to print (default: None for unlimited)

  • compact – If True, use more compact representation (default: False)

  • include_private – If True, include keys starting with ‘_’ (default: False)

Returns:

Pretty-formatted string

__len__()[source]

Returns the number of key-value pairs in the dictionary.

keys()[source]

Returns a view object displaying a list of all the keys.

values()[source]

Returns a view object displaying a list of all the values.

items()[source]

Returns a view object displaying a list of all the items (key, value pairs).

update(dictionary)[source]

Updates the dictionary with the key-value pairs from another dictionary or iterable.

setdefault(key, default=None)[source]

Returns the value of the given key. If the key does not exist, insert the key with the specified default value and return the default value.

pop(key, *args)[source]

Removes the specified key and returns the corresponding value. If key is not found, default is returned if given, otherwise KeyError is raised. Accepts optional default value like dict.pop.

__contains__(key)[source]

Checks if the dotdict contains the specified key.

__iter__()[source]

Returns an iterator over the keys of the dictionary.

copy()[source]

Creates a shallow copy of the DotDict object. Nested DotDicts/dicts/lists will be references, not copies. Use deepcopy for a fully independent copy.

__dir__()[source]

Provides attribute suggestions for dir() and tab completion. Includes both standard methods/attributes and the keys stored in _data.

__eq__(other)[source]

Check equality. Supports comparison with dict, DotDict, and scalar values.

__ne__(other)[source]

Check inequality.

__lt__(other)[source]

Less than comparison - delegate to _data or handle scalars.

__le__(other)[source]

Less than or equal.

__gt__(other)[source]

Greater than comparison - delegate to _data or handle scalars.

__ge__(other)[source]

Greater than or equal.

__bool__()[source]

Truth value testing. Empty DotDict is False, non-empty is True.

scitex.dict.listed_dict(keys=None)[source]

Return a defaultdict(list), optionally pre-seeded with empty lists.

Examples

>>> import random
>>> random.seed(42)
>>> d = listed_dict()
>>> for _ in range(10):
...     d['a'].append(random.randint(0, 10))
>>> print(d)
defaultdict(<class 'list'>, {'a': [10, 1, 0, 4, 3, 3, 2, 1, 10, 8]})
>>> import random
>>> random.seed(42)
>>> keys = ['a', 'b', 'c']
>>> d = listed_dict(keys)
>>> for _ in range(10):
...     d['a'].append(random.randint(0, 10))
...     d['b'].append(random.randint(0, 10))
...     d['c'].append(random.randint(0, 10))
>>> print(d)
defaultdict(<class 'list'>, {'a': [10, 4, 2, 8, 6, 1, 8, 8, 8, 7],
                             'b': [1, 3, 1, 1, 0, 3, 9, 3, 6, 9],
                             'c': [0, 3, 10, 9, 0, 3, 0, 10, 3, 4]})
scitex.dict.pop_keys(keys_list, keys_to_pop)[source]

Remove specified keys from a list of keys.

Parameters:
  • keys_list (list) – The original list of keys.

  • keys_to_pop (list) – The list of keys to remove from keys_list.

Returns:

A new list with the specified keys removed.

Return type:

list

Example

>>> keys_list = ['a', 'b', 'c', 'd', 'e', 'bde']
>>> keys_to_pop = ['b', 'd']
>>> pop_keys(keys_list, keys_to_pop)
['a', 'c', 'e', 'bde']
scitex.dict.replace(string, dict)[source]
scitex.dict.safe_merge(*dicts)[source]

Merges dictionaries while checking for key conflicts.

Example

>>> dict1 = {'a': 1, 'b': 2}
>>> dict2 = {'c': 3, 'd': 4}
>>> safe_merge(dict1, dict2)
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
Parameters:

*dicts (Dict[_Any, _Any]) – Variable number of dictionaries to merge

Returns:

Merged dictionary

Return type:

Dict[_Any, _Any]

Raises:

ValueError – If overlapping keys are found between dictionaries

scitex.dict.to_str(dictionary, delimiter='_')[source]

Convert a dictionary to a string representation.

Example

input_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3} result = dict2str(input_dict) print(result) # Output: a-1_b-2_c-3

Parameters:
  • dictionary (dict) – The input dictionary to be converted.

  • delimiter (str, optional) – The separator between key-value pairs (default is “_”).

Returns:

A string representation of the input dictionary.

Return type:

str

scitex.dict.flatten(nested_dict, parent_key='', sep='_')[source]