-
Notifications
You must be signed in to change notification settings - Fork 536
[ENH] Convenient load/save of interface inputs #1591
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
43bd933
[ENH] Convenient load/save of interface inputs
oesteban f1725c1
add save fn, add test #1578
oesteban a9feb80
add from_file, add test
oesteban ee3bfd2
load_inputs_from_json now overwrites inputs
oesteban e837d27
update CHANGES
oesteban 678b710
fix CHANGES
oesteban 48c80b4
add overwrite argument to json load
oesteban 7b05e48
Merge branch 'master' into enh/InterfaceLoadSettings
oesteban 775633d
fix tests, use get_traitsfree, etc
oesteban fe208e6
Merge remote-tracking branch 'upstream/master' into enh/InterfaceLoad…
oesteban 701dbc5
amend sloppy merge
oesteban 1788edf
fix json dump, add tests to check overwrite
oesteban 8958e9b
Merge branch 'master' into enh/InterfaceLoadSettings
oesteban 8ef4d64
Merge remote-tracking branch 'upstream/master' into enh/InterfaceLoad…
oesteban 3f2612a
update documentation
oesteban 0d4ea0e
update ants registration example
oesteban 4b155b1
fix details in docs and example
oesteban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,7 @@ | |
| from datetime import datetime as dt | ||
| from dateutil.parser import parse as parseutc | ||
| from warnings import warn | ||
|
|
||
| import simplejson as json | ||
|
|
||
| from .traits_extension import (traits, Undefined, TraitDictObject, | ||
| TraitListObject, TraitError, | ||
|
|
@@ -56,6 +56,8 @@ | |
|
|
||
| iflogger = logging.getLogger('interface') | ||
|
|
||
| PY35 = sys.version_info >= (3, 5) | ||
|
|
||
| if runtime_profile: | ||
| try: | ||
| import psutil | ||
|
|
@@ -758,14 +760,19 @@ class BaseInterface(Interface): | |
| _additional_metadata = [] | ||
| _redirect_x = False | ||
|
|
||
| def __init__(self, **inputs): | ||
| def __init__(self, from_file=None, **inputs): | ||
| if not self.input_spec: | ||
| raise Exception('No input_spec in class: %s' % | ||
| self.__class__.__name__) | ||
|
|
||
| self.inputs = self.input_spec(**inputs) | ||
| self.estimated_memory_gb = 1 | ||
| self.num_threads = 1 | ||
|
|
||
| if from_file is not None: | ||
| self.load_inputs_from_json(from_file, overwrite=False) | ||
|
|
||
|
|
||
| @classmethod | ||
| def help(cls, returnhelp=False): | ||
| """ Prints class help | ||
|
|
@@ -1148,6 +1155,34 @@ def version(self): | |
| self.__class__.__name__) | ||
| return self._version | ||
|
|
||
| def load_inputs_from_json(self, json_file, overwrite=True): | ||
| """ | ||
| A convenient way to load pre-set inputs from a JSON file. | ||
| """ | ||
|
|
||
| with open(json_file) as fhandle: | ||
| inputs_dict = json.load(fhandle) | ||
|
|
||
| for key, newval in list(inputs_dict.items()): | ||
| if not hasattr(self.inputs, key): | ||
| continue | ||
| val = getattr(self.inputs, key) | ||
| if overwrite or not isdefined(val): | ||
| setattr(self.inputs, key, newval) | ||
|
|
||
| def save_inputs_to_json(self, json_file): | ||
| """ | ||
| A convenient way to save current inputs to a JSON file. | ||
| """ | ||
| inputs = self.inputs.get() | ||
|
||
| for key, val in list(inputs.items()): | ||
| if not isdefined(val): | ||
| inputs.pop(key, None) | ||
|
|
||
| iflogger.debug('saving inputs {}', inputs) | ||
| with open(json_file, 'w') as fhandle: | ||
| json.dump(inputs, fhandle, indent=4) | ||
|
|
||
|
|
||
| class Stream(object): | ||
| """Function to capture stdout and stderr streams with timestamps | ||
|
|
||
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
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.
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.
this may be sufficient except for checks:
self.inputs.update(**inputs_dict)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.
mmm, I'm getting object does not have update. (?)
Anyways, I will use sets and get_traitsfree to implement this in a cleaner manner.