floris.type_dec#

Functions

convert_to_path(fn)

Converts an input string or pathlib.Path object to a fully resolved pathlib.Path object.

floris_array_converter(data)

For a given iterable, convert the data to a numpy array and cast to floris_float_type.

floris_numeric_dict_converter(data)

For the given dictionary, convert all the values to a numeric type.

iter_validator(iter_type, item_types)

Helper function to generate iterable validators that will reduce the amount of boilerplate code.

Classes

FromDictMixin()

A Mixin class to allow for kwargs overloading when a data class doesn't have a specific parameter defined.

floris.type_dec.floris_array_converter(data)[source]#

For a given iterable, convert the data to a numpy array and cast to floris_float_type. If the input is a scalar, np.array() creates a 0-dimensional array, and this is not supported in FLORIS so this function raises an error.

Return type:

ndarray

Parameters:

data (Iterable) --

Args:

data (Iterable): The input data to be converted to a Numpy array.

Raises:

TypeError: Raises if the input data is not iterable. TypeError: Raises if the input data cannot be converted to a Numpy array.

Returns:

np.ndarray: data converted to a Numpy array and cast to floris_float_type.

floris.type_dec.floris_numeric_dict_converter(data)[source]#

For the given dictionary, convert all the values to a numeric type. If a value is a scalar, it will be converted to a float. If a value is an iterable, it will be converted to a Numpy array and cast to floris_float_type. If a value is not a numeric type, a TypeError will be raised.

Return type:

dict

Parameters:

data (dict) --

Args:

data (dict): Dictionary of data to be converted to a numeric type.

Returns:

dict: Dictionary with the same keys and all values converted to a numeric type.

floris.type_dec.iter_validator(iter_type, item_types)[source]#

Helper function to generate iterable validators that will reduce the amount of boilerplate code.

Return type:

Callable

Parameters:

item_types (Any | Tuple[Any]) --

Args:

iter_type (iterable): The type of iterable object that should be validated. item_types (Union[Any, Tuple[Any]]): The type or types of acceptable item types.

Returns:

Callable: The attr.validators.deep_iterable iterable and instance validator.

floris.type_dec.convert_to_path(fn)[source]#

Converts an input string or pathlib.Path object to a fully resolved pathlib.Path object. If the input is a string, it is converted to a pathlib.Path object. The function then checks if the path exists as an absolute path, a relative path from the script, or a relative path from the system location. If the path does not exist in any of these locations, a FileExistsError is raised.

Return type:

Path

Parameters:

fn (str | Path) --

Args:

fn (str | Path): The user input file path or file name.

Raises:
FileExistsError: Raised if fn is not able to be found as an absolute path, nor as

a relative path.

TypeError: Raised if fn is neither a str, nor a pathlib.Path.

Returns:

Path: A resolved pathlib.Path object.

class floris.type_dec.FromDictMixin[source]#

A Mixin class to allow for kwargs overloading when a data class doesn't have a specific parameter defined. This allows passing of larger dictionaries to a data class without throwing an error.

classmethod from_dict(data)[source]#

Maps a data dictionary to an attr-defined class.

TODO: Add an error to ensure that either none or all the parameters are passed in

Args:
datadict

The data dictionary to be mapped.

Returns:
cls

The attr-defined class.

Parameters:

data (dict) --

as_dict()[source]#

Creates a YAML friendly dictionary that can be saved for future reloading. This dictionary will contain only Python types that can later be converted to their proper formats. See _attr_floris_filter for detail on which attributes are removed from the export.

Return type:

dict

Returns:

dict: All key, value pairs required for class recreation.