Story Transcript
EXPERT INSIGHT
Expert Python Programming Master Python by learning the best coding practices and advanced programming concepts
Fourth Edition Michał Jaworski Tarek Ziadé FOR SALE IN INDIA ONLY
Expert Python Programming Fourth Edition
Master Python by learning the best coding practices and advanced programming concepts
Michał Jaworski Tarek Ziadé
BIRMINGHAM—MUMBAI
Expert Python Programming Fourth Edition Copyright © 2021 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Producer: Shailesh Jain Acquisition Editor – Peer Reviews: Saby D'silva Project Editor: Rianna Rodrigues Content Development Editor: Edward Doxey Copy Editor: Safis Editor Technical Editor: Aditya Sawant Proofreader: Safis Editor Indexer: Pratik Shirodkar Presentation Designer: Pranit Padwal First published: September 2008 Second edition: May 2016 Third edition: April 2019 Fourth edition: May 2021 Production reference: 1260521 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-80107-110-9 www.packt.com
Contributors About the authors Michał Jaworski has more than 10 years of professional experience in writing
software using various programming languages. Michał has spent most of his career writing high-performance and distributed backend services for web applications. He has served in various roles at multiple companies: from an ordinary software engineer to lead software architect. His beloved language of choice has always been Python. I want to thank my wife for giving me constant support. Oliwia, you're the only person who knew from the very beginning that I was lying to myself when I said that this will be a light project that won't take much of my (our) time. I don't know why, but you encouraged me to do it anyway.
Tarek Ziadé is a software engineer, located in Burgundy, France. He works at
Elastic, building tools for developers. Before Elastic, he worked at Mozilla for 10 years, and he founded a French Python User group, called AFPy. Tarek has also written several articles about Python for various magazines, and a few books in French and English. I would like to thank Freya, Suki, Milo, and Amina for being so supportive of all my book projects.
About the reviewer Tal Einat has been developing software for nearly 20 years, of which Python has always been a major element. He's been a core developer of the Python language since 2010. Tal holds a B.Sc. in Math & Physics from Tel Aviv University. He likes hiking, computer games, philosophical sci-fi, and spending time with his family. For the past eight years, Tal has been developing educational technology, first at Compedia where he built a group developing VR and AR education apps, and later at the startup FullProof of which he was a co-founder. Tal currently works at Rhino Health, a startup working to the enable development and use of medical AI models with patient data from across the globe while preserving patient privacy. I dedicate my part of the work on this book to my grandfather Jacob "Yanek" Friedman who passed away recently — you live on in our thoughts, making us strong and putting a smile on our faces. I'd like to thank my wife, children, brother, parents and extended family, for being ever supportive and filling my life with so much joy.
Table of Contents Preface Chapter 1: Current Status of Python Where are we now and where are we going? What to do with Python 2 Keeping up to date PEP documents Active communities Other resources Summary
Chapter 2: Modern Python Development Environments Technical requirements Python's packaging ecosystem Installing Python packages using pip Isolating the runtime environment Application-level isolation versus system-level isolation Application-level environment isolation Poetry as a dependency management system System-level environment isolation Containerization versus virtualization Virtual environments using Docker Writing your first Dockerfile Running containers Setting up complex environments Useful Docker and Docker Compose recipes for Python
Virtual development environments using Vagrant Popular productivity tools Custom Python shells [i]
ix 1 2 3 5 6 8 11 12
15 16 17 17 19 23 24 27 32 34 36 37 41 43 46
56 59 59
Table of Contents
Using IPython Incorporating shells in your own scripts and programs Interactive debuggers Other productivity tools Summary
Chapter 3: New Things in Python
61 65 66 68 70
71
Technical requirements Recent language additions Dictionary merge and update operators Alternative – Dictionary unpacking Alternative – ChainMap from the collections module
Assignment expressions Type-hinting generics Positional-only parameters zoneinfo module graphlib module Not that new, but still shiny breakpoint() function Development mode Module-level __getattr__() and __dir__() functions Formatting strings with f-strings Underscores in numeric literals secrets module What may come in the future? Union types with the | operator Structural pattern matching Summary
Chapter 4: Python in Comparison with Other Languages Technical requirements Class model and object-oriented programming Accessing super-classes Multiple inheritance and Method Resolution Order Class instance initialization Attribute access patterns Descriptors Real-life example – lazily evaluated attributes
Properties Dynamic polymorphism Operator overloading
72 72 73 76 76
79 83 84 87 88 93 93 94 97 98 100 100 101 102 103 108
109 110 110 112 114 120 124 125 128
132 138 140
Dunder methods (language protocols) Comparison to C++
141 145 [ ii ]
Table of Contents
Function and method overloading
147
Single-dispatch functions
149
Data classes Functional programming Lambda functions The map(), filter(), and reduce() functions Partial objects and partial functions Generators Generator expressions Decorators Enumerations Summary
Chapter 5: Interfaces, Patterns, and Modularity Technical requirements Interfaces A bit of history: zope.interface Using function annotations and abstract base classes Using collections.abc
151 155 157 159 162 163 165 166 168 171
173 174 175 177 186 191
Interfaces through type annotations Inversion of control and dependency injection Inversion of control in applications Using dependency injection frameworks Summary
Chapter 6: Concurrency
192 195 197 206 212
213
Technical requirements What is concurrency? Multithreading What is multithreading? How Python deals with threads When should we use multithreading? Application responsiveness Multiuser applications Work delegation and background processing
An example of a multithreaded application Using one thread per item Using a thread pool Using two-way queues Dealing with errors in threads Throttling
214 214 216 217 221 223 223 224 225
226 229 231 236 238 241
Multiprocessing The built-in multiprocessing module Using process pools [ iii ]
245 247 251
Table of Contents
Using multiprocessing.dummy as the multithreading interface Asynchronous programming Cooperative multitasking and asynchronous I/O Python async and await keywords A practical example of asynchronous programming Integrating non-asynchronous code with async using futures Executors and futures Using executors in an event loop
254 255 256 257 262 265 267 268
Summary
269
Chapter 7: Event-Driven Programming
271
Technical requirements What exactly is event-driven programming? Event-driven != asynchronous Event-driven programming in GUIs Event-driven communication Various styles of event-driven programming Callback-based style Subject-based style Topic-based style Event-driven architectures Event and message queues Summary
272 272 273 274 277 279 280 281 286 288 290 293
Chapter 8: Elements of Metaprogramming
295
Technical requirements What is metaprogramming? Using decorators to modify function behavior before use One step deeper: class decorators Intercepting the class instance creation process Metaclasses The general syntax Metaclass usage Metaclass pitfalls Using the __init__subclass__() method as an alternative to metaclasses Code generation exec, eval, and compile The abstract syntax tree Import hooks Notable examples of code generation in Python Falcon's compiled router Hy
296 296 297 299 304 307 309 312 315 317 319 319 321 323 323 324 325
Summary
326 [ iv ]
Table of Contents
Chapter 9: Bridging Python with C and C++ Technical requirements C and C++ as the core of Python extensibility Compiling and loading Python C extensions The need to use extensions Improving performance in critical code sections Integrating existing code written in different languages Integrating third-party dynamic libraries Creating efficient custom datatypes Writing extensions Pure C extensions
327 329 329 330 332 333 334 335 335 336 337
A closer look at the Python/C API Calling and binding conventions Exception handling Releasing GIL Reference counting
341 345 349 351 353
Writing extensions with Cython
356
Cython as a source-to-source compiler Cython as a language
356 360
Downsides of using extensions Additional complexity Harder debugging Interfacing with dynamic libraries without extensions The ctypes module Loading libraries Calling C functions using ctypes Passing Python functions as C callbacks
362 363 364 365 365 365 367 369
CFFI Summary
372 374
Chapter 10: Testing and Quality Automation Technical requirements The principles of test-driven development Writing tests with pytest Test parameterization pytest's fixtures Using fakes Mocks and the unittest.mock module Quality automation Test coverage Style fixers and code linters Static type analysis Mutation testing [v]
377 378 379 381 389 392 402 405 410 411 415 419 420
Table of Contents
Useful testing utilities Faking realistic data values Faking time values Summary
427 427 429 430
Chapter 11: Packaging and Distributing Python Code Technical requirements Packaging and distributing libraries The anatomy of a Python package setup.py setup.cfg MANIFEST.in Essential package metadata Trove classifiers
433 434 434 435 438 440 440 442 443
Types of package distributions
445
sdist distributions bdist and wheel distributions
445 447
Registering and publishing packages Package versioning and dependency management The SemVer standard for semantic versioning CalVer for calendar versioning
Installing your own packages
450 453 455 456
457
Installing packages directly from sources Installing packages in editable mode
457 458
Namespace packages Package scripts and entry points Packaging applications and services for the web The Twelve-Factor App manifesto Leveraging Docker Handling environment variables The role of environment variables in application frameworks Creating standalone executables When standalone executables are useful Popular tools PyInstaller cx_Freeze py2exe and py2app
459 461 465 466 467 470 475 480 481 481 482 486 488
Security of Python code in executable packages Summary
Chapter 12: Observing Application Behavior and Performance Technical requirements Capturing errors and logs Python logging essentials
490 491
493 494 494 495
Logging system components
497 [ vi ]
Table of Contents Logging configuration
505
Good logging practices Distributed logging Capturing errors for later review Instrumenting code with custom metrics Using Prometheus Distributed application tracing Distributed tracing with Jaeger Summary
Chapter 13: Code Optimization
509 511 514 518 520 530 534 540
541
Technical requirements Common culprits for bad performance Code complexity Cyclomatic complexity The big O notation
542 542 543 544 545
Excessive resource allocation and leaks Excessive I/O and blocking operations Code profiling Profiling CPU usage Macro-profiling Micro-profiling
548 549 549 551 551 557
Profiling memory usage
560
Using the objgraph module C code memory leaks
562 570
Reducing complexity by choosing appropriate data structures Searching in a list Using sets Using the collections module deque defaultdict namedtuple
571 571 573 574 574 576 578
Leveraging architectural trade-offs Using heuristics and approximation algorithms Using task queues and delayed processing Using probabilistic data structures Caching Deterministic caching Non-deterministic caching
580 580 581 585 586 587 590
Summary Why subscribe?
595 597
Other Books You May Enjoy Index
599 601 [ vii ]
Preface Python rocks! From the earliest version in the late 1980s to the current 3.9 version, Python has evolved with the same philosophy: providing a multi-paradigm programming language with readability and productivity in mind. Initially, people used to see Python as yet another scripting language. Many of them didn't believe it could be used to build large and complex systems. But over the years, and thanks to some pioneer companies, it became obvious that Python could be used to build almost any kind of a software. Although writing Python code is easy, making it readable, reusable, and easy to maintain is challenging. You can achieve those qualities only through good software artistry and technique, which you will build gradually by constantly learning and gaining experience. This book was written to express many years of professional experience in building all kinds of applications with Python, from small system scripts done in a couple of hours to very large applications written by dozens of developers over several years. This book is divided into three parts: 1. Knowing your tools: Chapters 1 to 4 focus on basic elements of Python programmer's toolbelt. From productivity tools, through modern environments, to the newest syntax elements introduced in the latest Python releases. It also offers a safe landing zone for programmers who have experience with other programming languages and are just starting to learn more advanced Python. 2. Building applications with Python: Chapters 5 to 9 are all about design patterns, programming paradigms, and metaprogramming techniques. We will try to build some small but useful programs and will be often taking a deeper look into application architecture. We will also go a bit beyond Python and see how we can integrate code written using other programming languages. [ ix ]
Preface
3. Maintaining Python applications: Chapters 10 to 13 will be discussing all the things that usually happen after the application "goes live". We will showcase tools and techniques that help to keep applications easily maintainable and show how to approach common problems with packaging, deployment, monitoring, and performance optimization.
Who this book is for The Python programming book is intended for expert programmers who want to learn about Python's advanced-level concepts and latest features. This book is written for Python developers who wish to go further in mastering Python. And by developers, I mean mostly professional programmers who write Python software for a living. This is because it focuses mostly on tools and practices that are crucial for creating performant, reliable, and maintainable software in Python. However, this does not mean that hobbyists won't find anything interesting. This book is great for anyone who is interested in learning advanced-level concepts with Python. 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 the newest releases of Python for those who are still a bit behind and continue to use older versions of Python.
What this book covers Chapter 1, Current Status of Python, showcases the current state of the Python language and its community. We will see how Python is constantly changing and why it is changing. We will learn what to do with old Python 2 code and how to be constantly up to date with what is currently happening in the Python community. Chapter 2, Modern Python Development Environments, describes modern ways of setting up repeatable and consistent development environments for Python programmers. We will learn differences between application-level and systemlevel isolation. We will concentrate on two popular tools for environment isolation, virtualenv-type environments and Docker containers, but will also review other alternatives. At the end of the chapter, we will discuss common productivity tools that are extremely useful during development. Chapter 3, New Things in Python, showcases recent Python language additions. We will review the most important Python syntax changes that happened in the last four releases of Python. We will also take a look at exciting changes that are scheduled for the next major Python release—Python 3.10. [x]
Preface
Chapter 4, Python in Comparison with Other Languages, shows how Python compares to other languages. We will learn what programming idioms are and how to recognize them in code. We will take a deeper look into key elements of Python's object-oriented programming model and how it is different from other objectoriented programming languages but will also discuss other popular programming language features like descriptors, decorators, and dataclasses. This chapter should allow programmers with experience in other languages to safely land in the Python ecosystem. Chapter 5, Interfaces, Patterns, and Modularity, discusses elements of Python that allow for implementing various reusable design patterns. If focuses on the concept of class interfaces and how they can be implemented in Python. It also discusses inversion of control and dependency injection—two extremely useful but not necessarily popular programming techniques. Chapter 6, Concurrency, explains how to implement concurrency in Python using different approaches and libraries. It features three major concurrency models: multithreading, multiprocessing and asynchronous programming. In this chapter we will learn key differences between those models and how to use them effectively. Chapter 7, Event-Driven Programming, describes what event-driven programming is and how it relates to asynchronous programming and different concurrency models. We will present various approaches to event-driven programming along with useful libraries. Chapter 8, Elements of Metaprogramming, presents an overview of common approaches to metaprogramming available to Python programmers. We will learn about common metaprogramming techniques like decorators, as well as metaclasses and code generation patterns. Chapter 9, Bridging Python with C and C++, explains how to integrate code written in different languages in your Python application. We will learn when extensions in C can be useful and how to create them. Chapter 10, Testing and Quality Automation, is about providing automated testing and quality processes. We will learn about a popular testing framework—Pytest—and many useful testing techniques. We will also cover tools that can be used to assess code quality metrics and improve code style in fully automated way. Chapter 11, Packaging and Distributing Python Code, describes the current state of Python packaging and best practices for creating packages that are to be distributed as open source code in the Python Package Index (PyPI). We will also cover the topics of packaging applications for web development and creating standalone Python executables for desktop applications. [ xi ]
Expert Python Programming, Fourth Edition Python is used in a wide range of domains, thanks to its simple yet powerful nature. Although writing Python code is easy, making it readable, reusable, and easy to maintain can be challenging. Complete with best practices, useful tools, and standards implemented by professional Python developers, this fourth edition will help you in not only overcoming such challenges but also learning Python's latest features and advanced concepts. The book begins with a warm-up, where you will catchup with the latest Python improvements, syntax elements, and interesting tools to boost your development efficiency. Further, the initial few chapters will allow experienced programmers coming from different languages to safely land in the Python ecosystem.
As you progress, you will explore common software design patterns and various programming methodologies, such as eventdriven programming, concurrency, and metaprogramming. You will also go through complex code examples and try to solve meaningful problems by bridging Python and C and C++, writing extensions that benefit from the strengths of multiple languages. Finally, you will learn about the complete lifecycle of any application after it goes live. By the end of the book, you should be proficient in writing efficient and maintainable Python code.
www.packtpub.com
Things 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 about modern syntax elements of Python programming, such as f-strings, dataclasses, enums, and lambda functions
•
Demystify metaprogramming in Python with metaclasses
•
Write concurrent code in Python
•
Monitor and optimize the performance of Python application
•
Extend and integrate Python with code written in different languages
FOR SALE IN INDIA ONLY