castep_outputs.utilities.utility#
Utility functions for parsing castep outputs.
Functions
|
Add aliases of known names into dictionary. |
|
Transform a matched atreg value to species index tuple. |
|
Determine the datatype and return the appropriate type. |
|
Decorate to allow a parser to accept either a path or open file. |
|
Apply correct types to elements of in_dict by mapping given in type_dict. |
|
Turn a nested dictionary into a flattened dictionary. |
|
Get the only element of a Sequence ensuring uniqueness. |
|
Recursively transform datatypes into JSON safe variants. |
|
Return logging function to add file info to logs. |
|
Standardise data after processing. |
|
Normalise a dictionary key. |
|
Normalise a string. |
|
Parse numbers to int if all elements ints or float otherwise. |
|
Append items in in_dict to the keys in out_dict. |
|
Strip comments from data. |
|
Convert types to typ regardless of if data_in is iterable or otherwise. |
- castep_outputs.utilities.utility.add_aliases(in_dict, alias_dict, *, replace=False, inplace=True)[source]#
Add aliases of known names into dictionary.
If replace is True, this will remove the original.
- Parameters:
- Returns:
in_dict with keys substituted.
- Return type:
Examples
>>> add_aliases({'hi': 1, 'bye': 2}, {'hi': 'frog'}) {'hi': 1, 'bye': 2, 'frog': 1} >>> add_aliases({'hi': 1, 'bye': 2}, {'hi': 'frog'}, replace=True) {'bye': 2, 'frog': 1}
- castep_outputs.utilities.utility.atreg_to_index(dict_in, *, clear=True)[source]#
Transform a matched atreg value to species index tuple.
Optionally clear value from dictionary for easier processing.
- Parameters:
- Return type:
- Returns:
species (str) – Atomic species.
ind (int) – Internal index.
Examples
>>> parsed_line = {'x': 3.1, 'y': 2.1, 'z': 1.0, 'spec': 'Ar', 'index': '1'} >>> atreg_to_index(parsed_line, clear=False) ('Ar', 1) >>> parsed_line {'x': 3.1, 'y': 2.1, 'z': 1.0, 'spec': 'Ar', 'index': '1'} >>> atreg_to_index(parsed_line) ('Ar', 1) >>> parsed_line {'x': 3.1, 'y': 2.1, 'z': 1.0}
- castep_outputs.utilities.utility.determine_type(data)[source]#
Determine the datatype and return the appropriate type.
For dealing with miscellaneous data read from input files.
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.utility.file_or_path(*, mode, **open_kwargs)[source]#
Decorate to allow a parser to accept either a path or open file.
- castep_outputs.utilities.utility.fix_data_types(in_dict, type_dict)[source]#
Apply correct types to elements of in_dict by mapping given in type_dict.
- Parameters:
- Return type:
See also
to_type
Conversion 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.utility.flatten_dict(dictionary, parent_key='', separator='_')[source]#
Turn a nested dictionary into a flattened dictionary.
- Parameters:
dictionary (
MutableMapping
[Any
,Any
]) – The dictionary to flatten.parent_key (
str
) – The string to prepend to dictionary’s keys.separator (
str
) – The string used to separate flattened keys.
- Returns:
A flattened dictionary.
- Return type:
Notes
Taken from: https://stackoverflow.com/a/62186053
Examples
>>> flatten_dict({'hello': ['is', 'me'], ... "goodbye": {"nest": "birds", "child": "moon"}}) {'hello_0': 'is', 'hello_1': 'me', 'goodbye_nest': 'birds', 'goodbye_child': 'moon'}
- castep_outputs.utilities.utility.get_only(seq)[source]#
Get the only element of a Sequence ensuring uniqueness.
- Parameters:
- Returns:
The sole element of the sequence.
- Return type:
Any
- Raises:
ValueError – Value is not alone.
- castep_outputs.utilities.utility.json_safe(obj)[source]#
Recursively transform datatypes into JSON safe variants.
Including:
Ensuring dict keys are strings without spaces.
Ensuring complex numbers are split into real/imag components.
- Parameters:
- Returns:
Safe datatype.
- Return type:
Any
Examples
>>> json_safe(3 + 4j) {'real': 3.0, 'imag': 4.0} >>> json_safe({('Ar', 'Sr'): 3}) {'Ar_Sr': 3} >>> json_safe({(('Ar', 1), ('Sr', 1)): 3}) {'Ar_1_Sr_1': 3}
- castep_outputs.utilities.utility.log_factory(file)[source]#
Return logging function to add file info to logs.
- Parameters:
file (TextIO or FileInput or FileWrapper) – File to apply logging for.
- Returns:
Function for logging data.
- Return type:
Callable
- castep_outputs.utilities.utility.normalise(obj, mapping)[source]#
Standardise data after processing.
Recursively converts:
list
s totuple
sdefaultdict
s todict
stypes in mapping to their mapped type or apply mapped function.
- castep_outputs.utilities.utility.normalise_key(string)[source]#
Normalise a dictionary key.
This includes:
Removing all punctuation.
Lower-casing all.
Making all spacing single-underscore.
Examples
>>> normalise_key(" Several words ") 'several_words' >>> normalise_key("A sentence.") 'a_sentence' >>> normalise_key("I<3;;semi-colons;;!!!") 'i_3_semi_colons'
- castep_outputs.utilities.utility.normalise_string(string)[source]#
Normalise a string.
This includes:
Removing leading/trailing whitespace.
Making all spacing single-space.
Examples
>>> normalise_string(" Several words ") 'Several words'
- castep_outputs.utilities.utility.parse_int_or_float(numbers)[source]#
Parse numbers to int if all elements ints or float otherwise.
- Parameters:
- Returns:
Parsed numerical value.
- Return type:
Examples
>>> parse_int_or_float("3.141") 3.141 >>> parse_int_or_float("7") 7
- castep_outputs.utilities.utility.stack_dict(out_dict, in_dict)[source]#
Append items in in_dict to the keys in out_dict.
- castep_outputs.utilities.utility.strip_comments(data, *, comment_char='#!', remove_inline=False)[source]#
Strip comments from data.
- Parameters:
data (
TextIO
|FileWrapper
|Block
) – Data to strip comments from.remove_inline (
bool
) – Whether to remove inline comments or just line initial.comment_char (
str
|set
[str
]) –Character sets to read as comments and remove.
Note
If the chars are passed as a string, it is assumed that each character is a comment character.
To match a multicharacter comment you must pass this as a set or sequence of strings.
- Returns:
Block of data without comments.
- Return type:
Notes
Also strips trailing, but not leading whitespace to clean up comment blocks.
Also strips empty lines.
Examples
>>> from io import StringIO >>> inp = StringIO(''' ... Hello ... # Initial line comment ... End of line # comment ... // C-style ... ''') >>> x = strip_comments(inp, remove_inline=False) >>> type(x).__name__ 'Block' >>> '|'.join(x) 'Hello|End of line # comment|// C-style' >>> _ = inp.seek(0) >>> x = strip_comments(inp, remove_inline=True) >>> '|'.join(x) 'Hello|End of line|// C-style' >>> _ = inp.seek(0) >>> x = strip_comments(inp, comment_char={"//", "#"}) >>> '|'.join(x) 'Hello|End of line # comment'