Python in a Nutshell

Technical Books
My notes on Python in a Nutshell by Alex Martelli, Anna Martelli Ravenscroft, Steven Holden, Paul McGuire.
Author

Tyler Hillery

Published

January 7, 2024


Preface

While you can read this volume linearly from the beginning, we also aim for it to be a useful reference for the working programmer. You may choose to index to locate items of interest, or to read specific chapters for coverage their particular topics.

I don’t plan on reading this book from start finish. Below are the chapters I plan on reading

  • Part II, Core Python Language and Built-ins
    • Chapter 5, “Type Annotations”
  • Part III, Python Library and Extension Modules
    • Chapter 11. File and Text Operations
    • Chapter 12. Persistence and Databases
    • Chapter 15. Concurrency: Threads and Process
  • Part IV, Network and Web Programming
    • Chapter 18. Networking Basics
    • Chapter 19. Client-Side Networking Protocol Modules
    • Chapter 20. Serving HTTP

Part II, Core Python Language and Built-ins

Chapter 5. Type Annotations

  • Type annotations are not enforced at runtime.

  • PEP stands for Python Enhancement Proposal, a design document describing a new feature for python

  • The typing module defines several protocols, which are similar to what some other languages call “interfaces”. Protocols are abstract base classes intended to concisely express constraints on a type, ensuring it contains certain methods.

    cast is a way of overriding any interfaces or prior annotations that may be present at particular place in your code. It may hide actual type errors in your code, rendering the type-checking pass incomplete or inaccurate.

  • Generics are types that define a template for classes that can adapt the type annotations of their method signatures based on one or more parameters.

  • I like this general rule of thumb on when and now to add type annotations to new code

    the Two Function Rule: as soon as your script contains two functions or methods, go back and add type annotations to the method signatures, and any shared variables or types as necessary. Use TypeDict to annotate any dict structures that are used in place of classes, so that dict keys get clearly defined up front or get documented as you go; use NamedTuples(or dataclasses: some of the book’s authors strongly prefer the later option) to define the specific attributes for those data “bundles”

Part III, Python Library and Extension Modules

Chapter 11. File and Text Operations

  • File Buffering is where data can be temporarily held in memory on the way to or from the file.

  • Random Access files allows moving forward and back within the file, or jumping to read or write at a particular location in the file.

Chapter 12. Persistence and Databases

  • Python supplies several modules to serialize (save) Python objects to various kinds of byte streams and deserialize (load and re-create) Python objects back from streams. Serialization is also known as marshaling, which means formatting for data interchange

  • I was not aware of the psycopg3 package, I have always used psycopg2.

Chapter 15. Concurrency: Threads and Process

  • Processes are instances of running programs that the operating system protects from on that another. They can communicate with each other by using interprocess communication (IPC) mechanisms.

  • *Thread is a flow of control that shares a global state with other threads inside a single process

  • General rule of thumb: I/O-bound reach for async programming, CPU Bound reach for multiprocessing because in python the GIL ensures that only one Python-coded thread at a time can execute so multithreading isn’t as effective

Part IV, Network and Web Programming

Chapter 18. Networking Basics

  • The main connection-oriented protocol is the transmission control protocol.

  • The main connectionless or datagram protocol use the User Datagram Protocol (UDP).

  • Sockets give access to pipelines between independent endpoints, using a transport layer protocol to move information between those endpoints.

  • Both UDP and TCP are carried over a common Internet Protocol (IP) network layer known as TCP/IP.

Chapter 19. Client-Side Networking Protocol Modules

  • a URL is a string composed of several parts called components: the scheme, location, path, query and fragment e.g scheme://lo.ca.ti.on/pa/th?qu=ery#fragment

Chapter 20. Serving HTTP

  • Common Gateway Interface (CGI) requires the server to run a separate program each time a client request dynamic content.

    Checkout HTTP: The Definitive Guide
  • Web Server Gateway Interface (WSGI) is the standard way for all modern Python web development frameworks to interface with underlying web servers… WSGI is an interface, and that interface has two sides: the web server/gateway side and the application/framework side. The framework side’s job is to provide a WSGI application object, a callable object respecting conventions in the PEP, and to connect the application object to the server by whatever means the specific sever documents. Gunicorn is an example WSGI server.

  • ASGI is the async version of WSGI
Component Role WSGI ASGI
Web Server Handling HTTP requests, serving static files (HTML, CSS, JavaScript and managing SLL/TSL for secure HTTPS connections. It’s often used as a reverse proxy and load balancer as well forwarding requests to other servers in the background and returning responses to clients. NGINX NGINX
GI Server Designed for handling HTTP traffic destined for Python code. It describes how a web server communicates with web applications. Acts as middleware between NGINX and web apps Gunicorn Uvicorn or Hypercorn
GI Utility / Toolkit Commonly used by lightweight frameworks for various components to handle any functionality not strictly related to HTTP Werkzueg Starlette**
Application Framework Provides the core tools and functionalities to create a web app such as routing, templates, RESTful request dispatching. Django, Flask FastAPI, Quart
*Starlette can also be used as a lightweight app framework*

Review

I would take this review with a grain of salt as I only read select sections of this book that interested me. Overall I wasn’t a big fan of how this book was structured. There was far too many tables with methods or attributes with their definition opposed to content from the authors. With that being said I still think there is tons of great information in this book but it should be used as a reference guide for when you want to look something up.