Skip to content

Instantly share code, notes, and snippets.

View timtrueman's full-sized avatar

Tim Trueman timtrueman

View GitHub Profile
@timtrueman
timtrueman / heading.py
Created March 5, 2010 08:13
Tilt-compensated heading from magnetometer readings, roll and pitch
def wrap(angle):
if angle > pi:
angle -= (2*pi)
if angle < -pi:
angle += (2*pi)
if angle < 0:
angle += 2*pi
return angle
def magnetometer_readings_to_tilt_compensated_heading(bx, by, bz, phi, theta):
#!/usr/bin/env python
# encoding: utf-8
import sys
import socket
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from struct import unpack_from
from math import cos, sin, tan, atan, isnan, pi, degrees, radians, sqrt
from numpy import matrix
@timtrueman
timtrueman / curses_print_numpy_matrix.py
Created February 9, 2010 05:16
Python — Use the curses library to display a numpy matrix in a pretty manner
def display_matrix(screen, m, x, y, precision=2, title=None):
rows, cols = m.shape
if title:
screen.addstr(x, y, title)
x += 1
screen.addstr(x, y, "[")
screen.addstr(x, cols*(4+precision)+y+1, "]")
screen.addstr(rows+x-1, y, "[")
screen.addstr(rows+x-1, cols*(4+precision)+y+1, "]")
for row in range(rows):
@timtrueman
timtrueman / gist:296560
Created February 6, 2010 05:32
Takes a body of text and truncates full urls into domain + tld
def truncate_urls_to_domain_plus_tld(text):
"""
Get all the urls
Find the domain + tld
Find and replace each url found with its domain + tld
"""
match_urls = re.compile(r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^%s\s]|/)))')
break_down_url = re.compile(r'^((?P<scheme>https?|ftp):\/)?\/?((?P<username>.*?)(:(?P<password>.*?)|)@)?(?P<hostname>[^:\/\s]+)(?P<port>:([^\/]*))?(?P<path>(\/\w+)*\/)(?P<filename>[-\w.]+[^#?\s]*)?(?P<query>\?([^#]*))?(?P<fragment>#(.*))?$')
urls = match_urls.findall(text)
for url in urls:
def update_elo_score(k=32, defender_rating=1000, attacker_rating=1000, attacker_won=False):
q_a = 10**(defender_rating/400.0)
q_b = 10**(attacker_rating/400.0)
expected_defender_score = q_a / float(q_a + q_b)
expected_attacker_score = q_b / float(q_b + q_a)
print "Expected chance of winning, defender %0.1f%%, attacker %0.1f%%" % (expected_defender_score*100, expected_attacker_score*100)
if attacker_won:
updated_defender_rating = defender_rating + int(k * (0 - expected_defender_score))
updated_attacker_rating = attacker_rating + int(k * (1 - expected_attacker_score))
else:
#!/usr/bin/python
#encoding:utf-8
import stompy
import time
import curses
def monitor_stompserver():
try:
myscreen = curses.initscr()
@timtrueman
timtrueman / xplane.py
Created November 8, 2009 00:49
This is just meant as an example to get you started. You will need to install Twisted ("sudo easy_install twisted").
#!/usr/bin/env python
# encoding: utf-8
import socket
from struct import *
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import math
UDP_IP="127.0.0.1"
sudo dd bs=1024 if=/Volumes/Drobo/ubuntu-9.04-desktop-amd64.iso of=/dev/disk3
<script id="uniqueId" type="text/javascript">
(function() {
// Code you want to execute here
})();
</script>
<img src="spacer.gif"
onload="eval(document.getElementById('uniqueId').innerHTML);">