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 / 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 / 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()
from foo.forms import MyForm
def myview(request):
form = MyForm()
if request.POST:
form = MyForm(request.POST)
if form.is_valid():
# Do something with the contents, perhaps save the form
form.save() # You can call this sometimes.
return render_to_response("success page...", {})
@omarish
omarish / gist:3080200
Created July 10, 2012 00:42
Priceonomics Puzzles

Priceonomics Programming Puzzle, Summer 2012

We love solving puzzles at Priceonomics. We also like meeting people who like to solve puzzles. Here are two interesting puzzles we've faced at some point in the past months that we'd like to share with you.

If you have any questions, contact omar@priceonomics.com.


Puzzle 1: Heatwave