Skip to content

Instantly share code, notes, and snippets.

@leoricklin
Last active July 13, 2022 08:02
Show Gist options
  • Save leoricklin/af0ac7a6cc44e4b5786bd05c71963e13 to your computer and use it in GitHub Desktop.
Save leoricklin/af0ac7a6cc44e4b5786bd05c71963e13 to your computer and use it in GitHub Desktop.

TODO

Python Resources

Python進階技巧

name decorator required input param access scope
static method @staticmethod None can't access class static members
class method @classmethod cls can access class static members
instance method None self can access class static members and instance members
  • code snippet
class Shiba:

    pee_length = 10 # class static members

    def __init__(self, height, weight):
        self.height = height
        self.weight = weight

    @staticmethod
    def peeSta(length):
        print("pee" + "." * length)
    #

    @staticmethod
    def peeStaErr():
        print("pee" + "." * pee_length) # it will report "name 'pee_length' is not defined" error
    #

    @classmethod
    def peeCls(cls):
        print("pee" + "." * cls.pee_length)
    #
    def peeIns(self):
        print("pee" + "." * (Shiba.pee_length + self.height + self.weight) )

#
Shiba.peeSta(3)
# Result > pee...

Shiba.peeSta(20)
# Result > pee....................

Shiba.peeCls()
#result: pee..........

black_shiba = Shiba(90, 40)
black_shiba.peeSta(10)
# Result > pee...........

black_shiba.peeCls()
#result: pee..........

black_shiba.peeIns()
#result: pee........................................


action_if_true if condition else action_if_false
eval(expression[, globals[, locals]])

>>> eval("sum([8, 16, 32])")
56

Hcker Earth

Python Env

Pyenv

# Put the content into ~/.bashrc or ~/.bash_profile for Bash,
# .zshrc for ZSH

# you may need to add dir of command `pyenv` into PATH,
# if command pyenv is not available yet

if command -v pyenv &>/dev/null; then
    eval "$(pyenv init -)"
fi
if command -v pyenv-virtualenv &>/dev/null; then
    eval "$(pyenv virtualenv-init -)"
fi
  • Expose command conda but don't activate any environment, even the base environment. Execute the following commands in your shell.
# Run the content in the shell

# init conda, the following command write scripts into your shell init file automatically
conda init

# disable init of env "base"
conda config --set auto_activate_base false
  • Note: After this setup, the default python is the one set by pyenv global. Use pyenv and conda to manage environments separately.

  • Examples of managing virtual environments.

# virtual environments from pyenv
pyenv install 3.6.9
pyenv virtualenv 3.6.9 new-env
pyenv activate new-env
pyenv deactive
# You can also use `pyenv local`


# virtual environments from conda
conda env create new-env python=3.6
conda env list
conda activate new-env
conda deactivate
  • Default env location for pyenv is ~/.pyenv/versions.
  • Default env location for conda, check output from conda info.

Conda

Guide

Python.org

  • multiple line comment
'''
comments
'''
1. Introduction
1.1. Alternate Implementations
1.2. Notation
2. Lexical analysis
2.1. Line structure
2.2. Other tokens
2.3. Identifiers and keywords
2.4. Literals
2.5. Operators
2.6. Delimiters
3. Data model
3.1. Objects, values and types
3.2. The standard type hierarchy
3.3. Special method names
3.4. Coroutines
4. Execution model
4.1. Structure of a program
4.2. Naming and binding
4.3. Exceptions
5. The import system
5.1. importlib
5.2. Packages
5.3. Searching
5.4. Loading
5.5. The Path Based Finder
5.6. Replacing the standard import system
5.7. Package Relative Imports
5.8. Special considerations for __main__
5.9. Open issues
5.10. References
6. Expressions
6.1. Arithmetic conversions
6.2. Atoms
6.3. Primaries
6.4. Await expression
6.5. The power operator
6.6. Unary arithmetic and bitwise operations
6.7. Binary arithmetic operations
6.8. Shifting operations
6.9. Binary bitwise operations
6.10. Comparisons
6.11. Boolean operations
6.12. Assignment expressions
6.13. Conditional expressions
6.14. Lambdas
6.15. Expression lists
6.16. Evaluation order
6.17. Operator precedence
7. Simple statements
7.1. Expression statements
7.2. Assignment statements
7.3. The assert statement
7.4. The pass statement
7.5. The del statement
7.6. The return statement
7.7. The yield statement
7.8. The raise statement
7.9. The break statement
7.10. The continue statement
7.11. The import statement
7.12. The global statement
7.13. The nonlocal statement
8. Compound statements
8.1. The if statement
8.2. The while statement
8.3. The for statement
8.4. The try statement
8.5. The with statement
8.6. Function definitions
8.7. Class definitions
8.8. Coroutines
9. Top-level components
9.1. Complete Python programs
9.2. File input
9.3. Interactive input
9.4. Expression input
10. Full Grammar specification

Guide

Built-in Types
Truth Value Testing
Boolean Operations — and, or, not
Comparisons
Numeric Types — int, float, complex
Iterator Types
Sequence Types — list, tuple, range
Text Sequence Type — str
Binary Sequence Types — bytes, bytearray, memoryview
Set Types — set, frozenset
Mapping Types — dict
Context Manager Types
Generic Alias Type
Other Built-in Types
Special Attributes
Built-in Exceptions
Base classes
Concrete exceptions
Warnings
Exception hierarchy
Text Processing Services
string — Common string operations
re — Regular expression operations
difflib — Helpers for computing deltas
textwrap — Text wrapping and filling
unicodedata — Unicode Database
stringprep — Internet String Preparation
readline — GNU readline interface
rlcompleter — Completion function for GNU readline
Binary Data Services
struct — Interpret bytes as packed binary data
codecs — Codec registry and base classes
Data Types
datetime — Basic date and time types
zoneinfo — IANA time zone support
calendar — General calendar-related functions
collections — Container datatypes
collections.abc — Abstract Base Classes for Containers
heapq — Heap queue algorithm
bisect — Array bisection algorithm
array — Efficient arrays of numeric values
weakref — Weak references
types — Dynamic type creation and names for built-in types
copy — Shallow and deep copy operations
pprint — Data pretty printer
reprlib — Alternate repr() implementation
enum — Support for enumerations
graphlib — Functionality to operate with graph-like structures
Numeric and Mathematical Modules
numbers — Numeric abstract base classes
math — Mathematical functions
cmath — Mathematical functions for complex numbers
decimal — Decimal fixed point and floating point arithmetic
fractions — Rational numbers
random — Generate pseudo-random numbers
statistics — Mathematical statistics functions
Functional Programming Modules
itertools — Functions creating iterators for efficient looping
functools — Higher-order functions and operations on callable objects
operator — Standard operators as functions
File and Directory Access
pathlib — Object-oriented filesystem paths
os.path — Common pathname manipulations
fileinput — Iterate over lines from multiple input streams
stat — Interpreting stat() results
filecmp — File and Directory Comparisons
tempfile — Generate temporary files and directories
glob — Unix style pathname pattern expansion
fnmatch — Unix filename pattern matching
linecache — Random access to text lines
shutil — High-level file operations
Data Persistence
pickle — Python object serialization
copyreg — Register pickle support functions
shelve — Python object persistence
marshal — Internal Python object serialization
dbm — Interfaces to Unix “databases”
sqlite3 — DB-API 2.0 interface for SQLite databases
Data Compression and Archiving
zlib — Compression compatible with gzip
gzip — Support for gzip files
bz2 — Support for bzip2 compression
lzma — Compression using the LZMA algorithm
zipfile — Work with ZIP archives
tarfile — Read and write tar archive files
File Formats
csv — CSV File Reading and Writing
configparser — Configuration file parser
netrc — netrc file processing
xdrlib — Encode and decode XDR data
plistlib — Generate and parse Apple .plist files
Cryptographic Services
hashlib — Secure hashes and message digests
hmac — Keyed-Hashing for Message Authentication
secrets — Generate secure random numbers for managing secrets
Generic Operating System Services
os — Miscellaneous operating system interfaces
io — Core tools for working with streams
time — Time access and conversions
argparse — Parser for command-line options, arguments and sub-commands
getopt — C-style parser for command line options
logging — Logging facility for Python
logging.config — Logging configuration
logging.handlers — Logging handlers
getpass — Portable password input
curses — Terminal handling for character-cell displays
curses.textpad — Text input widget for curses programs
curses.ascii — Utilities for ASCII characters
curses.panel — A panel stack extension for curses
platform — Access to underlying platform’s identifying data
errno — Standard errno system symbols
ctypes — A foreign function library for Python
Concurrent Execution
threading — Thread-based parallelism
multiprocessing — Process-based parallelism
multiprocessing.shared_memory — Provides shared memory for direct access across processes
The concurrent package
concurrent.futures — Launching parallel tasks
subprocess — Subprocess management
sched — Event scheduler
queue — A synchronized queue class
contextvars — Context Variables
_thread — Low-level threading API
process = subprocess.Popen(['pip', 'list'],
                     stdout=subprocess.PIPE, 
                     stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
print(stdout.decode('utf-8'))
print(stderr.decode('utf-8'))
Networking and Interprocess Communication
asyncio — Asynchronous I/O
socket — Low-level networking interface
ssl — TLS/SSL wrapper for socket objects
select — Waiting for I/O completion
selectors — High-level I/O multiplexing
asyncore — Asynchronous socket handler
asynchat — Asynchronous socket command/response handler
signal — Set handlers for asynchronous events
mmap — Memory-mapped file support
Internet Data Handling
email — An email and MIME handling package
json — JSON encoder and decoder
mailcap — Mailcap file handling
mailbox — Manipulate mailboxes in various formats
mimetypes — Map filenames to MIME types
base64 — Base16, Base32, Base64, Base85 Data Encodings
binhex — Encode and decode binhex4 files
binascii — Convert between binary and ASCII
quopri — Encode and decode MIME quoted-printable data
uu — Encode and decode uuencode files
Structured Markup Processing Tools
html — HyperText Markup Language support
html.parser — Simple HTML and XHTML parser
html.entities — Definitions of HTML general entities
XML Processing Modules
xml.etree.ElementTree — The ElementTree XML API
xml.dom — The Document Object Model API
xml.dom.minidom — Minimal DOM implementation
xml.dom.pulldom — Support for building partial DOM trees
xml.sax — Support for SAX2 parsers
xml.sax.handler — Base classes for SAX handlers
xml.sax.saxutils — SAX Utilities
xml.sax.xmlreader — Interface for XML parsers
xml.parsers.expat — Fast XML parsing using Expat
Internet Protocols and Support
webbrowser — Convenient Web-browser controller
cgi — Common Gateway Interface support
cgitb — Traceback manager for CGI scripts
wsgiref — WSGI Utilities and Reference Implementation
urllib — URL handling modules
urllib.request — Extensible library for opening URLs
urllib.response — Response classes used by urllib
urllib.parse — Parse URLs into components
urllib.error — Exception classes raised by urllib.request
urllib.robotparser — Parser for robots.txt
http — HTTP modules
http.client — HTTP protocol client
ftplib — FTP protocol client
poplib — POP3 protocol client
imaplib — IMAP4 protocol client
nntplib — NNTP protocol client
smtplib — SMTP protocol client
smtpd — SMTP Server
telnetlib — Telnet client
uuid — UUID objects according to RFC 4122
socketserver — A framework for network servers
http.server — HTTP servers
http.cookies — HTTP state management
http.cookiejar — Cookie handling for HTTP clients
xmlrpc — XMLRPC server and client modules
xmlrpc.client — XML-RPC client access
xmlrpc.server — Basic XML-RPC servers
ipaddress — IPv4/IPv6 manipulation library
Multimedia Services
audioop — Manipulate raw audio data
aifc — Read and write AIFF and AIFC files
sunau — Read and write Sun AU files
wave — Read and write WAV files
chunk — Read IFF chunked data
colorsys — Conversions between color systems
imghdr — Determine the type of an image
sndhdr — Determine type of sound file
ossaudiodev — Access to OSS-compatible audio devices
Internationalization
gettext — Multilingual internationalization services
locale — Internationalization services
Program Frameworks
turtle — Turtle graphics
cmd — Support for line-oriented command interpreters
shlex — Simple lexical analysis
Graphical User Interfaces with Tk
tkinter — Python interface to Tcl/Tk
tkinter.colorchooser — Color choosing dialog
tkinter.font — Tkinter font wrapper
Tkinter Dialogs
tkinter.messagebox — Tkinter message prompts
tkinter.scrolledtext — Scrolled Text Widget
tkinter.dnd — Drag and drop support
tkinter.ttk — Tk themed widgets
tkinter.tix — Extension widgets for Tk
IDLE
Other Graphical User Interface Packages
Development Tools
typing — Support for type hints
pydoc — Documentation generator and online help system
Python Development Mode
Effects of the Python Development Mode
ResourceWarning Example
Bad file descriptor error example
doctest — Test interactive Python examples
unittest — Unit testing framework
unittest.mock — mock object library
unittest.mock — getting started
2to3 - Automated Python 2 to 3 code translation
test — Regression tests package for Python
test.support — Utilities for the Python test suite
test.support.socket_helper — Utilities for socket tests
test.support.script_helper — Utilities for the Python execution tests
test.support.bytecode_helper — Support tools for testing correct bytecode generation
Debugging and Profiling
Audit events table
bdb — Debugger framework
faulthandler — Dump the Python traceback
pdb — The Python Debugger
The Python Profilers
timeit — Measure execution time of small code snippets
trace — Trace or track Python statement execution
tracemalloc — Trace memory allocations
Software Packaging and Distribution
distutils — Building and installing Python modules
ensurepip — Bootstrapping the pip installer
venv — Creation of virtual environments
zipapp — Manage executable Python zip archives
Python Runtime Services
sys — System-specific parameters and functions
sysconfig — Provide access to Python’s configuration information
builtins — Built-in objects
__main__ — Top-level script environment
warnings — Warning control
dataclasses — Data Classes
contextlib — Utilities for with-statement contexts
abc — Abstract Base Classes
atexit — Exit handlers
traceback — Print or retrieve a stack traceback
__future__ — Future statement definitions
gc — Garbage Collector interface
inspect — Inspect live objects
site — Site-specific configuration hook
Custom Python Interpreters
code — Interpreter base classes
codeop — Compile Python code
Importing Modules
zipimport — Import modules from Zip archives
pkgutil — Package extension utility
modulefinder — Find modules used by a script
runpy — Locating and executing Python modules
importlib — The implementation of import
Using importlib.metadata
Python Language Services
parser — Access Python parse trees
ast — Abstract Syntax Trees
symtable — Access to the compiler’s symbol tables
symbol — Constants used with Python parse trees
token — Constants used with Python parse trees
keyword — Testing for Python keywords
tokenize — Tokenizer for Python source
tabnanny — Detection of ambiguous indentation
pyclbr — Python module browser support
py_compile — Compile Python source files
compileall — Byte-compile Python libraries
dis — Disassembler for Python bytecode
pickletools — Tools for pickle developers
Miscellaneous Services
formatter — Generic output formatting
MS Windows Specific Services
msilib — Read and write Microsoft Installer files
msvcrt — Useful routines from the MS VC++ runtime
winreg — Windows registry access
winsound — Sound-playing interface for Windows
Unix Specific Services
posix — The most common POSIX system calls
pwd — The password database
spwd — The shadow password database
grp — The group database
crypt — Function to check Unix passwords
termios — POSIX style tty control
tty — Terminal control functions
pty — Pseudo-terminal utilities
fcntl — The fcntl and ioctl system calls
pipes — Interface to shell pipelines
resource — Resource usage information
nis — Interface to Sun’s NIS (Yellow Pages)
syslog — Unix syslog library routines
Superseded Modules
optparse — Parser for command line options
imp — Access the import internals

Packages

Code analysis

Pyan takes one or more Python source files, performs a (rather superficial) static analysis, and constructs a directed graph of the objects in the combined source, and how they define or use each other. The graph can be output for rendering by GraphViz or yEd.

DB Connection

UUID

import uuid
print(str(uuid.uuid1()).upper()) 

Matplotlib

Cache

# speed up calculating Fibonacci numbers with dynamic programming
@cached(cache={})
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

# cache least recently used Python Enhancement Proposals
# maxsize -> num of items in the dics
@cached(cache=LRUCache(maxsize=32))
def get_pep(num):
    url = 'http://www.python.org/dev/peps/pep-%04d/' % num
    with urllib.request.urlopen(url) as s:
        return s.read()

# cache weather data for no longer than ten minutes
# ttl -> seconds
@cached(cache=TTLCache(maxsize=1024, ttl=600))
def get_weather(place):
    return owm.weather_at_place(place).get_weather()

the cache’s size equal to the number of its items

REST API

Tutorials

3.1. Using Python as a Calculator
3.1.1. Numbers
3.1.2. Strings
3.1.3. Lists
5.1. More on Lists
5.2. The del statement
5.3. Tuples and Sequences
5.4. Sets
5.5. Dictionaries
5.6. Looping Techniques
5.7. More on Conditions
5.8. Comparing Sequences and Other Types
7.1. Fancier Output Formatting
7.1.1. Formatted String Literals
7.1.2. The String format() Method
7.1.3. Manual String Formatting
7.1.4. Old string formatting
7.2. Reading and Writing Files
7.2.1. Methods of File Objects
7.2.2. Saving structured data with json
hive_sql_template = """SELECT DISTINCT
from_unixtime(unix_timestamp(calc_time_stamp)) as calc_time_stamp,
 mt_fd_ocap_hist_id, collection_name, context_group,
 tool_name, run_id, analysis, input, statistic, ocap_type, ocap_detail,
 ocap_cat, year, month, day, hour
FROM
    prod_mti_{region}_fab_{fab}_fd.idl_mt_fd_ocap_hist
WHERE
 (year,month,day) in ({partition})
 AND ocap_detail RLIKE '{lotlist}'"""

hive_sql = hive_sql_template.format(fab=fab,region=region,lotlist=lotlist,partition=partition)
print(f"Downloaded the tutorial in {toc - tic:0.4f} seconds")
  • 10.1. Operating System Interface
10.2. File Wildcards
10.3. Command Line Arguments
10.4. Error Output Redirection and Program Termination
10.5. String Pattern Matching
10.6. Mathematics
10.7. Internet Access
10.8. Dates and Times
10.9. Data Compression
import time
tic = time.perf_counter()
tutorial = feed.get_article(0)
toc = time.perf_counter()
print(f"Downloaded the tutorial in {toc - tic:0.4f} seconds")
from timer import Timer

"""Print the latest tutorial from Real Python"""
t = Timer()
t.start()
tutorial = feed.get_article(0)
t.stop()
# printing the elapsed time to the console
# Returns a series whose index is the original column names
# and whose values is the memory usage of each column in bytes.

df.memory_usage(deep=True)
10.11. Quality Control
10.12. Batteries Included
11. Brief Tour of the Standard Library — Part II
11.1. Output Formatting
11.2. Templating
11.3. Working with Binary Data Record Layouts
11.4. Multi-threading
11.5. Logging
11.6. Weak References
11.7. Tools for Working with Lists
11.8. Decimal Floating Point Arithmetic
12. Virtual Environments and Packages
12.1. Introduction
12.2. Creating Virtual Environments
12.3. Managing Packages with pip
13. What Now?
14. Interactive Input Editing and History Substitution
14.1. Tab Completion and History Editing
14.2. Alternatives to the Interactive Interpreter
15. Floating Point Arithmetic: Issues and Limitations
15.1. Representation Error
16. Appendix
16.1. Interactive Mode
16.1.1. Error Handling
16.1.2. Executable Python Scripts
16.1.3. The Interactive Startup File
16.1.4. The Customization Modules
History and Philosophy of Python
The Interpreter, an Interactive Shell
Execute a Script
Structuring with Indentation
Data Types and Variables
Operators
Sequential Data Types
List Manipulation
Shallow and Deep Copy
Dictionaries
Sets and Frozen Sets
Sets Examples
Keyboard Input
Conditional Statements
While Loops
For Loops
Output with Print
  • The Pythonic Way: The string method "format"

    The following diagram with an example usage depicts how the string method "format" works works for positional parameters:

Working with Dictionaries and while Loops
Functions
Passing Arguments
Namespaces
Global vs. Local Variables and Namespaces
File Management
Modular Programming and Modules
Packages
Errors and Exception Handling
Overview
Python 3 - Environment Setup
Python 3 - Basic Syntax
Python 3 - Variable Types
Python 3 - Basic Operators
Python 3 - Decision Making
Python 3 - Loops
Python 3 - Numbers
Python 3 - Strings
Python 3 - Lists
Python 3 - Tuples
Python 3 - Dictionary
Python 3 - Date & Time
Python 3 - Functions
Python 3 - Modules
Python 3 - Files I/O
Python 3 - Exceptions
Python 3 - Classes/Objects
Python 3 - Reg Expressions
Python 3 - CGI Programming
Python 3 - Database Access
Python 3 - Networking
Python 3 - Sending Email
Python 3 - Multithreading
Python 3 - XML Processing
Python 3 - GUI Programming
Python 3 - Further Extensions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment