Last active
January 8, 2022 17:43
-
-
Save ssokolow/151572 to your computer and use it in GitHub Desktop.
Python boilerplate from which I start all my projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""[application description here]""" | |
__appname__ = "[application name here]" | |
__author__ = "Stephan Sokolow (deitarion/SSokolow)" | |
__version__ = "0.0pre0" | |
__license__ = "GNU GPL 3.0 or later" | |
import logging | |
log = logging.getLogger(__name__) | |
# -- Code Here -- | |
if __name__ == '__main__': | |
from optparse import OptionParser | |
parser = OptionParser(version="%%prog v%s" % __version__, | |
usage="%prog [options] <argument> ...", | |
description=__doc__.replace('\r\n', '\n').split('\n--snip--\n')[0]) | |
parser.add_option('-v', '--verbose', action="count", dest="verbose", | |
default=2, help="Increase the verbosity. Use twice for extra effect") | |
parser.add_option('-q', '--quiet', action="count", dest="quiet", | |
default=0, help="Decrease the verbosity. Use twice for extra effect") | |
#Reminder: %default can be used in help strings. | |
# Allow pre-formatted descriptions | |
parser.formatter.format_description = lambda description: description | |
opts, args = parser.parse_args() | |
# Set up clean logging to stderr | |
log_levels = [logging.CRITICAL, logging.ERROR, logging.WARNING, | |
logging.INFO, logging.DEBUG] | |
opts.verbose = min(opts.verbose - opts.quiet, len(log_levels) - 1) | |
opts.verbose = max(opts.verbose, 0) | |
logging.basicConfig(level=log_levels[opts.verbose], | |
format='%(levelname)s: %(message)s') | |
Must be cause this post is old
Also take a look at python-boilerplate.com for various up-to-date Python boilerplates!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any specific reason for using
OptionParser
instead of the more popularArgParse
?