pyiron_base.storage.hdfstub module
Convenience class to lazily read values from HDF.
- class pyiron_base.storage.hdfstub.HDFStub(hdf, group_name)
Bases:
object
Provides lazy loading of data from HDF.
Instead of accessing an HDF group directly
>>> hdf[group_name] ...
you can wrap this with this class
>>> stub = HDFStub(hdf, group_name)
and then later perform this lookup with :method:`.load`
>>> stub.load() == hdf[group_name] True
For simple datatypes there’s not a big advantages to this, but
DataContainer
uses this to load its contents lazily and ensure that nested containers are also lazily loaded. This is done by customizing what happend on :method:`.load` via :method:`.register`. This class method adds a callback to the class that will be called when the specified type name is found in the hdf group that is to be loaded.>>> hdf['mytype/NAME'] MyType >>> hdf['mytype/TYPE'] <class 'my.module.MyType'> >>> HDFStub.register(MyType, lambda hdf, group: print(42) or hdf[group].to_object()) >>> my = HDFStub(hdf, 'mytype').load() 42 >>> my MyType(...)
This is intended to allow classes that want to be lazily loaded in a certain way to customize what arguments they pass to_object() (and therefore to their own initializers).
- load()
Read value from HDF.
If group_name is a node in HDF, simply its value will be returned. If it is a group in HDF and the ‘NAME’ node matches any of the types registered with :method:`.register`, it will be loaded with the provided callback. Otherwise it will be loaded with :method:`.ProjectHDFio.to_object()`.
- classmethod register(type, load)
Register call back for a new type.
- Parameters:
type (type) – class to be registered
load (function) – callback that is called on :method:`.load` when the type matches type_name, must accept hdf and group_name corresponding to the init parameters of this class and return (lazily) loaded object
- pyiron_base.storage.hdfstub.to_object(hdf_group)