brendano /.pythonrc

#!/usr/bin/env python
# need to set PYTHONSTARTUP=~/.pythonrc  in shell to get this to run
# I also use in ipython, via "execfile ~/.pythonrc" in ~/.ipython/ipythonrc
from __future__ import division
import sys, os, atexit

try:
    __IPYTHON__
    os.environ['PAGER'] = "less -r"

except NameError:
    try:
        import readline
    except ImportError:
        print "Module readline not available."
    else:
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    histfile = os.environ["HOME"] + "/.python_history"
    #looks like it and ipython will clobber each other
    #histfile = os.environ["HOME"] + "/.ipython/history"
    try:
        readline.read_history_file(histfile)
    except IOError:
        pass
    atexit.register(readline.write_history_file, histfile)
    del histfile

# nice to have stdout unbuffered.  but needs to be idempotent
if "" not in str(sys.stdout):
  sys.stdout = os.fdopen(1,'w',0)


## I think it's nice to have lots of crap in the global namespace by default

from urllib2 import urlopen
import re
from copy import copy, deepcopy
from pprint import pprint
from datetime import datetime,timedelta
from collections import defaultdict
import itertools
from itertools import *
import time
import random
import subprocess

for s in ("from BeautifulSoup import BeautifulSoup",):
  try:
    exec s
  except ImportError as e:
    print e

def myjoin(seq, sep=" "):
  " because str.join() is annoying "
  return sep.join(str(x) for x in seq)

def mate(file):
  return subprocess.call(("mate", file))

def uniq_c(seq):
  ret = defaultdict(lambda:0)
  for x in seq:
    ret[x] += 1
  return dict(ret)

def flatten(iter):
  return list(itertools.chain(*iter))

def flip(pairs):
  return [(y,x) for x,y in pairs]

T = True
F = False