Skip to content

Instantly share code, notes, and snippets.


Daphne Preston-Kendal dpk

View GitHub Profile
McCarthy's amb operator (kind of) implemented in Python
very unoptimized, much slow, wow
class amb:
def __init__(self, *values):
self.values = tuple(values)
if len(self.values) < 2:
dpk /
Created Feb 15, 2015
JavaScript length of a string (Python)
# jslen -- find the JavaScript length of a string (Python 3)
# Note to Python developers: since you know it as of Python 3.3, it'd
# be nice to have a way to find out in O(1) if a string contains any
# wide characters.
# find the preferred (i.e. faster) byte order
if ''.encode('utf-16') == b'\xFE\xFF':
preferred_encoding = 'utf-16le'
dpk /
Last active Nov 12, 2017
closefrom shim for Python 3
import os
import os.path
import fcntl
import ctypes
from ctypes.util import find_library
import re
libc = None
strategy = None
dpk /
Created Dec 7, 2014
infinite sequence of primes generator in Python (unbounded Sieve of Eratosthenes)
def primes():
prime_multiple_factors = {}
def add_factor(composite, factor):
if composite not in prime_multiple_factors:
prime_multiple_factors[composite] = set([factor])
x = 2
while True:
View marshal_proc.rb
module Rubinius
class CompiledCode
def _dump(depth)
def self._load(string)
scope, dump = Marshal.load(string)
cm =
cm.scope = scope
dpk /
Created Mar 14, 2014
Quick-and-dirty IRCCloud log export tool
import requests
import json
import sys
from collections import deque
from concurrent.futures import ThreadPoolExecutor as Pool
from datetime import datetime
from os import mkdir
from os.path import isdir
def debug(*args, **kwargs):
dpk / gist:8325992
Last active Jun 22, 2020
PyICU cheat sheet
View gist:8325992

PyICU cheat sheet

Because you can't get the docs.


Create a transliterator:

greek2latin = icu.Transliterator.createInstance('Greek-Latin')
dpk /
Created Aug 4, 2013
rename images downloaded from imgur so as to sort as in the original
#!/usr/bin/env python3
from lxml.html import html5parser as html
import requests
import os, os.path
import sys
class ImgurAlbumError(Exception): pass
def imgur_album_get(url):
dpk /
Last active Dec 19, 2015
Python: compact-ish unique identifiers
import base64
import struct
from uuid import getnode as get_mac
import os
from threading import Lock
import time
from ssl import RAND_bytes as random_bytes
mac = struct.pack('>Q', get_mac())[2:] # do this now in case it is slow (see Python manual)
dpk / gist:5694265
Created Jun 2, 2013
Python: iterate over the graphemes in a string.
View gist:5694265
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:]