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 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])
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])
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)
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
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
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)
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))
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()
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)
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)
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
def get_scope_objects(names): """ Looks for the names defined with dir() in an objects and divides them into different object types. """ classes = {} funcs = {} stmts = {} members = {} for n in names: if '__' in n and n not in mixin_funcs: continue try: # this has a builtin_function_or_method exe = getattr(scope, n) except AttributeError: # happens e.g. in properties of # PyQt4.QtGui.QStyleOptionComboBox.currentText # -> just set it to None members[n] = None else: if inspect.isclass(scope): if is_in_base_classes(scope, n, exe): continue if inspect.isbuiltin(exe) or inspect.ismethod(exe) \ or inspect.ismethoddescriptor(exe): funcs[n] = exe elif inspect.isclass(exe): classes[n] = exe elif inspect.ismemberdescriptor(exe): members[n] = exe else: stmts[n] = exe return classes, funcs, stmts, members
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))
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))
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))
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
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]
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
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)
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
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: # print(sym[0]) 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])
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))
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) )
def dump_value(value, depth=0): if depth > 10: return 'max_recursion_depth_exceeded' if isinstance(value, type): return dump_class(value, depth + 1) if inspect.isfunction(value): return dump_function(value) if callable(value): return {'type': 'callable'} # TODO more information if isinstance(value, types.ModuleType): return {'type': 'module'} # TODO module name if inspect.isdatadescriptor(value): return {'type': 'datadescriptor'} if inspect.ismemberdescriptor(value): return {'type': 'memberdescriptor'} return dump_simple(value)
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)
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()
def describe(thing): """Produce a short description of the given thing. returns thing's type and name """ # inspect.isgeneratorfunction(object) # inspect.isgenerator(object) # inspect.istraceback(object) # inspect.isframe(object) # inspect.iscode(object) # inspect.isroutine(object) # inspect.isabstract(object) # inspect.ismethoddescriptor(object) # inspect.isdatadescriptor(object) if inspect.ismodule(thing): if thing.__name__ in sys.builtin_module_names: return ('built-in module', thing.__name__) if hasattr(thing, '__path__'): return ('package', thing.__name__) else: return ('module', thing.__name__) if inspect.isbuiltin(thing): return ('built-in function', thing.__name__) if inspect.isgetsetdescriptor(thing): return ( 'getset descriptor', '%s.%s.%s' % ( thing.__objclass__.__module__, thing.__objclass__.__name__, thing.__name__)) if inspect.ismemberdescriptor(thing): return ( 'member descriptor', '%s.%s.%s' % ( thing.__objclass__.__module__, thing.__objclass__.__name__, thing.__name__)) if inspect.isclass(thing): return ('class', thing.__name__) if inspect.isfunction(thing): return ('function', thing.__name__) if inspect.ismethod(thing): return ('method', thing.__name__) if type(thing) is types.InstanceType: return ('instance of', thing.__class__.__name__) return (type(thing).__name__, '')
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 type(obj), obj.__str__() else: return type(obj), obj.__str__()
def _propsdict(obj): """Returns the @properties in an object. Example: >>> class PropertyCarrier(object): ... __slots__ = 'prop2' ... def __init__(self): ... self.prop2 = 3 ... @property ... def prop(self): ... return self.prop2 ... @prop.setter ... def prop(self, prop): ... self.prop2 = prop >>> _propsdict(PropertyCarrier()) {'prop': 3} """ descriptors = inspect.getmembers(obj.__class__, inspect.isdatadescriptor) # All data descriptors except slots and __weakref__ # See: http://docs.python.org/2/reference/datamodel.html return {dname: value.__get__(obj) for dname, value in descriptors if '__weakref__' != dname and not inspect.ismemberdescriptor(value)}