def _np_signature(f): """An enhanced funcsigs.signature that can handle numpy.ufunc.""" if not isinstance(f, np.ufunc): return funcsigs.signature(f) def names_from_num(prefix, n): if n <= 0: return [] elif n == 1: return [prefix] else: return [prefix + str(i + 1) for i in range(n)] input_names = names_from_num('x', f.nin) output_names = names_from_num('out', f.nout) keyword_only_params = [ ('where', True), ('casting', 'same_kind'), ('order', 'K'), ('dtype', None), ('subok', True), ('signature', None), ('extobj', None)] params = [] params += [Parameter(name, Parameter.POSITIONAL_ONLY) for name in input_names] if f.nout > 1: params += [Parameter(name, Parameter.POSITIONAL_ONLY, default=None) for name in output_names] params += [Parameter('out', Parameter.POSITIONAL_OR_KEYWORD, default=None if f.nout == 1 else (None,) * f.nout)] params += [Parameter(name, Parameter.KEYWORD_ONLY, default=default) for name, default in keyword_only_params] return funcsigs.Signature(params)
def __signature__(self): if hasattr(self, '__customsig'): return self.__customsig kwargs = [] for param in self.PARAMETERS: kwargs.append(funcsigs.Parameter(param.name, default=param.default, kind=funcsigs.Parameter.POSITIONAL_OR_KEYWORD)) self.__customsig = funcsigs.Signature(kwargs, __validate_parameters__=True) return self.__customsig
def extract_signature_and_value(func_or_str, default_parameters=None): if not inspect.isfunction(func_or_str): if default_parameters is None: parameters = [] else: kind = funcsigs.Parameter.POSITIONAL_OR_KEYWORD parameters = [funcsigs.Parameter(name, kind=kind) for name in default_parameters] return funcsigs.Signature(parameters), func_or_str signature = funcsigs.signature(func_or_str) # pass mock values to extract the value args = [None] * len(signature.parameters) return signature, func_or_str(*args)
def flatten_args(signature_parameters, args, kwargs): """Validates the arguments against the signature and flattens them. The flat list representation is a serializable format for arguments. Since the flatbuffer representation of function arguments is a list, we combine both keyword arguments and positional arguments. We represent this with two entries per argument value - [DUMMY_TYPE, x] for positional arguments and [KEY, VALUE] for keyword arguments. See the below example. See `recover_args` for logic restoring the flat list back to args/kwargs. Args: signature_parameters (list): The list of RayParameter objects representing the function signature, obtained from `extract_signature`. args: The non-keyword arguments passed into the function. kwargs: The keyword arguments passed into the function. Returns: List of args and kwargs. Non-keyword arguments are prefixed by internal enum DUMMY_TYPE. Raises: TypeError: Raised if arguments do not fit in the function signature. Example: >>> flatten_args([1, 2, 3], {"a": 4}) [None, 1, None, 2, None, 3, "a", 4] """ for obj in args: if isinstance(obj, ray.ObjectID) and obj.is_direct_actor_type(): raise NotImplementedError( "Objects produced by direct actor calls cannot be " "passed to other tasks as arguments.") restored = _restore_parameters(signature_parameters) reconstructed_signature = funcsigs.Signature(parameters=restored) try: reconstructed_signature.bind(*args, **kwargs) except TypeError as exc: raise TypeError(str(exc)) list_args = [] for arg in args: list_args += [DUMMY_TYPE, arg] for keyword, arg in kwargs.items(): list_args += [keyword, arg] return list_args
def template_def(signature, code): """ Returns a ``Mako`` template with the given ``signature``. :param signature: a list of postitional argument names, or a ``Signature`` object from ``funcsigs`` module. :code: a body of the template. """ if not isinstance(signature, funcsigs.Signature): # treating ``signature`` as a list of positional arguments # HACK: Signature or Parameter constructors are not documented. kind = funcsigs.Parameter.POSITIONAL_OR_KEYWORD signature = funcsigs.Signature([funcsigs.Parameter(name, kind=kind) for name in signature]) template_src = "<%def name='_func" + str(signature) + "'>\n" + code + "\n</%def>" return template_from(template_src).get_def('_func')
def __init__(self, api, path, opspec): self.api = api self.path = path self.opspec = opspec self.method = opspec['method'] os_type = opspec.get('type') if os_type is None: self.type = None else: self.type = api.models[os_type] pinfos = opspec['parameters'] # Sort the parameters: path, body, query, header pinfos = ( [pinfo for pinfo in pinfos if pinfo['paramType'] == 'path'] + [pinfo for pinfo in pinfos if pinfo['paramType'] == 'body'] + [pinfo for pinfo in pinfos if pinfo['paramType'] == 'query'] + [pinfo for pinfo in pinfos if pinfo['paramType'] == 'header']) self.params = [RPCParam(pinfo) for pinfo in pinfos] self.signature = funcsigs.Signature( [p.func_param for p in self.params]) self.__doc__ = opspec.get('summary')