ch01-12: clean up by @eumiro

This commit is contained in:
Luciano Ramalho
2021-02-14 20:28:07 -03:00
parent 584a7f21ca
commit 03ace4f4ae
33 changed files with 1383 additions and 86 deletions

View File

@@ -1,3 +0,0 @@
floats-*.txt
floats-*.npy
floats.bin

View File

@@ -1,21 +0,0 @@
# An Array of Sequences
Sample code for Chapter 2 of _Fluent Python 2e_ by Luciano Ramalho (O'Reilly, 2020)
## Running the tests
### Doctests
Use Python's standard ``doctest`` module, for example:
$ python3 -m doctest bisect_demo.py -v
### Jupyter Notebook
Install ``pytest`` and the ``nbval`` plugin:
$ pip install pytest nbval
Run:
$ pytest --nbval

4
02-array-seq/README.rst Normal file
View File

@@ -0,0 +1,4 @@
Sample code for Chapter 2 - "An array of sequences"
From the book "Fluent Python 2e" by Luciano Ramalho (O'Reilly)
http://shop.oreilly.com/product/0636920032519.do

View File

@@ -36,7 +36,7 @@ Demonstration of ``bisect.bisect_left``::
"""
# BEGIN BISECT_DEMO
# tag::BISECT_DEMO[]
import bisect
import sys
@@ -62,4 +62,4 @@ if __name__ == '__main__':
print('haystack ->', ' '.join(f'{n:2}' for n in HAYSTACK))
demo(bisect_fn)
# END BISECT_DEMO
# end::BISECT_DEMO[]

View File

@@ -9,4 +9,4 @@ my_list = []
for i in range(SIZE):
new_item = random.randrange(SIZE * 2)
bisect.insort(my_list, new_item)
print(f'{new_item:2} ->', my_list)
print(f'{new_item:2d} -> {my_list}')

View File

@@ -0,0 +1,238 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# memoryviews\n",
"\n",
"## References\n",
"\n",
"* [PEP-3118](https://www.python.org/dev/peps/pep-3118/) Revising the buffer protocol (2006)\n",
"* [issue14130](https://bugs.python.org/issue14130) memoryview: add multi-dimensional indexing and slicing (2012)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.7.1 (default, Dec 14 2018, 13:28:58) \n",
"[Clang 4.0.1 (tags/RELEASE_401/final)]\n"
]
}
],
"source": [
"import sys\n",
"print(sys.version)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<memory at 0x1045a31c8>"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mv1d = memoryview(bytes(range(35, 50)))\n",
"mv1d"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(mv1d)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<memory at 0x10464a120>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mv2d = mv1d.cast('B', [3, 5])\n",
"mv2d"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 5)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mv2d.shape"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(mv2d)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[35, 36, 37, 38, 39], [40, 41, 42, 43, 44], [45, 46, 47, 48, 49]]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mv2d.tolist()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[35, 36, 37, 38, 39]\n",
"[40, 41, 42, 43, 44]\n",
"[45, 46, 47, 48, 49]\n"
]
}
],
"source": [
"for row in mv2d.tolist():\n",
" print(row)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mv2d[1, 2]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mv2d.tolist()[1][2]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -1,11 +0,0 @@
Assigning to Slices
===================
An example that raises an error::
>>> l = list(range(10))
>>> l[2:5] = 100
Traceback (most recent call last):
...
TypeError: can only assign an iterable