本文整理汇总了Python中funcsigs.signature方法的典型用法代码示例。如果您正苦于以下问题:Python funcsigs.signature方法的具体用法?Python funcsigs.signature怎么用?Python funcsigs.signature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类funcsigs
的用法示例。
在下文中一共展示了funcsigs.signature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_args
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def get_args(fn):
"""Gets the arguments of a function.
Args:
fn (callable): The function to inspect.
Returns:
list: A list of argument names (str) of the function.
"""
argspec = _inspect_getargspec(fn)
args = argspec.args
# Empty args can be because `fn` is decorated. Use `funcsigs.signature`
# to re-do the inspect
if len(args) == 0:
args = funcsigs.signature(fn).parameters.keys()
args = list(args)
return args
示例2: _call_matcher
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def _call_matcher(self, _call):
"""
Given a call (or simply a (args, kwargs) tuple), return a
comparison key suitable for matching with other calls.
This is a best effort method which relies on the spec's signature,
if available, or falls back on the arguments themselves.
"""
sig = self._spec_signature
if sig is not None:
if len(_call) == 2:
name = ''
args, kwargs = _call
else:
name, args, kwargs = _call
try:
return name, sig.bind(*args, **kwargs)
except TypeError as e:
e.__traceback__ = None
return e
else:
return _call
示例3: get_signature
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def get_signature(obj, method_name):
method = getattr(obj, method_name)
# Eat self for unbound methods bc signature doesn't do it
if PY3:
if (
inspect.isclass(obj)
and not inspect.ismethod(method)
and not isinstance(obj.__dict__.get(method_name), staticmethod)
):
method = functools.partial(method, None)
else:
if (
isinstance(method, types.UnboundMethodType)
and method.__self__ is None
):
method = functools.partial(method, None)
try:
return signature(method)
except Exception:
return None
示例4: np_doc
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def np_doc(np_fun):
"""Attachs numpy docstring to a function.
Args:
np_fun: the numpy function whose docstring will be used.
Returns:
A function decorator that attaches the docstring from `np_fun` to the
decorated function.
"""
np_sig = _np_signature(np_fun)
def decorator(f):
"""The decorator."""
sig = funcsigs.signature(f)
unsupported_params = []
for name in np_sig.parameters:
if name not in sig.parameters:
unsupported_params.append(name)
f.__doc__ = _np_doc_helper(f, np_fun, unsupported_params)
return f
return decorator
示例5: np_doc_only
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def np_doc_only(np_f):
"""Attachs numpy docstring to a function.
This differs from np_doc in that it doesn't check for a match in signature.
Args:
np_f: the numpy function whose docstring will be used.
Returns:
A function decorator that attaches the docstring from `np_f` to the
decorated function.
"""
def decorator(f):
f.__doc__ = _np_doc_helper(f, np_f)
return f
return decorator
示例6: check_signature_supported
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def check_signature_supported(has_kwargs_param, has_vararg_param, keyword_defaults, name):
"""Check if we support the signature of this function.
We currently do not allow remote functions to have **kwargs. We also do not
support keyword argumens in conjunction with a *args argument.
Args:
has_kwards_param (bool): True if the function being checked has a **kwargs
argument.
has_vararg_param (bool): True if the function being checked has a *args
argument.
keyword_defaults (List): A list of the default values for the arguments to
the function being checked.
name (str): The name of the function to check.
Raises:
Exception: An exception is raised if the signature is not supported.
"""
# check if the user specified kwargs
if has_kwargs_param:
raise "Function {} has a **kwargs argument, which is currently not supported.".format(name)
# check if the user specified a variable number of arguments and any keyword arguments
if has_vararg_param and any([d != funcsigs._empty for _, d in keyword_defaults]):
raise "Function {} has a *args argument as well as a keyword argument, which is currently not supported.".format(name)
示例7: from_dict
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def from_dict(cls, dict):
if dict is None:
return cls()
params = inspect.signature(cls.__init__).parameters
if any(p.kind == inspect.Parameter.VAR_KEYWORD for p in
params.values()):
return cls(**dict)
param_names = set()
for i, (name, param) in enumerate(iteritems(params)):
if i == 0 and name == "self":
continue
if param.kind in KEYWORD_KINDS:
param_names.add(name)
filtered_dict = {k: v for k, v in iteritems(dict) if k in param_names}
return cls(**filtered_dict)
示例8: _set_signature
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def _set_signature(mock, original, instance=False):
# creates a function with signature (*args, **kwargs) that delegates to a
# mock. It still does signature checking by calling a lambda with the same
# signature as the original.
skipfirst = isinstance(original, ClassTypes)
result = _get_signature_object(original, instance, skipfirst)
if result is None:
return mock
func, sig = result
def checksig(*args, **kwargs):
sig.bind(*args, **kwargs)
_copy_func_details(func, checksig)
name = original.__name__
if not _isidentifier(name):
name = 'funcopy'
context = {'_checksig_': checksig, 'mock': mock}
src = """def %s(*args, **kwargs):
_checksig_(*args, **kwargs)
return mock(*args, **kwargs)""" % name
six.exec_(src, context)
funcopy = context[name]
_setup_func(funcopy, mock, sig)
return funcopy
示例9: _call_matcher
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def _call_matcher(self, _call):
"""
Given a call (or simply an (args, kwargs) tuple), return a
comparison key suitable for matching with other calls.
This is a best effort method which relies on the spec's signature,
if available, or falls back on the arguments themselves.
"""
sig = self._spec_signature
if sig is not None:
if len(_call) == 2:
name = ''
args, kwargs = _call
else:
name, args, kwargs = _call
try:
return name, sig.bind(*args, **kwargs)
except TypeError as e:
e.__traceback__ = None
return e
else:
return _call
示例10: get_args
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def get_args(fn: Callable) -> List[str]:
r"""Gets the arguments of a function.
Args:
fn (callable): The function to inspect.
Returns:
list: A list of argument names (``str``) of the function.
"""
argspec = inspect.getfullargspec(fn)
args = argspec.args
# Empty args can be because `fn` is decorated. Use `funcsigs.signature`
# to re-do the inspect
if len(args) == 0:
args = funcsigs.signature(fn).parameters.keys()
args = list(args)
return args
示例11: test_keywordonly_signature
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def test_keywordonly_signature():
"""
Test the keywordonly decorators ability to update the signature of the
function it wraps.
"""
kinds = {
'a': inspect_sig.Parameter.POSITIONAL_OR_KEYWORD,
'b': inspect_sig.Parameter.POSITIONAL_OR_KEYWORD,
'args': inspect_sig.Parameter.VAR_POSITIONAL,
'kwargs': inspect_sig.Parameter.VAR_KEYWORD,
'c': inspect_sig.Parameter.KEYWORD_ONLY,
'd': inspect_sig.Parameter.KEYWORD_ONLY,
}
sig_f = inspect_sig.signature(f)
for param in sig_f.parameters.values():
assert param.kind == kinds[param.name]
示例12: test_keywordonly_class
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def test_keywordonly_class():
"""
Decorating a function with no **kwargs-like argument should not be
allowed.
"""
kinds = {
'self': inspect_sig.Parameter.POSITIONAL_OR_KEYWORD,
'a': inspect_sig.Parameter.POSITIONAL_OR_KEYWORD,
'b': inspect_sig.Parameter.POSITIONAL_OR_KEYWORD,
'args': inspect_sig.Parameter.VAR_POSITIONAL,
'kwargs': inspect_sig.Parameter.VAR_KEYWORD,
'c': inspect_sig.Parameter.KEYWORD_ONLY,
'd': inspect_sig.Parameter.KEYWORD_ONLY,
}
sig = inspect_sig.signature(A.__init__)
for param in sig.parameters.values():
assert param.kind == kinds[param.name]
示例13: test_2D_fitting
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def test_2D_fitting():
"""
Makes sure that a scalar model with 2 independent variables has the
proper signature, and that the fit result is of the correct type.
"""
xdata = np.random.randint(-10, 11, size=(2, 400))
zdata = 2.5*xdata[0]**2 + 7.0*xdata[1]**2
a = Parameter('a')
b = Parameter('b')
x = Variable('x')
y = Variable('y')
new = a*x**2 + b*y**2
fit = Fit(new, xdata[0], xdata[1], zdata)
result = fit.model(xdata[0], xdata[1], 2, 3)
assert isinstance(result, tuple)
for arg_name, name in zip(('x', 'y', 'a', 'b'), inspect_sig.signature(fit.model).parameters):
assert arg_name == name
fit_result = fit.execute()
assert isinstance(fit_result, FitResults)
示例14: signature
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def signature(obj):
return ' [pip install funcsigs to show the signature]'
示例15: _check_signature
# 需要导入模块: import funcsigs [as 别名]
# 或者: from funcsigs import signature [as 别名]
def _check_signature(function, expected_sig):
actual_sig = signature(function)
print("expected: %s\nactual: %s\n" % (expected_sig, str(actual_sig)))
for (e, ev), (a, av) in zip(expected_sig, actual_sig.parameters.items()):
assert e == a and ev == av.default