The Python Standard Library documentation is very helpful for learning Python. So is Solveit! Solveit is jupyter notebook + AI with superpowers. Learning programming is so much fun and productive with AI. Therefore, I wanted to convert these html python documentation pages into solveit dialogues, which comprise small pieces of notes and code messages with appropriate headings, which can be extracted from the pages’ table of contents.
How it works:
We first get the html from the python documentation web page.
We turn it into (msg_type, element) where msg_type is note or code and element is soup element.
Turn elements into appropriate solveit messages for the dialog.
The goal is to use # for the title, ## for subheading, and ### for each function definition from the docs.
First, we grab html from the documentation and create soup.
If a is omitted or None, the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).
If a is an int, its absolute value is used directly.
With version 2 (the default), a str, bytes, or bytearray object gets converted to an int and all of its bits are used.
With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.
Changed in version 3.2: Moved to the version 2 scheme which uses all of the bits in a string seed.
Changed in version 3.11: The seed must be one of the following types: None, int, float, str, bytes, or bytearray.
state should have been obtained from a previous call to getstate(), and setstate() restores the internal state of the generator to what it was at the time getstate() was called.
Convert all children of an HTML element to markdown
print(html_to_md(bk))
Bookkeeping functions[¶](#bookkeeping-functions)
random.seed(*a=None*, *version=2*)[¶](#random.seed)
Initialize the random number generator.
If *a* is omitted or `None`, the current system time is used. If
randomness sources are provided by the operating system, they are used
instead of the system time (see the [os.urandom()](os.html#os.urandom) function for details
on availability).
If *a* is an int, its absolute value is used directly.
With version 2 (the default), a [str](stdtypes.html#str), [bytes](stdtypes.html#bytes), or [bytearray](stdtypes.html#bytearray)
object gets converted to an [int](functions.html#int) and all of its bits are used.
With version 1 (provided for reproducing random sequences from older versions
of Python), the algorithm for [str](stdtypes.html#str) and [bytes](stdtypes.html#bytes) generates a
narrower range of seeds.
Changed in version 3.2: Moved to the version 2 scheme which uses all of the bits in a string seed.
Changed in version 3.11: The *seed* must be one of the following types:
`None`, [int](functions.html#int), [float](functions.html#float), [str](stdtypes.html#str),
[bytes](stdtypes.html#bytes), or [bytearray](stdtypes.html#bytearray).
random.getstate()[¶](#random.getstate)
Return an object capturing the current internal state of the generator. This
object can be passed to [setstate()](#random.setstate) to restore the state.
random.setstate(*state*)[¶](#random.setstate)
*state* should have been obtained from a previous call to [getstate()](#random.getstate), and
[setstate()](#random.setstate) restores the internal state of the generator to what it was at
the time [getstate()](#random.getstate) was called.
soup to (msg_type, el)
Solveit messages have Code, Note, Prompt, and Raw for message types. But we want to focus on note and code for creating dialogs. By turning soup into (msg_type, el), we can easily turn those into sovleit messages with markdown.
Convert HTML element to list of formatted (msg_type, markdown) tuples
Let’s try it on bytearray function from the “https://docs.python.org/3.12/library/functions.html”.
bytearray_html ='''<dl class="py class" id="func-bytearray"><dt class="sig sig-object py"><em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytearray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em><span class="sig-paren">)</span></dt><dt class="sig sig-object py"><em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytearray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em><span class="sig-paren">)</span></dt><dt class="sig sig-object py"><em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">bytearray</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">source</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span></em><span class="sig-paren">)</span></dt><dd><p>Return a new array of bytes.</p><p>The optional <em>source</em> parameter can be used to initialize the array:</p><ul class="simple"><li><p>If it is a <em>string</em>, you must also give the <em>encoding</em>.</p></li><li><p>If it is an <em>integer</em>, the array will have that size.</p></li></ul><p>Without an argument, an array of size 0 is created.</p></dd></dl>'''
The optional source parameter can be used to initialize the array:
[note]
If it is a string, you must also give the encoding.
If it is an integer, the array will have that size.
[note]
Without an argument, an array of size 0 is created.
ba_msgs = format_msgs(ba_soup)ba_msgs
[('note',
"### `class bytearray(source=b'')`\n### `class bytearray(source, encoding)`\n### `class bytearray(source, encoding, errors)`"),
('note', 'Return a new array of bytes.'),
('note',
'The optional *source* parameter can be used to initialize the array:'),
('note',
'\n- If it is a *string*, you must also give the *encoding*.\n- If it is an *integer*, the array will have that size.\n'),
('note', 'Without an argument, an array of size 0 is created.')]
merge_dt(ba_msgs)
[('note',
"### `class bytearray(source=b'')`\n### `class bytearray(source, encoding)`\n### `class bytearray(source, encoding, errors)`"),
('note', 'Return a new array of bytes.'),
('note',
'The optional *source* parameter can be used to initialize the array:'),
('note',
'\n- If it is a *string*, you must also give the *encoding*.\n- If it is an *integer*, the array will have that size.\n'),
('note', 'Without an argument, an array of size 0 is created.')]
preview_msgs(format_msgs(ba_soup))
[note]
class bytearray(source=b'')
class bytearray(source, encoding)
class bytearray(source, encoding, errors)
[note]
Return a new array of bytes.
[note]
The optional source parameter can be used to initialize the array:
[note]
If it is a string, you must also give the encoding.
If it is an integer, the array will have that size.
[note]
Without an argument, an array of size 0 is created.
Looks good! We can use create_msg to create solveit messages.