4.6.8 pde.tools.misc module
Miscellaneous python functions
check whether a python module is available |
|
creates a folder if it not already exists |
|
decorator that makes vectorized methods work with scalars |
|
make a decorator usable with and without arguments: |
|
import a class or module given an identifier |
|
decorator that can be used to define read-only properties for classes. |
|
descriptor that can be used as a decorator to allow calling a method both as a classmethod and an instance method |
|
estimates the computation speed of a function |
|
write (JSON-serialized) attributes to a hdf file |
|
convert a value into a float or complex number |
|
returns a dtype in which all arguments can be represented |
|
convert an array with arbitrary dtype either to np.double or np.cdouble |
- class classproperty(fget=None, doc=None)[source]
Bases:
property
decorator that can be used to define read-only properties for classes.
This is inspired by the implementation of
astropy
, see astropy.org.Example
The decorator can be used much like the property decorator:
class Test(): item: str = 'World' @classproperty def message(cls): return 'Hello ' + cls.item print(Test.message)
- decorator_arguments(decorator)[source]
make a decorator usable with and without arguments:
The resulting decorator can be used like @decorator or @decorator(*args, **kwargs)
Inspired by https://stackoverflow.com/a/14412901/932593
- ensure_directory_exists(folder)[source]
creates a folder if it not already exists
- Parameters:
folder (str) – path of the new folder
- estimate_computation_speed(func, *args, **kwargs)[source]
estimates the computation speed of a function
- Parameters:
func (callable) – The function to call
- Returns:
the number of times the function can be calculated in one second. The inverse is thus the runtime in seconds per function call
- Return type:
- get_common_dtype(*args)[source]
returns a dtype in which all arguments can be represented
- Parameters:
*args – All items (arrays, scalars, etc) to be checked
Returns: numpy.cdouble if any entry is complex, otherwise np.double
- hdf_write_attributes(hdf_path, attributes=None, raise_serialization_error=False)[source]
write (JSON-serialized) attributes to a hdf file
- class hybridmethod(fclass, finstance=None, doc=None)[source]
Bases:
object
descriptor that can be used as a decorator to allow calling a method both as a classmethod and an instance method
Adapted from https://stackoverflow.com/a/28238047
- import_class(identifier)[source]
import a class or module given an identifier
- Parameters:
identifier (str) – The identifier can be a module or a class. For instance, calling the function with the string identifier == ‘numpy.linalg.norm’ is roughly equivalent to running from numpy.linalg import norm and would return a reference to norm.
- number(value)[source]
convert a value into a float or complex number
- Parameters:
value (Number or str) – The value which needs to be converted
- Return type:
- Result:
Number: A complex number or a float if the imaginary part vanishes
- number_array(data, dtype=None, copy=True)[source]
convert an array with arbitrary dtype either to np.double or np.cdouble
- Parameters:
data (
ndarray
) – The data that needs to be converted to a float array. This can also be any iterable of numbers.dtype (numpy dtype) – The data type of the field. All the numpy dtypes are supported. If omitted, it will be determined from data automatically.
copy (bool) – Whether the data must be copied (in which case the original array is left untouched). Note that data will always be copied when changing the dtype.
- Returns:
An array with the correct dtype
- Return type:
- preserve_scalars(method)[source]
decorator that makes vectorized methods work with scalars
This decorator allows to call functions that are written to work on numpy arrays to also accept python scalars, like int and float. Essentially, this wrapper turns them into an array and unboxes the result.
- Parameters:
method (TFunc) – The method being decorated
- Returns:
The decorated method
- Return type:
TFunc