-
Notifications
You must be signed in to change notification settings - Fork 12
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
Streamline kwargs #99
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These code changes are pretty slick; thanks for streamlining things @gonuke. I just have a question about exports in parastell.py
and some requests regarding docstrings. I'll try to push a commit to this PR for the docstrings.
…r convention for Python users
In my commit, I changed However, using these methods requires inputs to be defined in dictionary format, which is different from how we've defined inputs for the rest of the Python API. It might be best just to enforce one canonical input format for Python users, and maintain that |
Right. My first pass just made these global functions and took |
I think making |
…use of dictionaries as inputs
stellarator.construct_invessel_build(**invessel_build) | ||
stellarator.construct_magnets(**magnet_coils) | ||
stellarator.construct_source_mesh(**source_mesh) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@connoramoreno These all have positional arguments - shouldn't we call them with those arguments extracted from the dictionaries explicitly here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining the positional arguments and unpacking the respective dictionaries in the same function call defines those positional arguments twice, causing a TypeError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just unpacking the dictionaries will appropriately set the positional arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to explicitly set positional arguments, we'll need to do something similar to what I had before where secondary dictionaries are created for the kwargs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK - Is there an error if one of the positional arguments is missing? (Maybe we should add a test for that at some point)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, if a positional argument is missing, a TypeError
will be thrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E.g., I removed poloidal_angles
from config.yaml
to test:
File "/home/camoreno/parastell/parastell/parastell.py", line 545, in build_full_model
stellarator.construct_invessel_build(**invessel_build)
TypeError: Stellarator.construct_invessel_build() missing 1 required positional argument: 'poloidal_angles'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ready to merge, then...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good now. Thanks @gonuke
As I started looking at implementing NWL in the OO version, I realized that we were kind of heavy-handed with kwargs. I think this finds a better balance. Let me know what you think.
Some explanations for the changes:
construct_kwargs_from_dict
were for passing into a constructor for some class. That class is the best place to know what arguments are allowable, so its best to pass in the whole dictionary and then cherry-pick relevant data in the constructor.set_kwarg_attrs
was the function that cherry-picked from the dictionary, but raised exceptions by default for extra arguments. We probably discussed this at the time, and I may have recommended or endorsed it, but upon reflection it seems heavy-handed. It is only because of this that we had to carefully curate thekwargs
that we passed in the first place and maybe its fine to just silently ignore extrakwargs
.set_kwarg_attrs
became simpler, I decided to just put the code directly into the constructors rather than as a function. (this is perhaps dubious in our normal spirit of reuse...)construct_kwargs_from_dict
tofilter_kwargs
as this is more what it does and makes the name shorter.