LockableMixin#
- class LockableMixin[source]#
Mixin adding a lock to an object, to forbid the operations that modify it.
A locked object can still be read, copied and saved, but the methods that modify it in place raise a
RuntimeError. The lock protects an object owned by another object of the package against accidental modifications, for example the transformation of apysdic.ReparametrizedTransformationor the mesh of apysdic.SDICAnalysis, whose consistency assumes that it does not change.The lock is set by the owner with a
messageexplaining why the object is locked and what to do instead. The message is added to the error:RuntimeError: Cannot set the parameters: this PinholeIntrinsic is locked. It is used by a ReparametrizedTransformation, which keeps its parameters p equal to P(q) [...]
Important
The lock is not meant to be removed by the users: there is no public method to lock or unlock an object. Use
is_lockedandlocked_messageto know whether an object is locked and why, and work on a copy (copy) to get an object that can be modified.Note
A new object is always unlocked, including the objects returned by
copyand the loading methods (the lock is not saved).Implementing a lockable class
The owner locks an object with the internal method
_set_locked(). A class inheriting from the mixin calls_check_unlocked()at the beginning of each method that modifies the object, before any modification (so that a refused operation leaves the object unchanged):class PointCloud(LockableMixin, ...): def filter_points(self, mask, *, inplace=False): if inplace: self._check_unlocked("filter the points") ...
An object made of lockable objects (for example a mesh made of a point cloud and a connectivity) overrides
_set_locked()to propagate the lock to its components:class Mesh(LockableMixin, ...): def _set_locked(self, locked=True, *, message=None): super()._set_locked(locked, message=message) self._vertices._set_locked(locked, message=message) self._connectivity._set_locked(locked, message=message)
The lock state is stored in the attributes
_lockedand_locked_message(declared in the__slots__of the mixin). They do not need to be initialized: an object whose lock was never set is unlocked.The class attribute
_default_locked_messageis the explanation used when the object was locked without a message. A class can redefine it, for example to mention anoverride_lockoption.- _check_unlocked(action, *, override_lock=False)[source]#
Raise an error if the object is locked.
Call this method at the beginning of each method that modifies the object, before any modification.
Important
Internal use only (for the classes inheriting from the mixin).
- Parameters:
action (str) – The operation refused, as a verb phrase completing “Cannot …” (for example
"filter the points"or"set the coordinates").override_lock (bool, optional (default:
False)) – If True, no error is raised even if the object is locked (for the methods offering anoverride_lockoption, likeset_parameters).
- Raises:
RuntimeError – If the object is locked and
override_lockis False.- Return type:
None
- _set_locked(locked=True, *, message=None)[source]#
Lock or unlock the object.
When the object is locked, the methods that modify it raise a
RuntimeErrorcontainingmessage.Important
Internal use only: called by the objects of the package that own the object (and by
copyto unlock a new copy).Note
Locking an object that is already locked replaces its message. Unlocking an object removes its message.
- Parameters:
locked (bool, optional (default:
True)) – True to lock the object, False to unlock it.message (Optional[str], optional (default:
None)) – The reason of the lock, added to the errors raised by the locked object. It should explain which object owns it, why, and what to do instead. Ignored whenlockedis False. If None,_default_locked_messageis used in the errors.
- Raises:
TypeError – If
lockedis not a boolean ormessageis not a string.ValueError – If
messageis an empty string.
- Return type:
None
- property is_locked: bool#
[Get] Whether the object is locked.
A locked object is owned by another object of the package: the methods that modify it raise a
RuntimeError(seelocked_messagefor the reason).- Returns:
True if the object is locked, False otherwise.
- Return type: