pytokei

Python bindings to tokei, the Rust library to count code quickly.


Keywords
python, rust
License
MIT
Install
pip install pytokei==0.2.0

Documentation

pytokei


Pytokei is a python binding to tokei:

Tokei is a program that displays statistics about your code. Tokei will show the number of files, total lines within those files and code, comments, and blanks grouped by language.


This wrapper allows to obtain the same reports directly from python.

$ python
>>> import pytokei
>>> from rich import print
>>> langs = pytokei.Languages()
>>> langs.get_statistics(["."], ["tests/data", "requirements"], pytokei.Config())
>>> print(langs.report_compact_plain())
{
    'YAML': {'blanks': 6, 'code': 63, 'comments': 0, 'files': 1, 'lines': 69},
    'Python': {'lines': 376, 'blanks': 89, 'files': 2, 'code': 280, 'comments': 7},
    'Makefile': {'code': 18, 'lines': 26, 'comments': 0, 'blanks': 8, 'files': 1},
    'Markdown': {'code': 0, 'blanks': 37, 'files': 10, 'comments': 52, 'lines': 89},
    'Rust': {'blanks': 23, 'comments': 23, 'code': 317, 'lines': 363, 'files': 7},
    'TOML': {'code': 14, 'comments': 2, 'lines': 20, 'blanks': 4, 'files': 2}
}

It includes a small CLI for simple use cases (run against the pytokei's folder):

$ pytokei pytokei
                        pytokei                         
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ language   โ”ƒ Files โ”ƒ Lines โ”ƒ Code โ”ƒ Comments โ”ƒ Blanks โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Rust       โ”‚     9 โ”‚  1011 โ”‚  846 โ”‚       49 โ”‚    116 โ”‚
โ”‚ Python     โ”‚     5 โ”‚   568 โ”‚  436 โ”‚       13 โ”‚    119 โ”‚
โ”‚ Markdown   โ”‚    11 โ”‚   423 โ”‚  123 โ”‚      179 โ”‚    121 โ”‚
โ”‚ Plain Text โ”‚     4 โ”‚   133 โ”‚    0 โ”‚      133 โ”‚      0 โ”‚
โ”‚ TOML       โ”‚     3 โ”‚    75 โ”‚   59 โ”‚        6 โ”‚     10 โ”‚
โ”‚ YAML       โ”‚     1 โ”‚    69 โ”‚   63 โ”‚        0 โ”‚      6 โ”‚
โ”‚ Makefile   โ”‚     1 โ”‚    26 โ”‚   18 โ”‚        0 โ”‚      8 โ”‚
โ”‚ Dockerfile โ”‚     1 โ”‚    16 โ”‚    7 โ”‚        3 โ”‚      6 โ”‚
โ”‚ Shell      โ”‚     3 โ”‚    12 โ”‚   12 โ”‚        0 โ”‚      0 โ”‚
โ”‚ Autoconf   โ”‚     3 โ”‚     7 โ”‚    7 โ”‚        0 โ”‚      0 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

For more information about tokei, please visit the original repo.

PyPI pyversions example workflow license

Installation

pip install pytokei

Requires Python >= 3.7.

Binaries are available for:

  • Linux: x86_64, aarch64, i686, armv7l, musl-x86_64 & musl-aarch64
  • MacOS: x86_64 & arm64 (except python 3.7)
  • Windows: amd64 & win32

Otherwise, you can install from source which requires Rust stable to be installed.

Why this library?

Wanted to practice rust, and taking this library to python seemed like a good opportunity. It's awesome, and maybe more people coming from python will find something useful to do with it.

But really? Just for fun :)

What times should you expect?

Running Languages.get_statistics against cpython takes a little less than 200 milliseconds.

Some more info should be found in the docs.

Development

You will need:

  • maturin to compile the library

  • maturin develop / make develop to compile the code.

From python side:

Run make install-dev inside a virtual environment, make test, make mypy and make format to ensure everything is as expected, and make docs to build the documentation.

There are some problems when building the docs with mkdocstrings, a reminder is in the following github issue. For the moment, it seems that the best option is to remove the .so file and build the docs without it.

To create a new release:

  • Update the version in Cargo.toml.
  • Create a new tag to run the github action workflow.
  • git push --atomic origin main tag-name