scitex_core.dict

Scitex Core dict module.

class scitex_core.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.).

__init__(dictionary=None)[source]
get(key, default=None)[source]
to_dict()[source]

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

__str__()[source]

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

__repr__()[source]

Returns a string representation suitable for debugging. Shows the class name and the internal data representation.

__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_core.dict.listed_dict(keys=None)[source]
Example 1:

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]})

Example 2:

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_core.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_core.dict.replace(string, dict)[source]
scitex_core.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_core.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_core.dict.flatten(nested_dict, parent_key='', sep='_')[source]