Skip to content

Instantly share code, notes, and snippets.

View alexrutherford's full-sized avatar

Alex Rutherford alexrutherford

View GitHub Profile
@alexrutherford
alexrutherford / sanitise_json.py
Last active September 22, 2015 00:34
Python function to recursively clean $ from JSON to place in MongoDB
#################
def sanitiseNames(dummyObject):
#################
if type(dummyObject)==dict:
for k,v in dummyObject.items():
if re.search('\$',k,re.U):
# Replace
dummyObject[k.replace('$','')]=v
del dummyObject[k]
@alexrutherford
alexrutherford / youtube_api
Last active September 22, 2015 00:33
Snippet to grab video comments from YouTube API
import requests
ids=['qJS3xnD7Mus','LrNSOnQIVbI','p3tUqRBiMVo']
for id in ids:
commentsLink='https://gdata.youtube.com/feeds/api/videos/'+id+'/comments?v=2'+'&alt=json&max-results=50
commentsRaw=requests.get(commentsLink)
print commentsRaw.ststus_code
# 404 means not found, 200 means success
@alexrutherford
alexrutherford / entropy
Last active July 23, 2019 09:01
Function to calculate entropy using Python
import numpy as np
def entropy(vals):
sum=0.0
norm=0.0
for v in vals:norm+=v
vals=[v/norm for v in vals]
for v in vals:sum+=(v*np.log(v))
return -1.0*sum
@alexrutherford
alexrutherford / gist:8cc6f6671737a5d7dafe
Created July 16, 2014 21:03
Plot a set of time series from a dataframe with categories (in this case topics)
for a,b in df.groupby('topics'):
b.resample('D',how='count')['content'].plot(label=a,legend=True,figsize=(20,10),logy=False)
@alexrutherford
alexrutherford / Flask example
Last active September 22, 2015 00:32
Minimum snippet for using Python's Flask framework
from flask import Flask,render_template,jsonify,make_response,request,current_app,url_for
import pymysql
app=Flask('yt_db')
api=Api(app)
##########################
@app.route("/hello")
def hello():
return 'Hello world'
@alexrutherford
alexrutherford / Mac OS say command fun
Last active September 22, 2015 00:31
How to use the Mac say command line function
for v in $(say -v ? | awk '{print $1}'); do echo $v; say -v $v 'CHeck out my voice' ;done
@alexrutherford
alexrutherford / arg_checking_decorator
Last active September 22, 2015 02:25
Python function decorator to check types
def check_args(*types):
def real_decorator(func):
def wrapper(*args, **kwargs):
for val, typ in zip(args, types):
assert isinstance(val, typ), "Value {} is not of expected type {}".format(val, typ)
return func(*args, **kwargs)
return wrapper
return real_decorator
def do_long_computation(name):
@alexrutherford
alexrutherford / gist:988a53e918040c379746
Last active August 29, 2015 14:24
Gist for polygon hit detection in shapefiles
import glob
import shapefile
from matplotlib.path import Path
class HitDetector():
'''
Class to test if an arbitrary (lat,long) point
is within the boundary of a country outline
and which region it belongs in. Uses shapefiles
from gadm.org
@alexrutherford
alexrutherford / getTweetsByID.py
Created September 1, 2015 17:30
Python script for grabbing tweets by ID from API
import twitter
import csv
import json
"""
NOTE: You must have python-twitter installed on the machine running this script;
You can install it by running this on the command line:
sudo pip install python-twitter
@alexrutherford
alexrutherford / basemap.py
Last active September 22, 2015 00:30
Example plot of globe using basemap
from mpl_toolkits.basemap import Basemap
m = Basemap(projection='merc',lon_0=0,lat_0=0,resolution='l',area_thresh=30000,llcrnrlon=-120,llcrnrlat=-60,urcrnrlon=170,urcrnrlat=70)
m.drawcountries(linewidth=0.25,color='white')
m.drawlsmask(lakes=False)
for coord in random.sample(coords,10000):
# We select 10k checkins at random as too many checkins
m.plot(coord[1],coord[0],marker='o',color='r',markersize=1,latlon=True,alpha=0.1)
plt.savefig('map.png',dpi=200)