Skip to content

pytokei

Pytokei is a python binding to tokei, from their repo:


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.

For more information about tokei, please visit its repo.

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.

To use the CLI, install with the needed dependencies (maybe with pipx).

pipx install pytokei[cli]

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.

Time comparison tokei and pytokei

Just to see if the binding makes the code slower, a test was done against the cpython repository.

The timing isn't directly comparable, Rust's version prints the average time, while Python's version shows the minimum, but it should be enough to get an idea. A better test should be defined in the future.

The python version uses docker. A print like the following should appear:

$ sh run_dockerfile_py.sh
...
Successfully tagged bench_from_pytokei:latest
==================
Timing python run:
------------------
100 loops, best of 1: 194 msec per loop
==================

The rust version needs rust-script installed.

$ sh bench_rs.sh         
================
Timing rust run:
----------------
100 loops, average time was: 0.17748523967000002 sec
================