Example #1
0
def handle_module(module):
    print_tex("\\pagebreak")
    print_heading(1, "Module %s" % module.__name__)
    print_tex(inspect.getdoc(module))

    functions = {}
    data = {}

    for obj_name in dir(module):
        obj = getattr(module, obj_name)

        if inspect.ismodule(obj) or inspect.isbuiltin(obj) or \
           (inspect.isclass(obj) and \
            inspect.getmodule(obj) != module) or \
           (inspect.isfunction(obj) and \
            inspect.getmodule(obj) != module) or \
           isinstance(obj, __future__._Feature) or \
           obj_name.startswith('_'):
            continue

        if obj_name.startswith('__') and obj_name.endswith('__'):
            pass
        elif inspect.isclass(obj):
            handle_class(obj)
        elif inspect.isfunction(obj):
            functions[str_function(obj)] = inspect.getdoc(obj)
        else:
            data[obj_name] = str(obj)

    if functions:
        print_heading(2, "Functions")
        print_desc(functions)
    if data:
        print_heading(2, "Data")
        print_desc(data)
    def getmethods(self,modulePath,Class) :
        '''
         This will get the list of methods in given module or class.
         It accepts the module path and class name. If there is no
         class name then it has be mentioned as None.
        '''
        methodList = []
        moduleList = modulePath.split("/")
        newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]])
        print "Message : Method list is being obatined , Please wait ..."
        try :
            if Class :
                Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(), [Class], -1)
                ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)]
                self.ClassList = ClassList
                Class = vars(Module)[Class]
                methodList = [x.__name__ for x in Class.__dict__.values() if inspect.isfunction(x)]
            else :
                Module = __import__(moduleList[len(moduleList) - 1], globals(), locals(),[moduleList[len(moduleList) - 2]], -1)
                methodList = [x.__name__ for x in Module.__dict__.values() if inspect.isfunction(x)]
                ClassList = [x.__name__ for x in Module.__dict__.values() if inspect.isclass(x)]
                self.ClassList = ClassList
        except :
            print "Error : " +str(sys.exc_info()[1])


        self.method = methodList
        return self.method
Example #3
0
def wrap_typecheck_functions(prefix = "generator",
                             ignore = "generator.tools.typecheck"):
    functions = set()
    fixup     = defaultdict(list)
    # Collect all possible functions
    for name, module in sys.modules.items():
        if name.startswith(prefix) or name == "__main__":
            for name, element in getmembers(module):
                # Plain functions
                if isfunction(element):
                    functions.add(element)
                    fixup[element].append((module, name))
                # Class methods
                if isclass(element):
                    for name_, method in getmembers(element):
                        if isfunction(method):
                            functions.add(method)
                            fixup[method].append((element, name_))


    for function in functions:
        if len(function.__annotations__) == 0:
            continue
        if function.__module__ == ignore:
            continue

        wrapped = typecheck(function)
        for obj, attrname in fixup[function]:
            setattr(obj, attrname, wrapped)
Example #4
0
def _map(output_selector, *args):
    """Maps the given input fields to output fields."""
    if len(args) == 1:
        (input_selector, function, output_field) = \
        (Fields.ALL, args[0], Fields.UNKNOWN)
    elif len(args) == 2:
        if inspect.isfunction(args[0]) or _any_instance(args[0], \
        (DecoratedFunction, cascading.operation.Function, cascading.operation.Filter)):
            # The first argument is a function, the second is the output fields
            (input_selector, function, output_field) = \
            (Fields.ALL, args[0], args[1])
        else:
            # The first argument is the input tuple argument selector,
            # the second one is the function
            (input_selector, function, output_field) = \
            (args[0], args[1], Fields.UNKNOWN)
    elif len(args) == 3:
        (input_selector, function, output_field) = args
    else:
        raise Exception('map_{add,replace} needs to be called with 1 to 3 parameters')
    if isinstance(function, DecoratedFunction):
        # By default we take everything from the UDF's decorators
        df = function
        if output_field != Fields.UNKNOWN:
            # But if we specified the output fields for the map, use that
            df = DecoratedFunction.decorate_function(function.decorators['function'])
            df.decorators = dict(function.decorators)
            df.decorators['produces'] = output_field
    elif inspect.isfunction(function):
        df = udf(produces=output_field)(function)
    else:
        df = function
    return Apply(input_selector, df, output_selector)
Example #5
0
    def testInit(self):
        """
        Tests __init__() behavior on invalid values
        """
        for name in (None, "", "name"):
            for value in (None, "", "value"):
                for comparator in (None, True, lambda x: True):
                    if not all((name, value, comparator)) \
                            or (not inspect.isfunction(comparator)
                                and not inspect.ismethod(comparator)):
                        # One value is None
                        self.assertRaises(ValueError,
                                          pelix.ldapfilter.LDAPCriteria, name,
                                          value, comparator)

                    else:
                        # All values are OK
                        criteria = pelix.ldapfilter.LDAPCriteria(name, value,
                                                                 comparator)
                        self.assertEqual(name, criteria.name,
                                         "Name modified")
                        self.assertEqual(value, criteria.value,
                                         "Value modified")
                        self.assertTrue(
                            inspect.ismethod(criteria.comparator)
                            or inspect.isfunction(criteria.comparator),
                            "Invalid comparator accepted")
Example #6
0
 def __new__(cls, name, bases, attrs):
     exc_setting_name = '_%s__log_exceptions' % name
     logger_setting_name = '_%s__log_exceptions_logger_name' % name
     ignore_settings_name = '_%s__log_exceptions_ignore' % name
     
     kwargs = {
         'exceptions': Exception
     }
     
     if ignore_settings_name in attrs:
         kwargs['ignore'] = attrs[ignore_settings_name]
         del attrs[ignore_settings_name]
     
     if exc_setting_name in attrs:
         kwargs['exceptions'] = attrs[exc_setting_name]
         del attrs[exc_setting_name]
     
     if logger_setting_name in attrs:
         kwargs['logger'] = attrs[logger_setting_name]
         del attrs[logger_setting_name]
         
     log_exception_wrapper = log_exception(**kwargs)
     
     for n, v in attrs.items():
         if inspect.isfunction(v):
             attrs[n] = log_exception_wrapper(v)
     
     for base in bases:
         for n, v in base.__dict__.items():
             if not n in attrs and inspect.isfunction(v):
                 attrs[n] = log_exception_wrapper(v)
                 
     new_class = super(LogExceptionsMetaclass, cls).__new__(cls, name, bases, attrs)
     return new_class    
Example #7
0
def print_classes():
    for name, obj in inspect.getmembers(jb):
        if inspect.isclass(obj):
            #f1.write('paichi_the_found'
            f1.write('\n')
            f1.write("Class      Name ->  "+name)
            f1.write('\n')
            for N, O in inspect.getmembers(obj):
            	#f1.write(' --- Extra --- '
            	if inspect.isclass(O):
            		f1.write("      "+"SubClass      Name ->  "+N)
            		f1.write('\n')
            	if inspect.ismethod(O):
            		f1.write("      "+"SubMethod     Name ->  "+N)
            		f1.write('\n')
            	if inspect.isfunction(O):
            		f1.write("      "+"SubFunction   Name ->  "+N)
            		f1.write('\n')
        if inspect.ismethod(obj):
            #f1.write('paichi_the_found'
            f1.write('')
            f1.write('\n')
            f1.write("Method     Name ->  "+name)
            f1.write('\n')
        if inspect.isfunction(obj):
            #f1.write('paichi_the_found'
            f1.write('')
            f1.write('\n')
            f1.write("Function   Name ->  "+name)
            f1.write('\n')
Example #8
0
def deprecated(obj):
    """
    Decorator to apply to functions or classes that we think are not being (or
    should not be) used anymore.  Prints a warning to the log.
    """
    if inspect.isfunction(obj) or inspect.ismethod(obj):
        if inspect.isfunction(obj):
            obj_desc = "\"%s\" function" % obj.__name__
        else:
            obj_desc = "\"%s\" class" % obj.im_class.__name__
        def print_warning_wrapper(*args, **kwargs):
            sanslog.warning("The %s has been marked as deprecated and may be "\
                            "removed in a future version of Mantid.  If you "\
                            "believe this to have been marked in error, please "\
                            "contact the member of the Mantid team responsible "\
                            "for ISIS SANS." % obj_desc)
            return obj(*args, **kwargs)
        return print_warning_wrapper

    # Add a @deprecated decorator to each of the member functions in the class
    # (by recursion).
    if inspect.isclass(obj):
        for name, fn in inspect.getmembers(obj):
            if isinstance(fn, types.UnboundMethodType):
                setattr(obj, name, deprecated(fn))
        return obj

    assert False, "Programming error.  You have incorrectly applied the "\
                  "@deprecated decorator.  This is only for use with functions "\
                  "or classes."
Example #9
0
 def _call_build_method(self, context, type, platform, data):
     if self._build_methods[type] is not None:
         if isfunction(self._build_methods[type]):
             return self._build_methods[type](context, data)
         elif self._build_methods[type][platform] is not None and isfunction(self._build_methods[type][platform]):
             return self._build_methods[type][platform](context, data)
     raise 'do not support type: %s and platform: %s' % (type, platform)
Example #10
0
 def modification(self, profile):
   for func in StackEPROCESS.__dict__:
     if isfunction(StackEPROCESS.__dict__[func]):
       setattr(profile.object_classes['_EPROCESS'], func, StackEPROCESS.__dict__[func])
   for func in StackLDR_DATA_TABLE_ENTRY.__dict__:
     if isfunction(StackLDR_DATA_TABLE_ENTRY.__dict__[func]):
       setattr(profile.object_classes['_LDR_DATA_TABLE_ENTRY'], func, StackLDR_DATA_TABLE_ENTRY.__dict__[func])
Example #11
0
    def _ScopeInspector_GetFunctionAttributes(definitions):
        if type(definitions) != dict:
            return []
        from mantid.simpleapi import _get_function_spec
        keywords = []
        for name,obj in _iteritems(definitions):
            if name.startswith('_') : continue
            if _inspect.isclass(obj) or _inspect.ismodule(obj):
                continue
            if _inspect.isfunction(obj) or _inspect.isbuiltin(obj):
                keywords.append(name + _get_function_spec(obj))
                continue
            # Object could be a proxy so check and use underlying object
            if hasattr(obj,"_getHeldObject"):
                obj = obj._getHeldObject()
            attrs = dir(obj)
            for att in attrs:
                try:
                    fattr = getattr(obj,att)
                except Exception:
                    continue # not much we do if not even calling it causes an exception
                if att.startswith('_'):
                    continue
                if _inspect.isfunction(fattr) or _inspect.ismethod(fattr) or \
                        hasattr(fattr,'im_func'):
                    keywords.append(name + '.' + att + _get_function_spec(fattr))

        return keywords;
Example #12
0
    def __init__(self, *check, **kwargs):
        """        

        @keyword default: value/ function that will be
        assigned/called if prop value is requested but is None.
        
        @keyword reset: value/function that will be assigned/called on
        initialization
        
        @keyword blurb: Short description.
        @keyword doc: Longer description.
        """

        self.blurb = kwargs.pop('blurb', None)
        self.doc = kwargs.pop('doc', None)

        self.check = CheckAll(check or [])

        default = kwargs.pop('default', None)
        if inspect.isfunction(default) or inspect.ismethod(default):
            self.on_default = default
        else:
            if default is not None:
                default = self.check(default)
            self.on_default = (lambda: default)

        reset = kwargs.pop('reset', None)
        if inspect.isfunction(reset) or inspect.ismethod(reset):
            self.on_reset = reset
        else:
            if reset is not None:
                reset = self.check(reset)
            self.on_reset = (lambda: reset)

        self.name = None # set by the owning HasProps class
Example #13
0
def xmlrpc(func, name=None):
    global __xmlrpc_functions__

    if isinstance(func, str):
        return partial(xmlrpc, name=func)

    if inspect.isfunction(func):
        f_name = func.__name__
        if name:
            f_name = name
        rule = Rule()
        rule.endpoint = ".".join([func.__module__, func.__name__])
        rule.rule = rule.endpoint
        __xmlrpc_functions__[f_name] = rule
        func.xmlrpc_endpoint = (f_name, rule.endpoint)
    elif inspect.isclass(func):
        if not name:
            name = func.__name__
        for _name in dir(func):
            f = getattr(func, _name)
            if (inspect.ismethod(f) or inspect.isfunction(f)) and not _name.startswith("_"):
                f_name = name + "." + f.__name__
                endpoint = ".".join([func.__module__, func.__name__, _name])
                rule = Rule()
                rule.endpoint = rule.rule = endpoint
                if hasattr(f, "xmlrpc_endpoint"):
                    # the method has already been decorate by xmlrpc
                    _n, _e = f.xmlrpc_endpoint
                    __xmlrpc_functions__[name + "." + _n] = rule
                    del __xmlrpc_functions__[_n]
                else:
                    __xmlrpc_functions__[f_name] = rule
    else:
        raise Exception, "Can't support this type [%r]" % func
    return func
Example #14
0
def _rejigger_module(old, new, ignores):
    """Mighty morphin power modules"""
    __internal_swaprefs_ignore__ = "rejigger_module"
    oldVars = vars(old)
    newVars = vars(new)
    ignores += (id(oldVars),)
    old.__doc__ = new.__doc__

    # Get filename used by python code
    filename = new.__file__

    for name, value in newVars.iteritems():
        if name in oldVars:
            oldValue = oldVars[name]
            if oldValue is value:
                continue

            if _from_file(filename, value):
                if inspect.isclass(value):
                    _rejigger_class(oldValue, value, ignores)
                    
                elif inspect.isfunction(value):
                    _rejigger_func(oldValue, value, ignores)
        
        setattr(old, name, value)

    for name in oldVars.keys():
        if name not in newVars:
            value = getattr(old, name)
            delattr(old, name)
            if _from_file(filename, value):
                if inspect.isclass(value) or inspect.isfunction(value):
                    _remove_refs(value, ignores)
    
    _swap_refs(old, new, ignores)
Example #15
0
def cache(key, value):
    """Simple file cache using shelve.
    
    Aims to be a very simple wrapper for shelve.  Suppose we are performing
    an expensive function evaluation we want to cache:
    
        data = compute(args)
        
    we can cache the result and load it if available:
    
        data = cache("data", lambda: compute(args))
        
    Note the use of lambda to enable lazy evaluation of the function.
    """
    _do_open()

    if not inspect.isfunction(value):
        if _CACHE_WARNINGS:
            warnings.warn("cache value should be a function to enable lazy evaluation", UserWarning)
        
    if key not in _CACHE:
        if inspect.isfunction(value):
            _CACHE[key] = value()
        else:
            _CACHE[key] = value
        
    return _CACHE[key]
 def testWrap(self): #{{{
     '''Any other objects get wrapped in a function'''
     _ = isinstance
     self.assertFalse(isfunction(_))
     ret = callableobj(_)
     self.assertTrue(ret)
     self.assertTrue(isfunction(ret))
Example #17
0
  def _write_member_markdown_to_file(self, f, prefix, name, member):
    """Print `member` to `f`."""
    if (inspect.isfunction(member) or inspect.ismethod(member) or
        isinstance(member, property)):
      print("- - -", file=f)
      print("", file=f)
      self._print_function(f, prefix, name, member)
      print("", file=f)

      # Write an individual file for each function.
      if inspect.isfunction(member):
        indivf = open(
            os.path.join(self.shard_dir(name), name + ".md"), "w+")
        self._print_function(indivf, prefix, name, member)
    elif inspect.isclass(member):
      print("- - -", file=f)
      print("", file=f)
      print("%s `class %s` {#%s}" % (prefix, name,
                                     _get_anchor(self._module_to_name, name)),
            file=f)
      print("", file=f)
      self._write_class_markdown_to_file(f, name, member)
      print("", file=f)

      # Write an individual file for each class.
      indivf = open(
          os.path.join(self.shard_dir(name), name + ".md"), "w+")
      self._write_class_markdown_to_file(indivf, name, member)
    else:
      raise RuntimeError("Member %s has unknown type %s" % (name, type(member)))
Example #18
0
    def test_stubtreq_provides_all_functions_in_treq_all(self):
        """
        Every single function and attribute exposed by :obj:`treq.__all__` is
        provided by :obj:`StubTreq`.
        """
        treq_things = [(name, obj) for name, obj in getmembers(treq) if name in treq.__all__]
        stub = StubTreq(_StaticTestResource())

        api_things = [(name, obj) for name, obj in treq_things if obj.__module__ == "treq.api"]
        content_things = [(name, obj) for name, obj in treq_things if obj.__module__ == "treq.content"]

        # sanity checks - this test should fail if treq exposes a new API
        # without changes being made to StubTreq and this test.
        msg = (
            "At the time this test was written, StubTreq only knew about "
            "treq exposing functions from treq.api and treq.content.  If "
            "this has changed, StubTreq will need to be updated, as will "
            "this test."
        )
        self.assertTrue(all(isfunction(obj) for name, obj in treq_things), msg)
        self.assertEqual(set(treq_things), set(api_things + content_things), msg)

        for name, obj in api_things:
            self.assertTrue(isfunction(getattr(stub, name, None)), "StubTreq.{0} should be a function.".format(name))

        for name, obj in content_things:
            self.assertIs(getattr(stub, name, None), obj, "StubTreq.{0} should just expose treq.{0}".format(name))
Example #19
0
    def _execute(self, changes):

        #if self.handlers:
        for status, filepaths in changes.iteritems():
            for filepath in filepaths:
                #if self.debug:
                    #print status, filepath
                    #continue

                hdr = self.get_handler(filepath)
                if not hdr:
                    continue
                if inspect.isclass(hdr):
                    hdr_instance = hdr(self, *self.get_handler_args(filepath))
                    getattr(hdr_instance, status)()
                elif inspect.isfunction(hdr):
                    hdr(*self.get_handler_args(filepath))
                elif isinstance(hdr, (str, unicode)):
                    run_command_str(hdr)
                else:
                    raise Exception('Handler type error, %s' % type(hdr))

        if self.global_handler:
            hdr = self.global_handler
            if inspect.isfunction(hdr):
                hdr()
            elif isinstance(hdr, (str, unicode)):
                run_command_str(hdr)
            else:
                raise Exception('Handler type error, %s' % type(hdr))
Example #20
0
def help2md(filepath, output='README.md', name='code'):
    """help2md
    Converts python help to a .md file.
    params:
        - filename - The full path of the input file
        - output - The full path of the output file ( defaults to README.md )
        - name - The name of the file. It puts this at the top of the document.
    """
    document = '#' + name + '\n'
    c = imp.load_source(name, filepath)
    doc = inspect.getdoc(c)
    if doc:
        document += doc + '\n'
    else:
        document += '\n'
    
    main_file = getattr(c, '__file__')
    modules = []
    items = dir(c)
    for i in items:
        item = getattr(c, i)

        if inspect.isfunction(item):
            doc = inspect.getdoc(item)
            if doc == None:
                doc = 'No documentation'
            params = inspect.formatargspec(*inspect.getfullargspec(item))
            document += ('\n\n##' + i + '\n```python\ndef ' + i + params + 
                ':\n\t"""' + '\n\t'.join(doc.split('\n'))
                + '"""\n```')
        
        if inspect.isclass(item):
            doc = inspect.getdoc(item)
            if doc == None:
                doc = 'No documentation'
            document += ('\n\n##' + i + '\n```python\nclass ' + i + 
                '():\n\t"""' + '\n\t'.join(doc.split('\n')) 
                + '"""\n```')
            methods = dir(item)
            
            for m in methods:
                mitem = getattr(item, m)
                if inspect.isfunction(mitem):
                    params = inspect.formatargspec(
                        *inspect.getfullargspec(mitem))
                    doc = inspect.getdoc(mitem)
                    if doc == None:
                        doc = 'No documentation'
                    document += ('\n\n###' + m + '\n```python\n\tdef ' + m 
                    + params + ':\n\t\t"""' + '\n\t\t'.join(
                        doc.split('\n')) + '"""\n```')
        
        if inspect.ismodule(item):
            modules.append(i)
    
    document += '\n\n# Dependencies\n- ' + '\n- '.join(modules)
    document += '\n\n***Made with help2md***'
    with open(output, 'w') as ofile:
        ofile.write(document)
    return None
Example #21
0
def disc_benchmarks(root, ignore_import_errors=False):
    """
    Discover all benchmarks in a given directory tree, yielding Benchmark
    objects

    For each class definition, looks for any methods with a
    special name.

    For each free function, yields all functions with a special
    name.
    """

    root_name = os.path.basename(root)

    for module in disc_modules(root_name, ignore_import_errors=ignore_import_errors):
        for attr_name, module_attr in (
            (k, v) for k, v in module.__dict__.items()
            if not k.startswith('_')
        ):
            if inspect.isclass(module_attr):
                for name, class_attr in inspect.getmembers(module_attr):
                    if (inspect.isfunction(class_attr) or
                            inspect.ismethod(class_attr)):
                        benchmark = _get_benchmark(name, module, module_attr,
                                                   class_attr)
                        if benchmark is not None:
                            yield benchmark
            elif inspect.isfunction(module_attr):
                benchmark = _get_benchmark(attr_name, module, None, module_attr)
                if benchmark is not None:
                    yield benchmark
Example #22
0
    def register(self, obj, autogenerate=False):
        """
        Registers the inputted object to this scope.
        
        :param      obj | <module> || <function> || <climethod>
        """
        scope = self._scope

        # register a module
        if type(obj).__name__ == 'module':
            for key, value in vars(obj).items():
                # register a climethod
                if isinstance(value, climethod):
                    value.interface = self
                    scope[key] = value

                # register a function
                elif inspect.isfunction(value) and autogenerate:
                    meth = climethod(value)
                    meth.interface = self
                    scope[key] = meth

        # register a climethod
        elif isinstance(obj, climethod):
            obj.interface = self
            scope[obj.__name__] = obj

        # register a function
        elif inspect.isfunction(obj) and autogenerate:
            meth = climethod(obj)
            meth.interface = self
            scope[meth.__name__] = meth
Example #23
0
 def addHandler(self, comparator, callback):
     """Adds a handler to the SerialListener
         
     Throws ValueError: on invalid arguments
      comparator is either True or a function. That function must return a 
      boolean value when passed the message in question.  callback is a 
      function that is called iff the comparator returns True and is also 
      passed the msg.
     """
     # Make sure that the comparator is either a function or a boolean
     if not (inspect.isfunction(comparator) or inspect.ismethod(comparator) or isinstance(comparator, bool)):
         raise ValueError("Invalid comparator passed to addHandler, must be a function or boolean")
     # If comparator is a function check to make sure it takes atleast 1 argument beyond self
     if inspect.isfunction(comparator) or inspect.ismethod(comparator):
         args = inspect.getargspec(comparator).args
         try:
             args.remove("self")
         except:
             pass
         if len(args) == 0:
             raise ValueError("Invalid comparator passed to addHandler, must take atleast one argument")
     # Make sure that the callback is a function
     if not (inspect.isfunction(callback) or inspect.ismethod(callback)):
         raise ValueError("Invalid callback passed to addHandler, must be a function")
     else:
         # Make sure it has atleast one argument for the msg
         args = inspect.getargspec(callback).args
         try:
             args.remove("self")
         except:
             pass
         if len(args) == 0:
             raise ValueError("Invalid callback passed to addHandler, must take atleast one argument")
     self.handlers.append((comparator, callback))
def get_nonlocal_vars(func):

    if (inspect.isfunction(func)):

        code = func.__code__
    
        # Nonlocal references are named in co_freevars and resolved
        # by looking them up in __closure__ by positional index

        variablelist = ''
        for var in func.__code__.co_varnames:
            if variablelist == '':
                variablelist = variablelist + var
            else:
                variablelist = variablelist + '\l' + var
        if localvariables != '':
            localvariables.append((func.__name__, variablelist))


        if func.__closure__ is None:
            nonlocal_vars = {}
        else:
            nonlocal_vars = {
                var : cell.cell_contents
                for var, cell in zip(code.co_freevars, func.__closure__)
           }

        for key, value in nonlocal_vars.items():
            if value not in foundfunctions:
                if inspect.isfunction(value):
                    
                    foundfunctions.append(value)
                    get_nonlocal_vars(value)

        return nonlocal_vars
Example #25
0
def help(fnc=None):
    """
    Print help message, optionally about a specific function
    """
    import inspect
    self = sys.modules['__main__']
    if fnc:
        try:
            cmd = getattr(self, fnc)
        except:
            cmd = None
        if not inspect.isfunction(cmd):
            print "No function named: %s found" % fnc
            sys.exit(2)
        (args, varargs, varkw, defaults) = inspect.getargspec(cmd)
        print cmd.__doc__
        print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args]))
    else:
        print "Usage: cwutil [command]"
        for cname in dir(self):
            if not cname.startswith("_") and not cname == "cmd":
                cmd = getattr(self, cname)
                if inspect.isfunction(cmd):
                    doc = cmd.__doc__
                    print "\t%s - %s" % (cname, doc)
    sys.exit(1)
Example #26
0
    def __init__(self, tmpdir=None, extra_defs=None):
        u"""Initialize plugin architecture.

        Find all plugins offered by macro_plugins, check that they implement
        the required methods and add them to the plugin dictionary.

        The parser will need a working directory. If the user does not supply a
        valid one, the parser will create a temporary directory, which will be
        destroyed with the object.
        If the user supplies a valid one, it will be up to the user to manage
        its lifecycle."""
        # Setup logger
        self.log = logging.getLogger(self.__class__.__name__)
        # Setup plugins
        self.plugins = {}
        for mod in __all__:
            plugin = __plugins__[mod]
            if (inspect.isfunction(plugin.expects)
                    and inspect.isfunction(plugin.parse)):
                self.plugins[mod] = plugin
                self.log.debug(u"Found plugin \"%s\"", mod)
            else:
                self.log.debug(u"Invalid plugin \"%s\"", mod)
        # Setup temporary directory passthrough
        self._tmpdir = tmpdir
        self._tmpdir_managed = False
        # Setup macro expander variable
        self.macro_expander = None
        self.extra_defs = extra_defs
Example #27
0
	def generateUnittestCode(filepath, indent, test_filepath=None):
		tested_methods = set()
		if test_filepath:
			tested_methods = Generator.getTestedMethods(test_filepath)
		indent = indent.replace('\\t', '\t')
		base_path = os.path.dirname(os.path.abspath(filepath))
		module_name = os.path.basename(filepath)
		error_message = ''
		output = 'import unittest\n\n'
		if module_name.endswith('.py'):
			module_name = module_name[:-3]
		elif module_name.endswith('.pyc'):
			module_name = module_name[:-4]
		try:
			sys.path.append(base_path)
			module = importlib.import_module(module_name)
			output += 'import %s\n\n' % module_name
		except ImportError as e:
			logger.error('Failed')
			logger.error('%s', traceback.format_exc())

		for (name, obj) in inspect.getmembers(module):
			if inspect.isclass(obj):
				# Only consider classes from this module, ignore the classes imported
				# from other modules.
				if not str(obj.__module__).endswith(module_name):
					continue
				logger.debug('Found class "%s".', name)
				output += 'class %s%s(unittest.TestCase):\n' % (
						name,	_TEST_CLASS_SUFFIX)
				output += '%sdef setUp(self):\n%s%spass \n\n' % (indent, indent,
						indent)
				for (func_name, func_obj) in inspect.getmembers(obj):
					if inspect.isfunction(func_obj) or inspect.ismethod(func_obj):
						logger.debug('Found function "%s".', func_name)
						test_name1 = '%s%s.%s%s' % (
								name, _TEST_CLASS_SUFFIX, _TEST_METHOD_PREFIX, func_name)
						test_name2 = test_name1.replace('__', '_')
						# Either format is acceptable.
						if (test_name1 not in tested_methods) and (
								test_name2 not in	tested_methods):
							if test_name1 != test_name2:
								error_message += 'Please add test %s or %s.\n' % (
										test_name1, test_name2)
							else:
								error_message += 'Please add test %s.\n' % (test_name1)
							output += ('%sdef %s%s(self):\n%s%spass'
									' # To be implemented.\n\n' %(indent, _TEST_METHOD_PREFIX,
											func_name, indent, indent))
			elif inspect.isfunction(obj):
				output += 'class GlobalMethod_%s%s(unittest.TestCase):\n' % (
						name, _TEST_CLASS_SUFFIX)
				output += '%sdef %s%s(self):\n%s%spass # To be implemented.\n\n' %(
						indent, _TEST_METHOD_PREFIX, name, indent, indent)

		output += 'if __name__ == \'__main__\':\n%sunittest.main()' % indent
		if error_message:
			print error_message
			print
		print output
Example #28
0
 def run(self, iters, omega):
     word = self.lsystem.generate_word(iters, omega)
     for c in word:
         if c in self.lsystem.productions_dict:
             if isfunction(self.d):
                 self.turtle.forward(self.d())
             else:
                 self.turtle.forward(self.d)
         elif c == '[':
             self.pos_stack.append((self.turtle.xcor(),
                                    self.turtle.ycor(),
                                    self.turtle.heading()))
         elif c == ']':
             pos = self.pos_stack.pop()
             self.turtle.pu()
             self.turtle.setx(pos[0])
             self.turtle.sety(pos[1])
             self.turtle.setheading(pos[2])
             self.turtle.pd()
         elif c == '-':
             if isfunction(self.delta):
                 self.turtle.left(self.delta())
             else:
                 self.turtle.left(self.delta)
         elif c == '+':
             if isfunction(self.delta):
                 self.turtle.right(self.delta())
             else:
                 self.turtle.right(self.delta)
         else:
             SyntaxError("Invalid character in LSystem word.")
Example #29
0
def test_geo_spatial_binops(backend, geo, fn, arg_left, arg_right, expected):
    """Testing for geo spatial binary operations."""
    left = arg_left(geo) if isfunction(arg_left) else arg_left
    right = arg_right(geo) if isfunction(arg_right) else arg_right
    expr = getattr(left, fn)(right)
    result = expr.execute()
    testing.assert_almost_equal(result, expected, decimal=2)
def parseCallable(targetCall, name, user_args, user_kwargs):
    '''
    '''
    #
    # Check callable given
    #
    import inspect
    if not (inspect.ismethod(targetCall) or inspect.isfunction(targetCall)):
        raise GraphError("Callable must be a function or method.")

    # Common args
    callableArgs = {}
    callableArgs['sysPath'] = sys.path
    callableArgs['moduleName'] = targetCall.__module__

    # Ensure params can be serialized i.e. not object, instance or function
    try:
        callableArgs['user_args'] = json.dumps(user_args)
        callableArgs['user_kwargs'] = json.dumps(user_kwargs)
    except TypeError:
        raise GraphError("Error: invalid parameters (not JSON serializable): %s" % params)

    # Specific args
    if inspect.isfunction(targetCall):
        callableArgs['execType'] = 'function'
        callableArgs['funcName'] = targetCall.__name__
        taskName = name if name != "" else callableArgs['funcName']

    if inspect.ismethod(targetCall):
        callableArgs['execType'] = 'method'
        callableArgs['className'] = inspect.getmro(targetCall.im_class)[0].__name__
        callableArgs['methodName'] = targetCall.__name__
        taskName = name if name != "" else callableArgs['className'] + "." + callableArgs['methodName']

    return (taskName, callableArgs)
def _attach(
    logger_module: Union[str, ModuleType],
    modules: List[ModuleType],
    classes: List[Type[Any]],
    missings: List[Tuple[Type[Any], Type[Any]]],
) -> None:
    if isinstance(logger_module, str):
        logger_module = importlib.import_module(logger_module)

    logger = getattr(logger_module, "get_logger")()

    special_functions = set([
        "__init__",
        "__repr__",
        "__str__",
        "_repr_html_",
        "__len__",
        "__getitem__",
        "__setitem__",
        "__getattr__",
        "__enter__",
        "__exit__",
    ])

    # Modules
    for target_module in modules:
        target_name = target_module.__name__.split(".")[-1]
        for name in getattr(target_module, "__all__"):
            func = getattr(target_module, name)
            if not inspect.isfunction(func):
                continue
            setattr(target_module, name,
                    _wrap_function(target_name, name, func, logger))

    # Classes
    for target_class in classes:
        for name, func in inspect.getmembers(target_class, inspect.isfunction):
            if name.startswith("_") and name not in special_functions:
                continue
            try:
                isstatic = isinstance(
                    inspect.getattr_static(target_class, name), staticmethod)
            except AttributeError:
                isstatic = False
            wrapped_function = _wrap_function(target_class.__name__, name,
                                              func, logger)
            setattr(
                target_class, name,
                staticmethod(wrapped_function)
                if isstatic else wrapped_function)

        for name, prop in inspect.getmembers(
                target_class, lambda o: isinstance(o, property)):
            if name.startswith("_"):
                continue
            setattr(target_class, name,
                    _wrap_property(target_class.__name__, name, prop, logger))

    # Missings
    for original, missing in missings:
        for name, func in inspect.getmembers(missing, inspect.isfunction):
            setattr(
                missing,
                name,
                _wrap_missing_function(original.__name__, name, func, original,
                                       logger),
            )

        for name, prop in inspect.getmembers(
                missing, lambda o: isinstance(o, property)):
            setattr(
                missing, name,
                _wrap_missing_property(original.__name__, name, prop, logger))
Example #32
0
def lookslikeamethod(part):
    """ Does the passed part appear to be methodlike? """
    if inspect.isfunction(part) or inspect.ismethod(part) or inspect.isbuiltin(
            part) or inspect.ismethoddescriptor(part):
        return True
    return False
Example #33
0
def showrecipes(_file):
    """
    Takes in a file, and will return all the possible recipes that can be used
    on the file.

    Parameters
    ----------
    _file : str
        file name for the fits file

    Returns
    -------
    str
        String with all the information provided
    """
    # string that results are appended to
    result = ""

    # Find the file and open it with astrodata
    try:
        ad = astrodata.open(_file)
        tags = ad.tags
    except AstroDataError:
        result += ("There was an issue using the selected file, please check "
                   "the format and directory.")
        raise

    # New Mapper interface now requires a tag set and instrument name.
    # We decoupled the tags from 'ad'.
    dtags = set(list(ad.tags)[:])
    instpkg = ad.instrument(generic=True).lower()

    all_recipies = []
    functions_list = []
    for mode in ['sq', 'qa', 'ql']:

        try:
            rm = RecipeMapper(dtags, instpkg, mode=mode)
            recipe = rm.get_applicable_recipe()

        except RecipeNotFound:
            error_message = "The RecipeMapper returned a RecipeNotFound " \
                            "error. For showrecipes, this means that " \
                            "there does not exist a recipe for the given " \
                            "file. This may be because the observation type " \
                            "found in the astrodata tags does not match any" \
                            "module for the given mode"

            # If on the last run of the for loop, function_list is still empty,
            # and a RecipeNotFound error was raised again, then raise error.
            # Else module may just not exist for the current mode,
            if functions_list == [] and mode == mode[-1]:
                raise RecipeNotFound(error_message)

            else:
                pass

        except ModeError:
            # ModeError returned if the mode does not exist for the file
            # ql is not implemented yet, so this catches that exception
            pass

        else:
            mod = importlib.import_module(recipe.__module__)

            # finds all functions(recipies) in the module except for 'default'
            functions_list = [
                i[0] for i in inspect.getmembers(mod)
                if inspect.isfunction(i[1]) and (i[0] != '_default')
            ]

            # Appends said recipes to an external list so it can be called later
            for i in range(len(functions_list)):
                all_recipies.append(recipe.__module__ + "::" +
                                    functions_list[i])

    result += ("Input file: {}".format(os.path.abspath(ad.path)))

    result += ("\nInput tags: {}".format(tags))

    # Edge case exists where ql mode isn't implemented, sq/qa both pass due to
    # except clause, and then no recipes were found.

    if not all_recipies:
        result += "\n!!! No recipes were found for this file !!!"
    else:
        result += "\nRecipes available for the input file: "
        for recipe in all_recipies:
            result += "\n   " + recipe

    return result
Example #34
0
def f_var_replace(X, old_val, new_val):

    res = X.apply(lambda x: new_val if x == old_val else x)
    return res


def f_var_type_transf(X, transf_type):

    if transf_type in ['str', 'int', 'float', 'bool']:
        if transf_type == 'str':
            res = X.apply(lambda x: np.nan if pd.isnull(x) else str(x))
        elif transf_type == 'int':
            res = X.apply(lambda x: np.nan if pd.isnull(x) else int(x))
        elif transf_type == 'float':
            res = X.apply(lambda x: np.nan if pd.isnull(x) else float(x))
        elif transf_type == 'bool':
            res = X.apply(lambda x: np.nan if pd.isnull(x) else bool(x))

    else:
        raise ValueError("参数transf_type必须在'str','int','float','bool'中取值")

    return res


#******************************************************************************
import inspect

basic_utils_fun = dict((key,val) for key,val in locals().items() \
                       if inspect.isfunction(val) and val.__module__ in( '__main__','src.basic_utils'))
Example #35
0
	def GetPluginsList(self, model_path):
		""" Get plug-ins list from plug-in file
		"""

		### if amd or cmd
		if zipfile.is_zipfile(model_path):
			### zip importer from model path
			importer = zipimport.zipimporter(model_path)

			### where is the puglins.py file ?
			name = "plugins"
			if BlockPluginsList.IsInPackage(model_path):
				fullname = os.path.join(name,name)
			elif BlockPluginsList.IsInRoot(model_path):
				fullname = name
			else:
				fullname = None

			### There is errors in {plugins/}plugins.py file ?
			if fullname:
				try:
					module = importer.load_module(fullname)

				except Exception, info:
					sys.stderr.write(_("Error loading plug-ins: %s\n"%info))
					return info
			else:
				module = None

			### list of tuple composed by parent module, new module and old module
			L = []

			if module:
				### for element (function, method or class) in module coming from plugins.py
				for name,m in inspect.getmembers(module, inspect.isfunction):

					### it's method
					if 'self' in inspect.getargspec(m).args:

						### trying to eval new element to assign
						try:
							new = eval("module.%s"%name)
						except Exception, info:
							new = info
							new.__doc__ = str(info)

						### new element is function
						if inspect.isfunction(new):
							### object has attribute (override)
							if name in self.model.__class__.__dict__:
								old = self.model.__class__.__dict__[name]
							### object don't have attribute (create)
							else:
								old = None
						### new element is class
						#elif inspect.isclass(new):
							#old = self.model.__class__
						else:
							sys.stdout.write(_('WARNING: plug-in type (%s) not supported!'%(name)))

						L.append((m,new,old))
Example #36
0
 def __call__(self, test_func: FuncTypeVar) -> FuncTypeVar:
     if not inspect.isfunction(test_func):
         raise ValueError("Only functions can use override_tmpcadir()")
     return super().__call__(test_func)  # type: ignore[no-any-return]
Example #37
0
def parse_args_for_callable(fn):
    assert inspect.isfunction(fn) or inspect.ismethod(fn)

    sig = inspect.signature(fn)
    docstr = inspect.getdoc(fn).strip()
    docstr, param_helps = _parse_docstr(docstr)
    assert docstr and param_helps, "Please write documentation :)"

    parser = ArgParser()
    for arg_name, arg in sig.parameters.items():
        if arg_name == "self" or arg_name == "logger":
            continue

        # Arguments are required or not required,
        # and have either an annotation or default value.
        # By default, args are parsed as strings if not otherwise specified.
        # REVIEW josephz: Is there a better way to determine if it is a positional/required argument?
        required = arg.default is inspect.Parameter.empty
        default = arg.default if arg.default is not inspect.Parameter.empty else None
        type_ = arg.annotation if arg.annotation is not inspect.Parameter.empty else type(
            default) if default is not None else str

        helpstr = param_helps.get(arg_name, None)

        if type_ is bool:
            add_boolean_argument(parser,
                                 arg_name,
                                 help=helpstr,
                                 default=False if default is None else default)
        elif type_ in (tuple, list, GeneratorType):
            parser.add_argument("--" + arg_name,
                                default=default,
                                type=type_,
                                nargs="+",
                                help=helpstr)
        elif hasattr(type_, "__origin__") and (type_.__origin__ == list
                                               or type_.__origin__ == tuple
                                               or type_.__origin__ == List
                                               or type_.__origin__ == Tuple):
            type_ = type_.__args__[0]
            parser.add_argument("--" + arg_name,
                                default=default,
                                type=type_,
                                nargs="+",
                                help=helpstr)
        elif (hasattr(type_, "__origin__") and type_.__origin__ != List
              and type_.__origin__ != Tuple
              and ((hasattr(type_.__args__[0], "__origin__") and
                    (type_.__args__[0].__origin__ in (list, tuple))) or
                   (hasattr(type_.__args__[1], "__origin__") and
                    (type_.__args__[1].__origin__ in (list, tuple))))):
            nargs = "*" if isinstance(type_, type(Union)) else "+"
            type_ = type_.__args__[1] if type_.__args__[0] is type(
                None) else type_.__args__[0]
            type_ = type_.__args__[0]
            parser.add_argument("--" + arg_name,
                                default=default,
                                type=type_,
                                nargs=nargs,
                                help=helpstr)
        else:
            parser.add_argument("--" + arg_name,
                                default=default,
                                type=type_,
                                required=required,
                                help=helpstr)

    ArgParser.set_help(docstr)
    argv = parser.parse_args()

    return argv
Example #38
0
def main():

    while (True):

        # wait begin of minute (actual 5x second)
        print "Wait for start..."
        cnt = 0
        while True:
            dStart = datetime.datetime.now()

            s = int(dStart.minute * 60.0 + dStart.second +
                    dStart.microsecond / 1000000.0 + cfg.LEAD_START_IN_SECONDS)
            if s % int(cfg.START_INTERVAL_IN_SECONDS) == 0:
                break

            time.sleep(0.1)
            cnt += 1
            if cnt % 50 == 0:
                print "second=", dStart.second

        print "Started at {0}".format(str(dStart))

        utcTime = utc_time_15s_rounded()
        if isfunction(cfg.WORKING_DIR):
            dirName = cfg.WORKING_DIR(utcTime)
        else:
            dirName = cfg.WORKING_DIR
        if not os.path.exists(dirName):
            os.makedirs(dirName)
        baseName = cfg.BASE_FILE_NAME(dirName, dStart, utcTime)
        rawName = baseName + ".raw"
        waveName = baseName + ".wav"

        fv = open(rawName, "wb")
        print "Start write to {0}".format(rawName)

        cn = []
        for item in cfg.CMD_CHAIN:
            if len(cn) == 0:
                p = subprocess.Popen(item, shell=False,
                                     stdout=subprocess.PIPE)  # first
            elif len(cn) == len(cfg.CMD_CHAIN) - 1:
                p = subprocess.Popen(item,
                                     shell=False,
                                     stdin=cn[-1].stdout,
                                     stdout=fv)  # last
            else:
                p = subprocess.Popen(item,
                                     shell=False,
                                     stdin=cn[-1].stdout,
                                     stdout=subprocess.PIPE)  # middle

            cn.append(p)

        print "Writing...Wait for end record..."

        cnt = 0
        while True:
            dCurr = datetime.datetime.now()
            diff = dCurr - dStart
            if diff.total_seconds() > cfg.RECORD_DURATION_IN_SECONDS:
                break
            if cnt % 20 == 0:
                print "seconds writed =", diff.total_seconds()
            time.sleep(0.25)
            cnt += 1

        print "Terminating..."
        # kill entire chain
        os.kill(cn[0].pid, signal.SIGTERM)
        for item in cn:
            item.wait()

        print "Record ended."

        numBytes = fv.tell()
        fv.close()

        # prepend wav header
        with open(waveName, "wb") as dst, open(rawName, "rb") as fv:
            w = wave.Wave_write(dst)
            w.setparams(
                (1, 2, cfg.AUDIO_SAMPLE_RATE, numBytes / 2, 'NONE', 'NONE'))
            w.writeframesraw(fv.read())
            w.close()

        os.remove(rawName)  # delete raw file
        print "waveName=", waveName

        t1 = threading.Thread(target=decoder_proc,
                              args=(waveName, utcTime, baseName))
        t1.start()
Example #39
0
def _get_function_module_and_name(func: Callable) -> Tuple[str, str]:
    """Gets function's module and name, or raise error if not a function."""
    if not inspect.isfunction(func):
        raise ValueError('Expect a function, but got: {}'.format(func))
    return func.__module__, func.__name__
Example #40
0
 def not_module_or_function(x):
     return not (inspect.ismodule(x) or inspect.isfunction(x))
def looks_like_a_decorator(a):
    return (isfunction(a) or ismethod(a) or isclass(a))
Example #42
0
def _is_new_type(type_):
    return inspect.isfunction(type_) and hasattr(type_, "__supertype__")
Example #43
0
def info(
    object=None,
    maxwidth=76,
    output=sys.stdout,
):
    """Get help information for a function, class, or module.

       Example:
          >>> from scipy import *
          >>> info(polyval)
          polyval(p, x)

            Evaluate the polymnomial p at x.

            Description:
                If p is of length N, this function returns the value:
                p[0]*(x**N-1) + p[1]*(x**N-2) + ... + p[N-2]*x + p[N-1]
    """
    global _namedict, _dictlist

    if hasattr(object,'_ppimport_importer') or \
       hasattr(object, '_ppimport_module'):
        object = object._ppimport_module
    elif hasattr(object, '_ppimport_attr'):
        object = object._ppimport_attr

    if object is None:
        info(info)
    elif isinstance(object, types.StringType):
        if _namedict is None:
            _namedict, _dictlist = makenamedict()
        numfound = 0
        objlist = []
        for namestr in _dictlist:
            try:
                obj = _namedict[namestr][object]
                if id(obj) in objlist:
                    print >> output, "\n     *** Repeat reference found in %s *** " % namestr
                else:
                    objlist.append(id(obj))
                    print >> output, "     *** Found in %s ***" % namestr
                    info(obj)
                    print >> output, "-" * maxwidth
                numfound += 1
            except KeyError:
                pass
        if numfound == 0:
            print >> output, "Help for %s not found." % object
        else:
            print >> output, "\n     *** Total of %d references found. ***" % numfound

    elif inspect.isfunction(object):
        name = object.func_name
        arguments = apply(inspect.formatargspec, inspect.getargspec(object))

        if len(name + arguments) > maxwidth:
            argstr = split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print >> output, " " + argstr + "\n"
        print >> output, inspect.getdoc(object)

    elif inspect.isclass(object):
        name = object.__name__
        if hasattr(object, '__init__'):
            arguments = apply(inspect.formatargspec,
                              inspect.getargspec(object.__init__.im_func))
            arglist = arguments.split(', ')
            if len(arglist) > 1:
                arglist[1] = "(" + arglist[1]
                arguments = ", ".join(arglist[1:])
            else:
                arguments = "()"
        else:
            arguments = "()"

        if len(name + arguments) > maxwidth:
            argstr = split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print >> output, " " + argstr + "\n"
        doc1 = inspect.getdoc(object)
        if doc1 is None:
            if hasattr(object, '__init__'):
                print >> output, inspect.getdoc(object.__init__)
        else:
            print >> output, inspect.getdoc(object)

        methods = pydoc.allmethods(object)
        if methods != []:
            print >> output, "\n\nMethods:\n"
            for meth in methods:
                if meth[0] == '_':
                    continue
                thisobj = getattr(object, meth, None)
                if thisobj is not None:
                    methstr, other = pydoc.splitdoc(
                        inspect.getdoc(thisobj) or "None")
                print >> output, "  %s  --  %s" % (meth, methstr)

    elif type(object) is types.InstanceType:  ## check for __call__ method
        print >> output, "Instance of class: ", object.__class__.__name__
        print >> output
        if hasattr(object, '__call__'):
            arguments = apply(inspect.formatargspec,
                              inspect.getargspec(object.__call__.im_func))
            arglist = arguments.split(', ')
            if len(arglist) > 1:
                arglist[1] = "(" + arglist[1]
                arguments = ", ".join(arglist[1:])
            else:
                arguments = "()"

            if hasattr(object, 'name'):
                name = "%s" % object.name
            else:
                name = "<name>"
            if len(name + arguments) > maxwidth:
                argstr = split_line(name, arguments, maxwidth)
            else:
                argstr = name + arguments

            print >> output, " " + argstr + "\n"
            doc = inspect.getdoc(object.__call__)
            if doc is not None:
                print >> output, inspect.getdoc(object.__call__)
            print >> output, inspect.getdoc(object)

        else:
            print >> output, inspect.getdoc(object)

    elif inspect.ismethod(object):
        name = object.__name__
        arguments = apply(inspect.formatargspec,
                          inspect.getargspec(object.im_func))
        arglist = arguments.split(', ')
        if len(arglist) > 1:
            arglist[1] = "(" + arglist[1]
            arguments = ", ".join(arglist[1:])
        else:
            arguments = "()"

        if len(name + arguments) > maxwidth:
            argstr = split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print >> output, " " + argstr + "\n"
        print >> output, inspect.getdoc(object)

    elif hasattr(object, '__doc__'):
        print >> output, inspect.getdoc(object)
def isfunction(object):  # pylint: disable=redefined-builtin
  """TFDecorator-aware replacement for inspect.isfunction."""
  return _inspect.isfunction(tf_decorator.unwrap(object)[1])
Example #45
0
 def get_public_fields(obj):
     return [attr for attr in dir(obj)
             if not (attr.startswith("_")
                     or inspect.isbuiltin(attr)
                     or inspect.isfunction(attr)
                     or inspect.ismethod(attr))]
Example #46
0
def _get_methods(cls):
    import inspect
    # In Python 3 unbound methods are functions, but in Python 2 they are methods
    return inspect.getmembers(cls, predicate=lambda x: inspect.isfunction(x) or inspect.ismethod(x))
Example #47
0
def check_type(argname: str,
               value,
               expected_type,
               memo: Optional[_TypeCheckMemo] = None,
               *,
               globals: Optional[Dict[str, Any]] = None,
               locals: Optional[Dict[str, Any]] = None) -> None:
    """
    Ensure that ``value`` matches ``expected_type``.

    The types from the :mod:`typing` module do not support :func:`isinstance` or :func:`issubclass`
    so a number of type specific checks are required. This function knows which checker to call
    for which type.

    :param argname: name of the argument to check; used for error messages
    :param value: value to be checked against ``expected_type``
    :param expected_type: a class or generic type instance
    :param globals: dictionary of global variables to use for resolving forward references
        (defaults to the calling frame's globals)
    :param locals: dictionary of local variables to use for resolving forward references
        (defaults to the calling frame's locals)

    """
    if expected_type is Any or isinstance(value, Mock):
        return

    if expected_type is None:
        # Only happens on < 3.6
        expected_type = type(None)

    if memo is None:
        frame = sys._getframe(1)
        if globals is None:
            globals = frame.f_globals
        if locals is None:
            locals = frame.f_locals

        memo = _TypeCheckMemo(globals, locals)

    expected_type = resolve_forwardref(expected_type, memo)
    origin_type = getattr(expected_type, '__origin__', None)
    if origin_type is not None:
        checker_func = origin_type_checkers.get(origin_type)
        if checker_func:
            checker_func(argname, value, expected_type, memo)
        else:
            check_type(argname, value, origin_type, memo)
    elif isclass(expected_type):
        if issubclass(expected_type, Tuple):
            check_tuple(argname, value, expected_type, memo)
        elif issubclass(expected_type, (float, complex)):
            check_number(argname, value, expected_type)
        elif _subclass_check_unions and issubclass(expected_type, Union):
            check_union(argname, value, expected_type, memo)
        elif isinstance(expected_type, TypeVar):
            check_typevar(argname, value, expected_type, memo)
        elif issubclass(expected_type, IO):
            check_io(argname, value, expected_type)
        elif issubclass(expected_type, dict) and hasattr(
                expected_type, '__annotations__'):
            check_typed_dict(argname, value, expected_type, memo)
        elif getattr(expected_type, '_is_protocol', False):
            check_protocol(argname, value, expected_type)
        else:
            expected_type = (getattr(expected_type, '__extra__', None)
                             or origin_type or expected_type)

            if expected_type is bytes:
                # As per https://github.com/python/typing/issues/552
                if not isinstance(value, (bytearray, bytes, memoryview)):
                    raise TypeError(
                        'type of {} must be bytes-like; got {} instead'.format(
                            argname, qualified_name(value)))
            elif not isinstance(value, expected_type):
                raise TypeError('type of {} must be {}; got {} instead'.format(
                    argname, qualified_name(expected_type),
                    qualified_name(value)))
    elif isinstance(expected_type, TypeVar):
        # Only happens on < 3.6
        check_typevar(argname, value, expected_type, memo)
    elif isinstance(expected_type, Literal.__class__):
        # Only happens on < 3.7 when using Literal from typing_extensions
        check_literal(argname, value, expected_type, memo)
    elif (isfunction(expected_type)
          and getattr(expected_type, "__module__", None) == "typing" and
          getattr(expected_type, "__qualname__", None).startswith("NewType.")
          and hasattr(expected_type, "__supertype__")):
        # typing.NewType, should check against supertype (recursively)
        return check_type(argname, value, expected_type.__supertype__, memo)
Example #48
0
def _callable(member):
    return inspect.ismethod(member) or inspect.isfunction(member)
Example #49
0
def instantiate_device_type_tests(generic_test_class,
                                  scope,
                                  except_for=None,
                                  only_for=None):
    # Removes the generic test class from its enclosing scope so its tests
    # are not discoverable.
    del scope[generic_test_class.__name__]

    # Creates an 'empty' version of the generic_test_class
    # Note: we don't inherit from the generic_test_class directly because
    #   that would add its tests to our test classes and they would be
    #   discovered (despite not being runnable). Inherited methods also
    #   can't be removed later, and we can't rely on load_tests because
    #   pytest doesn't support it (as of this writing).
    empty_name = generic_test_class.__name__ + "_base"
    empty_class = type(empty_name, generic_test_class.__bases__, {})

    # Acquires members names
    # See Note [Overriding methods in generic tests]
    generic_members = set(generic_test_class.__dict__.keys()) - set(
        empty_class.__dict__.keys())
    generic_tests = [x for x in generic_members if x.startswith('test')]

    # Creates device-specific test cases
    for base in device_type_test_bases:
        # Skips bases listed in except_for
        if except_for is not None and only_for is not None:
            assert base.device_type not in except_for or base.device_type not in only_for,\
                "same device cannot appear in except_for and only_for"
        if except_for is not None and base.device_type in except_for:
            continue
        if only_for is not None and base.device_type not in only_for:
            continue

        class_name = generic_test_class.__name__ + base.device_type.upper()
        device_type_test_class = type(class_name, (base, empty_class), {})

        for name in generic_members:
            if name in generic_tests:  # Instantiates test member
                # Requires tests be a function for Python2 compat
                # (In Python2 tests are type checked methods wrapping functions)
                test = getattr(generic_test_class, name)
                if hasattr(test, '__func__'):
                    test = test.__func__
                assert inspect.isfunction(
                    test), "Couldn't extract function from '{0}'".format(name)

                # XLA-compat shim (XLA's instantiate_test takes doesn't take generic_cls)
                sig = inspect.signature(
                    device_type_test_class.instantiate_test)
                if len(sig.parameters) == 3:
                    # Instantiates the device-specific tests
                    device_type_test_class.instantiate_test(
                        name,
                        copy.deepcopy(test),
                        generic_cls=generic_test_class)
                else:
                    device_type_test_class.instantiate_test(
                        name, copy.deepcopy(test))
            else:  # Ports non-test member
                assert name not in device_type_test_class.__dict__, "Redefinition of directly defined member {0}".format(
                    name)

                # Unwraps to functions (when available) for Python2 compat
                nontest = getattr(generic_test_class, name)
                if hasattr(nontest, '__func__'):
                    nontest = nontest.__func__

                setattr(device_type_test_class, name, nontest)

        # Mimics defining the instantiated class in the caller's file
        # by setting its module to the given class's and adding
        # the module to the given scope.
        # This lets the instantiated class be discovered by unittest.
        device_type_test_class.__module__ = generic_test_class.__module__
        scope[class_name] = device_type_test_class
Example #50
0
def typechecked(func=None,
                *,
                always=False,
                _localns: Optional[Dict[str, Any]] = None):
    """
    Perform runtime type checking on the arguments that are passed to the wrapped function.

    The return value is also checked against the return annotation if any.

    If the ``__debug__`` global variable is set to ``False``, no wrapping and therefore no type
    checking is done, unless ``always`` is ``True``.

    This can also be used as a class decorator. This will wrap all type annotated methods,
    including ``@classmethod``, ``@staticmethod``,  and ``@property`` decorated methods,
    in the class with the ``@typechecked`` decorator.

    :param func: the function or class to enable type checking for
    :param always: ``True`` to enable type checks even in optimized mode

    """
    if func is None:
        return partial(typechecked, always=always, _localns=_localns)

    if not __debug__ and not always:  # pragma: no cover
        return func

    if isclass(func):
        prefix = func.__qualname__ + '.'
        for key, attr in func.__dict__.items():
            if inspect.isfunction(attr) or inspect.ismethod(
                    attr) or inspect.isclass(attr):
                if attr.__qualname__.startswith(prefix) and getattr(
                        attr, '__annotations__', None):
                    setattr(
                        func, key,
                        typechecked(attr,
                                    always=always,
                                    _localns=func.__dict__))
            elif isinstance(attr, (classmethod, staticmethod)):
                if getattr(attr.__func__, '__annotations__', None):
                    wrapped = typechecked(attr.__func__,
                                          always=always,
                                          _localns=func.__dict__)
                    setattr(func, key, type(attr)(wrapped))
            elif isinstance(attr, property):
                kwargs = dict(doc=attr.__doc__)
                for name in ("fset", "fget", "fdel"):
                    property_func = getattr(attr, name)
                    if property_func is None:
                        continue
                    kwargs[name] = typechecked(property_func,
                                               always=always,
                                               _localns=func.__dict__)

                setattr(func, key, property(**kwargs))

        return func

    # Find the frame in which the function was declared, for resolving forward references later
    if _localns is None:
        _localns = sys._getframe(1).f_locals

    # Find either the first Python wrapper or the actual function
    python_func = inspect.unwrap(func, stop=lambda f: hasattr(f, '__code__'))

    if not getattr(func, '__annotations__', None):
        warn('no type annotations present -- not typechecking {}'.format(
            function_name(func)))
        return func

    def wrapper(*args, **kwargs):
        memo = _CallMemo(python_func, _localns, args=args, kwargs=kwargs)
        check_argument_types(memo)
        retval = func(*args, **kwargs)
        try:
            check_return_type(retval, memo)
        except TypeError as exc:
            raise TypeError(*exc.args) from None

        # If a generator is returned, wrap it if its yield/send/return types can be checked
        if inspect.isgenerator(retval) or isasyncgen(retval):
            return_type = memo.type_hints.get('return')
            if return_type:
                origin = getattr(return_type, '__origin__', None)
                if origin in generator_origin_types:
                    return TypeCheckedGenerator(retval, memo)
                elif origin is not None and origin in asyncgen_origin_types:
                    return TypeCheckedAsyncGenerator(retval, memo)

        return retval

    async def async_wrapper(*args, **kwargs):
        memo = _CallMemo(python_func, _localns, args=args, kwargs=kwargs)
        check_argument_types(memo)
        retval = await func(*args, **kwargs)
        check_return_type(retval, memo)
        return retval

    if inspect.iscoroutinefunction(func):
        if python_func.__code__ is not async_wrapper.__code__:
            return wraps(func)(async_wrapper)
    else:
        if python_func.__code__ is not wrapper.__code__:
            return wraps(func)(wrapper)

    # the target callable was already wrapped
    return func
Example #51
0
def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'):
    """
    Get help information for a function, class, or module.

    Parameters
    ----------
    object : optional
        Input object to get information about.
    maxwidth : int, optional
        Printing width.
    output : file like object open for writing, optional
        Write into file like object.
    toplevel : string, optional
        Start search at this level.

    Examples
    --------
    >>> np.info(np.polyval) # doctest: +SKIP

       polyval(p, x)

         Evaluate the polymnomial p at x.

         ...

    """
    global _namedict, _dictlist
    # Local import to speed up numpy's import time.
    import pydoc, inspect

    if hasattr(object,'_ppimport_importer') or \
       hasattr(object, '_ppimport_module'):
        object = object._ppimport_module
    elif hasattr(object, '_ppimport_attr'):
        object = object._ppimport_attr

    if object is None:
        info(info)
    elif isinstance(object, ndarray):
        import numpy.numarray as nn
        nn.info(object, output=output, numpy=1)
    elif isinstance(object, str):
        if _namedict is None:
            _namedict, _dictlist = _makenamedict(toplevel)
        numfound = 0
        objlist = []
        for namestr in _dictlist:
            try:
                obj = _namedict[namestr][object]
                if id(obj) in objlist:
                    print >> output, "\n     *** Repeat reference found in %s *** " % namestr
                else:
                    objlist.append(id(obj))
                    print >> output, "     *** Found in %s ***" % namestr
                    info(obj)
                    print >> output, "-" * maxwidth
                numfound += 1
            except KeyError:
                pass
        if numfound == 0:
            print >> output, "Help for %s not found." % object
        else:
            print >> output, "\n     *** Total of %d references found. ***" % numfound

    elif inspect.isfunction(object):
        name = object.func_name
        arguments = inspect.formatargspec(*inspect.getargspec(object))

        if len(name + arguments) > maxwidth:
            argstr = _split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print >> output, " " + argstr + "\n"
        print >> output, inspect.getdoc(object)

    elif inspect.isclass(object):
        name = object.__name__
        arguments = "()"
        try:
            if hasattr(object, '__init__'):
                arguments = inspect.formatargspec(
                    *inspect.getargspec(object.__init__.im_func))
                arglist = arguments.split(', ')
                if len(arglist) > 1:
                    arglist[1] = "(" + arglist[1]
                    arguments = ", ".join(arglist[1:])
        except:
            pass

        if len(name + arguments) > maxwidth:
            argstr = _split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print >> output, " " + argstr + "\n"
        doc1 = inspect.getdoc(object)
        if doc1 is None:
            if hasattr(object, '__init__'):
                print >> output, inspect.getdoc(object.__init__)
        else:
            print >> output, inspect.getdoc(object)

        methods = pydoc.allmethods(object)
        if methods != []:
            print >> output, "\n\nMethods:\n"
            for meth in methods:
                if meth[0] == '_':
                    continue
                thisobj = getattr(object, meth, None)
                if thisobj is not None:
                    methstr, other = pydoc.splitdoc(
                        inspect.getdoc(thisobj) or "None")
                print >> output, "  %s  --  %s" % (meth, methstr)

    elif type(object) is types.InstanceType:  ## check for __call__ method
        print >> output, "Instance of class: ", object.__class__.__name__
        print >> output
        if hasattr(object, '__call__'):
            arguments = inspect.formatargspec(
                *inspect.getargspec(object.__call__.im_func))
            arglist = arguments.split(', ')
            if len(arglist) > 1:
                arglist[1] = "(" + arglist[1]
                arguments = ", ".join(arglist[1:])
            else:
                arguments = "()"

            if hasattr(object, 'name'):
                name = "%s" % object.name
            else:
                name = "<name>"
            if len(name + arguments) > maxwidth:
                argstr = _split_line(name, arguments, maxwidth)
            else:
                argstr = name + arguments

            print >> output, " " + argstr + "\n"
            doc = inspect.getdoc(object.__call__)
            if doc is not None:
                print >> output, inspect.getdoc(object.__call__)
            print >> output, inspect.getdoc(object)

        else:
            print >> output, inspect.getdoc(object)

    elif inspect.ismethod(object):
        name = object.__name__
        arguments = inspect.formatargspec(*inspect.getargspec(object.im_func))
        arglist = arguments.split(', ')
        if len(arglist) > 1:
            arglist[1] = "(" + arglist[1]
            arguments = ", ".join(arglist[1:])
        else:
            arguments = "()"

        if len(name + arguments) > maxwidth:
            argstr = _split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print >> output, " " + argstr + "\n"
        print >> output, inspect.getdoc(object)

    elif hasattr(object, '__doc__'):
        print >> output, inspect.getdoc(object)
def getCallTip(command='', locals=None):
    """For a command, return a tuple of object name, argspec, tip text.

    The call tip information will be based on the locals namespace."""
    calltip = ('', '', '')  # object name, argspec, tip text.
    # Get the proper chunk of code from the command.
    root = getRoot(command, terminator='(')
    try:
        if locals is not None:
            object = eval(root, locals)
        else:
            object = eval(root)
    except Exception:
        return calltip
    name = ''
    object, dropSelf = getBaseObject(object)
    try:
        name = object.__name__
    except AttributeError:
        pass
    tip1 = ''
    argspec = ''
    if inspect.isbuiltin(object):
        # Builtin functions don't have an argspec that we can get.
        pass
    elif inspect.isfunction(object):
        # tip1 is a string like: "getCallTip(command='', locals=None)"
        argspec = apply(inspect.formatargspec, inspect.getargspec(object))
        if dropSelf:
            # The first parameter to a method is a reference to an
            # instance, usually coded as "self", and is usually passed
            # automatically by Python; therefore we want to drop it.
            temp = argspec.split(',')
            if len(temp) == 1:  # No other arguments.
                argspec = '()'
            elif temp[0][:2] == '(*':  # first param is like *args, not self
                pass
            else:  # Drop the first argument.
                argspec = '(' + ','.join(temp[1:]).lstrip()
        tip1 = name + argspec
    doc = ''
    if callable(object):
        try:
            doc = inspect.getdoc(object)
        except Exception:
            pass
    if doc:
        # tip2 is the first separated line of the docstring, like:
        # "Return call tip text for a command."
        # tip3 is the rest of the docstring, like:
        # "The call tip information will be based on ... <snip>
        firstline = doc.split('\n')[0].lstrip()
        if tip1 == firstline or firstline[:len(name) + 1] == name + '(':
            tip1 = ''
        else:
            tip1 += '\n\n'
        docpieces = doc.split('\n\n')
        tip2 = docpieces[0]
        tip3 = '\n\n'.join(docpieces[1:])
        tip = '%s%s\n\n%s' % (tip1, tip2, tip3)
    else:
        tip = tip1
    #calltip = (name, argspec[1:-1], tip.strip())
    calltip = (name, argspec, tip.strip())
    return calltip
Example #53
0
def info(object=None, maxwidth=76, output=sys.stdout, toplevel='numpy'):
    """
    Get help information for a function, class, or module.

    Parameters
    ----------
    object : object or str, optional
        Input object or name to get information about. If `object` is a
        numpy object, its docstring is given. If it is a string, available
        modules are searched for matching objects.  If None, information
        about `info` itself is returned.
    maxwidth : int, optional
        Printing width.
    output : file like object, optional
        File like object that the output is written to, default is
        ``stdout``.  The object has to be opened in 'w' or 'a' mode.
    toplevel : str, optional
        Start search at this level.

    See Also
    --------
    source, lookfor

    Notes
    -----
    When used interactively with an object, ``np.info(obj)`` is equivalent
    to ``help(obj)`` on the Python prompt or ``obj?`` on the IPython
    prompt.

    Examples
    --------
    >>> np.info(np.polyval) # doctest: +SKIP
       polyval(p, x)
         Evaluate the polynomial p at x.
         ...

    When using a string for `object` it is possible to get multiple results.

    >>> np.info('fft') # doctest: +SKIP
         *** Found in numpy ***
    Core FFT routines
    ...
         *** Found in numpy.fft ***
     fft(a, n=None, axis=-1)
    ...
         *** Repeat reference found in numpy.fft.fftpack ***
         *** Total of 3 references found. ***

    """
    global _namedict, _dictlist
    # Local import to speed up numpy's import time.
    import pydoc
    import inspect

    if (hasattr(object, '_ppimport_importer')
            or hasattr(object, '_ppimport_module')):
        object = object._ppimport_module
    elif hasattr(object, '_ppimport_attr'):
        object = object._ppimport_attr

    if object is None:
        info(info)
    elif isinstance(object, ndarray):
        _info(object, output=output)
    elif isinstance(object, str):
        if _namedict is None:
            _namedict, _dictlist = _makenamedict(toplevel)
        numfound = 0
        objlist = []
        for namestr in _dictlist:
            try:
                obj = _namedict[namestr][object]
                if id(obj) in objlist:
                    print("\n     "
                          "*** Repeat reference found in %s *** " % namestr,
                          file=output)
                else:
                    objlist.append(id(obj))
                    print("     *** Found in %s ***" % namestr, file=output)
                    info(obj)
                    print("-" * maxwidth, file=output)
                numfound += 1
            except KeyError:
                pass
        if numfound == 0:
            print("Help for %s not found." % object, file=output)
        else:
            print("\n     "
                  "*** Total of %d references found. ***" % numfound,
                  file=output)

    elif inspect.isfunction(object):
        name = object.__name__
        arguments = formatargspec(*getargspec(object))

        if len(name + arguments) > maxwidth:
            argstr = _split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print(" " + argstr + "\n", file=output)
        print(inspect.getdoc(object), file=output)

    elif inspect.isclass(object):
        name = object.__name__
        arguments = "()"
        try:
            if hasattr(object, '__init__'):
                arguments = formatargspec(
                    *getargspec(object.__init__.__func__))
                arglist = arguments.split(', ')
                if len(arglist) > 1:
                    arglist[1] = "(" + arglist[1]
                    arguments = ", ".join(arglist[1:])
        except Exception:
            pass

        if len(name + arguments) > maxwidth:
            argstr = _split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print(" " + argstr + "\n", file=output)
        doc1 = inspect.getdoc(object)
        if doc1 is None:
            if hasattr(object, '__init__'):
                print(inspect.getdoc(object.__init__), file=output)
        else:
            print(inspect.getdoc(object), file=output)

        methods = pydoc.allmethods(object)
        if methods != []:
            print("\n\nMethods:\n", file=output)
            for meth in methods:
                if meth[0] == '_':
                    continue
                thisobj = getattr(object, meth, None)
                if thisobj is not None:
                    methstr, other = pydoc.splitdoc(
                        inspect.getdoc(thisobj) or "None")
                print("  %s  --  %s" % (meth, methstr), file=output)

    elif (sys.version_info[0] < 3 and isinstance(object, types.InstanceType)):
        # check for __call__ method
        # types.InstanceType is the type of the instances of oldstyle classes
        print("Instance of class: ", object.__class__.__name__, file=output)
        print(file=output)
        if hasattr(object, '__call__'):
            arguments = formatargspec(*getargspec(object.__call__.__func__))
            arglist = arguments.split(', ')
            if len(arglist) > 1:
                arglist[1] = "(" + arglist[1]
                arguments = ", ".join(arglist[1:])
            else:
                arguments = "()"

            if hasattr(object, 'name'):
                name = "%s" % object.name
            else:
                name = "<name>"
            if len(name + arguments) > maxwidth:
                argstr = _split_line(name, arguments, maxwidth)
            else:
                argstr = name + arguments

            print(" " + argstr + "\n", file=output)
            doc = inspect.getdoc(object.__call__)
            if doc is not None:
                print(inspect.getdoc(object.__call__), file=output)
            print(inspect.getdoc(object), file=output)

        else:
            print(inspect.getdoc(object), file=output)

    elif inspect.ismethod(object):
        name = object.__name__
        arguments = formatargspec(*getargspec(object.__func__))
        arglist = arguments.split(', ')
        if len(arglist) > 1:
            arglist[1] = "(" + arglist[1]
            arguments = ", ".join(arglist[1:])
        else:
            arguments = "()"

        if len(name + arguments) > maxwidth:
            argstr = _split_line(name, arguments, maxwidth)
        else:
            argstr = name + arguments

        print(" " + argstr + "\n", file=output)
        print(inspect.getdoc(object), file=output)

    elif hasattr(object, '__doc__'):
        print(inspect.getdoc(object), file=output)
Example #54
0
    def get_context_data(self, **kwargs):
        model_name = self.kwargs['model_name']
        # Get the model class.
        try:
            app_config = apps.get_app_config(self.kwargs['app_label'])
        except LookupError:
            raise Http404(_("App %(app_label)r not found") % self.kwargs)
        try:
            model = app_config.get_model(model_name)
        except LookupError:
            raise Http404(
                _("Model %(model_name)r not found in app %(app_label)r") %
                self.kwargs)

        opts = model._meta

        title, body, metadata = utils.parse_docstring(model.__doc__)
        if title:
            title = utils.parse_rst(title, 'model', _('model:') + model_name)
        if body:
            body = utils.parse_rst(body, 'model', _('model:') + model_name)

        # Gather fields/field descriptions.
        fields = []
        for field in opts.fields:
            # ForeignKey is a special case since the field will actually be a
            # descriptor that returns the other object
            if isinstance(field, models.ForeignKey):
                data_type = field.remote_field.model.__name__
                app_label = field.remote_field.model._meta.app_label
                verbose = utils.parse_rst(
                    (_("the related `%(app_label)s.%(data_type)s` object") % {
                        'app_label': app_label,
                        'data_type': data_type,
                    }),
                    'model',
                    _('model:') + data_type,
                )
            else:
                data_type = get_readable_field_data_type(field)
                verbose = field.verbose_name
            fields.append({
                'name': field.name,
                'data_type': data_type,
                'verbose': verbose or '',
                'help_text': field.help_text,
            })

        # Gather many-to-many fields.
        for field in opts.many_to_many:
            data_type = field.remote_field.model.__name__
            app_label = field.remote_field.model._meta.app_label
            verbose = _("related `%(app_label)s.%(object_name)s` objects") % {
                'app_label': app_label,
                'object_name': data_type,
            }
            fields.append({
                'name':
                "%s.all" % field.name,
                "data_type":
                'List',
                'verbose':
                utils.parse_rst(
                    _("all %s") % verbose, 'model',
                    _('model:') + opts.model_name),
            })
            fields.append({
                'name':
                "%s.count" % field.name,
                'data_type':
                'Integer',
                'verbose':
                utils.parse_rst(
                    _("number of %s") % verbose, 'model',
                    _('model:') + opts.model_name),
            })

        methods = []
        # Gather model methods.
        for func_name, func in model.__dict__.items():
            if inspect.isfunction(func):
                try:
                    for exclude in MODEL_METHODS_EXCLUDE:
                        if func_name.startswith(exclude):
                            raise StopIteration
                except StopIteration:
                    continue
                verbose = func.__doc__
                if verbose:
                    verbose = utils.parse_rst(utils.trim_docstring(verbose),
                                              'model',
                                              _('model:') + opts.model_name)
                # If a method has no arguments, show it as a 'field', otherwise
                # as a 'method with arguments'.
                if func_has_no_args(func) and not func_accepts_kwargs(
                        func) and not func_accepts_var_args(func):
                    fields.append({
                        'name': func_name,
                        'data_type': get_return_data_type(func_name),
                        'verbose': verbose or '',
                    })
                else:
                    arguments = get_func_full_args(func)
                    print_arguments = arguments
                    # Join arguments with ', ' and in case of default value,
                    # join it with '='. Use repr() so that strings will be
                    # correctly displayed.
                    print_arguments = ', '.join([
                        '='.join(
                            list(arg_el[:1]) + [repr(el) for el in arg_el[1:]])
                        for arg_el in arguments
                    ])
                    methods.append({
                        'name': func_name,
                        'arguments': print_arguments,
                        'verbose': verbose or '',
                    })

        # Gather related objects
        for rel in opts.related_objects:
            verbose = _("related `%(app_label)s.%(object_name)s` objects") % {
                'app_label': rel.related_model._meta.app_label,
                'object_name': rel.related_model._meta.object_name,
            }
            accessor = rel.get_accessor_name()
            fields.append({
                'name':
                "%s.all" % accessor,
                'data_type':
                'List',
                'verbose':
                utils.parse_rst(
                    _("all %s") % verbose, 'model',
                    _('model:') + opts.model_name),
            })
            fields.append({
                'name':
                "%s.count" % accessor,
                'data_type':
                'Integer',
                'verbose':
                utils.parse_rst(
                    _("number of %s") % verbose, 'model',
                    _('model:') + opts.model_name),
            })
        kwargs.update({
            'name': '%s.%s' % (opts.app_label, opts.object_name),
            'summary': title,
            'description': body,
            'fields': fields,
            'methods': methods,
        })
        return super(ModelDetailView, self).get_context_data(**kwargs)
Example #55
0
 def sanitized(item):
     if inspect.isfunction(getattr(msgpack, item)):
         # Only check objects that exist in the same file as msgpack
         return inspect.getfile(getattr(msgpack, item)) == inspect.getfile(msgpack)
Example #56
0
    def load(self):
        assert not self.loaded

        if self.is_ssl:
            self.ssl = create_ssl_context(
                keyfile=self.ssl_keyfile,
                certfile=self.ssl_certfile,
                ssl_version=self.ssl_version,
                cert_reqs=self.ssl_cert_reqs,
                ca_certs=self.ssl_ca_certs,
                ciphers=self.ssl_ciphers,
            )
        else:
            self.ssl = None

        encoded_headers = [
            (key.lower().encode("latin1"), value.encode("latin1"))
            for key, value in self.headers
        ]
        self.encoded_headers = (
            encoded_headers
            if b"server" in dict(encoded_headers)
            else [(b"server", b"uvicorn")] + encoded_headers
        )  # type: List[Tuple[bytes, bytes]]

        if isinstance(self.http, str):
            self.http_protocol_class = import_from_string(HTTP_PROTOCOLS[self.http])
        else:
            self.http_protocol_class = self.http

        if isinstance(self.ws, str):
            self.ws_protocol_class = import_from_string(WS_PROTOCOLS[self.ws])
        else:
            self.ws_protocol_class = self.ws

        self.lifespan_class = import_from_string(LIFESPAN[self.lifespan])

        try:
            self.loaded_app = import_from_string(self.app)
        except ImportFromStringError as exc:
            logger.error("Error loading ASGI app. %s" % exc)
            sys.exit(1)

        if self.interface == "auto":
            if inspect.isclass(self.loaded_app):
                use_asgi_3 = hasattr(self.loaded_app, "__await__")
            elif inspect.isfunction(self.loaded_app):
                use_asgi_3 = asyncio.iscoroutinefunction(self.loaded_app)
            else:
                call = getattr(self.loaded_app, "__call__", None)
                use_asgi_3 = asyncio.iscoroutinefunction(call)
            self.interface = "asgi3" if use_asgi_3 else "asgi2"

        if self.interface == "wsgi":
            self.loaded_app = WSGIMiddleware(self.loaded_app)
            self.ws_protocol_class = None
        elif self.interface == "asgi2":
            self.loaded_app = ASGI2Middleware(self.loaded_app)

        if self.debug:
            self.loaded_app = DebugMiddleware(self.loaded_app)
        if logger.level <= TRACE_LOG_LEVEL:
            self.loaded_app = MessageLoggerMiddleware(self.loaded_app)
        if self.proxy_headers:
            self.loaded_app = ProxyHeadersMiddleware(
                self.loaded_app, trusted_hosts=self.forwarded_allow_ips
            )

        self.loaded = True
    def check(self,param,annot,value,check_history=''):
        
        # Check list/tuple: type, len=1 (same check) or match (respective matches)
        def check_sequence(kind,kind_text):
            assert isinstance(value,kind), repr(param)+' failed annotation check(wrong type): value = '+repr(value)+\
                                           '\n  was type '+type_as_str(value)+' ...should be type '+kind_text+'\n'+check_history                 
            if len(annot) == 1:
                i = 0
                for v in value:
                    self.check(param,annot[0],v,check_history+kind_text+'['+str(i)+'] check: '+str(annot[0])+'\n')
                    i += 1
            else:
                assert len(annot) == len(value), repr(param)+' failed annotation check(wrong number of elements): value = '+repr(value)+\
                                                 '\n  annotation had '+str(len(annot))+ ' elements'+str(annot)+'\n'+check_history                 
                i = 0
                for a,v in zip(annot,value):
                    self.check(param,a,v, check_history+kind_text+'['+str(i)+'] check: '+str(annot[i])+'\n')
                    i += 1

        # Check dict: type, len=1, all keys (same check) and all values (same check)
        def check_dict():
            assert isinstance(value,dict), repr(param)+' failed annotation check(wrong type): value = '+repr(value)+\
                                           '\n  was type '+type_as_str(value)+' ...should be type dict\n'+check_history                 
            if len(annot) != 1:
                assert False, repr(param)+' annotation inconsistency: dict should have 1 item but had '+str(len(annot))+\
                              '\n  annotation = '+str(annot)+'\n'+check_history                 
            else:
                for annot_k,annot_v in annot.items(): # get first and only
                    pass
                for k,v in value.items():
                    self.check(param,annot_k,k, check_history+'dict key check: '  +str(annot_k)+'\n')
                    self.check(param,annot_v,v, check_history+'dict value check: '+str(annot_v)+'\n')
        
        # Check set/frozenset: type, len=1, all keys (same check) and all values (same check)
        def check_set(kind,kind_text):
            assert isinstance(value,kind), repr(param)+' failed annotation check(wrong type): value = '+repr(value)+\
                                           '\n  was type '+type_as_str(value)+' ...should be type ' +kind_text+'\n'+check_history                 
            if len(annot) != 1:
                assert False, repr(param)+' annotation inconsistency: '+kind_text+' should have 1 value but had '+str(len(annot))+\
                              '\n  annotation = '+str(annot)+'\n'+check_history                 
            else:
                for annot_v in annot:
                    pass
                for v in value:
                    self.check(param,annot_v,v,check_history+kind_text+' value check: '+str(annot_v)+'\n')

        # Check function/lambda: univariate, returns True, throws exception
        def check_predicate():
            assert len(annot.__code__.co_varnames) == 1, repr(param)+' annotation inconsistency: predicate should have 1 parameter but had '+str(len(annot.__code__.co_varnames))+\
                                                        '\n  annotation = '+str(annot)+'\n'+check_history                 
            try:
                worked = annot(value)
            except Exception as message:
                assert False, repr(param)+' annotation predicate('+str(annot)+') raised exception'+\
                              '\n  exception = '+str(message.__class__)[8:-2]+': '+str(message)+'\n'+check_history                 
            else:
                assert worked, repr(param)+' failed annotation check: value = '+repr(value)+\
                               '\n  predicate = '+str(annot)+'\n'+check_history 

        # Check string (as evaluated expression): returns True, throws exception
        def check_str():
            try:
                worked = eval(annot,self._args) 
            except Exception as message:
                    assert False, repr(param)+' annotation check(str predicate: '+repr(annot)+') raised exception'+\
                                  '\n  exception = '+str(message.__class__)[8:-2]+': '+str(message)+'\n'+check_history                 
            else:
                assert worked, repr(param)+' failed annotation check(str predicate: '+repr(annot)+')'\
                               '\n  args for evaluation: '+', '.join([str(k)+'->'+str(v) for k,v in self._args.items()])+'\n'+check_history

        
        # Decode annotation and check it #print('checking',p+':'+str(value),annot)
        if annot == None:
            pass
        elif type(annot) is type:
            assert isinstance(value,annot), repr(param)+' failed annotation check(wrong type): value = '+repr(value)+\
                                        '\n  was type '+type_as_str(value)+' ...should be type '+str(annot)[8:-2]+'\n'+check_history                 
        elif type(annot) is list:       check_sequence(list,'list')
        elif type(annot) is tuple:      check_sequence(tuple,'tuple')
        elif isinstance(annot,dict):    check_dict()
        elif type(annot) is set:        check_set(set,'set')
        elif type(annot) is frozenset:  check_set(frozenset,'frozenset')
        elif inspect.isfunction(annot): check_predicate()
        elif type(annot) is str:        check_str()
        else:
            try:
                annot.__check_annotation__(self.check,param,value,check_history)
            except AttributeError: 
                assert False, repr(param)+' annotation undecipherable: '+str(annot)+'\n'+check_history                 
            except Exception as message:
                if message.__class__ is AssertionError:
                    raise
                else:
                    assert False, repr(param)+' annotation protocol('+str(annot)+') raised exception'+\
                                 '\n  exception = '+str(message.__class__)[8:-2]+': '+str(message)+'\n'+check_history                 
Example #58
0
from inspect import getmembers, isfunction
from yahoo_fin import stock_info

functions_list = [o for o in getmembers(stock_info) if isfunction(o[1])]
Example #59
0
def deprecated(reason):
    """
    This is a decorator which can be used to mark functions
    as deprecated. It will result in a warning being emitted
    when the function is used.

    If no param is passed, a generic message is returned

    :param: reason: The reason for the raised Warning message

    Copied from https://stackoverflow.com/questions/40301488
    """

    if isinstance(reason, str):

        # The @deprecated is used with a 'reason'.
        #
        # .. code-block:: python
        #
        #    @deprecated("please, use another function")
        #    def old_function(x, y):
        #      pass

        def decorator(func1):

            if inspect.isclass(func1):
                fmt1 = "Call to deprecated class {name} ({reason})."
            else:
                fmt1 = "Call to deprecated function {name} ({reason})."

            @functools.wraps(func1)
            def new_func1(*args, **kwargs):
                warnings.simplefilter('always', DeprecationWarning)
                warnings.warn(fmt1.format(name=func1.__name__, reason=reason),
                              category=DeprecationWarning,
                              stacklevel=2)
                warnings.simplefilter('default', DeprecationWarning)
                return func1(*args, **kwargs)

            return new_func1

        return decorator

    elif inspect.isclass(reason) or inspect.isfunction(reason):

        # The @deprecated is used without any 'reason'.
        #
        # .. code-block:: python
        #
        #    @deprecated
        #    def old_function(x, y):
        #      pass

        func2 = reason

        if inspect.isclass(func2):
            fmt2 = "Call to deprecated class {name}."
        else:
            fmt2 = "Call to deprecated function {name}."

        @functools.wraps(func2)
        def new_func2(*args, **kwargs):
            warnings.simplefilter('always', DeprecationWarning)
            warnings.warn(fmt2.format(name=func2.__name__),
                          category=DeprecationWarning,
                          stacklevel=2)
            warnings.simplefilter('default', DeprecationWarning)
            return func2(*args, **kwargs)

        return new_func2

    else:
        raise TypeError(repr(type(reason)))
Example #60
0
def _is_method(f):
    return inspect.isfunction(f) or inspect.ismethod(f)