Exemplo n.º 1
0
def ismethod(func):
    try:
        assert _getargspec(func).args[0] == 'self'
        return True
    except:
        pass
    return False
Exemplo n.º 2
0
def getargspec(func):
    try:
        return func.argspec
    except AttributeError:
        try:
            return _getargspec(func)
        except TypeError:
            raise TypeError("Sorry, this really is not a function, is it?")
Exemplo n.º 3
0
 def get_arg_spec(f):
     arg_spec = _getargspec(f)
     args = arg_spec.args
     if arg_spec.varargs is not None:
         args.append(arg_spec.varargs)
     if arg_spec.keywords is not None:
         args.append(arg_spec.keywords)
     return Signature(args, {}, None)
Exemplo n.º 4
0
    def get_args(func):
        """Get a list of argument names for a function.

        :param func: The function to inspect.

        :return: A list of argument names.
        :rtype: list
        """
        return _getargspec(func).args
Exemplo n.º 5
0
    def getargspec(function):
        # print 'hasattr im_func', hasattr(function, 'im_func')
        if hasattr(function, 'im_func'):
            # print('this is a special function : %s' % function)
            # For methods or classmethods drop the first
            # argument from the returned list because
            # python supplies that automatically for us.
            # Note that this differs from what
            # inspect.getargspec() returns for methods.
            # NB: We use im_func so we work with
            #     instancemethod objects also.
            x = _getargspec(function.im_func)
            new_args = x.args[1:]
            spec = ArgSpec(args=new_args, varargs=x.varargs,
                           keywords=x.keywords, defaults=x.defaults)
            return spec

        # print 'calling normal %s' % function
        return _getargspec(function)
Exemplo n.º 6
0
def program_modules_with_function(typ, function_template):
    """ list the programs implementing a given function

    :param typ: which type of module it is (writer or reader)
    :type typ: str
    :param function_template: a function with the desired signature
    :type function_template: function
    """
    progs = []
    for prog in pclass.values(par.Program):
        module = import_program_module(prog, typ)
        if hasattr(module, function_template.__name__):
            function = getattr(module, function_template.__name__)

            # make sure the signature matches the template
            assert _getargspec(function) == _getargspec(function_template)

            progs.append(prog)

    return tuple(sorted(progs))
Exemplo n.º 7
0
 def __repr__(self):
     try:
         arg = _getargspec(self.__init__)
     except TypeError:
         argnames = ()
     else:
         argnames = arg.args[1:]
         if arg.varargs is not None:
             argnames.append(arg.varargs)
     return "%s(%s)" % (
         type(self).__name__,
         ", ".join("%s=%r" % (key, getattr(self, key)) for key in argnames if hasattr(self, key)),
     )
Exemplo n.º 8
0
 def __repr__(self):
     try:
         arg = _getargspec(self.__init__)
     except TypeError:
         argnames = ()
     else:
         argnames = arg.args[1:]
         if arg.varargs is not None:
             argnames.append(arg.varargs)
     return "%s(%s)" % (
         type(self).__name__,
         ", ".join("%s=%r" % (key, getattr(self, key))
                   for key in argnames if hasattr(self, key)))
Exemplo n.º 9
0
    def __init__(self, func, returntype=_webtypes.null, argtypes=None):

        if not _getargspec(func).args:
            raise TypeError(
                "{} func must take at least one argument (the"
                " 'self' reference to its resource)".format(self.__class__.__name__)
            )

        self._argtypes = argtypes.copy() if argtypes else {}
        self._func = func
        self._resource = None
        self._resource_class = None
        self._response_content_funcs_own = {}
        self._returntype = returntype
Exemplo n.º 10
0
    def outer(func):
        # we use lifetime_func here so we don't shadow the global
        # and get a 'referenced before assignment' error
        if isinstance(lifetime, (int, float)):
            lifetime_func = static_timeout(lifetime)
        else:
            lifetime_func = lifetime
        
        argspec = _getargspec(func)

        cacheable_keys = keys
        if not keys:
            cacheable_keys = argspec.args
        
        @_wraps(func)
        def inner(*args, **kwargs):
            # as zip terminates as soon as one iterator is exausted we dont
            # have to worry about the case where some args are specified and
            # some use thier default value as the len(args) (ie the specified 
            # args) will cause automatic termination
            args_mapping = zip(argspec.args, args)
            
            cache_keys = []
            for key, item in _chain(args_mapping, kwargs.items()):
                if key in cacheable_keys:
                    cache_keys.append((key, item))
            # lists are unhashable and cant be used as dict keys
            cache_keys.sort()
            cache_keys = tuple(val for key, val in cache_keys)
            
            recalculate = False
            cached = backend.get(cache_keys)
            if cached:
                expiry, output = cached
                expire = lifetime_func(expiry)
                if expire:
                    recalculate = True
            else:
                recalculate = True
                    
            if recalculate:
                output = func(*args, **kwargs)
                expiry = lifetime_func()
                backend[cache_keys] = expiry, output

            return output
        return inner
Exemplo n.º 11
0
 def __init__(self, func):
     self.__doc__ = func.__doc__
     code = func.func_code
     flags = code.co_flags
     locals = code.co_nlocals
     n = code.co_argcount
     names = list(code.co_varnames)
     has_args = has_kwargs = True
     if not (flags & _CO_VARKEYWORDS):
         #            flags |= _CO_VARKEYWORDS
         #            locals += 1
         #            names.insert(n, '___fake_kwargs')
         has_kwargs = False
     if not (flags & _CO_VARARGS):
         #            flags |= _CO_VARARGS
         #            locals += 1
         #            names.insert(n, '___fake_args')
         has_args = False
     new_code = _code(n, locals, code.co_stacksize, flags,
                      code.co_code, code.co_consts, code.co_names,
                      tuple(names), code.co_filename, code.co_name,
                      code.co_firstlineno, code.co_lnotab)
     self.__cache = {}
     self.__func = _function(new_code, func.func_globals, func.func_name,
                             func.func_defaults, func.func_closure)
     #        self.__func.__fake_args = fake_args
     #        self.__func.__fake_kwargs = fake_kwargs
     argspec = _getargspec(self.__func)
     #        args, varargs, varkw, defaults = argspec
     if has_args:
         self.__func.__maxargs = 666
     else:
         self.__func.__maxargs = len(argspec[0]) - 1
     if argspec[3] == None:
         defaults = []
         defaultlen = 0
     else:
         defaults = argspec[3]
         defaultlen = len(argspec[3])
     self.__func.__minargs = len(argspec[0]) - defaultlen - 1
     self.__func.__args = argspec[0]
     self.__func.__defaults = defaults
     self.__func.__has_args = has_args
     self.__func.__has_kwargs = has_kwargs
Exemplo n.º 12
0
    def __init__(self, func):
        self.__doc__ = func.__doc__
        code = func.func_code
        flags = code.co_flags
        locals = code.co_nlocals
        n = code.co_argcount
        names = list(code.co_varnames)
        has_args = has_kwargs = True
        if not (flags & _CO_VARKEYWORDS):
#            flags |= _CO_VARKEYWORDS
#            locals += 1
#            names.insert(n, '___fake_kwargs')
            has_kwargs = False
        if not (flags & _CO_VARARGS):
#            flags |= _CO_VARARGS
#            locals += 1
#            names.insert(n, '___fake_args')
            has_args = False
        new_code = _code(n, locals, code.co_stacksize, flags, code.co_code,
                         code.co_consts, code.co_names, tuple(names),
                         code.co_filename, code.co_name, code.co_firstlineno,
                         code.co_lnotab)
        self.__cache = {}
        self.__func = _function(new_code, func.func_globals, func.func_name,
                                func.func_defaults, func.func_closure)
#        self.__func.__fake_args = fake_args
#        self.__func.__fake_kwargs = fake_kwargs
        argspec = _getargspec(self.__func)
#        args, varargs, varkw, defaults = argspec
        if has_args:
            self.__func.__maxargs = 666
        else:
            self.__func.__maxargs = len(argspec[0]) -1
        if argspec[3] == None:
            defaults = []
            defaultlen = 0
        else:
            defaults = argspec[3]
            defaultlen = len(argspec[3]) 
        self.__func.__minargs = len(argspec[0]) - defaultlen - 1
        self.__func.__args = argspec[0]
        self.__func.__defaults = defaults
        self.__func.__has_args = has_args
        self.__func.__has_kwargs = has_kwargs
Exemplo n.º 13
0
    def __new__(cls, name, bases, dct):
        newcls = super(cls, CallbackMeta).__new__(cls, name, bases, dct)
        try:
            argspec = _getargspec(dct["__init__"])
        except (KeyError, TypeError):
            pass
        else:
            args = argspec.args[1:]
            if argspec.varargs:
                _warn("varargs in %r" % cls)
            if argspec.keywords:
                _warn("keywords in %r" % cls)

            for arg in args:
                if arg in cls.registry:
                    _warn("ambiguous constructor argument %r in %r"
                                  % (arg, cls))
                else:
                    cls.registry[arg] = newcls
        return newcls
Exemplo n.º 14
0
    def __new__(cls, name, bases, dct):
        newcls = super(cls, CallbackMeta).__new__(cls, name, bases, dct)
        try:
            argspec = _getargspec(dct["__init__"])
        except (KeyError, TypeError):
            pass
        else:
            args = argspec.args[1:]
            if argspec.varargs:
                _warn("varargs in %r" % cls)
            if argspec.keywords:
                _warn("keywords in %r" % cls)

            for arg in args:
                if arg in cls.registry:
                    _warn("ambiguous constructor argument %r in %r" %
                          (arg, cls))
                else:
                    cls.registry[arg] = newcls
        return newcls
Exemplo n.º 15
0
def getargspec(func):
    if isinstance(func, PickleableStaticMethod):
        return _getargspec(func.fn)
    else:
        return _getargspec(func)
Exemplo n.º 16
0
 def __init__(self, method):
     self.argspec = _getargspec(method)
Exemplo n.º 17
0
def getfuncargs(func):
    # type: (Callable) -> List[str]
    # pylint is too dumb to see that we do NOT use the deprecated variant. :-P
    return _getargspec(func).args  # pylint: disable=deprecated-method
Exemplo n.º 18
0
 def getfullargspec(fun, _fill=(None, ) * 3):  # noqa
     s = _getargspec(fun)
     return FullArgSpec(*s + _fill)
Exemplo n.º 19
0
 def patched_get_argspec(func):
     return ArgSpec(*inspect._getargspec(func))
Exemplo n.º 20
0
def _has_argname(argname, function):
    return argname in _getargspec(function).args
Exemplo n.º 21
0
def getargspec(func):
    if isinstance(func, PickleableStaticMethod):
        return _getargspec(func.fn)
    else:
        return _getargspec(func)
Exemplo n.º 22
0
 def getfullargspec(fun, _fill=(None, ) * 3):  # noqa
     """For compatibility with Python 3."""
     s = _getargspec(fun)
     return FullArgSpec(*s + _fill)
Exemplo n.º 23
0
 def required_argnames(self):
     argspec = _getargspec(self.func)
     # trim ``self`` off the beginning, defaultable args off the end
     return argspec.args[1:][: -len(argspec.defaults or ())]
Exemplo n.º 24
0
 def kwargs_argname(self):
     return _getargspec(self.func).keywords
Exemplo n.º 25
0
 def argdefaults(self):
     argspec = _getargspec(self.func)
     return dict(zip(reversed(argspec.args), reversed(argspec.defaults or ())))
Exemplo n.º 26
0
 def _get_kwarg_inputs(self):
     # Trim off first parameter as it is reserved for workflow_parameters
     return set(_getargspec(self.task_function).args[1:])
Exemplo n.º 27
0
 def getfullargspec(fun, _fill=(None, ) * 3):  # noqa
     s = _getargspec(fun)
     return FullArgSpec(*s + _fill)
Exemplo n.º 28
0
			def getargspec(cls, target):
				return cls(_getargspec(target))
Exemplo n.º 29
0
def _has_argname(argname, function):
    return argname in _getargspec(function).args
Exemplo n.º 30
0
 def _get_kwarg_inputs(self):
     # Trim off first two parameters as they are reserved for workflow_parameters and spark_context
     return set(_getargspec(self.task_function).args[2:])
Exemplo n.º 31
0
def getargspec(func):
    return _getargspec(unwrap(func))
Exemplo n.º 32
0
 def patched_get_argspec(func):
     return ArgSpec(*inspect._getargspec(func))
Exemplo n.º 33
0
 def __init__(self, method):
     self.argspec = _getargspec(method)
Exemplo n.º 34
0
def getargnames(func):
    while hasattr(func, "__wrapped__"):
        func = func.__wrapped__
    spec = _getargspec(func)
    return spec[:3]
Exemplo n.º 35
0
 def inspect_getfullargspec(func):
     return FullArgSpec(*_getargspec(func)[0:4] + ([], None, {}))
Exemplo n.º 36
0
 def inspect_getfullargspec(func):
     return FullArgSpec(*_getargspec(func)[0:4] + ([], None, {}))
Exemplo n.º 37
0
def grad_named(fun, argname):
    '''Takes gradients with respect to a named argument.
       Doesn't work on *args or **kwargs.'''
    arg_index = _getargspec(fun).args.index(argname)
    return grad(fun, arg_index)