Skip to content

Instantly share code, notes, and snippets.

@mharju
mharju / gist:1182864
Created August 31, 2011 05:12
Etsii ne lähtöjoukon n/2-kombinaatiot, joiden unioni muodostaa koko lähtöjoukon
def set_bipartitioning_combinations(iterable):
return ( (i, j) for i in itertools.permutations(iterable, len(iterable) / 2)
for j in itertools.permutations(iterable, len(iterable) / 2)
if set(i).union(j) == set(iterable) )
@mharju
mharju / gist:1286545
Created October 14, 2011 08:16
Asynchronous loading of a resource with resource names coming from external resource
protected void LoadFeedByName(string name, OpenReadCompletedEventHandler handler)
{
WebClient client = new WebClient();
client.OpenReadCompleted += handler;
if (App.UriProvider.IsFeedsAvailable)
{
client.OpenReadAsync(App.UriProvider.UriForName(name));
}
else
@mharju
mharju / gist:1292407
Created October 17, 2011 11:11
Quick & Dirty screensaver for PyCon.
import processing.opengl.*;
void setup()
{
size(1440, 960, OPENGL);
background(0);
}
void star(int size)
@mharju
mharju / gist:1300745
Created October 20, 2011 09:21
Playing around with functional features of C# to get concise code. This style does not feel so idiomatic, though.
OpenReadCompletedEventHandler GetGenericOpenHandler<T>(ObservableCollection<T> destination, Func<XElement, IEnumerable<T>> elementQuery)
{
return (object sender, OpenReadCompletedEventArgs e) =>
{
XElement xmlResult = this.GetXmlResult(e);
foreach (T result in elementQuery(xmlResult))
{
destination.Add(result);
}
};
@mharju
mharju / gist:1342709
Created November 6, 2011 09:54
Just a quick test to make fancy images out of b/w triangles. I fully agree on the lack of mathematical elegance. Maybe I'll clean this up sometime. Or then not.
// Patchwork
void setup()
{
size(640, 640, P3D);
noStroke();
initialize();
}
void initialize()
@mharju
mharju / gist:1504903
Created December 21, 2011 06:27
8h 12m 23s to hours representation. Used as python -c '<script here>'
import re
import sys
i = sys.stdin.readline()
print "%.2f" % sum( [ x*y for x, y in zip([int(a) for a in re.match(r'(\d+)h (\d+)m (\d+)s', i).groups()], (1, 1/60., 1/(60.*60.))) ])
@mharju
mharju / Makefile
Created December 30, 2011 14:20
Makefile for Ibniz (http://pelulamu.net/ibniz/) for use with OS X
# Copy SDLmain.m and SDLmain.h from the SDL DMG to the same directory
# To get decent framerates edit line 171 on ui_sdl.c and enter
# return SDL_GetTicks() / 3;
# ( source for that trick: http://lgo900.wordpress.com/2011/12/25/running-ibniz-on-mac-os-x/ )
CC=gcc
EXE=ibniz
LFLAGS=-framework SDL -framework Cocoa
FLAGS=-I/Library/Frameworks/SDL.Framework/Headers
all: ibniz
@mharju
mharju / gist:1593894
Created January 11, 2012 09:26
Invoke timeout after there has been no events in a given timeout
Observable.FromEvent<MapDragEventArgs>(Map, "MapPan")
.Publish(mapPan_ =>
{
mapPan_.Subscribe( _ =>
Observable.Timer(TimeSpan.FromMilliseconds(500))
.TakeUntil(mapPan_)
.Subscribe(__ => Debug.WriteLine("Timeout"))
);
return mapPan_;
@mharju
mharju / gist:1605373
Created January 13, 2012 10:04
tokenize input and subscribe. Instant profit when combined with JSON object async response stuff.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Subjects;
namespace ReactiveTest
# wat am i doing wrong here?
def setUp(self):
cursor = connection.cursor()
script = open('epic/sql/player.sql').read()
cursor.execute(script)
transaction.commit_unless_managed()
def tearDown(self):
cursor = connection.cursor()