-
Notifications
You must be signed in to change notification settings - Fork 51
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
fix from_json methods, add from_json_file and from_dict methods #288
Comments
In molsetup.py, some are decoders (like the @diogomart To make the method names consistent, would you be interested in the idea of making these basic methods (to_json, to_json_file, from_json, from_json_file) inheritable from a master (base) class? Earlier, I wanted to do the same thing in another issue I was also wondering if you might be interested in moving encoders and decoders into their own respective class? The expected keys, and the encode and decode function will be defined within the scope of the respective class. When there's a layered structure, then higher-level class can reference the encoders/decoders in the lower-level class. I'm not sure if this is common or if there's any efficiency drawback, but it might make maintenance easier. Let me know what you think, and I'm willing to take the work! |
Inheriting for these JSON interface methods carries the risk of having exceptional situations later and being unable to adhere to the base class. Maybe we'll have some new object for which reading from JSON doesn't make sense, then we'll choose to not inherit from it partially defeating the purpose of a base class, or have to find hacky ways to inherit anyway but not implement all the functions. At first sight I rather not inherit, but I'll think about it more... I like the features of the base class in #279 We also can't return the object passed as input (a dictionary) if the expected keys don't match: we need to raise an error. For example, if we dump a Polymer to JSON with v0.6.1 and load it again with a more recent version after PR #276 we get dictionaries instead of MoleculeSetup objects because the ring_corners attribute is not expected anymore. Raising an error would have allowed use to catch this in the tests. We need to create an exception like this one from #254 Lines 332 to 342 in 8780dd0
|
@diogomart thanks so much for your response. All this makes sense to me, and I understand it's difficult to implement at the moment.
This is currently handled by the individual decoder, but I wish the expected_keys are defined within each class like the structure in #279. Then, the inheritable methods always take these criteria from within the class. In #279,
This kind of error handling can also be made inheritable. I appreciate your insights. We can talk more in the future, if we want to re-organize this, and let me know if I can be of some use |
with open("my_configuration.json") as f:
json_string = f.read()
config_dict = json.loads(json_string)
mk_prep = MoleculePreparation.from_config(config_dict) becomes a nice one-liner: mk_prep = MoleculePreparation.from_json_file("my_configuration.json") |
For anyone following/reading this thread, here's an update: After the merge of #292, See a description of the structure and relation here: Usage and options Closing as resolved, but please feel free to re-open if you have any comments or suggestions regarding this. |
Some
from_json
methods in take Python dictionaries as argument. These methods should be renamed tofrom_dict
. Then,from_json
methods wrap thefrom_dict
ones. Andfrom_json_file
wrap thefrom_json
ones.The text was updated successfully, but these errors were encountered: