The main package __init__.py module bootstraps the data model system at import time.
pvgisprototype ¶
Dynamic Data Model Initialization
This module generates PVGIS-native data model classes (in fact Pydantic data models) from centralized definitions and registers them into the global namespace. The generated models are then exposed at the package level, so applications usually import them directly from pvgisprototype rather than from this module.
Architecture
The initialization process follows these steps :
- Import data model definitions from
PVGIS_DATA_MODEL_DEFINITIONS. - For each data model definition, ask
DataModelFactoryto build the corresponding data model class. - Register each generated data model class into the module's global namespace
- Remove the raw definitions to keep the public API clean.
This approach provides several benefits:
- Centralized Configuration: All data model schemas defined in one place
- Dynamic Generation: Models created at import time from declarative definitions
- Clean Namespace: Generated models appear as if directly defined in the module
- Consistency: Factory-based approach avoids repetitive boilerplate
Usage
In normal use, models are imported from the top-level package
>>> from pvgisprototype import SomeDataModel
>>> instance = SomeDataModel(field_1=value_1, field_2=value_2, ...)
Direct imports from this __init__ module are typically not required.
Implementation Details
- Uses
DataModelFactory.get_data_model()to construct data model classes - Modifies
globals()to register models at module level - Definitions are deleted after generation to reduce memory footprint
See Also
- pvgisprototype.core.factory.data_model.DataModelFactory : Factory class for data model generation
- pvgisprototype.core.data_model.definitions : Central dictionary of data model definitions
Notes
- All model schemas are centrally defined, making them easy to maintain and evolve
- Runtime model customization is possible based on configuration
- Because models are generated dynamically at import time, static analysis tools (IDEs, type checkers) may not recognize them for autocompletion or type checking
- Consider using type stubs (
.pyifiles) or explicit type annotations for better IDE support and static analysis
Modules:
| Name | Description |
|---|---|
algorithms | |
api | |
cli | |
core | |
log | Unified Logger for PVGIS¶ |
validation | |
web_api | |
Functions:
| Name | Description |
|---|---|
generate_data_models | Generate and register data model classes from definitions. |
generate_data_models ¶
Generate and register data model classes from definitions.
Iterates through the provided data model definitions dictionary and generates corresponding model classes using the DataModelFactory. Each generated data model class is injected into the caller's global namespace, making it accessible as a module-level attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_model_definitions | dict | Dictionary mapping data model names (str) to their configuration/schema definitions. Keys become the class names, values define model structure. | required |
Returns:
| Type | Description |
|---|---|
None | Models are registered as side effects in the global namespace. |
Notes
This function modifies the global namespace by calling globals()[name] = model. It should typically only be called once during module initialization.
Examples:
>>> definitions = {
... 'UserModel': {'fields': [...], 'validators': [...]},
... 'ProductModel': {'fields': [...], 'validators': [...]}
... }
>>> generate_data_models(definitions)
>>> # UserModel and ProductModel are now available as module attributes
Source code in pvgisprototype/__init__.py
def generate_data_models(data_model_definitions: dict):
"""
Generate and register data model classes from definitions.
Iterates through the provided data model definitions dictionary and
generates corresponding model classes using the DataModelFactory. Each
generated data model class is injected into the caller's global namespace,
making it accessible as a module-level attribute.
Parameters
----------
data_model_definitions : dict
Dictionary mapping data model names (str) to their configuration/schema
definitions. Keys become the class names, values define model structure.
Returns
-------
None
Models are registered as side effects in the global namespace.
Notes
-----
This function modifies the global namespace by calling `globals()[name] = model`.
It should typically only be called once during module initialization.
Examples
--------
>>> definitions = {
... 'UserModel': {'fields': [...], 'validators': [...]},
... 'ProductModel': {'fields': [...], 'validators': [...]}
... }
>>> generate_data_models(definitions)
>>> # UserModel and ProductModel are now available as module attributes
"""
for data_model_name in data_model_definitions.keys():
globals()[data_model_name] = DataModelFactory.get_data_model(
data_model_name=data_model_name,
data_model_definitions=data_model_definitions
)