Skip to content

Latest commit

 

History

History
236 lines (187 loc) · 3.88 KB

File metadata and controls

236 lines (187 loc) · 3.88 KB

Python modules which have helped me

Typer (and Annotated)

This is used for CLIs.

  1. Import modules
import typer
from typing import Annotated

Note: Annotated is optional

  1. Create app
app = typer.Typer(add_completion=False)
  1. Append @app.command() to main() 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

  1. Define script entry point
if __name__ == "__main__":
  app()
  1. 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]

Dataclass

This allows you to not have to define __init__, __str__, and __eq__ functions, and to treat methods as properties.

@dataclass

  1. Import module
import dataclasses from dataclass
  1. Define class
@dataclass
class Dog:
  name: str 
  age: int = 0

Hence, to create a class you can do Max = Dog("Max", 2).

@property

Define method

class C:
  @property
  def x(self):
    return 42

Make class isntance

c = C()

Now instead of doing c.x(), you can simply treat it like a property and do

c.x

Simple function formatting

Instead of doing

def getnthletter(word, number):

Do

def getnthletter(
  word: str,
  number: int = 0
) -> int:

More systems stuff

Getting user

import getpass
user = getpass.getuser()

This is helpful for getting home directories. For example

home_dir = "C:/{getpass.getuser()}"

Path

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)

Subprocess

import subprocess

To run the following gcloud storage cp --recursive src dst, you can do

subprocess.run([
  "gcloud",
  "storage",
  "cp",
  "--recursive",
  src,
  f"{dst}"
])

Shutil

import shutil

shutil.copy("src.txt", "dst.txt")
shutil.rmtree(private_dir)

Additional Python Modules & Patterns

These modules and patterns complement my list.


Logging

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") # 50

Use 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")

OS & System

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.argv

JSON

import json

with open("config.json") as f:
    cfg = json.load(f)

with open("out.json", "w") as f:
    json.dump(cfg, f, indent=2)

Subprocess & Shlex

import subprocess
import shlex

cmd = "gcloud storage cp --recursive src dst"
subprocess.run(shlex.split(cmd))