Compare commits
49 Commits
82b4fe2163
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf3161ca00 | ||
|
|
8c8c08170a | ||
|
|
162dbadbe5 | ||
|
|
cf99650007 | ||
|
|
ec03da74ca | ||
|
|
5b743b5bd7 | ||
|
|
648e9f6394 | ||
|
|
cc4e26c67a | ||
|
|
c5490b1569 | ||
|
|
3d5588b75e | ||
|
|
1bd4b1e7be | ||
|
|
612c145ac9 | ||
|
|
ab8a774924 | ||
|
|
802c353075 | ||
|
|
243a02b766 | ||
|
|
e06a925d2f | ||
|
|
74c52779b4 | ||
|
|
a4aea64dd1 | ||
|
|
bfedc4d6e6 | ||
|
|
41866ada97 | ||
|
|
68de220bd1 | ||
|
|
8e911b19be | ||
|
|
f355aa37ed | ||
|
|
d14c7bed18 | ||
|
|
1b44dfaf11 | ||
|
|
74ee1d4915 | ||
|
|
1907aea700 | ||
|
|
dfb8fce86e | ||
|
|
abc03e728d | ||
|
|
e7ecb9cff0 | ||
|
|
7b13f5b8fd | ||
|
|
e7c182d027 | ||
|
|
567f3529e3 | ||
|
|
166a388ac8 | ||
|
|
51055887dd | ||
|
|
d3309f03e8 | ||
|
|
3f9c89554a | ||
|
|
d00908d553 | ||
|
|
08230737cc | ||
|
|
28d6d03315 | ||
|
|
2e84544aff | ||
|
|
434f8d26b6 | ||
|
|
8158222f22 | ||
|
|
a40bd03b0e | ||
|
|
536e68686f | ||
|
|
5f2c3abfc7 | ||
|
|
b39c1c1f04 | ||
|
|
08318a657a | ||
|
|
646427f393 |
@@ -482,7 +482,7 @@
|
||||
" self.y = y\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
" return 'Vector(%r, %r)' % (self.x, self.y)\n",
|
||||
" return f'Vector({self.x!r}, {self.y!r})'\n",
|
||||
"\n",
|
||||
" def __abs__(self):\n",
|
||||
" return math.hypot(self.x, self.y)\n",
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
python3 -m doctest bisect_demo.py
|
||||
python3 -m doctest metro_lat_long.py
|
||||
python3 -m doctest metro_lat_lon.py
|
||||
pytest -q --nbval
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
Sample code for Chapter 8 - "Object references, mutability and recycling"
|
||||
Sample code for Chapter 6 - "Object references, mutability and recycling"
|
||||
|
||||
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
|
||||
http://shop.oreilly.com/product/0636920032519.do
|
||||
|
||||
22
08-def-type-hints/callable/variance.py
Normal file
22
08-def-type-hints/callable/variance.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from collections.abc import Callable
|
||||
|
||||
def update( # <1>
|
||||
probe: Callable[[], float], # <2>
|
||||
display: Callable[[float], None] # <3>
|
||||
) -> None:
|
||||
temperature = probe()
|
||||
# imagine lots of control code here
|
||||
display(temperature)
|
||||
|
||||
def probe_ok() -> int: # <4>
|
||||
return 42
|
||||
|
||||
def display_wrong(temperature: int) -> None: # <5>
|
||||
print(hex(temperature))
|
||||
|
||||
update(probe_ok, display_wrong) # type error # <6>
|
||||
|
||||
def display_ok(temperature: complex) -> None: # <7>
|
||||
print(temperature)
|
||||
|
||||
update(probe_ok, display_ok) # OK # <8>
|
||||
@@ -1,2 +1,2 @@
|
||||
geolib==1.0.7
|
||||
future==0.18.2
|
||||
future==0.18.3
|
||||
|
||||
@@ -6,8 +6,8 @@ def columnize(
|
||||
) -> Iterator[tuple[str, ...]]: # <1>
|
||||
if num_columns == 0:
|
||||
num_columns = round(len(sequence) ** 0.5)
|
||||
num_rows, reminder = divmod(len(sequence), num_columns)
|
||||
num_rows += bool(reminder)
|
||||
num_rows, remainder = divmod(len(sequence), num_columns)
|
||||
num_rows += bool(remainder)
|
||||
return (tuple(sequence[i::num_rows]) for i in range(num_rows)) # <2>
|
||||
# end::COLUMNIZE[]
|
||||
|
||||
|
||||
@@ -79,6 +79,8 @@ async def supervisor(cc_list: list[str],
|
||||
error = exc # <10>
|
||||
except KeyboardInterrupt:
|
||||
break
|
||||
else:
|
||||
error = None
|
||||
|
||||
if error:
|
||||
status = DownloadStatus.ERROR # <11>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
anyio==3.3.2
|
||||
certifi==2021.5.30
|
||||
certifi==2024.7.4
|
||||
charset-normalizer==2.0.6
|
||||
h11==0.12.0
|
||||
h11==0.16.0
|
||||
httpcore==0.13.7
|
||||
httpx==1.0.0b0
|
||||
idna==3.2
|
||||
idna==3.7
|
||||
rfc3986==1.5.0
|
||||
sniffio==1.2.0
|
||||
tqdm==4.62.3
|
||||
tqdm==4.66.3
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
click==7.1.2
|
||||
fastapi==0.65.2
|
||||
h11==0.12.0
|
||||
pydantic==1.8.2
|
||||
starlette==0.13.6
|
||||
h11==0.16.0
|
||||
pydantic==1.10.13
|
||||
starlette==0.40.0
|
||||
typing-extensions==3.7.4.3
|
||||
uvicorn==0.13.4
|
||||
|
||||
35
README.md
35
README.md
@@ -1,14 +1,7 @@
|
||||
# Fluent Python 2e example code
|
||||
|
||||
Example code for the book **Fluent Python, 2<sup>nd</sup> edition** by Luciano Ramalho (O'Reilly, 2021).
|
||||
Example code for the book **Fluent Python, Second Edition** by Luciano Ramalho (O'Reilly, 2022).
|
||||
|
||||
> **BEWARE**: This is a work in progress!
|
||||
>
|
||||
> * Code here may change and disappear without warning.
|
||||
>
|
||||
> * Major reorganizations may happen at any time.
|
||||
>
|
||||
> * No promises. No guarantees. Use at own risk.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@@ -16,37 +9,37 @@ All chapters are undergoing review and updates, including significant rewrites i
|
||||
|
||||
New chapters in **Fluent Python 2e** are marked with 🆕.
|
||||
|
||||
🚨 This table of contents is subject to change at any time until the book goes to the printer.
|
||||
> 🚨 This table of contents is subject to change at any time until the book goes to the printer.<BR>
|
||||
Latest change: Old **Part I—Prologue** merged into new **Part I—Data Structures**; parts renumbered accordingly; chapter numbers unchanged.
|
||||
|
||||
Part / Chapter #|Title|Directory|1<sup>st</sup> ed. Chapter #
|
||||
---:|---|---|:---:
|
||||
**I – Prologue**|
|
||||
**I – Data Structures**|
|
||||
1|The Python Data Model|[01-data-model](01-data-model)|1
|
||||
**II – Data Structures**|
|
||||
2|An Array of Sequences|[02-array-seq](02-array-seq)|2
|
||||
3|Dictionaries and Sets|[03-dict-set](03-dict-set)|3
|
||||
4|Unicode Text versus Bytes|[04-text-byte](04-text-byte)|4
|
||||
5|Data Class Builders|[05-data-classes](05-data-classes)|🆕
|
||||
6|Object References, Mutability, and Recycling|[06-obj-ref](06-obj-ref)|8
|
||||
**III – Functions as Objects**|
|
||||
**II – Functions as Objects**|
|
||||
7|Funcions as First-Class Objects|[07-1class-func](07-1class-func)|5
|
||||
8|Type Hints in Function Definitions|[08-def-type-hints](08-def-type-hints)|🆕
|
||||
9|Function Decorators and Closures|[09-closure-deco](09-closure-deco)|7
|
||||
8|Type Hints in Functions|[08-def-type-hints](08-def-type-hints)|🆕
|
||||
9|Decorators and Closures|[09-closure-deco](09-closure-deco)|7
|
||||
10|Design Patterns with First-Class Functions|[10-dp-1class-func](10-dp-1class-func)|6
|
||||
**IV – Object-Oriented Idioms**|
|
||||
**III – Object-Oriented Idioms**|
|
||||
11|A Pythonic Object|[11-pythonic-obj](11-pythonic-obj)|9
|
||||
12|Sequence Hacking, Hashing, and Slicing|[12-seq-hacking](12-seq-hacking)|10
|
||||
12|Special Methods for Sequences|[12-seq-hacking](12-seq-hacking)|10
|
||||
13|Interfaces, Protocols, and ABCs|[13-protocl-abc](13-protocol-abc)|11
|
||||
14|Inheritance: For Good or For Worse|[14-inheritance](14-inheritance)|12
|
||||
14|Inheritance: For Better or For Worse|[14-inheritance](14-inheritance)|12
|
||||
15|More About Type Hints|[15-more-types](15-more-types)|🆕
|
||||
16|Operator Overloading: Doing It Right|[16-op-overloading](16-op-overloading)|13
|
||||
**V – Control Flow**|
|
||||
16|Operator Overloading|[16-op-overloading](16-op-overloading)|13
|
||||
**IV – Control Flow**|
|
||||
17|Iterators, Generators, and Classic Coroutines|[17-it-generator](17-it-generator)|14
|
||||
18|Context Managers and else Blocks|[18-with-match](18-with-match)|15
|
||||
18|with, match, and else Blocks|[18-with-match](18-with-match)|15
|
||||
19|Concurrency Models in Python|[19-concurrency](19-concurrency)|🆕
|
||||
20|Concurrent Executors|[20-executors](20-executors)|17
|
||||
21|Asynchronous Programming|[21-async](21-async)|18
|
||||
**VI – Metaprogramming**|
|
||||
**V – Metaprogramming**|
|
||||
22|Dynamic Attributes and Properties|[22-dyn-attr-prop](22-dyn-attr-prop)|19
|
||||
23|Attribute Descriptors|[23-descriptor](23-descriptor)|20
|
||||
24|Class Metaprogramming|[24-class-metaprog](24-class-metaprog)|21
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,37 +1,6 @@
|
||||
# Short links for URLs in the book
|
||||
This file is deployed as `.htaccess` to the FPY.LI domain
|
||||
to map short URLs in Fluent Python to the original URLs.
|
||||
|
||||
## Problem: link rot
|
||||
To update it, I use tools in this other repo:
|
||||
|
||||
_Fluent Python, Second Edition_ has more than 1000 links to external resources.
|
||||
Inevitably, some of those links will rot as time passes.
|
||||
But I can't change the URLs in the print book...
|
||||
|
||||
## Solution: indirection
|
||||
|
||||
I replaced almost all URLs in the book with shortened versions that go through the `fpy.li` site which I control.
|
||||
The site has an `.htaccess` file with *temporary* redirects.
|
||||
|
||||
When I find out a link is stale, I can thange the redirect in `.htaccess` to a new target,
|
||||
so the link in the book is back in service through the updated redirect.
|
||||
|
||||
## Help wanted
|
||||
|
||||
Please report broken links as bugs in the [`FPY.LI.htaccess`](FPY.LI.htaccess) file.
|
||||
Also, feel free to send pull requests with fixes to that file.
|
||||
When I accept a PR, I will redeploy it to `fpy.li/.htaccess`.
|
||||
|
||||
## Details
|
||||
|
||||
Almost all URLs in the book are replaced with shortened versions like
|
||||
[`http://fpy.li/1-3`](http://fpy.li/1-3)—for chapter 1, link #3.
|
||||
|
||||
There are also custom short URLs like
|
||||
[`https://fpy.li/code`](https://fpy.li/code) which redirects to the example code repository.
|
||||
I used custom short URLs for URLs with 3 or more mentions, or links to PEPs.
|
||||
|
||||
Exceptions:
|
||||
|
||||
- URLs with `oreilly` in them are unchanged;
|
||||
- `fluentpython.com` URL (with no path) is unchanged;
|
||||
|
||||
The `FPY.LI.htaccess` is deployed at the root folder in `http://fpy.li`.
|
||||
https://github.com/pythonfluente/pythonfluente2e
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
ErrorDocument 404 /404.html
|
||||
|
||||
# main resources
|
||||
RedirectTemp /code https://github.com/fluentpython/example-code-2e
|
||||
RedirectTemp /home https://www.fluentpython.com/
|
||||
|
||||
# URLs mentioned at least three times
|
||||
RedirectTemp /bisect https://www.fluentpython.com/extra/ordered-sequences-with-bisect/
|
||||
RedirectTemp /cardxvi https://www.python.org/dev/peps/pep-0484/#the-numeric-tower
|
||||
RedirectTemp /collec https://docs.python.org/3/library/collections.html
|
||||
RedirectTemp /dask https://dask.org/
|
||||
RedirectTemp /dtmodel https://docs.python.org/3/reference/datamodel.html
|
||||
RedirectTemp /descr101 https://www.python.org/download/releases/2.2.3/descrintro/
|
||||
RedirectTemp /descrhow https://docs.python.org/3/howto/descriptor.html
|
||||
RedirectTemp /doctest https://docs.python.org/3/library/doctest.html
|
||||
RedirectTemp /effectpy https://effectivepython.com/
|
||||
RedirectTemp /fmtspec https://docs.python.org/3/library/string.html#formatspec
|
||||
RedirectTemp /gunicorn https://gunicorn.org/
|
||||
RedirectTemp /hashint https://www.fluentpython.com/extra/internals-of-sets-and-dicts/
|
||||
RedirectTemp /hattingh https://www.oreilly.com/library/view/using-asyncio-in/9781492075325/
|
||||
RedirectTemp /httpx https://www.python-httpx.org/
|
||||
RedirectTemp /initvar https://docs.python.org/3/library/dataclasses.html#init-only-variables
|
||||
RedirectTemp /mypy https://mypy.readthedocs.io/en/stable/
|
||||
RedirectTemp /norvigdp http://norvig.com/design-patterns/
|
||||
RedirectTemp /nsphere https://en.wikipedia.org/wiki/N-sphere
|
||||
RedirectTemp /oldcoro https://www.fluentpython.com/extra/classic-coroutines/
|
||||
RedirectTemp /pandas https://pandas.pydata.org/
|
||||
RedirectTemp /pep218 https://www.python.org/dev/peps/pep-0218/
|
||||
RedirectTemp /pep227 https://www.python.org/dev/peps/pep-0227/
|
||||
RedirectTemp /pep255 https://www.python.org/dev/peps/pep-0255/
|
||||
RedirectTemp /pep342 https://www.python.org/dev/peps/pep-0342/
|
||||
RedirectTemp /pep343 https://www.python.org/dev/peps/pep-0343/
|
||||
RedirectTemp /pep357 https://www.python.org/dev/peps/pep-0357/
|
||||
RedirectTemp /pep362 https://www.python.org/dev/peps/pep-0362/
|
||||
RedirectTemp /pep371 https://www.python.org/dev/peps/pep-0371/
|
||||
RedirectTemp /pep380 https://www.python.org/dev/peps/pep-0380/
|
||||
RedirectTemp /pep393 https://www.python.org/dev/peps/pep-0393/
|
||||
RedirectTemp /pep412 https://www.python.org/dev/peps/pep-0412/
|
||||
RedirectTemp /pep442 https://www.python.org/dev/peps/pep-0442/
|
||||
RedirectTemp /pep443 https://www.python.org/dev/peps/pep-0443/
|
||||
RedirectTemp /pep448 https://www.python.org/dev/peps/pep-0448/
|
||||
RedirectTemp /pep455 https://www.python.org/dev/peps/pep-0455/
|
||||
RedirectTemp /pep456 https://www.python.org/dev/peps/pep-0456/
|
||||
RedirectTemp /pep461 https://www.python.org/dev/peps/pep-0461/
|
||||
RedirectTemp /pep465 https://www.python.org/dev/peps/pep-0465/
|
||||
RedirectTemp /pep467 https://www.python.org/dev/peps/pep-0467/
|
||||
RedirectTemp /pep482 https://www.python.org/dev/peps/pep-0482/
|
||||
RedirectTemp /pep483 https://www.python.org/dev/peps/pep-0483/
|
||||
RedirectTemp /pep484 https://www.python.org/dev/peps/pep-0484/
|
||||
RedirectTemp /pep487 https://www.python.org/dev/peps/pep-0487/
|
||||
RedirectTemp /pep492 https://www.python.org/dev/peps/pep-0492/
|
||||
RedirectTemp /pep519 https://www.python.org/dev/peps/pep-0519/
|
||||
RedirectTemp /pep525 https://www.python.org/dev/peps/pep-0525/
|
||||
RedirectTemp /pep526 https://www.python.org/dev/peps/pep-0526/
|
||||
RedirectTemp /pep528 https://www.python.org/dev/peps/pep-0528/
|
||||
RedirectTemp /pep529 https://www.python.org/dev/peps/pep-0529/
|
||||
RedirectTemp /pep530 https://www.python.org/dev/peps/pep-0530/
|
||||
RedirectTemp /pep544 https://www.python.org/dev/peps/pep-0544/
|
||||
RedirectTemp /pep554 https://www.python.org/dev/peps/pep-0554/
|
||||
RedirectTemp /pep557 https://www.python.org/dev/peps/pep-0557/
|
||||
RedirectTemp /pep560 https://www.python.org/dev/peps/pep-0560/
|
||||
RedirectTemp /pep561 https://www.python.org/dev/peps/pep-0561/
|
||||
RedirectTemp /pep563 https://www.python.org/dev/peps/pep-0563/
|
||||
RedirectTemp /pep570 https://www.python.org/dev/peps/pep-0570/
|
||||
RedirectTemp /pep572 https://www.python.org/dev/peps/pep-0572/
|
||||
RedirectTemp /pep584 https://www.python.org/dev/peps/pep-0584/
|
||||
RedirectTemp /pep585 https://www.python.org/dev/peps/pep-0585/
|
||||
RedirectTemp /pep586 https://www.python.org/dev/peps/pep-0586/
|
||||
RedirectTemp /pep589 https://www.python.org/dev/peps/pep-0589/
|
||||
RedirectTemp /pep591 https://www.python.org/dev/peps/pep-0591/
|
||||
RedirectTemp /pep593 https://www.python.org/dev/peps/pep-0593/
|
||||
RedirectTemp /pep604 https://www.python.org/dev/peps/pep-0604/
|
||||
RedirectTemp /pep612 https://www.python.org/dev/peps/pep-0612/
|
||||
RedirectTemp /pep613 https://www.python.org/dev/peps/pep-0613/
|
||||
RedirectTemp /pep616 https://www.python.org/dev/peps/pep-0616/
|
||||
RedirectTemp /pep617 https://www.python.org/dev/peps/pep-0617/
|
||||
RedirectTemp /pep618 https://www.python.org/dev/peps/pep-0618/
|
||||
RedirectTemp /pep634 https://www.python.org/dev/peps/pep-0634/
|
||||
RedirectTemp /pep635 https://www.python.org/dev/peps/pep-0635/
|
||||
RedirectTemp /pep636 https://www.python.org/dev/peps/pep-0636/
|
||||
RedirectTemp /pep638 https://www.python.org/dev/peps/pep-0638/
|
||||
RedirectTemp /pep645 https://www.python.org/dev/peps/pep-0645/
|
||||
RedirectTemp /pep646 https://www.python.org/dev/peps/pep-0646/
|
||||
RedirectTemp /pep647 https://www.python.org/dev/peps/pep-0647/
|
||||
RedirectTemp /pep649 https://www.python.org/dev/peps/pep-0649/
|
||||
RedirectTemp /pep654 https://www.python.org/dev/peps/pep-0654/
|
||||
RedirectTemp /pep655 https://www.python.org/dev/peps/pep-0655/
|
||||
RedirectTemp /pep661 https://www.python.org/dev/peps/pep-0661/
|
||||
RedirectTemp /pep3099 https://www.python.org/dev/peps/pep-3099/
|
||||
RedirectTemp /pep3102 https://www.python.org/dev/peps/pep-3102/
|
||||
RedirectTemp /pep3104 https://www.python.org/dev/peps/pep-3104/
|
||||
RedirectTemp /pep3106 https://www.python.org/dev/peps/pep-3106/
|
||||
RedirectTemp /pep3107 https://www.python.org/dev/peps/pep-3107/
|
||||
RedirectTemp /pep3115 https://www.python.org/dev/peps/pep-3115/
|
||||
RedirectTemp /pep3118 https://www.python.org/dev/peps/pep-3118/
|
||||
RedirectTemp /pep3119 https://www.python.org/dev/peps/pep-3119/
|
||||
RedirectTemp /pep3129 https://www.python.org/dev/peps/pep-3129/
|
||||
RedirectTemp /pep3132 https://www.python.org/dev/peps/pep-3132/
|
||||
RedirectTemp /pep3141 https://www.python.org/dev/peps/pep-3141/
|
||||
RedirectTemp /pep3148 https://www.python.org/dev/peps/pep-3148/
|
||||
RedirectTemp /pep3155 https://www.python.org/dev/peps/pep-3155/
|
||||
RedirectTemp /pep3333 https://www.python.org/dev/peps/pep-3333/
|
||||
RedirectTemp /pypydif https://doc.pypy.org/en/latest/cpython_differences.html#subclasses-of-built-in-types
|
||||
RedirectTemp /shed4051 https://github.com/python/typeshed/issues/4051
|
||||
RedirectTemp /slatkin https://effectivepython.com/
|
||||
RedirectTemp /specattr https://docs.python.org/3/library/stdtypes.html#special-attributes
|
||||
RedirectTemp /typecoro https://docs.python.org/3.10/library/typing.html#typing.Coroutine
|
||||
RedirectTemp /typing https://docs.python.org/3/library/typing.html
|
||||
RedirectTemp /weakref https://www.fluentpython.com/extra/weak-references/
|
||||
Reference in New Issue
Block a user