The aux_tools submodule
Utility helpers for argument handling, unit conversion, and validators.
This module provides small building blocks used throughout fluidsolve for consistent argument parsing and validation.
Main parts:
Unit helpers:
toUnitsconverts numeric values and quantities to a requested unit.Argument helpers:
prepareArgsremovesNonevalues from keyword dictionaries andspecbuilds readable component specification dictionaries.Argument processor:
GetArgsprovides structured extraction/consumption of keyword arguments with validation chains.Validator factory:
vFuncontains static methods that return validator/transformer callables forGetArgs.getArg.
Typical usage pattern:
Wrap incoming kwargs with
GetArgs.Retrieve each expected argument through
getArgand a list of validators (defaults, type checks, conversions).Call
isEmptyto ensure no unexpected arguments are left.
Example:
args = GetArgs(kwargs)
name = args.getArg('name', [vFun.default(''), vFun.istype(str)])
roughness = args.getArg('e', [vFun.default(15), vFun.tounits(u.um)])
args.isEmpty()
This approach keeps component constructors compact, explicit, and uniform.
- fluidsolve.aux_tools.toUnits(value: int | float | Quantity, units: Quantity, magnitude: bool = False) Quantity[source]
Convert a value to a Quantity in the requested units.
- Parameters:
value (int | float | Quantity) – The input value.
units (Quantity) – The Quantity units to convert to.
magnitude (bool, optional) – If True, only the magnitude is returned.
- Returns:
Value converted to the requested units.
- Return type:
Quantity
- fluidsolve.aux_tools.prepareArgs(**kwargs: int) dict[source]
- Build an argument dict from kwargs, skipping None values.
This lets downstream functions use their own defaults when an argument was not provided.
- Returns:
Filtered argument dictionary.
- Return type:
dict
- fluidsolve.aux_tools.getPumpCurveDataText(data_in: str) list[source]
- Parse Q-H data copied from a pump curve digitizer.
Use eg. https://plotdigitizer.com/app or https://web.eecs.utk.edu/~dcostine/personal/PowerDeviceLib/DigiTest/index.html or similar
- Parameters:
data_in (str) – The input data from the pump curve.
- Returns:
Parsed numeric values.
- Return type:
list
- fluidsolve.aux_tools.spec(**kwargs: Any) Any[source]
Helper to define a component specification entry.
Example
spec(comp=’Tube’, nodes=[‘A’, ‘B’], sense=-1, D=50)
- class fluidsolve.aux_tools.GetArgs(args_in: dict | None = None)[source]
Bases:
objectProcess and validate an argument dictionary.
- Parameters:
args_in (dict, optional) – Input key-value pairs.
- Raises:
TypeError – Raised when args_in is not a dict.
- Returns:
None
- getArg(name: str, validators: list = [], remove: Any = True) Any[source]
- Get an argument value based on the key from an argument dict or kwargs dict.
A list of validator functions (see class vFun) can be added to validate or to modify the passed argument. Then this item is removed from the list of arguments (thus making it possible to check if every argument is used just once).
- Parameters:
name (str) – Key of the argument in the internal argument dict.
validators (list, optional) – The list with validator functions.
remove (bool, optional) – Remove the argument after processing.
- Raises:
TypeError – Name has to be a string
TypeError – Validators has to be a list
ValueError – Name is not found in the input arguments
TypeError – A validator is not a function
- Returns:
Validated or transformed argument value.
- Return type:
Any
- addArg(key: str, value: Any) None[source]
- Add one argument to the internal dictionary.
Overwrites the value if the key already exists.
- Parameters:
key (str) – Key of the argument to add.
value (Any) – Value of the argument to add.
- Returns:
None
- addArgs(extra_args: dict = {}) None[source]
- Add extra (default) arguments to the existing dict if not already there.
Can be used to set some default values.
- Parameters:
extra_args (dict, optional) – Default key-value pairs.
- Returns:
None
- restArgs() dict[source]
Return the remaining, unprocessed arguments.
- Returns:
Remaining arguments.
- Return type:
dict
- isEmpty(raiseerror: bool = True) bool[source]
- Check whether all arguments have been processed.
Raises or reports that arguments are still left.
- Parameters:
raiseerror (bool, optional) – Raise an error when arguments remain.
- Raises:
TypeError – Raised when arguments are left and raiseerror is True.
- Returns:
True when empty, False when arguments remain.
- Return type:
bool
- class fluidsolve.aux_tools.vFun[source]
Bases:
objectStatic validator helpers for use with GetArgs. Includes sanitizers, converters, and validation checks.
Every static method returns a function (Callable) with 2 arguments: The first one is the name for the argument (used for eventual error generation). The second one is the argument to be validated itself. Validators always return the (possibly modified) argument.
- static default(default: Any) Callable[[...], Any][source]
Give a default value for the argument.
- Parameters:
default (Any) – The default value.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static totype(type_type: Any, need: bool = True) Callable[[...], Any][source]
Cast the argument to the desired type.
- Parameters:
type_type (Any) – The type to be cast to.
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static stripspaces(need: bool = True) Callable[[...], str][source]
Strip the leading and trailing spaces from the argument.
- Parameters:
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, str]
- static tolower(need: bool = True) Callable[[...], str][source]
Set the argument to lower case.
- Parameters:
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, str]
- static toupper(need: bool = True) Callable[[...], str][source]
Set the argument to upper case.
- Parameters:
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, str]
- static tounits(units: Any, magnitude: bool = False, need: bool = True) Callable[[...], Any | Quantity][source]
- Convert the argument to a Quantity with desired units.
Optionally, only the magnitude is returned.
- Parameters:
units (Any) – The desired units.
magnitude (bool, optional) – If True, return only the magnitude.
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, Any | Quantity]
- static sanitizefilepath(need: bool = True) Callable[[...], Any][source]
Sanitize the argument as a filepath.
- Parameters:
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static tolambda(fun: Any, need: bool = True) Callable[[...], Any][source]
- Execute a transformation function.
- Eg. vFun.tolambda(lambda x: x.to(u.m) if isinstance(x, Quantity) else x * u.m)
vFun.tolambda(lambda x: x if isinstance(x, flsm.Medium) else flsm.Medium(prd=x))
- Parameters:
fun (Any) – The callable transformation.
need (bool, optional) – if False, this argument can also be None
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static istype(*type_type: Any, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check whether the argument matches one or more allowed types.
- Parameters:
type_type (list | tuple | Any) – Allowed type(s).
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static notnone(errmsg: str | None = None) Callable[[...], Any][source]
Check that the argument is not None.
- Parameters:
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static notempty(errmsg: str | None = None) Callable[[...], Any][source]
Check that the argument is not empty.
- Parameters:
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static haslen(length: int, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check that the argument length equals the expected length.
- Parameters:
length (int) – Desired length.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static lenmax(max_length: int, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check that the argument length does not exceed a maximum.
- Parameters:
max_length (int) – Max length.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static lenmin(min_length: int, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check that the argument length is at least a minimum.
- Parameters:
min_length (int) – Min length.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static inrange(low: int | float, high: int | float, inv: bool = False, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check if a value is inside or outside a range.
- Parameters:
low (int | float) – Min value
high (int | float) – Max value
inv (bool, optional) – If True value must be in range, if False it must be out of the range.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, None]
- static inlist(*items: Any, inv: bool = False, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
- Check whether a value is contained in a list (or excluded if inv=True).
This function accepts either a single list or tuple, or multiple individual arguments. If the condition fails and errmsg is provided, a ValueError is raised.
- Parameters:
items (list | tuple | Any) – Allowed (or disallowed) values.
inv (bool, optional) – False to allow listed values, True to forbid them.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
Examples
>>> inlist(1, 2, 3) True
>>> inlist([1, 2, 3]) True
>>> inlist(0, 0, inv=True) True
>>> inlist(0, errmsg="Invalid input") Traceback (most recent call last): ... ValueError: Invalid input
- static regex(expr: str, inv: bool = False, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check whether the argument matches a regex rule.
- Parameters:
expr (str) – The regex expression.
inv (bool, optional) – If True, the regex must apply, if False: the regex may not apply.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
Example
vFun.regex(r”^[0-9a-zA-Z]*$”)
- static fileexists(need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check if a file exists.
- Parameters:
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static isfilereadable(need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check if a file is readable.
- Parameters:
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static isfilewritable(need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check if a file is writable.
- Parameters:
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static isfileexecutable(need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check if a file is executable.
- Parameters:
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
- static islambda(condition: Any, need: bool = True, errmsg: str | None = None) Callable[[...], Any][source]
Check if a callable condition is fulfilled for the current argument.
The condition must be a callable of the form
condition(argvalue) -> bool.- Parameters:
condition (Any) – Callable condition evaluated on the current argument.
need (bool, optional) – if False, this argument can also be None
errmsg (str, optional) – The eventual error message.
- Returns:
The validator function.
- Return type:
Callable[…, Any]
Example
vFun.islambda(lambda x: x > 0, errmsg='must be positive')