Skip to main content

Monitor and manage deeply customizable metrics about your python code using ASTs

Project description


Monitor and manage deeply customizable metrics about your python code using ASTs.

Codewatch lets you write simple python code to track statistics about the state of your codebase and write lint-like assertions on those statistics. Use this to incrementally improve and evolve the quality of your code base, increase the visibility of problematic code, to encourage use of new patterns while discouraging old ones, to enforce coding style guides, or to prevent certain kinds of regression errors.

What codewatch does:

  1. Traverses your project directory
  2. Parses your code into AST nodes and calls your visitor functions
  3. Your visitor functions run and populate a stats dictionary
  4. After all visitor functions are called, your assertion functions are called
  5. Your assertion functions can assert on data in the stats dictionary, save metrics to a dashboard, or anything you can think of


Python: 2.7, 3.6, 3.7

Execute the following in your terminal:

pip install codewatch


codewatch codewatch_config_module

codewatch_config_module is a module that should contain your visitors, assertions and filters (if required)


You should use the @visit decorator. The passed in node is an astroid node which follows a similar API to ast.Node

from codewatch import visit

def _count_import(stats):

def count_import(node, stats, _rel_file_path):

def count_import_from(node, stats, _rel_file_path):

This will build a stats dictionary that contains something like the following:

    "total_imports_num": 763


Once again in the codewatch_config_module you can add assertions against this stat dictionary using the @assertion decorator

from codewatch import assertion

def number_of_imports_not_too_high(stats):
    threshold = 700
    actual = stats.get('total_imports_num')
    err = 'There were {} total imports detected which exceeds threshold of {}'.format(actual, threshold)
    assert actual <= threshold, err

In this case, the assertion would fail since 763 is the newStat and the message:

There were 763 total imports detected which exceeds threshold of 700

would be printed


You can add the following optional filters:

  1. directory_filter (defaults to skip test and migration directories)
# visit all directories
def directory_filter(_dir_name):
    return True
  1. file_filter (defaults to only include python files, and skips test files)
# visit all files
def file_filter(_file_name):
    return True

Tune these filters to suit your needs.


See the Contributing docs


Thanks goes to these wonderful people emoji key:

Josh Doncaster Marsiglio


Rohit Jain


Chris Abiad


Francois Campbell


Monica Moore


Jay Crumb


Jake Bolam


Shouvik D'Costa


Siavash Bidgoly


Noah Negin-Ulster


Vardan Nadkarni




We welcome contributions from the community, Top Hatters and non-Top Hatters alike. Check out our contributing guidelines for more details.


Special thanks to Carol Skelly for donating the 'tophat' GitHub organization.

Project details

Download files

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

Files for codewatch, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size codewatch-0.1.0-py2.py3-none-any.whl (31.3 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size codewatch-0.1.0.tar.gz (21.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page