Skip to main content

Count lines of Python code in directories

Project description

Countsy ๐Ÿ“

A fast, customizable line counter for Python projects (with plans to expand!)

Features โœจ

  • Count lines in Python files (with more languages coming soon!)
  • Flexible filtering: Tracks comments, blank lines, or both
  • Progress bars (optional) for large directories
  • Faster than cloc for pure Python projects (details below) and more detailed than wc -l

Installation โšก

$ pip install countsy

Usage ๐Ÿš€

Basic Command

$ countsy /path/to/folder

Note that /path/to/folder is not required. If left unfilled, i.e. calling countsy with no arguments /path/to/folder is set to the current directory.

Sample Output

$  countsy

>  Total Python-Files in current directory: 1129
>  Total lines in folder:  376190

Option 2: Using Diff Syntax (for clear input/output separation)

$   countsy

> Total Python-Files in current directory: 1129
> Total lines in folder: 376190

Flags ๐ŸŽ›๏ธ

Flag Description Default
--pbar Show progress bar False
--track-comments Exclude single/multi-line comments False
--track-blank-lines Exclude empty lines False
--track Exclude both comments and blank lines False

Example

$ countsy /path/to/folder --track --tqdm

>  100%|โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 1129/1129 [00:00<00:00, 4505.66it/s]
>  Total Python-Files in current directory: 1129
>  Total lines of Python-Code in folder: 212845
>  Total blank lines in Python-Files in folder: 28168
>  Total comments in Python-Files in folder: 135177
>  Total lines in folder: 376190

Comparison to bash and cloc

countsy:

$  time countsy --track
>  Total Python-Files in current directory: 1129
>  Total lines of Python-Code in folder: 212845
>  Total blank lines in Python-Files in folder: 28168
>  Total comments in Python-Files in folder: 135177
>  Total lines in folder: 376190
>  countsy --track  0.10s user 0.04s system 98% cpu 0.147 total

bash:

$ time find . -name '*.py' -exec cat {} \; | wc -l
> 375061
> find . -name '*.py' -exec cat {} \;  0.63s user 0.88s system 80% cpu 1.870 total
> wc -l  0.02s user 0.00s system 1% cpu 1.870 total

cloc:

time cloc --include-ext=py .
>    1438 text files.
>    1226 unique files.                                          
>    1847 files ignored.

>  github.com/AlDanial/cloc v 2.04  T=2.36 s (424.8 files/s, 152893.8 lines/s)
>  -------------------------------------------------------------------------------
>  Language                     files          blank        comment           code
>  -------------------------------------------------------------------------------
>  Python                        1001          42014          87199         231061
>  -------------------------------------------------------------------------------
>  SUM:                          1001          42014          87199         231061
>  -------------------------------------------------------------------------------
> cloc --include-ext=py .  1.78s user 0.20s system 78% cpu 2.502 total

As you can see, countsy is way faster than conventional methods, which is especially useful if you don't need advanced settings.

Disclaimerโš ๏ธ

  • tqdm is required for progress bars
  • The differences between countsy, bash and cloc result from a differentiation between multiline strings, PLOC and LLOC.
  • The first time I ran cloc on my GitHub folder (results above!) it took around 11 minutes. Maybe there is some indexing going on or caching. Subsequent runs take around 3 seconds, which is fast, but still way slower than countsy
  • Can not encode non-utf-8 python files (working on that)
  • This tool does not count multiline strings as a line. This causes unexpected behaviour if a logical line starts, inits the string in the same line and ends the str in a blank line (this is very uncommon but still possible) [Working on that with tokenizing and parsing tools] Example:\

Let main.py

(1) string = """
(2)        hello
(3)        hi
(4)"""
(5)
$ countsy --track main.py
>
> --track test.py
>  Total lines of Python-Code: 3
>  Total blank lines in Python-Files: 0
>  Total comments in Python-Files: 2
>  Total lines: 5

As you can see, this behaviour is divergent from expectation which is 5 Lines, 0 Comments. But it makes sense in that from line 4 the comment begins. This case is obviously not catastrophic but consider an example where you define a multiline string as in main.py at the beginning. Let the hypothetical file have 10000 lines with not multiline comments. The output would be 3 Lines and 9996 Comments. This is catastrophic.

Missing Modules? ๐Ÿ”ง

pip install tqdm

Roadmap ๐Ÿ—บ๏ธ

  • Support for more languages (JavaScript, C, C++ etc.)
  • Optimize speed for large codebases
  • Optional dependencies

Contributing ๐Ÿค

PRs and feature requests are welcome!

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

countsy-1.0.9.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

countsy-1.0.9-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file countsy-1.0.9.tar.gz.

File metadata

  • Download URL: countsy-1.0.9.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for countsy-1.0.9.tar.gz
Algorithm Hash digest
SHA256 f7ee1e83ff9d92d601f8ec996dd38beaaa56b76e0a15172fe1cf3e79c9010caf
MD5 4ae8fa5b1766316503490b608e65c887
BLAKE2b-256 d52dc5277734d74a986e32c11c6cdee3b80a52e2b0fdb4350e5dabf0d1867bb2

See more details on using hashes here.

File details

Details for the file countsy-1.0.9-py3-none-any.whl.

File metadata

  • Download URL: countsy-1.0.9-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for countsy-1.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 877c829c02225a4c1347a517bb161419e8ee436a1b853c54e7afab05891e027b
MD5 d9cf86eec01cdd33eb19a2a0ae52c574
BLAKE2b-256 9aca6ad58e8a92d2970298417b53064e884917e72e66a4b26cc02700d13bac01

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page