Skip to content

Instantly share code, notes, and snippets.

View 00krishna's full-sized avatar

Krishna Bhogaonker 00krishna

  • Student, UCLA
  • Los Angeles, CA, USA
View GitHub Profile
@00krishna
00krishna / select_cols.sql
Created May 24, 2014 07:39
select all columns in a table
SELECT attrelid::regclass, attnum, attname
FROM pg_attribute
WHERE attrelid = 'myschema.mytable'::regclass
AND attnum > 0
AND NOT attisdropped
ORDER BY attnum;
@00krishna
00krishna / imageview
Created June 4, 2014 08:59
view png image in ipython notebook
from IPython.core.display import Image
Image(filename='test.png')
@00krishna
00krishna / get_adjacent_cells
Created January 21, 2015 03:12
find adjacent cells in 2d grid
def get_adjacent_cells( self, x_coord, y_coord ):
result = {}
for x,y in [(x_coord+i,y_coord+j) for i in (-1,0,1) for j in (-1,0,1) if i != 0 or j != 0]:
if (x,y) in grid.cells:
result[(x,y)] = grid.cells[(x,y)]
@00krishna
00krishna / pdfxtract.py
Last active August 29, 2015 14:19 — forked from jmcarp/pdfxtract.py
"""
Extract PDF text using PDFMiner. Adapted from
http://stackoverflow.com/questions/5725278/python-help-using-pdfminer-as-a-library
"""
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter#process_pdf
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
@00krishna
00krishna / progressive_progressbar.py
Last active September 7, 2015 17:31
Python progressive progressbar working example
__author__ = 'krishnab'
from time import sleep
from blessings import Terminal
from progressive.bar import Bar
from progressive.tree import ProgressTree, Value, BarDescriptor
@00krishna
00krishna / optional_decorator_function.py
Created December 9, 2015 06:43
create an optional decorator
class OptionalDecoratorDecorator(object):
def __init__(self, decorator):
self.deco = decorator
def __call__(self, func):
self.deco = self.deco(func)
self.func = func
def wrapped(*args, **kwargs):
@00krishna
00krishna / install_postgis.sql
Created December 28, 2015 21:28
setup postgis on a database
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
@00krishna
00krishna / st_intersect_query.sql
Created March 16, 2016 20:59
postgis update query where there is an intersection
update census_tracts_2014
set council_district = t1.dist
from (select council_districts.dist, census_tracts_2014.affgeoid, council_districts.geom
from census_tracts_2014 inner join council_districts
on
ST_Intersects(census_tracts_2014.geom, ST_Transform(council_districts.geom,4269))) t1
where
ST_Intersects(census_tracts_2014.geom, ST_Transform(t1.geom,4269));
@00krishna
00krishna / average4.sql
Created March 30, 2016 23:42
take the average of 4 numbers, guarding for null values.
CREATE OR REPLACE FUNCTION AVERAGE4 (
V1 NUMERIC,
V2 NUMERIC,
V3 NUMERIC,
V4 NUMERIC)
RETURNS NUMERIC
AS $FUNCTION$
DECLARE
COUNT NUMERIC;
TOTAL NUMERIC;
-- lower case table names -- the psql friendly and more reader-friendly way
SELECT 'ALTER TABLE ' || quote_ident(t.table_schema) || '.'
|| quote_ident(t.table_name) || ' RENAME TO ' || quote_ident(lower(t.table_name)) || ';' As ddlsql
FROM information_schema.tables As t
WHERE t.table_schema NOT IN('information_schema', 'pg_catalog')
AND t.table_name <> lower(t.table_name)
ORDER BY t.table_schema, t.table_name;
--generates something like this
--ALTER TABLE public."SPRINT" RENAME TO sprint;