pymor.core.base

This module provides base classes from which most classes in pyMOR inherit.

The purpose of these classes is to provide some common functionality for all objects in pyMOR. The most notable features provided by BasicObject are the following:

  1. BasicObject sets class UberMeta as metaclass which itself inherits from abc.ABCMeta. Thus it is possible to define interface classes with abstract methods using the abstractmethod decorator. There are also decorators for abstract class methods, static methods, and properties.

  2. Using metaclass magic, each class deriving from BasicObject comes with its own logger instance accessible through its logger attribute. The logger prefix is automatically set to the class name.

  3. Logging can be disabled and re-enabled for each instance using the BasicObject.disable_logging and BasicObject.enable_logging methods.

  4. BasicObject.uid provides a unique id for each instance. While id(obj) is only guaranteed to be unique among all living Python objects, BasicObject.uid will be (almost) unique among all pyMOR objects that have ever existed, including previous runs of the application. This is achieved by building the id from a uuid4 which is newly created for each pyMOR run and a counter which is increased for any object that requests an uid.

  5. If not set by the user to another value, BasicObject.name is set to the name of the object’s class.

ImmutableObject derives from BasicObject and adds the following functionality:

  1. Using more metaclass magic, each instance which derives from ImmutableObject is locked after its __init__ method has returned. Each attempt to change one of its attributes raises an exception. Private attributes (of the form _name) are exempted from this rule.

  2. ImmutableObject.with_ can be used to create a copy of an instance with some changed attributes. E.g.

    obj.with_(a=x, b=y)
    

    creates a copy with the a and b attributes of obj set to x and y. with_ is implemented by creating a new instance, passing the arguments of with_ to __init__. The missing __init__ arguments are taken from instance attributes of the same name.

Module Contents

pymor.core.base.DONT_COPY_DOCSTRINGS[source]
pymor.core.base.NoneType[source]
pymor.core.base.abstractclassmethod[source]
pymor.core.base.abstractmethod[source]
pymor.core.base.abstractproperty[source]
pymor.core.base.abstractstaticmethod[source]
class pymor.core.base.BasicObject[source]

Base class for most classes in pyMOR.

logger[source]

A per-class instance of logging.Logger with the class name as prefix.

logging_disabled[source]

True if logging has been disabled.

name[source]

The name of the instance. If not set by the user, the name is set to the class name.

uid[source]

A unique id for each instance. The uid is obtained by using UID and is unique for all pyMOR objects ever created.

Methods

disable_logging

Disable logging output for this instance.

enable_logging

Enable logging output for this instance.

disable_logging(doit=True)[source]

Disable logging output for this instance.

enable_logging(doit=True)[source]

Enable logging output for this instance.

class pymor.core.base.ImmutableMeta(name, bases, namespace)[source]

Bases: UberMeta

Metaclass for ImmutableObject.

class pymor.core.base.ImmutableObject[source]

Bases: BasicObject

Base class for immutable objects in pyMOR.

Instances of ImmutableObject are immutable in the sense that after execution of __init__, any modification of a non-private attribute will raise an exception.

Warning

For instances of ImmutableObject, the result of member function calls should be completely determined by the function’s arguments together with the object’s __init__ arguments and the current state of pyMOR’s global defaults.

While, in principle, you are allowed to modify private members after instance initialization, this should never affect the outcome of future method calls. In particular, if you update any internal state after initialization, you have to ensure that this state is not affected by possible changes of the global defaults.

Methods

with_

Returns a copy with changed attributes.

with_(new_type=None, **kwargs)[source]

Returns a copy with changed attributes.

A a new class instance is created with the given keyword arguments as arguments for __init__. Missing arguments are obtained form instance attributes with the same name.

Parameters

new_type

If not None, return an instance of this class (instead of type(self)).

**kwargs

Names of attributes to change with their new values. Each attribute name has to be an argument to __init__.

Returns

Copy of self with changed attributes.

class pymor.core.base.UID[source]

Provides unique, quickly computed ids by combining a session UUID4 with a counter.

counter = [0][source]
prefix[source]
class pymor.core.base.UberMeta(name, bases, namespace)[source]

Bases: abc.ABCMeta

Metaclass for defining Abstract Base Classes (ABCs).

Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as ‘virtual subclasses’ – these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won’t show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super()).

class pymor.core.base.classinstancemethod(cls_meth)[source]

Methods

instancemethod

instancemethod(inst_meth)[source]