Pyteen

This is an emerging package for managing a collection of self-contained, short Python code snippets that are all tested and validated, easy to contribute to, and simple to reuse.

Code size

The main aspect here is code size, still counted in lines of lines (LOC). This project started with a strict limit of ten lines of code because in Python this can already be enough to solve some useful little tasks, and, unsurpringly, the first chosen project name was “tenliners”. But of course, not all code lines are equally useful or effective, which is why it makes little sense to count comments or docstrings or empty lines (non-code). In fact, you only really care about “effective code”, code that has the most effect (or value) to solve your task or demonstrate something. Please find more details about effective code below.

Now the number ten is somewhat arbitrary (why not 11?) and it can be hard or impossible to express some interesting things with ten or less lines even in Python. Therefore “ten” was stretched a bit into “teen” leading to the name “Pyteen” and, hence, a max. limit of 19 lines of effective code. Let’s see where that goes!

Effective code

Apart from comments or docstrings or empty lines there is also very little effect in lines with only opening or closing brackets, braces or parentheses which are often typed manually to make data structures more readable, but also generated by code formatting tools like Black. The same holds with imports which only enable you to use some modules. What is left is code that really does something when it executes. And this is what Pyteen wants to measure.

Example

“Sieve of Eratosthenes” code with 12 effective lines highlighted
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def sieve_of_eratosthenes(n):
    """Yield primes in [2..n] using the "Sieve of Eratosthenes".
    """
    assert type(n) == int
    # Create a Boolean array "prime[0..n]" and initialize
    # all entries in it as true. A value in prime[i] will
    # finally be false if i is not a prime, else true.
    p, prime = 2, [True for i in range(n + 1)]
    while p * p <= n:
        # If prime[p] is not changed, then it is a prime:
        if prime[p]:
            # Update all multiples of p:
            for i in range(p * 2, n + 1, p):
                prime[i] = False
        p += 1
    prime[0:2] = [False, False]
    # Yield all prime numbers < n:
    for p in range(n + 1):
        if prime[p]:
            yield p