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 / 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 / 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 / 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 / gist:19313e7dcdfde73fcbd5
Created May 23, 2014 22:13
connect to postgresql database
# Import Libraries
library(RPostgreSQL)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#' Connect to PostgreSQL database
#'
#' This takes a database name and then connects to it.
#' @param dname The name of the database that houses the dlhs data
@00krishna
00krishna / template_plpsql_function
Last active August 29, 2015 14:01
template for plpsql function
-- Function: template_function(OUT result boolean)
-- DROP FUNCTION template_function(OUT result boolean)
CREATE OR REPLACE FUNCTION template_function(OUT result boolean)
AS
$BODY$
DECLARE
BEGIN
@00krishna
00krishna / pandas_read_csv
Created April 22, 2014 02:24
import csv to python by pandas
from pandas import read_csv
from urllib import urlopen
page = urlopen("http://econpy.pythonanywhere.com/ex/NFL_1979.csv")
df = read_csv(page)
print df
print df['Line']
@00krishna
00krishna / gist:9165405
Created February 23, 2014 01:42
Postgresql: if you are trying to convert a string to an integer, be careful of nulls. Here is a postgres solution.
SELECT
NULLIF(your_value, '')::int
# -*- coding: utf-8 -*-
"""
LICENSE: BSD (same as pandas)
example use of pandas with oracle mysql postgresql sqlite
- updated 9/18/2012 with better column name handling; couple of bug fixes.
- used ~20 times for various ETL jobs. Mostly MySQL, but some Oracle.
to do:
save/restore index (how to check table existence? just do select count(*)?),
finish odbc,
@00krishna
00krishna / filelist.py
Created February 17, 2014 23:53
Get list of files in directory
from os import listdir
from os.path import isfile, join
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
@00krishna
00krishna / linkgetter.py
Created February 15, 2014 23:21
Get all links in a web page
import re, urllib
htmlSource = urllib.urlopen("http://sebsauvage.net/index.html").read(200000)
linksList = re.findall('<a href=(.*?)>.*?</a>',htmlSource)
for link in linksList:
print link