Extract code exercises from code itself
Project description
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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codrill-0.0.2.tar.gz.
File metadata
- Download URL: codrill-0.0.2.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a58e7e5589f8be1dc29d746ae71dece711deafe3dbd44b26bb4e3932072ca99a
|
|
| MD5 |
aa3deba9d9b245015c3c1634bcefeaa8
|
|
| BLAKE2b-256 |
4f25035c28e826df835b56d902020d8e2f58cb01c69c2ae81499560b1ab1b182
|
File details
Details for the file codrill-0.0.2-py3-none-any.whl.
File metadata
- Download URL: codrill-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6915b06cc5c4e5bb8344e6397955192d149710354567f60a5709bbb004165f2f
|
|
| MD5 |
3ec6c3bab2f6b9c8ca44c2064a9d7c1e
|
|
| BLAKE2b-256 |
42c14c10230c5446b29612fcf45a7e68f8d0f85cfe1db596a154ca4f926da730
|