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:
BasicObject
sets classUberMeta
as metaclass which itself inherits fromabc.ABCMeta
. Thus it is possible to define interface classes with abstract methods using theabstractmethod
decorator. There are also decorators for abstract class methods, static methods, and properties.Using metaclass magic, each class deriving from
BasicObject
comes with its ownlogger
instance accessible through itslogger
attribute. The logger prefix is automatically set to the class name.Logging can be disabled and re-enabled for each instance using the
BasicObject.disable_logging
andBasicObject.enable_logging
methods.
BasicObject.uid
provides a unique id for each instance. Whileid(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.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:
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.
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
andb
attributes ofobj
set tox
andy
.with_
is implemented by creating a new instance, passing the arguments ofwith_
to__init__
. The missing__init__
arguments are taken from instance attributes of the same name.
Module Contents¶
- 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.
- 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 output for this instance.
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 globaldefaults
.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
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.
- 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()).