Slots

For performance reasons all tree nodes support slots. They all sub-class the HasSlots class, which is the top-level class of pypet (its direct descendant is HasLogger, see below). This class provides an __all_slots__ property (with the help of the MetaSlotMachine metaclass) that lists all existing __slots__ of a class including the inherited ones. Moreover, via __getstate__ and __setstate__ HasSlots takes care that all sub-classes can be pickled with the lowest protocol and don’t need to implement __getstate__ and __setstate__ themselves even when they have __slots__. However, sub-classes that still implement these functions should call the parent ones via super. Sub-classes are not required to define __slots__. If they don’t, HasSlots wil also automatically handle their __dict__ in __getstate__ and __setstate__.

class pypet.slots.HasSlots[source]

Top-class that allows mixing of classes with and without slots.

Takes care that instances can still be pickled with the lowest protocol. Moreover, provides a generic __dir__ method that lists all slots.

__dir__()[source]

Includes all slots in the dir method

__setstate__(state)[source]

Recalls state for items with slots

__weakref__

list of weak references to the object (if defined)

pypet.slots.get_all_slots(cls)[source]

Iterates through a class’ (cls) mro to get all slots as a set.

class pypet.slots.MetaSlotMachine(name, bases, dictionary)[source]

Meta-class that adds the attribute __all_slots__ to a class.

__all_slots__ is a set that contains all unique slots of a class, including the ones that are inherited from parents.

__init__(name, bases, dictionary)[source]

Initialize self. See help(type(self)) for accurate signature.

Logging

HasLogger can be sub-classed to allow per class or even per instance logging. The logger is initialized via _set_logger() and is available via the _logger attribute. HasLogger also takes care that the logger does not get pickled when __getstate__ and __setstate__ are called. Thus, you are always advised in sub-classes that also implement these functions to call the parent ones via super. HasLogger is a direct sub-class of HasSlots. Hence, support for __slots__ is ensured.

class pypet.pypetlogging.HasLogger[source]

Abstract super class that automatically adds a logger to a class.

To add a logger to a sub-class of yours simply call myobj._set_logger(name). If name=None the logger is chosen as follows:

self._logger = logging.getLogger(self.__class.__.__module__ + '.' + self.__class__.__name__)

The logger can be accessed via myobj._logger.

__getstate__()[source]

Called for pickling.

Removes the logger to allow pickling and returns a copy of __dict__.

__setstate__(statedict)[source]

Called after loading a pickle dump.

Restores __dict__ from statedict and adds a new logger.

_set_logger(name=None)[source]

Adds a logger with a given name.

If no name is given, name is constructed as type(self).__name__.

pypet.pypetlogging.rename_log_file(filename, trajectory=None, env_name=None, traj_name=None, set_name=None, run_name=None, process_name=None, host_name=None)[source]

Renames a given filename with valid wildcard placements.

LOG_ENV ($env) is replaces by the name of the trajectory`s environment.

LOG_TRAJ ($traj) is replaced by the name of the trajectory.

LOG_RUN ($run) is replaced by the name of the current run. If the trajectory is not set to a run ‘run_ALL’ is used.

LOG_SET ($set) is replaced by the name of the current run set. If the trajectory is not set to a run ‘run_set_ALL’ is used.

LOG_PROC ($proc) is replaced by the name fo the current process.

LOG_HOST ($host) is replaced by the name of the current host.

Parameters:
  • filename – A filename string
  • traj – A trajectory container, leave None if you provide all the parameters below
  • env_name – Name of environemnt, leave None to get it from traj
  • traj_name – Name of trajectory, leave None to get it from traj
  • set_name – Name of run set, leave None to get it from traj
  • run_name – Name of run, leave None to get it from traj
  • process_name – The name of the desired process. If None the name of the current process is taken determined by the multiprocessing module.
  • host_name – Name of host, leave None to determine it automatically with the platform module.
Returns:

The new filename