# skill
Extract code exercises from code itself
To install: ```pip install skill```
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:
```python
def nth(iterable, n, default=None):
"Returns the nth item or a default value"
return ... # fill in the blans
```
See the answer in [itertools recipes](https://docs.python.org/3/library/itertools.html#itertools-recipes).
The latter contains many more oportunities 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).
```python
from skill 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](https://docs.python.org/3/library/itertools.html#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).
```python
from skill 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()
```
The output is:
```python
accumulate(iterable, func=<built-in function add>)
'''Return running totals'''
batch(iterable, n=1, return_tail=True)
'''Iterator yielding batches of size n of the input iterable.
See also grouper and seq_batch.
:param iterable: in put iterable
:param n: batch size
:param return_tail: whether to return the last chunk (even if it's length is not the batch size)
:return: an iterator'''
consume(iterator, n)
'''Advance the iterator n-steps ahead. If n is none, consume entirely.'''
first_elements_and_full_iter(it, n=1)
'''Given an iterator it, returns the pair (first_elements, it) (where it is the full original
iterator).
This is useful when you need to peek into an iterator before actually processing it (say
because the way you will process it will depend on what you see in there).
:param it: an iterator
:param n: the number of first elements you want to peek at
:return:
first_elements: A list of the first n elements of the iterator
it: The original (full) iterator'''
flatten(listOfLists)
'''Flatten one level of nesting'''
grouper(iterable, n=1, fillvalue='drop')
'''Returns an iterable that feeds tuples of size n corresponding to chunks of the input iterable.
See also batch and seq_batch.
:param iterable: Input iterable
:param n: chunk (batch) size
:param fillvalue: The element to use to fill the last chunk, or 'drop' to keep only elements of the iterable,
meaning that the last tuple grouper will feed will be of size < n
:return: An iterable that feeds you chunks of size n of the input iterable
>>> list(grouper('ABCDEFG', 3, 'x'))
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]
>>> list(grouper('ABCDEFG', 3, 'drop'))
[('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]'''
grouper_no_fill(iterable, n)
'''grouper_no_fill('ABCDEFG', 3) --> ABC DEF G'''
iter_except(func, exception, first=None)
'''Call a function repeatedly until an exception is raised.
Converts a call-until-exception interface to an iterator interface.
Like __builtin__.iter(func, sentinel) but uses an exception instead
of a sentinel to end the loop.
Examples:
bsddbiter = iter_except(db.next, bsddb.error, db.first)
heapiter = iter_except(functools.partial(heappop, h), IndexError)
dictiter = iter_except(d.popitem, KeyError)
dequeiter = iter_except(d.popleft, IndexError)
queueiter = iter_except(q.get_nowait, Queue.Empty)
setiter = iter_except(s.pop, KeyError)'''
ncycles(iterable, n)
'''Returns the sequence elements n times'''
nth(iterable, n, default=None)
'''Returns the nth item or a default value'''
padnone(iterable)
'''Returns the sequence elements and then returns None indefinitely.
Useful for emulating the behavior of the built-in map() function.'''
pairwise(iterable)
'''s -> (s0,s1), (s1,s2), (s2, s3), ...'''
powerset(iterable)
'''powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)'''
print_iter_progress(iterator, print_progress_every=None, header_template='{hour:02.0f}:{minute:02.0f}:{second:02.0f} - iteration {iteration}', data_msg_intro_str='', data_to_string=None)
'''Wraps an iterator, allowing one to use the iterator as one would, but will print progress messages every
print_progress_every iterations.
header of print string can be specified through header_template
data information can be printed too through data_msg_intro_str and data_to_string (a function) specifications
Examples (but the doctest won't work, since time will be different):
>>> for x in print_iter_progress(xrange(50), print_progress_every=10):
... pass
...
9:30:5 - iteration 0
9:30:5 - iteration 10
9:30:5 - iteration 20
9:30:5 - iteration 30
9:30:5 - iteration 40
>>> for x in print_iter_progress(xrange(50),
... print_progress_every=15,
... data_msg_intro_str="data times two is: {data_str}",
... data_to_string=lambda x: x * 2):
... pass
...
9:30:55 - iteration 0data times two is: 0
9:30:55 - iteration 15data times two is: 30
9:30:55 - iteration 30data times two is: 60
9:30:55 - iteration 45data times two is: 90'''
quantify(iterable, pred=<class 'bool'>)
'''Count how many times the predicate is true'''
random_combination(iterable, r)
'''Random selection from combinations(iterable, r)'''
random_combination_with_replacement(iterable, r)
'''Random selection from combinations_with_replacement(iterable, r)'''
random_permutation(iterable, r=None)
'''Random selection from itertools.permutations(iterable, r)'''
random_product(*args, **kwds)
'''Random selection from itertools.product(*args, **kwds)'''
random_subset(iterator, K)
'''Uses reservoir sampling to get a sample from an iterator without knowing how many points there are
in advance.'''
repeatfunc(func, times=None, *args)
'''Repeat calls to func with specified arguments.
Example: repeatfunc(random.random)'''
roundrobin(*iterables)
'''roundrobin('ABC', 'D', 'EF') --> A D E B F C'''
running_mean(it, chk_size=2, chk_step=1)
'''Running mean (moving average) on iterator.
Note: When input it is list-like, ut.stats.smooth.sliders version of running_mean is 4 times more efficient with
big (but not too big, because happens in RAM) inputs.
:param it: iterable
:param chk_size: width of the window to take means from
:return:
>>> list(running_mean([1, 3, 5, 7, 9], 2))
[2.0, 4.0, 6.0, 8.0]
>>> list(running_mean([1, 3, 5, 7, 9], 2, chk_step=2))
[2.0, 6.0]
>>> list(running_mean([1, 3, 5, 7, 9], 2, chk_step=3))
[2.0, 8.0]
>>> list(running_mean([1, 3, 5, 7, 9], 3))
[3.0, 5.0, 7.0]
>>> list(running_mean([1, -1, 1, -1], 2))
[0.0, 0.0, 0.0]
>>> list(running_mean([-1, -2, -3, -4], 3))
[-2.0, -3.0]'''
seq_batch(seq, n=1, return_tail=True, fillvalue=None)
'''An iterator of equal sized batches of a sequence.
See also grouper and seq_batch.
:param seq: a sequence (should have a .__len__ and a .__getitem__ method)
:param n: batch size
:param return_tail:
* True (default): Return the tail (what's remaining if the seq len is not a multiple of the batch size),
as is (so the last batch might not be of size n
* None: Return the tail, but fill it will the value specified in the fillvalue argument, to make it size n
* False: Don't return the tail at all
:param fillvalue: Value to be used to fill the tail if return_tail == None
>>> seq = [1, 2, 3, 4, 5, 6, 7]
>>> list(seq_batch(seq, 3, False))
[[1, 2, 3], [4, 5, 6]]
>>> list(seq_batch(seq, 3, True))
[[1, 2, 3], [4, 5, 6], [7]]
>>> list(seq_batch(seq, 3, None))
[[1, 2, 3], [4, 5, 6], [7, None, None]]
>>> list(seq_batch(seq, 3, None, 0))
[[1, 2, 3], [4, 5, 6], [7, 0, 0]]'''
tabulate(function, start=0)
'''Return function(0), function(1), ...'''
take(n, iterable)
'''Return first n items of the iterable as a list'''
tee_lookahead(t, i)
'''Inspect the i-th upcomping value from a tee object
while leaving the tee object at its current position.
Raise an IndexError if the underlying iterator doesn't
have enough values.'''
unique_everseen(iterable, key=None)
'''List unique elements, preserving order. Remember all elements ever seen.
>>> list(unique_everseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D']
>>> import string
>>> list(unique_everseen('ABBCcAD', string.lower))
['A', 'B', 'C', 'D']'''
unique_justseen(iterable, key=None)
'''List unique elements, preserving order. Remember only the element just seen.
>>> list(unique_justseen('AAAABBBCCDAABBB'))
['A', 'B', 'C', 'D', 'A', 'B']
>>> import string
>>> list(unique_justseen('ABBCcAD', string.lower))
['A', 'B', 'C', 'A', 'D']'''
window(seq, n=2)
'''Returns a sliding window (of width n) over data from the iterable'''
```
Raw data
{
"_id": null,
"home_page": "https://github.com/thorwhalen/skill",
"name": "skill",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Thor Whalen",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/b8/35/edd3f9e2afea61e8854976fd3af0c1afe94869c02882ebdfd20b50a39c5c/skill-0.0.7.tar.gz",
"platform": "any",
"description": "# skill\nExtract code exercises from code itself\n\nTo install:\t```pip install skill```\n\nThere's so many extremely useful gems in builtins (there's also a lot of useless noise).\nPersonally, I use `collections`, `itertools` and `functools` as well as `map` and `zip` constantly. \nAlso recently, `contextlib`. \n\nSometimes, you can avoid a many line function simply by putting a few right builtin elements together. \n\nKnowing what's out there is a first step. \n\nBut it's not enough. You got to think of these components when a problem arises. \nSo you need actual practice. \n\nFor example, what would the one liner be to implement this function:\n\n```python\ndef nth(iterable, n, default=None):\n \"Returns the nth item or a default value\"\n return ... # fill in the blans\n```\n\nSee the answer in [itertools recipes](https://docs.python.org/3/library/itertools.html#itertools-recipes).\nThe latter contains many more oportunities for such exercises. \n\nBut it would be nice to be able to extract these automatically from code. So here's my little version of that.\n\n# Examples:\n\n## `more_itertools`\n\nTo get random exercises for the `more_itertools` module \n(need to pip install it if you don't have it).\n\n```python\nfrom skill import Exercises\nimport more_itertools.more as m\ne = Exercises(m)\n# and then repeatedly ask for random exercises.\ne.print_random_exercise()\n```\n\nThis `Exercises` class is meant to be subclassed to include tracking of exercises presented,\nand possibly performance metrics (explicitly self-declared or inferred from a response).\nThese statistics can then be used to chose the exercises not randomly, but so as to\noptimize learning.\n\n\n## itertools recipes\n\nI don't know of a pip installable package for the \n [itertools recipes](https://docs.python.org/3/library/itertools.html#itertools-recipes)\nso I copied that code and put in a local file under `.../ut/util/uiter.py`.\n\nUsing that setup, in the following I'll print out all the exercises that \nhave no more than 30 lines of docs and 4 lines of code. \nThis filtering helps to not get exercises that are too large in their \ndescription (the docs) or their solution (the code).\n\n```python\nfrom skill import snippets_of_funcs\nimport ut.util.uiter as m \n# find the file for uiter here: \n# https://github.com/thorwhalen/ut/blob/master/util/uiter.py\n\nfor snippet in snippets_of_funcs(m, max_code_lines=4, max_doc_lines=30):\n if not snippet.startswith('_'):\n print(snippet)\n print()\n```\n\nThe output is:\n\n```python\naccumulate(iterable, func=<built-in function add>)\n'''Return running totals'''\n\nbatch(iterable, n=1, return_tail=True)\n'''Iterator yielding batches of size n of the input iterable.\nSee also grouper and seq_batch.\n:param iterable: in put iterable\n:param n: batch size\n:param return_tail: whether to return the last chunk (even if it's length is not the batch size)\n:return: an iterator'''\n\nconsume(iterator, n)\n'''Advance the iterator n-steps ahead. If n is none, consume entirely.'''\n\nfirst_elements_and_full_iter(it, n=1)\n'''Given an iterator it, returns the pair (first_elements, it) (where it is the full original\niterator).\nThis is useful when you need to peek into an iterator before actually processing it (say\nbecause the way you will process it will depend on what you see in there).\n:param it: an iterator\n:param n: the number of first elements you want to peek at\n:return:\n first_elements: A list of the first n elements of the iterator\n it: The original (full) iterator'''\n\nflatten(listOfLists)\n'''Flatten one level of nesting'''\n\ngrouper(iterable, n=1, fillvalue='drop')\n'''Returns an iterable that feeds tuples of size n corresponding to chunks of the input iterable.\nSee also batch and seq_batch.\n:param iterable: Input iterable\n:param n: chunk (batch) size\n:param fillvalue: The element to use to fill the last chunk, or 'drop' to keep only elements of the iterable,\nmeaning that the last tuple grouper will feed will be of size < n\n:return: An iterable that feeds you chunks of size n of the input iterable\n\n>>> list(grouper('ABCDEFG', 3, 'x'))\n[('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]\n>>> list(grouper('ABCDEFG', 3, 'drop'))\n[('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]'''\n\ngrouper_no_fill(iterable, n)\n'''grouper_no_fill('ABCDEFG', 3) --> ABC DEF G'''\n\niter_except(func, exception, first=None)\n'''Call a function repeatedly until an exception is raised.\n\nConverts a call-until-exception interface to an iterator interface.\nLike __builtin__.iter(func, sentinel) but uses an exception instead\nof a sentinel to end the loop.\n\nExamples:\n bsddbiter = iter_except(db.next, bsddb.error, db.first)\n heapiter = iter_except(functools.partial(heappop, h), IndexError)\n dictiter = iter_except(d.popitem, KeyError)\n dequeiter = iter_except(d.popleft, IndexError)\n queueiter = iter_except(q.get_nowait, Queue.Empty)\n setiter = iter_except(s.pop, KeyError)'''\n\nncycles(iterable, n)\n'''Returns the sequence elements n times'''\n\nnth(iterable, n, default=None)\n'''Returns the nth item or a default value'''\n\npadnone(iterable)\n'''Returns the sequence elements and then returns None indefinitely.\n\nUseful for emulating the behavior of the built-in map() function.'''\n\npairwise(iterable)\n'''s -> (s0,s1), (s1,s2), (s2, s3), ...'''\n\npowerset(iterable)\n'''powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)'''\n\nprint_iter_progress(iterator, print_progress_every=None, header_template='{hour:02.0f}:{minute:02.0f}:{second:02.0f} - iteration {iteration}', data_msg_intro_str='', data_to_string=None)\n'''Wraps an iterator, allowing one to use the iterator as one would, but will print progress messages every\nprint_progress_every iterations.\n\nheader of print string can be specified through header_template\ndata information can be printed too through data_msg_intro_str and data_to_string (a function) specifications\n\nExamples (but the doctest won't work, since time will be different):\n\n>>> for x in print_iter_progress(xrange(50), print_progress_every=10):\n... pass\n...\n9:30:5 - iteration 0\n9:30:5 - iteration 10\n9:30:5 - iteration 20\n9:30:5 - iteration 30\n9:30:5 - iteration 40\n\n>>> for x in print_iter_progress(xrange(50),\n... print_progress_every=15,\n... data_msg_intro_str=\"data times two is: {data_str}\",\n... data_to_string=lambda x: x * 2):\n... pass\n...\n9:30:55 - iteration 0data times two is: 0\n9:30:55 - iteration 15data times two is: 30\n9:30:55 - iteration 30data times two is: 60\n9:30:55 - iteration 45data times two is: 90'''\n\nquantify(iterable, pred=<class 'bool'>)\n'''Count how many times the predicate is true'''\n\nrandom_combination(iterable, r)\n'''Random selection from combinations(iterable, r)'''\n\nrandom_combination_with_replacement(iterable, r)\n'''Random selection from combinations_with_replacement(iterable, r)'''\n\nrandom_permutation(iterable, r=None)\n'''Random selection from itertools.permutations(iterable, r)'''\n\nrandom_product(*args, **kwds)\n'''Random selection from itertools.product(*args, **kwds)'''\n\nrandom_subset(iterator, K)\n'''Uses reservoir sampling to get a sample from an iterator without knowing how many points there are\nin advance.'''\n\nrepeatfunc(func, times=None, *args)\n'''Repeat calls to func with specified arguments.\n\nExample: repeatfunc(random.random)'''\n\nroundrobin(*iterables)\n'''roundrobin('ABC', 'D', 'EF') --> A D E B F C'''\n\nrunning_mean(it, chk_size=2, chk_step=1)\n'''Running mean (moving average) on iterator.\nNote: When input it is list-like, ut.stats.smooth.sliders version of running_mean is 4 times more efficient with\nbig (but not too big, because happens in RAM) inputs.\n:param it: iterable\n:param chk_size: width of the window to take means from\n:return:\n\n>>> list(running_mean([1, 3, 5, 7, 9], 2))\n[2.0, 4.0, 6.0, 8.0]\n>>> list(running_mean([1, 3, 5, 7, 9], 2, chk_step=2))\n[2.0, 6.0]\n>>> list(running_mean([1, 3, 5, 7, 9], 2, chk_step=3))\n[2.0, 8.0]\n>>> list(running_mean([1, 3, 5, 7, 9], 3))\n[3.0, 5.0, 7.0]\n>>> list(running_mean([1, -1, 1, -1], 2))\n[0.0, 0.0, 0.0]\n>>> list(running_mean([-1, -2, -3, -4], 3))\n[-2.0, -3.0]'''\n\nseq_batch(seq, n=1, return_tail=True, fillvalue=None)\n'''An iterator of equal sized batches of a sequence.\nSee also grouper and seq_batch.\n:param seq: a sequence (should have a .__len__ and a .__getitem__ method)\n:param n: batch size\n:param return_tail:\n * True (default): Return the tail (what's remaining if the seq len is not a multiple of the batch size),\n as is (so the last batch might not be of size n\n * None: Return the tail, but fill it will the value specified in the fillvalue argument, to make it size n\n * False: Don't return the tail at all\n:param fillvalue: Value to be used to fill the tail if return_tail == None\n\n>>> seq = [1, 2, 3, 4, 5, 6, 7]\n>>> list(seq_batch(seq, 3, False))\n[[1, 2, 3], [4, 5, 6]]\n>>> list(seq_batch(seq, 3, True))\n[[1, 2, 3], [4, 5, 6], [7]]\n>>> list(seq_batch(seq, 3, None))\n[[1, 2, 3], [4, 5, 6], [7, None, None]]\n>>> list(seq_batch(seq, 3, None, 0))\n[[1, 2, 3], [4, 5, 6], [7, 0, 0]]'''\n\ntabulate(function, start=0)\n'''Return function(0), function(1), ...'''\n\ntake(n, iterable)\n'''Return first n items of the iterable as a list'''\n\ntee_lookahead(t, i)\n'''Inspect the i-th upcomping value from a tee object\nwhile leaving the tee object at its current position.\n\nRaise an IndexError if the underlying iterator doesn't\nhave enough values.'''\n\nunique_everseen(iterable, key=None)\n'''List unique elements, preserving order. Remember all elements ever seen.\n>>> list(unique_everseen('AAAABBBCCDAABBB'))\n['A', 'B', 'C', 'D']\n>>> import string\n>>> list(unique_everseen('ABBCcAD', string.lower))\n['A', 'B', 'C', 'D']'''\n\nunique_justseen(iterable, key=None)\n'''List unique elements, preserving order. Remember only the element just seen.\n>>> list(unique_justseen('AAAABBBCCDAABBB'))\n['A', 'B', 'C', 'D', 'A', 'B']\n>>> import string\n>>> list(unique_justseen('ABBCcAD', string.lower))\n['A', 'B', 'C', 'A', 'D']'''\n\nwindow(seq, n=2)\n'''Returns a sliding window (of width n) over data from the iterable'''\n```\n\n",
"bugtrack_url": null,
"license": "mit",
"summary": "Extract code exercises from code itself",
"version": "0.0.7",
"project_urls": {
"Homepage": "https://github.com/thorwhalen/skill"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "68475c3048f4b6ae06c5b8f389f29df5d8855c978ce7c1d9af463f605233bd12",
"md5": "e125b79901789f12007905a8281ffc77",
"sha256": "c856b26b3fb7c0a4302102b4022097b7c8702887a0c60f92b594de0833537b87"
},
"downloads": -1,
"filename": "skill-0.0.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e125b79901789f12007905a8281ffc77",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6695,
"upload_time": "2025-02-04T14:58:05",
"upload_time_iso_8601": "2025-02-04T14:58:05.419030Z",
"url": "https://files.pythonhosted.org/packages/68/47/5c3048f4b6ae06c5b8f389f29df5d8855c978ce7c1d9af463f605233bd12/skill-0.0.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b835edd3f9e2afea61e8854976fd3af0c1afe94869c02882ebdfd20b50a39c5c",
"md5": "9887b3b224b3d59244757af5a845d313",
"sha256": "32e312eefca6927007bbc7a51357cc97a576642a34dfeaa9640670002a996cf7"
},
"downloads": -1,
"filename": "skill-0.0.7.tar.gz",
"has_sig": false,
"md5_digest": "9887b3b224b3d59244757af5a845d313",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6473,
"upload_time": "2025-02-04T14:58:07",
"upload_time_iso_8601": "2025-02-04T14:58:07.044053Z",
"url": "https://files.pythonhosted.org/packages/b8/35/edd3f9e2afea61e8854976fd3af0c1afe94869c02882ebdfd20b50a39c5c/skill-0.0.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-04 14:58:07",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "thorwhalen",
"github_project": "skill",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "skill"
}