Skip to content

Instantly share code, notes, and snippets.

Avatar

Joseph L. Sheedy jsheedy

View GitHub Profile
@jsheedy
jsheedy / context_manager_iterator.py
Created Jan 29, 2016
a context manager / iterator which remembers the final value
View context_manager_iterator.py
import random
class Foo():
data = (random.randint(0, 1000) for x in range(10))
last = None
def __iter__(self):
return self
@jsheedy
jsheedy / monty.py
Last active Jan 12, 2016
numerical solution of the monty hall problem
View monty.py
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)
View index.html
<!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 / iter_file.py
Last active Aug 31, 2020
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.
View iter_file.py
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()
@jsheedy
jsheedy / d3.geo.projection.js
Last active Nov 7, 2015
d3-grid-map hammer projection invert
View d3.geo.projection.js
(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,
View designer.html
<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>
You can’t perform that action at this time.