Explore your Python code with graph queries
Pycograph creates a RedisGraph model of your Python code. You can:
- query it with Cypher
- visualize it with RedisInsight
Getting Started
Install Pycograph from PyPI:
pip install pycograph
Start RedisGraph and RedisInsight. E.g. via Docker containers:
docker run -d -p 6379:6379 redislabs/redismod
docker run -d -v redisinsight:/db -p 8001:8001 redislabs/redisinsight:latest
Load your project's code with the pycograph load
command:
pycograph load --project-dir ~/code/docker/compose
See some examplesRedisGraph Model
RedisGraph is based on the Property Graph Model .
Pycograph's current model:
Node labels
- package
- module
- class
- function
- constant
- init
- test_package
- test_module
- test_function
- test_helper_function
- test_constant
In the current model, each node has exactly one label.
Properties for all nodes
- name
- full_name
- is_test_object
- test_type
Relationship types
contains
No properties.
imports
Properties:
- from
- name
- as_name
- reference_type: absolute / relative
- level
calls
Properties:
- reference_name
- called_attribute
How Does It Work?
You can find the source code on GitHub.
Libraries used:
- ast module of the Python standard library for the abstract syntax tree
- Pydantic both for the models of the intermediate objects and for the settings
- redisgraph-py for creating the RedisGraph model
- typer for the command line interface
Limitations
Pycograph is in beta version.
It creates a basic model with focus on the relationships betweeen the different parts of the code base. Even that model might be incomplete, ignoring some less common syntax. The goal is to provide some useful insight, not to create an exhaustive model.
If Pycograph finds a syntax error, it skips the module containing the syntax error and tries to build a model from the rest of the code base.
Below are some of the limitations. If you bump into other limitations, please open a GitHub issue.
Imports
The following imports will be ignored by Pycograph:
- imported external packages
import *
syntax- variables
- globals
Calls
- All the limitations of the imports.
- Resolving calls of more than 2 levels.
Other Known Limitations
- No support for
.py
files containing Jinja templates (e.g. cookiecutter)
Caveats
Pycograph is a tool for exploration. Its goal is to provide insight, not to take decisions. It shouldn't trigger automated code changes or cleanup.
Disclaimer
see LICENSE
Many Thanks To
- Docker Compose
- Ajeet S. Raina a.k.a. Collabnix
- The libraries listed above :-)