Skip to content

Instantly share code, notes, and snippets.

View happysundar's full-sized avatar

Sundar Rajagopalan happysundar

View GitHub Profile
RAISE LOG 'Calculated income for user % is %', user.id, user.income;
DROP FUNCTION IF EXISTS remove_all();
CREATE FUNCTION remove_all() RETURNS void AS $$
DECLARE
rec RECORD;
cmd text;
BEGIN
cmd := '';
FOR rec IN SELECT
@happysundar
happysundar / filter_non_null_elems_from_array.sql
Created June 27, 2014 17:58
filtering out all non-null elements from an array in postgresql
WITH T1 AS (
SELECT
program_id,
ARRAY [alias_title,
alias_title_2,
alias_title_3,
alias_title_4] AS title_array
FROM movies
),
T2 AS (
set nocompatible " must be the first line
filetype on
filetype indent on
filetype plugin on
set laststatus=2
syntax on
set cursorline
set statusline=%<%f\%h%m%r%=%-20.(line=%l\ \ col=%c%V\ \ totlin=%L%)\ \ \%h%m%r%=%-40(bytval=0x%B,%n%Y%)\%P
" Allow saving of files as sudo when I forgot to start vim using sudo.
cmap w!! w !sudo tee > /dev/null %
@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,
@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 / 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 / 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 / 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 / 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