Skip to content

Instantly share code, notes, and snippets.

@peterkeen
peterkeen / balance_sheet.pl
Created December 31, 2010 05:52
Calculates month-over-month and year-over-year balances using ledger and SQLite
#!/usr/bin/env perl
use strict;
use warnings;
use DBI;
my ($year, $month, $day) = get_date();
my @balances = (
@peterkeen
peterkeen / gist:808562
Created February 2, 2011 22:10
Add this to your [aliases] section in ~/.gitconfig
alias = "!sh -c '[ $# = 2 ] && git config --global alias.\"$1\" \"$2\" && exit 0 || echo \"usage: git alias <new alias> <original command>\" >&2 && exit 1' -"
@peterkeen
peterkeen / gist:840678
Created February 23, 2011 16:40
transcript
$ cat run
cat run
#!/bin/sh
nohup bash -c 'while true; do sleep 10; done' </dev/null &> sleeper.log &
echo $! > /tmp/sleeper.pid
;
$ sudo ./run
sudo ./run
[sudo] password for pak:
@peterkeen
peterkeen / gist:1025209
Created June 14, 2011 15:59
SQLAlchemy Session from DBAPI Connection
def session_from_connection(conn):
def passthrough_conn():
return conn
# note that the url here doesn't matter, but it has to parse correctly as a url
engine = create_engine('postgresql://foo@bar/abc', creator=passthrough_conn)
session = Session(bind=engine)
2011/07/05 * Eat At Joe's
Expenses:Dinner $50.00
Liabilities:Credit Card
; :reimbursable:
2011/07/06 * Drink At Fred's
Expenses:Drinks $25.00
Liabilities:Credit Card
; :reimbursable:
@peterkeen
peterkeen / gist:1138235
Created August 10, 2011 20:55 — forked from ErinCall/gist:1138231
{}.update
>>> foo = { 'a': 1, 'b': 2 }
>>> foo.update({'b': 444444, 'c': 3});
>>> foo
{'a': 1, 'c': 3, 'b': 444444}
>>> dict(bar='baz', **foo)
{'a': 1, 'c': 3, 'b': 444444, 'bar': 'baz'}
$ irb
irb
>> a = 1
a = 1
=> 1
>> a[0]
a[0]
=> 1
>> a[1]
a[1]
@peterkeen
peterkeen / gist:1548326
Created January 1, 2012 20:56
python "anonymous" functions
def some_anonymous_function(arg1, arg2):
print "%s %s" % (arg1, arg2)
def some_named_function(func, arg1, arg2):
func(arg1, arg2)
some_named_function(some_anonymous_function, arg1, arg2)
@peterkeen
peterkeen / query.sql
Created March 4, 2012 01:41
Complicated ledger query
-- Count the number of months where the 12 month moving average of after-tax expenses excluding travel (basically, normal ordinary expenses) went up, down, or didn't change
select
sum(case when pct_change > 0 then 1 else 0 end) as up,
sum(case when pct_change < 0 then 1 else 0 end) as down,
sum(case when pct_change = 0 then 1 else 0 end) as no_change
from (
select
xtn_month,
((expenses - prev) / prev) * 100 as pct_change
2012/02/29 * Medical Fund Transfer To Savings
[Assets:Checking] $4,000.00
[Assets:Funds:Medical]
2012/02/29 * Savings Deposit
Assets:Savings $4,000.00
Assets:Checking
2012/02/29 * Medical Fund Transfer To Savings
[Assets:Medical] $4,000.00