Utils Module

The utils module provides configuration management and utility functions.

Helper Functions (Internal)

xflow.utils.helper.split_sequence(items: Sequence[T], split_ratio: float = 0.8, seed: int = 42, shuffle: bool = True) Tuple[List[T], List[T]][source]

Split a sequence into two parts.

Parameters:
  • items – Any sequence (list, tuple, etc.) of type T.

  • split_ratio – Ratio for first part (0.0 to 1.0).

  • seed – Random seed for reproducibility.

  • shuffle – Whether to shuffle before splitting.

Returns:

Tuple of (first_part, second_part) as lists of type T.

xflow.utils.helper.subsample_sequence(items: Sequence[T], n_samples: int | None = None, fraction: float | None = None, strategy: str = 'random', seed: int | None = 42) List[T][source]

Subsampling function for any Sequence.

Parameters:
  • items – Any sequence (list, tuple, etc.) of type T.

  • n_samples – Exact number to sample.

  • fraction – Fraction to sample (0.0 to 1.0).

  • strategy – “random”, “first”, “last”, “stride”, or “reservoir”.

  • seed – Random seed for reproducibility.

Returns:

List of sampled items of type T.

xflow.utils.helper.deep_update(base: MutableMapping[str, Any], updates: Dict[str, Any]) None[source]

Recursively update a dictionary with another dictionary.

Nested dictionaries are merged, other values are replaced. Modifies base dictionary in-place.

Parameters:
  • base – Dictionary to update (modified in-place)

  • updates – Dictionary with updates to apply

Example

>>> base = {"a": {"x": 1, "y": 2}, "b": 3}
>>> updates = {"a": {"x": 10, "z": 3}, "c": 4}
>>> deep_update(base, updates)
>>> base
{"a": {"x": 10, "y": 2, "z": 3}, "b": 3, "c": 4}

IO Functions (Internal)

xflow.utils.io.scan_files(root_paths: str | PathLike[str] | List[str | PathLike[str]], extensions: str | List[str] | None = None, return_type: str = 'path', recursive: bool = True) List[str] | List[Path][source]

Scan directories for files with specified extensions.

Parameters:
  • root_paths – Single path or list of paths to scan

  • extensions – File extensions to include (e.g., ‘.jpg’ or [‘.jpg’, ‘.png’]). If None, includes all files.

  • return_type – “path” to return Path objects, “str” to return strings

  • recursive – Whether to scan subdirectories recursively

Returns:

Sorted list of file paths

xflow.utils.io.copy_file(source_path: str | PathLike[str], target_path: str | PathLike[str], filename: str | None = None) Path[source]

Copy a file to target directory.

Parameters:
  • source_path – Source file path

  • target_path – Target directory path

  • filename – Optional new filename (uses source filename if None)

Returns:

Path to the copied file

xflow.utils.parser.load_file(file_path: str | PathLike[str]) Any[source]

Load config file.

xflow.utils.parser.save_file(data: Any, file_path: str | PathLike[str]) None[source]

Save data to file. Format determined by file extension.