Skip to content

Instantly share code, notes, and snippets.

View RhetTbull's full-sized avatar

Rhet Turnbull RhetTbull

View GitHub Profile
import sys
# Prints a list of digits to stdout
def emit(digits: list[int], newline:bool = False) -> None:
for i in digits:
print(i, end='')
if newline:
print()
@michalc
michalc / sqlite.py
Last active April 3, 2024 17:26
Use libsqlite3 directly from Python with ctypes: without using the built-in sqlite3 Python package, and without compiling anything
# From https://stackoverflow.com/a/68876046/1319998, which is itself inspired by https://stackoverflow.com/a/68814418/1319998
from contextlib import contextmanager
from collections import namedtuple
from ctypes import cdll, byref, string_at, c_char_p, c_int, c_double, c_int64, c_void_p
from ctypes.util import find_library
from sys import platform
def query(db_file, sql, params=()):
@p4bl0-
p4bl0- / 00_readme.md
Last active December 1, 2025 16:13
A complete compiler for a simple language (in less than 150 LoC)

This project is a tiny compiler for a very simple language consisting of boolean expression.

The language has two constants: 1 for true and 0 for false, and 4 logic gates: ! (not), & (and), | (or), and ^ (xor).

It can also use parentheses to manage priorities.

Here is its grammar in BNF format:

expr ::= "0" | "1"

@matiaskorhonen
matiaskorhonen / CustomPhotoImport.swift
Last active September 14, 2024 16:40
Custom image import into Photos.app using PhotoKit. Import an original JPEG plus an edited version of it. Proof of concept.
import Cocoa
import Photos
let original = URL(fileURLWithPath: "/Users/matt/Code/personal/PhotoKitPlayground/Test/DSC_6326.jpg")
let edited = URL(fileURLWithPath: "/Users/matt/Code/personal/PhotoKitPlayground/Test/DSC_6326-edit.jpg")
let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
if status != .authorized {
PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
@RhetTbull
RhetTbull / uti.csv
Created June 28, 2021 04:10
A CSV list of Apple macOS Universal Type Identifiers (UTIs) and associated extensions & MIME types. Generated programmaticaly with calls to UTTypeCreatePreferredIdentifierForTag (https://developer.apple.com/documentation/coreservices/1448939-uttypecreatepreferredidentifierf)
extension UTI preferred_extension MIME_type
c public.c-source c None
f public.fortran-source f None
h public.c-header h None
i public.c-source.preprocessed i None
l public.lex-source l None
m public.objective-c-source m None
o public.object-code o None
r com.apple.rez-source r None
s public.assembly-source s None
@pudquick
pudquick / findpboard.py
Created January 30, 2021 00:37
Get / Set shared Find Pasteboard state on macOS via pyobjc
from AppKit import NSPasteboard, NSFindPboard, NSStringPboardType, NSString
def get_fboard():
fboard = NSPasteboard.pasteboardWithName_(NSFindPboard)
if NSStringPboardType in fboard.types():
return fboard.stringForType_(NSStringPboardType)
def set_fboard(findstr):
fboard = NSPasteboard.pasteboardWithName_(NSFindPboard)
fboard.declareTypes_owner([NSStringPboardType], None)
@gullyn
gullyn / flappy.html
Last active November 19, 2025 15:40
Flappy bird in 205 bytes (improved!)
<body onload=z=c.getContext`2d`,setInterval(`c.width=W=150,Y<W&&P<Y&Y<P+E|9<p?z.fillText(S++${Y=`,9,9|z.fillRect(p`}*0,Y-=--M${Y+Y},P+E,9,W),P))):p=M=Y=S=6,p=p-6||(P=S%E,W)`,E=49) onclick=M=9><canvas id=c>
@RhetTbull
RhetTbull / copy_file_with_progress_bar.py
Last active November 15, 2025 06:12
Python method to copy a file with a callback (e.g. to show a progress bar). Includes example of copying with both click.progressbar and tqdm.
""" Copy a file with callback (E.g. update a progress bar) """
# based on flutefreak7's answer at StackOverflow
# https://stackoverflow.com/questions/29967487/get-progress-back-from-shutil-file-copy-thread/48450305#48450305
# License: MIT License
import os
import pathlib
import shutil
@jph00
jph00 / py_stdlib.md
Last active October 23, 2025 03:44
Hyperlinked index to every module, function, and class in the Python standard library

All of the python 3.9 standard library

(Too big for a single gist, so see first reply for the rest)

Table of contents