Skip to content

Instantly share code, notes, and snippets.

@jeamland
jeamland / execfiletest.py
Created November 17, 2011 23:01
Execfile failure mode
import tempfile
test_script = """
import os
def foo():
print repr(os.environ)
"""
with tempfile.NamedTemporaryFile() as tmp:
@jeamland
jeamland / mouse_event.py
Created November 30, 2011 01:45
Javascript mouse events for Selenium
def mouse_event(browser, event, element_id, x, y):
script = """
var element = document.getElementById("%(element_id)s");
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initMouseEvent("%(event)s", true, true, window, 1, 0, 0, %(x)d, %(y)d, false, false, false, false, 0, null);
element.dispatchEvent(event);
} else {
var event = document.createEventObject();
No.
@jeamland
jeamland / gist:2214560
Created March 27, 2012 10:05
System of a Doom
Motherboard: Gigabyte GA-Z77MX-D3H
$145 (PCCaseGear, MSY no-show)
CPU: Intel Core i5-2500K
$212 (MSY, PCCaseGear $215)
RAM: G.Skill Ripjaws-X 8 GB (2 x 4 GB) 240-Pin DDR3-1600 Kit
$54 (MSY, PCCaseGear $55)
GPU: Sapphire Radeon HD 7870
$369 (MSY, PCCaseGear $395)
SSD: Intel 520 60GB
$113 (MSY, PCCaseGear $119)
@jeamland
jeamland / gist:2715212
Created May 17, 2012 00:38
On Learning to Code

I finished high school in the first year of a new assessment system in South Australia. This of course resulted in curriculum changes and all that kind of fun that happens around these kind of things. One of the objections parents raised was that the maths curriculum was too hard. As a child of two mathematicians I got to hear more about this as one of my parents was in the process of actually becoming involved in various aspects of the high school maths curriculum and how it was examined. One of the areas singled out as being too difficult was probability, you know calculating the chance of something happening. So the Authorities decided to remove it. What was funny to those of us who knew what was going on was that they didn't remove counting.

Counting is the process by which we work out how much of something there is. There are ways to do this mathematically, combinations and permutations and all that jazz. The joke here is that probability, at least in the way the course was structured, involved counting

line = "2012/05/18 00:01:57.350 [0.1107 0.0673] [-40434.7711 4055.2146 -48.3776] [[0.0000 -0.0000 0.0000]] [12.4549 25.2789 -4.0204] [33.9825 34.1984 23.4829] [-30128.3000 96816.4946 96816.4946] [0.0682 -84.2729] [83.4846 3.3248]"
date, time, line = line.split(' ', 2)
print date, time
bits = line.split('] [')
last_bits = bits[-1].split('] [')
bits[-1:] = last_bits
@jeamland
jeamland / gist:3311828
Created August 10, 2012 06:34
try/except/finally
>>> def foo():
... try:
... print 'one'
... raise Exception('wombats')
... print 'two'
... except:
... print 'three'
... raise
... finally:
... print 'four'
@jeamland
jeamland / gist:3399471
Created August 20, 2012 02:25
Enforced abstract base classes
class AbstractBaseMetaClass(type):
def __new__(meta, classname, bases, attributes):
if getattr(meta, '_required_methods', None) is None:
meta._required_methods = []
for name, attribute in attributes.items():
if name == '__metaclass__':
continue
if callable(attribute):
meta._required_methods.append(name)
@jeamland
jeamland / gist:3785330
Created September 26, 2012 00:42
Magic IPtables Port Bouncer Script
#!/bin/bash
# TCP Proxy using IPTables
IPTABLES=/sbin/iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
# Flush nat table
$IPTABLES -t nat -F
@jeamland
jeamland / gist:3838125
Created October 5, 2012 04:36
Django admin vs readonly
<freakboy3742> Holocaine: I'm here...
<Holocaine> freakboy3742: It's cool. He was having a sad over there not being
a "view" permission in django-admin and I suggested that if he wanted to find
out why that got bounced he could talk to you (for example) and find out what
other alternatives might exist.
<freakboy3742> Holocaine: Yeah - that's an interesting discussions with lots
of history
<Holocaine> freakboy3742: It looked like one which is why I figured it'd be
worth him getting some background on it before trying to head in any particular
direction.