Refactor file serialization: suffix-driven formats, public to_dict/from_dict#148
Merged
raphaeltimbo merged 1 commit intoJul 18, 2026
Merged
Conversation
…om_dict Replace the per-class toml/json save/load logic with a single serializers module (ccp/data_io/serializers.py): - File format is decided by the file suffix (.toml or .json); the file_type parameter is removed. Unsupported suffixes raise ValueError instead of silently truncating or leaving variables unbound. - Writes are atomic (temp file + os.replace), so a failed dump never corrupts an existing file. - Point, Curve, Impeller, StraightThrough and BackToBack now expose public to_dict()/from_dict() (replacing _dict_to_save/_dict_from_load) and inherit save()/load() from a Serializable mixin. - Saved files record the writing library version in a "ccp_version" key, enabling future format migrations; Curve/Impeller files nest points under a "points" table. Legacy flat files still load. - Fix app pages passing io.StringIO to Impeller/StraightThrough/ BackToBack.load (broken since petrobras#145 for Impeller): pages now use the public from_dict with toml parsed in memory. - Tests: round-trips parametrized over toml and json for Point, Curve and Impeller, unsupported-format error case, and a legacy flat-file load test. Part of the app-page diff is whitespace-only: ruff format applied to files that were not formatted on main.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #145. This refactors how
ccpobjects are saved to and loaded from files, replacing the per-class toml/json branching with a single shared mechanism.Design
point.save("point.json")writes JSON,point.save("point.toml")writes TOML. Thefile_typeparameter is removed, and an unsupported/missing extension raises a clearValueErrorinstead of silently truncating the target file (save) or leavingparametersunbound (load).ccp/data_io/serializers.pyholds the suffix→(load, dump) registry,save_dict/load_dicthelpers, and aSerializablemixin. Adding a future format is one line in the registry.to_dict()/from_dict()onPoint,Curve,Impeller,StraightThroughandBackToBack(replacing private_dict_to_save/_dict_from_load). This gives an in-memory serialization API — used by the app to embed impellers inside.ccpzips without temp files.os.replaced onto the target, so a failed save never corrupts an existing file.ccp_version(same versioning scheme the app'sconvert()migration helper already uses), so future format changes can be migrated.Curve/Impellerfiles nest points under apointstable; legacy flat files (and files withoutccp_version) still load.Bug fix
The app pages pass
io.StringIOtoImpeller.load/StraightThrough.load/BackToBack.loadwhen restoring.ccpsession files. ForImpellerthis has been broken since #145 (loadnow callsopen()on its argument). Pages 1–4 andtest_appnow parse the toml in memory and use the publicfrom_dict.Breaking changes
save(file_name, file_type=...)→save(file_name); the filename must have a.tomlor.jsonextension (no silent extension appending).loadno longer accepts file-like objects — usefrom_dict(toml.loads(...))for in-memory data._dict_to_save/_dict_from_loadare replaced by publicto_dict/from_dict.StraightThrough.save/BackToBack.savegain json support via the mixin; theirfileparameter is nowfile_name.Tests
Point,Curve(previously untested) andImpeller.pytest ccp/tests -n 4 --dist loadfile), including the app integration tests against the real example.ccpfiles.Notes for review
ruff formatapplied to files that were not ruff-clean on main (hide-whitespace view recommended).-m "not slow") pre-date this branch and are unrelated (verified on clean main); worth a separate fix.