Skip to main content

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.

    ✅ 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 !

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

Metadata

Release files for linestats 1.2.1

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

Source distribution (sdist)

Source distribution for linestats 1.2.1
File Size Uploaded Provenance Actions
linestats-1.2.1.tar.gz 20.1 kB Without provenance Details for linestats-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for linestats 1.2.1
File Interpreter ABI Platform Actions
linestats-1.2.1-py3-none-any.whl Python 3 none any Details for linestats-1.2.1-py3-none-any.whl

Total release size: 41.1 kB

Release files / linestats-1.2.1.tar.gz

Download URL linestats-1.2.1.tar.gz
Size 20.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4daaa5addc90480a1f2945eef9c8b8e9dec93d84f5f677e4ec75f48549b79d6f
BLAKE2b-256 checksum
How to use checksums
57524c60281c84644de3213b53e0013a6163b67848e963d734af539a0d95391d
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / linestats-1.2.1-py3-none-any.whl

Download URL linestats-1.2.1-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b7f59483e6c5e84a32bd6206039b610bed8868b914986f46ebddcaae876d660
BLAKE2b-256 checksum
How to use checksums
d5be426022f5f98574d4fb77b14ec8455406ed0598ab1ad41295146934a3077d
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for linestats 1.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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