Skip to content

Instantly share code, notes, and snippets.

View dpk's full-sized avatar

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
dpk / jslen.py
Created February 15, 2015 18:56
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'
else:
@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
@dpk
dpk / primesieve.py
Created December 7, 2014 23:47
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])
else:
prime_multiple_factors[composite].add(factor)
x = 2
while True:
module Rubinius
class CompiledCode
def _dump(depth)
Marshal.dump([@scope, Rubinius::ToolSets::Runtime::CompiledFile::Marshal.new.marshal(self)])
end
def self._load(string)
scope, dump = Marshal.load(string)
cm = Rubinius::ToolSets::Runtime::CompiledFile::Marshal.new.unmarshal(dump)
cm.scope = scope
@dpk
dpk / irccexport.py
Created March 14, 2014 23:21
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
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 / imgurorder.py
Created August 4, 2013 12:40
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
dpk / uniqid.py
Last active December 19, 2015 22:59
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
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:]