Skip to content

Instantly share code, notes, and snippets.

@sammachin
sammachin / gist:6c2b91d52b1052b0ec58
Created February 5, 2015 13:00
Convert 32bit binary int from BE to LE to Dec.
import struct
def convert(input):
v1 = struct.pack('>I', int(input, 2))
v2 = struct.unpack('<I', v1)
output = int(''.join(map(str,v2)))
return output
@sammachin
sammachin / conv.pl
Created February 5, 2015 13:26
convert BE/LE binary now with MOAR perl
my($input) = 0b01000000010000100000111100000000;
my $v1 = pack("I>", $input);
my $v2 = unpack("I<", $v1);
print "$v2"
@sammachin
sammachin / validate.py
Created May 1, 2015 20:05
Tornado Validate Twilio Request
class Validate(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
signature = self.request.headers.get('X-Twilio-Signature')
AUTH_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
validator = RequestValidator(AUTH_TOKEN)
url = self.request.uri
var = self.request.arguments
for x in var:
var[x] = ''.join(var[x])
@sammachin
sammachin / validate.py
Created May 2, 2015 10:03
Twilio Request Validation in Tornado on Heroku
import os
import tornado.httpserver
import tornado.ioloop
import tornado.web
from twilio import twiml
from oauth2client.client import SignedJwtAssertionCredentials
from twilio.util import RequestValidator
class ValidateHandler(tornado.web.RequestHandler):
@sammachin
sammachin / ws_socket_server.py
Last active August 29, 2015 14:26
Callbacks inside WebSocket Connections
#!/usr/bin/env python
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
from datetime import datetime
import sched, time
class WSHandler(tornado.websocket.WebSocketHandler):
@sammachin
sammachin / gist:4648722
Last active December 11, 2015 19:28
Convert an amount into the smallest number of notes & coins, currently written for British Currency but should be easy to modify
def toCash(amount):
pounds,pence = divmod(amount, 1)
pounds = int(pounds)
pence = pence*100
pence = int(round(pence))
notes = [50, 20, 10, 5, 1] #These are the whole amounts Yes I know the £1 & £2 are coins in the UK!
coins = [50, 20, 10, 5, 2, 1] #These are the less than whole amounts eg the pence
for note in notes:
count = pounds / note
if count >= 1:
@sammachin
sammachin / gist:5347517
Last active December 16, 2015 00:29
Python function to normalise a UK dialed number into an e.164 number
def normalise(number):
if re.match("^00.*", number):
return number.lstrip("0")
elif re.match("^0[1-9].*", number):
return "44" + number.lstrip("0")
elif re.match("^\+44.*", number)
return number.lstrip("+")
else:
return number
@sammachin
sammachin / limit.js
Created June 22, 2013 17:23
Rate limiting in javascript, ideal for things like watch location
var timer = false;
function doSomething(){
if (timer == false){
console.log("DOING SOMETHING");
timer = true;
console.log("Timer is set")
setTimeout(function(){timer = false;
console.log("Timer is clear")
},10000);
@sammachin
sammachin / gist:7888612
Last active December 30, 2015 21:39
Selecting and then adding a specific number to a Tropo application
(Examples written using httpie as a command line tool)
username and password are the values you use on the tropo web portal
Request a selection of avalible numbers for a given city prefix in this case 415 (San Francisco)
http --auth username:password GET http://api.tropo.com/addresses available==1 prefix==1415
This will return a JSON object as below that contains a selection of avalible numbers
@sammachin
sammachin / Add the following to
Created February 9, 2016 10:51
Raspberry Pi Chromium Kiosk/Sinage
# 1900x1200 at 32bit depth, DMT mode
disable_overscan=1
framebuffer_width=1900
framebuffer_height=1200
framebuffer_depth=32
framebuffer_ignore_alpha=1
hdmi_pixel_encoding=1
hdmi_group=2