castep_outputs.utilities.type_conv#
Functions for parsing raw data into python types.
Functions
|
Determine the datatype and return the appropriate type. |
|
Apply correct types to elements of in_dict by mapping given in type_dict. |
Convert from (Fortran) bytes to a Python type. |
|
Parse numbers to int if all elements ints or float otherwise. |
|
|
Convert types to typ regardless of if data_in is iterable or otherwise. |
- castep_outputs.utilities.type_conv.determine_type(data)[source]#
Determine the datatype and return the appropriate type.
For dealing with miscellaneous data read from input files.
- Parameters:
data (str) – String to process.
- Returns:
Best type to attempt.
- Return type:
type
Examples
>>> determine_type('T') <class 'bool'> >>> determine_type('False') <class 'bool'> >>> determine_type('3.1415') <class 'float'> >>> determine_type('123') <class 'int'> >>> determine_type('1/3') <class 'float'> >>> determine_type('BEEF') <class 'str'>
- castep_outputs.utilities.type_conv.fix_data_types(in_dict, type_dict)[source]#
Apply correct types to elements of in_dict by mapping given in type_dict.
- Parameters:
in_dict (MutableMapping[str, Any]) – Dictionary of {key: values} to convert.
type_dict (dict[str, type]) – Mapping of keys to types the keys should be converted to.
- Return type:
None
See also
to_typeConversion function.
Notes
Modifies the dictionary in-place.
Examples
>>> my_dict = {"int": "7", "float": "3.141", "bool": "T", ... "vector": ["3", "4", "5"], "blank": "Hello"} >>> type_map = {"int": int, "float": float, "bool": bool, "vector": float} >>> fix_data_types(my_dict, type_map) >>> print(my_dict) {'int': 7, 'float': 3.141, 'bool': True, 'vector': (3.0, 4.0, 5.0), 'blank': 'Hello'}
- castep_outputs.utilities.type_conv.parse_bytes(data_in: bytes, typ: type[T]) T[source]#
- castep_outputs.utilities.type_conv.parse_bytes(data_in: bytes, typ: tuple[type[T], ellipsis]) tuple[T, ...]
Convert from (Fortran) bytes to a Python type.
- Parameters:
data_in – Data to convert.
typ – Type to convert to.
- Returns:
Processed value.
- Raises:
TypeError – Invalid type form passed.
Examples
>>> b_rep = (3).to_bytes(4, byteorder="big") >>> parse_bytes(b_rep, int) 3 >>> parse_bytes(b_rep, (int, ...)) (3,) >>> parse_bytes(b_rep * 3, (int, ...)) (3, 3, 3) >>> parse_bytes(b_rep * 3, int) Traceback (most recent call last): ValueError: Multiple elements in sequence (remainder=3, 3).
- castep_outputs.utilities.type_conv.parse_int_or_float(numbers: str) int | float[source]#
- castep_outputs.utilities.type_conv.parse_int_or_float(numbers: Iterable[str]) tuple[int | float, ...]
Parse numbers to int if all elements ints or float otherwise.
- Parameters:
numbers – Sequence of numbers to parse.
- Returns:
Parsed numerical value.
Examples
>>> parse_int_or_float("3.141") 3.141 >>> parse_int_or_float("7") 7
- castep_outputs.utilities.type_conv.to_type(data_in: str, typ: type[T]) T[source]#
- castep_outputs.utilities.type_conv.to_type(data_in: Iterable[str | U], typ: type[T]) tuple[T | U, ...]
- castep_outputs.utilities.type_conv.to_type(data_in: Mapping[K, str | U], typ: type[T]) dict[K, T | U]
- castep_outputs.utilities.type_conv.to_type(data_in: U, typ: Any) U
Convert types to typ regardless of if data_in is iterable or otherwise.
- Parameters:
data_in – Data to convert.
typ – Type to convert to.
- Returns:
Converted data.