Skip to content

Instantly share code, notes, and snippets.

View pfuntner's full-sized avatar

John Pfuntner pfuntner

View GitHub Profile
@pfuntner
pfuntner / version-example
Last active January 18, 2019 16:23
Nice git-based version info
#! /usr/bin/env python
import os
import re
import sys
import logging
import argparse
import datetime
import subprocess
@pfuntner
pfuntner / log_example
Last active January 18, 2019 14:23
Example of using Python logging
#! /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')
@pfuntner
pfuntner / argparse-example.py
Last active January 22, 2019 17:46
Python argparse example
#! /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
@pfuntner
pfuntner / print-test
Created October 12, 2018 12:22
Test Python print function call vs statement
# 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()))
@pfuntner
pfuntner / unixdate
Last active April 3, 2018 15:46
Unix-style date script for Cygwin/Git bash
#! /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")
@pfuntner
pfuntner / dynaload-example.py
Last active April 3, 2018 15:18
Example usage of dynaloader
#! /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()))
@pfuntner
pfuntner / dynaloader.py
Created March 26, 2018 17:39
Dynamic loader for Python code
"""
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()))