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 it 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, and
    • -v to print the linestats version.
  • 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)
    

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

All notable changes to linestats will be documented below.

The format is inspired from Keep a Changelog. This project adheres to Semantic Versioning.

[1.0.0]

Added

  • [F.P.A. Vogt, 2020-06-20] Add option to save output to file (#2).
  • [F.P.A. Vogt, 2020-06-19] Initial module assembly + Github upload.

Fixed:

  • [F.P.A. Vogt, 2020-06-20] Fix bug for scripts with last lines containing only spaces (#2).

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

Uploaded Source

Built Distribution

linestats-1.0.0-py3-none-any.whl (19.8 kB view hashes)

Uploaded 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