Dead Simple Python

Book description

Dead Simple Python is a thorough introduction to every feature of the Python language for programmers who are impatient to write production code. Instead of revisiting elementary computer science topics, you'll dive deep into idiomatic Python patterns so you can write professional Python programs in no time.

After speeding through Python's basic syntax and setting up a complete programming environment, you'll learn to work with Python's dynamic data typing, its support for both functional and object-oriented programming techniques, special features like generator expressions, and advanced topics like concurrency. You'll also learn how to package, distribute, debug, and test your Python project.

Master how to:

•Make Python's dynamic typing work for you to produce cleaner, more adaptive code. •Harness advanced iteration techniques to structure and process your data. •Design classes and functions that work without unwanted surprises or arbitrary constraints. •Use multiple inheritance and introspection to write classes that work intuitively. •Improve your code's responsiveness and performance with asynchrony, concurrency, and parallelism. •Structure your Python project for production-grade testing and distribution

The most pedantically pythonic primer ever printed, Dead Simple Python will take you from working with the absolute basics to coding applications worthy of publication.

Publisher resources

View/Submit Errata

Table of contents

  1. Title Page
  2. Copyright
  3. Dedication
  4. About the Author
  5. Foreword
  6. Acknowledgments
  7. Introduction
    1. Who Is This Book For?
    2. What Does “Simple” Mean?
    3. What’s in This Book?
    4. What’s NOT in This Book
    5. How to Read This Book
      1. About the Vocabulary
      2. Theory Recaps
      3. Objective or Opinionated?
      4. The Examples
      5. What About a Project?
    6. Prerequisites
  8. Part I: THE PYTHON ENVIRONMENT
    1. Chapter 1: The Python Philosophy
      1. What Is Python, Exactly?
      2. Myths: What Python Isn’t
        1. Myth #1: Python Is Merely a Scripting Language
        2. Myth #2: Python Is Slow
        3. Myth #3: Python Cannot Be Compiled
        4. Myth #4: Python Gets Compiled Behind the Scenes
        5. Myth #5: Python Is Unsuitable for Large Projects
      3. Python 2 vs. Python 3
      4. Defining “Pythonic” Code
      5. The Zen of Python
      6. Documentation, PEPs, and You
      7. Who Calls the Shots?
      8. The Python Community
      9. The Pursuit of the One Obvious Way
      10. Wrapping Up
    2. Chapter 2: Your Workbench
      1. Installing Python
        1. Installing on Windows
        2. Installing on macOS
        3. Installing on Linux
        4. Installing from Source
      2. Meet the Interpreter
        1. Interactive Session
        2. Running Python Files
      3. Packages and Virtual Environments
        1. Creating a Virtual Environment
        2. Activating a Virtual Environment
        3. Leaving a Virtual Environment
      4. Introducing pip
        1. System-Wide Packages
        2. Installing Packages
        3. requirements.txt
        4. Upgrading Packages
        5. Removing Packages
        6. Finding Packages
        7. One Warning About pip . . .
      5. Virtual Environments and Git
        1. The Whole Shebang
        2. File Encoding
      6. A Few Extra Virtual Environment Tricks
        1. Using a Virtual Environment Without Activating
        2. The Alternatives
        3. The Line Limit Debate
        4. Tabs or Spaces?
      7. Quality Control: Static Analyzers
        1. Pylint
        2. Flake8
        3. Mypy
      8. Style Janitors: Autoformatting Tools
        1. autopep8
        2. Black
      9. Testing Frameworks
      10. An Exhibition of Code Editors
        1. IDLE
        2. Emacs and Vim
        3. PyCharm
        4. Visual Studio Code
        5. Sublime Text
        6. Spyder
        7. Eclipse + PyDev/LiClipse
        8. The Eric Python IDE
      11. Wrapping Up
    3. Chapter 3: Syntax Crash Course
      1. Hello, World!
      2. Statements and Expression
      3. The Importance of Whitespace
      4. Doing Nothing
      5. Comments and Docstrings
        1. Docstrings
      6. Declaring Variables
        1. What About Constants?
      7. Mathematics
        1. Meet the Number Types
        2. Operators
        3. The math Module
      8. Logic
        1. Conditionals
        2. Comparison Operators
        3. Boolean, None, and Identity Operators
        4. Truthiness
        5. Logical Operators
        6. The Walrus Operator
        7. Ellipsis
      9. Strings
        1. String Literals
        2. Raw Strings
        3. Formatted Strings
        4. Template Strings
        5. String Conversion
        6. A Note on String Concatenation
      10. Functions
      11. Classes and Objects
      12. Error Handling
      13. Tuples and Lists
      14. Loops
        1. while Loop
        2. Loop Control
        3. for Loop
      15. Structural Pattern Matching
        1. Literal Patterns and Wildcards
        2. Or Patterns
        3. Capture Patterns
        4. Guard Statements
        5. More About Structural Pattern Matching
      16. Wrapping Up
    4. Chapter 4: Project Structure and Imports
      1. Setting Up the Repository
      2. Modules and Packages
        1. PEP 8 and Naming
        2. Project Directory Structure
      3. How import Works
      4. Import Dos and Don’ts
        1. Importing Functions from Modules
        2. The Problem of Shadowing
        3. The Trouble with Nested Packages
        4. Beware of Importing All
      5. Importing Within Your Project
        1. Absolute Imports
        2. Relative Imports
        3. Importing from the Same Package
      6. Entry Points
        1. Module Entry Points
        2. Package Entry Points
        3. Controlling Package Imports
        4. Program Entry Points
      7. The Python Module Search Path
      8. What Really Happens
      9. Wrapping Up
  9. Part II: ESSENTIAL STRUCTURES
    1. Chapter 5: Variables and Types
      1. Variables According to Python: Names and Values
      2. Assignment
      3. Data Types
        1. The type() Function
        2. Duck Typing
      4. Scope and Garbage Collection
        1. Local Scope and the Reference-Counting Garbage Collector
        2. Interpreter Shutdown
        3. Global Scope
        4. The Dangers of Global Scope
        5. The nonlocal Keyword
        6. Scope Resolution
        7. The Curious Case of the Class
        8. Generational Garbage Collector
      5. The Immutable Truth
      6. Passing by Assignment
      7. Collections and References
        1. Shallow Copy
        2. Deep Copy
      8. Coercion and Conversion
      9. A Note About Systems Hungarian Notation
      10. Terminology Review
      11. Wrapping Up
    2. Chapter 6: Functions and Lambdas
      1. Python Function Essentials
      2. Recursion
      3. Default Argument Values
      4. Keyword Arguments
      5. On Overloaded Functions
      6. Variadic Arguments
        1. Keyword Variadic Arguments
      7. Keyword-Only Parameters
        1. Positional-Only Parameters
        2. Argument Types: All Together Now!
      8. Nested Functions
      9. Closures
        1. Recursion with Closures
        2. Stateful Closures
      10. Lambdas
        1. Why Lambdas Are Useful
        2. Lambdas as Sorting Keys
      11. Decorators
      12. Type Hints and Function Annotations
        1. Duck Typing and Type Hints
        2. Should You Use Type Hinting?
      13. Wrapping Up
    3. Chapter 7: Objects and Classes
      1. Declaring a Class
        1. The Initializer
        2. The Constructor
        3. The Finalizer
      2. Attributes
        1. Instance Attributes
        2. Class Attributes
      3. Scope-Naming Conventions
        1. Nonpublic
        2. Public
        3. Name Mangling
        4. Public, Nonpublic, or Name Mangled?
      4. Methods
        1. Instance Methods
        2. Class Methods
        3. Static Methods
      5. Properties
        1. Setting Up the Scenario
        2. Defining a Property
        3. Property with Decorators
        4. When Not to Use Properties
      6. Special Methods
        1. Scenario Setup
        2. Conversion Methods
        3. Comparison Methods
        4. Binary Operator Support
        5. Unary Operator Support
        6. Making Callable
        7. More Special Methods: Looking Ahead
      7. Class Decorators
      8. Structural Pattern Matching with Objects
      9. Functional Meets Object Oriented
      10. When to Use Classes
        1. Classes Aren’t Modules
        2. Single Responsibility
        3. Sharing State
        4. Are Objects Right for You?
      11. Wrapping Up
    4. Chapter 8: Errors and Exceptions
      1. Exceptions in Python
      2. Reading Tracebacks
      3. Catching Exceptions: LBYL vs. EAFP
      4. Multiple Exceptions
      5. Beware the Diaper Anti-pattern
      6. Raising Exceptions
      7. Using Exceptions
        1. Exceptions and Logging
        2. Bubbling Up
        3. Exception Chaining
      8. Else and Finally
        1. Else: “If All Goes Well”
        2. Finally: “After Everything”
      9. Creating Exceptions
      10. A Gallery of Exceptions
      11. Wrapping Up
  10. Part III: DATA AND FLOW
    1. Chapter 9: Collections and Iteration
      1. Loops
        1. while Loops
        2. for Loops
      2. Collections
        1. Tuples
        2. Named Tuples
        3. Lists
        4. Deques
        5. Sets
        6. frozenset
        7. Dictionaries
        8. Check or Except?
        9. Dictionary Variants
      3. Unpacking Collections
        1. Starred Expressions
        2. Unpacking Dictionaries
      4. Structural Pattern Matching on Collections
      5. Accessing by Index or Key
      6. Slice Notation
        1. Start and Stop
        2. Negative Indices
        3. Steps
        4. Copy with Slice
        5. Slice Objects
        6. Slicing on Custom Objects
        7. Using islice
      7. The in Operator
      8. Checking Collection Length
      9. Iteration
        1. Iterables and Iterators
        2. Manually Using Iterators
        3. Iterating with for Loops
        4. Sorting Collections in Loops
        5. Enumerating Loops
        6. Mutation in Loops
        7. Loop Nesting and Alternatives
      10. Iteration Tools
        1. Basic Built-in Tools
        2. Filter
        3. Map
        4. Zip
        5. Itertools
      11. Custom Iterable Classes
      12. Wrapping Up
    2. Chapter 10: Generators and Comprehensions
      1. Lazy Evaluation and Eager Iterables
      2. Infinite Iterators
      3. Generators
        1. Generators vs. Iterator Classes
        2. Closing Generators
        3. Behavior on Close
        4. Throwing Exceptions
      4. yield from
      5. Generator Expressions
        1. Generator Objects Are Lazy
        2. Generator Expressions with Multiple Loops
        3. Conditionals in Generator Expressions
        4. Nested Generator Expressions
      6. List Comprehensions
      7. Set Comprehensions
      8. Dictionary Comprehensions
      9. Hazards of Generator Expressions
        1. They Quickly Become Unreadable
        2. They Don’t Replace Loops
        3. They Can Be Hard to Debug
        4. When to Use Generator Expressions
      10. Simple Coroutines
        1. Returning Values from a Coroutine
        2. Sequence of Behavior
      11. What About Async?
      12. Wrapping Up
    3. Chapter 11: Text IO and Context Managers
      1. Standard Input and Output
        1. Revisiting print()
        2. Revisiting input()
      2. Streams
      3. Context Manager Basics
      4. File Modes
      5. Reading Files
        1. The read() Method
        2. The readline() Method
        3. The readlines() Method
        4. Reading with Iteration
      6. Stream Position
      7. Writing Files
        1. The write() Method
        2. The writelines() Method
        3. Writing Files with print()
        4. Line Separators
      8. Context Manager Details
        1. How Context Managers Work
        2. Using Multiple Context Managers
        3. Implementing the Context Management Protocol
        4. The __enter__() Method
        5. The __exit__() Method
        6. Using the Custom Class
      9. Paths
        1. Path Objects
        2. Parts of a Path
        3. Creating a Path
        4. Relative Paths
        5. Paths Relative to Package
        6. Path Operations
        7. Out-of-Place File Writes
        8. The os Module
      10. File Formats
        1. JSON
        2. Other Formats
      11. Wrapping Up
    4. Chapter 12: Binary and Serialization
      1. Binary Notation and Bitwise
        1. Number Systems Refresher
        2. Python Integers and Binary
        3. Bitwise Operations
      2. Bytes Literals
      3. Bytes-Like Objects
        1. Creating a bytes Object
        2. Using int.to_bytes()
        3. Sequence Operations
        4. Converting bytes to int
      4. struct
        1. struct Format String and Packing
        2. Unpacking with struct
        3. struct objects
      5. Bitwise on Bytes-Like Objects
        1. Bitwise Operations via Integers
        2. Bitwise Operations via Iteration
      6. memoryview
      7. Reading and Writing Binary Files
        1. Organizing the Data
        2. Writing to a File
        3. Reading from a Binary File
        4. Seek with Binary Stream
        5. BufferedRWPair
      8. Serialization Techniques
        1. Forbidden Tools: pickle, marshal, and shelve
        2. Serialization Formats
      9. Wrapping Up
  11. Part IV: ADVANCED CONCEPTS
    1. Chapter 13: Inheritance and Mixins
      1. When to Use Inheritance
        1. Crimes of Inheritance
      2. Basic Inheritance in Python
      3. Multiple Inheritance
        1. Method Resolution Order
        2. Ensuring Consistent Method Resolution Order
        3. Explicit Resolution Order
        4. Resolving Base Class in Multiple Inheritance
      4. Mixins
      5. Wrapping Up
    2. Chapter 14: Metaclasses and ABCs
      1. Metaclasses
        1. Creating Classes with type
        2. Custom Metaclasses
      2. Type Expectations with Duck Typing
        1. EAFP: Catching Exceptions
        2. LBYL: Checking for Attributes
      3. Abstract Classes
        1. Built-in ABCs
        2. Deriving from ABCs
        3. Implementing Custom ABCs
      4. Virtual Subclasses
        1. Setting Up the Example
        2. Using Virtual Subclassing
      5. Wrapping Up
    3. Chapter 15: Introspection and Generics
      1. Special Attributes
      2. Inside Object Attribute Access: The __dict__ Special Attribute
        1. Listing Attributes
        2. Getting an Attribute
        3. Checking for an Attribute
        4. Setting an Attribute
        5. Deleting an Attribute
      3. Function Attributes
        1. The Wrong Way to Use Function Attributes
        2. Mutability and Function Attributes
      4. Descriptors
        1. The Descriptor Protocol
        2. Writing a Descriptor Class (the Slightly Wrong Way)
        3. Using a Descriptor
        4. Writing a Descriptor Class the Right Way
        5. Using Multiple Descriptors in the Same Class
      5. Slots
        1. Binding Attribute Names to Values
        2. Using Arbitrary Attributes with Slots
        3. Slots and Inheritance
      6. Immutable Classes
      7. Single-Dispatch Generic Functions
        1. Registering Single-Dispatch Functions with Type Hints
        2. Registering Single-Dispatch Functions with Explicit Type
        3. Registering Single-Dispatch Functions with the register() Method
      8. Using the Element Class
      9. Arbitrary Execution
      10. Wrapping Up
    4. Chapter 16: Asynchrony and Concurrency
      1. Asynchrony in Python
      2. The Example Scenario: Collatz Game, Synchronous Version
      3. Asynchrony
        1. Native Coroutines
        2. Tasks
        3. The Event Loop
        4. Making It (Actually) Asynchronous
      4. Scheduling and Asynchronous Execution Flow
        1. Simplifying the Code
      5. Asynchronous Iteration
      6. Asynchronous Context Managers
      7. Asynchronous Generators
      8. Other Asynchrony Concepts
      9. Wrapping Up
    5. Chapter 17: Threading and Parallelism
      1. Threading
        1. Concurrency vs. Parallelism
        2. Basic Threading
        3. Timeouts
        4. Daemonic Threads
        5. Futures and Executors
        6. Timeouts with Futures
      2. Race Conditions
        1. A Race Condition Example
        2. Creating Multiple Threads with ThreadPoolExecutor
      3. Locks
      4. Deadlock, Livelock, and Starvation
      5. Passing Messages with Queue
      6. Futures with Multiple Workers
      7. Achieving Parallelism with Multiprocessing
        1. Pickling Data
        2. Speed Considerations and ProcessPoolExecutor
      8. The Producer/Consumer Problem
        1. Importing the Modules
        2. Monitoring the Queue
        3. Subprocess Cleanup
        4. Consumers
        5. Checking for an Empty Queue
        6. Producers
        7. Starting the Processes
        8. Performance Results
        9. Logging with Multiprocessing
      9. Wrapping Up
  12. Part V: BEYOND THE CODE
    1. Chapter 18: Packaging and Distribution
      1. Planning Your Packaging
        1. The Dangers of Cargo Cult Programming
        2. A Note on Packaging Opinions
        3. Determining Your Packaging Goals
      2. Project Structure: src or src-less
      3. Packaging a Distribution Package with setuptools
        1. Project Files and Structure
        2. Where Metadata Belongs
        3. The README.md and LICENSE Files
        4. The setup.cfg File
        5. The setup.py File
        6. The MANIFEST.in File
        7. The requirements.txt File
        8. The pyproject.toml File
        9. Testing the Setup Configuration
      4. Building Your Package
      5. Publishing on pip (Twine)
        1. Uploading to Test PyPI
        2. Installing Your Uploaded Package
        3. Uploading to PyPI
      6. Alternative Packaging Tools
        1. Poetry
        2. Flit
      7. Distributing to End Users
        1. PEX
        2. Freezers
        3. Images and Containers
        4. A Note on Native Linux Packaging
      8. Documentation
      9. Wrapping Up
    2. Chapter 19: Debugging and Logging
      1. Warnings
        1. Types of Warnings
        2. Filtering Warnings
        3. Converting Warnings into Exceptions
      2. Logging
        1. Logger Objects
        2. Handler Objects
        3. Logging with Levels
        4. Controlling the Log Level
        5. Running the Example
        6. Filter, Formatter, and Configuration
      3. Assert Statements
        1. Proper Use of assert
        2. Wrong Use of assert
        3. Seeing assert in Action
      4. The inspect Module
      5. Using pdb
        1. A Debugging Example
        2. Starting the Debugger
        3. Debugger Shell Commands
        4. Stepping Through the Code
        5. Setting a Breakpoint and Stepping into a Function
        6. Moving Through the Execution Stack
        7. Inspecting the Source
        8. Checking a Solution
        9. Postmortem Debugging
      6. Using faulthandler
      7. Evaluating Your Program’s Security with Bandit
      8. Reporting Bugs to Python
      9. Wrapping Up
    3. Chapter 20: Testing and Profiling
      1. What About TDD?
      2. Test Frameworks
      3. The Example Project
      4. Testing and Project Structure
      5. Testing Basics
        1. Starting the Example
        2. Unit Testing
        3. Executing the Tests with pytest
        4. Testing for Exceptions
      6. Test Fixtures
        1. Continuing the Example: Using the API
        2. Sharing Data Between Test Modules
      7. Flaky Tests and Conditionally Skipping Tests
      8. Advanced Fixtures: Mocking and Parametrizing
        1. Continuing the Example: Representing a Typo
        2. Parametrizing
        3. Indirect Parametrization of Fixtures
        4. Mocking Inputs with Monkeypatch
        5. Marking
        6. Capturing from Standard Streams
        7. GUI Testing
        8. Continuing the Example: Connecting the API to Typo
        9. Autouse Fixtures
        10. Mixed Parametrization
        11. Fuzzing
        12. Wrapping Up the Example
      9. Code Coverage
      10. Automating Testing with tox
      11. Benchmarking and Profiling
        1. Benchmarking with timeit
        2. Profiling with cProfile or profile
        3. tracemalloc
      12. Wrapping Up
    4. Chapter 21: The Parting of the Ways
      1. About the Future
      2. Where Do You Go from Here?
        1. Application Development in Python
        2. Game Development in Python
        3. Web Development in Python
        4. Client-Side Python
        5. Data Science in Python
        6. Machine Learning in Python
        7. Security
        8. Embedded Development in Python
        9. Scripting
      3. Python Flavors
      4. Developing for Python
        1. Developing Python Packages and Tools
        2. Developing Python Extensions
        3. Contributing to Python
      5. Getting Involved with Python
        1. Asking Questions
        2. Answering Questions
        3. User Groups
        4. PyLadies
        5. Conferences
        6. Joining the Python Software Foundation
      6. And the Road Goes Ever On . . .
  13. Appendix A: Special Attributes and Methods
    1. Special Methods
    2. Special Attributes
  14. Appendix B: Python Debugger (pdb) Commands
  15. Appendix : Glossary
    1. A
    2. B
    3. C
    4. D
    5. E
    6. F
    7. G
    8. H
    9. I
    10. K
    11. L
    12. M
    13. N
    14. O
    15. P
    16. Q
    17. R
    18. S
    19. T
    20. U
    21. V
    22. W
    23. Y
    24. Z
  16. Index

Product information

  • Title: Dead Simple Python
  • Author(s): Jason C. McDonald
  • Release date: November 2022
  • Publisher(s): No Starch Press
  • ISBN: 9781718500921