Skip to content

Instantly share code, notes, and snippets.

@omarish
omarish / csv2json.py
Created November 11, 2010 00:52
Easily convert a CSV file to JSON.
#!/usr/bin/env python
import csv
try: import simplejson as json
except: import json
from optparse import OptionParser
def main():
usage = "usage: csv2json: %prog [options] arg"
parser = OptionParser()
parser.add_option("-i","--input",dest="input",help="input csv file")
@omarish
omarish / cumsum.js
Created December 16, 2010 23:47
simple cumulative sum in javascript.
cumsum = [];
j = [0,1,2,3,4];
for(var a=0;a<j.length;a++) {
if(a==0) cumsum[a] = j[0];
else cumsum[a] = cumsum[a-1] + j[a];
}
@omarish
omarish / googl.py
Created March 24, 2011 21:46
Shorten a URL using goo.gl
#!/usr/bin/python
from urllib2 import urlopen, Request
from urllib import quote
from simplejson import loads
def shorten(url):
u = urlopen(Request('http://goo.gl/api/url', 'url=%s' % quote(url), {'User-Agent':'toolbar'}))
a = loads(u.read())
return a['short_url']
@omarish
omarish / screen.rc
Created March 25, 2011 23:56
My Default screen.rc file for a django project. just run screen -c screen.rc and get to work!
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%w%{.rW}%n %t^{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
startup_message off
defscrollback 5000
altscreen on
autodetach on
sessionname mendel
bind ' ' windowlist -b
vbell off
@omarish
omarish / node.py
Created March 26, 2011 20:33
Simple JSON-Friendly Python Tree
We couldn’t find that file to show.
@omarish
omarish / handler.py
Created March 28, 2011 01:37
Parse any JSON data structure into suitable javascript objects.
import time, math
def dt_handler(obj):
if isinstance(obj, datetime) or isinstance(obj, date):
v = math.floor(time.mktime(obj.timetuple()))
return "DATE(%d)" % v
else: return repr(obj)
@omarish
omarish / gist:1107935
Created July 26, 2011 20:31
Use a custom firefox profile in Selenium RC2
#!/usr/bin/env python
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile(
profile_directory='/path/to/profile/directory/default/'
)
driver = webdriver.Firefox(firefox_profile=profile)
#!/usr/bin/env python
A1 = 'd64a84456adc959f56de6af685d0dadd' # "hard", according to google
B1 = '8d8a1b73876ca678cc3afa372e5199de'
import hashlib
m = hashlib.md5()
m.update('problems')
B2 = m.hexdigest()
print B2 == B1
#!/usr/bin/env python
import random
import string
import operator
from collections import defaultdict
class Client:
def __init__(self, body=None):
""" Initialize a client. An optional body can be passed as ``body``,
otherwise a random string with 100 letters will be created. """
@omarish
omarish / timer.py
Created January 26, 2012 22:03
Simple process timer in python.
from datetime import datetime
import time
class timer:
def __enter__(self):
self.start = datetime.now()
return self
def __exit__(self, type, value, traceback):
self.end = datetime.now()