Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Hacking

Bart Wiegmans bdw

💭
Hacking
View GitHub Profile
@bdw
bdw / Dictionary.php
Created Jan 7, 2013
PHP on-disk dictionary. It stores a key-value hash as a binary tree (depth-first), with a 'right offset pointer'. A long time ago, I had methods that manipulated this on-disk, but unfortunately, I don't anymore. I will figure out how, though, and rewrite this into a more honest language.
View Dictionary.php
<?php
class Dictionary {
var $file;
var $index;
var $current;
function Dictionary($file) {
$this->file = $file;
$this->index = array();
@bdw
bdw / tree.go
Last active Dec 10, 2015
Binary tree in Go
View tree.go
package main
import (
"fmt"
"strconv"
"io"
"encoding/binary"
)
type BinaryTree struct {
@bdw
bdw / genius.py
Last active Jun 18, 2019
Genius - a roman daemon. This is a python context manager which tries to ensure it is the only process identified by a pidfile.
View genius.py
import os
import fcntl
import signal
import time
import contextlib
class Genius(object):
'''
A genius is basically a roman daemon. It differs from python-daemon
in the following ways:
View palindrome.py
import random
import io
import time
def profile(func):
def wrapper(*args):
start = time.clock()
rv = func(*args)
end = time.clock()
@bdw
bdw / life.py
Created May 28, 2013
Game of Life met pygame en numpy.
View life.py
#!/usr/bin/env python
import argparse
import numpy
import pygame
pygame.init()
from pygame.locals import QUIT # Dat deze uit z'n namespace moet is echt stom
# 3 regels:
# 1. Levende cellen met minder dan 2 levende buren gaan dood
@bdw
bdw / volume.js
Created Dec 26, 2013
function to calculate volume contained in the 'pools' in a two-dimensional array.
View volume.js
function calculateVolume(heights) {
var left = 0, right = 1, volume = 0;
while (right < heights.length) {
var level = heights[left];
if (heights[right] <= level) {
// tag the 'left cursor' along
left = right;
right = right + 1;
} else {
@bdw
bdw / ve.py
Created Feb 14, 2014
Really easy virtualenv wrapper
View ve.py
#!/usr/bin/env python
import argparse
import os
import os.path
import sys
import subprocess
import shutil
try:
import virtualenv
View proposal-gsoc-2014.html
<h3>What is your e-mail address and IRC nick?</h3>
<p>bartwiegmans@gmail.com, brrt on freenode</p>
<h3>What is your web page, blog, or microblog?</h3>
<p><a href="https://plus.google.com/u/0/+BartWiegmans/posts">I have a google+ page</a> - although I don't actually post a lot of things there.</p>
@bdw
bdw / compact.py
Created Mar 21, 2014
An algorithm to compact arrays (python). I figured it out but I can't explain why it works.
View compact.py
def compact(a_list):
# strategy - find the first empty and nonempty items
empty, nonempty = 0, 0
while empty < len(a_list) and nonempty < len(a_list):
# ok, so in principle this works really well... why?
# what happens is that nonempty looks for the earliest nonempty
# element and empty looks for the earliest empty element
if a_list[nonempty] is None:
nonempty += 1
@bdw
bdw / broken-re.py
Created Mar 25, 2014
showing python re is subtly broken
View broken-re.py
#!/usr/bin/env python
import re
s = r'"foo \" bar" baz "quix"'
# same regex right?
p_a = r'"(\\"|[^"])*"'
p_b = r'"([^"]|\\")*"'
# no
print(re.match(p_a, s).group(0))