Skip to content

Instantly share code, notes, and snippets.

View jsheedy's full-sized avatar

Joseph L. Sheedy jsheedy

  • San Francisco Bay Area
View GitHub Profile
@jsheedy
jsheedy / designer.html
Last active August 29, 2015 14:12
designer
<link rel="import" href="../chart-js/chart-js.html">
<link rel="import" href="../cool-clock/cool-clock.html">
<link rel="import" href="../core-input/core-input.html">
<link rel="import" href="../paper-radio-button/paper-radio-button.html">
<link rel="import" href="../paper-radio-group/paper-radio-group.html">
<link rel="import" href="../google-map/google-map.html">
<polymer-element name="my-element">
<template>
@jsheedy
jsheedy / d3.geo.projection.js
Last active November 7, 2015 02:12
d3-grid-map hammer projection invert
(function() {
d3.geo.project = function(object, projection) {
var stream = projection.stream;
if (!stream) throw new Error("not yet supported");
return (object && d3_geo_projectObjectType.hasOwnProperty(object.type) ? d3_geo_projectObjectType[object.type] : d3_geo_projectGeometry)(object, stream);
};
function d3_geo_projectFeature(object, stream) {
return {
type: "Feature",
id: object.id,
@jsheedy
jsheedy / iter_file.py
Last active February 2, 2024 06:59
Sometimes you need a file-like object when all you have is an iterator, for instance when using psycopg2's cursor.copy_from. This class will handle the impedance mismatch.
import io
import sys
class IteratorFile(io.TextIOBase):
""" given an iterator which yields strings,
return a file like object for reading those strings """
def __init__(self, it):
self._it = it
self._f = io.StringIO()
<!DOCTYPE html>
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Press+Start+2P' rel='stylesheet' type='text/css'>
<title>brown</title>
<style type="text/css">
html, body {
height: 100%;
background-color: brown;
margin: 0px;
@jsheedy
jsheedy / monty.py
Last active January 12, 2016 23:17
numerical solution of the monty hall problem
import random
GOAT = 0
CAR = 1
TRIALS = 10**4
def run_trial(switch=False):
choice = random.randint(0,2)
doors = [GOAT, GOAT, CAR]
random.shuffle(doors)
@jsheedy
jsheedy / context_manager_iterator.py
Created January 29, 2016 20:43
a context manager / iterator which remembers the final value
import random
class Foo():
data = (random.randint(0, 1000) for x in range(10))
last = None
def __iter__(self):
return self
@jsheedy
jsheedy / uw_rooftop.py
Created February 20, 2016 01:54
latest weather observations from UW ATG building rooftop
""" provides latest_uw_data(), a generator which yields dictionary records of current
weather obs from UW ATG """
from datetime import datetime,timedelta
from urllib import request
def latest_uw_data():
url = "http://www.atmos.washington.edu/cgi-bin/latest_uw.cgi?data"
with request.urlopen(url) as f:
@jsheedy
jsheedy / extract_data.py
Last active March 18, 2016 18:26
JSON stream extractor
#!/usr/bin/env python
""" parses a JSON document on stdin, and prints to stdout the portion of the document
referenced by the Python getter substring given as the first argument.
This implementation uses eval(), so use with care! No warranty implied.
For example
$ curl 'http://api.duckduckgo.com/?q=ramen&format=json' 2> /dev/null | ~/bin/extract_data.py '["RelatedTopics"][0]["Text"]'
@jsheedy
jsheedy / floyd_steinberg.py
Created May 25, 2016 23:46
floyd-steinberg dithering of a single channel uint8 image
def floyd_steinberg_dither(data):
result = data.astype(np.float64)
fs_matrix = np.array( (
( np.nan, np.nan, 7/16 ),
( 3/16, 5/16, 1/16),
), dtype=np.float64 );
fs_mask = np.array( (
@jsheedy
jsheedy / skeletonize.py
Created June 10, 2016 23:56
OpenCV-Python skeletonize function
def skeletonize(img):
""" OpenCV function to return a skeletonized version of img, a Mat object"""
# hat tip to http://felix.abecassis.me/2011/09/opencv-morphological-skeleton/
img = img.copy() # don't clobber original
skel = img.copy()
skel[:,:] = 0
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))