4.7.8 pde.tools.misc module
Miscellaneous python functions.
Check whether a python module is available. |
|
Creates a folder if it not already exists. |
|
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 data into an array, assuming float numbers if no dtype is given. |
- class classproperty(fget=None, doc=None)[source]
Bases:
propertyDecorator 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:
objectDescriptor 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:
Number
- Result:
Number: A complex number or a float if the imaginary part vanishes
- number_array(data, dtype=None, copy=None)[source]
Convert data into an array, assuming float numbers if no dtype is given.
- Parameters:
data (
ndarray) – The data that needs to be converted to a number 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
doubleunless data contains complex numbers in which case it will becdouble.copy (bool) – Whether the data must be copied (in which case the original array is left untouched). The default None implies that data is only copied if necessary, e.g., when changing the dtype.
- Returns:
An array with the correct dtype
- Return type: