Created
June 7, 2022 09:35
-
-
Save rindPHI/29c8e446bda9f6c600d17df197b096b9 to your computer and use it in GitHub Desktop.
Finding functions in Jupyter notebooks by matching names and signatures.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inspect | |
import sys | |
import types | |
from typing import Dict | |
def global_functions() -> Dict[str, str]: | |
result = {} | |
module = sys.modules['__main__'] | |
for key in dir(module): | |
value = getattr(module, key) | |
if not isinstance(value, types.FunctionType): | |
continue | |
source = inspect.getfile(value) | |
if not source.endswith('.ipynb') and not 'ipykernel' in source: | |
continue | |
result[key + str(inspect.signature(value))] = ( | |
'(this notebook)' if 'ipykernel' in source | |
else source[source.rindex('/') + 1:]) | |
return result | |
def find_functions(*terms: str) -> Dict[str, str]: | |
return { | |
sig: source | |
for sig, source in global_functions().items() | |
if all(term in sig for term in terms) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment