hbllmutils.meta.code

This module provides comprehensive utilities for analyzing Python source code and its dependencies.

The hbllmutils.meta.code package offers a collection of tools for extracting, analyzing, and inspecting Python source code, import statements, module structures, and object metadata. It is designed to facilitate code analysis, documentation generation, and understanding code dependencies.

Main components exported by this module:

  • Import Analysis:

    • ImportStatement: Represents a regular import statement

    • analyze_imports(): Extracts all import statements from Python code

  • Module Path Resolution:

    • get_package_name(): Converts a source file path to its Python module name

    • get_pythonpath_of_source_file(): Determines the PYTHONPATH and module path for a file

  • Object Inspection:

    • ObjectInspect: Contains inspection information about a Python object

    • get_object_info(): Retrieves comprehensive metadata about any Python object

  • Prompt Generation:

    • get_prompt_for_source_file(): Generates structured prompts for LLM analysis

  • Source Analysis:

    • ImportSource: Pairs import statements with object inspection data

    • SourceInfo: Contains comprehensive information about a Python source file

    • get_source_info(): Analyzes a source file and extracts all relevant information

Example Usage::
>>> from hbllmutils.meta.code import analyze_imports, get_source_info, get_prompt_for_source_file
>>> # Analyze imports in code
>>> code = '''
... import os
... from typing import List, Dict
... '''
>>> imports = analyze_imports(code)
>>> print(imports[0])
import os
>>> # Get comprehensive source file information
>>> info = get_source_info('mymodule.py')
>>> print(info.package_name)
'mypackage.mymodule'
>>> print(len(info.imports))
5
>>> # Generate LLM prompt for code analysis
>>> prompt = get_prompt_for_source_file('calculator.py')
>>> # Use this prompt for documentation generation, testing, etc.

This module is particularly useful for:

  • Static code analysis and introspection

  • Automated documentation generation

  • Dependency tracking and visualization

  • Code understanding and refactoring tools

  • LLM-assisted code analysis and generation