本文整理匯總了Python中inspect.Parameter.empty方法的典型用法代碼示例。如果您正苦於以下問題:Python Parameter.empty方法的具體用法?Python Parameter.empty怎麽用?Python Parameter.empty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類inspect.Parameter
的用法示例。
在下文中一共展示了Parameter.empty方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _parameter_count
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def _parameter_count(funcsig: signature) -> int:
"""Get the number of positional-or-keyword or position-only parameters in a
function signature.
Parameters
----------
funcsig : inspect.Signature
A UDF signature
Returns
-------
int
The number of parameters
"""
return sum(
param.kind in {param.POSITIONAL_OR_KEYWORD, param.POSITIONAL_ONLY}
for param in funcsig.parameters.values()
if param.default is Parameter.empty
)
示例2: _get_free_params
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [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: _yield_abbreviations_for_parameter
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def _yield_abbreviations_for_parameter(param, kwargs):
"""Get an abbreviation for a function parameter."""
name = param.name
kind = param.kind
ann = param.annotation
default = param.default
not_found = (name, empty, empty)
if kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY):
if name in kwargs:
value = kwargs.pop(name)
elif ann is not empty:
warn("Using function annotations to implicitly specify interactive controls is deprecated. Use an explicit keyword argument for the parameter instead.", DeprecationWarning)
value = ann
elif default is not empty:
value = default
else:
yield not_found
yield (name, value, default)
elif kind == Parameter.VAR_KEYWORD:
# In this case name=kwargs and we yield the items in kwargs with their keys.
for k, v in kwargs.copy().items():
kwargs.pop(k)
yield k, v, empty
示例4: find_abbreviations
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def find_abbreviations(self, kwargs):
"""Find the abbreviations for the given function and kwargs.
Return (name, abbrev, default) tuples.
"""
new_kwargs = []
try:
sig = self.signature()
except (ValueError, TypeError):
# can't inspect, no info from function; only use kwargs
return [ (key, value, value) for key, value in kwargs.items() ]
for param in sig.parameters.values():
for name, value, default in _yield_abbreviations_for_parameter(param, kwargs):
if value is empty:
raise ValueError('cannot find widget or abbreviation for argument: {!r}'.format(name))
new_kwargs.append((name, value, default))
return new_kwargs
# Abbreviations to widgets
示例5: _from_dict
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def _from_dict(cls, values: dict) -> 'Base':
args = [k for k, v in signature(cls.__init__).parameters.items() if k not in ('kwargs', '_kwargs')
and v.default == Parameter.empty][1:]
required = {}
for arg in args:
prop_name = arg[:-1] if arg.endswith('_') and not keyword.iskeyword(arg) else arg
prop_type = cls.prop_type(prop_name)
value = values.pop(arg, None)
if prop_type:
if issubclass(prop_type, Base) and isinstance(value, dict):
value = prop_type.from_dict(value)
elif issubclass(prop_type, (list, tuple)) and isinstance(value, (list, tuple)):
item_type = cls.prop_item_type(prop_name)
if issubclass(item_type, Base):
value = tuple(v if isinstance(v, (Base, EnumBase)) else item_type.from_dict(v) for v in value)
elif issubclass(prop_type, EnumBase):
value = get_enum_value(prop_type, value)
required[arg] = value
instance = cls(**required)
instance.__from_dict(values)
return instance
示例6: find_abbreviations
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def find_abbreviations(self, kwargs):
"""Find the abbreviations for the given function and kwargs.
Return (name, abbrev, default) tuples.
"""
new_kwargs = []
try:
sig = self.signature()
except (ValueError, TypeError):
# can't inspect, no info from function; only use kwargs
return [ (key, value, value) for key, value in kwargs.items() ]
for parameter in sig.parameters.values():
for name, value, default in _yield_abbreviations_for_parameter(parameter, kwargs):
if value is empty:
raise ValueError('cannot find widget or abbreviation for argument: {!r}'.format(name))
new_kwargs.append((name, value, default))
return new_kwargs
示例7: _get_param_annotation
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def _get_param_annotation(p):
# if not annotated, infer type from default
if p.annotation == Parameter.empty and p.default == Parameter.empty:
raise ValueError(
f"Param {p}: both annotation and default are empty, "
"so cannot infer any useful annotation."
)
if p.annotation != Parameter.empty:
return p.annotation
# case on default types
if p.default is None:
raise ValueError(
f"Param {p}: default is None and annotation is empty, "
"cannot infer useful annotation"
)
if isinstance(p.default, tuple):
raise ValueError(f"Param {p}: default is tuple, cannot infer type")
if isinstance(p.default, dict):
raise ValueError(f"Param{p}: default is tuple, cannot infer type")
return type(p.default)
示例8: get_default_arg_names
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def get_default_arg_names(function):
# Note: this code intentionally mirrors the code at the beginning of getfuncargnames,
# to get the arguments which were excluded from its result because they had default values
return tuple(
p.name
for p in signature(function).parameters.values()
if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY)
and p.default is not Parameter.empty
)
示例9: get_default_arg_names
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def get_default_arg_names(function: Callable[..., Any]) -> Tuple[str, ...]:
# Note: this code intentionally mirrors the code at the beginning of getfuncargnames,
# to get the arguments which were excluded from its result because they had default values
return tuple(
p.name
for p in signature(function).parameters.values()
if p.kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY)
and p.default is not Parameter.empty
)
示例10: widget_from_abbrev
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def widget_from_abbrev(cls, abbrev, default=empty):
"""Build a ValueWidget instance given an abbreviation or Widget."""
if isinstance(abbrev, ValueWidget) or isinstance(abbrev, fixed):
return abbrev
if isinstance(abbrev, tuple):
widget = cls.widget_from_tuple(abbrev)
if default is not empty:
try:
widget.value = default
except Exception:
# ignore failure to set default
pass
return widget
# Try single value
widget = cls.widget_from_single_value(abbrev)
if widget is not None:
return widget
# Something iterable (list, dict, generator, ...). Note that str and
# tuple should be handled before, that is why we check this case last.
if isinstance(abbrev, Iterable):
widget = cls.widget_from_iterable(abbrev)
if default is not empty:
try:
widget.value = default
except Exception:
# ignore failure to set default
pass
return widget
# No idea...
return None
示例11: __init__
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def __init__(self, name, default=Parameter.empty, annotation=Parameter.empty):
super().__init__(name, Parameter.POSITIONAL_OR_KEYWORD, default=default, annotation=annotation)
示例12: default_instance
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def default_instance(cls):
"""
Construct a default instance of this type
"""
args = [k for k, v in signature(cls.__init__).parameters.items() if v.default == Parameter.empty][1:]
required = {a: None for a in args}
return cls(**required)
示例13: _yield_abbreviations_for_parameter
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def _yield_abbreviations_for_parameter(parameter, kwargs):
"""Get an abbreviation for a function parameter."""
name = parameter.name
kind = parameter.kind
ann = parameter.annotation
default = parameter.default
not_found = (name, empty, empty)
if kind in (Parameter.POSITIONAL_OR_KEYWORD, Parameter.KEYWORD_ONLY):
if name in kwargs:
value = kwargs.pop(name)
elif ann is not empty:
param.main.warning("Using function annotations to implicitly specify interactive controls is deprecated. "
"Use an explicit keyword argument for the parameter instead.", DeprecationWarning)
value = ann
elif default is not empty:
value = default
if isinstance(value, (Iterable, Mapping)):
value = fixed(value)
else:
yield not_found
yield (name, value, default)
elif kind == Parameter.VAR_KEYWORD:
# In this case name=kwargs and we yield the items in kwargs with their keys.
for k, v in kwargs.copy().items():
kwargs.pop(k)
yield k, v, empty
示例14: widget_from_abbrev
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def widget_from_abbrev(cls, abbrev, name, default=empty):
"""Build a ValueWidget instance given an abbreviation or Widget."""
if isinstance(abbrev, Widget):
return abbrev
if isinstance(abbrev, tuple):
widget = cls.widget_from_tuple(abbrev, name, default)
if default is not empty:
try:
widget.value = default
except Exception:
# ignore failure to set default
pass
return widget
# Try single value
widget = cls.widget_from_single_value(abbrev, name)
if widget is not None:
return widget
# Something iterable (list, dict, generator, ...). Note that str and
# tuple should be handled before, that is why we check this case last.
if isinstance(abbrev, Iterable):
widget = cls.widget_from_iterable(abbrev, name)
if default is not empty:
try:
widget.value = default
except Exception:
# ignore failure to set default
pass
return widget
# No idea...
return fixed(abbrev)
示例15: widget_from_tuple
# 需要導入模塊: from inspect import Parameter [as 別名]
# 或者: from inspect.Parameter import empty [as 別名]
def widget_from_tuple(o, name, default=empty):
"""Make widgets from a tuple abbreviation."""
int_default = (default is empty or isinstance(default, int))
if _matches(o, (Real, Real)):
min, max, value = _get_min_max_value(o[0], o[1])
if all(isinstance(_, Integral) for _ in o) and int_default:
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, name=name)
elif _matches(o, (Real, Real, Real)):
step = o[2]
if step <= 0:
raise ValueError("step must be >= 0, not %r" % step)
min, max, value = _get_min_max_value(o[0], o[1], step=step)
if all(isinstance(_, Integral) for _ in o) and int_default:
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, step=step, name=name)
elif _matches(o, (Real, Real, Real, Real)):
step = o[2]
if step <= 0:
raise ValueError("step must be >= 0, not %r" % step)
min, max, value = _get_min_max_value(o[0], o[1], value=o[3], step=step)
if all(isinstance(_, Integral) for _ in o):
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, step=step, name=name)
elif len(o) == 4:
min, max, value = _get_min_max_value(o[0], o[1], value=o[3])
if all(isinstance(_, Integral) for _ in [o[0], o[1], o[3]]):
cls = IntSlider
else:
cls = FloatSlider
return cls(value=value, start=min, end=max, name=name)