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.

Classes

BasicObject

Base class for most classes in pyMOR.

ImmutableMeta

Metaclass for ImmutableObject.

ImmutableObject

Base class for immutable objects in pyMOR.

UID

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

UberMeta

classinstancemethod

class pymor.core.base.BasicObject[source]

Bases: object

Base class for most classes in pyMOR.

disable_logging(doit=True)[source]

Disable logging output for this instance.

enable_logging(doit=True)[source]

Enable logging output for this instance.

property logger[source]

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

property logging_disabled[source]

True if logging has been disabled.

name = None[source]

Name of the object.

property uid[source]

A unique id for each instance.

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

class pymor.core.base.ImmutableMeta(classname, bases, classdict)[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.

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

Returns a copy with changed attributes.

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]

Bases: object

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

Attributes

UID

counter, prefix, uid

counter = [0][source]
prefix = '6425c9fc-cb3d-4843-b5ef-9a151ca7f614_'[source]
uid[source]
class pymor.core.base.UberMeta(classname, bases, classdict)[source]

Bases: ABCMeta

class pymor.core.base.classinstancemethod[**P, R](cls_meth: Callable[[Concatenate[Any, P]], R])[source]

Bases: object

instancemethod(inst_meth: Callable[[Concatenate[Any, P]], R]) → Self[source]
Return type:

Self

cls_meth: Callable[[Concatenate[Any, P]], R][source]