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:
BasicObjectsets classUberMetaas metaclass which itself inherits fromabc.ABCMeta. Thus it is possible to define interface classes with abstract methods using theabstractmethoddecorator. There are also decorators for abstract class methods, static methods, and properties.Using metaclass magic, each class deriving from
BasicObjectcomes with its ownloggerinstance accessible through itsloggerattribute. The logger prefix is automatically set to the class name.Logging can be disabled and re-enabled for each instance using the
BasicObject.disable_loggingandBasicObject.enable_loggingmethods.
BasicObject.uidprovides a unique id for each instance. Whileid(obj)is only guaranteed to be unique among all living Python objects,BasicObject.uidwill 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.nameis 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
ImmutableObjectis 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
aandbattributes ofobjset toxandy.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.
Classes
Base class for most classes in pyMOR. |
|
Metaclass for |
|
Base class for immutable objects in pyMOR. |
|
Provides unique, quickly computed ids by combining a session UUID4 with a counter. |
|
- class pymor.core.base.BasicObject[source]¶
Bases:
objectBase class for most classes in pyMOR.
Methods
Attributes
- property logger[source]¶
A per-class instance of
logging.Loggerwith the class name as prefix.
- class pymor.core.base.ImmutableMeta(classname, bases, classdict)[source]¶
Bases:
UberMetaMetaclass for
ImmutableObject.
- class pymor.core.base.ImmutableObject[source]¶
Bases:
BasicObjectBase class for immutable objects in pyMOR.
Instances of
ImmutableObjectare 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.Attributes
- 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
selfwith changed attributes.
- class pymor.core.base.UID[source]¶
Bases:
objectProvides unique, quickly computed ids by combining a session UUID4 with a counter.