4.6.8 pde.tools.misc module

Miscellaneous python functions.

module_available

Check whether a python module is available.

ensure_directory_exists

Creates a folder if it not already exists.

preserve_scalars

Decorator that makes vectorized methods work with scalars.

decorator_arguments

make a decorator usable with and without arguments:

import_class

Import a class or module given an identifier.

classproperty

Decorator that can be used to define read-only properties for classes.

hybridmethod

Descriptor that can be used as a decorator to allow calling a method both as a classmethod and an instance method.

estimate_computation_speed

Estimates the computation speed of a function.

hdf_write_attributes

Write (JSON-serialized) attributes to a hdf file.

number

Convert a value into a float or complex number.

get_common_dtype

Returns a dtype in which all arguments can be represented.

number_array

Convert data into an array, assuming float numbers if no dtype is given.

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)
deleter(fdel)[source]

Descriptor to obtain a copy of the property with a different deleter.

getter(fget)[source]

Descriptor to obtain a copy of the property with a different getter.

setter(fset)[source]

Descriptor to obtain a copy of the property with a different setter.

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

Parameters:

decorator (Callable) – the decorator that needs to be modified

Returns:

the decorated function

Return type:

Callable

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:

float

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.

Parameters:
  • hdf_path – Path to a group or dataset in an open HDF file

  • attributes (dict) – Dictionary with values written as attributes

  • raise_serialization_error (bool) – Flag indicating whether serialization errors are raised or silently ignored

Return type:

None

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

classmethod(fclass)[source]
instancemethod(finstance)[source]
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.

module_available(module_name)[source]

Check whether a python module is available.

Parameters:

module_name (str) – The name of the module

Returns:

True if the module can be imported and False otherwise

Return type:

bool

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:

int | float | complex

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 double unless data contains complex numbers in which case it will be cdouble.

  • 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:

ndarray

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