This is used for CLIs.
- Import modules
import typer
from typing import AnnotatedNote: Annotated is optional
- Create app
app = typer.Typer(add_completion=False)- Append
@app.command()tomain()function
@app.command()
def main(
arg1: Annotated[int, typer.argument(help="Input arg1")],
arg2: Annotated[str, typer.argument(help="Input arg1")]
):Note: you can do this to multiple functions
- Define script entry point
if __name__ == "__main__":
app()- Using CLI If you have one entry function:
python <script.py> [arg1] [arg2]If you have multiple entry functions:
python <script.py> <function> [arg1] [arg2]This allows you to not have to define __init__, __str__, and __eq__ functions, and to treat methods as properties.
- Import module
import dataclasses from dataclass- Define class
@dataclass
class Dog:
name: str
age: int = 0Hence, to create a class you can do Max = Dog("Max", 2).
Define method
class C:
@property
def x(self):
return 42Make class isntance
c = C()Now instead of doing c.x(), you can simply treat it like a property and do
c.xInstead of doing
def getnthletter(word, number):Do
def getnthletter(
word: str,
number: int = 0
) -> int:import getpass
user = getpass.getuser()This is helpful for getting home directories. For example
home_dir = "C:/{getpass.getuser()}"from pathlib import Path
home_dir = Path("C:/user")You can then concatenate with strings to form other paths:
private_dir = home_dir / "private"You can also make new directories:
private_dir.mkdir(exist_ok=True)import subprocessTo run the following gcloud storage cp --recursive src dst, you can do
subprocess.run([
"gcloud",
"storage",
"cp",
"--recursive",
src,
f"{dst}"
])import shutil
shutil.copy("src.txt", "dst.txt")
shutil.rmtree(private_dir)These modules and patterns complement my list.
Use structured logging instead of print() for real-world scripts.
import logging
# Configure logging
logging.basicConfig(level=logging.INFO) # only shows if level >= 20
# Log messages
logging.debug("Debug message") # 10
logging.info("Info message") # 20
logging.warning("Warning message") # 30
logging.error("Error message") # 40
logging.critical("Critical message") # 50Use handlers
import logging
log = logging.getLogger("my_app")
log.setLevel(logging.INFO)
# Console output
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# File output
file_handler = logging.FileHandler("app.log")
file_handler.setLevel(logging.WARNING)
# Format
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)
log.addHandler(console_handler)
log.addHandler(file_handler)
log.info("This goes to console")
log.warning("This goes to console and file")import os
import sys
# Environment variables
api_key = os.getenv("API_KEY")
# Current working directory
cwd = os.getcwd()
# Exit process
sys.exit(1)
# Raw CLI args
args = sys.argvimport json
with open("config.json") as f:
cfg = json.load(f)
with open("out.json", "w") as f:
json.dump(cfg, f, indent=2)import subprocess
import shlex
cmd = "gcloud storage cp --recursive src dst"
subprocess.run(shlex.split(cmd))