示例#1
0
    def test_getset(self):
        obj = self.obj_constructor(**self.obj_data[0])
        data_desc = []
        for x in inspect.getmembers(obj.__class__):
            if (inspect.isdatadescriptor(x[1]) or\
                inspect.isgetsetdescriptor(x[1]) or\
               inspect.ismemberdescriptor(x[1])) and not x[0].startswith("__"):
                data_desc.append(x)

        for attr in data_desc:
            #z = getattr(obj, x[0])
            attr_types = self.obj_getset[attr[0]]
            for _type, vals in _iteritems(self.obj_types):
                if _type not in attr_types:
                    with self.assertRaises(TypeError):
                        setattr(obj, attr[0], vals[0])
                else:
                    for val in vals:
                        setattr(obj, attr[0], val)

            with self.assertRaises(TypeError):
                self.assertTrue(delattr(obj, x[0]), x[0])

            setattr(obj, attr[0], self.obj_types[self.obj_getset[attr[0]][0]][0])
            z = getattr(obj, attr[0])
            self.assertTrue(self.obj_types[self.obj_getset[attr[0]][0]][0] == z)
示例#2
0
 def properties(self):
     if self._cls is None:
         return []
     return [name for name,func in inspect.getmembers(self._cls)
             if not name.startswith('_') and
             (func is None or isinstance(func, property) or
              inspect.isgetsetdescriptor(func))]
示例#3
0
def is_class_dict(dct):
    if not isinstance(dct, dict):
        return False
    if '__dict__' not in dct \
       or not inspect.isgetsetdescriptor(dct['__dict__']):
        return False
    return True
示例#4
0
def get_size(obj, seen=None):
    """Recursively finds size of objects in bytes
    https://github.com/bosswissam/pysize/blob/master/pysize.py
    単体では動くが、locals().keys()を渡して回すとエラー
    """
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d):
                    size += get_size(obj.__dict__, seen)
                break
    #以下でtype型を検出したときにTypeError, その他にUnsupportedOperationがRaiseされること有り
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum((get_size(i, seen) for i in obj))
    return size
示例#5
0
def get_size(obj, seen=None):
    """Recursively finds size of objects in bytes"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d):
                    size += get_size(obj.__dict__, seen)
                break
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum((get_size(i, seen) for i in obj))
        
    if hasattr(obj, '__slots__'): # can have __slots__ with __dict__
        size += sum(get_size(getattr(obj, s), seen) for s in obj.__slots__ if hasattr(obj, s))
        
    return size
示例#6
0
 def test_excluding_predicates(self):
     #self.istest(inspect.isbuiltin, 'sys.exit')  # Not valid for Jython
     self.istest(inspect.isbuiltin, '[].append')
     self.istest(inspect.iscode, 'mod.spam.func_code')
     self.istest(inspect.isframe, 'tb.tb_frame')
     self.istest(inspect.isfunction, 'mod.spam')
     self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
     self.istest(inspect.ismethod, 'git.argue')
     self.istest(inspect.ismodule, 'mod')
     self.istest(inspect.istraceback, 'tb')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
     self.istest(inspect.isgenerator, '(x for x in xrange(2))')
     self.istest(inspect.isgeneratorfunction, 'generator_function_example')
     if hasattr(types, 'GetSetDescriptorType'):
         self.istest(inspect.isgetsetdescriptor,
                     'type(tb.tb_frame).f_locals')
     else:
         self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
     if hasattr(types, 'MemberDescriptorType'):
         # Not valid for Jython
         # self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
         pass
     else:
         self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
示例#7
0
文件: __test.py 项目: glensc/libcomps
    def test_union1(self):
        obj1 = self.__union_prep1()
        obj2 = self.obj_constructor(**self.obj_data[1])
        obj = obj1 + obj2
        for x in inspect.getmembers(obj.__class__):
            if (inspect.isdatadescriptor(x[1]) or\
               inspect.isgetsetdescriptor(x[1]) or\
               inspect.ismemberdescriptor(x[1])) and\
               not x[0].startswith("__") and\
               not inspect.ismethod(x[1]):
                if getattr(obj2, x[0]):
                    self.assertTrue(getattr(obj, x[0]) == getattr(obj2, x[0]))
                else:
                    self.assertTrue(getattr(obj, x[0]) == getattr(obj1, x[0]))

        for member, v in _iteritems(self.obj_list_members):
            _list = getattr(obj, member)
            self.assertTrue(len(_list) == 4)

        for member, v in _iteritems(self.obj_dict_members):
            _d1 = dict([(k, v) for k, v in _iteritems(getattr(obj1, member))])
            _d2 = dict([(k, v) for k, v in _iteritems(getattr(obj2, member))])
            _d1.update(_d2)
            _dict = getattr(obj, member)
            _d = dict([(k,v) for k,v in _iteritems(_dict)])
            self.assertTrue(_d == _d1)
示例#8
0
文件: pycode.py 项目: ownport/pycode
def struct_code(thing):
    """ returns code structure, given an object or a path to an object."""
    result = dict()
    obj, result['name'] = resolve(thing)    
    result['type'], name = describe(obj)
    module = inspect.getmodule(obj)
    result['module_name'] = module.__name__
    result['module_doc'] = module.__doc__

    try:
        result['file'] = inspect.getabsfile(obj)
    except TypeError:
        result['file'] = '(built-in)'
    
    if type(obj) is _OLD_INSTANCE_TYPE:
        # If the passed object is an instance of an old-style class,
        # document its available methods instead of its value.
        obj = obj.__class__
    elif not (inspect.ismodule(obj) or
              inspect.isclass(obj) or
              inspect.isroutine(obj) or
              inspect.isgetsetdescriptor(obj) or
              inspect.ismemberdescriptor(obj) or
              isinstance(obj, property)):
        # If the passed object is a piece of data or an instance,
        # document its available methods instead of its value.
        obj = type(obj)
    code_struct = PyCodeStruct()
    result['struct'] = code_struct.struct(obj, result['name'])
    return result
示例#9
0
def print_offsets():
    ''' Print the attribute names, sizes and offsets in the C structure
    
    Assuming that the sizes are correct and add up to an offset of 4100 bytes, 
    everything should add up correctly. This information was taken from the 
    WinSpec 2.6 Spectroscopy Software User Manual version 2.6B, page 251.

    If this table doesn't add up, something changed in the definitions of the 
    datatype widths. Fix this in winspec.structs file and let me know!
    '''

    import inspect, re

    A = Header()

    for i in [Header, AxisCalibration, ROIinfo]:
        fields = []

        print('\n{:30s}[{:4s}]\tsize'.format(repr(i), 'offs'))
        
        for name,obj in inspect.getmembers(i):
            if inspect.isdatadescriptor(obj) and not inspect.ismemberdescriptor(obj) \
                and not inspect.isgetsetdescriptor(obj):
                
                fields.append((name, obj))

        fields = sorted(fields, key=lambda x: x[1].offset)

        for name, obj in fields:
            print('{:30s}[{:4d}]\t{:4d}'.format(name, obj.size, obj.offset))
示例#10
0
    def import_from(s1, module):
        syms = inspect.getmembers(module)
        str_syms = dir(module)
        name_as = ""
        if len(s1) == 4:
            name_as = s1[3][1]


        if not (s1[1][1] in str_syms):
            print("import error")
            exit()
        else:
            for sym in syms:
                if sym[0] == s1[1][1]:
                    if inspect.isfunction(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Function(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        if len(s1) == 4:
                            GLOBAL_SYMBOL_LIST.append(Class(name_as))
                        else:
                            GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                    else:
                        print(sym[0])
示例#11
0
def ignore_frames(x):
    import inspect
    import types

    l = []
    if inspect.isclass(x):
        # must be a class object
        l.extend([x.__mro__, x.__dict__])

        if hasattr(x, '__weakref__'):
            l.extend([x.__weakref__])

        for member in x.__dict__.values():
            # ignore attributes.
            if inspect.isgetsetdescriptor(member):
                l.append(member)

    # ignore the module and module dict
    if inspect.ismodule(x):
        l.extend([x, x.__dict__])

    # ignore a frame; this will not work with multi-threaded applications
    # use refcycle in that case for live applications
    if inspect.isframe(x):
        # this can't detect multi-threaded.
        l.extend([x])

    return l
示例#12
0
 def import_name(s1):
     if s1[0] in NON_TERMINAL:
         if s1[0] in NON_TERMINAL and s1[0] == 286:
             dot_name = ""
             module_name = ""
             for name in s1[1]:
                 if not isinstance(name, int):
                     module_name += name[1]
             if len(s1) == 2:
                 dot_name = module_name
             elif len(s1) == 4:
                 dot_name = s1[3][1]
             try:
                 module = importlib.import_module(module_name)
             except ImportError:
                 print("Import Error, No module named " + module_name)
                 exit()
             new_module = Module(module_name)
             new_module.SYMBOL_LIST = []
             syms = inspect.getmembers(module)
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     #new_module.SYMBOL_LIST.append(Function(dot_name+'.' + sym[0]))
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     new_module.SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     new_module.SYMBOL_LIST.append(Class(sym[0], [], []))
                 else:
                     print(sym[0])
                 self.local_names.append(new_module)
         else:
             for j in range(1,len(s1)):
                 import_name(s1[j])
	def handleSlots(self, obj):
		pyobj = obj.pyobj

		flat = self.flatTypeDict(type(pyobj))

		# Relies on type dictionary being flattened.
		for name, member in flat.iteritems():
			assert not isinstance(name, program.AbstractObject), name
			assert not isinstance(member, program.AbstractObject), member

			# TODO Directly test for slot wrapper?
			# TODO slot wrapper for methods?
			isMember = inspect.ismemberdescriptor(member)

			# HACK some getsets may as well be members
			isMember |= inspect.isgetsetdescriptor(member) and member in self.getsetMember

			if isMember:
				try:
					value = member.__get__(pyobj, type(pyobj))
				except:
					print "Error getting attribute %s" % name
					print "obj", pyobj
					for k, v in inspect.getmembers(pyobj):
						print '\t', k, repr(v)
					raise
				mangledName = self.compiler.slots.uniqueSlotName(member)
				obj.addSlot(self.__getObject(mangledName), self.__getObject(value))
示例#14
0
 def test_excluding_predicates(self):
     #XXX: Jython's PySystemState needs more work before this
     #will be doable.
     if not test_support.is_jython:
         self.istest(inspect.isbuiltin, 'sys.exit')
     self.istest(inspect.isbuiltin, '[].append')
     self.istest(inspect.isclass, 'mod.StupidGit')
     self.istest(inspect.iscode, 'mod.spam.func_code')
     self.istest(inspect.isframe, 'tb.tb_frame')
     self.istest(inspect.isfunction, 'mod.spam')
     self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
     self.istest(inspect.ismethod, 'git.argue')
     self.istest(inspect.ismodule, 'mod')
     self.istest(inspect.istraceback, 'tb')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
     if hasattr(types, 'GetSetDescriptorType'):
         self.istest(inspect.isgetsetdescriptor,
                     'type(tb.tb_frame).f_locals')
     #XXX: This detail of PyFrames is not yet supported in Jython
     elif not test_support.is_jython:
         self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
     if hasattr(types, 'MemberDescriptorType'):
         self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
     else:
         self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
示例#15
0
 def print_variable_type(self, objType):
     default_vars=["__builtins__", "__doc__","__path__", "__cached__", "__file__", "__name__", "__package__", "__version__"]
     self.dprint("[ %s ]" %objType.__name__)
     self.indent();self.indent()
     for ModObj in self.Modules:
         for name in dir(ModObj):
             obj = getattr(ModObj, name)
             #print(name, ":", type(obj))
             if not (inspect.isclass(obj) or
                 inspect.isfunction(obj)  or
                 inspect.isroutine(obj)  or
                 inspect.isfunction(obj)  or
                 inspect.isgeneratorfunction(obj)  or
                 inspect.isgenerator(obj)  or
                 inspect.istraceback(obj)  or
                 inspect.isframe(obj)      or
                 inspect.iscode(obj)       or
                 inspect.isabstract(obj)   or
                 inspect.ismethoddescriptor(obj)  or
                 inspect.isdatadescriptor(obj)    or
                 inspect.isgetsetdescriptor(obj)  or
                 inspect.ismemberdescriptor(obj)  or
                 inspect.isbuiltin(obj)):
                 if name not in default_vars:
                     if type(obj) is  objType:
                         ObjName = ModObj.__name__ + '.' + name
                         self.dprint("%s" %ObjName)
     self.dedent();self.dedent()
示例#16
0
def test_headers():
    ''' Print the attribute names, sizes and offsets in the C structure
Assuming that the sizes are correct and add up to an offset of 4100 bytes,
everything should add up correctly. This information was taken from the
WinSpec 2.6 Spectroscopy Software User Manual version 2.6B, page 251.

If this table doesn't add up, something changed in the definitions of the
datatype widths. Fix this in winspec.structs file and let me know!
'''

    import inspect, re

    A = Header()
    
    for i in [Header, AxisCalibration, ROIinfo]:
        fields = []

        print '\n{:30s}[{:4s}]\tsize'.format(i, 'offs')
        
        for name,obj in inspect.getmembers(i):
            if inspect.isdatadescriptor(obj) and not inspect.ismemberdescriptor(obj) \
                and not inspect.isgetsetdescriptor(obj):
                
                fields.append((name, obj))

        fields.sort(key=lambda x: re.search('(?<=ofs=)([0-9]+)', str(x[1])).group(0),
                cmp=lambda x,y: cmp(int(x),int(y))); fields

        for name, obj in fields:
            print '{:30s}[{:4d}]\t{:4d}'.format(name, obj.size, obj.offset)
示例#17
0
def describe_module(module, kind, constants=[]):
    """
    Describe the module object passed as argument
    including its classes and functions.
    """

    module_name = module.__name__

    if kind == object_types.LIBRARY:
        klass = Library(module_name)
    else:
        klass = Module(module_name, kind)
        
    klass.docs = getdoc(module)
    klass.comments = getcomments(module)
    
    klass.filename = module.__file__
    inheritance_diagram = []
    
    count = 0
   
    for name in dir(module):
        
        if name in EXCLUDED_ATTRS:
            continue
        
        obj = getattr(module, name)

        if ismodule(obj):
            continue

        if ismemberdescriptor(obj) or isgetsetdescriptor(obj):
            continue
        
        if isclass(obj):
            count += 1
            describe_class(obj, klass, module_name, constants)

            if obj.__module__ == module.__name__:
                inheritance_diagram.append(obj)
            
        elif isbuiltin(obj):
            count += 1
        elif ismethod(obj) or isfunction(obj) or ismethoddescriptor(obj) or \
             isinstance(obj, types.MethodType):
            count +=1
            describe_func(obj, klass, module_name)
        else:
            attribute = Attribute(module_name + '.' + name, type(obj), obj)
            klass.Add(attribute)

            if constants:
                attribute.is_redundant = name not in constants

    if kind not in [object_types.PACKAGE, object_types.LIBRARY]:
        if inheritance_diagram and len(inheritance_diagram) < 20:
            klass.inheritance_diagram = inheritance.InheritanceDiagram(inheritance_diagram, klass)

    return klass, count
示例#18
0
    def import_name(s1):
        if s1[0] in NON_TERMINAL:
            if s1[0] in NON_TERMINAL and s1[0] == 286:
                dot_name = ""
                module_name = ""
                for name in s1[1]:
                    if type(name) != type(1):
                        module_name += name[1]
                if len(s1) == 2:
                    dot_name = module_name
                elif len(s1) == 4:
                    dot_name = s1[3][1]
                try:
                    module = importlib.import_module(module_name)
                except ImportError:
                    print("Import Error, No module named " + module_name)
                    exit()

                a = dir(module)
                syms = inspect.getmembers(module)
                for sym in syms:
                    if inspect.isfunction(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.isbuiltin(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.ismethod(sym[1]):
                        pass
                    elif inspect.isgeneratorfunction:
                        GLOBAL_SYMBOL_LIST.append(Function(dot_name + "." + sym[0]))
                    elif inspect.isgenerator(sym[1]):
                        pass
                    elif inspect.istraceback(sym[1]):
                        pass
                    elif inspect.isframe(sym[1]):
                        pass
                    elif inspect.iscode(sym[1]):
                        pass
                    elif inspect.isroutine(sym[1]):
                        pass
                    elif inspect.isabstract(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isdatadescriptor(sym[1]):
                        pass
                    elif inspect.isgetsetdescriptor(sym[1]):
                        pass
                    elif inspect.ismemberdescriptor(sym[1]):
                        pass
                    elif inspect.isclass(sym[1]):
                        GLOBAL_SYMBOL_LIST.append(Class(dot_name + "." + sym[0], [], []))
                    else:
                        print(sym[0])
            else:
                for j in range(1, len(s1)):
                    import_name(s1[j])
示例#19
0
 def parse_import(st):
     if st[0] == 283:
         import_name(st[2])
     elif st[0] == 284:
         module_name = ""
         if type(st[2]) != type(1):
             for name in st[2]:
                 if type(name) != type(1):
                     module_name += name[1]
         try:
             module = importlib.import_module(module_name)
         except ImportError:
             print("Import Error, No module named " + module_name)
             exit()
         if len(st)==5 and st[4][1] == "*":
             syms = inspect.getmembers(module)
             str_syms = dir(module)
             name_as = ""
             for sym in syms:
                 if inspect.isfunction(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isbuiltin(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.ismethod(sym[1]):
                     pass
                 elif inspect.isgeneratorfunction:
                     GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
                 elif inspect.isgenerator(sym[1]):
                     pass
                 elif inspect.istraceback(sym[1]):
                     pass
                 elif inspect.isframe(sym[1]):
                     pass
                 elif inspect.iscode(sym[1]):
                     pass
                 elif inspect.isroutine(sym[1]):
                     pass
                 elif inspect.isabstract(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isdatadescriptor(sym[1]):
                     pass
                 elif inspect.isgetsetdescriptor(sym[1]):
                     pass
                 elif inspect.ismemberdescriptor(sym[1]):
                     pass
                 elif inspect.isclass(sym[1]):
                     GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
                 else:
                     print(sym[0])
         else:
             for counter in range(len(st[4])):
                 if not isinstance(st[4][counter],int) and st[4][counter][0] == 285:
                     import_from(st[4][counter], module)
示例#20
0
文件: util.py 项目: Michael0x2a/mypy
def get_class_descriptors(cls: 'Type[object]') -> Sequence[str]:
    import inspect  # Lazy import for minor startup speed win
    # Maintain a cache of type -> attributes defined by descriptors in the class
    # (that is, attributes from __slots__ and C extension classes)
    if cls not in fields_cache:
        members = inspect.getmembers(
            cls,
            lambda o: inspect.isgetsetdescriptor(o) or inspect.ismemberdescriptor(o))
        fields_cache[cls] = [x for x, y in members if x != '__weakref__' and x != '__dict__']
    return fields_cache[cls]
示例#21
0
    def _str_member_list(self, name):
        """
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        """
        out = []
        if self[name]:
            out += ['.. rubric:: %s' % name, '']
            prefix = getattr(self, '_name', '')

            if prefix:
                prefix = '~%s.' % prefix

            # Lines that are commented out are used to make the
            # autosummary:: table. Since SymPy does not use the
            # autosummary:: functionality, it is easiest to just comment it
            # out.
            # autosum = []
            others = []
            for param, param_type, desc in self[name]:
                param = param.strip()

                # Check if the referenced member can have a docstring or not
                param_obj = getattr(self._obj, param, None)
                if not (callable(param_obj)
                        or isinstance(param_obj, property)
                        or inspect.isgetsetdescriptor(param_obj)):
                    param_obj = None

                # if param_obj and (pydoc.getdoc(param_obj) or not desc):
                #     # Referenced object has a docstring
                #     autosum += ["   %s%s" % (prefix, param)]
                # else:
                others.append((param, param_type, desc))

            # if autosum:
            #     out += ['.. autosummary::']
            #     if self.class_members_toctree:
            #         out += ['   :toctree:']
            #     out += [''] + autosum

            if others:
                maxlen_0 = max(3, max([len(x[0]) for x in others]))
                hdr = sixu("=")*maxlen_0 + sixu("  ") + sixu("=")*10
                fmt = sixu('%%%ds  %%s  ') % (maxlen_0,)
                out += ['', '', hdr]
                for param, param_type, desc in others:
                    desc = sixu(" ").join(x.strip() for x in desc).strip()
                    if param_type:
                        desc = "(%s) %s" % (param_type, desc)
                    out += [fmt % (param.strip(), desc)]
                out += [hdr]
            out += ['']
        return out
示例#22
0
    def _str_member_list(self, name):
        """
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        """
        out = []
        if self[name]:
            out += ['.. rubric:: %s' % name, '']
            prefix = getattr(self, '_name', '')

            if prefix:
                prefix = '~%s.' % prefix

            autosum = []
            others = []
            for param, param_type, desc in self[name]:
                param = param.strip()

                # Check if the referenced member can have a docstring or not
                param_obj = getattr(self._obj, param, None)
                if not (callable(param_obj)
                        or isinstance(param_obj, property)
                        or inspect.isgetsetdescriptor(param_obj)):
                    param_obj = None

                if param_obj and (pydoc.getdoc(param_obj) or not desc):
                    # Referenced object has a docstring
                    autosum += ["   %s%s" % (prefix, param)]
                else:
                    others.append((param, param_type, desc))

            if autosum:
                out += ['.. currentmodule:: '
                        + self._obj.__module__ + '.' + self._obj.__name__]
                out += []
                out += ['.. autosummary::']
                if self.class_members_toctree:
                    out += ['   :toctree:']
                out += [''] + autosum

            if others:
                maxlen_0 = max(3, max([len(x[0]) + 4 for x in others]))
                hdr = sixu("=") * maxlen_0 + sixu("  ") + sixu("=") * 10
                fmt = sixu('%%%ds  %%s  ') % (maxlen_0,)
                out += ['', '', hdr]
                for param, param_type, desc in others:
                    desc = sixu(" ").join(x.strip() for x in desc).strip()
                    if param_type:
                        desc = "(%s) %s" % (param_type, desc)
                    out += [fmt % ("**" + param.strip() + "**", desc)]
                out += [hdr]
            out += ['']
        return out
示例#23
0
文件: utils.py 项目: adamcobabe/pymel
def helpNonVerbose(thing, title='Python Library Documentation: %s', forceload=0):
    """
    Utility method to return python help in the form of a string
    (based on the code in pydoc.py)
    Note: only a string (including unicode) should be passed in for "thing"
    """
    
    import pydoc as pydocs
    import inspect
    import string

    result=""

    # Important for converting an incoming c++ unicode character string!
    thingStr=str(thing)

    """Display text documentation, given an object or a path to an object."""
    try:
        # Possible two-stage object resolution!
        # Sometimes we get docs for strings, other times for objects
        #
        try:
            object, name = pydocs.resolve(thingStr, forceload)
        except:
            # Get an object from a string
            thingObj=eval(thingStr)
            object, name = pydocs.resolve(thingObj, forceload)
        desc = pydocs.describe(object)
        module = inspect.getmodule(object)
        if name and '.' in name:
            desc += ' in ' + name[:name.rfind('.')]
        elif module and module is not object:
            desc += ' in module ' + module.__name__
        if not (inspect.ismodule(object) or
                inspect.isclass(object) or
                inspect.isroutine(object) or
                inspect.isgetsetdescriptor(object) or
                inspect.ismemberdescriptor(object) or
                isinstance(object, property)):
            # If the passed object is a piece of data or an instance,
            # document its available methods instead of its value.
            object = type(object)
            desc += ' object'
        text = pydocs.TextDoc()
        result=pydocs.plain(title % desc + '\n\n' + text.document(object, name))
        
        # Remove multiple empty lines
        result = [ line for line in result.splitlines() if line.strip() ]
        result = string.join(result,"\n")

    except:
        pass

    return result
示例#24
0
    def _str_member_list(self, name):
        """
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        """
        out = []
        if self[name] and self.class_members_list:
            out += ['.. rubric:: %s' % name, '']
            prefix = getattr(self, '_name', '')

            if prefix:
                prefix = '~%s.' % prefix

            autosum = []
            others = []
            for param, param_type, desc in self[name]:
                param = param.strip()

                # Check if the referenced member can have a docstring or not
                param_obj = getattr(self._obj, param, None)
                if not (callable(param_obj)
                        or isinstance(param_obj, property)
                        or inspect.isgetsetdescriptor(param_obj)):
                    param_obj = None

                # pandas HACK - do not exclude attributes wich are None
                # if param_obj and (pydoc.getdoc(param_obj) or not desc):
                #     # Referenced object has a docstring
                #     autosum += ["   %s%s" % (prefix, param)]
                # else:
                #     others.append((param, param_type, desc))
                autosum += ["   %s%s" % (prefix, param)]

            if autosum:
                out += ['.. autosummary::']
                if self.class_members_toctree:
                    out += ['   :toctree:']
                out += [''] + autosum

            if others:
                maxlen_0 = max(3, max(len(x[0]) for x in others))
                hdr = sixu("=")*maxlen_0 + sixu("  ") + sixu("=")*10
                fmt = sixu('%%%ds  %%s  ') % (maxlen_0,)
                out += ['', hdr]
                for param, param_type, desc in others:
                    desc = sixu(" ").join(x.strip() for x in desc).strip()
                    if param_type:
                        desc = "(%s) %s" % (param_type, desc)
                    out += [fmt % (param.strip(), desc)]
                out += [hdr]
            out += ['']
        return out
示例#25
0
文件: pycode.py 项目: ownport/pycode
 def struct(self, obj, name=None, *args):
 
     """Generate structure for an object."""
     
     args = (obj, name) + args
     if inspect.isgetsetdescriptor(obj): return self.struct_data(*args)
     if inspect.ismemberdescriptor(obj): return self.struct_data(*args)
     if inspect.ismodule(obj): return self.struct_module(*args)
     if inspect.isclass(obj): return self.struct_class(*args)
     if inspect.isroutine(obj): return self.struct_routine(*args)
     if isinstance(obj, property): return self.struct_property(*args)
     return self.struct_date(*args)
示例#26
0
    def __init__(self, name, module, obj, *, docstring=None):
        assert isinstance(obj, type)
        if docstring is None:
            init_doc = inspect.getdoc(obj.__init__) or ''
            if init_doc == object.__init__.__doc__:
                init_doc = ''
            docstring = ((inspect.getdoc(obj) or '') + '\n\n' +
                         init_doc).strip()
        super().__init__(name, module, obj, docstring=docstring)

        self.doc = {}
        """A mapping from identifier name to a `pdoc.Doc` objects."""

        self.doc.update(_var_docstrings(self))

        public_objs = [
            (name, inspect.unwrap(obj))
            for name, obj in inspect.getmembers(self.obj)
            # Filter only *own* members. The rest are inherited
            # in Class._fill_inheritance()
            if name in self.obj.__dict__ and _is_public(name)
        ]
        index = list(self.obj.__dict__).index
        public_objs.sort(key=lambda i: index(i[0]))

        # Convert the public Python objects to documentation objects.
        for name, obj in public_objs:
            if name in self.doc and self.doc[name].docstring:
                continue
            if inspect.isroutine(obj):
                self.doc[name] = Function(
                    name,
                    self.module,
                    obj,
                    cls=self,
                    method=not self._method_type(self.obj, name))
            elif (inspect.isdatadescriptor(obj)
                  or inspect.isgetsetdescriptor(obj)
                  or inspect.ismemberdescriptor(obj)):
                self.doc[name] = Variable(name,
                                          self.module,
                                          inspect.getdoc(obj),
                                          obj=getattr(obj, 'fget', obj),
                                          cls=self,
                                          instance_var=True)
            else:
                self.doc[name] = Variable(
                    name,
                    self.module,
                    docstring=isinstance(obj, type) and inspect.getdoc(obj)
                    or "",
                    cls=self,
                    instance_var=name in getattr(self.obj, "__slots__", ()))
示例#27
0
文件: core.py 项目: joricomico/AWCpy
 def _clonable(of):
     sets = of.__dict__.copy()
     sets.update(type(of).__dict__.copy())
     final = sets.copy()
     for key in sets:
         v = sets[key]
         if isfunction(v) or ismethod(v) or isgeneratorfunction(v) or isgenerator(v) \
         or isroutine(v) or isabstract(v) or isclass(v) or ismodule(v) or istraceback(v) \
         or isframe(v) or iscode(v) or isbuiltin(v) or ismethoddescriptor(v) \
         or isdatadescriptor(v) or isgetsetdescriptor(v) or ismemberdescriptor(v) \
         or v is None or v == '__main__' or key == '__module__': final.pop(key)
     return final
示例#28
0
文件: util.py 项目: swetavooda/mypy
def get_class_descriptors(cls: 'Type[object]') -> Sequence[str]:
    import inspect  # Lazy import for minor startup speed win
    # Maintain a cache of type -> attributes defined by descriptors in the class
    # (that is, attributes from __slots__ and C extension classes)
    if cls not in fields_cache:
        members = inspect.getmembers(
            cls, lambda o: inspect.isgetsetdescriptor(o) or inspect.
            ismemberdescriptor(o))
        fields_cache[cls] = [
            x for x, y in members if x != '__weakref__' and x != '__dict__'
        ]
    return fields_cache[cls]
示例#29
0
def pyi_file(obj, indent=""):
    string = ""
    if inspect.ismodule(obj):
        string += GENERATED_COMMENT
        members = get_module_members(obj)
        for member in members:
            string += pyi_file(member, indent)

    elif inspect.isclass(obj):
        indent += INDENT
        mro = inspect.getmro(obj)
        if len(mro) > 2:
            inherit = f"({mro[1].__name__})"
        else:
            inherit = ""
        string += f"class {obj.__name__}{inherit}:\n"

        body = ""
        if obj.__doc__:
            body += f'{indent}"""\n{indent}{do_indent(obj.__doc__, indent)}\n{indent}"""\n'

        fns = inspect.getmembers(obj, fn_predicate)

        # Init
        if obj.__text_signature__:
            body += f"{indent}def __init__{obj.__text_signature__}:\n"
            body += f"{indent+INDENT}pass\n"
            body += "\n"

        for (name, fn) in fns:
            body += pyi_file(fn, indent=indent)

        if not body:
            body += f"{indent}pass\n"

        string += body
        string += "\n\n"

    elif inspect.isbuiltin(obj):
        string += f"{indent}@staticmethod\n"
        string += function(obj, indent)

    elif inspect.ismethoddescriptor(obj):
        string += function(obj, indent)

    elif inspect.isgetsetdescriptor(obj):
        # TODO it would be interesing to add the setter maybe ?
        string += f"{indent}@property\n"
        string += function(obj, indent, text_signature="(self)")
    else:
        raise Exception(f"Object {obj} is not supported")
    return string
示例#30
0
def get_callables(obj, if_of = None, check_where_defined=False):
    publics = (getattr(obj, x) for x in dir(obj) if is_public(x))
    callables = (x for x in publics if callable(x) or isgetsetdescriptor(x))

    if check_where_defined:
        callables = (c for c in callables if ( 'pygame' in c.__module__ or
                    ('__builtin__' == c.__module__ and isclass(c)) )
                    and REAL_HOMES.get(c, 0) in (obj, 0))

    if if_of:
        callables = (x for x in callables if if_of(x)) # isclass, ismethod etc

    return set(callables)
示例#31
0
def is_data_object(obj):
    """Return True of obj holds data

    This routine is used to distinguish objects we should show help
    for (like modules, classes, methods, and so forth) from other
    types of object.
    
    """

    # Test borrowed from pydoc.py
    return not (inspect.ismodule(obj) or inspect.isclass(obj)
                or inspect.isroutine(obj) or inspect.isgetsetdescriptor(obj) or
                inspect.ismemberdescriptor(obj) or isinstance(obj, property))
示例#32
0
 def _clonable(of):
     sets = of.__dict__.copy()
     sets.update(type(of).__dict__.copy())
     final = sets.copy()
     for key in sets:
         v = sets[key]
         if isfunction(v) or ismethod(v) or isgeneratorfunction(v) or isgenerator(v) \
         or isroutine(v) or isabstract(v) or isclass(v) or ismodule(v) or istraceback(v) \
         or isframe(v) or iscode(v) or isbuiltin(v) or ismethoddescriptor(v) \
         or isdatadescriptor(v) or isgetsetdescriptor(v) or ismemberdescriptor(v) \
         or v is None or v == '__main__' or key == '__module__':
             final.pop(key)
     return final
示例#33
0
def get_callables(obj, if_of = None, check_where_defined=False):
    publics = (getattr(obj, x) for x in dir(obj) if is_public(x))
    callables = (x for x in publics if callable(x) or isgetsetdescriptor(x))

    if check_where_defined:
        callables = (c for c in callables if ( 'pygame' in c.__module__ or
                    ('__builtin__' == c.__module__ and isclass(c)) )
                    and REAL_HOMES.get(c, 0) in (obj, 0))

    if if_of:
        callables = (x for x in callables if if_of(x)) # isclass, ismethod etc

    return set(callables)
示例#34
0
def resolve_from_type(obj, name):
    raw = type_lookup(obj, name)

    # Resolve any descriptors.
    if (inspect.ismethoddescriptor(raw) or inspect.isgetsetdescriptor(raw)
            or inspect.ismemberdescriptor(raw)):
        getter = raw.__get__
    else:
        try:
            getter = resolve_from_type(raw, '__get__')
        except AttributeError:
            # Not a descriptor.
            return raw
    return getter(obj, type(obj))
示例#35
0
 def _update_hash_for_members_of_obj(self, hash_accumulator, obj):
     members_to_hash = [
         m_value for (m_name, m_value) in inspect.getmembers(obj) if not (
             inspect.isgetsetdescriptor(m_value)
             # We ignore __module__ because it can vary depending on whether
             # that class's module was run as __main__.
             or m_name in
             {"__class__", "__dict__", "__members__", "__module__"})
     ]
     add_to_hash(
         hash_accumulator,
         type_prefix=TypePrefix.HASH,
         obj_bytes=self._check_and_hash(members_to_hash),
     )
示例#36
0
def visit_member(parent_name, member):
    cur_name = ".".join([parent_name, member.__name__])
    if inspect.isclass(member):
        queue_dict(member, cur_name)
        for name, value in inspect.getmembers(member):
            if hasattr(value, '__name__') and (not name.startswith("_") or
                                               name == "__init__"):
                visit_member(cur_name, value)
    elif callable(member):
        queue_dict(member, cur_name)
    elif inspect.isgetsetdescriptor(member):
        return
    else:
        raise RuntimeError("Unsupported generate signature of member, type {0}".
                           format(str(type(member))))
示例#37
0
    def _str_member_list(self, name):
        """
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        """
        out = []
        if self[name]:
            out += ['.. rubric:: %s' % name, '']
            prefix = getattr(self, '_name', '')

            if prefix:
                prefix = '~%s.' % prefix

            autosum = []
            others = []
            for param, param_type, desc in self[name]:
                param = param.strip()

                # Check if the referenced member can have a docstring or not
                param_obj = getattr(self._obj, param, None)
                if not (callable(param_obj) or isinstance(param_obj, property)
                        or inspect.isgetsetdescriptor(param_obj)):
                    param_obj = None

                if param_obj and (pydoc.getdoc(param_obj) or not desc):
                    # Referenced object has a docstring
                    autosum += ["   %s%s" % (prefix, param)]
                else:
                    others.append((param, param_type, desc))

            if autosum:
                out += ['.. autosummary::', '   :toctree:', '']
                out += autosum

            if others:
                maxlen_0 = max(3, max([len(x[0]) for x in others]))
                hdr = u"=" * maxlen_0 + u"  " + u"=" * 10
                fmt = u'%%%ds  %%s  ' % (maxlen_0, )
                out += ['', hdr]
                for param, param_type, desc in others:
                    desc = u" ".join(x.strip() for x in desc).strip()
                    if param_type:
                        desc = "(%s) %s" % (param_type, desc)
                    out += [fmt % (param.strip(), desc)]
                out += [hdr]
            out += ['']
        return out
示例#38
0
def render(thing, hlevel=1, forceload=0):
    """ Render Markdown documentation, given an object or a path to an object. """
    object, name = pydoc.resolve(thing, forceload)
    if type(object) is pydoc._OLD_INSTANCE_TYPE:
        # If the passed object is an instance of an old-style class,
        # document its available methods instead of its value.
        object = object.__class__
    elif not (inspect.ismodule(object) or inspect.isclass(object) or
              inspect.isroutine(object) or inspect.isgetsetdescriptor(object)
              or inspect.ismemberdescriptor(object)
              or isinstance(object, property)):
        # If the passed object is a piece of data or an instance,
        # document its available methods instead of its value.
        object = type(object)
    mddoc = MdDoc(hlevel=hlevel)
    return mddoc.document(object, name)
示例#39
0
def is_data_object(obj):
    """Return True of obj holds data

    This routine is used to distinguish objects we should show help
    for (like modules, classes, methods, and so forth) from other
    types of object.
    
    """
    
    # Test borrowed from pydoc.py
    return not (inspect.ismodule(obj) or
                inspect.isclass(obj) or
                inspect.isroutine(obj) or
                inspect.isgetsetdescriptor(obj) or
                inspect.ismemberdescriptor(obj) or
                isinstance(obj, property))
示例#40
0
文件: json.py 项目: barryloper/dorthy
def _is_visible_type(attribute):
    return not (
        inspect.isfunction(attribute)
        or inspect.ismethod(attribute)
        or inspect.isbuiltin(attribute)
        or inspect.isroutine(attribute)
        or inspect.isclass(attribute)
        or inspect.ismodule(attribute)
        or inspect.istraceback(attribute)
        or inspect.isframe(attribute)
        or inspect.iscode(attribute)
        or inspect.isabstract(attribute)
        or inspect.ismethoddescriptor(attribute)
        or inspect.isdatadescriptor(attribute)
        or inspect.isgetsetdescriptor(attribute)
        or inspect.ismemberdescriptor(attribute)
    )
示例#41
0
 def document(self, object, name=None, *args):
     """Generate documentation for an object."""
     args = (object, name) + args
     # 'try' clause is to attempt to handle the possibility that inspect
     # identifies something in a way that pydoc itself has issues handling;
     # think 'super' and how it is a descriptor (which raises the exception
     # by lacking a __name__ attribute) and an instance.
     if inspect.isgetsetdescriptor(object): return self.docdata(*args)
     if inspect.ismemberdescriptor(object): return self.docdata(*args)
     try:
         if inspect.ismodule(object): return self.docmodule(*args)
         if inspect.isclass(object): return self.docclass(*args)
         if inspect.isroutine(object): return self.docroutine(*args)
     except AttributeError:
         pass
     if isinstance(object, property): return self.docproperty(*args)
     return self.docother(*args)
示例#42
0
def get_size(obj, seen=None):
    """Recursively finds size of objects in bytes.

    Original source: https://github.com/bosswissam/pysize

    MIT License

    Copyright (c) [2018] [Wissam Jarjoui]

    Args:
        obj (object): Object to calculate size of.
        seen (None or Set): Set of objects seen so far.

    Returns:
        int: Byte size of `obj`.
    """
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if (inspect.isgetsetdescriptor(d) or
                        inspect.ismemberdescriptor(d)):
                    size += get_size(obj.__dict__, seen)
                break
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif (hasattr(obj, '__iter__') and not
            isinstance(obj, (str, bytes, bytearray))):
        size += sum((get_size(i, seen) for i in obj))

    if hasattr(obj, '__slots__'):  # can have __slots__ with __dict__
        size += sum(get_size(getattr(obj, s), seen)
                    for s in obj.__slots__ if hasattr(obj, s))

    return size
示例#43
0
def getClassInfo(_class):

    parentClassName = inspect.getclasstree([_class])[0][0].__name__

    writeDefinition("class %s(%s):\n" % (_class.__name__, parentClassName))
    writeDefinition("\n")

    indent()

    for memberName in _class.__dict__:
        member = getattr(_class, memberName)
        if inspect.isbuiltin(member):
            getMemberInfo(member, isBuiltIn=True)
            continue
        if inspect.ismethod(member):
            getMemberInfo(member)
            continue
        if inspect.isfunction(member):
            getMemberInfo(member)
            continue
        if inspect.isroutine(member):
            getMemberInfo(member, isBuiltIn=True)
            continue
        if inspect.istraceback(member):
            getMemberInfo(member)
            continue
        if inspect.isframe(member):
            getMemberInfo(member)
            continue
        if inspect.iscode(member):
            getMemberInfo(member)
            continue
        if inspect.ismethoddescriptor(member):
            getMemberInfo(member)
            continue
        if inspect.isdatadescriptor(member):
            getMemberInfo(member)
            continue
        if inspect.isgetsetdescriptor(member):
            getMemberInfo(member)
            continue
        if inspect.ismemberdescriptor(member):
            getMemberInfo(member)

    dedent()
示例#44
0
def determine_object_type(obj):
    if inspect.ismodule(obj):
        return 'Module', obj.__str__()
    elif inspect.isclass(obj):
        return 'Class', obj.__str__()
    elif inspect.ismethod(obj):
        return 'Method', obj.__str__()
    elif inspect.isfunction(obj):
        return 'Function', obj.__str__()
    elif inspect.isgeneratorfunction(obj):
        return 'Generator Function', obj.__str__()
    elif inspect.isgenerator(obj):
        return 'Generator', obj.__str__()
    elif inspect.iscoroutinefunction(obj):
        return 'Coroutine Function', obj.__str__()
    elif inspect.iscoroutine(obj):
        return 'Coroutine', obj.__str__()
    elif inspect.isawaitable(obj):
        return 'Awaitable', obj.__str__()
    elif inspect.istraceback(obj):
        return 'Traceback', obj.__str__()
    elif inspect.isframe(obj):
        return 'Frame', obj.__str__()
    elif inspect.iscode(obj):
        return 'Code', obj.__str__()
    elif inspect.isbuiltin(obj):
        return 'Builtin', obj.__str__()
    elif inspect.isroutine(obj):
        return 'Routine', obj.__str__()
    elif inspect.isabstract(obj):
        return 'Abstract Base Class', obj.__str__()
    elif inspect.ismethoddescriptor(obj):
        return 'Method Descriptor', obj.__str__()
    elif inspect.isdatadescriptor(obj):
        return 'Data Descriptor', obj.__str__()
    elif inspect.isgetsetdescriptor(obj):
        return 'Getset Descriptor', obj.__str__()
    elif inspect.ismemberdescriptor(obj):
        return 'Member Descriptor', obj.__str__()
    elif inspect.isbuiltin(obj):
        return str(type(obj)), obj.__str__()
    else:
        return str(type(obj)), obj.__str__()
示例#45
0
def deep_getsizeof(obj, seen = None):
	"""Recursively finds size of objects in bytes"""
	size = sys.getsizeof(obj)
	seen = seen or set()
	if id(obj) in seen:
		return 0
	seen.add(id(obj))
	if hasattr(obj, '__dict__'):
		for cls in obj.__class__.__mro__:
			if '__dict__' in cls.__dict__:
				d = cls.__dict__['__dict__']
				if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d):
					size += deep_getsizeof(obj.__dict__, seen)
				break
	if isinstance(obj, dict):
		size += sum((deep_getsizeof(v, seen) for v in obj.values()))
		size += sum((deep_getsizeof(k, seen) for k in obj.keys()))
	elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
		size += sum((deep_getsizeof(i, seen) for i in obj))
	return size
示例#46
0
def get_size(obj, seen=None):
    """Recursively finds size of objects in bytes

    :param obj:
    :param seen:
    :return:
    """
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(
                        d):
                    size += get_size(obj.__dict__, seen)
                break
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not isinstance(obj,
                                                     (str, bytes, bytearray)):
        try:
            size += sum((get_size(i, seen) for i in obj))
        except TypeError:
            logging.exception(
                "Unable to get size of %r. This may lead to incorrect sizes. "
                "Please report this error.", obj)
    if hasattr(obj, '__slots__'):  # can have __slots__ with __dict__
        size += sum(
            get_size(getattr(obj, s), seen) for s in obj.__slots__
            if hasattr(obj, s))

    return size
示例#47
0
    def _inspect(self, include_prefix=None, exclude_prefix=None):
        """

        returns properties and methods of the class/object

        properties,methods = self._inspect()

        :return: (properties,methods)
        """
        properties = []
        methods = []
        for name, obj in inspect.getmembers(self.__class__):
            if include_prefix and not name.startswith(include_prefix):
                continue
            if exclude_prefix and name.startswith(exclude_prefix):
                continue
            if inspect.ismethod(obj):
                methods.append(name)
            elif inspect.ismethoddescriptor(obj):
                continue
            elif inspect.isfunction(obj):
                methods.append(name)
            elif inspect.isclass(obj):
                properties.append(name)
            elif inspect.isgetsetdescriptor(obj):
                continue
            else:
                properties.append(name)

        for item in self.__dict__.keys():
            if include_prefix and not name.startswith(include_prefix):
                continue
            if exclude_prefix and name.startswith(exclude_prefix):
                continue
            if item not in properties:
                properties.append(item)

        self._properties_ = properties
        self._methods_ = methods

        return self._properties_, self._methods_
示例#48
0
    def stub(self):
        """
        Generate stub for class.
        """
        doc = self.value.__doc__.strip()
        abstract = not doc.startswith("{}(".format(self.name))

        anc = []
        match = re.search(r"Implements: `[^`]*`(, `[^`]*`)*", doc)
        if match is not None:
            for match in re.finditer(r"`([^`]*)`", match.group(0)):
                anc.append(match.group(1))
        if abstract:
            anc.append("metaclass=ABCMeta")

        sig = "{}({})".format(self.name, ", ".join(anc)) if anc else self.name

        functions = []
        variables = []

        if not abstract:
            init = doc.splitlines()[0].strip()
            init = init.replace(self.name + "(", "__init__(self, ", 1)
            init = init.replace(" -> {}".format(self.name), "")
            functions.append(Other("__init__", "def {}: ...".format(init)))

        for name, value in sorted(self.value.__dict__.items(), key=lambda x: x[0]):
            if name.startswith("_"):
                continue
            if inspect.ismethoddescriptor(value):
                functions.append(Function(name, value))
            elif inspect.isgetsetdescriptor(value):
                variables.append(Property(name, value))
            else:
                variables.append(Variable(name, value))

        return CLASS_TEMPLATE.render(
            indent=indent,
            sig=sig,
            variables=variables,
            functions=functions)
示例#49
0
def doc_asstring(thing, title='Python Library Documentation: %s', forceload=0):
    """return text documentation as a string, given an object or a path to an object."""
    try:
        object, name = pydoc.resolve(thing, forceload)
        desc = pydoc.describe(object)
        module = inspect.getmodule(object)
        if name and '.' in name:
            desc += ' in ' + name[:name.rfind('.')]
        elif module and module is not object:
            desc += ' in module ' + module.__name__
        if not (inspect.ismodule(object) or inspect.isclass(object) or
                inspect.isroutine(object) or inspect.isgetsetdescriptor(object)
                or inspect.ismemberdescriptor(object)
                or isinstance(object, property)):
            # If the passed object is a piece of data or an instance,
            # document its available methods instead of its value.
            object = type(object)
            desc += ' object'
        return title % desc + '\n\n' + pydoc.text.document(object, name)
    except (ImportError, ErrorDuringImport), value:
        print value
示例#50
0
 def test_excluding_predicates(self):
     self.istest(inspect.isbuiltin, 'sys.exit')
     self.istest(inspect.isbuiltin, '[].append')
     self.istest(inspect.iscode, 'mod.spam.__code__')
     self.istest(inspect.isframe, 'tb.tb_frame')
     self.istest(inspect.isfunction, 'mod.spam')
     self.istest(inspect.isfunction, 'mod.StupidGit.abuse')
     self.istest(inspect.ismethod, 'git.argue')
     self.istest(inspect.ismodule, 'mod')
     self.istest(inspect.istraceback, 'tb')
     self.istest(inspect.isdatadescriptor, 'collections.defaultdict.default_factory')
     self.istest(inspect.isgenerator, '(x for x in range(2))')
     self.istest(inspect.isgeneratorfunction, 'generator_function_example')
     if hasattr(types, 'GetSetDescriptorType'):
         self.istest(inspect.isgetsetdescriptor,
                     'type(tb.tb_frame).f_locals')
     else:
         self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
     if hasattr(types, 'MemberDescriptorType'):
         self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
     else:
         self.assertFalse(inspect.ismemberdescriptor(datetime.timedelta.days))
示例#51
0
 def test_excluding_predicates(self):
     self.istest(inspect.isbuiltin, 'sys.exit')
     self.istest(inspect.isbuiltin, '[].append')
     self.istest(inspect.isclass, 'mod.StupidGit')
     self.istest(inspect.iscode, 'mod.spam.func_code')
     self.istest(inspect.isframe, 'tb.tb_frame')
     self.istest(inspect.isfunction, 'mod.spam')
     self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
     self.istest(inspect.ismethod, 'git.argue')
     self.istest(inspect.ismodule, 'mod')
     self.istest(inspect.istraceback, 'tb')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
     if hasattr(types, 'GetSetDescriptorType'):
         self.istest(inspect.isgetsetdescriptor,
                     'type(tb.tb_frame).f_locals')
     else:
         self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
     if hasattr(types, 'MemberDescriptorType'):
         self.istest(inspect.ismemberdescriptor, 'datetime.timedelta.days')
     else:
         self.failIf(inspect.ismemberdescriptor(datetime.timedelta.days))
 def import_builtin():
     module = importlib.import_module('builtins')
     syms = inspect.getmembers(module)
     for sym in syms:
         if inspect.isfunction(sym[1]):
             GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
         elif inspect.isbuiltin(sym[1]):
             GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
         elif inspect.ismethod(sym[1]):
             pass
         elif inspect.isgeneratorfunction:
             GLOBAL_SYMBOL_LIST.append(Function(sym[0]))
         elif inspect.isgenerator(sym[1]):
             pass
         elif inspect.istraceback(sym[1]):
             pass
         elif inspect.isframe(sym[1]):
             pass
         elif inspect.iscode(sym[1]):
             pass
         elif inspect.isroutine(sym[1]):
             pass
         elif inspect.isabstract(sym[1]):
             pass
         elif inspect.ismemberdescriptor(sym[1]):
             pass
         elif inspect.isdatadescriptor(sym[1]):
             pass
         elif inspect.isdatadescriptor(sym[1]):
             pass
         elif inspect.isgetsetdescriptor(sym[1]):
             pass
         elif inspect.ismemberdescriptor(sym[1]):
             pass
         elif inspect.isclass(sym[1]):
             GLOBAL_SYMBOL_LIST.append(Class(sym[0]))
         else:
             print(sym[0])
示例#53
0
def get_size(obj, seen=None) -> int:
    """Recursively finds size of objects in bytes"""
    import inspect

    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if hasattr(obj, "__dict__"):
        for cls in obj.__class__.__mro__:
            if "__dict__" in cls.__dict__:
                d = cls.__dict__["__dict__"]
                if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(
                        d):
                    size += get_size(obj.__dict__, seen)
                break
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif isinstance(obj, np.ndarray) and obj.base is not None:
        # The check for ndarray and base is not None is whether obj is a view on an ndarray,
        # which needs to be iterated on, since getsizeof(view) does not reflect the
        size += sys.getsizeof(obj.base)
    elif hasattr(obj, "__iter__") and not isinstance(
            obj, (str, bytes, bytearray, np.ndarray)):
        size += sum((get_size(i, seen) for i in obj))

    if hasattr(obj, "__slots__"):  # can have __slots__ with __dict__
        size += sum(
            get_size(getattr(obj, s), seen) for s in obj.__slots__
            if hasattr(obj, s))

    return size
示例#54
0
def get_size(obj, seen=None):
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(
                        d):
                    size += get_size(obj.__dict__, seen)
                break
    if isinstance(obj, dict):
        # 这里避免重复计算
        size += sum(
            (get_size(v, seen) for v in obj.values()
             if not isinstance(v, (str, int, float, bytes, bytearray))))
        # size += sum((get_size(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not isinstance(obj,
                                                     (str, bytes, bytearray)):
        # 这里避免重复计算
        try:
            size += sum(
                (get_size(i, seen) for i in obj
                 if not isinstance(i, (str, int, float, bytes, bytearray))))
        except:
            pass

    if hasattr(obj, '__slots__'):
        size += sum(
            get_size(getattr(obj, s), seen) for s in obj.__slots__
            if hasattr(obj, s))

    return size
示例#55
0
 def test_excluding_predicates(self):
     # XXX: Jython's PySystemState needs more work before this will
     # be doable.
     if not is_jython:
         self.istest(inspect.isbuiltin, 'sys.exit')
     self.istest(inspect.isbuiltin, '[].append')
     self.istest(inspect.isclass, 'mod.StupidGit')
     self.istest(inspect.iscode, 'mod.spam.func_code')
     self.istest(inspect.isframe, 'tb.tb_frame')
     self.istest(inspect.isfunction, 'mod.spam')
     self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
     self.istest(inspect.ismethod, 'git.argue')
     self.istest(inspect.ismodule, 'mod')
     self.istest(inspect.istraceback, 'tb')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
     self.istest(inspect.isgenerator, '(x for x in xrange(2))')
     self.istest(inspect.isgeneratorfunction, 'generator_function_example')
     if hasattr(types, 'GetSetDescriptorType'):
         self.istest(inspect.isgetsetdescriptor,
                     'type(tb.tb_frame).f_locals')
     else:
         self.failIf(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
示例#56
0
文件: memsize.py 项目: jerodg/jgutils
def memsize(obj: Any, seen: set = None, human: bool = False) -> Union[int, str]:
    """Calculate size of object in bytes

    :param obj: Any
    :param seen: set
    :param human: bool
    :return: int"""
    size = getsizeof(obj)
    seen = seen or set()
    obj_id = id(obj)

    if obj_id in seen:
        return 0

    seen.add(obj_id)

    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if isgetsetdescriptor(d) or ismemberdescriptor(d):
                    size += memsize(obj.__dict__, seen)
                break

    if isinstance(obj, dict):
        size += sum((memsize(v, seen) for v in obj.values()))
        size += sum((memsize(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum((memsize(i, seen) for i in obj))

    if hasattr(obj, '__slots__'):  # can have __slots__ with __dict__
        size += sum(memsize(getattr(obj, s), seen) for s in obj.__slots__ if hasattr(obj, s))

    if human:
        return autosize(size=size)

    return size
示例#57
0
def get_size(obj, seen=None):
    """Recursively finds size of objects in bytes

	References:
		https://github.com/bosswissam/pysize/blob/master/pysize.py
	"""

    if isinstance(obj, (np.ndarray, np.void)):
        return obj.size * obj.itemsize

    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                gs_descriptor = inspect.isgetsetdescriptor(d)
                m_descriptor = inspect.ismemberdescriptor(d)
                if gs_descriptor or m_descriptor:
                    size += get_size(obj.__dict__, seen)
                break
    any_str = isinstance(obj, (str, bytes, bytearray))
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not any_str:
        size += sum((get_size(i, seen) for i in obj))
    if hasattr(obj, '__slots__'):
        size += sum(
            get_size(getattr(obj, s), seen) for s in obj.__slots__
            if hasattr(obj, s))
    return size
示例#58
0
def get_size(obj, seen=None):
    """Recursively finds size of objects in bytes"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if hasattr(obj, '__dict__'):
        for cls in obj.__class__.__mro__:
            if '__dict__' in cls.__dict__:
                d = cls.__dict__['__dict__']
                if inspect.isgetsetdescriptor(d) or inspect.ismemberdescriptor(d):
                    size += get_size(obj.__dict__, seen)
                break
    if isinstance(obj, dict):
        size += sum((get_size(v, seen) for v in obj.values()))
        size += sum((get_size(k, seen) for k in obj.keys()))
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum((get_size(i, seen) for i in obj))
    return size
示例#59
0
def visit_member(parent_name, member):
    cur_name = ".".join([parent_name, member.__name__])
    if inspect.isclass(member):
        for name, value in inspect.getmembers(member):
            if hasattr(value, '__name__') and (not name.startswith("_")
                                               or name == "__init__"):
                visit_member(cur_name, value)
    elif callable(member):
        try:
            doc = ('document', md5(member.__doc__))
            args = inspect.getargspec(member)
            all = (args, doc)
            member_dict[cur_name] = all
        except TypeError:  # special for PyBind method
            member_dict[cur_name] = "  ".join([
                line.strip() for line in pydoc.render_doc(member).split('\n')
                if "->" in line
            ])
    elif inspect.isgetsetdescriptor(member):
        return
    else:
        raise RuntimeError(
            "Unsupported generate signature of member, type {0}".format(
                str(type(member))))
示例#60
0
 def test_excluding_predicates(self):
     self.istest(inspect.isbuiltin, 'sys.exit')
     if check_impl_detail():
         self.istest(inspect.isbuiltin, '[].append')
     self.istest(inspect.iscode, 'mod.spam.func_code')
     self.istest(inspect.isframe, 'tb.tb_frame')
     self.istest(inspect.isfunction, 'mod.spam')
     self.istest(inspect.ismethod, 'mod.StupidGit.abuse')
     self.istest(inspect.ismethod, 'git.argue')
     self.istest(inspect.ismodule, 'mod')
     self.istest(inspect.istraceback, 'tb')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.closed')
     self.istest(inspect.isdatadescriptor, '__builtin__.file.softspace')
     self.istest(inspect.isgenerator, '(x for x in xrange(2))')
     self.istest(inspect.isgeneratorfunction, 'generator_function_example')
     if hasattr(types, 'GetSetDescriptorType'):
         self.istest(inspect.isgetsetdescriptor,
                     'type(tb.tb_frame).f_locals')
     else:
         self.assertFalse(inspect.isgetsetdescriptor(type(tb.tb_frame).f_locals))
     if hasattr(types, 'MemberDescriptorType'):
         self.istest(inspect.ismemberdescriptor, 'type(lambda: None).func_globals')
     else:
         self.assertFalse(inspect.ismemberdescriptor(type(lambda: None).func_globals))