Python 3 教程 文档

  • Effective Python: 90 Specific Ways to Write Better Python, 2nd Edition
    • 1. Pythonic Thinking
      • 1.1. Know Which Version of Python You’re Using
      • 1.2. Follow the PEP 8 Style Guide
      • 1.3. Know the Differences Between bytes and str
      • 1.4. Prefer Interpolated F-Strings Over C-style Format Strings and str.format
      • 1.5. Write Helper Functions Instead of Complex Expressions
      • 1.6. Prefer Multiple Assignment Unpacking Over Indexing
      • 1.7. The range built-in function is useful for loops
      • 1.8. Use zip to Process Iterators in Parallel
      • 1.9. Avoid else Blocks After for and while Loops
      • 1.10. Prevent Repetition with Assignment Expressions
    • 2. Lists and Dictionaries
      • 2.1. Know How to Slice Sequences
      • 2.2. Avoid Striding and Slicing in a Single Expression
      • 2.3. Prefer Catch-All Unpacking Over Slicing
      • 2.4. Sort by Complex Criteria Using the key Parameter
      • 2.5. Be Cautious When Relying on dict Insertion Ordering
      • 2.6. Prefer get Over in and KeyError to Handle Missing Dictionary Keys
      • 2.7. Prefer defaultdict Over setdefault to Handle Missing Items in Internal State
      • 2.8. Know How to Construct Key-Dependent Default Values with __missing__
    • 3. Functions
      • 3.1. Never Unpack More Than Three Variables When Functions Return Multiple Values
      • 3.2. Prefer Raising Exceptions to Returning None
      • 3.3. Know How Closures Interact with Variable Scope
      • 3.4. Reduce Visual Noise with Variable Positional Arguments
      • 3.5. Provide Optional Behavior with Keyword Arguments
      • 3.6. Use None and Docstrings to Specify Dynamic Default Arguments
      • 3.7. Enforce Clarity with Keyword-Only and Positional-Only Arguments
      • 3.8. Define Function Decorators with functools.wraps
    • 4. Comprehensions and Generators
      • 4.1. Use Comprehensions Instead of map and filter
      • 4.2. Avoid More Than Two Control Subexpressions in Comprehensions
      • 4.3. Avoid Repeated Work in Comprehensions by Using Assignment Expressions
      • 4.4. Consider Generators Instead of Returning Lists
      • 4.5. Be Defensive When Iterating Over Arguments
      • 4.6. Consider Generator Expressions for Large List Comprehensions
      • 4.7. Compose Multiple Generators with yield from
      • 4.8. Avoid Injecting Data into Generators with send
      • 4.9. Avoid Causing State Transitions in Generators with throw
      • 4.10. Consider itertools for Working with Iterators and Generators
    • 5. Clases and Interfaces
      • 5.1. Compose Classes Instead of Nesting Many Levels of Built-in Types
      • 5.2. Accept Functions Instead of Classes for Simple Interfaces
      • 5.3. Use @classmethod Polymorphism to Construct Objects Generically
      • 5.4. Initialize Parent Classes with super
      • 5.5. Consider Composing Functionality with Mix-in Classes
      • 5.6. Prefer Public Attributes Over Private Ones
      • 5.7. Inherit from collections.abc for Custom Container Types
    • 6. Metaclasses and Attributes
      • 6.1. Use Plain Attributes Instead of Setter and Getter Methods
      • 6.2. Consider @property Instead of Refactoring Attributes
      • 6.3. Use Descriptors for Reusable @property Methods
      • 6.4. Use __getattr__, __getattribute__, and __setattr__ for Lazy Attributes
      • 6.5. Validate Subclasses with __init_subclass__
      • 6.6. Register Class Existence with __init_subclass__
      • 6.7. Annotate Class Attributes with __set_name__
      • 6.8. Prefer Class Decorators Over Metaclasses for Composable Class Extensions
    • 7. Concurrency-and-Parallelism
      • 7.1. Use subprocess to Manage Child Processes
      • 7.2. Use Threads for Blocking I/O, Avoid for Parallelism
      • 7.3. Use Lock to Prevent Data Races in Threads
      • 7.4. Use Queue to Coordinate Work Between Threads
      • 7.5. Know How to Recognize When Concurrency Is Necessary
      • 7.6. Avoid Creating New Thread Instances for On-demand Fan-out
      • 7.7. Understand How Using Queue for Concurrency Requires Refactoring
      • 7.8. Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency
      • 7.9. Achieve Highly Concurrent I/O with Coroutines
      • 7.10. Know How to Port Threaded I/O to asyncio
      • 7.11. Mix Threads and Coroutines to Ease the Transition to asyncio
      • 7.12. Avoid Blocking the asyncio Event Loop to Maximize Responsiveness
      • 7.13. Consider concurrent.futures for True Parallelism
    • 8. Robustness-and-Performance
      • 8.1. Take Advantage of Each Block in try/except/else/finally
      • 8.2. Consider contextlib and with Statements for Reusable try/finally Behavior
      • 8.3. Use datetime Instead of time for Local Clocks
      • 8.4. Make pickle Reliable with copyreg
      • 8.5. Use decimal When Precision Is Paramount
      • 8.6. Profile Before Optimizing
      • 8.7. Prefer deque for Producer–Consumer Queues
      • 8.8. Consider Searching Sorted Sequences with bisect
      • 8.9. Know How to Use heapq for Priority Queues
      • 8.11. Consider memoryview and bytearray for Zero-Copy Interactions with bytes
    • 9. Testing-and-Debugging
      • 9.1. Use repr Strings for Debugging Output
      • 9.2. Verify Related Behaviors in TestCase Subclasses
      • 9.4. Isolate Tests from Each Other with setUp, tearDown, setUpModule, and tearDownModule
      • 9.5. Use Mocks to Test Code with Complex Dependencies
      • 9.6. Encapsulate Dependencies to Facilitate Mocking and Testing
      • 9.7. Consider Interactive Debugging with pdb
      • 9.8. Use tracemalloc to Understand Memory Usage and Leaks
    • 10. Collaboration
      • 10.1. Know Where to Find Community-Built Modules
      • 10.2. Use Virtual Environments for Isolated and Reproducible Dependencies
      • 10.3. Write Docstrings for Every Function, Class, and Module
      • 10.4. Use Packages to Organize Modules and Provide Stable APIs
      • 10.5. Consider Module-Scoped Code to Configure Deployment Environments
      • 10.6. Define a Root Exception to Insulate Callers from APIs
      • 10.7. Know How to Break Circular Dependencies
      • 10.8. Consider warnings to Refactor and Migrate Usage
      • 10.9. Consider Static Analysis via typing to Obviate Bugs
  • Beyond the Basic Stuff with Python
    • 1. ENVIRONMENT SE TUP
      • 1.1. The Filesystem
      • 1.2. Paths in Python
      • 1.3. Programs and Processes
      • 1.4. The Command Line
      • 1.5. Environment Variables and PATH
      • 1.6. Running Python Programs Without the Command Line
    • 2. CODE FOR M AT T ING W IT H BL ACK
      • 2.1. How to Lose Friends and Alienate Co-Workers
      • 2.2. Horizontal Spacing
      • 2.3. Vertical Spacing
      • 2.4. Black: The Uncompromising Code Formatter
    • 3. CHOOSING UNDERSTANDABLE NAMES
      • 3.1. Casing Styles
      • 3.3. Summary
    • 4. FINDING CODE SMELL S
      • 4.1. Duplicate Code
      • 4.2. Magic Numbers
      • 4.3. Commented-Out Code and Dead Code
      • 4.4. Print Debugging
      • 4.5. Variables with Numeric Suffixes
      • 4.6. Classes That Should Just Be Functions or Modules
      • 4.7. List Comprehensions Within List Comprehensions
      • 4.8. Empty except Blocks and Poor Error Messages
      • 4.9. Code Smell Myths
      • 4.10. Summary
    • 5. WRITING PY THONIC CODE
      • 5.1. The Zen of Python
      • 5.2. Learning to Love Significant Indentation
      • 5.3. Commonly Misused Syntax
      • 5.4. Formatting Strings
      • 5.5. Making Shallow Copies of Lists
      • 5.6. Pythonic Ways to Use Dictionaries
      • 5.7. Conditional Expressions: Python’s “Ugly” Ternary Operator
      • 5.8. Working with Variable Values
      • 5.9. Summary
    • 6. PROGR AMMING JARGON
      • 6.1. Definitions
      • 6.2. Commonly Confused Terms
      • 6.3. Summary
      • 6.4. Further Reading
    • 7. COMMON PYTHON GOTCHAS
      • 7.1. Don’t Add or Delete Items from a List While Looping Over It
      • 7.2. Don’t Copy Mutable Values Without copy.copy() and copy.deepcopy()
      • 7.3. Don’t Use Mutable Values for Default Arguments
      • 7.4. Don’t Build Strings with String Concatenation
      • 7.5. Don’t Expect sort() to Sort Alphabetically
      • 7.6. Don’t Assume Floating-Point Numbers Are Perfectly Accurate
      • 7.7. Don’t Chain Inequality != Operators
      • 7.8. Don’t Forget the Comma in Single-Item Tuples
      • 7.9. Summary
    • 8. ESOTERIC PYTHON ODDITIES
      • 8.1. Why 256 Is 256 but 257 Is Not 257
      • 8.2. String Interning
      • 8.3. Python’s Fake Increment and Decrement Operators
      • 8.4. All of Nothing
      • 8.5. Boolean Values Are Integer Values
      • 8.6. Chaining Multiple Kinds of Operators
      • 8.7. Python’s Antigravity Feature
      • 8.8. Summary
    • 9. WRITING EFFECTIVE FUNCTIONS
      • 9.1. Function Size Trade-Offs
      • 9.2. Function Parameters and Arguments
      • 9.3. Functional Programming
      • 9.4. Return Values Should Always Have the Same Data Type
      • 9.5. Raising Exceptions vs. Returning Error Codes
      • 9.6. Summary
    • 10. COMME N T S, DOCS T R INGS, AND T YPE HINTS
      • 10.1. Comments
      • 10.2. Docstrings
      • 10.3. Type Hints
      • 10.4. Summary
    • 11. ORGANIZING YOUR CODE
      • 11.1. Git Commits and Repos
      • 11.2. Using Cookiecutter to Create New Python Projects
      • 11.3. Installing Git
      • 11.4. The Git Workflow
      • 11.5. Creating a Git Repo on Your Computer
      • 11.6. Viewing the Commit Log
      • 11.7. Recovering Old Changes
      • 11.8. GitHub and the git push Command
      • 11.9. Summary
    • 12. ME ASURING PERFORMANCE AND
      • 12.1. The timeit Module
      • 12.2. The cProfile Profiler
      • 12.3. Big O Algorithm Analysis
      • 12.4. Big O Orders
      • 12.5. Determining the Big O Order of Your Code
      • 12.6. Summary
    • 13. PRACTICE PROJECTS
      • 13.1. The Tower of Hanoi
      • 13.2. Four-in-a-Row
      • 13.3. Summary
    • 14. OBJECT-ORIENTED PROGR AMMING AND CLASSES
      • 14.1. Real-World Analogy: Filling Out a Form
      • 14.2. Creating Objects from Classes
      • 14.3. Creating a Simple Class: WizCoin
      • 14.4. The type() Function and qualname Attribute
      • 14.5. Non-OOP vs. OOP Examples: Tic-Tac-Toe
      • 14.6. Designing Classes for the Real World Is Hard
      • 14.7. Summary
    • 15. OBJECT-ORIENTED PROGRAMMING AND INHERITANCE
      • 15.1. How Inheritance Works
      • 15.2. The isinstance() and issubclass() Functions
      • 15.3. Class Methods
      • 15.4. Class Attributes
      • 15.5. Static Methods
      • 15.6. When to Use Class and Static Object-Oriented Features
      • 15.7. Object-Oriented Buzzwords
      • 15.8. When Not to Use Inheritance
      • 15.9. Multiple Inheritance
      • 15.10. Method Resolution Order
      • 15.11. Summary
    • 16. PYTHONIC OOP: PROPERTIES AND DUNDER METHODS
      • 16.1. Properties
      • 16.2. Python’s Dunder Methods
      • 16.3. Summary
  • Workig with Excel file
    • 1. Chapter One
      • 1.1. Excel Files
      • 1.2. Introspecting an Excel Workbook
    • 2. Processing a Single Worksheet
      • 2.1. Read and Write an Excel File
      • 2.2. Filter for Specific Rows
    • 3. Access All the Worksheets
      • 3.1. Filter for Specific Rows Across All Worksheets
      • 3.2. Select Specific Columns Across All Worksheets
    • 4. Reading a Set of Worksheets in an Excel Workbook
      • 4.1. Filter for Specific Rows Across a Set of Worksheets BASE PYTHON
    • 5. Processing Multiple Workbooks
      • 5.1. Count Number of Workbooks and Rows and
      • 5.2. Concatenate Data from Multiple Workbooks
      • 5.3. Sum and Average Values per Workbook andWorksheet
  • OpenCV-Python3 no GUI Tutorials
    • 1. Introduction to OpenCV
      • 1.1. Introduction to OpenCV-Python Tutorials
      • 1.2. Install OpenCV-Python in Windows
      • 1.3. Install OpenCV-Python in Fedora
      • 1.4. Install OpenCV-Python in Ubuntu
      • 1.5. Using pip
      • 1.6. Getting Started with Images
      • 1.7. OpenCV-Python Bindings
    • 2. Core Operations
      • 2.1. Basic Operations on Images
      • 2.2. Arithmetic Operations on Images
      • 2.3. Performance Measurement and Improvement Techniques
      • 2.4. Mathematical Tools in OpenCV
    • 3. Image Processing in OpenCV
      • 3.1. Changing Colorspaces
      • 3.2. Image Thresholding
      • 3.3. Geometric Transformations of Images
      • 3.4. Smoothing Images
      • 3.5. Morphological Transformations
      • 3.6. Image Gradients
      • 3.7. Canny Edge Detection
      • 3.8. Image Pyramids
    • 4. Contours in OpenCV
      • 4.1. Contours : Getting Started
      • 4.2. Contour Features
      • 4.3. Contour Properties
      • 4.4. Contours : More Functions
      • 4.5. Contours Hierarchy
    • 5. Histograms in OpenCV
      • 5.1. Histograms - 1 : Find, Plot, Analyze !!!
      • 5.2. Histograms - 2: Histogram Equalization
      • 5.3. Histograms - 3 : 2D Histograms
      • 5.4. Histogram - 4 : Histogram Backprojection
    • 6. Image Transforms in OpenCV
      • 6.1. Fourier Transform
    • 7. Advanced Image Processing in OpenCV
      • 7.1. Template Matching
      • 7.2. Hough Line Transform
      • 7.3. Hough Circle Transform
      • 7.4. Image Segmentation with Watershed Algorithm
      • 7.5. Interactive Foreground Extraction using GrabCut Algorithm
    • 8. Feature Detection and Description
      • 8.1. Understanding Features
      • 8.2. Harris Corner Detection
      • 8.3. Shi-Tomasi Corner Detector & Good Features to Track
      • 8.4. FAST Algorithm for Corner Detection
      • 8.5. ORB (Oriented FAST and Rotated BRIEF)
      • 8.6. Feature Matching
    • 9. Camera Calibration and 3D Reconstruction
      • 9.1. Camera Calibration
      • 9.2. Pose Estimation
      • 9.3. Depth Map from Stereo Images
    • 10. Machine Learning
      • 10.1. Understanding k-Nearest Neighbour
      • 10.2. OCR of Hand-written Data using kNN
      • 10.3. Understanding SVM
      • 10.4. OCR of Hand-written Data using SVM
      • 10.5. Understanding K-Means Clustering
      • 10.6. K-Means Clustering in OpenCV
    • 11. Computational Photography
      • 11.1. Image Denoising
      • 11.2. Image Inpainting
      • 11.3. High Dynamic Range (HDR)
    • 12. Object Detection
      • 12.1. Face Detection using Haar Cascades
Theme by the Executable Book Project

7. Concurrency-and-Parallelism¶

  • 7.1. Use subprocess to Manage Child Processes
    • 7.1.1. Note
    • 7.1.2. Things to Remember
  • 7.2. Use Threads for Blocking I/O, Avoid for Parallelism
    • 7.2.1. Things to Remember
  • 7.3. Use Lock to Prevent Data Races in Threads
    • 7.3.1. Things to Remember
  • 7.4. Use Queue to Coordinate Work Between Threads
    • 7.4.1. Queue to the Rescue
  • 7.5. Know How to Recognize When Concurrency Is Necessary
    • 7.5.1. Things to Remember
  • 7.6. Avoid Creating New Thread Instances for On-demand Fan-out
    • 7.6.1. Things to Remember
  • 7.7. Understand How Using Queue for Concurrency Requires Refactoring
    • 7.7.1. Things to Remember
  • 7.8. Consider ThreadPoolExecutor When Threads Are Necessary for Concurrency
    • 7.8.1. Things to Remember
  • 7.9. Achieve Highly Concurrent I/O with Coroutines
    • 7.9.1. Things to Remember
  • 7.10. Know How to Port Threaded I/O to asyncio
    • 7.10.1. Things to Remember
  • 7.11. Mix Threads and Coroutines to Ease the Transition to asyncio
    • 7.11.1. Things to Remember
  • 7.12. Avoid Blocking the asyncio Event Loop to Maximize Responsiveness
    • 7.12.1. Things to Remember
  • 7.13. Consider concurrent.futures for True Parallelism
    • 7.13.1. Things to Remember

上一页

6.8. Prefer Class Decorators Over Metaclasses for Composable Class Extensions

下一页

7.1. Use subprocess to Manage Child Processes

By Bu Kun
© Copyright From 2020. Build on 2024-1-15. by BU Kun @ OSGeo China Chapter.