Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / dd.py
Last active July 21, 2023 08:01
A Python wrapper for the GNU dd command.
from execute import execute, Retcode
def dd(ifile, ofile, bs, count=None, seek=None):
"""
Wrapper for the GNU dd command.
Inputs:
ifile (str): Input file
ofile (str): Output file
bs (str): Block size
@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:
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
"""
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 / execute.py
Last active November 3, 2023 06:29
Python subprocess with timeout.
import subprocess
import signal
class Signal(Exception):
"""
This exception is raise by the signal handler.
"""
pass
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: