Skip to content
Matt Harley edited this page Oct 9, 2015 · 4 revisions

Logging and args

# My recommendation is argparse, it's usually more than powerful enough and comes with python. You can even pass in a list of arguments (default of None means that it uses sys.argv) so that you can automate testing of your command line arguments too.

parser.add_argument('-v', '--verbose', action='count', default=0)
parser.add_argument('-q', '--quiet', action='count', default=0)

logging_level = logging.WARN + 10*args.quiet - 10*args.verbose

# script -vv -> DEBUG
# script -v -> INFO
# script -> WARNING
# script -q -> ERROR
# script -qq -> CRITICAL
# script -qqq -> no logging at all

Boltons

Boltons is a set of over 100 BSD-licensed, pure-Python utilities in the same spirit as — and yet conspicuously missing from — the standard library, including:

Boltons is tested against Python 2.6, 2.7, 3.4, and PyPy. Full and extensive docs are available on Read The Docs. See what's new by checking the CHANGELOG.

Others

Clone this wiki locally