Skip to main content

codrill

Extract code exercises from code itself

To install: pip install codrill

There's so many extremely useful gems in builtins (there's also a lot of useless noise). Personally, I use collections, itertools and functools as well as map and zip constantly. Also recently, contextlib.

Sometimes, you can avoid a many line function simply by putting a few right builtin elements together.

Knowing what's out there is a first step.

But it's not enough. You got to think of these components when a problem arises. So you need actual practice.

For example, what would the one liner be to implement this function:

def nth(iterable, n, default=None):
    "Returns the nth item or a default value"
    return ...  # fill in the blanks

See the answer in itertools recipes. The latter contains many more opportunities for such exercises.

But it would be nice to be able to extract these automatically from code. So here's my little version of that.

Examples:

more_itertools

To get random exercises for the more_itertools module (need to pip install it if you don't have it).

from codrill import Exercises
import more_itertools.more as m
e = Exercises(m)
# and then repeatedly ask for random exercises.
e.print_random_exercise()

This Exercises class is meant to be subclassed to include tracking of exercises presented, and possibly performance metrics (explicitly self-declared or inferred from a response). These statistics can then be used to chose the exercises not randomly, but so as to optimize learning.

itertools recipes

I don't know of a pip installable package for the itertools recipes so I copied that code and put in a local file under .../ut/util/uiter.py.

Using that setup, in the following I'll print out all the exercises that have no more than 30 lines of docs and 4 lines of code. This filtering helps to not get exercises that are too large in their description (the docs) or their solution (the code).

from codrill import snippets_of_funcs
import ut.util.uiter as m
# find the file for uiter here:
#   https://github.com/thorwhalen/ut/blob/master/util/uiter.py

for snippet in snippets_of_funcs(m, max_code_lines=4, max_doc_lines=30):
    if not snippet.startswith('_'):
        print(snippet)
        print()

Metadata

Release files for codrill 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for codrill 0.0.2
File Size Uploaded
codrill-0.0.2.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for codrill 0.0.2
File Interpreter ABI Platform
codrill-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / codrill-0.0.2.tar.gz

Download URL codrill-0.0.2.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a58e7e5589f8be1dc29d746ae71dece711deafe3dbd44b26bb4e3932072ca99a
BLAKE2b-256 checksum
How to use checksums
4f25035c28e826df835b56d902020d8e2f58cb01c69c2ae81499560b1ab1b182
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / codrill-0.0.2-py3-none-any.whl

Download URL codrill-0.0.2-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6915b06cc5c4e5bb8344e6397955192d149710354567f60a5709bbb004165f2f
BLAKE2b-256 checksum
How to use checksums
42c14c10230c5446b29612fcf45a7e68f8d0f85cfe1db596a154ca4f926da730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page