Skip to content

Instantly share code, notes, and snippets.

@swaroopch
swaroopch / treeiterator.py
Created June 23, 2010 05:59
Implement a tree iterator without using a stack
def eachnode(tree, n=0):
if n == len(tree):
raise StopIteration
yield tree[n] ## The method of access is specific to the data structure
for subnode in eachnode(tree, n+1):
yield subnode
if __name__ == '__main__':
tree = [10,20,30] ## Assuming array for test purposes, can be a real tree.
@swaroopch
swaroopch / flattener.py
Created July 20, 2010 07:45
Wondering of a clean way to flatten a list (multiple levels)
#!/usr/bin/env python
def flatten(array):
out = []
def real_flatten(array):
for item in array:
if isinstance(item, (list, tuple, set)):
real_flatten(item)
else:
out.append(item)
@swaroopch
swaroopch / restructuredtext_helper.vim
Created August 3, 2010 14:15
Making it easy to create headings in ReStructuredText syntax, in Vim
if has('ruby')
function! Heading(char)
ruby <<EOF
buffer = VIM::Buffer.current
new_line = VIM::evaluate("a:char") * buffer.line.length
buffer.append(buffer.line_number, new_line)
EOF
endfunction
command H1 call Heading('=')
@swaroopch
swaroopch / open_filename_in_tab.vim
Created August 6, 2010 06:18
In Vim, command to open a filename under the cursor in a new Vim tab (or if URL, open in browser)
if has('python') " Assumes Python >= 2.6
" Quick way to open a filename under the cursor in a new tab
" (or URL in a browser)
function! Open()
python <<EOF
import re
import platform
import vim
@swaroopch
swaroopch / flask-boilerplate-tmux.bash
Created December 5, 2010 07:00
A command that scripts a tmux session
#!/bin/bash
function flask-boilerplate-tmux
{
# https://github.com/swaroopch/flask-boilerplate
BASE="$HOME/code/flask-boilerplate"
cd $BASE
tmux start-server
tmux new-session -d -s flaskboilerplate -n model
@swaroopch
swaroopch / client.py
Created February 3, 2012 06:48
Two Legged OAuth API server (not working)
#!/usr/bin/env python
import oauth2 as oauth # pip install oauth2
parameters = {
'oauth_version' : '1.0',
'oauth_nonce' : oauth.generate_nonce(),
'oauth_timestamp' : oauth.generate_timestamp(),
}
@swaroopch
swaroopch / output.txt
Created February 15, 2012 20:25
Receive and close web request ASAP, process data later
→ python server.py
Run after this server starts : curl http://127.0.0.1:5000/?test=1
[2012-02-15 12:24:34,716] [werkzeug] [INFO] * Running on http://127.0.0.1:5000/
[2012-02-15 12:24:34,717] [werkzeug] [INFO] * Restarting with reloader
Run after this server starts : curl http://127.0.0.1:5000/?test=1
[2012-02-15 12:24:36,459] [root] [INFO] Received data ImmutableMultiDict([('test', u'1')])
[2012-02-15 12:24:36,460] [root] [INFO] Closing connection!
[2012-02-15 12:24:36,460] [werkzeug] [INFO] 127.0.0.1 - - [15/Feb/2012 12:24:36] "GET /?test=1 HTTP/1.1" 200 -
[2012-02-15 12:24:36,460] [root] [INFO] In separate process, doing something with data {'test': [u'1']}
@swaroopch
swaroopch / TestJedis.java
Created April 17, 2012 13:58
Testing Jedis (Java Redis API) for parallel requests
// https://github.com/xetorthio/jedis
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@swaroopch
swaroopch / core.clj
Created September 26, 2012 11:23
Figuring out how to use Monger (Clojure MongoDB library) with multiple databases
(ns scratchpad.core
(:require [monger.core :as mg]
[monger.collection :as mc]
[monger.query :as mq]))
(def local-mongodb
(.getDB (mg/connect-via-uri! "mongodb://127.0.0.1:27017/local") "local"))
@swaroopch
swaroopch / expose.sh
Created November 8, 2012 07:03
(not working) How to expose a local server to the outside world via an EC2 server?
# Before: I have a local Java or Django server running on port 8000
function expose_port {
local_port=$1
shift
ssh -vvv -i "$HOME/.ssh/aws.pem" -N -R "9000:localhost:8000" "ubuntu@ec2-12-34-56-789.compute-1.amazonaws.com"
}