Skip to content

Instantly share code, notes, and snippets.

View highfestiva's full-sized avatar

highfestiva highfestiva

View GitHub Profile
@highfestiva
highfestiva / udp_route.py
Created February 13, 2013 23:19
UDP router for testing latency, jitter and packet loss. Useful when developing network games.
#!/usr/bin/env python3
from random import uniform
from sched import scheduler
import socket
from threading import Semaphore, Thread
class EternalScheduler:
def __init__(self):
@highfestiva
highfestiva / translate_stdout
Created September 20, 2013 08:31
Why not translate everything you print into Swedish? Translator stolen from this guy: https://github.com/terryyin/google-translate-python/blob/master/translate.py
import re
from urllib import request
from urllib.parse import quote
string_pattern = r"\"(([^\"\\]|\\.)*)\""
match_string = re.compile(
r"\,?\["
+ string_pattern + r"\,"
+ string_pattern + r"\,"
+ string_pattern + r"\,"
import web
urls = (
'/(.*)', 'index' # Regexp matching for URI -> class
)
class index:
def GET(self, name):
return '<html><body>Hi!</body></html>'
@highfestiva
highfestiva / tcpscan.py
Last active August 29, 2015 14:00
Parallel tcp port scan
#!/usr/bin/env python3
# Scan TCP ports in a multithreaded fashion.
import multiprocessing
import re
import socket
import sys
def checkport(_):
global queue
@highfestiva
highfestiva / natural_sort.py
Created May 2, 2014 14:18
Sort input from pipe or files on command line
#!/usr/bin/env python3
import re, sys
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
return [int(text) if text.isdigit() else text.lower()
for text in re.split(_nsre, s)]
fs = [sys.stdin] if not sys.stdin.isatty() else [open(fn) for fn in sys.argv[1:]]
for f in fs:
@highfestiva
highfestiva / example_alarm.py
Created May 12, 2014 22:40
Generator state machine home alarm snippet
def alarm_main():
print('Alarm not turned on.')
while not alarm_active():
yield # yield resumes in the next timeslice.
print('Alarm turned on, waiting for burglar.')
while alarm_active():
if alarm_sensor_detecting_something():
print('FAT BEEP! (Waiting 10 seconds to wake the neighbours up.)')
for _ in range(10): yield
print('Siren turned off. Resetting alarm.')
@highfestiva
highfestiva / fuzzy_path.py
Last active August 29, 2015 14:02
Fuzzy finding a path given a directory, say 'Deploy/31337 Application/SETTINGS.INI' would find 'deployment/application_12/settings.xml'. Now includes support for partial wildcards in a glob-like method.
#!/usr/bin/env python3
from glob import glob as doglob
import difflib
import os
def _match_ratio(s1,s2):
return difflib.SequenceMatcher(None,s1.lower(),s2.lower()).ratio()
def find(basepath, fuzzypath, thresold=0.7):
void setup() {
Serial.begin(9600);
while (!Serial)
;
pinMode(OPIN_LED_STATE, OUTPUT);
pinMode(OPIN_SIREN_ON, OUTPUT);
pinMode(IPIN_SWITCH, INPUT_PULLUP);
pinMode(IPIN_SENSOR1, INPUT);
pinMode(IPIN_SENSOR2, INPUT);
digitalWrite(OPIN_SIREN_ON, LOW);
active = device.active
inactive = lambda: not active()
while inactive():
yield
active_init_blip(device)
statewait(inactive, timeout=initiation_timeout)
if inactive():
inactive_blip(device)
return
active_running_blip(device)
@highfestiva
highfestiva / sendsms.py
Created June 19, 2014 22:06
Twilio sending SMS.
from twilio.rest import TwilioRestClient
msg = 'My message from Österbotten'
phones = ('+46-123-456789', '+35-2-23 45 67')
account_sid = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
token = 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
for phone in phones:
client = TwilioRestClient(account_sid, token)
client.messages.create(to=phone,
from_="+46zzzzzzzzz",
body=msg)