Skip to content

Instantly share code, notes, and snippets.

View pfuntner's full-sized avatar

John Pfuntner pfuntner

View GitHub Profile
@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()))
@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 / 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 / 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 / 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 / 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 / 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 / test1.py
Last active April 20, 2019 14:14
Simple Python unit test
#! /usr/bin/env python
import os
import logging
from unittest import TestCase
import random
logging.basicConfig(format='%(asctime)s %(levelname)s %(pathname)s:%(lineno)d %(msg)s')
log = logging.getLogger()
@pfuntner
pfuntner / BrunosSatellite
Last active April 29, 2019 13:04
whatami output
It is Sat 2019-04-27 23:34:17.053346 UTC
I am /home/John Pfuntner/bin/whatami: 2019-04-27 08:55:21, github.com:pfuntner/toys, master, 8d78c9, d0fc9c11b3abb42fa0ac7e1119221692
['uname', '-a']: 'CYGWIN_NT-6.3 BrunosSatellite 2.11.2(0.329/5/3) 2018-11-08 14:34 x86_64 Cygwin'
['uname', '-n']: 'BrunosSatellite'
['uname', '-s']: 'CYGWIN_NT-6.3'
['uname', '-v']: '2018-11-08 14:34'
['uname', '-r']: '2.11.2(0.329/5/3)'
['uname', '-m']: 'x86_64'
['uname', '-p']: 'unknown'
@pfuntner
pfuntner / restart_test.yml
Last active April 20, 2021 14:55
Running multiple tasks in an Ansible handler
- block:
- name: message 1
debug: msg=operation1
- name: message 2
debug: msg=operation2
- name: message 3
debug: msg=operation3