-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add rcfile function (which loads rc params from a given file). #861
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
Conversation
thanks for the contribution, @memmett - though I haven't had a chance to test it yet. I also think it might make sense to wrap this functionality in a context manager - since if someone isn't simply changing their default RC, they are probably going to be using a few different ones. it would be something like with rc_context(fname):
plt.plot()
... and then at the end, the original rc settings are restored. |
@@ -892,6 +898,13 @@ def rcdefaults(): | |||
""" | |||
rcParams.update(rcParamsDefault) | |||
|
|||
def rcfile(fname): |
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.
rename this to rc_file, to be PEP8 compliant and consistent with the other functions around it
@ivanov, thanks for the suggestions. I'll try creating a context manager... |
just to be more explicit, I think that both types of functionality have their uses, they complement each other nicely. |
I sent a note to the -devel list about this, and also pinging @tonysyu and @arsenovic since tonysyu/mpltools#2 is getting at this as well. |
rc_context is a context manager that optionally accepts a file name: with mpl.rc_context(): mpl.rc('lines', linewidth=22) ... or with mpl.rc_context(fname): ...
@ivanov I added a context manager that optionally accepts a filename, and renamed rcfile to rc_file. |
@memmett this is shaping up nicely, can you add docstrings to rc_context? Also, some tests for the new functionality would be great. thinking some more about this, it occurs to me that it maybe also make sense to have
that way one wouldn't have to do the kind of rc param saving that you're now doing in the context manager by hand, but also not be forced to go through the file system in order to make use of the context manager. There's probably a clean separation to be made here, but I'm struggling to see it at the moment. Certainly checking the type of the argument that was passed would be one approach to move forward. It reminds me of a similar bifurcation that happens whenever you want a function to accept either a filename or a file object, and I never feel good about solving that in the naive manner. |
@ivanov I finally got around to adding a docstring for the context manager and allowing the context manager to accept a dictionary of new settings too. I guess we could use non-keyword arguments and test their type (isinstance(arg, str)) to either: load settings from a file in the case of a string, or update the rcParams dictionary in the case of a dictionary. I think this (keyword arguments as currently implemented in the pull request vs non-keyword arguments as described) is a matter of taste - so I'll leave that to you and the other devs to decide. I'll be happy to implement either technique - just let me know. |
This looks great. I'd like to see a unit test -- at least for rc_context to ensure that params added by the context manager are reverted after the |
@memmett: I would love to see this feature in v1.2 (which is going to be frozen on the 20th). I believe the action is with you to add some tests. If you need a hand with this, please let us know and we can try to provide some guidance. |
@pelson Thanks for reminding me about this! I'm happy to add some tests - where do these belong? |
It looks to me like the thing to do is make a new test module: lib/matplotlib/tests/test_rcparams.py |
assert mpl.rcParams['lines.linewidth'] == linewidth | ||
|
||
# test rc_file | ||
mpl.rc_file(fname) |
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 will have a global effect on the tests and will need to be undone at the end of execution. Perhaps:
try:
mpl.rc_file(fname)
assert mpl.rcParams['lines.linewidth'] == 33
finally:
linewidth = mpl.rcParams['lines.linewidth']
Other than my comments, this looks good to me. +1 |
This is good to go, but cannot be merged automatically. This is probably due to a collision in |
Actually, we will need to add something to |
Should I put a note in "new in matplotlib-1.2"? |
rc_context is a context manager that optionally accepts a file name: with mpl.rc_context(): mpl.rc('lines', linewidth=22) ... or with mpl.rc_context(fname): ...
Alrighty, hopefully my rebase worked! |
@memmett: Great work. I really like this feature. Merging. |
Add rcfile function (which loads rc params from a given file) and associated context manager.
The new 'rcfile' function allows the user to load rc parameters from an explicitly named file.
The rc_params function was split into two to facilitate this change.