Skip to content

Instantly share code, notes, and snippets.

dpk dpk

Block or report user

Report or block dpk

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View mccarthy.py
"""
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 Feb 15, 2015
JavaScript length of a string (Python)
View jslen.py
# 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 Nov 12, 2017
closefrom shim for Python 3
View closefrom.py
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 Dec 7, 2014
infinite sequence of primes generator in Python (unbounded Sieve of Eratosthenes)
View primesieve.py
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:
View marshal_proc.rb
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 Mar 14, 2014
Quick-and-dirty IRCCloud log export tool
View irccexport.py
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 Jul 17, 2018
PyICU cheat sheet
View gist:8325992

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 Aug 4, 2013
rename images downloaded from imgur so as to sort as in the original
View imgurorder.py
#!/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 Dec 19, 2015
Python: compact-ish unique identifiers
View uniqid.py
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 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:]
You can’t perform that action at this time.