caput.misc

A set of miscellaneous routines that don’t really fit anywhere more specific.

caput.misc.getfullargspec(*args, **kwargs)[source]

See inspect.getfullargspec.

This is a Python 2 patch that will be removed.

caput.misc.import_class(class_path)[source]

Import class dynamically from a string.

Parameters

class_path (str) – Fully qualified path to the class. If only a single component, look up in the globals.

Returns

class – The class we want to load.

Return type

class object

caput.misc.listize(**_)[source]

Make functions that already work with np.ndarray or scalars accept lists.

Also works with tuples.

Returns

listized_function

Return type

func

class caput.misc.lock_file(name, preserve=False, comm=None)[source]

Bases: object

Manage a lock file around a file creation operation.

Parameters
  • filename (str) – Final name for the file.

  • preserve (bool, optional) – Keep the temporary file in the event of failure.

  • comm (MPI.COMM, optional) – If present only rank=0 will create/remove the lock file and move the file.

Returns

tmp_name – File name to use in the locked block.

Return type

str

Examples

>>> from . import memh5
>>> container = memh5.BasicCont()
>>> with lock_file('file_to_create.h5') as fname:
...     container.save(fname)
...
property lockfile

Full path to the lockfile (with file extension).

property tmpfile

Full path to the lockfile (without file extension).

caput.misc.open_h5py_mpi(f, mode, use_mpi=True, comm=None)[source]

Ensure that we have an h5py File object.

Opens with MPI-IO if possible.

The returned file handle is annotated with two attributes: .is_mpi which says whether the file was opened as an MPI file and .opened which says whether it was opened in this call.

Parameters
  • f (string, h5py.File or h5py.Group) – Filename to open, or already open file object. If already open this is just returned as is.

  • mode (string) – Mode to open file in.

  • use_mpi (bool, optional) – Whether to use MPI-IO or not (default True)

  • comm (mpi4py.Comm, optional) – MPI communicator to use. Uses COMM_WORLD if not set.

Returns

fh – File handle for h5py.File, with two extra attributes .is_mpi and .opened.

Return type

h5py.File

caput.misc.scalarize(dtype=<class 'numpy.float64'>)[source]

Handle scalars and other iterables being passed to numpy requiring code.

Parameters

dtype (np.dtype, optional) – The output datatype. Used only to set the return type of zero-length arrays.

Returns

vectorized_function

Return type

func

caput.misc.vectorize(**base_kwargs)[source]

An improved vectorization decorator.

Unlike the np.vectorize decorator this version works on methods in addition to functions. It also gives an actual scalar value back for any scalar input, instead of returning a 0-dimension array.

Parameters

**kwargs – Any keyword arguments accepted by np.vectorize

Returns

vectorized_function

Return type

func