API Reference
UtilsX - a collection of generic Python utility functions and types.
collections
Utilities for working with collections.
check_equal_length(*collections)
Given an arbitrary number of collections, check if they all have equal length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*collections
|
Sized
|
Objects which have length to be checked for its equality. |
()
|
Returns:
Type | Description |
---|---|
bool
|
Whether all provided collections have equal length. |
Raises:
Type | Description |
---|---|
ValueError
|
If no collections provided. |
Source code in src/utilsx/collections.py
get_duplicates(iterable)
Get a set of all values in a collection that are duplicates, i.e., present more than once.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable
|
Iterable[T]
|
A collection to check. |
required |
Returns:
Type | Description |
---|---|
frozenset[T]
|
A set of values that are present more than once. |
Source code in src/utilsx/collections.py
is_collection_of_equal_elements(collection)
Check whether all elements in a collection are equal to each other.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collection
|
Collection[Any]
|
A collection to check that all elements are equal. |
required |
Returns:
Type | Description |
---|---|
bool
|
Whether all elements are equal to each other. |
Source code in src/utilsx/collections.py
constants
Common constant values from math, physics, etc.
decorators
Profiling pipeline nodes.
narrow_return(index)
Makes a function returning a tuple return only the element at the given index.
Implemented as a decorator factory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index
|
int
|
The index of the tuple element to return. |
required |
Returns:
Type | Description |
---|---|
Callable[[Callable[..., tuple[Any, ...]]], Callable[..., Any]]
|
A decorator. |
Source code in src/utilsx/decorators.py
dicts
Utilities for working with dictionaries.
multiply_dict_values(dictionary, multiplier)
Get a copy of the dictionary with values multiplied by scalar, preserving keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
Mapping[T, float]
|
A dictionary to multiply values of. |
required |
multiplier
|
float
|
A scalar multiplier. |
required |
Returns:
Type | Description |
---|---|
dict[T, float]
|
A copy of the original dictionary with values multiplied by scalar. |
Source code in src/utilsx/dicts/_modification.py
remove_items_with_zero_values(dictionary)
Drop key-value pairs from a dictionary whose values are zero.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict[T, float]
|
To be filtered to exclude key-value pairs with zero values. |
required |
Returns:
Type | Description |
---|---|
dict[T, float]
|
A subset of the original dictionary items, only pairs with non-zero values. |
Source code in src/utilsx/dicts/_filtering.py
rename_keys_in_nested_dict(dictionary, renaming)
Replace all specified keys by other specified names in an arbitrarily deep dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict[str, Any]
|
A dictionary of arbitrary depth. |
required |
renaming
|
dict[str, str]
|
A mapping from old to new key names. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Copy of the original dictionary with |
dict[str, Any]
|
at all levels of key depth. |
Source code in src/utilsx/dicts/_modification.py
sort_by_value(dictionary, reverse=False)
Sort a dictionary with numeric values by those values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dictionary
|
dict[T, NumberT]
|
A dictionary to sort by value. |
required |
reverse
|
bool
|
False for ascending order, True for descending. Exactly matches the |
False
|
Returns:
Type | Description |
---|---|
dict[T, NumberT]
|
Same dictionary in terms of content, just sorted by value. |
Source code in src/utilsx/dicts/_sorting.py
sum_dicts(*dicts)
Given dictionaries, return their summation: a union of keys and totals of values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*dicts
|
dict[T, float]
|
To be added up together, any number. |
()
|
Returns:
Type | Description |
---|---|
dict[T, float]
|
A combined dictionary with a union of keys and totals of values. |
Source code in src/utilsx/dicts/_combination.py
exceptions
Utilities for raising exceptions.
hint_if_extra_uninstalled(required_modules, extra_name, package_name)
Check if an optional dependency group is installed, and hint if not, via ImportError
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
required_modules
|
Iterable[str]
|
Modules which need to be installed in venv for that dependency group. |
required |
extra_name
|
str
|
Name of an optional dependency group. |
required |
package_name
|
str
|
Name of the package which provides a given optional dependency group. |
required |
Raises:
Type | Description |
---|---|
ImportError
|
If any of the required modules are not installed. |
Source code in src/utilsx/exceptions.py
prohibit_negative_values(values, exception_class=ValueError, exception_msg='Negative values are prohibited')
Raise an exception if an iterable of numbers has negative values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Iterable[float]
|
To check for any negative member. |
required |
exception_class
|
type[Exception]
|
Exception class to raise if any member is negative,
defaults to |
ValueError
|
exception_msg
|
str
|
A message to add to the raised exception. |
'Negative values are prohibited'
|
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in src/utilsx/exceptions.py
raise_key_error_with_suggestions(attempted_key, existing_keys, object_name='object', attribute_name='key')
Raise a key error complemented with suggestions based on closest matches.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attempted_key
|
str
|
A key that was attempted to be found. |
required |
existing_keys
|
Collection[str]
|
Existing keys, among which an attempted key was not found. |
required |
object_name
|
str
|
Archetype of an object that was searched by key. |
'object'
|
attribute_name
|
str
|
If this key represents an attribute with explicit name. |
'key'
|
Returns:
Type | Description |
---|---|
NoReturn
|
Never returns anything. |
Raises:
Type | Description |
---|---|
KeyError
|
Complemented with close matches, if any. |
Notes
Inspired by dataset name hint implemented in Kedro: https://github.com/kedro-org/kedro
Source code in src/utilsx/exceptions.py
functional
math
Utilities for mathematical operations.
ceil_to_multiple(x, multiple)
Ceil a number to the next multiple of another value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float
|
Number to ceil. |
required |
multiple
|
int
|
Enforce the output to be a multiple of. |
required |
Returns:
Type | Description |
---|---|
int
|
X input ceiled to the next multiple of another value. |
Source code in src/utilsx/math/_rounding.py
check_values_add_up_to_one(values, mode='either')
Check if values in a collection add up to 1 or 100.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
values
|
Collection[float]
|
Values to check. |
required |
mode
|
Literal['fractions', 'percentages', 'either']
|
"fractions" if they should add up to 1, "percentages" if they should add up to 100, "either" if either of this works. |
'either'
|
Returns:
Type | Description |
---|---|
bool
|
Boolean outcome of the check. |
Source code in src/utilsx/math/_collections.py
convert_number_to_units(number, units)
Convert a number to thousands or millions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
number
|
float
|
Number to convert. |
required |
units
|
_TUnits
|
Units to convert to. |
required |
Returns:
Type | Description |
---|---|
float
|
A number converted to specified units. |
Source code in src/utilsx/math/_downscaling.py
double(x)
Multiply a number by two: in other words, double it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float
|
The number to multiply by two. |
required |
Returns:
Type | Description |
---|---|
float
|
Result of multiplying this number by literal integer two. |
halve(x)
Divide a number by two: in other words, get its half.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
float
|
The number to divide by two. |
required |
Returns:
Type | Description |
---|---|
float
|
Result of dividing this number by literal integer two. |
is_monotonically_growing(time_series, multiplier)
Check whether a time series can be considered monotonically growing.
To be called so, each next element should be at least multiplier
times bigger than
a previous one. Series of less than two elements are considered non-growing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_series
|
Sequence[float]
|
A sequence of numbers. |
required |
multiplier
|
float
|
How many times bigger each next element should be. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if monotonically growing, False otherwise. |
Source code in src/utilsx/math/_collections.py
normalize(values)
safe_divide(numerator, denominator, fallback=0)
Divide one number by another, falling back on something in case of ZeroDivisionError
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numerator
|
float
|
Number to divide. |
required |
denominator
|
float
|
Denominator to use. |
required |
fallback
|
float
|
Fallback value to use in case the denominator is zero, defaults to zero. |
0
|
Source code in src/utilsx/math/_division.py
pandas
Utilities for enhancing your Pandas workflows.
count_na(df)
Get the total number of missing values in a DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
To count missing values in. |
required |
Returns:
Type | Description |
---|---|
int
|
The number of missing values. |
text
Utilities for text transformations.
add_suffix(base, suffix, separator='_')
Add suffix to a base string using a separator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base
|
str
|
Base string to add a suffix to. |
required |
suffix
|
str
|
A suffix to add. |
required |
separator
|
str
|
A separator to insert between the base and suffix. |
'_'
|
Returns:
Type | Description |
---|---|
str
|
A string with suffix concatenated to the base on the right, with a separator in between. |
If the suffix is empty, returns just the base string, omitting the separator.
Source code in src/utilsx/text.py
typevars
A collection of type variables.