本文整理汇总了Python中inspect.Parameter.VAR_POSITIONAL属性的典型用法代码示例。如果您正苦于以下问题:Python Parameter.VAR_POSITIONAL属性的具体用法?Python Parameter.VAR_POSITIONAL怎么用?Python Parameter.VAR_POSITIONAL使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类inspect.Parameter
的用法示例。
在下文中一共展示了Parameter.VAR_POSITIONAL属性的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: auto_assign
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def auto_assign(func):
# Signature:
sig = signature(func)
for name, param in sig.parameters.items():
if param.kind in (Parameter.VAR_POSITIONAL, Parameter.VAR_KEYWORD):
raise RuntimeError('Unable to auto assign if *args or **kwargs in signature.')
# Wrapper:
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
for i, (name, param) in enumerate(sig.parameters.items()):
# Skip 'self' param:
if i == 0: continue
# Search value in args, kwargs or defaults:
if i - 1 < len(args):
val = args[i - 1]
elif name in kwargs:
val = kwargs[name]
else:
val = param.default
setattr(self, name, val)
func(self, *args, **kwargs)
return wrapper
示例2: _get_free_params
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def _get_free_params(fun, ignore=None):
"""Get the names of the free parameters of the function ``f``.
Args:
fun (callable): The function to inspect.
ignore (list[str]): A list of argument names (as str) to ignore.
Returns:
list[str]: The name of the free parameters not listed in ``ignore``.
"""
ignore = ignore or []
free_params = []
for name, param in signature(fun).parameters.items():
if param.default == Parameter.empty and param.kind != Parameter.VAR_POSITIONAL:
if name not in ignore:
free_params.append(name)
return free_params
示例3: getargspec
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def getargspec(func):
signature = inspect.signature(func)
args = []
varargs = None
keywords = None
defaults = []
for param in signature.parameters.values(): # type: Parameter
if param.kind == Parameter.VAR_POSITIONAL:
varargs = param.name
elif param.kind in (
Parameter.POSITIONAL_ONLY,
Parameter.KEYWORD_ONLY,
Parameter.POSITIONAL_OR_KEYWORD):
args.append(param.name)
elif param.kind == Parameter.VAR_KEYWORD:
keywords = param.name
# noinspection PyProtectedMember
if param.default is not inspect._empty:
defaults.append(param.default)
return ArgSpec(args, varargs, keywords, tuple(defaults))
示例4: accept_variable_arguments
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def accept_variable_arguments(func):
for param in signature(func).parameters.values():
if param.kind in (Parameter.VAR_POSITIONAL,
Parameter.VAR_KEYWORD):
return True
return False
示例5: get_argspec
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def get_argspec(f):
sig = inspect.signature(f)
args = [n for n, p in sig.parameters.items() if p.kind in ARG_TYPES]
pos_args = [
n
for n, p in sig.parameters.items()
if p.kind in POSARG_TYPES and p.default == inspect._empty
]
varargs = [
n for n, p in sig.parameters.items() if p.kind == Parameter.VAR_POSITIONAL
]
# only use first vararg (how on earth would you have more anyways?)
vararg_name = varargs[0] if varargs else None
varkws = [n for n, p in sig.parameters.items() if p.kind == Parameter.VAR_KEYWORD]
# only use first varkw (how on earth would you have more anyways?)
kw_wildcard_name = varkws[0] if varkws else None
kwargs = OrderedDict(
[
(n, p.default)
for n, p in sig.parameters.items()
if p.default != inspect._empty
]
)
return args, vararg_name, kw_wildcard_name, pos_args, kwargs
示例6: _convert_from_parameter_kind
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def _convert_from_parameter_kind(kind):
if kind == Parameter.POSITIONAL_ONLY:
return 0
if kind == Parameter.POSITIONAL_OR_KEYWORD:
return 1
if kind == Parameter.VAR_POSITIONAL:
return 2
if kind == Parameter.KEYWORD_ONLY:
return 3
if kind == Parameter.VAR_KEYWORD:
return 4
示例7: _convert_to_parameter_kind
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def _convert_to_parameter_kind(value):
if value == 0:
return Parameter.POSITIONAL_ONLY
if value == 1:
return Parameter.POSITIONAL_OR_KEYWORD
if value == 2:
return Parameter.VAR_POSITIONAL
if value == 3:
return Parameter.KEYWORD_ONLY
if value == 4:
return Parameter.VAR_KEYWORD
示例8: _update_backend
# 需要导入模块: from inspect import Parameter [as 别名]
# 或者: from inspect.Parameter import VAR_POSITIONAL [as 别名]
def _update_backend(cls, backend):
if cls.__original_docstring__ is None:
cls.__original_docstring__ = cls.__doc__
all_keywords = set()
element_keywords = cls._element_keywords(backend)
for element, keywords in element_keywords.items():
with param.logging_level('CRITICAL'):
all_keywords |= set(keywords)
setattr(cls, element,
cls._create_builder(element, keywords))
filtered_keywords = [k for k in all_keywords if k not in cls._no_completion]
sorted_kw_set = sorted(set(filtered_keywords))
if sys.version_info.major == 2:
kws = ', '.join('{opt}=None'.format(opt=opt) for opt in sorted_kw_set)
old_doc = cls.__original_docstring__.replace(
'params(strict=Boolean, name=String)','')
cls.__doc__ = '\n opts({kws})'.format(kws=kws) + old_doc
else:
from inspect import Parameter, Signature
signature = Signature([Parameter('args', Parameter.VAR_POSITIONAL)]
+ [Parameter(kw, Parameter.KEYWORD_ONLY)
for kw in sorted_kw_set])
cls.__init__.__signature__ = signature