code. hack it.
I’m sitting in a hotel lobby in London writing this blog post with my second “first 30 minutes of wifi are free” :) I’ll admit that 3 GBP for 24 hours wifi wasn’t in fact too abusive but… I hate paying for things I could get for free. So after a couple quick tests (I have 2 devices, a laptop and a wifi enabled cell phone) I realized these facts: the information that’s input on... Read more...
A few patterns Ive landed while using angularjs. Organizing your resources with a factory and $resource: angular.factory('API', ['$resource', function($resource){ return { book: $resource('/api/book/:id', {id: '@id'}, {}), config: $resource('/api/config/') }; }]); The edit / cancel pattern: angular.controller('BookCtrl', ['$scope', 'API', function($scope, API){ $scope.config = null; $scope.books = []; $scope.selectedBook = null; $scope.selectedBookEdit = null; $scope.showBookEditor = false; API.config.get(function(c){ $scope.config = c; }); API.book.query(function(books){ $scope.books = books; }); $scope.selectBook = function(b){ $scope.selectedBook = b; $scope.selectedBookEdit = angularjs.copy(b); $scope.showBookEditor... Read more...
Getting rid of your for `for-loops` in javascript. var original = [1,2,3,4,5,6]; var i, newList = []; for (i = 0; i < original.length; i++) { newList.push(original[i] * 2); } var original = [1,2,3,4,5,6]; var newList = original.map(function(o){ return o * 2; }); Iterating over an objects keys var obj = {a: 1, b: 2, c: 3}, k, v; for (k in obj) { if (obj.hasOwnProperty(k)) { v = obj[k]; // ... } } var... Read more...
Keep it hidden. You shouldn't pollute the global scope unless necessary. (function(){ var your_script; })(); Using the same 'immediately invoked function' you can get a hold of an undefined variable which is `undefined` (function(undefined){ var your_script; // now you compare for `undefined` using: if (your_script === undefined){ // ... } })(); Map and Reduce var l = [21,49,80,43,59]; var doubled = l.map(function(d){ return d*2; }); var max_value = doubled.reduce(function(a,b){ return a > b ? a... Read more...
The important piece of info I've been missing is that by default d3 uses the index of the element as it's ID. To use a custom ID you need to pass a function to the data() method that returns that actual ID of the element: var data = [ {'id': 1, 'value': 3324}, {'id': 2, 'value': 32432}, {'id': 3, 'value': 98}, // ... ]; var bars = svg.selectAll("rect") .data(data, function(d, i) { return d.id; });... Read more...
$(document).ready(function(){ var div = $('<div id="viz"></div>') .css("width", "100%") .css("height", "100%") .css("z-index", -1) .css("position", "fixed") .css("top", 0) .css("left", 0); $("body").append(div); var width=1000, height=1000, xcount=30, ycount=30, cellw=width/xcount, cellh=height/ycount; var xscale = d3.scale.linear() .domain([0, xcount]) .range([0, width]); var yscale = d3.scale.linear() .domain([0, ycount]) .range([0, height]); var svg = d3.select("#viz").append("svg") .attr("id", "svg") .attr("height", "100%") .attr("width", "100%") .attr("preserveAspectRatio", "xMinYMin slice") .attr("viewBox", "0 0 " + width + " " + height); var color = d3.scale.category20c(); function update(data) { // join... Read more...
This is just a collection of commands I find myself looking up again and again. Hopefully by putting them here, they'll sink in :) git remote set-url origin /vagrant git remote set-url origin git@github.com:<username>/<repo>.git git checkout --track origin/<other-branch-name> git checkout -b new-branch git commit -m "changes" # new changes are merged into master inbetween time git checkout new-branch git rebase master Vim folding: # with visual select zf za Coverage: coverage run --source='.' --omit='*migrations*','*tests*' ./manage.py... Read more...
l = [ (k, v) for k, v in d.items() ] Read more...
Link your public ssh key on your github account: cat ~/.ssh/id_rsa.pub Save it with "Add SSH key" here: https://github.com/settings/ssh. This will allow you to push using your ssh key. Clone your repo: git clone git@github.com:<username>/<reponame>.git For example: git clone git@github.com:sheanmassey/sheanmassey.github.io.git See diff of unstaged changes: git diff git diff | vim - Committing all modified files: git commit -a -m "this is my commit message" Pushing your changes to github: git push You'll almost always... Read more...