packflow.utils package

Subpackages

Submodules

packflow.utils.data module

packflow.utils.data.check_delimiter_collisions(obj, delimiter, path='')[source]

Recursively check for keys containing the delimiter character.

Parameters:
  • obj (dict) – Dictionary to check for delimiter collisions

  • delimiter (str) – The delimiter character to check for

  • path (str) – Current path (for recursive calls)

Returns:

List of paths where delimiter collisions were found

Return type:

List[str]

packflow.utils.data.flatten_dict(obj, delimiter='.', flatten_lists=False)[source]

Create a flattened dictionary from a nested object.

Parameters:
  • obj (dict)

  • delimiter (str) – Default ‘.’

  • flatten_lists (bool) – Default False

Returns:

Flattened dictionary.

Return type:

dict

packflow.utils.data.flatten_records(records, delimiter='.', flatten_lists=False)[source]

Flatten a list of dictionaries

Parameters:
  • records (List[dict])

  • delimiter (str) – Default ‘.’

  • flatten_lists (bool) – Default False

Returns:

Flattened records.

Return type:

List[dict]

packflow.utils.data.get_nested_field(obj, field, delimiter=None)[source]

Retrieves a single nested field from a dictionary given a field name.

Example: get_nested_field({“foo”: {“bar”: 5}}, ‘foo.bar’) -> 5

Parameters:
  • obj (dict) – An arbitrary dictionary of data

  • field (str) – The key name to retrieve where subfields are split by $delimiter

Returns:

The value retrieved from the nested key

Return type:

Any

packflow.utils.data.get_nested_field_direct(obj, field, delimiter='.')[source]

Retrieves a nested field by traversing the dictionary structure directly. Does not use flattening, preserving keys that contain the delimiter.

Parameters:
  • obj (dict) – Dictionary to traverse

  • field (str) – Field path using delimiter notation (e.g., “a.b.c”)

  • delimiter (str) – Delimiter to split the field path

Returns:

The value at the nested path, or None if not found

Return type:

Any

packflow.utils.data.records_to_ndarray(records, feature_names, dtype=None, delimiter=None)[source]

Converts records to a numpy nd array

Example

in: [{“num”: 1, “num2”: 1}, {“num”: 2, “num”: 2}] args: feature_names=[“num”, “num2”] out: np.array([[1, 1], [2, 2]])

Parameters:
  • records (List[Dict]) – A list of dictionary items to be converted

  • feature_names (List[str]) – The values to extract for each row

  • dtype (str) – The numpy data type to coerce the array to. Defaults to ‘float32’

Return type:

numpy.ndarray

packflow.utils.data.set_nested_field_direct(obj, field, value, delimiter='.')[source]

Sets a nested field by creating the nested structure directly. Does not use flattening/unflattening.

Parameters:
  • obj (dict) – Dictionary to modify (modified in place)

  • field (str) – Field path using delimiter notation (e.g., “a.b.c”)

  • value (Any) – Value to set at the nested path

  • delimiter (str) – Delimiter to split the field path

Return type:

None

Module contents