Skip to content

Instantly share code, notes, and snippets.

@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 / gist:8298386
Created January 7, 2014 12:07
Hack the CES Scavenger hunt ;-) You'll need a recent mac with BT4.0 running mavericks, and a copy of bleacon (npm install bleacon) start the scavenger hunt, then run this script and sit back.
#! /usr/bin/env node
var Bleacon = require('bleacon');
var uuid = '842AF9C408F511E39282F23C91AEC05E';
var major = 65000;
var measuredPower = -59;
function myLoop(){
setTimeout(function () {
Bleacon.stopAdvertising();
@sammachin
sammachin / getquota.py
Created February 10, 2014 10:15
AAISP GET Quota, snippet to get your remaining quota from Andrews & Arnolds 'clueless' server in python, needs requests & beautifulsoup4 modules
import requests
from bs4 import BeautifulSoup
aauser = 'xx99@a'
aapass = 'xxxxx'
def getQuota():
r = requests.get('https://clueless.aa.net.uk/main.cgi', auth=(aauser, aapass))
soup = BeautifulSoup(r.text)
qstring = soup.find_all('table')[1].find('tbody').find_all('td')[5].find('a').string
@sammachin
sammachin / mo_sms.py
Last active January 21, 2017 21:31
Simple MO SMS HTTP Server for A&A SIP2SIM
#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
import logging
import cgi
PORT = 8001
I = '0.0.0.0'
Here is a hypothetical example I don't know if this is legal and correct but this is my idea.
Sam Systems Ltd. earns £10k from consulting work for Acme Mega Corp.
As SS is VAT registered on the flat rate scheme eh invoices 12K and pays £1680 of that to HMRC as VAT
He then takes the remaining £10320 and invests that in R&D of a prototype new product as follows:
He spends 2 months of his time building a back end system paying himself a minimal salary of £663/month
@sammachin
sammachin / setup.py
Created August 7, 2014 11:29
Setup The
#! /usr/bin/env python
import requests #we're going to use the requests library for a nice HTTP
import json #and the JSON library for parsing the data
@sammachin
sammachin / fetch.py
Last active November 27, 2016 22:30
Fetch data from the ONS API
# We need to specify our api key here to use in the request
apikey = "XXXXXX"
#Now we'll setup the base url for the ons API
baseurl = "http://data.ons.gov.uk/ons/api/data/dataset/"
# We need to specify the dataset we are after, in this case its just the split of gender
dataset = "QS104EW"
#And pass in some of our parameters