This file contains hidden or 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 | |
import os | |
import re | |
import sys | |
import logging | |
import argparse | |
import datetime | |
import subprocess |
This file contains hidden or 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 | |
import logging | |
import argparse | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument('-v', '--verbose', dest='verbose', action='count', help='Enable debugging - multiple uses prints more messages') | |
group.add_argument('--loglevel', dest='loglevel', action='store', help='Set log level: DEBUG, INFO, WARNING, ERROR, CRITICAL') |
This file contains hidden or 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 | |
""" | |
I've always been a fan of using the getopt module but am sometimes encouraged by folks to use argparse. It does seem to have | |
some advantages but it's always been a little cryptic to me too. So I came up with a sample for some common options/arguments | |
I often what to deal with and put in my own tools. It's still a little unnatural to me but it works. | |
With regard to mutually exclusive options, argparse gives you a way to do it but it does not allow the options to appear on the | |
same command line - an error is thrown when this happens. When I implement mutually exclusive options with getopt, I typically | |
only accept the last option and behave as though the other options were not specified at all. I would kind of prefer that behavior |
This file contains hidden or 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
# There is no pound-hash operator because I intend this script to be called using `py -2 print-test` or `py -3 print-test` | |
import sys | |
def test(stmt): | |
sys.stdout.write('\nTesting {stmt!r}\n'.format(**locals())) | |
try: | |
exec(stmt) | |
except Exception as e: | |
sys.stderr.write('Caught: {e}\n'.format(**locals())) |
This file contains hidden or 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/python | |
import sys | |
import subprocess | |
cmd = sys.argv[1:] | |
if not any(word.startswith("+") for word in cmd): | |
cmd.insert(0, "+%a %b %e %H:%M:%S %Z %Y") |
This file contains hidden or 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/python | |
import sys | |
import requests | |
def dynaload(path): | |
url = "https://raw.githubusercontent.com/{path}".format(**locals()) | |
req = requests.get(url) | |
if req.status_code != 200: | |
sys.stderr.write("Error loading {url}:\n".format(**locals())) |
This file contains hidden or 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
""" | |
import sys | |
import requests | |
""" | |
def dynaload(path): | |
url = "https://raw.githubusercontent.com/{path}".format(**locals()) | |
req = requests.get(url) | |
if req.status_code != 200: | |
sys.stderr.write("Error loading {url}:\n".format(**locals())) |
NewerOlder