Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _ensure_4args(func):
""" Conditionally wrap function to ensure 4 input arguments
Parameters
----------
func: callable
with two, three or four positional arguments
Returns
-------
callable which possibly ignores 0, 1 or 2 positional arguments
"""
if func is None:
return None
if isinstance(func, _Blessed): # inspect on __call__ is a hassle...
return func
self_arg = 1 if inspect.ismethod(func) else 0
if hasattr(inspect, 'getfullargspec'):
args = inspect.getfullargspec(func)[0]
else: # Python 2:
args = inspect.getargspec(func)[0]
if len(args) == 4 + self_arg:
return func
if len(args) == 3 + self_arg:
return lambda x, y, p=(), backend=math: func(x, y, p)
elif len(args) == 2 + self_arg:
return lambda x, y, p=(), backend=math: func(x, y)
else:
raise ValueError("Incorrect numer of arguments")
return lambda x, y, p=(), backend=math: func(x, y, p)
elif len(args) == 2 + self_arg:
return lambda x, y, p=(), backend=math: func(x, y)
else:
raise ValueError("Incorrect numer of arguments")
def _default(arg, default):
return default if arg is None else arg
def _concat(*args):
return np.concatenate(list(map(np.atleast_1d, args)))
class _Callback(_Blessed):
def __init__(self, indep, dep, params, exprs, Lambdify=None):
self.indep, self.dep, self.params = indep, dep, params
if indep is None:
self.args = _concat(self.dep, self.params)
else:
self.args = _concat(self.indep, self.dep, self.params)
self.input_width = len(self.args)
self.exprs = exprs
self.callback = Lambdify(self.args, self.exprs)
self.ny = len(dep)
self.take_params = len(params)
def __call__(self, x, y, params=(), backend=None):
_x = np.asarray(x)
_y = np.asarray(y)