Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
havin a normal one

Dan Nguyen dannguyen

💭
havin a normal one
View GitHub Profile
@dannguyen
dannguyen / strip-instagram-data.py
Created Aug 26, 2015
strip-instagram-data-ids: remove some identifier information from a list of Instagram images
View strip-instagram-data.py
# convenience script to obfuscate, but not completely redact, *some*
# identifier information from a list of Instagram images, such as
# all likes and commenters. Image unique ID is left in so original data
# can be recovered
import json
from random import choice
from string import ascii_letters, digits
FNAME = './examples/images.json'
jdata = json.load(open(FNAME))
@dannguyen
dannguyen / index.html
Last active Aug 27, 2015
Modified version of bl.ocks.org/mbostock/5735770
View index.html
<!DOCTYPE html>
<meta charset="utf-8">
<!--
copied from Mike Bostock:
http://bl.ocks.org/mbostock/5735770
http://bost.ocks.org/mike/example/#1
-->
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.19/topojson.min.js"></script>
@dannguyen
dannguyen / erb-to-slim-kindof.rb
Last active Aug 29, 2015
Prepare ERB for SLIM
View erb-to-slim-kindof.rb
# Not great, but better than doing everything by hand...
def prepare_erb_for_slim(body)
# remove all %>
body.gsub!(/%> */, '')
# convert <%-/= to -
body.gsub!(/<%(-|=) */, '\1 ')
@dannguyen
dannguyen / loofah-and-nokogiri-whitespace.rb
Created Feb 14, 2014
How to use the Loofah library with a Nokogiri node to preserve line breaks between block elements
View loofah-and-nokogiri-whitespace.rb
npage = Nokogiri::HTML(html)
h[:text] = Loofah.fragment(npage.search('.displaytext').to_html).to_text
@dannguyen
dannguyen / ruby-wrap-graphicsmagick-blue.rb
Created Feb 18, 2014
Ruby invoking graphicsmagick to make images blue because I'm too lazy to learn shell loops
View ruby-wrap-graphicsmagick-blue.rb
Sector.all.each do |sector|
files = Dir.glob("./**/skift/#{sector.slug}.png").reject{|f| f =~ /-blue\.\w{3,4}$/}
files.each do |fname|
cmd = "convert #{fname} -fill '#2b74a5' +opaque darkblue #{fname}"
puts cmd
`#{cmd}`
end
end
@dannguyen
dannguyen / sublime.text.user-settings.json
Last active Aug 29, 2015
Setting the defaults for my Sublime Text 3
View sublime.text.user-settings.json
{
"always_show_minimap_viewport": true,
"auto_complete_commit_on_tab": true,
"auto_find_in_selection": true,
"binary_file_patterns":
[
"*.log",
"*.jpg",
"*.jpeg",
"*.png",
@dannguyen
dannguyen / sublime-text-3.keys.move-character.json
Created Mar 16, 2014
Sublime Text 3 keyboard shortcut: Mapping Shift-Tab to move outside a right closing bracket/braces/quote-mark
View sublime-text-3.keys.move-character.json
[
{
"keys": ["shift+tab"], "command": "move", "args": {"by": "characters", "forward": true}
}
]
@dannguyen
dannguyen / value-lengths-dist-for-field.sql
Last active Aug 29, 2015
find distribution of value lengths for a given field
View value-lengths-dist-for-field.sql
SET @field_name = "place_of_service";
SET @tablename = "payments";
SET @query = CONCAT('SELECT ', @field_name, ', count(1), length(', @field_name, ') AS lt FROM ', @tablename,' GROUP BY ', @field_name, ' ORDER BY lt desc');
PREPARE stmt FROM @query;
EXECUTE stmt;
@dannguyen
dannguyen / minimal-rspec-setup.rb
Created Apr 3, 2014
Minimal number of files to run `rspec` from command-line in your folder
View minimal-rspec-setup.rb
# Needed files:
# - Rakefile
# - Gemfile
# - spec/spec_helper
#####
## Gemfile
source 'https://rubygems.org'
gem 'rspec'
View ruby-enumerables-sugar.rb
stores.each do |store|
if country == store[:name]
store_id = store[:id]
break
end
end
# http://ruby-doc.org/core-1.9.3/Enumerable.html
### With #select