Skip to content

Instantly share code, notes, and snippets.

@tohyongcheng
tohyongcheng / persist_cache_to_disk.py
Last active June 20, 2016 05:29
Persisting a cache in Python to disk using a decorator
def persist_cache_to_disk(filename):
def decorator(original_func):
try:
cache = pickle.load(open(filename, 'r'))
except (IOError, ValueError):
cache = {}
atexit.register(lambda: pickle.dump(cache, open(filename, "w")))
def new_func(*args):
var nationalities = [
"Afghan",
"Albanian",
"Algerian",
"American",
"Andorran",
"Angolan",
"Anguillian",
"Antiguans",
"Argentinean",
# If a number is divisible by 3, return "Fizz".
# If a number is divisible by 5, return "Buzz".
# If a number is divisible by 3 and 5, return "FizzBuzz"
def fizzbuzz(x)
___
end
assert_equal fizzbuzz(3), "Fizz"
assert_equal fizzbuzz(50), "Buzz"
@tohyongcheng
tohyongcheng / Capfile
Created April 21, 2015 08:25
Example Capfile
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
@tohyongcheng
tohyongcheng / deploy.rb
Created April 21, 2015 03:22
Example of capistrano deploy.rb
# config valid only for current version of Capistrano
lock '3.3.5'
set :application, 'app_name'
set :repo_url, 'git@github.com:xxx/xxx.git'
# Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
@tohyongcheng
tohyongcheng / production.rb
Created April 21, 2015 02:53
Example of capistrano production.rb file
# server-based syntax
# ======================
# Defines a single server with a list of roles and multiple properties.
# You can define all roles on a single server, or split them:
# production server is xxx.xxx.xxx.xxx
server 'xxx.xxx.xxx.xxx', user: 'deployer', roles: %w{app web}
# server 'example.com', user: 'deployer', roles: %w{app db web}, my_property: :my_value
@tohyongcheng
tohyongcheng / nginx
Created April 21, 2015 02:45
/etc/rc.d/init.d/nginx
#!/bin/sh
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx="/opt/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
@tohyongcheng
tohyongcheng / routesdestroybug.txt
Created July 1, 2014 02:26
Reproduce Rails Destroy Bug (Mac OS)
export app_name="reproduce_rails_routing_bug"
echo $app_name
rails new ${app_name}
cd ${app_name}
rails g scaffold foo
cd config
sed -i '' '/^$/d' routes.rb
sed -i '' '/#/d' routes.rb
cd ..
rails destroy scaffold foo
@tohyongcheng
tohyongcheng / user.rb
Last active August 29, 2015 13:56
merge existing account when fb login
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
if user
return user
else
registered_user = User.where(:email => auth.info.email).first
if registered_user
registered_user.update_attributes(:provider => auth.provider, :uid => auth.uid, picture: (URI.parse(auth.info.image.split("?")[0] << "?width=200&height=200") if auth.info.image?) )
return registered_user
else