aliastools¶
This module provides tools for the efficient handling of input and output sequence aliases.
Note
We are not sure if class LazyInOutSequenceImport always works as expected
under Python 3.6. Hence, function write_sequencealiases() does not use it
when generating the alias modules inputs and outputs if one executes it
with Python 3.6. Applying LazyInOutSequenceImport under Python 3.6 is at
your own risk.
Module aliastools implements the following members:
LazyInOutSequenceImportImport the input or output sequence the alias is referring to only when required.
write_sequencealiases()Write the alias modules inputs and outputs.
-
class
hydpy.core.aliastools.LazyInOutSequenceImport(modulename: str, classname: str, alias: str, namespace: Dict[str, Any])[source]¶ Bases:
objectImport 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
LazyInOutSequenceImportfor 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 aLazyInOutSequenceImportobject instead with an input or output class object (except when directly asking for it with functiontype).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() >>> def normalise_typestring(typestr): ... # for handling differences between Python 3.6 and newer versions ... if "class" in typestr: ... typestr = typestr.split("'")[1] ... if typestr == "typing.GenericMeta": ... return "type" ... return typestr >>> normalise_typestring(str(type(hland_T))) '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' >>> normalise_typestring(str(type(hland_T))) '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 # correct for Python 3.7 and newer but incorrect for Python 3.6
>>> hland_T = get_example_alias() >>> hland_T != P True >>> hland_T = get_example_alias() >>> P != hland_T True
>>> hland_T = get_example_alias() >>> normalise_typestring(str(hland_T)) 'hydpy.models.hland.hland_inputs.T'
>>> hland_T = get_example_alias() >>> normalise_typestring(repr(hland_T)) '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()