Skip to content

python_language

TREE_SITTER_QUERY = '(function_definition name: (identifier) @function.name) @function.definition(class_definition name: (identifier) @class.name body: (block (function_definition name: (identifier) @function.name) @function.definition))' module-attribute

Tree-sitter query for extracting function names and definitions.

PythonExtractor

Bases: TreeSitterExtractor

Source code extractor for extracting Python functions.

Source code in src/codablellm/languages/python_language.py
class PythonExtractor(TreeSitterExtractor):
    """
    Source code extractor for extracting Python functions.
    """

    def __init__(self) -> None:
        super().__init__("Python", TREE_SITTER_QUERY)  # type: ignore

    def get_extractable_files(self, path: PathLike) -> Set[Path]:
        return rglob_file_extensions(path, [".py"])

    @requires_extra("python", "Python source code extraction", "tree_sitter_python")
    def get_language(self) -> Language:
        return Language(tsp.language())  # type: ignore

    def is_installed(self) -> bool:
        return tsp is not None