Skip to content

Instantly share code, notes, and snippets.

View alejio's full-sized avatar

Alex Spanos alejio

  • Greater London
View GitHub Profile
@alejio
alejio / gmap
Created February 15, 2015 16:35
Google maps in ipython
def gmap(lat,lon,zoom=14):
from IPython.display import IFrame
from IPython.core.display import display
# Google Maps URL template for an iframe
google_maps_url = "http://maps.google.com/maps?q={0}+{1}&" + \
"ie=UTF8&t=h&z={2}&{0},{1}&output=embed".format(lat, lon,zoom)
@alejio
alejio / gist:ab8f9c5e5e80f9ea5a37
Created February 16, 2015 13:38
update python packages
import pip
from subprocess import call
for dist in pip.get_installed_distributions():
call("pip install --upgrade " + dist.project_name, shell=True)
@alejio
alejio / distance_on_unit_sphere.py
Last active August 29, 2015 14:15
distance on earth
import math
def distance_on_unit_sphere(lat1, long1, lat2, long2):
# Convert latitude and longitude to
# spherical coordinates in radians.
degrees_to_radians = math.pi/180.0
# phi = 90 - latitude
phi1 = (90.0 - lat1)*degrees_to_radians
@alejio
alejio / omit_in_parentheses.py
Last active August 29, 2015 14:20
Nanodegree > MongoDB > Lesson 4 > Problem set 1 > Omitting text in parentheses
def trim_par(line):
outp = []
comp=re.compile(r'\(+\S+\)', re.IGNORECASE)
linesplit=line.split()
for index in range(0, len(linesplit)):
m = comp.search(linesplit[index])
if m:
continue
else:
outp.append(linesplit[index]
@alejio
alejio / regex_misc.py
Last active August 29, 2015 14:20
Regex stuff
#Reject entries containing non-alphanumeric chars
if re.match('^[a-zA-Z0-9_]+$', test1):
#do stuff
@alejio
alejio / process_arachnid.py
Last active August 29, 2015 14:20
Nanodegree > Project 2 > Lesson 4 > Problem 1
def res_clean(line):
#set name entry equal to label if broken
n = re.match('^[a-zA-Z0-9_]+$',line["name"])
if line["name"]=="NULL" or not n:
line["name"]=line["rdf-schema#label"]
#if value is null set to none
for key, value in line.items():
if value=="NULL":
line[key]=None
#trim redundant parentheses from rdf-schema#label
@alejio
alejio / pop_dens.py
Created July 17, 2015 15:15
My first scraper: Scrape zipatlas.com for population statistics per zipcode
def get_IA_pop_dens_ZIP():
#scrapes table from http://zipatlas.com/us/ia/zip-code-comparison/population-density.htm
#and http://zipatlas.com/us/ia/zip-code-comparison/population-density.?.htm
url_base = "http://zipatlas.com/us/ia/zip-code-comparison/"
page1 = "population-density."
pnum = [i for i in range(1,11)]
htm = "htm"
tables_pages =[]
for page in pnum:
if page==1:
@alejio
alejio / corstarsl.Rd
Last active August 29, 2015 14:26
Print readable and interpretable correlation table (using rcorr)
#Took this function that makes nice correlation tables from here
#http://myowelt.blogspot.co.uk/2008/04/beautiful-correlation-tables-in-r.html
corstarsl <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
## define notions for significance levels; spacing is important.
mystars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " ")))
@alejio
alejio / ggplot2_hist.Rd
Last active August 29, 2015 14:26
R: ggplot2 histogram with density
#Adjusted from http://www.r-bloggers.com/how-to-make-a-histogram-with-ggplot2/
ggplot(data=data1, aes(x=`BOTTLE QTY`)) +
geom_histogram(aes(y =..density..),
col="red",
fill="green",
alpha = .2, origin = -0.5, binwidth=1) +
labs(title="Histogram for Bottle Quantities") +
labs(x="Bottles", y="Count") + scale_x_continuous(limits=c(0,30), breaks=0:30)
@alejio
alejio / SQLite_save.Rd
Created August 1, 2015 18:08
R: Save data frame to SQLite db
require("RSQLite")
# Set up database
drv <- dbDriver("SQLite")
tfile <- "Iowa.db"
con <- dbConnect(drv, dbname = "Iowa.db")
dbWriteTable(con, "data1", as.data.frame(data1))
dbDisconnect(con)