Skip to content

Instantly share code, notes, and snippets.

@saivenkat
saivenkat / add_module_to_class_dynamic.rb
Created July 9, 2009 08:40
Include or Extend module dynamically
module Base
def call
"I am called"
end
end
class Child
def help
"Please help me"
end
#install libnotify-bin for this to work. Do a apt-get install libnotify-bin
def notify(title, message, urgency = 'normal')
command = "notify-send -u #{urgency} '#{title}' '#{message}'"
system command
end
@saivenkat
saivenkat / tail_recursion.rb
Created July 17, 2009 10:20
Tail recursion in ruby
def factorial_tail(number)
proc_fact = lambda do |acc, x|
if x <= 0
acc
else
proc_fact.call(x * acc, x - 1)
end
end
return proc_fact.call(1, number)
end
@saivenkat
saivenkat / infinite_list.py
Created October 25, 2009 13:29
infinite list
def increment():
i = 0
while True:
yield i
i += 1
for j in increment():
print i
if (j > 10) : break
@saivenkat
saivenkat / fibonacci_infinite_list.py
Created October 25, 2009 13:47
infinite fibonacci list
def fibonacci():
i = j = 1
while True:
result, i, j = i, j, i + j
yield result
for k in fibonacci():
print k
if (k > 100) : break
@saivenkat
saivenkat / post_to_twitter.py
Created November 5, 2009 17:23
Update twitter status
from httplib2 import Http
from urllib import urlencode
class TwitterClient:
def __init__(self, username, password):
self.username = username
self.password = password
def update_status(self, message):
client = Http()
@saivenkat
saivenkat / nb_load_timer.rb
Created November 29, 2009 10:35
A url timer using NeverBlock
require "neverblock"
require "net/http"
require "uri"
def load_timer(url, number_of_requests=50)
@pool = NB::Pool::FiberPool.new(number_of_requests)
start = Time.now
number_of_requests.times do
@pool.spawn do
request_url = URI.parse(url)
@saivenkat
saivenkat / event_couchdb.py
Created December 3, 2009 17:20
Eventlet CouchDb
import StringIO
from eventlet import coros
from eventlet.green import urllib2
class EventCouchDb():
def __init__(self, url):
self.url = url
def fetch(self, url, callback):
callback.write(urllib2.urlopen(url).read())
def get(self, callback):
@saivenkat
saivenkat / single.py
Created December 4, 2009 03:16
Singleton Pattern in Python
class HTTPClient(object):
_inst = None
def __new__(cls):
if cls._inst:
return cls._inst
else:
o = super(HTTPClient, cls).__new__(cls)
cls._inst = o
return o
@saivenkat
saivenkat / redis-node.js
Created December 10, 2009 14:33
Node.js Redis Client
//Sample usage
//var redis = require("./redis");
//var sys = require('sys');
//var c = redis.connect('0.0.0.0', 6379);
//c.addListener("connect", function (){
// c.hasKey("foss").addCallback(function(data) {
// sys.puts("Cool " + data);
// c.quit();
// });