lingvo.core.inspect_utils module

Utilities to bind function signatures to params.

lingvo.core.inspect_utils._IsDefinableParameter(parameter)[source]

Checks if the parameter can be defined in Params.

Parameters

parameter – inspect.Parameter to be checked.

Returns

True if the parameter’s kind is either POSITIONAL_OR_KEYWORD or KEYWORD_ONLY which are definable in Params, False if it is either VAR_POSITIONAL or VAR_KEYWORD which are ignorable.

Raises

ValueError – The parameter has another kind which are possibly not supported, e.g., POSITIONAL_ONLY parameters.

lingvo.core.inspect_utils._ExtractParameters(func, ignore, bound)[source]

Extracts parameters of func which can be defined in Params.

Parameters
  • func – A callable to be analysed.

  • ignore – A collection of parameter names in func to be ignored.

  • bound – Whether the func is used as a bound function (an object method or a class method) or not. If True, the first parameter of the func will be ignored.

Returns

A generator of inspect.Parameter representing definable parameters.

lingvo.core.inspect_utils.DefineParams(func, params, ignore=None, bound=False)[source]

Defines params for each parameter of given callable.

This allows you to define the parameters necessary to call a callable without having to type the Define statements yourself. Default values for the function parameters will be copied into the params object as well.

To use this function for analysing a class instantiation, users usually can pass the class type as the func. If it does not work correctly, pass the __init__ method of the class with bound=True instead.

Parameters
  • func – A callable to be analysed. Parameters of this function will be defined in params. This function expects that func maintains the explicit signature of its parameters. Implicit parameters that are stored in *args or **kwargs could not be analysed correctly.

  • params – A Params object to be updated. New parameters will be defined here.

  • ignore – A collection of parameter names in func to be ignored from defining corresponding entries in params.

  • bound – Whether func will be used as a bound function (an object method or a class method) or not. If True, the first parameter of func (typically self or cls) will be ignored.

lingvo.core.inspect_utils._MakeArgs(func, params, **kwargs)[source]

Generates an argument list to call func.

Parameters
  • func – A callable to be called.

  • params – A Params object containing arguments for func.

  • **kwargs – Argument/value pairs that should override params.

Returns

A dict containing function parameters to be used as **kwargs of func.

lingvo.core.inspect_utils.CallWithParams(func, params, **kwargs)[source]

Call a function or method with a Params object.

Parameters
  • func – A callable to be called.

  • params – A Params object containing parameters of func.

  • **kwargs – Argument/value pairs that should override params.

Returns

The return values from func.

lingvo.core.inspect_utils.ConstructWithParams(cls, params, **kwargs)[source]

Constructs a class object with a Params object.

Parameters
  • cls – A class type to be constructed.

  • params – A Params object containing parameters of cls.__init__.

  • **kwargs – Argument/value pairs that should override params.

Returns

The constructed object.