Skip to content

Instantly share code, notes, and snippets.

View olemb's full-sized avatar

Ole Martin Bjørndalen olemb

View GitHub Profile
@olemb
olemb / pubdir.py
Created July 1, 2013 15:14
A version of dir() which lists only public attributes.
"""
This goes in the PYTHONSTARTUP file.
Example:
>>> pubdir([])
['append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
"""
__pubdir_no_arg = []
@olemb
olemb / howoldami
Last active December 19, 2015 02:58
Compute your age in days and years.
#!/usr/bin/python
"""
howoldami - Compute your age in days and years.
Unix has whoami, but it's missing howoldami. No longer!
$ howoldami
You are 15884 days old (or 43.49 years)
@olemb
olemb / ponpoff.sh
Created June 27, 2013 11:39
ponpoff - turn prompt on and off in bash
# Turn prompt off
function poff {
PS1='$ '
}
 
# Turn prompt on
function pon {
# Todo: make this work on mac
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
}
@olemb
olemb / dotdecode
Created October 20, 2011 15:37
Remove dotencode from a Mercurial repository
#!/bin/sh
# Remove dotencode from a Mercurial repository.
if [ -z $1 ]; then
echo "usage: dotdecode REPOSITORY"
echo
echo "A backup will be made to REPOSITORY.dotencode"
exit 1
fi
@olemb
olemb / jshint-cli
Created September 5, 2011 10:46
First stab at a CLI frontend for jshint.js
#!/usr/bin/env node
/*
* CLI frontend for jshint.js
*
* When I wrote this, I didn't know there is
*
* a much more mature project at:
* https://github.com/jshint/node-jshint
*
@olemb
olemb / files
Created September 2, 2011 17:29
Open nautilus in current directory by default
#!/bin/sh
#
# Open nautilus in current directory by default
#
if [ -z $* ]; then
nautilus .
else
nautilus $*
@olemb
olemb / gist:1171411
Created August 25, 2011 18:36
types.py
Today's lesson: Never call your module types.py if you plan to run
Python from when standing in the same directory. It took me some
time to figure out what was going on:
$ touch types.py # empty file
$ python
Traceback (most recent call last):
File "/usr/lib/python2.7/site.py", line 68, in <module>
import os
File "/usr/lib/python2.7/os.py", line 398, in <module>
@olemb
olemb / chdir_contexthandler.py
Created August 23, 2011 05:28
chdir context handler
import os
from contextlib import contextmanager
@contextmanager
def chdir(dirname):
old = os.getcwd()
os.chdir(dirname)
yield old
os.chdir(old)
@olemb
olemb / tmpdir_contexthandler.py
Created August 23, 2011 05:27
tmp contexthandler
import os
import shutil
import tempfile
from contextlib import contextmanager
@contextmanager
def tmpdir(suffix='', prefix='tmp', dir=None):
dirname = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
yield dirname
shutil.rmtree(dirname)