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

Skip to content

Commit c5770fd

Browse files
committed
Corrections from @efiring
1 parent 4fa16f3 commit c5770fd

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

lib/matplotlib/cbook.py

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,7 @@ class _FuncInfo(object):
26682668
Class used to store a function
26692669
26702670
Each object has:
2671-
* The direct function (direct)
2671+
* The direct function (function)
26722672
* The inverse function (inverse)
26732673
* A boolean indicating whether the function
26742674
is bounded in the interval 0-1 (bounded_0_1), or
@@ -2678,23 +2678,23 @@ class _FuncInfo(object):
26782678
certain combination of parameters is valid.
26792679
26802680
"""
2681-
def __init__(self, direct, inverse, bounded_0_1=True, check_params=None):
2682-
self.direct = direct
2681+
def __init__(self, function, inverse, bounded_0_1=True, check_params=None):
2682+
self.function = function
26832683
self.inverse = inverse
26842684

2685-
if (hasattr(bounded_0_1, '__call__')):
2685+
if callable(bounded_0_1):
26862686
self._bounded_0_1 = bounded_0_1
26872687
else:
26882688
self._bounded_0_1 = lambda x: bounded_0_1
26892689

26902690
if check_params is None:
26912691
self._check_params = lambda x: True
2692-
elif (hasattr(check_params, '__call__')):
2692+
elif callable(check_params):
26932693
self._check_params = check_params
26942694
else:
26952695
raise ValueError("Check params must be a callable, returning "
26962696
"a boolean with the validity of the passed "
2697-
"parameters or None.")
2697+
"parameters, or None.")
26982698

26992699
def is_bounded_0_1(self, params=None):
27002700
return self._bounded_0_1(params)
@@ -2767,52 +2767,56 @@ def __init__(self, str_func):
27672767
String to be parsed.
27682768
27692769
"""
2770-
try: # For python 2.7 and python 3+ compatibility
2771-
is_str = isinstance(str_func, basestring)
2772-
except NameError:
2773-
is_str = isinstance(str_func, str)
27742770

2775-
if not is_str:
2771+
if not isinstance(str_func, six.string_types):
27762772
raise ValueError("The argument passed is not a string.")
2777-
self._str_func = str_func
2773+
self._str_func = six.text_type(str_func)
27782774
self._key, self._params = self._get_key_params()
2779-
self._func = self.func
2775+
self._func = self._parse_func()
27802776

2781-
@property
2782-
def func(self):
2777+
def _parse_func(self):
27832778
"""
2784-
Returns the _FuncInfo object, replacing the relevant parameters if
2785-
necessary in the lambda functions.
2779+
Parses the parameters to build a new _FuncInfo object,
2780+
replacing the relevant parameters if necessary in the lambda
2781+
functions.
27862782
27872783
"""
27882784

27892785
func = self._funcs[self._key]
27902786
if self._params:
2791-
m = func.direct
2792-
direct = (lambda x, m=m: m(x, self._params))
2787+
m = func.function
2788+
function = (lambda x, m=m: m(x, self._params))
27932789

27942790
m = func.inverse
27952791
inverse = (lambda x, m=m: m(x, self._params))
27962792

27972793
is_bounded_0_1 = func.is_bounded_0_1(self._params)
27982794

2799-
func = _FuncInfo(direct, inverse,
2795+
func = _FuncInfo(function, inverse,
28002796
is_bounded_0_1)
28012797
else:
2802-
func = _FuncInfo(func.direct, func.inverse,
2798+
func = _FuncInfo(func.function, func.inverse,
28032799
func.is_bounded_0_1())
28042800
return func
28052801

28062802
@property
2807-
def directfunc(self):
2803+
def func_info(self):
2804+
"""
2805+
Returns the _FuncInfo object.
2806+
2807+
"""
2808+
return self._func
2809+
2810+
@property
2811+
def function(self):
28082812
"""
28092813
Returns the callable for the direct function.
28102814
28112815
"""
2812-
return self._func.direct
2816+
return self._func.function
28132817

28142818
@property
2815-
def invfunc(self):
2819+
def inverse(self):
28162820
"""
28172821
Returns the callable for the inverse function.
28182822
@@ -2829,30 +2833,28 @@ def is_bounded_0_1(self):
28292833
return self._func.is_bounded_0_1()
28302834

28312835
def _get_key_params(self):
2832-
str_func = six.text_type(self._str_func)
2836+
str_func = self._str_func
28332837
# Checking if it comes with parameters
28342838
regex = '\{(.*?)\}'
28352839
params = re.findall(regex, str_func)
28362840

28372841
if params:
2838-
for i in range(len(params)):
2842+
for i, param in enumerate(params):
28392843
try:
2840-
params[i] = float(params[i])
2841-
except:
2842-
raise ValueError("Error with parameter number %i: '%s'. "
2843-
"'p' in parametric function strings must "
2844-
" be replaced by a number that is not "
2845-
"zero, e.g. 'log10(x+{0.1})'." %
2846-
(i, params[i]))
2844+
params[i] = float(param)
2845+
except ValueError:
2846+
raise ValueError("Parameter %i is '%s', which is "
2847+
"not a number." %
2848+
(i, param))
28472849

28482850
str_func = re.sub(regex, '{p}', str_func)
28492851

28502852
try:
28512853
func = self._funcs[str_func]
2852-
except:
2854+
except ValueError, KeyError:
28532855
raise ValueError("%s: invalid string. The only strings "
28542856
"recognized as functions are %s." %
2855-
(str_func, self._funcs.keys()))
2857+
(str_func, list(self._funcs)))
28562858

28572859
# Checking that the parameters are valid
28582860
if not func.check_params(params):

lib/matplotlib/tests/test_cbook.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -553,22 +553,22 @@ class TestFuncParser(object):
553553
ids=validstrings)
554554
def test_values(self, string, func):
555555
func_parser = cbook._StringFuncParser(string)
556-
f = func_parser.directfunc
556+
f = func_parser.function
557557
assert_array_almost_equal(f(self.x_test), func(self.x_test))
558558

559559
@pytest.mark.parametrize("string", validstrings, ids=validstrings)
560560
def test_inverse(self, string):
561561
func_parser = cbook._StringFuncParser(string)
562-
f = func_parser.func
563-
fdir = f.direct
562+
f = func_parser.func_info
563+
fdir = f.function
564564
finv = f.inverse
565565
assert_array_almost_equal(finv(fdir(self.x_test)), self.x_test)
566566

567567
@pytest.mark.parametrize("string", validstrings, ids=validstrings)
568-
def test_get_invfunc(self, string):
568+
def test_get_inverse(self, string):
569569
func_parser = cbook._StringFuncParser(string)
570-
finv1 = func_parser.invfunc
571-
finv2 = func_parser.func.inverse
570+
finv1 = func_parser.inverse
571+
finv2 = func_parser.func_info.inverse
572572
assert_array_almost_equal(finv1(self.x_test), finv2(self.x_test))
573573

574574
@pytest.mark.parametrize("string, bounded",

0 commit comments

Comments
 (0)