summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimeon Simeonov2022-11-10 21:32:00 +0100
committerSimeon Simeonov2022-11-10 21:32:00 +0100
commit72ff13968fc20f043fdec657b4cfe385f4666146 (patch)
tree3f4749be8ca0babc2df9028c3db1f8df9af51754
parentf52fe43ba3bea5e9f5091445513279f800494760 (diff)
Update notebooks/python/python_3_8_to_3_11.ipynb
-rw-r--r--notebooks/python/python_3_8_to_3_11.ipynb134
1 files changed, 89 insertions, 45 deletions
diff --git a/notebooks/python/python_3_8_to_3_11.ipynb b/notebooks/python/python_3_8_to_3_11.ipynb
index 62fea7d..fd5a3fc 100644
--- a/notebooks/python/python_3_8_to_3_11.ipynb
+++ b/notebooks/python/python_3_8_to_3_11.ipynb
@@ -17,6 +17,10 @@
17 "\n", 17 "\n",
18 "Released on October 14th, 2019.\n", 18 "Released on October 14th, 2019.\n",
19 "\n", 19 "\n",
20 "Latest Python version to support MS Windows <= 7.\n",
21 "\n",
22 "By default, the end-of-life is scheduled 5 years after the first release, but can be adjusted by the release manager of each branch.\n",
23 "\n",
20 "\n", 24 "\n",
21 "## Highlights\n", 25 "## Highlights\n",
22 "\n", 26 "\n",
@@ -31,7 +35,7 @@
31 }, 35 },
32 { 36 {
33 "cell_type": "code", 37 "cell_type": "code",
34 "execution_count": 113, 38 "execution_count": 1,
35 "id": "a21e461a-8a7c-49d2-8a53-ab63ff2488bb", 39 "id": "a21e461a-8a7c-49d2-8a53-ab63ff2488bb",
36 "metadata": {}, 40 "metadata": {},
37 "outputs": [ 41 "outputs": [
@@ -47,6 +51,10 @@
47 "source": [ 51 "source": [
48 "# The walrus operator:\n", 52 "# The walrus operator:\n",
49 "\n", 53 "\n",
54 "# \"Guido van Rossum searched through a Dropbox code base and discovered some\n",
55 "# evidence that programmers value writing fewer lines over shorter lines\" - PEP 572\n",
56 "\n",
57 "# example1\n",
50 "my_list = [\"Greham\", \"John\", \"Terry G\", \"Eric\", \"Terry J\", \"Michael\"]\n", 58 "my_list = [\"Greham\", \"John\", \"Terry G\", \"Eric\", \"Terry J\", \"Michael\"]\n",
51 "\n", 59 "\n",
52 "# Python < 3.8\n", 60 "# Python < 3.8\n",
@@ -55,13 +63,25 @@
55 " print(f\"List is too long ({list_length} elements, expected <= 10)\")\n", 63 " print(f\"List is too long ({list_length} elements, expected <= 10)\")\n",
56 "\n", 64 "\n",
57 "# while using the walrus operator...\n", 65 "# while using the walrus operator...\n",
58 "if (list_length := len(my_list)) > 5:\n", 66 "if (list_length := len(my_list)) > 5: # fewer but longer lines :)\n",
59 " print(f\"List is too long ({list_length} elements, expected <= 10)\")" 67 " print(f\"List is too long ({list_length} elements, expected <= 10)\")\n",
68 "\n",
69 "\n",
70 "# example2\n",
71 "def expensive_func(x):\n",
72 " \"\"\"assuming that the function is performing an expensive task\"\"\"\n",
73 " return x\n",
74 "\n",
75 "# reuse the value that's expensive to compute\n",
76 "[y := expensive_func(8), y**2, y**3]\n",
77 "\n",
78 "# share a subexpression between a comprehension filter clause and its output\n",
79 "filtered_data = [y for x in (2, None, 4) if (y := expensive_func(x)) is not None]"
60 ] 80 ]
61 }, 81 },
62 { 82 {
63 "cell_type": "code", 83 "cell_type": "code",
64 "execution_count": 114, 84 "execution_count": 2,
65 "id": "99b75989-e991-4f77-a62e-01776f5b606c", 85 "id": "99b75989-e991-4f77-a62e-01776f5b606c",
66 "metadata": {}, 86 "metadata": {},
67 "outputs": [ 87 "outputs": [
@@ -106,7 +126,7 @@
106 }, 126 },
107 { 127 {
108 "cell_type": "code", 128 "cell_type": "code",
109 "execution_count": 115, 129 "execution_count": 3,
110 "id": "f683072f-c555-404b-b2c9-0d7d3621b118", 130 "id": "f683072f-c555-404b-b2c9-0d7d3621b118",
111 "metadata": {}, 131 "metadata": {},
112 "outputs": [ 132 "outputs": [
@@ -165,7 +185,7 @@
165 }, 185 },
166 { 186 {
167 "cell_type": "code", 187 "cell_type": "code",
168 "execution_count": 116, 188 "execution_count": 4,
169 "id": "a21fdf0a-92d2-4987-93c4-ea68f34f71b3", 189 "id": "a21fdf0a-92d2-4987-93c4-ea68f34f71b3",
170 "metadata": {}, 190 "metadata": {},
171 "outputs": [ 191 "outputs": [
@@ -175,15 +195,18 @@
175 "text": [ 195 "text": [
176 "2019\n", 196 "2019\n",
177 "('GREHAM', 'John', 'TerryG', 'Eric', 'TerryJ', 'Michael')\n", 197 "('GREHAM', 'John', 'TerryG', 'Eric', 'TerryJ', 'Michael')\n",
198 "---\n",
178 "Performing a very expensive operation\n", 199 "Performing a very expensive operation\n",
179 "Performing a very expensive operation\n", 200 "Performing a very expensive operation\n",
180 "dataset.string='836278421', id(dataset.string)=36514294704\n", 201 "dataset.string='836278421', id(dataset.string)=36108881968\n",
181 "Performing a very expensive operation\n", 202 "Performing a very expensive operation\n",
182 "Performing a very expensive operation\n", 203 "Performing a very expensive operation\n",
183 "dataset.string='836278421', id(dataset.string)=36514289520\n", 204 "dataset.string='836278421', id(dataset.string)=36108885360\n",
205 "---Doing the same using cached property---\n",
184 "Performing a very expensive operation\n", 206 "Performing a very expensive operation\n",
185 "dataset.cached_string='836278421', id(dataset.cached_string)=36514289520\n", 207 "dataset.cached_string='836278421', id(dataset.cached_string)=36108885360\n",
186 "dataset.cached_string='836278421', id(dataset.cached_string)=36514289520\n", 208 "dataset.cached_string='836278421', id(dataset.cached_string)=36108885360\n",
209 "---\n",
187 "Permutations of 10 things taken 3 at a time: math.perm(10, 3)=720\n", 210 "Permutations of 10 things taken 3 at a time: math.perm(10, 3)=720\n",
188 "Combinations of 10 things taken 3 at a time: math.comb(10, 3)=120\n" 211 "Combinations of 10 things taken 3 at a time: math.comb(10, 3)=120\n"
189 ] 212 ]
@@ -219,8 +242,6 @@
219 "\n", 242 "\n",
220 "\n", 243 "\n",
221 "# new functools.cached_property() decorator, for computed properties cached for the life of the instance\n", 244 "# new functools.cached_property() decorator, for computed properties cached for the life of the instance\n",
222 "import statistics\n",
223 "\n",
224 "class Dataset:\n", 245 "class Dataset:\n",
225 " def __init__(self, sequence_of_numbers):\n", 246 " def __init__(self, sequence_of_numbers):\n",
226 " self.data = sequence_of_numbers\n", 247 " self.data = sequence_of_numbers\n",
@@ -237,11 +258,14 @@
237 " print(\"Performing a very expensive operation\")\n", 258 " print(\"Performing a very expensive operation\")\n",
238 " return \"\".join(map(str, self.data))\n", 259 " return \"\".join(map(str, self.data))\n",
239 "\n", 260 "\n",
261 "print(\"---\")\n",
240 "dataset = Dataset((836, 278, 421))\n", 262 "dataset = Dataset((836, 278, 421))\n",
241 "print(f\"{dataset.string=}, {id(dataset.string)=}\")\n", 263 "print(f\"{dataset.string=}, {id(dataset.string)=}\")\n",
242 "print(f\"{dataset.string=}, {id(dataset.string)=}\")\n", 264 "print(f\"{dataset.string=}, {id(dataset.string)=}\")\n",
265 "print(\"---Doing the same using cached property---\")\n",
243 "print(f\"{dataset.cached_string=}, {id(dataset.cached_string)=}\")\n", 266 "print(f\"{dataset.cached_string=}, {id(dataset.cached_string)=}\")\n",
244 "print(f\"{dataset.cached_string=}, {id(dataset.cached_string)=}\")\n", 267 "print(f\"{dataset.cached_string=}, {id(dataset.cached_string)=}\")\n",
268 "print(\"---\")\n",
245 "\n", 269 "\n",
246 "\n", 270 "\n",
247 "# new combinatoric functions math.perm() and math.comb()\n", 271 "# new combinatoric functions math.perm() and math.comb()\n",
@@ -260,27 +284,33 @@
260 "\n", 284 "\n",
261 " Released on October 5th, 2020.\n", 285 " Released on October 5th, 2020.\n",
262 " \n", 286 " \n",
287 " PEP 602 - CPython adopts an annual release cycle.\n",
263 " \n", 288 " \n",
264 " ## Highlights\n", 289 " Python 3.9 is the last version providing those Python 2 backward compatibility layers,\n",
265 " \n", 290 " to give more time to Python projects maintainers to organize the removal of the Python 2 support and add support for Python 3.9.\n",
266 " - **PEP 584** – Add Union Operators To dict\n",
267 " \n", 291 " \n",
268 " - **PEP 585** – Type Hinting Generics In Standard Collections - you can now use built-in collection types such as `list` and `dict` as generic types instead of importing the corresponding capitalized types (e.g. `List` or `Dict`) from `typing`\n", 292 " Python 3.9 uses a new parser, based on PEG (parsing expression gramma) instead of LL (Left to right, performing Leftmost derivation of the sentence). \n",
293 " The new parser’s performance is roughly comparable to that of the old parser, but the PEG formalism is more flexible than LL when it comes to designing new language features.\n",
294 "\n",
295 "COVID-19.\n",
296 "\n",
297 "\n",
298 "## Highlights\n",
269 " \n", 299 " \n",
270 " - **PEP 614** – Relaxing Grammar Restrictions On Decorators\n", 300 " - **PEP 584** - Add Union Operators To dict\n",
271 " \n", 301 " \n",
272 " - **PEP 616** – String methods to remove prefixes and suffixes\n", 302 " - **PEP 585** - Type Hinting Generics In Standard Collections - you can now use built-in collection types such as `list` and `dict` as generic types instead of importing the corresponding capitalized types (e.g. `List` or `Dict`) from `typing`\n",
273 " \n", 303 " \n",
274 " - **PEP 593** – Flexible function and variable annotations\n", 304 " - **PEP 616** - String methods to remove prefixes and suffixes\n",
275 " \n", 305 " \n",
276 " - **PEP 615** – Support for the IANA Time Zone Database in the Standard Library - the new `zoneinfo` module\n", 306 " - **PEP 615** - Support for the IANA Time Zone Database in the Standard Library - the new `zoneinfo` module\n",
277 " \n", 307 " \n",
278 " - Python now gets the absolute path of the script filename specified on the command line (ex: python3 script.py): the `__file__` attribute of the `__main__` module became an absolute path, rather than a relative path. These paths now remain valid after the current directory is changed by `os.chdir()`. As a side effect, the traceback also displays the absolute path for `__main__` module frames in this case." 308 " - Python now gets the absolute path of the script filename specified on the command line (ex: python3 script.py): the `__file__` attribute of the `__main__` module became an absolute path, rather than a relative path. These paths now remain valid after the current directory is changed by `os.chdir()`. As a side effect, the traceback also displays the absolute path for `__main__` module frames in this case."
279 ] 309 ]
280 }, 310 },
281 { 311 {
282 "cell_type": "code", 312 "cell_type": "code",
283 "execution_count": 117, 313 "execution_count": 5,
284 "id": "1f567f26-3783-4435-bcff-48826ea50404", 314 "id": "1f567f26-3783-4435-bcff-48826ea50404",
285 "metadata": {}, 315 "metadata": {},
286 "outputs": [ 316 "outputs": [
@@ -310,7 +340,7 @@
310 }, 340 },
311 { 341 {
312 "cell_type": "code", 342 "cell_type": "code",
313 "execution_count": 118, 343 "execution_count": 6,
314 "id": "3f527469-64c7-4cef-8a4d-ef536c418fa1", 344 "id": "3f527469-64c7-4cef-8a4d-ef536c418fa1",
315 "metadata": {}, 345 "metadata": {},
316 "outputs": [ 346 "outputs": [
@@ -335,6 +365,10 @@
335 "source": [ 365 "source": [
336 "## Other changes\n", 366 "## Other changes\n",
337 "\n", 367 "\n",
368 "- PEP 614 - Relaxing Grammar Restrictions On Decorator\n",
369 "\n",
370 "- PEP 593 - Flexible function and variable annotations\n",
371 "\n",
338 "- The hashlib module can now use *SHA3* hashes and *SHAKE XOF* from *OpenSSL* when available\n", 372 "- The hashlib module can now use *SHA3* hashes and *SHAKE XOF* from *OpenSSL* when available\n",
339 "\n", 373 "\n",
340 "- new `math.lcm(*integers)` function while `math.gcd(*integers)` now handles multiple arguments. `fractions.gcd()` is removed\n", 374 "- new `math.lcm(*integers)` function while `math.gcd(*integers)` now handles multiple arguments. `fractions.gcd()` is removed\n",
@@ -382,7 +416,7 @@
382 }, 416 },
383 { 417 {
384 "cell_type": "code", 418 "cell_type": "code",
385 "execution_count": 119, 419 "execution_count": 7,
386 "id": "11aaff48-28a3-49f5-86b6-059af6ab534d", 420 "id": "11aaff48-28a3-49f5-86b6-059af6ab534d",
387 "metadata": {}, 421 "metadata": {},
388 "outputs": [], 422 "outputs": [],
@@ -432,7 +466,7 @@
432 }, 466 },
433 { 467 {
434 "cell_type": "code", 468 "cell_type": "code",
435 "execution_count": 120, 469 "execution_count": 8,
436 "id": "59329225-db92-4eb6-ad1b-b88455e48d50", 470 "id": "59329225-db92-4eb6-ad1b-b88455e48d50",
437 "metadata": {}, 471 "metadata": {},
438 "outputs": [ 472 "outputs": [
@@ -442,23 +476,13 @@
442 "text": [ 476 "text": [
443 "Started with: ['the', 'clock']\n" 477 "Started with: ['the', 'clock']\n"
444 ] 478 ]
445 },
446 {
447 "data": {
448 "text/plain": [
449 "True"
450 ]
451 },
452 "execution_count": 120,
453 "metadata": {},
454 "output_type": "execute_result"
455 } 479 }
456 ], 480 ],
457 "source": [ 481 "source": [
458 "# PEP 634, PEP 635, PEP 636 - Structural pattern matching\n", 482 "# PEP 634, PEP 635, PEP 636 - Structural pattern matching\n",
459 "command = \"start the clock\"\n", 483 "command = \"start the clock\"\n",
460 "# command = input(\"Command: \")\n", 484 "# command = input(\"Command: \")\n",
461 "match command.split():\n", 485 "match command.split(): # match statement\n",
462 " case [\"start\", *args]:\n", 486 " case [\"start\", *args]:\n",
463 " print(f\"Started with: {args}\")\n", 487 " print(f\"Started with: {args}\")\n",
464 " case [\"stop\"] | [\"quit\"]:\n", 488 " case [\"stop\"] | [\"quit\"]:\n",
@@ -472,7 +496,27 @@
472 " case _:\n", 496 " case _:\n",
473 " print(\"Something completely unexpected happened :)\")\n", 497 " print(\"Something completely unexpected happened :)\")\n",
474 "\n", 498 "\n",
475 "\n", 499 "# __match_args__ can be defined / used in a class in order to select 'matchable' attributes"
500 ]
501 },
502 {
503 "cell_type": "code",
504 "execution_count": 9,
505 "id": "ded7dcfa-24e2-4a1d-8aad-4703a9aa24aa",
506 "metadata": {},
507 "outputs": [
508 {
509 "data": {
510 "text/plain": [
511 "True"
512 ]
513 },
514 "execution_count": 9,
515 "metadata": {},
516 "output_type": "execute_result"
517 }
518 ],
519 "source": [
476 "# PEP 604, PEP 612, PEP 613 - Various typing features\n", 520 "# PEP 604, PEP 612, PEP 613 - Various typing features\n",
477 "import typing\n", 521 "import typing\n",
478 "\n", 522 "\n",
@@ -497,7 +541,7 @@
497 "\n", 541 "\n",
498 "- the entire `distutils` package is deprecated, to be removed in Python 3.12. Its functionality for specifying package builds has already been completely replaced by third-party packages `setuptools` and `packaging`\n", 542 "- the entire `distutils` package is deprecated, to be removed in Python 3.12. Its functionality for specifying package builds has already been completely replaced by third-party packages `setuptools` and `packaging`\n",
499 "\n", 543 "\n",
500 "- new `itertools.pairwise()`\n", 544 "- new `itertools.pairwise()` (example: `pairwise('ABCDEFG') --> AB BC CD DE EF FG`)\n",
501 "\n", 545 "\n",
502 "- `os.path.realpath()` now accepts a strict keyword-only argument. When set to `True`, `OSError` is raised if a path doesn’t exist or a symlink loop is encountered\n", 546 "- `os.path.realpath()` now accepts a strict keyword-only argument. When set to `True`, `OSError` is raised if a path doesn’t exist or a symlink loop is encountered\n",
503 "\n", 547 "\n",
@@ -517,10 +561,10 @@
517 "\n", 561 "\n",
518 "Released on October 24th, 2022.\n", 562 "Released on October 24th, 2022.\n",
519 "\n", 563 "\n",
564 "The first reference implementation (CPython) using C11 instead of C89.\n",
520 "\n", 565 "\n",
521 "## Highlights\n",
522 "\n", 566 "\n",
523 "- The first reference implementation (CPython) using C11 instead of C89\n", 567 "## Highlights\n",
524 "\n", 568 "\n",
525 "- Python 3.11 is between 10-60% faster than Python 3.10. \"On average, we measured a 1.25x speedup on the standard benchmark suite\"\n", 569 "- Python 3.11 is between 10-60% faster than Python 3.10. \"On average, we measured a 1.25x speedup on the standard benchmark suite\"\n",
526 "\n", 570 "\n",
@@ -614,7 +658,7 @@
614 }, 658 },
615 { 659 {
616 "cell_type": "code", 660 "cell_type": "code",
617 "execution_count": 121, 661 "execution_count": 10,
618 "id": "ef9285ea-2bdb-4fb0-968d-833011fe0724", 662 "id": "ef9285ea-2bdb-4fb0-968d-833011fe0724",
619 "metadata": {}, 663 "metadata": {},
620 "outputs": [ 664 "outputs": [
@@ -658,7 +702,7 @@
658 }, 702 },
659 { 703 {
660 "cell_type": "code", 704 "cell_type": "code",
661 "execution_count": 122, 705 "execution_count": 11,
662 "id": "c70ce21d-bdf8-4a30-a5a2-c888ab75b92a", 706 "id": "c70ce21d-bdf8-4a30-a5a2-c888ab75b92a",
663 "metadata": {}, 707 "metadata": {},
664 "outputs": [ 708 "outputs": [
@@ -718,7 +762,7 @@
718 }, 762 },
719 { 763 {
720 "cell_type": "code", 764 "cell_type": "code",
721 "execution_count": 123, 765 "execution_count": 12,
722 "id": "f1f9977c-20f4-4bc1-9fa7-14acd0f16193", 766 "id": "f1f9977c-20f4-4bc1-9fa7-14acd0f16193",
723 "metadata": {}, 767 "metadata": {},
724 "outputs": [ 768 "outputs": [
@@ -748,7 +792,7 @@
748 }, 792 },
749 { 793 {
750 "cell_type": "code", 794 "cell_type": "code",
751 "execution_count": 124, 795 "execution_count": 13,
752 "id": "7260ad15-4215-4b48-b6a3-870a14d8079c", 796 "id": "7260ad15-4215-4b48-b6a3-870a14d8079c",
753 "metadata": {}, 797 "metadata": {},
754 "outputs": [ 798 "outputs": [
@@ -757,7 +801,7 @@
757 "output_type": "stream", 801 "output_type": "stream",
758 "text": [ 802 "text": [
759 "0.0\n", 803 "0.0\n",
760 "+0.0\n" 804 "0.0\n"
761 ] 805 ]
762 } 806 }
763 ], 807 ],
@@ -770,7 +814,7 @@
770 "print(f\"{x:z.1f}\")\n", 814 "print(f\"{x:z.1f}\")\n",
771 "\n", 815 "\n",
772 "x = decimal.Decimal('-.00001')\n", 816 "x = decimal.Decimal('-.00001')\n",
773 "print(f\"{x:+z.1f}\")" 817 "print(f\"{x:-z.1f}\")"
774 ] 818 ]
775 } 819 }
776 ], 820 ],