Skip to content

Instantly share code, notes, and snippets.

@kenbellows
Last active August 14, 2019 14:26
Show Gist options
  • Save kenbellows/a1db106b0a3f54f274ec93a093c54039 to your computer and use it in GitHub Desktop.
Save kenbellows/a1db106b0a3f54f274ec93a093c54039 to your computer and use it in GitHub Desktop.
Python Interpreter Setup
"""
Custom evironment setup for the python interpreter
Usage:
1. Save to ~/.pythonrc (name doesn't matter, *rc is just convention)
2. Point the PYTHONSTARTUP environment variable to your file; e.g. for bash, add
the following to your ~/.bashrc or ~/.profile:
export PYTHONSTARTUP=~/.pythonrc
"""
### enable tab completion
import rlcompleter, readline
readline.parse_and_bind("tab: complete")
### import custom python utilities
import sys
from os.path import expanduser
# I keep my custom_utils.py in my home directory, but put it where you want and update the path here
sys.path.append(expanduser('~'))
from custom_utils import *
"""
Some handy utilities, especially nice for Python CLI work
"""
from math import ceil
def chunk(size, _iter):
""" split an iterable into chunks of the given size """
_num_groups = range(int(ceil(len(_iter)/float(size))))
return (_iter[n*size:(n+1)*size] for n in _num_groups)
def P(obj, key=None):
"""
List the public properties of an object, optionally
filtered by a `key` function
"""
props = [p for p in dir(obj) if p[0] is not '_']
if key is not None:
props = [p for p in props if key(p)]
return props
def PT(obj, key=None):
"""
List an object's public properties in a 4-col table,
optionally filtered by a `key` function
"""
_p = P(obj, key=key)
if filter is not None:
_max_w = max(len(p) for p in _p)
print('\n'.join(''.join(n.ljust((_max_w/4 + 2)*4) for n in c) for c in chunk(4, P(obj))))
"""
Environment setup for pdb, the python debugger
Usage: Save to ~/.pdbrc
"""
### enable tab completion
import rlcompleter
import pdb
pdb.Pdb.complete = rlcompleter.Completer(locals()).complete
### import custom python utilities
import sys
from os.path import expanduser
# I keep my custom_utils.py in my home directory, but put it where you want and update the path here
sys.path.append(expanduser('~'))
from custom_utils import *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment