Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use all optional model to improve code quality / readability #4

Merged
merged 12 commits into from
Sep 23, 2024

Conversation

MartinBelthle
Copy link
Contributor

@MartinBelthle MartinBelthle commented Sep 20, 2024

Thanks to a home-made all_optional_model decorator (copy of the one I've created inside Antares Web) we're able to create a class that have all default values, example:

class NonOptionalSTStorageProperties(BaseModel):
    withdrawal_nominal_capacity: float = 0
    efficiency: float = 1
    initial_level: float = 0.5
    enabled: bool = True

and another class

@all_optional_model
class STStorageProperties(NonOptionalSTStorageProperties):
    pass

Thanks to this we avoid this horrible code:

self._withdrawal_nominal_capacity = check_if_none(st_storage_properties.withdrawal_nominal_capacity, 0)
self._efficiency = check_if_none(st_storage_properties.efficiency, 1)
self._initial_level = check_if_none(st_storage_properties.initial_level, 0.5)
self._enabled = check_if_none(st_storage_properties.enabled, True)

and this code (not better):

return STStorageProperties(
    group=self._group,
    injection_nominal_capacity=self._injection_nominal_capacity,
    withdrawal_nominal_capacity=self._withdrawal_nominal_capacity,
    reservoir_capacity=self._reservoir_capacity,
    efficiency=self._efficiency,
    initial_level=self._initial_level,
    initial_level_optim=self._initial_level_optim,
    enabled=self._enabled,
)

IMO it's a better way to do things but we can discuss that

@MartinBelthle MartinBelthle changed the title Use all optional model to improve code quality / readability use all optional model to improve code quality / readability Sep 20, 2024
@MartinBelthle
Copy link
Contributor Author

MartinBelthle commented Sep 23, 2024

We could rename the class NonOptionalSTStorageProperties in DefaultSTStorageProperties which could be more accurate

Copy link
Contributor

@Sigurd-Borge Sigurd-Borge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great - thank you Martin.

@Sigurd-Borge Sigurd-Borge merged commit d1d31d3 into main Sep 23, 2024
4 checks passed
@Sigurd-Borge Sigurd-Borge deleted the simplify-code branch September 23, 2024 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants