-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Added a find_all method to the RcParams dictionary. #1861
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…s & added some tests.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,8 +706,8 @@ class RcParams(dict): | |
:mod:`matplotlib.rcsetup` | ||
""" | ||
|
||
validate = dict([ (key, converter) for key, (default, converter) in \ | ||
defaultParams.iteritems() ]) | ||
validate = dict((key, converter) for key, (default, converter) in \ | ||
defaultParams.iteritems()) | ||
msg_depr = "%s is deprecated and replaced with %s; please use the latter." | ||
msg_depr_ignore = "%s is deprecated and ignored. Use %s" | ||
|
||
|
@@ -738,6 +738,17 @@ def __getitem__(self, key): | |
key = alt | ||
return dict.__getitem__(self, key) | ||
|
||
def __repr__(self): | ||
import pprint | ||
class_name = self.__class__.__name__ | ||
indent = len(class_name) + 1 | ||
repr_split = pprint.pformat(dict(self), indent=1, width=80 - indent).split('\n') | ||
repr_indented = ('\n' + ' ' * indent).join(repr_split) | ||
return '{}({})'.format(class_name, repr_indented) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this style of formatting is not supported in python2.6 In [5]: '{}({})'.format('sorry', '>= 2.7 only')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-5-b84304fedd68> in <module>()
----> 1 '{}({})'.format('sorry', '>= 2.7 only')
ValueError: zero length field name in format There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @ivanov - travis spotted this one too - I'm always making this mistake! Bring on ending support for 2.6 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Phil Elson, on 2013-04-12 12:32, wrote:
Good thing Travis spotted it as well, though sometimes it's easy
You won't hear complaints from me about it. Machines where I'm |
||
|
||
def __str__(self): | ||
return '\n'.join('{}: {}'.format(k, v) for k, v in sorted(self.items())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above (>= 2.7 only) |
||
|
||
def keys(self): | ||
""" | ||
Return sorted list of keys. | ||
|
@@ -750,22 +761,24 @@ def values(self): | |
""" | ||
Return values in order of sorted keys. | ||
""" | ||
return [self[k] for k in self.iterkeys()] | ||
return [self[k] for k in self.keys()] | ||
|
||
def find_all(self, key_contains): | ||
def find_all(self, pattern): | ||
""" | ||
Return the subset of this RcParams dictionary for which the given | ||
``key_contains`` string is found somewhere in the key. | ||
``pattern`` string is found, by :func:`re.search`, somewhere in the key. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this a little bit unclear. Maybe just say "pattern is a Python regular expression". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @mdboom. I'll change to:
I'm happy to iterate on this though if you have further improvements? |
||
|
||
.. note:: | ||
|
||
Changes to the returned dictionary are *not* propagated to | ||
the parent RcParams dictionary. | ||
|
||
""" | ||
import re | ||
pattern_re = re.compile(pattern) | ||
return RcParams((key, value) | ||
for key, value in self.items() | ||
if key_contains in key) | ||
if pattern_re.search(key)) | ||
|
||
|
||
def rc_params(fail_on_error=False): | ||
|
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 know this has been merged already, but I only just saw this trailing backslash.
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.
Thanks for finding. I have just fixed directly on
1.3.x
in `207469f