python - Searching for import location -
python - Searching for import location -
i'm trying write python script/function help determine import from. want functionality pyqt4 extended , useful python standard library.
to accomplish need dynamically search modules, shared objects, classes, , perchance more 'types' of things given search term.
i want pass module/class/etc. string, import dynamically , search it.
the interface of function this:
search(object_to_search, search_term)
examples:
search('datetime', 'today') -> 'datetime.datetime.today' search('pyqt4', 'nonmodal') -> 'pyqt4.qtcore.qt.nonmodal' search('pyqt4', 'qicon') -> 'pyqt4.qtgui.qicon'
i suppose back upwards wildcards '*' search of sys.path
beyond scope of i'm concered @ time.
this type of script useful pyqt4. there lot of 'enum-like' things nonmodal
above , finding location import or reference them can cumbersome.
i tend utilize c++ qt documentation quite bit pyqt4 info since it's typically easy translate examples python. c++ qt documentation more comprehensive. it's much easier find sample code qt proper. however, import locations of of these 'enum-like' attributes hard find in pyqt4 without manually searching docs or guessing. goal automate process , have script tell me location of thing i'm looking for.
i have tried few different ways of implementing including searching object recursively dir()
, using inspect
module, , hacky version of import/discovery shared objects. below various implementations, non of work quite right.
is type of thing possible? if so, best way this? also, i've tried trim downwards number of things search omitting built-in things, functions, etc., i'm not convinced necessary or correct.
attempted solution:
def search_obj_inspect(obj, search_term): """search given object 'search_term' 'inspect' module""" import inspect def searchable_type(obj): is_class = inspect.isclass(obj) root_type_or_obj = is_class , obj not in [object, type] abstract_class = is_class , inspect.isabstract(obj) method_or_func = inspect.ismethod(obj) or inspect.isfunction(obj) try: hidden_attribute = obj.__name__.startswith('__') except attributeerror: hidden_attribute = false searchable = true # avoid infinite recursion when looking through 'object' , 'type' b/c # members of each other if true in [abstract_class, root_type_or_obj, hidden_attribute, method_or_func, inspect.isbuiltin(obj)]: searchable = false homecoming searchable # fixme: search obj search term? obj.__name__ == search_term? n, v in inspect.getmembers(obj, predicate=lambda x: searchable_type(x)): if search_term in n: try: homecoming v.__name__ except attributeerror: homecoming str(v) homecoming none def search_obj_dir(obj, search_term): """search given object , it's python submodules attr""" import re import types searchable_types = (types.moduletype, types.classtype, types.instancetype, types.dictproxytype) item in dir(obj): if item.startswith('__'): go on if re.search(search_term, item): homecoming obj.__dict__[item].__module__ else: try: submod = obj.__dict__[item] except (keyerror, attributeerror): # nil left recursively search go on if not isinstance(submod, searchable_types): go on #mod = search_obj_dir(submod, search_term) #if mod: #return '.'.join([mod, submod.__name__]) homecoming none def search_so(module, attr): """search given modules shared objects attr""" import os import re try: path = module.__path__[0] except (attributeerror, indexerror): homecoming false root, dirs, files in os.walk(path): filename in files: if filename.endswith('.so'): py_module_name = filename.split('.')[0] so_module = __import__(module.__name__, globals(), locals(), [py_module_name]) if re.search(attr, py_module_name): homecoming ''.join([module.__name__, '.', py_module_name]) try: found = search_obj_dir( so_module.__dict__[py_module_name], attr) except keyerror: # isn't top-level so, might subpackage # fixme: recursively search well, imports # might weird go on if found: homecoming found
thanks @jonclements hint pydoc module. found way accomplish pretty much wanted combination of pkgutil , inspect modules.
the 'trick' go through modules in bundle (like pyqt4) pkgutil.iter_modules
, non-package objects can searched inspect.getmembers
. general thought following:
for pkg in given_pkg: module_or_class in pkg: attribute in module_or_class:
you can see total solution here.
python
Comments
Post a Comment