aliastools

This module provides tools for the efficient handling of input and output sequence aliases.

Module aliastools implements the following members:


class hydpy.core.aliastools.LazyInOutSequenceImport(modulename: str, classname: str, alias: str, namespace: Dict[str, Any])[source]

Bases: object

Import the input or output sequence the alias is referring to only when required.

Importing all input and output sequences costs a significant amount of initialisation time, and one typically uses at most a few of them within a specific project. Hence, we implemented class LazyInOutSequenceImport for postponing the imports of the sequences available by modules inputs and outputs. We hope we could catch all exceptional cases so that a user will never realise to work with a LazyInOutSequenceImport object instead with an input or output class object (except when directly asking for it with function type).

Directly after importing, the alias is of type LazyInOutSequenceImport:

>>> from hydpy.core.aliastools import LazyInOutSequenceImport
>>> def get_example_alias():
...     return LazyInOutSequenceImport(
...         modulename="hydpy.models.hland.hland_inputs",
...         classname="T",
...         alias="hland_T",
...         namespace=locals(),
...     )
>>> hland_T = get_example_alias()
>>> type(hland_T)
<class 'hydpy.core.aliastools.LazyInOutSequenceImport'>

After accessing an attribute, its type changed to type, which is the meta-class of all input and output sequences:

>>> hland_T.__name__
'T'
>>> type(hland_T)
<class 'type'>

Due to Python’s special method lookup, we must explicitly define the relevant ones. The following examples list all considered methods:

>>> hland_T = get_example_alias()
>>> from hydpy.models.hland.hland_inputs import T, P
>>> hash(hland_T) == hash(T)
True
>>> hland_T = get_example_alias()
>>> hland_T == T
True
>>> hland_T = get_example_alias()
>>> T == hland_T
True
>>> hland_T = get_example_alias()
>>> hland_T != P
True
>>> hland_T = get_example_alias()
>>> P != hland_T
True
>>> hland_T = get_example_alias()
>>> str(hland_T)
"<class 'hydpy.models.hland.hland_inputs.T'>"
>>> hland_T = get_example_alias()
>>> repr(hland_T)
"<class 'hydpy.models.hland.hland_inputs.T'>"
>>> hland_T = get_example_alias()
>>> dir(hland_T)   
['INIT', 'NDIM', ...]
hydpy.core.aliastools.write_sequencealiases() None[source]

Write the alias modules inputs and outputs.

After extending a model or adding a new one, you can update both modules by calling write_sequencealiases():

>>> from hydpy.core.aliastools import write_sequencealiases
>>> write_sequencealiases()