Skip to content

Instantly share code, notes, and snippets.

@ekimekim
ekimekim / scriptlib.py
Created May 29, 2013 06:02
A quick and dirty way of interpreting command line args and options for scripts. As an example, calling a script with: $ python script.py hello -x=123 --foobar world causes the script's main function to be called with the args: main('hello', 'world', x='123', foobar=True) with the function's docstring printed in case of TypeError.
from functools import wraps
import sys
def with_argv(fn):
"""Decorator that wraps your 'main' function, giving it some automatic behaviour.
The resulting function should not be passed any args.
Command line arguments are interpreted as follows:
The first argument is the program name, it is dropped (you can still access it with sys.argv[0]).
If an argument has form '--key=value', main's kwargs are updated
@ekimekim
ekimekim / escapes.py
Created May 29, 2013 05:56
A python library of constants and utility functions for using ANSI terminal escapes.
"""
A python library of constants and utility functions for using ANSI terminal escapes.
Example usage:
>>> import escapes as e
>>> print e.FORECOLOUR(e.RED) + "this is red" + e.UNFORMAT
"""
ESC = "\033" # escape character for terminal
CSI = ESC + "[" # Control Sequence Initiator
@ekimekim
ekimekim / clock_overlay.py
Created May 29, 2013 05:51
A gevent-based script that copies input to output, but interrupts every second to paint the current time at the top of the screen. May change the time format with an optional argument.
"""
A gevent-based script that copies input to output, but interrupts every second to paint the current time at the top of the screen.
May change the time format with an optional argument.
Time format is as per the date(1) command, as well as the following escapes:
\e -> ESC character
\t -> Tab
\\ -> Literal \
For example, you could color your time output bold blue with the argument '\e[1;34m%X\e[m'