Skip to content

Comments

Support pickling a linopy model#438

Merged
FabianHofmann merged 7 commits intoPyPSA:masterfrom
victorgarcia98:support-pickle-model
Apr 2, 2025
Merged

Support pickling a linopy model#438
FabianHofmann merged 7 commits intoPyPSA:masterfrom
victorgarcia98:support-pickle-model

Conversation

@victorgarcia98
Copy link
Contributor

Closes #340 .

Changes proposed in this Pull Request

Defining __getstate__ and __setstate__ helps avoid a potential recursion error during pickling, especially in classes that override __getattr__. Since __getattr__ is called whenever an attribute is not found, an implicit or default pickling process might unintentionally trigger it, leading to infinite recursion. By explicitly specifying how the object's state should be serialized (__getstate__) and restored (__setstate__), the class ensures that only its actual attributes are processed, preventing unnecessary lookups and recursion errors during pickling and unpickling.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

Victor Garcia Reolid and others added 7 commits April 2, 2025 21:37
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Copy link
Collaborator

@FabianHofmann FabianHofmann left a comment

Choose a reason for hiding this comment

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

Wonderful @victorgarcia98! Thank you for your contribution!

@FabianHofmann FabianHofmann merged commit 67e13a1 into PyPSA:master Apr 2, 2025
20 checks passed
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.

Serializing and deserializing linopy.Model

2 participants