Skip to content

Instantly share code, notes, and snippets.

@skagedal
skagedal / test_pickRandom.html
Created January 29, 2012 22:19
Speed testing and "fair coin" testing for pickRandom
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="underscore.js"></script>
<script type="text/javascript">
function time(s, f) {
start = new Date();
f();
end = new Date();
$("#speedtest").append(s + ": " + String(end - start) + " ms<br />");
@skagedal
skagedal / underscore-pickrandom.js
Created January 31, 2012 11:22
pickRandom mixin for underscore
// underscore-pickrandom.js
// (c) 2012 Simon Kågedal Reimer
// This file is freely distributable under the MIT license.
// _.pickRandom - a mixin for underscore.js.
//
// Pick random elements from an array. Works similar to
// `_.first(_.shuffle(array, n))`, but is more efficient -- operates
// in time proportional to `n` rather than the length of the whole
// array.
@skagedal
skagedal / puzzle.py
Created December 27, 2012 16:08
Solves a 3D wooden puzzle thing we got for christmas.
# Script to solve a wooden 3D puzzle thing someone got for christmas.
# Two-colored tiles should be placed in layers so that no color appears twice
# in each layer or in each column.
#
# Outputs:
#We have a solution!
# Pink ,Red Orange,Yellow Grey ,Green
#Blue ,Yellow Pink ,Green Grey ,Orange
# Blue ,Grey Pink ,Orange Green ,Red
#Orange,Red Yellow,Grey Pink ,Blue
@skagedal
skagedal / test_gst010_gobject.py
Created May 31, 2013 22:06
Test case for broken GStreamer 0.10 with PyGObject
#!/usr/bin/python
"""From https://bugzilla.gnome.org/show_bug.cgi?id=631901#c7 but with
some bug fixes. Should print message objects, but on GStreamer 0.10,
just prints None."""
from gi.repository import GObject
GObject.threads_init()
from gi.repository import Gst
@skagedal
skagedal / bug.vala
Last active December 19, 2015 10:59
Vala bug
/*
* With Vala 0.20.1 from Ubuntu PPA, this gives no errors or warnings on "vala test.vala -C",
* but gives C code that doesn't compile:
/home/simon/bug.vala.c: In function ‘_vala_main’:
/home/simon/bug.vala.c:76:2: error: initializer element is not constant
/home/simon/bug.vala.c:76:2: error: (near initialization for ‘foos[0].s’)
*/
struct Foo {
@skagedal
skagedal / mmongo.rb
Created April 11, 2015 20:17
mmongo in ruby
#!/usr/bin/env ruby
# mmongo: An alternative to "meteor mongo" that lets you pass any
# arguments to mongo after the -- argument, as in:
# mmongo myapp.meteor.com -- myfile.js
#
# I rewrote this with more features in JavaScript with Node.js:
# https://github.com/skagedal/mmongo
require 'uri'
@skagedal
skagedal / uilabel-trace.py
Created June 20, 2015 21:07
Plot of error in UILabel's vertical entering
#
# See http://stackoverflow.com/questions/30759598/how-does-uilabel-vertically-center-its-text/30910504#30910504
#
import re
import matplotlib.pyplot as pyplot
input = open("trace-output.txt").read()
entries = input.split("=====")
@skagedal
skagedal / scrape.py
Last active December 29, 2015 17:54
Libris Downloader
from urllib.request import urlopen
counter = 1
while True:
url = 'http://libris.kb.se/xsearch?d=swepub&hitlist&q=l%C3%A4ros%C3%A4te%3agu&f=ext&spell=true&hist=true&n=200&start=' + str(counter)
print ("Fetching: " + url)
data = urlopen(url).read()
if not data.find(b"<record>") >= 0:
print("No more records!")
import json
import re
from os import listdir
def fix_escapes(string):
# Libris over-escapes some backslashes.
string = string.replace("\\\\\"","\\\"")
# Libris fails to properly escape backslashes in strings, which occurs for example with inline
# LaTeX codes like "$\geq" which should be escaped as "$\\geq". They do seem to properly
# escape quote chars, however. Now, we can't easily know whethera string liike "\n" should be
from urllib.request import urlopen
counter = 1
while True:
url = 'http://libris.kb.se/xsearch?d=swepub&hitlist&q=l%C3%A4ros%C3%A4te%3agu&f=ext&spell=true&hist=true&n=200&format=json&start=' + str(counter)
print ("Fetching: " + url)
data = urlopen(url).read()
if not data.find(b'"identifier"') >= 0:
print("No more records!")