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.add_metaclass(metaclass)[source]

Adds a metaclass to a given class.

This decorator is used instead of __metaclass__ to allow for Python 2 and 3 compatibility.

Inspired by the six module: (https://bitbucket.org/gutworth/six/src/784c6a213c4527ea18f86a800f51bf16bc1df5bc/six.py?at=default)

For example:

@add_metaclass(MyMetaClass)
class MyClass(object):
    pass

is equivalent to

class MyClass(object):
    __metaclass__ = MyMetaClass

in Python 2 or

class MyClass(object, metaclass=MyMetaClass)
    pass

in Python 3.

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.

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(traj, filename, process_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.

Parameters:
  • traj – A trajectory container
  • filename – A filename string
  • process_name – The name of the desired process. If None the name of the current process is taken determined by the multiprocessing module.
Returns:

The new filename