8000 Rcparam validation fix by tacaswell · Pull Request #3564 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Rcparam validation fix #3564

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 19 commits into from
Oct 14, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MNT : simplify and relax validation on nseq
Simplify and relax the validation for sequences of floats and
ints.

 - unified logic
 - input is not restricted to coma-separated lists, list, and tuples.
   any length 2 sequence (like an array) will now work.
  • Loading branch information
tacaswell committed Sep 25, 2014
commit a298ab2859561796f1f16efe440d3722502797b6
56 changes: 30 additions & 26 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,50 +172,54 @@ def validate_maskedarray(v):
' please delete it from your matplotlibrc file')


class validate_nseq_float:
_seq_err_msg = ('You must supply exactly {n:d} values, you provided '
'{num:d} values: {s}')

_str_err_msg = ('You must supply exactly {n:d} comma-separated values, '
'you provided '
'{num:d} comma-separated values: {s}')


class validate_nseq_float(object):
def __init__(self, n):
self.n = n

def __call__(self, s):
"""return a seq of n floats or raise"""
if isinstance(s, six.string_types):
ss = s.split(',')
if len(ss) != self.n:
raise ValueError(
'You must supply exactly %d comma separated values' %
self.n)
try:
return [float(val) for val in ss]
except ValueError:
raise ValueError('Could not convert all entries to floats')
s = s.split(',')
err_msg = _str_err_msg
else:
assert type(s) in (list, tuple)
if len(s) != self.n:
raise ValueError('You must supply exactly %d values' % self.n)
err_msg = _seq_err_msg

if len(s) != self.n:
raise ValueError(err_msg.format(n=self.n, num=len(s), s=s))

try:
return [float(val) for val in s]
except ValueError:
raise ValueError('Could not convert all entries to floats')


class validate_nseq_int:
class validate_nseq_int(object):
def __init__(self, n):
self.n = n

def __call__(self, s):
"""return a seq of n ints or raise"""
if isinstance(s, six.string_types):
ss = s.split(',')
if len(ss) != self.n:
raise ValueError(
'You must supply exactly %d comma separated values' %
self.n)
try:
return [int(val) for val in ss]
except ValueError:
raise ValueError('Could not convert all entries to ints')
s = s.split(',')
err_msg = _str_err_msg
else:
assert type(s) in (list, tuple)
if len(s) != self.n:
raise ValueError('You must supply exactly %d values' % self.n)
err_msg = _seq_err_msg

if len(s) != self.n:
raise ValueError(err_msg.format(n=self.n, num=len(s), s=s))

try:
return [int(val) for val in s]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time for some factorisation? This is class is exactly the same as the float version, except for the float/int right?

except ValueError:
raise ValueError('Could not convert all entries to ints')


def validate_color(s):
Expand Down
0