Skip to content

Instantly share code, notes, and snippets.

Avatar

nathants nathants

View GitHub Profile
View retry.sh
#!/bin/bash
# define the retry fn
retry() {
e=0
if [[ $- =~ e ]]; then
e=1
fi
set +e
max_tries=10
@nathants
nathants / pip-upgrade.py
Created Jun 18, 2018
check for updates and optionally upgrade packages via pip
View pip-upgrade.py
#!/usr/bin/env python3
"""
check for updates and optionally upgrade packages via pip
"""
import os
import subprocess
import sys
import termios
View die-hard.py
from hypothesis import given, settings
from hypothesis.strategies import lists, sampled_from
def new_state():
return {'big': 0, 'small': 0}
def desired_state(state):
return state['big'] != 4
def empty_big(state):
View log-watcher.py
#!/usr/bin/env python3
import functools
import sys
import time
import collections
import threading
import queue
email_to = '_@_.com'
email_subject = 'error'
View auto-restart.sh
#!/bin/bash
set -eou pipefail
# usage: auto-restart.sh python program.py
trap 'exit 1' SIGINT
count=$(ps -eo args | grep ^/bin/bash | grep "auto-restart $*"$ | grep -v grep | wc -l)
if (($count > 2)); then
echo already running: auto-restart $@ >&2
View rotate-logs.sh
#!/bin/bash
set -euo pipefail
# usage: myprogram.py | rotate-logs /tmp/myprogram.log [1000000]
file=$1
max_lines=${2:-1000000}
touch $file
line_count=$(cat $file | wc -l)
@nathants
nathants / pys.sh
Last active Jun 5, 2020
3 bash functions for quick python oneliners
View pys.sh
PY_IMPORTS="
import datetime
import string
import random
import math
import time
import itertools
import collections
import functools
import fcntl
View license.txt
All public gists https://gist.github.com/nathants
Copyright 2020-present, Nathan Todd-Stone
MIT License, http://www.opensource.org/licenses/mit
@nathants
nathants / color.py
Last active Jun 15, 2020
colorize input text
View color.py
#!/usr/bin/env python3
"""
colorize input text
usage: echo a b c | color a:green c:red
"""
import functools
import sys
@nathants
nathants / tail_tinysnitch_logs.sh
Created Jun 15, 2020
a small and colorful format to tail tinysnitch logs
View tail_tinysnitch_logs.sh
# tinysnitch: https://github.com/nathants/tinysnitch
# pys: https://gist.github.com/nathants/741b066af9faa15f3ed50ed6cf677d67
# color: https://gist.github.com/nathants/336bc5e501ad174aeeb7986f2b0633e4
# colorful snitch logs
tail -f /tmp/tinysnitchd.log \
| grep --line-buffered -e allow -e deny \
| grep --line-buffered -v 1.1.1.1:53 \
| pys 'i.split("INFO")[-1]' \
| stdbuf -oL sed -E 's:\|.+>::' \
You can’t perform that action at this time.