Skip to content

Instantly share code, notes, and snippets.

View dpk's full-sized avatar

Daphne Preston-Kendal dpk

View GitHub Profile
@dpk
dpk / gist:8325992
Last active February 27, 2024 05:08
PyICU cheat sheet

PyICU cheat sheet

Because you can't get the docs.

Transliteration

Create a transliterator:

greek2latin = icu.Transliterator.createInstance('Greek-Latin')
@dpk
dpk / gist:2830988
Created May 29, 2012 21:47
djb2 and djb2a hash functions in Ruby
def djb2 str
hash = 5381
str.each_byte do |b|
hash = (((hash << 5) + hash) + b) % (2 ** 32)
end
hash
end
def djb2a str
@dpk
dpk / gist:1844421
Created February 16, 2012 12:13
Remove sendmail from FreeBSD
# remove sendmail from a FreeBSD system
echo 'sendmail_enable="NO"' >> /etc/rc.conf
chmod 0 /usr/libexec/sendmail/sendmail
chmod 0 /usr/sbin/sendmail
mv /usr/libexec/sendmail/sendmail /usr/libexec/sendmail/sendmail.bak
mv /usr/sbin/sendmail /usr/sbin/sendmail.bak
@dpk
dpk / gist:931741
Created April 20, 2011 15:54
Plan: a very basic SXML-to-XML transformer. Only takes a small, simple subset of the full SXML.
(deffn sxml->xml (sxml)
(with (tag (car sxml) attrs nil children nil)
(if (= (caadr sxml) '@) ; if we have attributes
(do (if (is-a? (car (cdadr sxml)) 'list)
(set attrs (cdadr sxml)) ; association list attribute mode
(set attrs (pairs (cdadr sxml)))) ; pair list attribute mode -- plan only
(set children (cddr sxml)))
(set children (cdr sxml))) ; else, if we don't have any attributes
@dpk
dpk / bfc
Last active February 18, 2022 15:20
Brainfuck compiler in 182 bytes. (172 without shebang; 163 without shebang or automatic feeding to C compiler)
#!/bin/sh
tr -Cd '][.,<>+-'|sed 's/\./putchar(*p);/g;s/,/*p=getchar();/g;s/[+-]/&&*p;/g;s/[<>]/&&p;/g;s/\[/while(*p){/g;y/]<>/}-+/;s/^/main(){int a[30000];int *p=a;/;s/$/}/'|cc -xc -
@dpk
dpk / define-macro.scm
Created March 4, 2012 23:57
define-macro for Chicken Scheme
; kind of a stupid implementation but with
(define-syntax define-macro
(er-macro-transformer
(lambda (exp rename compare)
(if (symbol? (cadr exp))
(let ((name (cadr exp))
(expndr (caddr exp))
(-exp (gensym)) (-rename (gensym)) (-compare (gensym)))
`(define-syntax ,name
@dpk
dpk / gist:5694265
Created June 2, 2013 17:46
Python: iterate over the graphemes in a string.
import unicodedata as u
def itergraphemes(str):
def modifierp(char): return u.category(char)[0] == 'M'
start = 0
for end, char in enumerate(str):
if not modifierp(char) and not start == end:
yield str[start:end]
start = end
yield str[start:]
#include <stdlib.h>
unsigned long utf8_strlen(unsigned char *s) {
unsigned long len = 0;
s--;
while (*++s) {
len += !(*s >> 7) || (*s >> 6);
}
return len;
}
@dpk
dpk / mk-darwin-x86_64.diff
Last active December 18, 2017 11:13
Patch file for mk-with-libs.tgz to enable installing and building mk from https://9fans.github.io/plan9port/unix/ on Mac OS. to apply: patch -p1 < mk-darwin-x86_64.diff
diff -rNu mk.old/libbio/Make.Darwin-x86_64 mk.new/libbio/Make.Darwin-x86_64
--- mk.old/libbio/Make.Darwin-x86_64 1970-01-01 01:00:00.000000000 +0100
+++ mk.new/libbio/Make.Darwin-x86_64 2017-12-18 11:33:22.000000000 +0100
@@ -0,0 +1,7 @@
+CC=gcc
+CFLAGS+=-Wall -Wno-missing-braces -Wno-parentheses -Wno-switch -O2 -g -c -I. -I${PREFIX}/include
+O=o
+AR=ar
+ARFLAGS=rvc
+NAN=nan64.$O
@dpk
dpk / closefrom.py
Last active November 12, 2017 23:08
closefrom shim for Python 3
import os
import os.path
import fcntl
import ctypes
from ctypes.util import find_library
import re
libc = None
F_CLOSEM = None
strategy = None