Skip to main content

This project is currently deprecated and may be archived. If you're looking for something similar, you could try bellybutton or writing a custom checker in pylint instead.

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

See the Contributing docs

Contributors

Thanks goes to these wonderful people emoji key:

Josh Doncaster Marsiglio
Josh Doncaster Marsiglio

💻
Rohit Jain
Rohit Jain

💻
Chris Abiad
Chris Abiad

💻
Francois Campbell
Francois Campbell

💻
Monica Moore
Monica Moore

🎨
Jay Crumb
Jay Crumb

📖
Jake Bolam
Jake Bolam

🚇
Shouvik D'Costa
Shouvik D'Costa

📖
Siavash Bidgoly
Siavash Bidgoly

🚇
Noah Negin-Ulster
Noah Negin-Ulster

💻
Vardan Nadkarni
Vardan Nadkarni

💻
greenkeeper[bot]
greenkeeper[bot]

🚇
Kazushige Tominaga
Kazushige Tominaga

💻

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

Credits

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

Release files for codewatch 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codewatch 1.0.0
File Size Uploaded
codewatch-1.0.0.tar.gz 21.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for codewatch 1.0.0
File Interpreter ABI Platform
codewatch-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 53.4 kB

Release files / codewatch-1.0.0.tar.gz

Download URL codewatch-1.0.0.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a5e9157de9df1184fd2fa8e4a82e17c9751617309fb497ee1171f61d5674a0b1
BLAKE2b-256 checksum
How to use checksums
fc9bbf53d3a7fc4e22367b3e5d5828aeac6f3b6f1b6dc0f9ac902d7d772d5879
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.7.1 requests/2.26.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

Release files / codewatch-1.0.0-py2.py3-none-any.whl

Download URL codewatch-1.0.0-py2.py3-none-any.whl
Size 31.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cb1800dcf817d7be18ae6455d51028d3cdc11f3957570978fe35453ce3db4924
BLAKE2b-256 checksum
How to use checksums
ddfb04d55b56ededdf8f5450737ebb2431490518ba848451e026fd5e82c4df2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.7.1 requests/2.26.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.15

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.0

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

3 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page