Skip to content

Instantly share code, notes, and snippets.

View happysundar's full-sized avatar

Sundar Rajagopalan happysundar

View GitHub Profile
@happysundar
happysundar / apache_rewrite.ini
Created February 11, 2014 09:52
Rewrite rule to transform part of the path to a query string key/value pair
RewriteEngine On
#
# This rewrite rule will change a URI from something like :
# http://foo.com/corpus/59/d0/400_59d022a8dcc082473fac85dbb219e724SongStream-ParadeofLights-ServerSide-640x765.png
# Notice this part of the URI ^^^ -- that becomes a query parameter...
# i.e, into http://foo.com/corpus/59/d0/59d022a8dcc082473fac85dbb219e724SongStream-ParadeofLights-ServerSide-640x765.png?width=400
#
#
@happysundar
happysundar / using schema to validate command line arguments.py
Created February 21, 2014 20:37
This snippet shows how to use https://github.com/halst/schema to validate command line arguments in a python script. You should use https://github.com/docopt/docopt to handle command line arguments
schema = Schema(
{
'--schema_dir':
And(
And(
os.path.exists,
os.path.isdir,
lambda s: os.access(s, os.R_OK),
error="The path '%s' should be an existing directory and be readable. Please check" % arguments['--schema_dir']
),
@happysundar
happysundar / postgres.md
Created February 24, 2014 03:37
get postgres up and running in OS X Mavericks
  1. brew cask install postgres_app

  2. In the current version of Postgres.app, the supplemental binaries are installed in /Applications/Postgres.app/Contents/Versions/9.3/bin, not in /Applications/Postgres.app/Contents/MacOS/bin. So, edit your .[bash|zsh]rc and add :

    export PSQL_PATH=/Applications/Postgres.app/Contents/Versions/9.3/bin

Followed by:

@happysundar
happysundar / brew_update.sh
Created February 24, 2014 06:15
update brew packages
brew update && brew upgrade brew-cask && brew cleanup
@happysundar
happysundar / ansible-playbook.sh
Created February 25, 2014 03:06
try an ansible playbook with a vagrant instance
ansible-playbook -i ../vagrant/vagrant_ansible_inventory_default -u vagrant -vvv --private-key=/Users/sundar/.vagrant.d/insecure_private_key ./playbook.yml
@happysundar
happysundar / check_env.py
Created February 27, 2014 20:45
check python version, virtual env presence
def check_env():
(py_major, py_minor, py_patchlevel) = platform.python_version_tuple()
if py_major != '2' and py_minor != '7':
exit("This script should be run in python version 2.7.X. Current version of python is %s" % platform.python_version())
if not hasattr(sys, 'real_prefix'):
exit("This script should be run within a virtualevn. Have you run the 'workon <virtual_env_name>' command?")
__filename = inspect.getframeinfo(inspect.currentframe()).filename
__path = os.path.dirname(os.path.abspath(filename))
# And then, do something like:
if not os.path.isabs(arguments['--bound-spec']):
arguments['--bound-spec'] = os.path.normpath(os.path.join(__path, arguments['--bound-spec']))
@happysundar
happysundar / staticmethod_vs_classmethod.md
Created March 5, 2014 07:24
classmethod vs staticmethod in python

@staticmethod vs @classmethod

If you intend to refer to class variables within a @staticmethod, then you end up using the actual name of the enclosing class. This makes the method un-inheritable only in the cases you refer to the class variables, and the variables happen to be overriden in the subclass - it can still be called from the derived class / derived class instance. In other words, @staticmethods may not behave like proper, polymorphic methods when class variables are overridden.

class Base(object):
	class_vars = ['A','B','C']
	
	@staticmethod
@happysundar
happysundar / graphs.py
Created March 11, 2014 23:38
Implementation of a directed graph, and associated DFS, Cycle detection algorithms
class DiGraph(object):
def __init__(self):
super(DiGraph, self).__init__()
self.g = dict()
def add_node_and_neighbors(self, node, neighbors):
assert node
assert isinstance(node, basestring)