Skip to content

Instantly share code, notes, and snippets.

@rindPHI
Created June 7, 2022 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rindPHI/29c8e446bda9f6c600d17df197b096b9 to your computer and use it in GitHub Desktop.
Save rindPHI/29c8e446bda9f6c600d17df197b096b9 to your computer and use it in GitHub Desktop.
Finding functions in Jupyter notebooks by matching names and signatures.
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