Skip to main content

Python module to count the number of scripted, commented, docstringed, and empty lines in Python code.

Project description

linestats

github last-commit issues pypi

Ever wanted/needed to know how many lines are empty, comments, docstrings or actual code in some Python scripts of yours ?

Presenting linestats: a small Python module to count the number of scripted, commented, docstringed, and empty lines in Python code. Here is what linestats contains and what it does: linestats.txt

Table of contents

Installation

linestats is available on pypi. The following command should take care of things:

pip install linestats

Alternatively, you can also get the code from its dedicated Github repository.

Running

Using linestats is fairly straightforward. You can do so (1) in any decent terminal using the in-built entry point, or (2) from within a Python shell by importing the corresponding module. Both methods are the same, and here's how they work:

  • from a terminal: The basic syntax is

    linestats -p some/path/to/dir/or/file.py
    

    This will process the file.py (or all .pyfile located at the indicated location, if a directory is specified). The other options are:

    • -h for help,
    • -r to run a recursive search for .py files in subfolders,
    • -s output_file.txt to save the statistics to file instead of sys.stdout,
    • -v to print the linestats version, and
    • -w to print detailed info for every file.
  • from a Python script/shell:

    import linestats
    from pathlib import Path
    
    p = Path('some', 'path', 'to', 'dir', 'or', 'file.py')
    s = Path('.', 'output_file.py')
    
    linestats.extract_line_stats(p, recursive=True, save_to_file=s, verbose=False)
    

Limitations

linestats is extremely basic, and the output should be largely self-explanatory. Still, here are a few aspects to keep in mind when using this code:

  1. linestats will only process .py files. At present, there is no way to force the processing of other file types.
  2. The different lines are counted in the following category order: docstrings -> comments -> emtpy -> code. Each step feeds the following only with the lines that do not belong to it. This implies, for example, that
    • an empty line inside a docstring is counted as a docstring line,
    • a docstring line starting with # is also counted as a docstring line,
    • a = 4 # A comment on the same line as code would be counted as a code line, and
    • each line belongs to a single category, is counted once only, so the sum of all the categories adds up to 100%.

Changelog

See here.

Information for dev work

Code of conduct

This project and everyone participating in it is governed by the linestats Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to frederic.vogt@alumni.anu.edu.au.

Branching model

The develop branch is the default one, where all contributions get merged. When a new release is warranted, a Pull Request to the master branch is issued. This implies that the master branch will always reflect the state of the latest release of the code.

Contributors are required to work in their own branches, and issue Pull Requests into the develop branch when appropriate.

The master and develop branches are protected.

CI/CD

Automated CI/CD checks are triggered upon Pull Requests being issued towards the develop and master branches. At the time being, they are implemented using dedicated Github Actions specified under .github/workflows. These checks include:

  • code linting using pylint
  • code testing using pytest
  • check that the CHANGELOG was updated
  • check that the code version was incremented (for PR to master only)

Release mechanism

Assuming the content of develop is ready for a release, here are the steps to follow:

  1. Issue a PR from develop into master. Merge it if all looks ok.

    :white_check_mark: The CI_check_version Action will run to check that the version has been increased.

  2. Create a new release from Github. This step has not been automated yet. Make sure to enter the same version number as set in the code !

    :white_check_mark: Upon publication of the release, the CI_pypi Action will directly upload the code to testpypi and pypi. Make sure it succeeds !

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

linestats-1.2.1.tar.gz (20.1 kB view details)

Uploaded Source

Built Distribution

linestats-1.2.1-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file linestats-1.2.1.tar.gz.

File metadata

  • Download URL: linestats-1.2.1.tar.gz
  • Upload date:
  • Size: 20.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for linestats-1.2.1.tar.gz
Algorithm Hash digest
SHA256 4daaa5addc90480a1f2945eef9c8b8e9dec93d84f5f677e4ec75f48549b79d6f
MD5 f7e79a64ba3f92532a4b2e5ef1b90b69
BLAKE2b-256 57524c60281c84644de3213b53e0013a6163b67848e963d734af539a0d95391d

See more details on using hashes here.

File details

Details for the file linestats-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: linestats-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for linestats-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b7f59483e6c5e84a32bd6206039b610bed8868b914986f46ebddcaae876d660
MD5 88e4b4d410efc2c62660f1ca297deb30
BLAKE2b-256 d5be426022f5f98574d4fb77b14ec8455406ed0598ab1ad41295146934a3077d

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