Skip to content

Instantly share code, notes, and snippets.

View happysundar's full-sized avatar

Sundar Rajagopalan happysundar

View GitHub Profile
@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)
@happysundar
happysundar / subpackage_import.py
Created March 19, 2014 21:57
import subpackage
#
# use this if you want to include modules from a subfolder
#
__ = os.path.abspath(sys.argv[0])
while os.path.dirname(__) != __:
if os.path.exists(os.path.join(__, 'common', '__init__.py')):
sys.path.insert(0, __)
break
@happysundar
happysundar / input_validation.py
Created March 19, 2014 22:20
input validation in python using Schema
schema = Schema(
{
'rovi_data_dir':
And(
And(
os.path.exists,
os.path.isdir,
lambda s: os.access(s, os.R_OK),
error="The path to rovi data '%s' should be an existing directory and be readable. Please check" % arguments['<rovi_data_dir>']
),
@happysundar
happysundar / postgres_backup_restore.sh
Created May 15, 2014 03:31
Commands to backup and restore postgres database
/usr/pgsql-9.3/bin/pg_dump -U <user_name> -d <db_name> -f <backup_file>.sql -h <db_host>
/usr/pgsql-9.3/bin/psql -U <user_name> -d <db_name> -f <backup_file>.sql -h <db_host>
@happysundar
happysundar / create_movies_table.sql
Created June 14, 2014 20:06
Creating a plpgsql function
DROP FUNCTION IF EXISTS get_image_records( BIGINT ) CASCADE;
CREATE OR REPLACE FUNCTION
get_image_records(input_program_id BIGINT)
RETURNS SETOF JSON STABLE
AS $$
BEGIN
RETURN QUERY
WITH T2 AS (
SELECT
file_url :: TEXT,