Welcome to Python cheatsheet!¶
- Python basic cheatsheet
- Python Naming Rule
- Using
__future__backport features - Check object attributes
- Define a function
__doc__ - Check instance type
- Check, Get, Set attribute
- Check inheritance
- Check all global variables
- Check callable
- Get function/class name
__new__&__init__- The diamond problem
- Representations of your class behave
- Break up a long string
- Get list item SMART
- Get dictionary item SMART
- Set a list/dict SMART
setoperations- NamedTuple
__iter__- Delegating Iteration- Using Generator as Iterator
- Emulating a list
- Emulating a dictionary
- Decorator
- Decorator with arguments
- for: exp else: exp
- try: exp else: exp
- Lambda function
- Option arguments - (*args, **kwargs)
type()declare (create) aclass- Callable object
- Context Manager -
withstatement - Using
@contextmanager - Using
withstatement open file - Reading file chunk
- Property - Managed attributes
- Computed attributes - Using property
- Descriptor - manage attributes
@staticmethod,@classmethod- Abstract method - Metaclass
- Common Use Magic
- Parsing csv string
- Using
__slots__to save memory - Using annotation for type hints
- Using annotation to check type
- New in Python3 cheatsheet
printis a function- String is unicode
- New Super
- Remove
<> - Not allow
from module import *inside function - Add
nonlocalkeyword - Extended iterable unpacking
- General unpacking
- Function annotations
- Variable annotations
- Core support for typing module and generic types
- Format byte string
- fstring
- Suppressing exception
- Generator delegation
asyncandawaitsyntax- Asynchronous generators
- Asynchronous comprehensions
- Matrix multiplication
- Data Classes
- Built-in
breakpoint()
- Python unicode cheatsheet
- Python generator cheatsheet
- Glossary of Generator
- Produce value via generator
- Unpacking Generators
- Implement Iterable object via generator
- Send message to generator
yield fromexpression- yield (from) EXPR return RES
- Generate sequences
- What
RES = yield from EXPactually do? for _ in gen()simulateyield from- Check generator type
- Check Generator State
- Simple compiler
- Context manager and generator
- What
@contextmanageractually doing? - profile code block
yield fromand__iter__yield from == awaitexpression- Closure in Python - using generator
- Implement a simple scheduler
- Simple round-robin with blocking
- simple round-robin with blocking and non-blocking
- Asynchronous Generators
- Asynchronous generators can have
try..finallyblocks - send value and throw exception into async generator
- Simple async round-robin
- Async generator get better performance than async iterator
- Asynchronous Comprehensions
- Python Regular Expression cheatsheet
- Python socket cheatsheet
- Get Hostname
- Transform Host & Network Endian
- IP dotted-quad string & byte format convert
- Mac address & byte format convert
- Simple TCP Echo Server
- Simple TCP Echo Server through IPv6
- Disable IPv6 Only
- Simple TCP Echo Server Via SocketServer
- Simple TLS/SSL TCP Echo Server
- Set ciphers on TLS/SSL TCP Echo Server
- Simple UDP Echo Server
- Simple UDP Echo Server Via SocketServer
- Simple UDP client - Sender
- Broadcast UDP Packets
- Simple UNIX Domain Socket
- Simple duplex processes communication
- Simple Asynchronous TCP Server - Thread
- Simple Asynchronous TCP Server - select
- Simple Asynchronous TCP Server - poll
- Simple Asynchronous TCP Server - epoll
- Simple Asynchronous TCP Server - kqueue
- High-Level API - selectors
- Simple Non-blocking TLS/SSL socket via selectors
- "socketpair" - Similar to PIPE
- Using sendfile do copy
- Sending a file through sendfile
- Linux kernel Crypto API - AF_ALG
- AES-CBC encrypt/decrypt via AF_ALG
- AES-GCM encrypt/decrypt via AF_ALG
- AES-GCM encrypt/decrypt file with sendfile
- Compare the performance of AF_ALG to cryptography
- Sniffer IP packets
- Sniffer TCP packet
- Sniffer ARP packet
- Python cryptography cheatsheet
- Simple https server
- Check certificate information
- Generate a self-signed certificate
- Prepare a Certificate Signing Request (csr)
- Generate RSA keyfile without passphrase
- Sign a file by a given private key
- Verify a file from a signed digest
- Simple RSA encrypt via pem file
- Simple RSA encrypt via RSA module
- Simple RSA decrypt via pem file
- Simple RSA encrypt with OAEP
- Simple RSA decrypt with OAEP
- Using DSA to proof of identity
- Using AES CBC mode encrypt a file
- Using AES CBC mode decrypt a file
- AES CBC mode encrypt via password (using cryptography)
- AES CBC mode decrypt via password (using cryptography)
- AES CBC mode encrypt via password (using pycrypto)
- AES CBC mode decrypt via password (using pycrytpo)
- Ephemeral Diffie Hellman Key Exchange via cryptography
- Calculate DH shared key manually via cryptography
- Calculate DH shared key from (p, g, pubkey)
- Python Concurrency Cheatsheet
- Execute a shell command
- Create a thread via "threading"
- Performance Problem - GIL
- Consumer and Producer
- Thread Pool Template
- Using multiprocessing ThreadPool
- Mutex lock
- Deadlock
- Implement "Monitor"
- Control primitive resources
- Ensure tasks has done
- Thread-safe priority queue
- Multiprocessing
- Custom multiprocessing map
- Graceful way to kill all child processes
- Simple round-robin scheduler
- Scheduler with blocking function
- PoolExecutor
- What "with ThreadPoolExecutor" doing?
- Future Object
- Future error handling
- Python SQLAlchemy Cheatsheet
- Set a database URL
- Sqlalchemy Support DBAPI - PEP249
- Transaction and Connect Object
- Metadata - Generating Database Schema
- Inspect - Get Database Information
- Reflection - Loading Table from Existing Database
- Get Table from MetaData
- Create all Tables Store in "MetaData"
- Create Specific Table
- Create table with same columns
- Drop a Table
- Some Table Object Operation
- SQL Expression Language
- insert() - Create an "INSERT" Statement
- select() - Create a "SELECT" Statement
- join() - Joined Two Tables via "JOIN" Statement
- Delete Rows from Table
- Check Table Existing
- Create multiple tables at once
- Create tables with dynamic columns (Table)
- Object Relational add data
- Object Relational update data
- Object Relational delete row
- Object Relational relationship
- Object Relational self association
- Object Relational basic query
- mapper: Map
Tabletoclass - Get table dynamically
- Object Relational join two tables
- join on relationship and group_by count
- Create tables with dynamic columns (ORM)
- Close database connection
- Cannot use the object after close the session
- Python asyncio cheatsheet
- What is @asyncio.coroutine?
- What is a Task?
- What event loop doing? (Without polling)
- What
asyncio.waitdoing? - Future like object
- Future like object
__await__other task - Patch loop runner
_run_once - Put blocking task into Executor
- Socket with asyncio
- Event Loop with polling
- Transport and Protocol
- Transport and Protocol with SSL
- What
loop.create_serverdo? - Inline callback
- Asynchronous Iterator
- What is asynchronous iterator
- Asynchronous context manager
- What is asynchronous context manager
- decorator
@asynccontextmanager - What loop.sock_* do?
- Simple asyncio connection pool
- Simple asyncio UDP echo server
- Simple asyncio web server
- Simple HTTPS asyncio web server
- Simple asyncio WSGI web server
- Python test cheatsheet
- A simple Python unittest
- Python unittest setup & teardown hierarchy
- Different module of setUp & tearDown hierarchy
- Run tests via unittest.TextTestRunner
- Test raise exception
- Pass arguments into a TestCase
- Group multiple testcases into a suite
- Group multiple tests from different TestCase
- Skip some tests in the TestCase
- Monolithic Test
- Cross-module variables to Test files
- skip setup & teardown when the test is skipped
- Re-using old test code
- Testing your document is right
- Re-using doctest to unittest
- Customize test report
- Mock - using
@patchsubstitute original method - What
with unittest.mock.patchdo? - Mock - substitute
open
- Python C API cheatsheet
- Python Design Pattern in C