Skip to content

Instantly share code, notes, and snippets.

View mpkocher's full-sized avatar

M. Kocher mpkocher

View GitHub Profile
@mpkocher
mpkocher / _colorize.py
Created December 14, 2025 02:11
Redesign/Approach to _colorize.py using Types
"""
https://github.com/python/cpython/blob/main/Lib/_colorize.py
## Redesign/Approach to _colorize.py
1. Improve ergonomics of applying a theme from f"{t.BLUE} text {t.reset}" to f"{t.BLUE(' text ')}".
2. Leverage Types to improve readability. Specifically, ANSIColor to be a specific type.
3. NoColor vs Color is similar to None | int from a type standpoint.
4. There's a bit going on with `ThemeSection(Mapping[str,str])`. Maybe this can be streamlined.
5. `ThemeSection.no_colors()` can be simplified by #3
@mpkocher
mpkocher / PydanticSecretModeling.md
Last active November 23, 2024 04:56
Notes and Comments on Modeling Secrets in Pydantic
@mpkocher
mpkocher / secrets.py
Created November 23, 2024 04:34
Modeling Secrets in Pydantic
"""
Pydantic Secrets
https://docs.pydantic.dev/latest/api/types/#pydantic.types.SecretStr
- Why does the "Box'ed" secret container understand (or leak) information of the secret?
- Internally, the "Box" shouldn't use `.get_secret_value()`. Otherwise, it's leaking info.
- Mixing up the Box and `.get_secret_value()` undermines the point of using `.get_secret_value()`
- Why is the hash value leaking? Comparing two secrets should require and explicit `s1.get_secret_value() == s2.get_secret_value()` call.
- Why does the repr/str communicate "non-empty" values? This is encouraging and enabling an anti-pattern.
@mpkocher
mpkocher / example.py
Created October 26, 2024 06:14
Pydantic Serialization of Complex Keys in Dicts/Maps
"""
https://github.com/pydantic/pydantic/issues/10724
Example of using complex keys in Dict using Pydantic for Serialization
There's a two different approaches
1. Fundamentally hook into the core serialization and map the structure into a list[{Key, Value}] (or similar) to get the Obj -> Json -> Obj working
2. Create an intermediate Object in Pydantic and add `to_X` method and `from_X` classmethod to get the Obj -> Obj2 -> Json -> Obj2 -> Obj
Method 2 is shown below
@mpkocher
mpkocher / example.py
Last active December 12, 2025 23:14
Pydantic Settings. Example of configure a yaml/json file at runtime.
"""
Different workarounds for setting a configuration file path at runtime.
https://github.com/pydantic/pydantic-settings/issues/259
There's an explicit step of validating the file path to make the errors more obvious.
Otherwise, a cryptic pydantic error will be raised.
"""
import argparse
@mpkocher
mpkocher / Makefile
Last active August 6, 2024 01:18
Gibson.com Scraper
.PHONY: compile run extract
default: compile ;
compile:
scala-cli compile gibson.scala
fmt:
scala-cli fmt .
run:
./gibson scraper -o .
@mpkocher
mpkocher / Declined.scala
Last active July 2, 2022 23:46
ZIO 1.x + Decline Example for creating CLI tools and running them with scala-cli.
//> using platform "jvm"
//> using scala "2.13.8"
//> using lib "dev.zio::zio:1.0.14"
//> using lib "com.monovore::decline:2.2.0"
//> using mainClass "DeclinedApp"
// Runnable using `scala-cli run Declined.scala -- --user Ralph --alpha 3.14`
import zio.{ExitCode, ZEnv, ZIO, Task, RIO, IO, UIO, URIO}
import zio.console._
@mpkocher
mpkocher / scrape_gibson.py
Last active March 18, 2021 07:04
Scrape Gibson.com
import sys
import json
import logging
from typing import List, Tuple, Dict, Any
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
log = logging.getLogger(__name__)
@mpkocher
mpkocher / pydantic-cli-sharing-subparse-options.py
Created October 29, 2020 22:54
Sharing Subparser Options in pydantic-cli
"""
Example of Using a Subparser
Demonstration of using two Pydantic data models to
build a subparser and sharing options.
The major friction point and limitation is the order in which the options appear in --help.
For example,
@mpkocher
mpkocher / amzlink.py
Last active September 21, 2021 12:13 — forked from pybites/amzlink.py