Skip to content

Instantly share code, notes, and snippets.

View cppio's full-sized avatar

Parth Shastri cppio

View GitHub Profile
import plistlib, pprint, sys, uuid
with open(sys.argv[1], "rb") as plist:
plist = plistlib.load(plist)
assert plist["$archiver"] == "NSKeyedArchiver"
objs = plist["$objects"]
def decode(val):
if isinstance(val, dict):
from PIL import Image
def trim(im):
data = list(im.getdata())
cs = set()
w, h = im.size
for x in range(w):
cs.add(data[x])
cs.add(data[(h - 1) * w + x])
for y in range(h):
import hashlib, sys, zipfile
with zipfile.ZipFile(sys.argv[1]) as zip:
for filename in sorted(zip.namelist()):
if filename.endswith("/"):
continue
with zip.open(filename) as file:
print(hashlib.file_digest(file, sys.argv[2]).hexdigest(), filename)
import struct, sys, zlib
PNG_HEADER = b"\x89PNG\r\n\x1a\n"
for path in sys.argv[1:]:
with open(path, "rb") as png:
png = png.read()
print(path + ":")
if not png.startswith(PNG_HEADER):
print("not a png")
@cppio
cppio / varnames.py
Last active October 15, 2025 05:20
import dis, sys
def varnames():
frame = sys._getframe(1)
instrs = dis.get_instructions(frame.f_code)
instr = next(instr for instr in instrs if instr.offset > frame.f_lasti)
match instr.opname:
case "STORE_FAST" | "STORE_GLOBAL" | "STORE_NAME":
return instr.argval
case "STORE_FAST_LOAD_FAST":
def sig(fn):
code = fn.__code__
defaults = fn.__defaults__
kwdefaults = fn.__kwdefaults__
if defaults is None:
defaults = ()
if kwdefaults is None:
kwdefaults = {}
pos, varnames = code.co_varnames[:code.co_argcount], code.co_varnames[code.co_argcount:]
@cppio
cppio / descr.py
Last active October 18, 2025 13:14
Python descriptors
import types
class StaticMethod:
def __init__(self, f):
self.f = f
def __get__(self, instance, owner):
return self.f
class ClassMethod:
╭───────╮
│ Hello │
╰───────╯
┌───────┐
│ Hello │
└───────┘
┏━━━━━━━┓
┃ Hello ┃
import Lean.Elab.Term
deriving instance Lean.ToExpr for String.Pos
deriving instance Lean.ToExpr for Substring
deriving instance Lean.ToExpr for Lean.SourceInfo
deriving instance Lean.ToExpr for Lean.Syntax
syntax "log! " (interpolatedStr(term) <|> term:arg) : term
@[term_elab termLog!__]
-- https://arxiv.org/abs/1911.08174
def T : Prop := ∀ p, (p → p) → p → p
def δ (z : T) : T := fun p η h => η (z (T → T) id id z p η h)
def ω : T := fun _ _ h => propext ⟨fun _ => h, fun _ => id⟩ ▸ δ
def Ω : T := δ ω
noncomputable def WellFounded.fix' {α : Sort u} {r : α → α → Prop} (hwf : WellFounded r) {C : α → Sort v} (F : ∀ x, (∀ y, r y x → C y) → C x) x : C x :=
@Acc.rec α r (fun x _ => C x) (fun x _ => F x) x (Ω (∀ x, Acc r x) (fun wf x => ⟨x, fun y _ => wf y⟩) hwf.apply x)
theorem WellFounded.fix'_eq : @WellFounded.fix' α r hwf C F x = F x fun y _ => hwf.fix' F y := rfl