Skip to content

Instantly share code, notes, and snippets.

@ccorcos
ccorcos / gist:9657279
Created March 20, 2014 04:30
Why is the div larger than my inner svg element using D3?
http://stackoverflow.com/questions/4640348/why-do-scrollbars-appear-when-using-a-svg-element-inside-a-div/4642713#4642713
svg {
display:block;
}
@ccorcos
ccorcos / gist:9657939
Created March 20, 2014 05:41
A more complete but still incomplete Bounded Force Layout
I found this: http://bl.ocks.org/mbostock/1129492
But it doesn't work when you want the svg to be responsive to the browser window size:
svg = d3.select("#network").append("svg")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMidYMid meet");
Since the height and width are the units of the svg now, we need to account for when the height and width aren't the same as when initialized. Note that for this example, height = width = 800. This example needs to be alters if height != width.
var currentWidth = $("svg").width();
@ccorcos
ccorcos / gist:9675658
Created March 20, 2014 22:47
Meteor scoping
I was having an issue that a function in another file wasn't seeing a variable in another file:
http://stackoverflow.com/questions/22545559/javascript-design-patterns-with-meteor/22546366
file scope
var x = 10;
meteor global scope
x = 10;
@ccorcos
ccorcos / gist:9677078
Last active August 29, 2015 13:57
Bootstrap 2 typeahead with Meteor
I spent way too long trying to get this to work.
Template.search.events({
'keyup input#search': function(e, t) {
if (e.keyCode == 13) {
// if the user presses enter to select a typeahead
var node = Nodes.findOne({
title: e.target.value
});
@ccorcos
ccorcos / gist:10683090
Created April 14, 2014 21:13
simple python exception
class MyException(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
if True:
raise MyException("fucked up")
@ccorcos
ccorcos / gist:10774818
Created April 15, 2014 21:07
how to check for a python package version at runtime
import pkg_resources
def version(packageName):
return pkg_resources.get_distribution(packageName).version
package = "matplotlib"
print package + ": " + version(package)
@ccorcos
ccorcos / smallNumbers.py
Created April 15, 2014 21:52
How to take the sum of very small numbers, a + b + c + ..., when you only have log(a), log(b), log(c), and exp(log(a)) = 0.0 due to lack of precision. What we get in return is log(a + b + c + ...)
from pylab import *
# Some Log rules:
# a*b = log(a) + log(b)
# a/b = log(a) - log(b)
# a**b = b*log(a)
# exp(log(x)) = log(exp(x)) = x
#
# The Trick:
# log(a + b) = log(a + b) - log(c) + log(c)
@ccorcos
ccorcos / gist:11129559
Created April 21, 2014 01:10
random integer in range
function randomInRange(min,max) {
var random = Math.floor(Math.random() * (max - min + 1)) + min;
return random;
}
@ccorcos
ccorcos / okcancel.js
Created April 21, 2014 03:06
ok cancel events for meteor
////////// Helpers for in-place editing //////////
// Returns an event map that handles the "escape" and "return" keys and
// "blur" events on a text input (given by selector) and interprets them
// as "ok" or "cancel".
okCancelEvents = function(selector, callbacks) {
var ok = callbacks.ok || function() {};
var cancel = callbacks.cancel || function() {};
var events = {};
events['keyup ' + selector + ', keydown ' + selector] =
@ccorcos
ccorcos / newNode.js
Created April 21, 2014 03:10
ok cancel event
Session.setDefault("parents", [])
Session.setDefault("children", [])
Template.main.rendered = function() {
// autoupdate the parents and children
Deps.autorun(function() {
nodeId = Session.get("editNode");
if (nodeId) {
parents = parentsOf(nodeId)
children = childrenOf(nodeId)