0%

Attain a deep understanding of building, maintaining, packaging, and shipping robust Python applications

Key Features

  • Discover the new features of Python, such as dictionary merge, the zoneinfo module, and structural pattern matching
  • Create manageable code to run in various environments with different sets of dependencies
  • Implement effective Python data structures and algorithms to write, test, and optimize code

Book Description

This new edition of Expert Python Programming provides you with a thorough understanding of the process of building and maintaining Python apps. Complete with best practices, useful tools, and standards implemented by professional Python developers, this fourth edition has been extensively updated. Become familiar with the latest Python improvements, syntax elements, and interesting tools to boost your development efficiency.

The initial few chapters will allow experienced programmers coming from different languages to transition to the Python ecosystem. You will explore common software design patterns and various programming methodologies, such as event-driven programming, concurrency, and metaprogramming. You will also go through complex code examples and try to solve meaningful problems by bridging Python with C and C++, writing extensions that benefit from the strengths of multiple languages. Finally, you will understand the complete lifetime of any application after it goes live, including packaging and testing automation.

By the end of this book, you will have gained actionable Python programming insights that will help you effectively solve challenging problems.

What you will learn

  • Explore modern ways of setting up repeatable and consistent Python development environments
  • Effectively package Python code for community and production use
  • Learn modern syntax elements of Python programming, such as f-strings, enums, and lambda functions
  • Demystify metaprogramming in Python with metaclasses
  • Write concurrent code in Python
  • Extend and integrate Python with code written in C and C++

Who this book is for

The Python programming book is intended for expert programmers who want to learn Python's advanced-level concepts and latest features.

Anyone who has basic Python skills should be able to follow the content of the book, although it might require some additional effort from less experienced programmers. It should also be a good introduction to Python 3.9 for those who are still a bit behind and continue to use other older versions.

Table of Contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Get in touch
  2. Current Status of Python
    1. Where are we now and where are we going?
    2. What to do with Python 2
    3. Keeping up to date
    4. PEP documents
    5. Active communities
    6. Other resources
    7. Summary
  3. Modern Python Development Environments
    1. Technical requirements
    2. Python's packaging ecosystem
    3. Installing Python packages using pip
    4. Isolating the runtime environment
    5. Application-level isolation versus system-level isolation
    6. Application-level environment isolation
    7. Poetry as a dependency management system
    8. System-level environment isolation
    9. Containerization versus virtualization
    10. Virtual environments using Docker
    11. Writing your first Dockerfile
    12. Running containers
    13. Setting up complex environments
    14. Useful Docker and Docker Compose recipes for Python
    15. Virtual development environments using Vagrant
    16. Popular productivity tools
    17. Custom Python shells
    18. Using IPython
    19. Incorporating shells in your own scripts and programs
    20. Interactive debuggers
    21. Other productivity tools
    22. Summary
  4. New Things in Python
    1. Technical requirements
    2. Recent language additions
    3. Dictionary merge and update operators
    4. Alternative – Dictionary unpacking
    5. Alternative – ChainMap from the collections module
    6. Assignment expressions
    7. Type-hinting generics
    8. Positional-only parameters
    9. zoneinfo module
    10. graphlib module
    11. Not that new, but still shiny
    12. breakpoint() function
    13. Development mode
    14. Module-level __getattr__() and __dir__() functions
    15. Formatting strings with f-strings
    16. Underscores in numeric literals
    17. secrets module
    18. What may come in the future?
    19. Union types with the | operator
    20. Structural pattern matching
    21. Summary
  5. Python in Comparison with Other Languages
    1. Technical requirements
    2. Class model and object-oriented programming
    3. Accessing super-classes
    4. Multiple inheritance and Method Resolution Order
    5. Class instance initialization
    6. Attribute access patterns
    7. Descriptors
    8. Real-life example – lazily evaluated attributes
    9. Properties
    10. Dynamic polymorphism
    11. Operator overloading
    12. Dunder methods (language protocols)
    13. Comparison to C++
    14. Function and method overloading
    15. Single-dispatch functions
    16. Data classes
    17. Functional programming
    18. Lambda functions
    19. The map(), filter(), and reduce() functions
    20. Partial objects and partial functions
    21. Generators
    22. Generator expressions
    23. Decorators
    24. Enumerations
    25. Summary
  6. Interfaces, Patterns, and Modularity
    1. Technical requirements
    2. Interfaces
    3. A bit of history: zope.interface
    4. Using function annotations and abstract base classes
    5. Using collections.abc
    6. Interfaces through type annotations
    7. Inversion of control and dependency injection
    8. Inversion of control in applications
    9. Using dependency injection frameworks
    10. Summary
  7. Concurrency
    1. Technical requirements
    2. What is concurrency?
    3. Multithreading
    4. What is multithreading?
    5. How Python deals with threads
    6. When should we use multithreading?
    7. Application responsiveness
    8. Multiuser applications
    9. Work delegation and background processing
    10. An example of a multithreaded application
    11. Using one thread per item
    12. Using a thread pool
    13. Using two-way queues
    14. Dealing with errors in threads
    15. Throttling
    16. Multiprocessing
    17. The built-in multiprocessing module
    18. Using process pools
    19. Using multiprocessing.dummy as the multithreading interface
    20. Asynchronous programming
    21. Cooperative multitasking and asynchronous I/O
    22. Python async and await keywords
    23. A practical example of asynchronous programming
    24. Integrating non-asynchronous code with async using futures
    25. Executors and futures
    26. Using executors in an event loop
    27. Summary
  8. Event-Driven Programming
    1. Technical requirements
    2. What exactly is event-driven programming?
    3. Event-driven != asynchronous
    4. Event-driven programming in GUIs
    5. Event-driven communication
    6. Various styles of event-driven programming
    7. Callback-based style
    8. Subject-based style
    9. Topic-based style
    10. Event-driven architectures
    11. Event and message queues
    12. Summary
  9. Elements of Metaprogramming
    1. Technical requirements
    2. What is metaprogramming?
    3. Using decorators to modify function behavior before use
    4. One step deeper: class decorators
    5. Intercepting the class instance creation process
    6. Metaclasses
    7. The general syntax
    8. Metaclass usage
    9. Metaclass pitfalls
    10. Using the __init__subclass__() method as an alternative to metaclasses
    11. Code generation
    12. exec, eval, and compile
    13. The abstract syntax tree
    14. Import hooks
    15. Notable examples of code generation in Python
    16. Falcon's compiled router
    17. Hy
    18. Summary
  10. Bridging Python with C and C++
    1. Technical requirements
    2. C and C++ as the core of Python extensibility
    3. Compiling and loading Python C extensions
    4. The need to use extensions
    5. Improving performance in critical code sections
    6. Integrating existing code written in different languages
    7. Integrating third-party dynamic libraries
    8. Creating efficient custom datatypes
    9. Writing extensions
    10. Pure C extensions
    11. A closer look at the Python/C API
    12. Calling and binding conventions
    13. Exception handling
    14. Releasing GIL
    15. Reference counting
    16. Writing extensions with Cython
    17. Cython as a source-to-source compiler
    18. Cython as a language
    19. Downsides of using extensions
    20. Additional complexity
    21. Harder debugging
    22. Interfacing with dynamic libraries without extensions
    23. The ctypes module
    24. Loading libraries
    25. Calling C functions using ctypes
    26. Passing Python functions as C callbacks
    27. CFFI
    28. Summary
  11. Testing and Quality Automation
    1. Technical requirements
    2. The principles of test-driven development
    3. Writing tests with pytest
    4. Test parameterization
    5. pytest's fixtures
    6. Using fakes
    7. Mocks and the unittest.mock module
    8. Quality automation
    9. Test coverage
    10. Style fixers and code linters
    11. Static type analysis
    12. Mutation testing
    13. Useful testing utilities
    14. Faking realistic data values
    15. Faking time values
    16. Summary
  12. Packaging and Distributing Python Code
    1. Technical requirements
    2. Packaging and distributing libraries
    3. The anatomy of a Python package
    4. setup.py
    5. setup.cfg
    6. MANIFEST.in
    7. Essential package metadata
    8. Trove classifiers
    9. Types of package distributions
    10. sdist distributions
    11. bdist and wheel distributions
    12. Registering and publishing packages
    13. Package versioning and dependency management
    14. The SemVer standard for semantic versioning
    15. CalVer for calendar versioning
    16. Installing your own packages
    17. Installing packages directly from sources
    18. Installing packages in editable mode
    19. Namespace packages
    20. Package scripts and entry points
    21. Packaging applications and services for the web
    22. The Twelve-Factor App manifesto
    23. Leveraging Docker
    24. Handling environment variables
    25. The role of environment variables in application frameworks
    26. Creating standalone executables
    27. When standalone executables are useful
    28. Popular tools
    29. PyInstaller
    30. cx_Freeze
    31. py2exe and py2app
    32. Security of Python code in executable packages
    33. Summary
  13. Observing Application Behavior and Performance
    1. Technical requirements
    2. Capturing errors and logs
    3. Python logging essentials
    4. Logging system components
    5. Logging configuration
    6. Good logging practices
    7. Distributed logging
    8. Capturing errors for later review
    9. Instrumenting code with custom metrics
    10. Using Prometheus
    11. Distributed application tracing
    12. Distributed tracing with Jaeger
    13. Summary
  14. Code Optimization
    1. Technical requirements
    2. Common culprits for bad performance
    3. Code complexity
    4. Cyclomatic complexity
    5. The big O notation
    6. Excessive resource allocation and leaks
    7. Excessive I/O and blocking operations
    8. Code profiling
    9. Profiling CPU usage
    10. Macro-profiling
    11. Micro-profiling
    12. Profiling memory usage
    13. Using the objgraph module
    14. C code memory leaks
    15. Reducing complexity by choosing appropriate data structures
    16. Searching in a list
    17. Using sets
    18. Using the collections module
    19. deque
    20. defaultdict
    21. namedtuple
    22. Leveraging architectural trade-offs
    23. Using heuristics and approximation algorithms
    24. Using task queues and delayed processing
    25. Using probabilistic data structures
    26. Caching
    27. Deterministic caching
    28. Non-deterministic caching
    29. Summary
  15. Other Books You May Enjoy
  16. Index
3.133.109.211