Skip to content

Instantly share code, notes, and snippets.

@sivagao
sivagao / douban_lib_info_mashup_plugin.js
Created October 26, 2012 22:08
javascript , douban & lib info mashup , which use the chrome plugin's permissions to solve the cross origin(跨域问题)
function check() {
var a = location.href;
a = a.search("book.douban.com/subject");
if(a == -1) {
checkJun()
} else {
checkDouban()
}
}
function checkDouban() {
@sivagao
sivagao / Python strategy pattern.py
Created November 3, 2012 10:39
Python: strategy pattern + function2method using types.MethodType
# sivagao
# 2012年11月3日
class StrategyExample:
def __init__(self, func=None):
self.name = 'StrategyExample 0'
if func:
self.execute = types.MethodType(func, self)
def execute(self):
@sivagao
sivagao / microtemplate.js
Created November 5, 2012 14:06
micro template - quickly execute js in template string by john resing
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
var cache = {};
this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
@sivagao
sivagao / rake_learning.rb
Created November 7, 2012 01:41
learning Rake@Ruby
=begin
in our Rakefile, we create a file task entry named “passwd”.
This says the goal of this task is to create a file name “passwd”.
The contents of “passwd” depend on the contents of a file containing a list of our users. Let’s call this file “userlist”.
=end
file "passwd" => ["userlist"] do
pwds = read_passwords
users = read_users userlist
@sivagao
sivagao / using builder,GD,Dir to build file.rb
Created November 7, 2012 03:02
#practical ruby for system administrator practical ruby for system administrator #使用builder写xml(html), 使用GD生成图片,使用file.open 生成重复内容,Dir对系类文件操作
require "builder"
builder = Builder::XmlMarkup.new
page = builder.html do |html|
html.head { |head| head.title("Users") }
html.body { |body| body.a("bob", "href" => "b1") }
end
require "GD"
image = GD::Image.new(100, 100) # create an empty canvas, 100 pixels square
@sivagao
sivagao / using_file_safely_with_lock.rb
Created November 7, 2012 03:38
#practical ruby for system administrator #practical ruby for system administrator - 用聪明的方式构建文件 - 安全的使用文件,通过File::LOCK_EX,等
File.open("/tmp/foo", "w") do |f|
f.flock(File::LOCK_EX) # 取得互斥锁,如果未得,等待block,直到获取成功
f.puts "Locking is the key to ... pun interrupted"
f.flock(File::LOCK_UN)
end
File.open("/tmp/foo", "w") do |f|
@sivagao
sivagao / Using memcache, mysql (RMDB), ORM_ActiveRecord.rb
Created November 7, 2012 07:25
Using memcache, mysql (RMDB), ORM_ActiveRecord #practical ruby for system administrator
# using memcache
memcached -d -m 1024 -l 0.0.0.0 -p 11211
cached_obj = Memcache.new('localhost:11211', :readonly => true)
cached_obj.request_timeout = 10
cached_obj = Memcache.new('localhost:11211', "mem2.example.com:11211")
zoo = Memcache.new("localhost:11211")
zoo[:donkeys] = ["siva","siva","siva"*4]
@sivagao
sivagao / writting test, rake, document with rdoc.rb
Created November 7, 2012 09:46
writting test, rake, document with rdoc #practical ruby for system administrator
task :default => :test
task :test do
ruby "tests/test1.rb"
ruby "tests/test2.rb"
end
@sivagao
sivagao / Ruby: clean data, present the data with graph and chart(graphviz,scruffy).rb
Created November 7, 2012 09:48
clean data, present the data with graph and chart(graphviz,scruffy) #practical ruby for system administrator
# attempting to present data without thought as to the analysis often gives rise to
# presentations that are nothing more than stylish gloss on a stinky foundation.
# 其实这一章没有太多好用的类库介绍,基本是使用。ruby的使用
# 回去可以整理下。
# Listing 9-8. Storing the Array of Logins in a Timestamped File for Later Retrieval
require "fileutils"
require "remote_host"
@sivagao
sivagao / Ruby: using Timeout, TCPServer, socket, net,ssh, pcaplet(packet analysis).rb
Created November 7, 2012 09:49
using Timeout, TCPServer, socket, net,ssh, pcaplet(packet analysis)/http#practical ruby for system administrator
require "socket"
socket = TCPSocket.open("www.theonion.com", "80")
TCPSocket.open("www.theonion.com", 80) do |socket|
socket.puts "GET / HTTP/1.0\n\n"
puts socket.read
end