Skip to main content

Extract dependencies of modules (function, class, import) in python repository

Project description

Dependency call extraction tool for Python modules

What does this package do?

This package can use to extract functions, import statements (TODO: class) in a module file, and their call dependencies in a source repository.

Example:

Consider a simple repository structure

simple_repo
  |
  |_ file1.py
  |_ folder1.py
        |_ file2.py     

The content in each file is

# file1.py
def print_function(str):
  print(str)

# folder1/file2.py
from file1 import print_function

def start_print():
  print_function("Author: Nam Le Hai")

def print_hello():
  print_function("hello")

The tool can use to find the dependency of print_hello() function in file2.py which is the print_function() function imported from file1.py.

Quickstart Guide

Currently, our package only supports extracting dependency for functions and import statements in a module files.

Requirement

Python >= 3.10

Install pydepcall

Using Anaconda (feel free to use other env)

conda create -n YOUR_ENV_NAME python=3.10
conda activate YOUR_ENV_NAME
pip install pydepcall

Usage:

from pydepcall import Extractor

If you want to extract all module files in the repository

reposrc = YOUR_LOCAL_PATH_OF_REPO
extractor = Extractor(reposrc)
output = extractor.extract()

If you want to extract a specific module file in the repository

reposrc = YOUR_LOCAL_PATH_OF_REPO
module_file = YOUR_LOCAL_PATH_OF_FILE_IN_REPO
extractor = Extractor(reposrc, module_file)
output = extractor.extract()

Example

>>> from pydepcall import Extractor
>>> reposrc = "simple_repo"
>>> extractor = Extractor(reposrc)
>>> output = extractor.extract()
>>> output
{'simple_repo/file1.py': <pydepcall.Node.ModuleNode object at 0x7faeb6d84580>, 'simple_repo/folder1/file2.py': <pydepcall.Node.ModuleNode object at 0x7faeb7822050>}

>>> output["simple_repo/folder1/file2.py"].function_list
[<pydepcall.Node.FunctionNode object at 0x7faeb6bc2740>, <pydepcall.Node.FunctionNode object at 0x7faeb6bc2530>]

>>> output["simple_repo/folder1/file2.py"].function_list[0].children
[<pydepcall.Node.ImportNode object at 0x7fc5fade6320>]

>>> output["simple_repo/folder1/file2.py"].function_list[0].children[0].children
[<pydepcall.Node.FunctionNode object at 0x7faeb6bc22c0>]

>>> output["simple_repo/folder1/file2.py"].function_list[0].children[0].children[0].content
'def print_function(str):\n  print(str)'

Output format

For extracting a specific module, the output will be a ModuleNode of the input file.

For extracting the whole repository, the output will be the dictionary of ModuleNode with the keys are all module files in the repository.

Node objects

The package has 5 main nodes:

  • ModuleNode: contains all FunctionNode and ImportNode in a module file
  • ImportNode: a node represents an import statement
  • FunctionNode: a node represents a python function
  • ClassNode: a node represents a class
  • BlockNode: a node represents a codeblock in the module file

Every node except ModuleNode has the following attributes:

  • path: the module file's local path contain that node
  • content: the text content of the node (function, import, class or codeblock)
  • position_in_file: the position of the node in the module file

For FunctionNode and ImportNode, we can acquire their dependencies through their children (node.children) attribute.

Please see Node.py for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydepcall-0.0.2.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydepcall-0.0.2-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file pydepcall-0.0.2.tar.gz.

File metadata

  • Download URL: pydepcall-0.0.2.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for pydepcall-0.0.2.tar.gz
Algorithm Hash digest
SHA256 bf15db559c385d9d38925b9c343a4a5b58bf191b690735348d226c61fbab3e4a
MD5 9eec51fd0ef792ed8706d5c72120a853
BLAKE2b-256 7e55bda8520df5d3b907c92817fd8d4f93ce4c3e2b75a6055b40e41c9fadb6ff

See more details on using hashes here.

File details

Details for the file pydepcall-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pydepcall-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for pydepcall-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d406c89912c381b5743eff968738f0db2135551b7c736edc251599d1a5ccc28e
MD5 6ca58aef5e3ba7da7a123367f3df18c2
BLAKE2b-256 4b41addb3f28132da07144a32161f567839d8a6dabf5a2914a3da4c5efa7451e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page