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.

Installation

Python: >=2

Execute the following in your terminal:

pip install codewatch

Usage

codewatch codewatch_config_module

codewatch_config_module is a file that should contain the following two methods:

1. Visit all directories:

def directory_filter(_dir_name):
    return True

2. Visit all files:

def file_filter(_file_name):
    return True

Tune these filters to suit your needs.

Then, you should use the @visit decorator. It follows a similar API to ast.NodeVisitor:

from codewatch import visitor


def _count_import(stats):
    stats.increment('total_imports_num')

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

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

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

{
    "total_imports_num": 763
}

Then, 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
    newStat = stats.get('total_imports_num')
    err = 'There were {} total imports detected which exceeds threshold of {}'.format(newStat, threshold)
    return newStat <= 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

Contributing

Helpful resources

TODO

Contributors

Thanks goes to these wonderful people! (Emoji key)


Josh Doncaster Marsiglio

💻

Rohit Jain

💻

Chris Abiad

💻

Francois Campbell

🤔

Monica Moore

🎨

Jake Bolam

🚇

Shouvik D'Costa

🚇

Siavash Bidgoly

🚇

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.15.tar.gz (17.5 kB view hashes)

Uploaded Source

Built Distribution

codewatch-0.0.15-py2.py3-none-any.whl (24.2 kB view hashes)

Uploaded Python 2 Python 3

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