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

Convience methods to write to/from json #359

Open
jthorton opened this issue Oct 11, 2024 · 2 comments
Open

Convience methods to write to/from json #359

jthorton opened this issue Oct 11, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@jthorton
Copy link
Contributor

While writing tests for the industry benchmark I find myself writing the same two bits of code a lot to write and load AlchemicalNetworks to json file. I think it would make sense to add them as functions to avoid repeated code. I would suggest a to_json_file method and a from_json_file class method. In the future we might consider other supported file types like messagepack so a general to_file and from_file might be more appropriate with the file type inferred from the name.

I noticed that the Transformation object can already be written to json using its dump method so I think having some general serialisation options on the GufeTokenizable class would be very useful!

@dotsdl dotsdl added enhancement New feature or request good first issue Good for newcomers labels Oct 15, 2024
@dotsdl dotsdl added this to the Release 1.2 milestone Oct 15, 2024
@dotsdl
Copy link
Member

dotsdl commented Oct 15, 2024

Agree with this @jthorton! Although this gets a bit tricky, since there are multiple representations a GufeTokenizable can take, including dict, shallow_dict, keyed_dict, and now keyed_chain. All of these are readily JSON-serializable except shallow_dict, but which one should be used for e.g. to_json?

What may help is to create a gufe.serialization submodule with convenience functions, such as to_json, from_json, to_msgpack, from_msgpack, etc. These functions could by default return an in-memory representation, but could optionally take a filename or file-like object to write to.

Do you think that would be sufficient, or still be annoying as a downstream user of GufeTokenizables?

@jthorton
Copy link
Contributor Author

All of these are readily JSON-serializable except shallow_dict, but which one should be used for e.g. to_json?

I would say the most minimal one which can be used to roundtrip the same object currently I just use dict as I wasn't sure but if the to/from methods could handle this for me that would be fantastic!

What may help is to create a gufe.serialization submodule with convenience functions, such as to_json, from_json, to_msgpack, from_msgpack, etc. These functions could by default return an in-memory representation, but could optionally take a filename or file-like object to write to.

Great idea, I think that would help a lot and save downstream implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Sprint - Available
Development

No branches or pull requests

2 participants