propertytools¶
This module implements property like classes with similar or additional
behaviour.
Module propertytools implements the following members:
DefaultPropertyBoolno description available
DefaultPropertySeriesAggregationTypeno description available
DefaultPropertySeriesFileTypeno description available
DefaultPropertyStrno description available
ProtectedPropertyStrno description available
TypeInputType variable.
TypeInput_contraType variable.
TypeOutputType variable.
TypeOutput_coType variable.
BaseDescriptorBase class for defining descriptors.
FGetCallback protocol for getter functions.
FSetCallback protocol for setter functions.
FDelCallback protocol for deleter functions.
BasePropertyAbstract base class for deriving classes similar toproperty.
PropertyClassPropertymimics the behaviour of the built-in functionproperty.
ProtectedPropertyAproperty-like class which prevents getting an attribute before setting it.
ProtectedPropertiesIterable forProtectedPropertyobjects.
DependentPropertyproperty-like class which prevents accessing a dependent attribute before preparing certain other attributes.
DefaultPropertyproperty-like class which uses the getter function to return a default value unless a custom value is available.
- class hydpy.core.propertytools.BaseDescriptor[source]¶
- Bases: - object- Base class for defining descriptors. 
- class hydpy.core.propertytools.FGet(*args, **kwargs)[source]¶
- Bases: - Protocol[- TypeOutput_co]- Callback protocol for getter functions. 
- class hydpy.core.propertytools.FSet(*args, **kwargs)[source]¶
- Bases: - Protocol[- TypeInput_contra]- Callback protocol for setter functions. 
- class hydpy.core.propertytools.FDel(*args, **kwargs)[source]¶
- Bases: - Protocol- Callback protocol for deleter functions. 
- class hydpy.core.propertytools.BaseProperty[source]¶
- Bases: - Generic[- TypeInput,- TypeOutput],- BaseDescriptor- Abstract base class for deriving classes similar to - property.- BasePropertyprovides the abstract methods- call_fget(),- call_fset(), and- call_fdel(), which are the appropriate places to add custom functionalities (e.g. caching). See subclass- Propertyfor an example, which mimics the behaviour of the built-in- propertyfunction.- BasePropertyproperty uses dummy getter, setter and deleter functions to indicate than at an actual getter, setter or deleter function is missing. In case they are called due to the wrong implementation of a- BasePropertysubclass, they raise a- RuntimeError:- >>> from hydpy.core.propertytools import BaseProperty >>> BaseProperty._fgetdummy(None) Traceback (most recent call last): ... RuntimeError - >>> BaseProperty._fsetdummy(None, None) Traceback (most recent call last): ... RuntimeError - >>> BaseProperty._fdeldummy(None) Traceback (most recent call last): ... RuntimeError - abstract call_fget(obj: Any) TypeOutput[source]¶
- Method for implementing unique getter functionalities. 
 
- class hydpy.core.propertytools.Property(fget: ~hydpy.core.propertytools.FGet[~hydpy.core.propertytools.TypeOutput] = <function BaseProperty._fgetdummy>, fset: ~hydpy.core.propertytools.FSet[~hydpy.core.propertytools.TypeInput] = <function BaseProperty._fsetdummy>, fdel: ~hydpy.core.propertytools.FDel = <function BaseProperty._fdeldummy>)[source]¶
- Bases: - BaseProperty[- TypeInput,- TypeOutput]- Class - Propertymimics the behaviour of the built-in function- property.- The only advantage of - Propertyover- propertyis that it allows defining different input and output types statically. If the input and output types are identical, prefer- property, which is probably faster.- The following test class implements its attribute x by defining all three “property methods” (getter/fget, setter/fset, deleter/fdel), but its attribute y by defining none of them: - >>> from hydpy.core.propertytools import Property >>> class Test: ... ... def __init__(self): ... self._x = None ... self._y = None ... ... @Property ... def x(self): ... return self._x ... @x.setter ... def x(self, value): ... self._x = value ... @x.deleter ... def x(self): ... self._x = None ... ... y = Property() - After initialising a test object, you can use its attribute x as expected: - >>> test = Test() >>> test.x >>> test.x = 2 >>> test.x 2 >>> del test.x >>> test.x - When trying to invoke attribute y, you get the following error messages: - >>> test.y Traceback (most recent call last): ... AttributeError: Attribute `y` of object `test` is not gettable. - >>> test.y = 1 Traceback (most recent call last): ... AttributeError: Attribute `y` of object `test` is not settable. - >>> del test.y Traceback (most recent call last): ... AttributeError: Attribute `y` of object `test` is not deletable. - getter(fget: FGet[TypeOutput]) Property[TypeInput, TypeOutput][source]¶
- Add the given getter function and its docstring to the property and return it. 
 
- class hydpy.core.propertytools.ProtectedProperty(fget: ~hydpy.core.propertytools.FGet[~hydpy.core.propertytools.TypeOutput] = <function BaseProperty._fgetdummy>, fset: ~hydpy.core.propertytools.FSet[~hydpy.core.propertytools.TypeInput] = <function BaseProperty._fsetdummy>, fdel: ~hydpy.core.propertytools.FDel = <function BaseProperty._fdeldummy>)[source]¶
- Bases: - BaseProperty[- TypeInput,- TypeOutput]- A - property-like class which prevents getting an attribute before setting it.- Some attributes need preparations before being accessible. Consider the case where a property of a Python class (being part of the API) links to an attribute of a Cython extension class (not part of the API). If the Cython attribute is, for example, a vector requiring memory allocation, trying to query this vector before it has been initialised results in a program crash. Using - ProtectedPropertyis a means to prevent such problems.- The following class Test defines most simple getter, setter, and deleter functions for its only property x: - >>> from hydpy.core.propertytools import ProtectedProperty >>> class Test: ... ... def __init__(self): ... self._x = None ... ... x = ProtectedProperty() ... @x.getter ... def x(self): ... "Test" ... return self._x ... @x.setter ... def x(self, value): ... self._x = value ... @x.deleter ... def x(self): ... self._x = None - Trying to query x directly after initialising a Test object results in an - AttributeNotReadyerror:- >>> test = Test() >>> test.x Traceback (most recent call last): ... hydpy.core.exceptiontools.AttributeNotReady: Attribute `x` of object `test` has not been prepared so far. - After setting a value, you can query this value as expected: - >>> test.x = 1 >>> test.x 1 - After deleting the value, the protection mechanism applies again: - >>> del test.x >>> test.x Traceback (most recent call last): ... hydpy.core.exceptiontools.AttributeNotReady: Attribute `x` of object `test` has not been prepared so far. - call_fget(obj: Any) TypeOutput[source]¶
- When ready, call fget; otherwise, raise an - AttributeNotReadyexception.
 - isready(obj: Any) bool[source]¶
- Return - Trueor- Falseto indicate if the protected property is ready for the given object. If the object is unknown,- isready()returns- False.
 - getter(fget: FGet[TypeOutput]) ProtectedProperty[TypeInput, TypeOutput][source]¶
- Add the given getter function and its docstring to the property and return it. 
 - setter(fset: FSet[TypeInput]) ProtectedProperty[TypeInput, TypeOutput][source]¶
- Add the given setter function to the property and return it. 
 - deleter(fdel: FDel) ProtectedProperty[TypeInput, TypeOutput][source]¶
- Add the given deleter function to the property and return it. 
 
- hydpy.core.propertytools.ProtectedPropertyStr¶
- ProtectedPropertyfor handling- strobjects.- alias of - ProtectedProperty[- str,- str]
- class hydpy.core.propertytools.ProtectedProperties(*properties: ProtectedProperty[Any, Any])[source]¶
- Bases: - object- Iterable for - ProtectedPropertyobjects.- You can collect an arbitrary number of - ProtectedPropertyobjects within a- ProtectedPropertiesobject. Its- allready()method allows checking the status of all properties at ones:- >>> from hydpy.core import propertytools as pt >>> class Test: ... ... @pt.ProtectedProperty ... def x(self): ... return "this is x" ... @x.setter ... def x(self, value): ... pass ... ... @pt.ProtectedProperty ... def z(self): ... return "this is z" ... @z.setter ... def z(self, value): ... pass ... ... protectedproperties = pt.ProtectedProperties(x, z) - >>> test1 = Test() >>> test1.x = None >>> test2 = Test() >>> test2.x = None >>> test2.z = None >>> Test.protectedproperties.allready(test1) False >>> Test.protectedproperties.allready(test2) True 
- class hydpy.core.propertytools.DependentProperty(protected: ~hydpy.core.propertytools.ProtectedProperties, fget: ~hydpy.core.propertytools.FGet[~hydpy.core.propertytools.TypeOutput] = <function BaseProperty._fgetdummy>, fset: ~hydpy.core.propertytools.FSet[~hydpy.core.propertytools.TypeInput] = <function BaseProperty._fsetdummy>, fdel: ~hydpy.core.propertytools.FDel = <function BaseProperty._fdeldummy>)[source]¶
- Bases: - BaseProperty[- TypeInput,- TypeOutput]- property-like class which prevents accessing a dependent attribute before preparing certain other attributes.- Please read the documentation on class - ProtectedProperty, from which we take the following example. x is a simple- ProtectedPropertyagain, but time we add the- DependentPropertyy:- >>> from hydpy.core import propertytools as pt >>> class Test: ... ... def __init__(self): ... self._x = None ... self._y = None ... ... @pt.ProtectedProperty ... def x(self): ... return self._x ... @x.setter ... def x(self, value): ... self._x = value ... @x.deleter ... def x(self): ... self._x = None ... ... y = pt.DependentProperty(protected=(x,)) ... ... @y.getter ... def y(self): ... return self._y ... @y.setter ... def y(self, value): ... self._y = value ... @y.deleter ... def y(self): ... self._y = None - Initially, due to x not being prepared, there is no way to get, set, or delete attribute y: - >>> test = Test() >>> test.y Traceback (most recent call last): ... hydpy.core.exceptiontools.AttributeNotReady: Attribute `y` of object `test` is not usable so far. At least, you have to prepare attribute `x` first. >>> test.y = 1 Traceback (most recent call last): ... hydpy.core.exceptiontools.AttributeNotReady: Attribute `y` of object `test` is not usable so far. At least, you have to prepare attribute `x` first. >>> del test.y Traceback (most recent call last): ... hydpy.core.exceptiontools.AttributeNotReady: Attribute `y` of object `test` is not usable so far. At least, you have to prepare attribute `x` first. - After assigning a value to x, y behaves like a common property: - >>> test.x = "anything" >>> test.y = 1 >>> test.y 1 >>> del test.y >>> test.y - protected: ProtectedProperties¶
 - call_fget(obj: Any) TypeOutput[source]¶
- Call fget when all required attributes are ready; otherwise, raise an - AttributeNotReadyerror.
 - call_fset(obj: Any, value: TypeInput) None[source]¶
- Call fset when all required attributes are ready; otherwise, raise an - AttributeNotReadyerror.
 - call_fdel(obj: Any) None[source]¶
- Call fdel when all required attributes are ready; otherwise, raise an - AttributeNotReadyerror.
 - getter(fget: FGet[TypeOutput]) DependentProperty[TypeInput, TypeOutput][source]¶
- Add the given getter function and its docstring to the property and return it. 
 - setter(fset: FSet[TypeInput]) DependentProperty[TypeInput, TypeOutput][source]¶
- Add the given setter function to the property and return it. 
 - deleter(fdel: FDel) DependentProperty[TypeInput, TypeOutput][source]¶
- Add the given deleter function to the property and return it. 
 
- class hydpy.core.propertytools.DefaultProperty(fget: ~hydpy.core.propertytools.FGet[~hydpy.core.propertytools.TypeOutput] = <function BaseProperty._fgetdummy>)[source]¶
- Bases: - BaseProperty[- TypeInput,- TypeOutput]- property-like class which uses the getter function to return a default value unless a custom value is available.- In the following example, the default value of property x is one: - >>> from hydpy.core.propertytools import DefaultProperty >>> class Test: ... ... @DefaultProperty ... def x(self): ... "Default property x." ... return 1 - Initially, property x returns the default value defined by its getter function: - >>> test = Test() >>> test.x 1 - Assigned custom values override such default values: - >>> test.x = 3 >>> test.x 3 - After removing the custom value, it is again up to the getter function to return the default value: - >>> del test.x >>> test.x 1 - Trying to delete a not existing custom value does not harm: - >>> del test.x - The documentation string of the getter functions serves as the documentation string of the default property: - >>> Test.x.__doc__ 'Default property x.' 
- hydpy.core.propertytools.DefaultPropertyBool¶
- DefaultPropertyfor handling- boolobjects.- alias of - DefaultProperty[- bool,- bool]
- hydpy.core.propertytools.DefaultPropertyStr¶
- DefaultPropertyfor handling- strobjects.- alias of - DefaultProperty[- str,- str]
- hydpy.core.propertytools.DefaultPropertySeriesFileType¶
- DefaultPropertyfor handling- SeriesFileTypeliterals.- alias of - DefaultProperty[- Literal[‘npy’, ‘asc’, ‘nc’],- Literal[‘npy’, ‘asc’, ‘nc’]]
- hydpy.core.propertytools.DefaultPropertySeriesAggregationType¶
- DefaultPropertyfor handling- SeriesAggregationTypeliterals.- alias of - DefaultProperty[- Literal[‘none’, ‘mean’],- Literal[‘none’, ‘mean’]]
