Skip to main content

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

Project description

Overview

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

Installation

Python: 2.7, 3.6, 3.7

Execute the following in your terminal:

pip install codewatch

Usage

codewatch codewatch_config_module

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

Visitors

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):
    stats.increment('total_imports_num')

@visit('import')
def count_import(node, stats, _rel_file_path):
    _count_import(stats)

@visit('importFrom')
def count_import_from(node, stats, _rel_file_path):
    _count_import(stats)

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

{
    "total_imports_num": 763
}

Assertions

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

from codewatch import assertion


@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

Filters

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.

Contributing

View our Code of Conduct here

Running tests

Assuming you have a suitable python version with pip:

# setup
pip install -r requirements.txt -r requirements_test.txt

# run the tests!
pytest

Contributors

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

💻

Credits

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.

Source Distribution

codewatch-0.0.17.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

codewatch-0.0.17-py2.py3-none-any.whl (26.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file codewatch-0.0.17.tar.gz.

File metadata

  • Download URL: codewatch-0.0.17.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for codewatch-0.0.17.tar.gz
Algorithm Hash digest
SHA256 c3bd1229e8a0ee2e8aff957d674714c9fec28a7ce7f901f44cf0292073685bb4
MD5 a7dfa0550a016246051224a04bef55d1
BLAKE2b-256 c02b71162c4be24563a12de5e44d0283d64e7a514b715707cc702b0def74b867

See more details on using hashes here.

File details

Details for the file codewatch-0.0.17-py2.py3-none-any.whl.

File metadata

  • Download URL: codewatch-0.0.17-py2.py3-none-any.whl
  • Upload date:
  • Size: 26.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for codewatch-0.0.17-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b37959b274cd79185d63332d16c88eb0e83c34f1b2018a33e42629b38efe36ac
MD5 d21c9d1d1f5fa241a5143f009c7f285f
BLAKE2b-256 2528411fb9e6e51100523f0ad527460877acb3c6236cd6b62a4bcf2d85c669fe

See more details on using hashes here.

Supported by

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