Skip to content

Instantly share code, notes, and snippets.

View milkey-mouse's full-sized avatar

Milkey Mouse milkey-mouse

View GitHub Profile
@milkey-mouse
milkey-mouse / classify.py
Last active November 18, 2024 16:41
Classify lines of a text file according to a category with GPT-4o(-mini)
#!/usr/bin/env python3
from base64 import b64encode
from hashlib import blake2b
from math import exp, log
from typing import Iterable, TextIO
import asyncio
import json
import os
import random
import sqlite3
@milkey-mouse
milkey-mouse / accidental_backrooms.md
Created October 27, 2024 03:52
what happens when you accidentally send claude 150 lines of pseudorandom numbers one at a time instead of all at once

2024-10-27T03:31:07 conversation: 01jb60e3hwwaqdrr30fm27sat5

Model: claude-3-5-sonnet-latest

Prompt:

2783979810

Response:

@milkey-mouse
milkey-mouse / seed_to_unit.rs
Created October 27, 2024 01:23
Sample uniformly from (Z/nZ)* to generate deterministic random permutations
#[inline]
pub fn mod_gt_neg_m(a: i64, m: u64) -> u64 {
debug_assert!(
m.try_into().is_ok_and(|m: i64| a >= -m),
"input to mod_gt_neg_m must be >= -m"
);
if a < 0 {
(a + m as i64) as u64
} else {
@milkey-mouse
milkey-mouse / unshare_userns.py
Created October 24, 2024 20:56
Simple example of "pretending you're root" with unshare(CLONE_NEWNS) in Python
from contextlib import contextmanager
import ctypes
import ctypes.util
import os
MS_BIND = 4096
MS_REC = 16384
MS_PRIVATE = 1 << 18
@milkey-mouse
milkey-mouse / parse_items.py
Last active October 20, 2024 17:05
Parse Rust item definitions from `cargo doc` output
#!/usr/bin/env python3
from html.parser import HTMLParser
from io import StringIO
import re
import sys
class ItemParser(HTMLParser):
MISSING_SEMICOLONS = re.compile(r"((^|\n)(?:[^/]|/[^/])*[^};\s])(\s*(?://.*)?)$")
use core::{
mem::{self, ManuallyDrop},
sync::atomic::{AtomicUsize, Ordering},
};
use std::num::NonZero;
static MAX_THREAD_ID: AtomicUsize = AtomicUsize::new(0);
thread_local! {
static THREAD_ID: NonZero<usize> = {
#!/usr/bin/env python3
import os
import sys
import textwrap
# https://github.com/coreutils/coreutils/commit/fcfba90d0d27a1bacf2020bac4dbec74ed181028
IO_BUFSIZE = 256 * 1024
@milkey-mouse
milkey-mouse / sort-imports.py
Last active November 4, 2024 05:18
Sort imports in Python files
#!/usr/bin/env python3
# sort-imports.py: Sort imports in Python files.
#
# Usage: ./sort-imports.py file1.py file2.py ...
#
# Sorts imports within "stanzas". A stanza is a block of consecutive import
# statements with the same indentation. Each stanza is sorted to contain first
# all `from` imports, sorted by module, then by imported names; then `import`
# imports, sorted by module.
// Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
// See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
use std::{error::Error, fmt::Display};
#[derive(Debug)]
pub struct Utf8Error;
impl Display for Utf8Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
import ast
from collections import defaultdict
from dataclasses import dataclass
from textwrap import dedent
@dataclass
class FunctionSpan:
indent: int
definition_line: int