Skip to content

Instantly share code, notes, and snippets.

🐢

shunwen shunwen

🐢
View GitHub Profile
@shunwen
shunwen / chop_dos_date_time.bat
Created Jul 23, 2012
Chop %date% and %time% into useful tokens
View chop_dos_date_time.bat
REM get date from MM/DD/YYYY
SET YEAR=%DATE:~6,4%
SET MONTH=%DATE:~0,2%
SET DAY=%DATE:~3,2%
SET DATENOW=%YEAR%%MONTH%%DAY%
REM get time from hh:mm:ss.ss
SET hh=%TIME:~0,2%
IF %hh% LSS 10 SET hh=0%hh:~1,1%
SET mm=%TIME:~3,2%
@shunwen
shunwen / test.py
Created Dec 25, 2012
Python local variable declared in for loop stays alive outside the loop.
View test.py
bar = [1,2,3]
for foo in bar:
foo = foo + 1
break
print foo
@shunwen
shunwen / default_value_in_lambda.rb
Created May 13, 2013
Use default value in lambda
View default_value_in_lambda.rb
lambda { |*args| (args.first || default) }
@shunwen
shunwen / visit_url.rb
Created Jun 23, 2013
How to visit an url in Ruby
View visit_url.rb
# Not suitble for production
require 'open-uri'
open('http://github.com')
require 'net/http'
require 'uri'
uri = URI.parse "http://github.com/"
response = Net::HTTP.get_response uri
# Use system call
@shunwen
shunwen / is_a.rb
Created Jul 31, 2013
Use #is_a? instead of #===
View is_a.rb
omg = [1,2,3]
omg ===Array # => false
Array === omg # => true
omg.is_a? Array # => true
tmp = 1
tmp.is_a? Array #=> false
View roman_numeral.rb
module RomanNumeral
VALUES_OF_LETTERS = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 }
MODERN_ROMAN_PATTERNS = { CM: 900, CD: 400, XC:90, XL: 40, IX: 9, IV: 4 }
def self.i_to_old_roman int
i_to_roman int, VALUES_OF_LETTERS
end
def self.i_to_modern_roman int
@shunwen
shunwen / log_json_to_csv.rb
Created Sep 9, 2013
Replace Rails log with json format, then convert each json entries from the log file to csv.
View log_json_to_csv.rb
require 'json'
require 'csv'
SAMPLE_DATA = {"key1": "value1", "key2": "value2", "key3": "value3"}
headers = JSON.parse(SAMPLE_DATA).keys
input_lines = File.readlines "path/to/log/file"
parsed_line = 0
csv_file = CSV.open "path/to/file.csv", "w"
View vagrant_init_steps.sh
mkdir test_prj
cd test_prj
berks init
create Berksfile
create Thorfile
create .gitignore
run git init from "."
create Gemfile
create Vagrantfile
View knife_config_example.rb
# See http://docs.opscode.com/config_rb_knife.html for more information on knife configuration options
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
node_name "YOUR OPSCODE USERNAME" # The actual name will be set when provisioning
client_key "#{ENV['HOME']}/.chef/USERNAME.pem"
validation_client_name "ORGANIZATION_NAME-validator"
validation_key "#{ENV['HOME']}/.chef/ORGANIZATION_NAME-validator.pem"
chef_server_url "https://api.opscode.com/organizations/ORGANIZATION_NAME"
@shunwen
shunwen / Vagrantfile_example.rb
Last active Aug 26, 2018
Vagrant up with chef client
View Vagrantfile_example.rb
Vagrant.configure("2") do |config|
config.vm.hostname = "NODE NAME" # will be the node name on chef-server.
config.vm.box = "BOX NAME"
config.vm.box_url = "BOX IMAGE DOWNLOAD URL WHEN BOX DOES NOT EXIST"
config.vm.boot_timeout = 180 # Raise error when bootup takes too long
config.omnibus.chef_version = :latest # Use the latest chef version on this box
#config.vm.network :private_network, ip: "33.33.33.10" # THIS CAUSES SSH PROBLEM, remove it
You can’t perform that action at this time.