0% found this document useful (0 votes)
104 views20 pages

Writing Python in A Nutshell 4th Edition

SacPy podcast, Python authors as guests.

Uploaded by

William Hooten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views20 pages

Writing Python in A Nutshell 4th Edition

SacPy podcast, Python authors as guests.

Uploaded by

William Hooten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Writing Python in a Nutshell, 4th Edition

Anna Martelli Ravenscroft and Alex Martelli


A short history of the book
2003: 1st edition. (up to) v2.2; martellibot; 600 pages
2006: 2nd edition. (up to) v2.5; Alex (with Anna as tech reviewer); 738 pages
(eep, what growth!)
…long time lapse as Alex was not yet using Python 3…
2017: 3rd edition. v2.7 & 3.5; Alex & Anna, with Steve Holden as co-author; 772
pages (way too big!)
2023: 4th edition. V3.7 to 3.11; Alex, Anna, Steve, and Paul McGuire; 735 pages
(a little better!)
The process of writing/editing/finalizing
● Location (authors): 2 x Sunnyvale, CA; 1 x Austin, TX; 1 near London, UK
● Tools: Google Spreadsheets, weekly Google Meet meetings, Google Docs…
(and more meetings, spreadsheets, docs…)
→ …meeting all chapter-by-chapter deadlines (to the editor’s surprise!-)
● Writing stylistic issues:
○ Oxford commas and active voice (avoid passives!)
○ Typography (italics? bold? which font should commas inside code be?)
○ Indicating version numbers, e.g.: 3.9+
● Reviewers and copyeditor (Rachel Head rocked!) and indexer, oh my!
Some hard decisions
● Not covering
○ asyncio & friends (too important: deserves whole books of its own!)
○ key 3rd-party frameworks (django, flask, panda, PyTorch,
TensorFlow/Keras, …, any GUI, …)
● Two online-only chapters:
https://github.com/pynutshell/pynut4/tree/main/chapters
○ Packaging (chapter 24)
○ Extending and Embedding (chapter 25)
Some not-so-hard decisions
● Github repo for all sample code from the book:
https://github.com/pynutshell/pynut4/tree/main
● Search PEP abstracts by keywords:
https://ptmcg.pythonanywhere.com/pep_search
● Search for language changes by version:
https://ptmcg.pythonanywhere.com/python_nutshell_app_a_search
A few of our favorite things
Anna’s favorite new thing
The zoneinfo Module 3.9+
● Concrete implementation of timezones for use with tzinfo
● Uses system timezone data, or tzdata as a fallback
○ On Windows, you may need to pip install tzdata
○ Once tzdata installed, you don’t need to import tzdata
● Get a list of timezone names: zoneinfo.available_timezones()
● zoneinfo has one class ZoneInfo

>>> from zoneinfo import ZoneInfo


Updating a Timezone
>>> d
datetime.datetime(2023,5,4,18,30,
tzinfo=zoneinfo.ZoneInfo(key='America/New_York'))
>>> d=d.replace(tzinfo=ZoneInfo("America/Los_Angeles"))
>>> d
datetime.datetime(2023,5,4,18,30,
tzinfo=zoneinfo.ZoneInfo(key='America/Los_Angeles'))
>>> dk=d.astimezone(tz=ZoneInfo("Asia/Katmandu"))
>>> dk
datetime.datetime(2023,5,5,7,15,
tzinfo=zoneinfo.ZoneInfo(key='Asia/Katmandu'))
# Katmandu offset is +5 hours 45 minutes from UTC (+12:45 ahead of PST)
replace() vs astimezone()
replace
● returns new datetime with updated timezone without changing other
attributes
● pass keyword arg tzinfo= to change timezone
● can also be used to replace other attributes, e.g.:
>>> d=d.replace(hour=19, minute=0)
astimezone
● returns new datetime with updated time, date (as needed), & timezone
● won’t work with tzinfo=, requires keyword arg tz=
Alex’s favorite (and… not-so-much) things

● Since 3.0…
○ text strings default to Unicode (good!)
○ identifiers may contain unicode (hmmm…)
■ They get normalized (NFKC, not NFC as rec'd in UAX#31!)
■ see PEP 672 (https://peps.python.org/pep-0672)
■ and devote your time (I estimate: several months!) to study
https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandar
d-15.0.pdf and https://www.unicode.org/reports/ (all of them!)
Middle Dot “·” is a Valid Identifier Character

Not valid as a leading character


May cause difficulties due to visual similarity between · and .
Python in a Nutshell, 4th edition, Chapter 3, page 36
Unicode Identifiers Can Often “normalize to ASCII”

ᴾ𝘆𝙩𝚑𝓸𝔫 𝐹º𝑛t 𝘔ⅸᵉ𝐫


http://ptmcg.pythonanywhere.com/font_mixer
18 Unicode Characters Normalize To ‘A’...
A 65 LATIN CAPITAL LETTER A
ᴬ 7468 MODIFIER LETTER CAPITAL A
Ⓐ 9398 CIRCLED LATIN CAPITAL LETTER A
A 65313 FULLWIDTH LATIN CAPITAL LETTER A
𝐀 119808 MATHEMATICAL BOLD CAPITAL A
𝐴 119860 MATHEMATICAL ITALIC CAPITAL A
𝑨 119912 MATHEMATICAL BOLD ITALIC CAPITAL A
𝒜 119964 MATHEMATICAL SCRIPT CAPITAL A
𝓐 120016 MATHEMATICAL BOLD SCRIPT CAPITAL A
𝔄 120068 MATHEMATICAL FRAKTUR CAPITAL A
𝔸 120120 MATHEMATICAL DOUBLE-STRUCK CAPITAL A
𝕬 120172 MATHEMATICAL BOLD FRAKTUR CAPITAL A
𝖠 120224 MATHEMATICAL SANS-SERIF CAPITAL A
𝗔 120276 MATHEMATICAL SANS-SERIF BOLD CAPITAL A
𝘈 120328 MATHEMATICAL SANS-SERIF ITALIC CAPITAL A
𝘼 120380 MATHEMATICAL SANS-SERIF BOLD ITALIC CAPITAL A
𝙰 120432 MATHEMATICAL MONOSPACE CAPITAL A
🄰 127280 SQUARED LATIN CAPITAL LETTER A
But NOT Α (Greek capital letter α)!
Some tidbits from Paul McGuire
Watch out for str.lstrip() and str.rstrip();
they might remove more than you want!
>>> print("https://shell.com".lstrip("https://"))
ell.com

removeprefix() 3.9+ does what you probably wanted:


>>> print("https://shell.com".removeprefix("https://"))
shell.com

There’s also a new removesuffix() 3.9+


COUNT HIGHER WITH INTS THAN WITH FLOATS

Floats lose integer


precision at 2**53

Ints can count up to


2**(2**63), or
roughly 10**(2.8e18)
https://www.build-python-from-source.com/
Found Choose
● this helpfulPython
website version
while Choosefor
● searching a few other options
common system libraries
● Generates text for a bash script (good for Linux
used while buiding Python
or WSL)
from source
Python’s sort() Cited in Supreme Court Case
● Python’s sort() algorithm (known as
TimSort, named for its author Tim
Peters) was part of the source code
cited in the case of Oracle America v.
Google, 2012
● The case was generally decided for
Google, except for the infringement on
TimSort – the judgment against
Google for that was $0
A portion of our royalties for Python in a Nutshell
are donated to the PSF
Discounts and book signings and Q&A

Discount code for 40% off, good thru May 31, 2023:
https://play.google.com/redeem?code=61P98Z8G5R20U
Upcoming book signings (tentative):
● North Bay Python https://2023.northbaypython.org/
● PyBay https://www.sfpythonmeetup.com/

Q&A

You might also like