Skip to content

Instantly share code, notes, and snippets.

@daleobrien
daleobrien / gist:3000515
Created June 27, 2012 00:50
Function to print nicely formatted tables

Function to print ascii tables out.

 wb = Workbook()
 wb.country_code = 61

 data = [['a','b','c'],[1,2,3],[4,5,6]]
 write_sheet(wb, data, "test_sheet", print_to_screen=True)  # add one sheet
@daleobrien
daleobrien / gist:3001403
Created June 27, 2012 04:20
Python SQL framework

Basic processing

import psycopg2
from prettytable import PrettyTable

conn = psycopg2.connect("dbname=databasename user=postgres")
cur = conn.cursor()
conn.autocommit = True
@daleobrien
daleobrien / weighted_pick.py
Created October 1, 2012 03:23
Weight Randon Choice
# from http://glowingpython.blogspot.com.au/2012/09/weighted-random-choice.html
from numpy import cumsum, sort, sum, searchsorted
from numpy.random import rand
from pylab import hist,show,xticks
def weighted_pick(weights,n_picks):
"""
Weighted random selection
returns n_picks random indexes.
@daleobrien
daleobrien / round_to_signigicant_digits.py
Last active December 14, 2015 01:58
Round a number by a number of significant digits
from math import log10, floor
def round_sig(x, sig=2):
'''
>>> round_sig(0.0232)
0.023
>>> round_sig(0.0232, 1)
0.02
>>> round_sig(1234243, 3)
from random import randrange
from datetime import timedelta, datetime
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
@daleobrien
daleobrien / Iterate_by_month.py
Last active December 14, 2015 04:18
Iterate between two months between 2 dates. Note some months will may have less days than the first month, in that case, the last day of the month will be returned.
#!/usr/bin/env python
from datetime import date
from calendar import monthrange
def by_month(start, end):
day_of_month = start.day
@daleobrien
daleobrien / gist:5140204
Last active December 14, 2015 19:58
Postgresql SQL statement to generate a range of days between 2 dates
SELECT
'2010-01-01'::DATE + day - 1 AS day
FROM
generate_series(1, '2011-01-01'::DATE - '2010-01-01'::DATE) AS day;
@daleobrien
daleobrien / gzip_decompress.cpp
Created March 13, 2013 09:52
gzip decompression, in memory
namespace io = boost::iostreams; //<-- good practicetypedef
std::vector<char> buffer_t;
void CompressionUtils::Inflate(const buffer_t &compressed, buffer_t &decompressed){
io::filtering_ostream os;
os.push(io::gzip_decompressor());
os.push(io::back_inserter(decompressed));
io::write(os, &compressed[0], compressed.size());
}
@daleobrien
daleobrien / .django_bash_completion.sh
Last active December 15, 2015 03:19
Allows django tab autocompletion
# #########################################################################
# This bash script adds tab-completion feature to django-admin.py and
# manage.py.
#
# Testing it out without installing
# =================================
#
# To test out the completion without "installing" this, just run this file
# directly, like so:
#
@daleobrien
daleobrien / .inputrc
Created April 17, 2013 22:34
This allows you to search through your history using the up and down arrows … i.e. type "cd /" and press the up arrow and you'll search through everything in your history that starts with "cd /".
"\e[A": history-search-backward
"\e[B": history-search-forward
set show-all-if-ambiguous on
set completion-ignore-case on