How to find a function in the Python module tree?

I am using SQLAlchemy and trying to import the group_by function into the interpreter, but I cannot find it. Is there an easy way to search the module tree to find out where this function lives?

Of course, I tried from sqlalchemy import + tab and searched manually, but at each level of the tree there are too many options to check.

+4
source share
2 answers

An easy way to do this is to go beyond the interpreter and just find documents .


Meanwhile, it seems to me that there is no such function for import. There are group_by methods for sqlalchemy.orm.query.Query and sqlalchemy.sql.expression.[Compound]Select[Base] objects.


But if you really want to recursively go through all the modules in the package looking for a name, here is how you do it:

 import inspect def find_name(package, name): if hasattr(package, name): yield package for modulename, submodule in inspect.getmembers(package, inspect.ismodule): yield from find_name(submodule, name) 

For Python 3.2 or earlier, you need to replace the yield from loop with a loop:

 for modulename, submodule in inspect.getmembers(package, inspect.ismodule): for result in find_name(submodule, name): yield result 

And if you just want to get the first result, instead of all the results, you can simply return instead of yield ing:

 def find_name(package, name): if hasattr(package, name): return package for modulename, submodule in inspect.getmembers(package, inspect.ismodule): result = find_name(submodule, name) if result: return result 
+3
source

try printing SQLAlchemy.__all__ , which will return you a list of all the functions in this module that are public.

0
source

Source: https://habr.com/ru/post/1481459/


All Articles