Skip to content

Instantly share code, notes, and snippets.

View hanksudo's full-sized avatar
Follow your passion.

Hank Wang hanksudo

Follow your passion.
View GitHub Profile
hanksudo / linebyline.js
Last active August 29, 2015 13:55
Read data line by line and turn in to JSON
View linebyline.js
var fs = require('fs');
var readline = require('readline');
var stream = require('stream');
var instream = fs.createReadStream('test.txt', 'utf8');
var outstream = new stream;
var rl = readline.createInterface(instream, outstream);
var obj = {}
rl.on('line', function (line) {
hanksudo /
Created April 11, 2014 11:52
SSH without
sudo curl "" -o /usr/bin/ssh-copy-id
sudo chmod +x /usr/bin/ssh-copy-id

ssh-copy-id -i ~/.ssh/ user@host
ssh-copy-id -i ~/.ssh/ "user@host -p 8129" 
hanksudo /
Created May 8, 2014 09:46
Checkout remote branch
git checkout --track origin/daves_branch
hanksudo / keybind.sublime-map
Last active August 29, 2015 14:01
Mako translate warp with selection
View keybind.sublime-map
{ "keys": ["alt+super+t"], "command": "insert_snippet", "args": {"name": "Packages/User/mako-translate.sublime-snippet"} }
hanksudo / Perform an operation on each dictionary
Created May 15, 2014 07:48
Perform an operation on each dictionary value
View Perform an operation on each dictionary
my_dict.update((x, y*2) for x, y in my_dict.items())
hanksudo / Sort list by
Created May 15, 2014 07:49
Sort list by key
View Sort list by
from operator import itemgetter
newList = sorted(listToSort, key=itemgetter('key'))
hanksudo /
Created June 17, 2014 14:11
Mount NTFS USB to read / write

Step 1

$ mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
map -hosts on /net (autofs, nosuid, automounted, nobrowse)
map auto_home on /home (autofs, automounted, nobrowse)
hanksudo /
Last active August 29, 2015 14:07
read CSV demo in Python 2.7
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
with open('sample_data.csv') as f:
for row in csv.reader(f):
print row
hanksudo /
Created October 20, 2014 06:46
Prime numbers
#!/usr/bin/env python
# -*- coding: utf-8 -*-
for n in range(2, 100):
for i in range(2, n):
if n % i == 0:
print '{} is a prime!'.format(n)
hanksudo /
Last active August 29, 2015 14:12
use setdefault to set dict default value for missing key.
# prefer
def _addErr1(errors, field, message):
errors.setdefault(field, []).append(message)
# not prefer
def _addErr2(errors, field, message):
if not field in errors:
errors[field] = []