Skip to content

Instantly share code, notes, and snippets.

View anandology's full-sized avatar

Anand Chitipothu anandology

View GitHub Profile
@anandology
anandology / runserver.py
Created May 21, 2013 10:19
suggested improvements to funnel runserver.py
import sys
from website import app, models, init_for
init_for('dev')
models.db.create_all()
try:
port = int(sys.argv[1])
except (IndexError, ValueError):
port = 3000
app.run('0.0.0.0', port=port, debug=True)
@anandology
anandology / mysql2pgsql.py
Created May 20, 2013 11:13
Utility to translate MySQL queries to PostgreSQL
"""Script to translate MySQL query to PostgreSQL.
There are three main differences between MySQL and PostgreSQL.
* Postgres expects single-quotes for quoting values, but mysql allows
both single and double quotes. Need to change all the double
quoted values into single quotes.
* Postgres expects double-quotes for column names and mysql expects
back-quotes. Need to change all back quotes to double quotes.
"""Example to compute word frequency using simple map/reduce utility from openlibrary.
https://github.com/internetarchive/openlibrary/tree/master/openlibrary/data/mapreduce.py
"""
import sys
import logging
from openlibrary.data import mapreduce
class WordFrequecy(mapreduce.Task):
def map(self, key, value):
@anandology
anandology / pyexec.py
Last active December 15, 2015 04:59
Example to demonstrate exec in Python
def run(init_code, code, wrapper, tests):
env = {}
exec(init_code, env)
runTests = env['runTests']
# Now all the variables and functions defined in init_code will be available in env
# replace the marker in wrapper. This will be done by erb template in pythonmonk
@anandology
anandology / gist:3929961
Created October 22, 2012 06:24
BSNL DNS servers are crappy
# BSNL
# Primary DNS Server: 218.248.245.1
# Secondary DNS Server: 218.248.255.141
#
# In this case the secondary DNS server looks unhealthy.
# Taking too long to respond or failing to respond.
$ time dig @218.248.255.141 archive.org
; <<>> DiG 9.6-ESV-R4-P3 <<>> @218.248.255.141 archive.org
@anandology
anandology / map.rkt
Created September 4, 2012 13:14
Implementation of map function in Racket
#lang racket
; Tail-recursive implementation of map
(define (map f xs)
(define (map-iter xs result)
(if (null? xs)
(reverse result)
(map-iter (cdr xs) (cons (f (car xs)) result))))
(map-iter xs '()))
import sys
# Uses httplib2 by default.
# Pass "--requests" command-line arg to use requests.
if "--requests" in sys.argv:
import requests
requests.get("https://auth.hasgeek.com/").content
else:
import httplib2
@anandology
anandology / count.py
Created May 24, 2012 07:36
Code to demonstrate issues of using iterators and generators in multi-threaded applications
import threading
def count():
i = 0
while True:
i += 1
yield i
class Counter:
def __init__(self):
@anandology
anandology / datetime_racecondition.py
Created May 9, 2012 10:33
Program to demonstrate a race condition in datetime module
"""I suspect that there is a race condition in datetime module.
When I ran the following code in a multi-threading application:
datetime.datetime.strptime('20120509100335', "%Y%m%d%H%M%S")
I've noticed the following error in the log.
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
@anandology
anandology / 00-Protocol
Created March 22, 2012 05:54
Liveweb Proxy Design
Liveweb is a http proxy.
Request:
GET http://www.example.com/foo.html HTTP/1.1
Header1: Value1
Header2: Value2
Response: