Recipes for Python
. Hands-on code examples, snippets and guides for daily work.
- Flatten a nested sequence
- Unpacking Elements from Iterables
- Finding the Largest or Smallest N Items
- Implementing a Priority Queue
- Calculating with Dictionarie
- Finding Commonalities in Two Dictionaries
- Sorting Objects Without Native Comparison Support
- Grouping Records Together Based on a Field
- Tuples with Named Fields -
namedtuple
(命名元组) - Ordered Dictionary -
OrderedDict
- Data Classes -
dataclasses
(数据类)
- Time: Timestamp (UNIX Time), UTC, Local Time
- Time Zone
- Representation
- Function (Method) Decorator
- Function Decorator Without Argument
- Function Decorator With Required Arguments
- Function Decorator With Optional Arguments
- Class Decorator
with
Statement- Context Manager
- Multiple Context Managers
- Context Manager Protocol
- Single Use Context Manager
- Reentrant Context Manager
- Reusable Context Manager
- Type Hint
- Basic Types
- Union Types:
|
,,typing.Union
typing.Optional
- Constants and Class Attributes:
typing.Final
TypedDict
namedtuple
:typing.NamedTuple
itertools.chain
- socket
- Regex
- Literal:
typing.Literal
- Any:
typing.Any
andobject
- Type objects
- Callable objects
- Class Variables:
typing.ClassVar
- Restricting Inheritance and Overriding:
@typing.final
typing.NoReturn
- Inheritance of File Descriptor
- File Object (I/O)
- Text I/O
- Binary I/O
open()
Reference Implementation
socketserver
Class Diagram- TCP Server (IPv4)
- TCP Client (IPv4)
- I/O Multiplex (I/O多路复用) (Server)
- I/O Multiplex (I/O多路复用) (Client)
- Pack/Unpack Binary Data -
struct
- Coroutine (协程)
- Concurrent Coroutines (or Tasks) (并行协程)
- Timeout
- Waiting Primitives
- Queue (队列)
- Scheduled Tasks (调度任务)
- TCP Server (Low-Level APIs)
- TCP Client (Low-Level APIs)
argparse
(deprecated since Python 3.2)optparse
: C-Style Parsergetopt
pip
- Standard Package Managerpipx
- Install and Run Python Applicationspipenv
- Virtual Environment Manager
- Project:
pyproject.toml
black
isort
mypy
pylint
- URL Parsing:
urllib.parse
- HTTP Datetime Format
- HTTP Cookie (Server Side):
http.cookies
- ASGI / WSGI /
CGI - IDNA (Internationalized Domain Names in Applications, 国际化域名应用)
urllib.request
(builtin)aiohttp
: asynciorequests
: sync io, usingurllib3
FastAPI
: ASGI + type annotations + data model + OpenAPI(Swagger UI)Django
: full-featuredFlask
: tiny
http.server
(builtin)aiohttp
: asyncio, APIuvicorn
: ASGI, WebSocketsHypercorn
: HTTP/2, HTTP/3uWSGI
: WSGI, HTTP/2
- Official Driver
- ODM
redis-py
: Sync + Asyncaioredis
: Async (Obsoleted byredis-py
)pyton-redis-orm
: ORM
- Parallelism and Concurrent (并发)
- Multi-Threads Parallelism for I/O-bound tasks
- Multi-Processes Parallelism for CPU-bound tasks
- Multi-Processes - Queue (队列)
- Process Pool
- High-Level Threads-Based Concurrent
- High-Level Processes-Based Concurrent
- Synchronization Primitives -
Event
(For Processes and Threads) - Synchronization Primitives - Mutex Lock (互斥锁):
Lock
(For Processes and Threads) - Synchronization Primitives - Reentrant Lock (重入锁):
RLock
- Synchronization Primitives - Condition Variable (条件变量):
Condition
(For Processes and Threads) - Synchronization Primitives - Semaphore (信号量):
Semaphore
/BoundedSemaphore
(For Processes and Threads) - Synchronization Primitives - (栅栏):
Barrier
- Networks and Communications (网络通信)
- Asynchronous I/O (异步 I/O)
- Setup Python Project