Skip to content

Instantly share code, notes, and snippets.

def prompt(question, answers):
"""
Prompt the user with a question and only accept defined answers.
Input:
question (str): Question string
answers (list): A list containing accpeted response value
Output:
answer (str|int): Provided answer
"""
def prompt_yn(question):
"""
Prompt the user with a yes or no question.
Input:
question (str): Question string
Output:
answer (bool): Answer True/False
"""
while True:
import re
def to_units(value, unit):
"""
Convert a size string from unit x to unit y.
Input:
value (str): Size string
unit (str): Unit to convert to
Output:
import re
def to_bytes(value):
"""
Convert a size string with an exponent into bytes.
Input:
value (str): Size string
Output:
bytes (int): Size in bytes
@wkettler
wkettler / get_zpool_drives.py
Last active August 29, 2015 13:56
Return the device ID's for all drives in a zpool.
from execute import execute, Retcode
import re
def get_zpool_drives(zpool):
"""
Returns all drives in the zpool.
Inputs:
zpool (str): Zpool name
Outputs:
@wkettler
wkettler / ipcheck.sh
Last active August 29, 2015 13:57
Track public IP and notify by email if it has changed.
#!/bin/bash
#
# ipcheck.sh
#
# Check public IP and notify by email if it has changed.
#
EMAIL="test@test.com"
LOG="/root/.myip"
@wkettler
wkettler / parse_table.py
Last active August 29, 2015 13:58
Parse a text based table such as those frequently returned when executing commands via bash.
def parse_table(f, idx=0, delim=None, head=None):
"""
Parse a table.
Inputs:
f (str): Path to file
idx (int): Index column
delim (str): Table delimiter
head (list): Optionally defined table header
Outputs:
@wkettler
wkettler / pprint_table.py
Last active August 29, 2015 14:06
Pretty print a list of list, i.e. a table.
def pprint_table(table):
"""
Pretty print table.
Inputs:
table (list): table
Outputs:
None
"""
padding = []
@wkettler
wkettler / tree.py
Last active August 29, 2015 14:11
Autovivification removes need for horrendous nested calls to {}.setdefault() when working with trees in python. my_tree = tree() my_tree['tra']['la']['la']
from collections import defaultdict
def tree():
return defaultdict(tree)
@wkettler
wkettler / carbon-cache
Last active December 25, 2015 14:59 — forked from chalmerj/gist:1492384
#!/bin/bash
#
# carbon-cache
#
# Graphite's carbon-cache daemon init script.
#
# William Kettler <william.p.kettler@gmail.com>
#
# Source init-functions