logo资料库

Expert Python Programming-3rd edition (2019).pdf

第1页 / 共629页
第2页 / 共629页
第3页 / 共629页
第4页 / 共629页
第5页 / 共629页
第6页 / 共629页
第7页 / 共629页
第8页 / 共629页
资料共629页,剩余部分请下载后查看
Cover
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Table of Contents
Preface
Section 1: Before You Start
Chapter 1: Current Status of Python
Technical requirements
Where are we now and where we are going to?
Why and how Python changes
Being up-to-date with changes ­by following PEP documents
Python 3 adoption at the time of writing this book
The main differences between Python 3 and Python 2
Why should I care?
The main syntax differences and common pitfalls
Syntax changes
Changes in the standard library
Changes in data types and collections and string literals
The popular tools and techniques used for maintaining cross-version compatibility
Not only CPython
Why should I care?
Stackless Python
Jython
IronPython
PyPy
MicroPython
Useful resources
Summary
Chapter 2: Modern Python Development Environments
Technical requirements
Installing additional Python packages using pip
Isolating the runtime environment
Application-level isolation versus system-level isolation
Python's venv
venv versus virtualenv
System-level environment isolation
Virtual development environments using Vagrant
Virtual environments using Docker
Containerization versus virtualization
Writing your first Dockerfile
Running containers
Setting up complex environments
Useful Docker recipes for Python
Reducing the size of containers
Addressing services inside of a Compose environment
Communicating between multiple Compose environments
Popular productivity tools
Custom Python shells – ipython, bpython, ptpython, and so on
Setting up the PYTHONSTARTUP environment variable
IPython
bpython
ptpython
Incorporating shells in your own scripts and programs
Interactive debuggers
Summary
Section 2: Python Craftsmanship
Chapter 3: Modern Syntax Elements - Below the Class Level
Technical requirements
Python's built-in types
Strings and bytes
Implementation details
String concatenation
Constant folding, the peephole optimizer, and the AST optimizer
String formatting with f-strings
Containers
Lists and tuples
Implementation details
List comprehensions
Other idioms
Dictionaries
Implementation details
Weaknesses and alternatives
Sets
Implementation details
Supplemental data types and containers
Specialized data containers from the collections module
Symbolic enumeration with the enum module
Advanced syntax
Iterators
Generators and yield statements
Decorators
General syntax and possible implementations
As a function
As a class
Parametrizing decorators
Introspection preserving decorators
Usage and useful examples
Argument checking
Caching
Proxy
Context provider
Context managers – the with statement
The general syntax and possible implementations
As a class
As a function – the contextlib module
Functional-style features of Python
What is functional programming?
Lambda functions
map(), filter(), and reduce()
Partial objects and partial() functions
Generator expressions
Function and variable annotations
The general syntax
The possible uses
Static type checking with mypy
Other syntax elements you may not know of yet
The for ... else ... statement
Keyword-only arguments
Summary
Chapter 4: Modern Syntax Elements - Above the Class Level
Technical requirements
The protocols of the Python language – dunder methods and attributes
Reducing boilerplate with data classes
Subclassing built-in types
MRO and accessing methods from superclasses
Old-style classes and super in Python 2
Understanding Python's Method Resolution Order
Super pitfalls
Mixing super and explicit class calls
Heterogeneous arguments
Best practices
Advanced attribute access patterns
Descriptors
Real-life example – lazily evaluated attributes
Properties
Slots
Summary
Chapter 5: Elements of Metaprogramming
Technical requirements
What is metaprogramming?
Decorators – a method of metaprogramming
Class decorators
Using __new__() for overriding the instance creation process
Metaclasses
The general syntax
New Python 3 syntax for metaclasses
Metaclass usage
Metaclass pitfalls
Code generation
exec, eval, and compile
Abstract syntax tree (AST)
Import hooks
Projects that use code generation patterns
Falcon's compiled router
Hy
Summary
Chapter 6: Choosing Good Names
Technical requirements
PEP 8 and naming best practices
Why and when to follow PEP 8?
Beyond PEP 8 – Team-specific style guidelines
Naming styles
Variables
Constants
Naming and usage
Public and private variables
Functions and methods
The private controversy
Special methods
Arguments
Properties
Classes
Modules and packages
The naming guide
Using the has/is prefixes for Boolean elements
Using plurals for variables that are collections
Using explicit names for dictionaries
Avoid generic names and redundancy
Avoiding existing names
Best practices for arguments
Building arguments by iterative design
Trusting the arguments and your tests
Using *args and **kwargs magic arguments carefully
Class names
Module and package names
Useful tools
Pylint
pycodestyle and flake8
Summary
Chapter 7: Writing a Package
Technical requirements
Creating a package
The confusing state of Python packaging tools
The current landscape of Python packaging thanks to PyPA
Tool recommendations
Project configuration
setup.py
setup.cfg
MANIFEST.in
Most important metadata
Trove classifiers
Common patterns
Automated inclusion of version string from package
README file
Managing dependencies
The custom setup command
Working with packages during development
setup.py install
Uninstalling packages
setup.py develop or pip -e
Namespace packages
Why is it useful?
PEP 420 - implicit namespace packages
Namespace packages in previous Python versions
Uploading a package
PyPI - Python Package Index
Uploading to PyPI - or other package index
.pypirc
Source packages versus built packages
sdist
bdist and wheels
Standalone executables
When standalone executables useful?
Popular tools
PyInstaller
cx_Freeze
py2exe and py2app
Security of Python code in executable packages
Making decompilation harder
Summary
Chapter 8: Deploying the Code
Technical requirements
The Twelve-Factor App
Various approaches to deployment automation
Using Fabric for deployment automation 
Your own package index or index mirror
PyPI mirroring
Bundling additional resources with your Python package
Common conventions and practices
The filesystem hierarchy
Isolation
Using process supervision tools
Application code running in user space
Using reverse HTTP proxies
Reloading processes gracefully
Code instrumentation and monitoring
Logging errors – Sentry/Raven
Monitoring system and application metrics
Dealing with application logs
Basic low-level log practices
Tools for log processing
Summary
Chapter 9: Python Extensions in Other Languages
Technical requirements
Differentiating between the C and C++ languages
Loading extensions in C or C++
The need to use extensions
Improving the performance in critical code sections
Integrating existing code written in different languages
Integrating third-party dynamic libraries
Creating custom datatypes
Writing extensions
Pure C extensions
A closer look at Python/C API
Calling and binding conventions
Exception handling
Releasing GIL
Reference counting
Writing extensions with Cython
Cython as a source-to-source compiler
Cython as a language
Challenges with using extensions
Additional complexity
Debugging
Interfacing with dynamic libraries without extensions
The ctypes module
Loading libraries
Calling C functions using ctypes
Passing Python functions as C callbacks
CFFI
Summary
Section 3: Quality over Quantity
Chapter 10: Managing Code
Technical requirements
Working with a version control system
Centralized systems
Distributed systems
Distributed strategies
Centralized or distributed?
Use Git if you can
GitFlow and GitHub Flow
Setting up continuous development processes
Continuous integration
Testing every commit
Merge testing through CI
Matrix testing
Continuous delivery
Continuous deployment
Popular tools for continuous integration
Jenkins
Buildbot
Travis CI
GitLab CI
Choosing the right tool and common pitfalls
Problem 1 – Complex build strategies
Problem 2 – Long building time
Problem 3 – External job definitions
Problem 4 – Lack of isolation
Summary
Chapter 11: Documenting Your Project
Technical requirements
The seven rules of technical writing
Write in two steps
Target the readership
Use a simple style
Limit the scope of information
Use realistic code examples
Use a light but sufficient approach
Use templates
Documentation as code
Using Python docstrings
Popular markup languages and styles for documentation
Popular documentation generators for Python libraries
Sphinx
Working with the index pages
Registering module helpers
Adding index markers
Cross-references
MkDocs
Documentation building and continuous integration
Documenting web APIs
Documentation as API prototype with API Blueprint
Self-documenting APIs with Swagger/OpenAPI
Building a well-organized documentation system
Building documentation portfolio
Design
Usage
Recipe
Tutorial
Module helper
Operations
Your very own documentation portfolio
Building a documentation landscape
Producer's layout
Consumer's layout
Summary
Chapter 12: Test-Driven Development
Technical requirements
I don't test
Three simple steps of test-driven development
Preventing software regression
Improving code quality
Providing the best developer documentation
Producing robust code faster
What kind of tests?
Unit tests
Acceptance tests
Functional tests
Integration tests
Load and performance testing
Code quality testing
Python standard test tools
unittest
doctest
I do test
unittest pitfalls
unittest alternatives
nose
Test runner
Writing tests
Writing test fixtures
Integration with setuptools and plugin system
Wrap-up
py.test
Writing test fixtures
Disabling test functions and classes
Automated distributed tests
Wrap-up
Testing coverage
Fakes and mocks
Building a fake
Using mocks
Testing environment and dependency compatibility
Dependency matrix testing
Document-driven development
Writing a story
Summary
Section 4: Need for Speed
Chapter 13: Optimization - Principles and Profiling Techniques
Technical requirements
The three rules of optimization
Making it work first
Working from the user's point of view
Keeping the code readable and maintainable
Optimization strategy
Looking for another culprit
Scaling the hardware
Writing a speed test
Finding bottlenecks
Profiling CPU usage
Macro-profiling
Micro-profiling
Profiling memory usage
How Python deals with memory
Profiling memory
objgraph
C code memory leaks
Profiling network usage
Tracing network transactions
Summary
Chapter 14: Optimization - Some Powerful Techniques
Technical requirements
Defining complexity
Cyclomatic complexity
The big O notation
Reducing complexity by choosing proper data structures
Searching in a list
Using sets
Using collections
deque
defaultdict
namedtuple
Using architectural trade-offs
Using heuristics and approximation algorithms
Using task queues and delayed processing
Using probabilistic data structures
Caching
Deterministic caching
Non-deterministic caching
Cache services
Memcached
Summary
Chapter 15: Concurrency
Technical requirements
Why concurrency?
Multithreading
What is multithreading?
How Python deals with threads
When should we use threading?
Building responsive interfaces
Delegating work
Multiuser applications
An example of a threaded application
Using one thread per item
Using a thread pool
Using two-way queues
Dealing with errors and rate limiting
Multiprocessing
The built-in multiprocessing module
Using process pools
Using multiprocessing.dummy as the multithreading interface
Asynchronous programming
Cooperative multitasking and asynchronous I/O
Python async and await keywords
asyncio in older versions of Python
A practical example of asynchronous programming
Integrating non-asynchronous code with async using futures
Executors and futures
Using executors in an event loop
Summary
Section 5: Technical Architecture
Chapter 16: Event-Driven and Signal Programming
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
Chapter 17: Useful Design Patterns
Technical requirements
Creational patterns
Singleton
Structural patterns
Adapter
Interfaces
Using zope.interface
Using function annotations and abstract base classes
Using collections.abc
Proxy
Facade
Behavioral patterns
Observer
Visitor
Template
Summary
Appendix A: reStructuredText Primer
reStructuredText
Section structure
Lists
Inline markup
Literal block
Links
Other Books You May Enjoy
Index
Expert Python Programming Third Edition Become a master in Python by learning coding best practices and advanced programming concepts in Python 3.7 Michał Jaworski Tarek Ziadé BIRMINGHAM - MUMBAI
Expert Python Programming Third Edition Copyright © 2019 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. Commissioning Editor: Kunal Chaudhari Acquisition Editor: Chaitanya Nair Content Development Editor: Zeeyan Pinheiro Technical Editor: Ketan Kamble Copy Editor: Safis Editing Project Coordinator: Vaidehi Sawant Proofreader: Safis Editing Indexer: Priyanka Dhadke Graphics: Alishon Mendonsa Production Coordinator: Shraddha Falebhai First published: September 2008 Second edition: May 2016 Third edition: April 2019 Production reference: 1270419 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78980-889-6 www.packtpub.com
To my beloved wife, Oliwia, for her love, inspiration, and her endless patience. To my loyal friends, Piotr, Daniel, and Paweł, for their support. To my mother, for introducing me to the amazing world of programming. – Michał Jaworski
mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content Packt.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors About the authors Michał Jaworski has 10 years' of professional experience in Python. He has been in various roles at different companies, from an ordinary full-stack developer, through software architect, to VP of engineering in a fast-paced start-up company. He is currently a senior backend engineer at Showpad. He is highly experienced in designing high-performance distributed services. He is also an active contributor to many open source Python projects. Tarek Ziadé is a Python developer located in the countryside near Dijon, France. He works at Mozilla in the services team. He founded a French Python user group called Afpy, and has written several books about Python in French and English. When he is not hacking on his computer or hanging out with his family, he's spending time between his two passions, running and playing the trumpet. You can visit his personal blog (Fetchez le Python) and follow him on Twitter (tarek_ziade).
About the reviewer Cody Jackson is a disabled military veteran, the founder of Socius Consulting, an IT and business management consulting company in San Antonio, and a co-founder of Top Men Technologies. He is currently employed at CACI International as the lead ICS/SCADA modeling and simulations engineer. He has been involved in the tech industry since 1994, when he joined the Navy as a nuclear chemist and radcon technician. Prior to CACI, he worked at ECPI University as a computer information systems adjunct professor. A self- taught Python programmer, he is the author of Learning to Program Using Python and Secret Recipes of the Python Ninja. He holds an Associate in Science degree, a Bachelor of Science degree, and a Master of Science degree. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
分享到:
收藏