Information about a module (notebook or Python file)
AST Parsing Utilities
extract_docments_signature
def extract_docments_signature( node:Union[ast.FunctionDef, ast.AsyncFunctionDef], # AST function node source_lines:List[str], # Source code lines)->str: # Function signature
Extract function signature with docments-style comments
parse_function
def parse_function( node:Union[ast.FunctionDef, ast.AsyncFunctionDef], # AST function node source_lines:List[str], # Source code lines is_exported:bool=False, # Has #| export)->FunctionInfo: # Function information
Parse a function definition from AST
parse_class
def parse_class( node:ast.ClassDef, # AST class node source_lines:List[str], # Source code lines is_exported:bool=False, # Has #| export)->ClassInfo: # Class information
Parse a notebook code cell for functions, classes, variables, and imports
Module Parsing
parse_notebook
def parse_notebook( path:Path, # Path to notebook)->ModuleInfo: # Module information
Parse a notebook file for module information
parse_python_file
def parse_python_file( path:Path, # Path to Python file)->ModuleInfo: # Module information
Parse a Python file for module information
Testing
Let’s test the parser on our own notebooks:
# Test parsing the core modulecore_info = parse_notebook(Path("core.ipynb"))print(f"Module: {core_info.name}")print(f"Title: {core_info.title}")print(f"Description: {core_info.description}")print(f"\nFunctions ({len(core_info.functions)}):")for func in core_info.functions[:3]: # Show first 3print(f" - {func.name}")print(f"\nClasses ({len(core_info.classes)}):")for cls in core_info.classes[:3]: # Show first 3print(f" - {cls.name}")print(f"\nTesting refactored parse_class function...")print("Class signatures:")for cls in core_info.classes:print(f"\n{cls.name}:")print(f" Decorators: {cls.decorators}")print(f" Methods: {[m.name for m in cls.methods]}")print(f" Attributes: {[a.name for a in cls.attributes]}")print(f" Signature: {cls.signature[:100]}...") # First 100 chars
# Test extracting function signaturesprint("Function signatures with docments:")for func in core_info.functions[:2]:print(f"\n{func.name}:")print(func.signature)
Function signatures with docments:
get_notebook_files:
def get_notebook_files(path: Path = None, # Directory to search (defaults to nbs_path)
recursive: bool = True # Search subdirectories
) -> List[Path]: # List of notebook paths
get_subdirectories:
def get_subdirectories(path: Path = None, # Directory to search (defaults to nbs_path)
recursive: bool = False # Include all nested subdirectories
) -> List[Path]: # List of directory paths