Skip to content

Data Manipulation

Coalescing, type checking, and data standardization.


coalesce

Python
coalesce(*args)

Returns the first non-None value in the given list of arguments. If all arguments are None, returns None.

RETURNS DESCRIPTION
any **Example**:: >>> coalesce(None, None, "first non-none", 5) 'first non-none' >>> coalesce(None, None, None) None **Note**: This function is analogous to the SQL COALESCE function and is useful for defaulting values when multiple potential inputs may contain None.

The first argument that is not None, or None if all arguments are None.

standardize_none

Python
standardize_none(data: Any, none_like_values: set = None, evaluate_as_string: bool = False) -> Any

Recursively standardizes mistyped None values to proper None. --> ('', ' ', 'null', 'none', 'nan', 'n/a', 'na', 'undefined', 'missing', 'nil', 'void', 'blank') Handles pandas DataFrames and Series if pandas is available.

Text Only
                     If not provided, defaults to common None-like placeholders.
RETURNS DESCRIPTION
Any

The standardized data with all mistyped None values replaced with proper None.

typechecker

Python
typechecker(data: Union[Dict[str, Any], List[Dict[str, Any]]], expected_types: Dict[str, Union[Type, List[Type]]], none_is_ok: bool = False, errors: str = 'raise', verbose: bool = False) -> bool

Validates that each entry in a dictionary or a list of dictionaries matches the expected type.

Args: data: A dictionary or a list of dictionaries to validate. expected_types: A dictionary mapping parameter names to the expected types. Each type can be a single type or a list of types. none_is_ok: If True, allows None as a valid value. errors: A string that determines the error handling strategy. If set to 'raise', the function will raise a TypeError when a mismatch is found. If set to 'coerce', the function will attempt to coerce the value to the expected type.

Raises: TypeError: If any parameter does not match the expected type and 'errors' is set to 'raise'. ValueError: If any required parameter is missing.

Returns: bool: True if the data is valid, False otherwise.

Usage example: >>> from WrenchCL.Tools import typechecker

Text Only
>>> data = [
>>>     {"name": "John", "age": 30},
>>>     {"name": "Jane", "age": "twenty"}
>>> ]
>>> expected_types = {"name": str, "age": int}

>>> try:
>>>     result = typechecker(data, expected_types, none_is_ok=False, errors='raise')
>>>     print("Data is valid:", result)
>>> except (TypeError, ValueError) as e:
>>>     print(f"Validation failed: {e}")

# This will raise a TypeError because the age of "Jane" is not an int