Skip to content

Instantly share code, notes, and snippets.

View salmoni's full-sized avatar

James Smith salmoni

View GitHub Profile
@salmoni
salmoni / goQueryTest.go
Last active July 20, 2024 15:23
Parsing HTML in Go/Golang using goQuery to extract data from only one of multiple tables. Demonstrates nested Find statements.
package main
import (
"fmt"
"log"
"strings"
"github.com/PuerkitoBio/goquery"
)
@salmoni
salmoni / ParseCSVFile.py
Created May 1, 2014 11:42
Python: Class to parse a CSV file using the parser in another gist. Instantiate this object with a) the path + filename, b) delimiters (multiple accepted) and c) quote characters and it returns a file object that can be iterated over and read normally.
class CSVObject(object):
"""
This class instantiates a file object as an interable. It means that
CSV files can be read more efficiently than reading the entire data
into memory.
"""
def __init__(self, fileName, delims, quotes):
self.fileName = fileName
self.delims = delims
self.quotes = quotes
@salmoni
salmoni / ParseLineOfCSV.py
Created April 12, 2014 20:51
Parses a single line of CSV with a set (multiple!) delimiters and a set (multiple!) of quotation characters. Embedded quotes are kept honest (see examples at bottom of file).
def ParseLine(line, delims, quotes):
"""
Parses a line of text into components. This attempts to
be a proper parser that can cope with multiple delimiters.
"""
inQuote = False # flag for being 'within' quotes
token = '' # current token
tokens = [] # list of tokens
for char in line:
if inQuote: # so if we're in the middle of a quote...
@salmoni
salmoni / today.py
Created April 8, 2014 15:28
Get the date today. Returns something like 2013-10-14.
import datetime
def GetToday():
now = datetime.date.today()
return now.strftime("%Y-%m-%d")
if __name__ == '__main__':
print GetToday()
@salmoni
salmoni / GroupedDescriptives.py
Created April 8, 2014 15:26
Grouped descriptives
"""
Grouped descriptives
(c) 2013, Alan James Salmoni
"""
def Count(data):
return len(data)
def Sum(data):
@salmoni
salmoni / least_squares_fit.py
Created April 8, 2014 15:25
Brief code to calculate line of best fit for a single vector ('y'). Assumes the 'x' variable is a simple range from 0 .. N-1. Returns the intercept, the slope, the correlation between x and y, and the residual (error). Doesn't use the lstsq function in numpy but the results are the same.
import numpy
x = numpy.array(range(400))
xm = x.mean()
xstd = x.std()
def fitline(y):
N = len(y)
x = numpy.array(range(N))
ym = y.mean()
@salmoni
salmoni / Force IE10
Created April 8, 2014 15:23
Changing Windows registry so that IE emulates IE10. For all codes, see http://msdn.microsoft.com/en-us/library/ee330730(v=vs.85).aspx
import _winreg as wreg
current_file = __file__
key = wreg.CreateKey(wreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION")
wreg.SetValueEx(key, current_file, 0, wreg.REG_DWORD, 10001)
@salmoni
salmoni / linear_least_squares.py
Created August 19, 2013 14:49
Brief code to calculate line of best fit for a single vector ('y'). Assumes the 'x' variable is a simple range from 0 .. N-1. Returns the intercept, the slope, the correlation between x and y, and the residual (error). Doesn't use the lstsq function in numpy but the results are the same.
import numpy
x = numpy.array(range(400))
xm = x.mean()
xstd = x.std()
def fitline(y):
N = len(y)
x = numpy.array(range(N))
ym = y.mean()
@salmoni
salmoni / velocity.r
Created August 19, 2013 14:48
R code to measure 'velocity', how much each data point differs according to its neighbours. Accepts a vector and returns a single value measurement of velocity.
velocity <- function(vector) {
N <- length(vector)
val <- sum(abs(vector[1:N-1]-vector[2:N]))
return (val)
}