@@ -2668,7 +2668,7 @@ class _FuncInfo(object):
2668
2668
Class used to store a function
2669
2669
2670
2670
Each object has:
2671
- * The direct function (direct )
2671
+ * The direct function (function )
2672
2672
* The inverse function (inverse)
2673
2673
* A boolean indicating whether the function
2674
2674
is bounded in the interval 0-1 (bounded_0_1), or
@@ -2678,23 +2678,23 @@ class _FuncInfo(object):
2678
2678
certain combination of parameters is valid.
2679
2679
2680
2680
"""
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
2683
2683
self .inverse = inverse
2684
2684
2685
- if ( hasattr ( bounded_0_1 , '__call__' ) ):
2685
+ if callable ( bounded_0_1 ):
2686
2686
self ._bounded_0_1 = bounded_0_1
2687
2687
else :
2688
2688
self ._bounded_0_1 = lambda x : bounded_0_1
2689
2689
2690
2690
if check_params is None :
2691
2691
self ._check_params = lambda x : True
2692
- elif ( hasattr ( check_params , '__call__' ) ):
2692
+ elif callable ( check_params ):
2693
2693
self ._check_params = check_params
2694
2694
else :
2695
2695
raise ValueError ("Check params must be a callable, returning "
2696
2696
"a boolean with the validity of the passed "
2697
- "parameters or None." )
2697
+ "parameters, or None." )
2698
2698
2699
2699
def is_bounded_0_1 (self , params = None ):
2700
2700
return self ._bounded_0_1 (params )
@@ -2767,52 +2767,56 @@ def __init__(self, str_func):
2767
2767
String to be parsed.
2768
2768
2769
2769
"""
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 )
2774
2770
2775
- if not is_str :
2771
+ if not isinstance ( str_func , six . string_types ) :
2776
2772
raise ValueError ("The argument passed is not a string." )
2777
- self ._str_func = str_func
2773
+ self ._str_func = six . text_type ( str_func )
2778
2774
self ._key , self ._params = self ._get_key_params ()
2779
- self ._func = self .func
2775
+ self ._func = self ._parse_func ()
2780
2776
2781
- @property
2782
- def func (self ):
2777
+ def _parse_func (self ):
2783
2778
"""
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.
2786
2782
2787
2783
"""
2788
2784
2789
2785
func = self ._funcs [self ._key ]
2790
2786
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 ))
2793
2789
2794
2790
m = func .inverse
2795
2791
inverse = (lambda x , m = m : m (x , self ._params ))
2796
2792
2797
2793
is_bounded_0_1 = func .is_bounded_0_1 (self ._params )
2798
2794
2799
- func = _FuncInfo (direct , inverse ,
2795
+ func = _FuncInfo (function , inverse ,
2800
2796
is_bounded_0_1 )
2801
2797
else :
2802
- func = _FuncInfo (func .direct , func .inverse ,
2798
+ func = _FuncInfo (func .function , func .inverse ,
2803
2799
func .is_bounded_0_1 ())
2804
2800
return func
2805
2801
2806
2802
@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 ):
2808
2812
"""
2809
2813
Returns the callable for the direct function.
2810
2814
2811
2815
"""
2812
- return self ._func .direct
2816
+ return self ._func .function
2813
2817
2814
2818
@property
2815
- def invfunc (self ):
2819
+ def inverse (self ):
2816
2820
"""
2817
2821
Returns the callable for the inverse function.
2818
2822
@@ -2829,30 +2833,28 @@ def is_bounded_0_1(self):
2829
2833
return self ._func .is_bounded_0_1 ()
2830
2834
2831
2835
def _get_key_params (self ):
2832
- str_func = six . text_type ( self ._str_func )
2836
+ str_func = self ._str_func
2833
2837
# Checking if it comes with parameters
2834
2838
regex = '\{(.*?)\}'
2835
2839
params = re .findall (regex , str_func )
2836
2840
2837
2841
if params :
2838
- for i in range ( len ( params ) ):
2842
+ for i , param in enumerate ( params ):
2839
2843
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 ))
2847
2849
2848
2850
str_func = re .sub (regex , '{p}' , str_func )
2849
2851
2850
2852
try :
2851
2853
func = self ._funcs [str_func ]
2852
- except :
2854
+ except ValueError , KeyError :
2853
2855
raise ValueError ("%s: invalid string. The only strings "
2854
2856
"recognized as functions are %s." %
2855
- (str_func , self ._funcs . keys ( )))
2857
+ (str_func , list ( self ._funcs )))
2856
2858
2857
2859
# Checking that the parameters are valid
2858
2860
if not func .check_params (params ):
0 commit comments