jrai_common_mixins package

Submodules

jrai_common_mixins.cachable module

Base class for all classes with cachable methods. This is required for classes to work with the caching_property decoractor.

class jrai_common_mixins.cachable.Cachable[source]

Bases: object

Base class for all classes with cachable methods. This is required for classes to work with the caching_property decoractor.

__init__()[source]
classmethod caching_method(*arg_cmps, _cm_maxsize=None, **kwarg_cmps)[source]

A decorator for class methods that cache results for subsequent calls.

Caution

This will internally store references to all arguments whenever the arguments are determined to be different from a previous set of arguments.

Parameters:
  • cls – A subclass of this class.

  • *arg_cmps – Comparison functions for positional arguments. These must be functions that accept 2 arguments and return a boolean to indicate if the arguments are considered equal. If a function is None then arguments will be compared using the built-in id() function.

  • _cm_maxsize – The maximum number of cached results to store for this method. If None, no maximum is imposed. Values of zero or less are equivalent to None.

  • **kwarg_cmps – Comparison functions for keyword arguments. These must map keyword argument names to comparision functions as described for arg_cmps.

Returns:

A caching method decoractor.

classmethod caching_property(method)[source]

A decorator for class properties that cache results for subsequent calls.

Parameters:
  • cls – A subclass of this class.

  • method – The method to convert to a caching property.

Returns:

A caching property.

clear_cache()[source]

Clear cached values.

clear_cached_method(name)[source]

Clear a specific method cache.

Parameters:

name – The method name or the method itself.

clear_cached_property(name)[source]

Clear a specific property cache.

Parameters:

name – The property name.

clear_old_cache_entries(by_atime=True, properties=None, methods=None, **kwargs)[source]

Clear old cache entries by age.

Parameters:
  • by_atime – If True, clear entries with access times older thant he specified time, else clear entries with creation times older than the specified time.

  • properties – An optional list of properties to clear. If neither properties nor methods are specified then the entire cache will be cleared of old entries.

  • methods – An optional list of methods to clear. If neither properties nor methods are specified then the entire cache will be cleared of old entries.

  • **kwargs – Keyword arguments for creating datetime.timedelta objects to specify the threshold age.

sort_cached_method_values_by_atime(name=None)[source]

Sort cached method values by access time.

Parameters:

name – A method name or method object to sort. If None, all cached method values will be sorted.

jrai_common_mixins.registrable module

Base class for all registrable classes.

class jrai_common_mixins.registrable.Registrable[source]

Bases: object

Base class for all registrable classes, i.e. classes that can be defined anywhere and registered into a central class register for use by the internal components of this library.

To create a set of registrable classes, first devine a base class that derives from this class (e.g. EventMetric(Registrable), SequenceMetric(Registrable) and define a dict class attribute named _REGISTER in that base class. Define subclasses of that base class to create registrable subclasses.

classmethod clear_registered()[source]

Clear all registered classes.

classmethod get_registered(name)[source]

Get a registered class.

Parameters:

name – The name of the registered class, as returned by get_name().

Returns:

The registered class.

classmethod get_registration_base_class()[source]

Find the base class that contains the register. The base class should be a subclass of Registrable that defines a dict class attribute named _REGISTER (or whatever the value of _REGISTER_NAME is).

Raises:

ValueError – Failed to find a registration base class.

classmethod get_registration_name(from_register=False)[source]

Get the name of the class to use for registration. If the class’s name contains the registration base class’s name as a suffix, then the returned name will be the class’s name without this suffix. For example, a class named “L2Metric” derived from a registrable base class “Metric” will return the string “L2”.

If the class’s name does no include the base class’s name as a suffix then the full name will be returned.

Parameters:

from_register – If True, check if the class already exists in the register and return the name under which it was registered. Use this to recover the names of previously registered classes.

Returns:

The registration name.

classmethod list_registered()[source]

List all of the currently registered names.

Returns:

A list of the registered names, sorted alphabetically.

classmethod register(name=None)[source]

Register this class.

Parameters:

name – An optional name to use when registering the class. If not used, it will use the value returned by get_registration_name()

Module contents

Package stub.