Python inspect.getfullargspec() Examples

The following are 30 code examples of inspect.getfullargspec(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module inspect , or try the search function .
Example #1
Source File: baseconnector.py    From resolwe with Apache License 2.0 7 votes vote down vote up
def validate_url(wrapped, instance, args, kwargs):
    """Enforces argument named "url" to be relative path.

    Check that it is instance of str or os.PathLike and that it represents
    relative path.
    """
    try:
        # Use -1 since self is not included in the args.
        url = args[getfullargspec(wrapped).args.index("url") - 1]
    except IndexError:
        url = kwargs.get("url")
    if not isinstance(url, (str, PathLike)):
        raise TypeError("Argument 'url' must be a string or path-like object")
    if PurePath(url).is_absolute():
        raise ValueError("Argument 'url' must be a relative path")
    return wrapped(*args, **kwargs) 
Example #2
Source File: _build_docs.py    From webviz-config with MIT License 6 votes vote down vote up
def _annotation_to_string(annotation: Any) -> str:
    """Takes in a type annotation (that could come from e.g. inspect.getfullargspec)
    and transforms it into a human readable string.
    """

    def remove_fix(string: str, fix: str, prefix: bool = True) -> str:
        if prefix and string.startswith(fix):
            return string[len(fix) :]
        if not prefix and string.endswith(fix):
            return string[: -len(fix)]
        return string

    text_type = str(annotation)
    text_type = remove_fix(text_type, "typing.")
    text_type = remove_fix(text_type, "<class '")
    text_type = remove_fix(text_type, "'>", prefix=False)
    text_type = text_type.replace("pathlib.Path", "str (corresponding to a path)")

    return text_type 
Example #3
Source File: interface.py    From gradio-UI with Apache License 2.0 6 votes vote down vote up
def get_config_file(self):
        config = {
            "input_interfaces": [
                (iface.__class__.__name__.lower(), iface.get_template_context())
                for iface in self.input_interfaces],
            "output_interfaces": [
                (iface.__class__.__name__.lower(), iface.get_template_context())
                for iface in self.output_interfaces],
            "function_count": len(self.predict),
            "live": self.live,
            "show_input": self.show_input,
            "show_output": self.show_output,
            "title": self.title,
            "description": self.description,
            "thumbnail": self.thumbnail
        }
        try:
            param_names = inspect.getfullargspec(self.predict[0])[0]
            for iface, param in zip(config["input_interfaces"], param_names):
                if not iface[1]["label"]:
                    iface[1]["label"] = param.replace("_", " ")
        except ValueError:
            pass
        return config 
Example #4
Source File: util.py    From lirpg with MIT License 6 votes vote down vote up
def store_args(method):
    """Stores provided method args as instance attributes.
    """
    argspec = inspect.getfullargspec(method)
    defaults = {}
    if argspec.defaults is not None:
        defaults = dict(
            zip(argspec.args[-len(argspec.defaults):], argspec.defaults))
    if argspec.kwonlydefaults is not None:
        defaults.update(argspec.kwonlydefaults)
    arg_names = argspec.args[1:]

    @functools.wraps(method)
    def wrapper(*positional_args, **keyword_args):
        self = positional_args[0]
        # Get default arg values
        args = defaults.copy()
        # Add provided arg values
        for name, value in zip(arg_names, positional_args[1:]):
            args[name] = value
        args.update(keyword_args)
        self.__dict__.update(args)
        return method(*positional_args, **keyword_args)

    return wrapper 
Example #5
Source File: util.py    From HardRLWithYoutube with MIT License 6 votes vote down vote up
def store_args(method):
    """Stores provided method args as instance attributes.
    """
    argspec = inspect.getfullargspec(method)
    defaults = {}
    if argspec.defaults is not None:
        defaults = dict(
            zip(argspec.args[-len(argspec.defaults):], argspec.defaults))
    if argspec.kwonlydefaults is not None:
        defaults.update(argspec.kwonlydefaults)
    arg_names = argspec.args[1:]

    @functools.wraps(method)
    def wrapper(*positional_args, **keyword_args):
        self = positional_args[0]
        # Get default arg values
        args = defaults.copy()
        # Add provided arg values
        for name, value in zip(arg_names, positional_args[1:]):
            args[name] = value
        args.update(keyword_args)
        self.__dict__.update(args)
        return method(*positional_args, **keyword_args)

    return wrapper 
Example #6
Source File: interface.py    From gradio-UI with Apache License 2.0 6 votes vote down vote up
def get_config_file(self):
        config = {
            "input_interfaces": [
                (iface.__class__.__name__.lower(), iface.get_template_context())
                for iface in self.input_interfaces],
            "output_interfaces": [
                (iface.__class__.__name__.lower(), iface.get_template_context())
                for iface in self.output_interfaces],
            "function_count": len(self.predict),
            "live": self.live,
            "show_input": self.show_input,
            "show_output": self.show_output,
            "title": self.title,
            "description": self.description,
            "thumbnail": self.thumbnail
        }
        try:
            param_names = inspect.getfullargspec(self.predict[0])[0]
            for iface, param in zip(config["input_interfaces"], param_names):
                if not iface[1]["label"]:
                    iface[1]["label"] = param.replace("_", " ")
        except ValueError:
            pass
        return config 
Example #7
Source File: schedule.py    From tomodachi with MIT License 6 votes vote down vote up
def schedule_handler(cls: Any, obj: Any, context: Dict, func: Any, interval: Optional[Union[str, int]] = None, timestamp: Optional[str] = None, timezone: Optional[str] = None, immediately: Optional[bool] = False) -> Any:
        async def handler() -> None:
            values = inspect.getfullargspec(func)
            kwargs = {k: values.defaults[i] for i, k in enumerate(values.args[len(values.args) - len(values.defaults):])} if values.defaults else {}
            try:
                routine = func(*(obj,), **kwargs)
                if isinstance(routine, Awaitable):
                    await routine
            except Exception as e:
                logging.getLogger('exception').exception('Uncaught exception: {}'.format(str(e)))

        context['_schedule_scheduled_functions'] = context.get('_schedule_scheduled_functions', [])
        context['_schedule_scheduled_functions'].append((interval, timestamp, timezone, immediately, func, handler))

        start_func = cls.start_scheduler(cls, obj, context)
        return (await start_func) if start_func else None 
Example #8
Source File: hammer_tool.py    From hammer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def make_step_from_method(func: Callable[[], bool], name: str = "") -> HammerToolStep:
        """
        Create a HammerToolStep from a method.

        :param func: Method for the given substep (e.g. self.elaborate)
        :param name: Name of the hook. If unspecified, defaults to func.__name__.
        :return: A HammerToolStep defining this step.
        """
        if not callable(func):
            raise TypeError("func is not callable")
        if not hasattr(func, "__self__"):
            raise ValueError("This function does not take unbound functions")
        annotations = inspect.getfullargspec(func).annotations
        if annotations != {'return': bool}:
            raise TypeError("Function {func} does not meet the required signature".format(func=str(func)))

        # Wrapper to make __func__ take a proper type annotation for "self"
        def wrapper(x: HammerTool) -> bool:
            return func.__func__(x)  # type: ignore # no type stub for builtin __func__

        if name == "":
            name = func.__name__
        return make_raw_hammer_tool_step(func=wrapper, name=name) 
Example #9
Source File: baseconnector.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def validate_urls(wrapped, instance, args, kwargs):
    """Enforces argument named "urls" to be a list of relative paths."""
    try:
        # Use -1 since self is not included in the args.
        urls = args[getfullargspec(wrapped).args.index("urls") - 1]
    except IndexError:
        urls = kwargs.get("urls")
    # Check that URLS is really a list of strings.
    if not isinstance(urls, list):
        raise TypeError("Argument urls must be a list of strings or path-like objects")
    if not all(isinstance(url, (str, PathLike)) for url in urls):
        raise TypeError("Argument urls must be a list of strings or path-like objects")
    # Check that all URLS are relative.
    if any(PurePath(url).is_absolute() for url in urls):
        raise ValueError("Paths must be relative.")
    return wrapped(*args, *kwargs) 
Example #10
Source File: _methodical.py    From automat with MIT License 6 votes vote down vote up
def _getArgSpec(func):
    """
    Normalize inspect.ArgSpec across python versions
    and convert mutable attributes to immutable types.

    :param Callable func: A function.
    :return: The function's ArgSpec.
    :rtype: ArgSpec
    """
    spec = getArgsSpec(func)
    return ArgSpec(
        args=tuple(spec.args),
        varargs=spec.varargs,
        varkw=spec.varkw if six.PY3 else spec.keywords,
        defaults=spec.defaults if spec.defaults else (),
        kwonlyargs=tuple(spec.kwonlyargs) if six.PY3 else (),
        kwonlydefaults=(
            tuple(spec.kwonlydefaults.items())
            if spec.kwonlydefaults else ()
        ) if six.PY3 else (),
        annotations=tuple(spec.annotations.items()) if six.PY3 else (),
    ) 
Example #11
Source File: apply.py    From mars with Apache License 2.0 6 votes vote down vote up
def df_apply(df, func, axis=0, raw=False, result_type=None, args=(), dtypes=None,
             output_type=None, index=None, elementwise=None, **kwds):
    if isinstance(func, (list, dict)):
        return df.aggregate(func)

    if isinstance(output_type, str):
        output_type = getattr(OutputType, output_type.lower())

    # calling member function
    if isinstance(func, str):
        func = getattr(df, func)
        sig = inspect.getfullargspec(func)
        if "axis" in sig.args:
            kwds["axis"] = axis
        return func(*args, **kwds)

    op = ApplyOperand(func=func, axis=axis, raw=raw, result_type=result_type, args=args, kwds=kwds,
                      output_type=output_type, elementwise=elementwise)
    return op(df, dtypes=dtypes, index=index) 
Example #12
Source File: safe_kwargs.py    From aioalice with MIT License 6 votes vote down vote up
def safe_kwargs(func_or_class):
    from ..types.base import AliceObject

    spec = getfullargspec(func_or_class)
    all_args = spec.args

    save_raw_kwargs = isclass(func_or_class) and issubclass(func_or_class, AliceObject)

    @functools.wraps(func_or_class)
    def wrap(*args, **kwargs):
        accepted_kwargs = {k: v for k, v in kwargs.items() if k in all_args}
        res = func_or_class(*args, **accepted_kwargs)

        if save_raw_kwargs:
            # saving all kwargs for access to unexpected attrs
            res._raw_kwargs.update(kwargs)

        return res

    return wrap 
Example #13
Source File: util.py    From rl_graph_generation with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def store_args(method):
    """Stores provided method args as instance attributes.
    """
    argspec = inspect.getfullargspec(method)
    defaults = {}
    if argspec.defaults is not None:
        defaults = dict(
            zip(argspec.args[-len(argspec.defaults):], argspec.defaults))
    if argspec.kwonlydefaults is not None:
        defaults.update(argspec.kwonlydefaults)
    arg_names = argspec.args[1:]

    @functools.wraps(method)
    def wrapper(*positional_args, **keyword_args):
        self = positional_args[0]
        # Get default arg values
        args = defaults.copy()
        # Add provided arg values
        for name, value in zip(arg_names, positional_args[1:]):
            args[name] = value
        args.update(keyword_args)
        self.__dict__.update(args)
        return method(*positional_args, **keyword_args)

    return wrapper 
Example #14
Source File: test_ImageToText.py    From python3-anticaptcha with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_customcatpcha_params(self):
        default_init_params = [
            "self",
            "anticaptcha_key",
            "sleep_time",
            "save_format",
            "language",
            "callbackUrl",
        ]
        default_handler_params = ["self", "captcha_link", "captcha_file", "captcha_base64"]
        # get customcaptcha init and captcha_handler params
        aioinit_params = inspect.getfullargspec(ImageToTextTask.aioImageToTextTask.__init__)
        aiohandler_params = inspect.getfullargspec(
            ImageToTextTask.aioImageToTextTask.captcha_handler
        )

        # get customcaptcha init and captcha_handler params
        init_params = inspect.getfullargspec(ImageToTextTask.ImageToTextTask.__init__)
        handler_params = inspect.getfullargspec(ImageToTextTask.ImageToTextTask.captcha_handler)
        # check aio module params
        assert default_init_params == aioinit_params[0]
        assert default_handler_params == aiohandler_params[0]
        # check sync module params
        assert default_init_params == init_params[0]
        assert default_handler_params == handler_params[0] 
Example #15
Source File: test_NoCaptcha.py    From python3-anticaptcha with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_nocaptcha_params(self):
        default_init_params = ["self", "anticaptcha_key", "sleep_time", "callbackUrl"]
        default_handler_params = ["self", "websiteURL", "websiteKey", 'recaptchaDataSValue']
        # get customcaptcha init and captcha_handler params
        aioinit_params = inspect.getfullargspec(NoCaptchaTask.aioNoCaptchaTask.__init__)
        aiohandler_params = inspect.getfullargspec(NoCaptchaTask.aioNoCaptchaTask.captcha_handler)

        # get customcaptcha init and captcha_handler params
        init_params = inspect.getfullargspec(NoCaptchaTask.NoCaptchaTask.__init__)
        handler_params = inspect.getfullargspec(NoCaptchaTask.NoCaptchaTask.captcha_handler)
        # check aio module params
        assert default_init_params == aioinit_params[0]
        assert default_handler_params == aiohandler_params[0]
        # check sync module params
        assert default_init_params == init_params[0]
        assert default_handler_params == handler_params[0] 
Example #16
Source File: test_CustomResultHandler.py    From python3-anticaptcha with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_result_handler_params(self):
        default_init_params = ["self", "anticaptcha_key", "sleep_time"]
        default_handler_params = ["self", "task_id"]
        # get customcaptcha init and task_handler params
        aioinit_params = inspect.getfullargspec(
            CustomResultHandler.aioCustomResultHandler.__init__
        )
        aiohandler_params = inspect.getfullargspec(
            CustomResultHandler.aioCustomResultHandler.task_handler
        )

        # get customcaptcha init and task_handler params
        init_params = inspect.getfullargspec(CustomResultHandler.CustomResultHandler.__init__)
        handler_params = inspect.getfullargspec(
            CustomResultHandler.CustomResultHandler.task_handler
        )
        # check aio module params
        assert default_init_params == aioinit_params[0]
        assert default_handler_params == aiohandler_params[0]
        # check sync module params
        assert default_init_params == init_params[0]
        assert default_handler_params == handler_params[0] 
Example #17
Source File: functions.py    From chainer-compiler with MIT License 6 votes vote down vote up
def analyze_args(self, func, module_function=False):
        sig = inspect.signature(func)
        argspec = inspect.getfullargspec(func)

        parameter_count = 0
        for k, v in sig.parameters.items():
            # TODO improve it
            if k == 'kwargs':
                continue
            parameter_count += 1

        isSelfRemoved = not module_function and parameter_count != len(argspec.args) + len(argspec.kwonlyargs)

        if isSelfRemoved:
            self.add_arg(argspec.args[0], None)

        for k, v in sig.parameters.items():
            # TODO improve it
            if k == 'kwargs':
                continue

            self.add_arg(v.name, values.parse_instance(None, v.name, v.default)) 
Example #18
Source File: lib.py    From TestSlide with MIT License 6 votes vote down vote up
def _validate_return_type(template, value, caller_frame_info):
    try:
        argspec = inspect.getfullargspec(template)
    except TypeError:
        return
    expected_type = argspec.annotations.get("return")
    if expected_type:
        try:
            _validate_argument_type(expected_type, "return", value)
        except TypeError as type_error:
            raise TypeError(
                f"{str(type_error)}: {repr(value)}\n"
                f"Defined at {caller_frame_info.filename}:"
                f"{caller_frame_info.lineno}"
            )


##
## Private attributes
## 
Example #19
Source File: core.py    From mars with Apache License 2.0 6 votes vote down vote up
def execute_agg(cls, ctx, op):
        (input_chunk,), device_id, xp = as_same_device(
            [ctx[c.key] for c in op.inputs], device=op.device, ret_extra=True)
        axis = cls.get_axis(op.axis)
        func_name = getattr(cls, '_func_name', None)
        reduce_func = getattr(xp, func_name)
        out = op.outputs[0]
        with device(device_id):
            if "dtype" in inspect.getfullargspec(reduce_func).args:
                ret = reduce_func(input_chunk, axis=axis,
                                  dtype=op.dtype,
                                  keepdims=bool(op.keepdims))
            else:
                ret = reduce_func(input_chunk, axis=axis,
                                  keepdims=bool(op.keepdims))

            ctx[out.key] = ret.astype(op.dtype, order=out.order.value, copy=False) 
Example #20
Source File: duct.py    From omniduct with MIT License 6 votes vote down vote up
def __init_with_kwargs__(cls, self, kwargs, **fallbacks):
        if not hasattr(self, '_Duct__inited_using_kwargs'):
            self._Duct__inited_using_kwargs = {}
        for cls_parent in reversed([
                parent for parent in inspect.getmro(cls)
                if issubclass(parent, Duct)
                and parent not in self._Duct__inited_using_kwargs
                and '__init__' in parent.__dict__
        ]):
            self._Duct__inited_using_kwargs[cls_parent] = True
            if six.PY3:
                argspec = inspect.getfullargspec(cls_parent.__init__)
                keys = argspec.args[1:] + argspec.kwonlyargs
            else:
                keys = inspect.getargspec(cls_parent.__init__).args[1:]
            params = {}
            for key in keys:
                if key in kwargs:
                    params[key] = kwargs.pop(key)
                elif key in fallbacks:
                    params[key] = fallbacks[key]
            cls_parent.__init__(self, **params) 
Example #21
Source File: utils.py    From mars with Apache License 2.0 5 votes vote down vote up
def log_unhandled(func):
    mod_logger = _get_mod_logger()
    if not mod_logger:
        return func

    func_name = getattr(func, '__qualname__', func.__module__ + func.__name__)
    func_args = inspect.getfullargspec(func)

    @functools.wraps(func)
    def _wrapped(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except:  # noqa: E722
            kwcopy = kwargs.copy()
            kwcopy.update(zip(func_args.args, args))
            if getattr(func, '__closure__', None) is not None:
                kwargs.update(zip(
                    func.__code__.co_freevars + getattr(func.__code__, 'co_cellvars', ()),
                    [getattr(c, 'cell_contents', None) for c in func.__closure__],
                ))

            messages = []
            for k, v in kwcopy.items():
                if 'key' in k:
                    messages.append('%s=%r' % (k, v))

            err_msg = 'Unexpected exception occurred in %s.' % func_name
            if messages:
                err_msg += ' ' + ' '.join(messages)
            mod_logger.exception(err_msg)
            raise
    return _wrapped 
Example #22
Source File: cacher_tests.py    From paramz with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_signature(self):
        try:
            from inspect import getfullargspec, getdoc
            self.assertEqual(getfullargspec(self.cached.__call__), getfullargspec(self.O.__call__))
            self.assertEqual(getdoc(self.cached.__call__), getdoc(self.O.__call__))
        except ImportError:
            try:
                from inspect import signature, getdoc
                print(signature(self.cached.__call__), signature(self.O.__call__))
                self.assertEqual(signature(self.cached.__call__), signature(self.O.__call__))
                self.assertEqual(getdoc(self.cached.__call__), getdoc(self.O.__call__))
            except ImportError:
                from inspect import getargspec, getdoc
                self.assertEqual(getargspec(self.cached.__call__), getargspec(self.O.__call__))
                self.assertEqual(getdoc(self.cached.__call__), getdoc(self.O.__call__)) 
Example #23
Source File: base.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def _default_repr(self):
    """
    Produce something like:
    "imgaug.MyAugmentor(field1={self.field1}, field2={self.field2})"

    It assumes that the instance `self` contains attributes that match its constructor.
    """
    classname = type(self).__name__
    argspec = inspect.getfullargspec(self.__init__)
    assert argspec.varargs is None, "The default __repr__ in {} doesn't work for varargs!".format(classname)
    assert argspec.varkw is None, "The default __repr__ in {} doesn't work for kwargs!".format(classname)
    defaults = {}

    fields = argspec.args[1:]
    defaults_pos = argspec.defaults
    if defaults_pos is not None:
        for f, d in zip(fields[::-1], defaults_pos[::-1]):
            defaults[f] = d

    for k in argspec.kwonlyargs:
        fields.append(k)
        if k in argspec.kwonlydefaults:
            defaults[k] = argspec.kwonlydefaults[k]

    argstr = []
    for f in fields:
        assert hasattr(self, f), \
            "Attribute {} in {} not found! Default __repr__ only works if " \
            "the instance has attributes that match the constructor.".format(f, classname)
        attr = getattr(self, f)
        if f in defaults and attr is defaults[f]:
            continue
        argstr.append("{}={}".format(f, pprint.pformat(attr)))
    return "imgaug.{}({})".format(classname, ', '.join(argstr)) 
Example #24
Source File: base.py    From pybids with MIT License 5 votes vote down vote up
def _setup(self, collection, variables, *args, **kwargs):
        """Replaces __init__ to set instance attributes because on Python
        >= 3.3, we can't override both new and init. """
        self.collection = collection
        self.variables = listify(variables)
        self.groupby = kwargs.pop('groupby', None)
        self.output = listify(kwargs.pop('output', None))
        self.output_prefix = kwargs.pop('output_prefix', None)
        self.output_suffix = kwargs.pop('output_suffix', None)
        self.dense = kwargs.pop('dense', False)

        # Convert any args to named keyword arguments in order to make sure
        # that operations like densification, alignment, etc. correctly detect
        # all named arguments.
        if args:
            arg_spec = inspect.getfullargspec(self._transform)
            for i, arg_val in enumerate(args):
                # Skip first two argnames--they're always 'self' and
                # 'variables'
                kwargs[arg_spec.args[2 + i]] = arg_val

        self.kwargs = kwargs

        # Expand any detected variable group names or wild cards
        self._expand_variable_groups()
        self._expand_variable_names() 
Example #25
Source File: pruning_wrapper.py    From model-optimization with Apache License 2.0 5 votes vote down vote up
def call(self, inputs, training=None):
    if training is None:
      training = K.learning_phase()

    def add_update():
      with tf.control_dependencies([
          tf.debugging.assert_greater_equal(
              self.pruning_step,
              np.int64(0),
              message=self._PRUNE_CALLBACK_ERROR_MSG)
      ]):
        with tf.control_dependencies(
            [self.pruning_obj.conditional_mask_update()]):
          return tf.no_op('update')

    def no_op():
      return tf.no_op('no_update')

    update_op = tf_utils.smart_cond(training, add_update, no_op)
    self.add_update(update_op)
    # Always execute the op that performs weights = weights * mask
    # Relies on UpdatePruningStep callback to ensure the weights
    # are sparse after the final backpropagation.
    #
    # self.add_update does nothing during eager execution.
    self.add_update(self.pruning_obj.weight_mask_op())
    # TODO(evcu) remove this check after dropping py2 support. In py3 getargspec
    # is deprecated.
    if hasattr(inspect, 'getfullargspec'):
      args = inspect.getfullargspec(self.layer.call).args
    else:
      args = inspect.getargspec(self.layer.call).args
    # Propagate the training bool to the underlying layer if it accepts
    # training as an arg.
    if 'training' in args:
      return self.layer.call(inputs, training=training)

    return self.layer.call(inputs) 
Example #26
Source File: hooks.py    From python-netsurv with MIT License 5 votes vote down vote up
def _getargspec(func):
        return inspect.getfullargspec(func) 
Example #27
Source File: hooks.py    From python-netsurv with MIT License 5 votes vote down vote up
def _getargspec(func):
        return inspect.getfullargspec(func) 
Example #28
Source File: decorator.py    From lambda-packs with MIT License 5 votes vote down vote up
def getargspec(f):
    """A replacement for inspect.getargspec"""
    spec = getfullargspec(f)
    return ArgSpec(spec.args, spec.varargs, spec.varkw, spec.defaults) 
Example #29
Source File: promise.py    From mars with Apache License 2.0 5 votes vote down vote up
def reject_on_exception(func):
    """
    Decorator on actor callback functions that handles exceptions by
    sending it to caller as promise rejections. The function should have
    an argument called ``callback``.
    """
    arg_names = inspect.getfullargspec(func).args
    callback_pos = None
    if arg_names:
        for idx, name in enumerate(arg_names):
            if name == 'callback':
                callback_pos = idx
                break

    @functools.wraps(func)
    def _wrapped(*args, **kwargs):
        callback = None
        if 'callback' in kwargs:
            callback = kwargs['callback']
        elif callback_pos and callback_pos < len(args):
            callback = args[callback_pos]

        try:
            return func(*args, **kwargs)
        except:  # noqa: E722
            actor = args[0]
            logger.exception('Unhandled exception in promise call')
            if callback:
                actor.tell_promise(callback, *sys.exc_info(), _accept=False)
            else:
                raise
    return _wrapped 
Example #30
Source File: ingredient.py    From sacred with MIT License 5 votes vote down vote up
def config_hook(self, func):
        """
        Decorator to add a config hook to this ingredient.

        Config hooks need to be a function that takes 3 parameters and returns
        a dictionary:
        (config, command_name, logger) --> dict

        Config hooks are run after the configuration of this Ingredient, but
        before any further ingredient-configurations are run.
        The dictionary returned by a config hook is used to update the
        config updates.
        Note that they are not restricted to the local namespace of the
        ingredient.
        """
        argspec = inspect.getfullargspec(func)
        args = ["config", "command_name", "logger"]
        if not (
            argspec.args == args
            and argspec.varargs is None
            and not argspec.kwonlyargs
            and argspec.defaults is None
        ):
            raise ValueError(
                "Wrong signature for config_hook. Expected: "
                "(config, command_name, logger)"
            )
        self.config_hooks.append(func)
        return self.config_hooks[-1]

    # =========================== Public Interface ============================