Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f4c1aba

Browse files
committed
simplified option validation using the generic ValidateInStrings where possible
svn path=/trunk/matplotlib/; revision=3426
1 parent 409e210 commit f4c1aba

File tree

1 file changed

+47
-54
lines changed

1 file changed

+47
-54
lines changed

lib/matplotlib/__init__.py

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,26 @@ def compare_versions(a, b):
468468
else: return False
469469
else: return False
470470

471+
472+
##########################
473+
# to go to rcdefaults.py
474+
#
475+
476+
class ValidateInStrings:
477+
def __init__(self, key, valid, ignorecase=False):
478+
'valid is a list of legal strings'
479+
self.key = key
480+
self.ignorecase = ignorecase
481+
def func(s):
482+
if ignorecase: return s.lower()
483+
else: return s
484+
self.valid = dict([(func(k),k) for k in valid])
485+
486+
def __call__(self, s):
487+
if self.ignorecase: s = s.lower()
488+
if s in self.valid: return self.valid[s]
489+
raise ValueError('Unrecognized %s string "%s": valid strings are %s'%(self.key, s, self.valid.values()))
490+
471491
def validate_path_exists(s):
472492
'If s is a path, return s, else False'
473493
if os.path.exists(s): return s
@@ -494,36 +514,19 @@ def validate_int(s):
494514
except ValueError:
495515
raise ValueError('Could not convert "%s" to int' % s)
496516

497-
def validate_backend(s, fail_on_err = True):
498-
s=s.lower()
499-
backends = ['Agg2', 'Agg', 'Aqt', 'Cairo', 'CocoaAgg', 'EMF', 'GD', 'GDK',
500-
'GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'Paint', 'Pdf', 'PS',
501-
'QtAgg', 'Qt4Agg', 'SVG', 'Template', 'TkAgg', 'WX', 'WXAgg']
502-
for i in backends:
503-
if s == i.lower(): return i
504-
if fail_on_err: raise ValueError('Backend must be %s, or %s'% \
505-
', '.join((backends[:-1], backends[-1])))
506-
else: return None
507-
508-
def validate_numerix(s):
509-
'return "Numeric" or "numarray" or "numpy" or raise'
510-
sl = s.lower()
511-
if sl=='numeric': return 'Numeric'
512-
elif sl=='numarray': return 'numarray'
513-
elif sl=='numpy': return 'numpy'
514-
else:
515-
raise ValueError('Numerix must be Numeric, numarray, or numpy')
517+
validate_backend = ValidateInStrings('backend',[
518+
'Agg2', 'Agg', 'Aqt', 'Cairo', 'CocoaAgg', 'EMF', 'GD', 'GDK',
519+
'GTK', 'GTKAgg', 'GTKCairo', 'FltkAgg', 'Paint', 'Pdf', 'PS',
520+
'QtAgg', 'Qt4Agg', 'SVG', 'Template', 'TkAgg', 'WX', 'WXAgg',
521+
], ignorecase=True)
516522

517-
def validate_toolbar(s):
518-
"""
519-
return toolbar string 'None', 'classic', 'toolbar2'
520-
"""
521-
s = s.lower()
522-
if s=='none': return 'None'
523-
elif s=='classic': return s
524-
elif s=='toolbar2': return s
525-
else:
526-
raise ValueError('toolbar must be None | classic | toolbar2')
523+
validate_numerix = ValidateInStrings('numerix',[
524+
'Numeric','numarray','numpy',
525+
], ignorecase=True)
526+
527+
validate_toolbar = ValidateInStrings('toolbar',[
528+
'None','classic','toolbar2',
529+
], ignorecase=True)
527530

528531
class validate_nseq_float:
529532
def __init__(self, n):
@@ -585,6 +588,10 @@ def validate_comma_sep_str(s):
585588
except ValueError:
586589
raise ValueError('Could not convert all entries to strings')
587590

591+
validate_orientation = ValidateInStrings('orientation',[
592+
'landscape', 'portrait',
593+
])
594+
588595
def validate_latex_preamble(s):
589596
'return a list'
590597
preamble_list = validate_comma_sep_str(s)
@@ -599,22 +606,6 @@ def validate_latex_preamble(s):
599606
return preamble_list
600607

601608

602-
class ValidateInStrings:
603-
def __init__(self, valid, ignorecase=False):
604-
'valid is a list of legal strings'
605-
self.ignorecase = ignorecase
606-
def func(s):
607-
if ignorecase: return s.lower()
608-
else: return s
609-
self.validd = dict([(func(k),1) for k in valid])
610-
611-
def __call__(self, s):
612-
if self.ignorecase: s = s.lower()
613-
if s in self.validd: return s
614-
raise ValueError('Unrecognized string "%s": valid strings are %s'%(s, self.validd.keys()))
615-
616-
617-
validate_orientation = ValidateInStrings(['landscape', 'portrait'])
618609

619610
def validate_aspect(s):
620611
if s in ('auto', 'equal'):
@@ -633,13 +624,15 @@ def validate_fontsize(s):
633624
except ValueError:
634625
raise ValueError('not a valid font size')
635626

636-
validate_verbose = ValidateInStrings(Verbose.levels)
627+
validate_verbose = ValidateInStrings('verbose',[
628+
'silent', 'helpful', 'debug', 'debug-annoying',
629+
])
637630

638-
validate_ps_papersize = ValidateInStrings([
639-
'auto', 'letter', 'legal', 'ledger',
640-
'a0', 'a1', 'a2','a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10',
641-
'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10',
642-
], ignorecase=True)
631+
validate_ps_papersize = ValidateInStrings('ps_papersize',[
632+
'auto', 'letter', 'legal', 'ledger',
633+
'a0', 'a1', 'a2','a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10',
634+
'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10',
635+
], ignorecase=True)
643636

644637
def validate_ps_distiller(s):
645638
s = s.lower()
@@ -715,17 +708,17 @@ def validate_usetex(s):
715708
else:
716709
return bl
717710

718-
validate_joinstyle = ValidateInStrings(['miter', 'round', 'bevel'], ignorecase=True)
711+
validate_joinstyle = ValidateInStrings('joinstyle',['miter', 'round', 'bevel'], ignorecase=True)
719712

720-
validate_capstyle = ValidateInStrings(['butt', 'round', 'projecting'], ignorecase=True)
713+
validate_capstyle = ValidateInStrings('capstyle',['butt', 'round', 'projecting'], ignorecase=True)
721714

722715
def validate_linecol_linestyle(s):
723716
try:
724717
dashes = validate_nseq_float(2)(s)
725718
warnings.warn("Deprecated negative_linestyle specification; use 'solid' or 'dashed'")
726719
return (0, dashes) # (offset, (solid, blank))
727720
except ValueError:
728-
V = ValidateInStrings(['solid', 'dashed'], ignorecase=True)
721+
V = ValidateInStrings('linecol_linestyle',['solid', 'dashed'], ignorecase=True)
729722
return(V(s))
730723

731724
class ValidateInterval:

0 commit comments

Comments
 (0)