Skip to content

Instantly share code, notes, and snippets.

View markwatson's full-sized avatar
💻
hacking the mainframe

Mark Watson markwatson

💻
hacking the mainframe
View GitHub Profile
function countCSSRules() {
var results = '',
log = '';
if (!document.styleSheets) {
return;
}
for (var i = 0; i < document.styleSheets.length; i++) {
countSheet(document.styleSheets[i]);
}
function countSheet(sheet) {
@markwatson
markwatson / csv.py
Last active August 29, 2015 13:56
Reads CSV files while handling all types of edge cases. (Built as an exercise - probably not useful)
import re
import unittest
class CsvReader(object):
"""
Reads CSV files while handling all types of edge cases.
"""
def __init__(self, lines):
"""
A new CSV reader with the given lines.
@markwatson
markwatson / time_card.py
Created July 16, 2013 04:21
A simple time tracker script.
#/usr/bin/env python
"""
time_cards.py
This utility tracks time.
"""
import datetime
import atexit
@markwatson
markwatson / merge_tuples.rb
Created March 11, 2013 21:40
Merges an array of arrays by adding the elements together.
# Takes an array of totals_tuples and combines them into a single tuple.
def self.merge_tuples(tuples)
tuples.inject([]) do |xs, ys|
l = [xs.length, ys.length].max
zs = []
l.times.each do |i|
zs[i] = (xs[i] || 0) + (ys[i] || 0)
end
zs
end
@markwatson
markwatson / pretty_json.py
Last active December 12, 2015 04:58
A sublime text plugin that pretty formats all the select JSON blobs.
import sublime, sublime_plugin
import json
import re
class PrettyJsonCommand(sublime_plugin.TextCommand):
def run(self, edit):
re_json_p = re.compile(r'^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$')
try:
for region in self.view.sel():
@markwatson
markwatson / stats.py
Created May 3, 2012 20:37
Random stats functions
def mean(x): return sum(x) / len(x)
def std_dev(x, mx=None):
if mx is None:
mx = mean(x)
return math.sqrt(sum((i - mx)**2 for i in x)/len(x))
@markwatson
markwatson / coroutine_io.py
Created April 26, 2012 19:07
A python IO class that writes to a coroutine.
import io
class CoroutineIO(io.TextIOBase):
"""
Creates an writable IO interface to a coroutine.
"""
def __init__(self, coroutine):
"""
Creates a new IO object with a coroutine. The
coroutine should take no arguments.
@markwatson
markwatson / iter_utils.py
Created April 13, 2012 19:39
Random python functions for working with iterators.
"""
A number of functions for working with iterators.
"""
def all(iterable):
for element in iterable:
if not element:
return False
return True
@markwatson
markwatson / eq.py
Created March 22, 2012 17:59
Find out if all values in a sequence are equal.
def eq(*vals):
"""
Finds out if a bunch of values are equal to each other.
"""
if len(vals) == 0:
return False
else:
return reduce(lambda x, y: x if x == y else not x, vals) == vals[0]
@markwatson
markwatson / print_unique.py
Created March 8, 2012 18:29
A Python class that only prints unique files. Useful for debugging inner loops.
class PrintUnique(object):
"""
A class that keeps track of what it has printed to stdout and won't
print anything twice.
"""
printed = set()
@classmethod
def write(cls, s):
"""