def head_from_fun(fun, bound=False, debug=False): """Generate signature function from actual function.""" # we could use inspect.Signature here, but that implementation # is very slow since it implements the argument checking # in pure-Python. Instead we use exec to create a new function # with an empty body, meaning it has the same performance as # as just calling a function. is_function = inspect.isfunction(fun) is_callable = hasattr(fun, '__call__') is_method = inspect.ismethod(fun) if not is_function and is_callable and not is_method: name, fun = fun.__class__.__name__, fun.__call__ else: name = fun.__name__ definition = FUNHEAD_TEMPLATE.format( fun_name=name, fun_args=_argsfromspec(getfullargspec(fun)), fun_value=1, ) if debug: # pragma: no cover print(definition, file=sys.stderr) namespace = {'__name__': fun.__module__} # pylint: disable=exec-used # Tasks are rarely, if ever, created at runtime - exec here is fine. exec(definition, namespace) result = namespace[name] result._source = definition if bound: return partial(result, object()) return result
def head_from_fun(fun, bound=False, debug=False): """Generate signature function from actual function.""" # we could use inspect.Signature here, but that implementation # is very slow since it implements the argument checking # in pure-Python. Instead we use exec to create a new function # with an empty body, meaning it has the same performance as # as just calling a function. if not isfunction(fun) and hasattr(fun, '__call__'): name, fun = fun.__class__.__name__, fun.__call__ else: name = fun.__name__ definition = FUNHEAD_TEMPLATE.format( fun_name=name, fun_args=_argsfromspec(getfullargspec(fun)), fun_value=1, ) if debug: # pragma: no cover print(definition, file=sys.stderr) namespace = {'__name__': fun.__module__} # pylint: disable=exec-used # Tasks are rarely, if ever, created at runtime - exec here is fine. exec(definition, namespace) result = namespace[name] result._source = definition if bound: return partial(result, object()) return result
def verify_args(self, given, _index=0): S = getfullargspec(self.run) _index = 1 if S.args and S.args[0] == 'self' else _index required = S.args[_index:-len(S.defaults) if S.defaults else None] missing = required[len(given):] if missing: raise self.UsageError('Missing required {0}: {1}'.format( text.pluralize(len(missing), 'argument'), ', '.join(missing)))
def format_args(self): wrapped = getattr(self.object, '__wrapped__') if wrapped is not None: argspec = getfullargspec(wrapped) fmt = formatargspec(*argspec) fmt = fmt.replace('\\', '\\\\') return fmt return ''
def format_args(self): wrapped = getattr(self.object, '__wrapped__', None) if wrapped is not None: argspec = getfullargspec(wrapped) fmt = formatargspec(*argspec) fmt = fmt.replace('\\', '\\\\') return fmt return ''
def format_args(self): wrapped = getattr(self.object, "__wrapped__", None) if wrapped is not None: argspec = getfullargspec(wrapped) fmt = formatargspec(*argspec) fmt = fmt.replace("\\", "\\\\") return fmt return ""
def verify_args(self, given, _index=0): S = getfullargspec(self.run) _index = 1 if S.args and S.args[0] == "self" else _index required = S.args[_index : -len(S.defaults) if S.defaults else None] missing = required[len(given) :] if missing: raise self.UsageError( "Missing required {0}: {1}".format(text.pluralize(len(missing), "argument"), ", ".join(missing)) )
def head_from_fun(fun, bound=False, debug=False): if not isfunction(fun) and hasattr(fun, '__call__'): name, fun = fun.__class__.__name__, fun.__call__ else: name = fun.__name__ definition = FUNHEAD_TEMPLATE.format( fun_name=name, fun_args=_argsfromspec(getfullargspec(fun)), fun_value=1, ) if debug: # pragma: no cover print(definition, file=sys.stderr) namespace = {'__name__': 'headof_{0}'.format(name)} exec(definition, namespace) result = namespace[name] result._source = definition if bound: return partial(result, object()) return result
def head_from_fun(fun, bound=False, debug=False): # we could use inspect.Signature here, but that implementation # is very slow since it implements the argument checking # in pure-Python. Instead we use exec to create a new function # with an empty body, meaning it has the same performance as # as just calling a function. if not isfunction(fun) and hasattr(fun, '__call__'): name, fun = fun.__class__.__name__, fun.__call__ else: name = fun.__name__ definition = FUNHEAD_TEMPLATE.format( fun_name=name, fun_args=_argsfromspec(getfullargspec(fun)), fun_value=1, ) if debug: # pragma: no cover print(definition, file=sys.stderr) namespace = {'__name__': fun.__module__} exec(definition, namespace) result = namespace[name] result._source = definition if bound: return partial(result, object()) return result
def fun_takes_argument(name, fun, position=None): spec = getfullargspec(fun) return ( spec.varkw or spec.varargs or (len(spec.args) >= position if position else name in spec.args) )
def arity_greater(fun, n): argspec = getfullargspec(fun) return argspec.varargs or len(argspec.args) > n