Skip to content

Instantly share code, notes, and snippets.

@CrazyPython
Created February 19, 2017 19:01
Show Gist options
  • Save CrazyPython/85dd67641cc09903f6812e32263bd2ea to your computer and use it in GitHub Desktop.
Save CrazyPython/85dd67641cc09903f6812e32263bd2ea to your computer and use it in GitHub Desktop.
To use, save each under bin without the extensions.
#!/usr/bin/env python
import argparse, sys
parser = argparse.ArgumentParser(description="Format numbers as human readable byte units. Accepts basic mathematical expressions. Characters allowed are \"0123456789\\t\\n -+/*%\".")
parser.add_argument("bytes", help="number to format")
parser.add_argument("--no-suffix", help="don't add a 'B' suffix to the end of the output", action="store_true")
args = parser.parse_args()
allowed_chars = '0123456789\t\n -+/*%'
def format(num):
suffix = 'B'
units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']
if args.no_suffix:
suffix = ''
units = ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']
for unit in units:
if abs(num) < 1024.0:
return "%3.1f%s%s" % (num, unit, suffix)
num /= 1024.0
return "%.1f%s%s" % (num, 'Yi', suffix)
for char in args.bytes:
if char not in allowed_chars:
sys.stderr.write("{}: error: argument bytes: invalid int value or math expression: '{}'\n".format("formatbytes", args.bytes))
sys.exit(1)
try:
evaluated = eval(args.bytes)
except SyntaxError:
sys.stderr.write("{}: error: argument bytes: error while evaluating expression: '{}'\n".format("formatbytes", args.bytes))
sys.exit(1)
print(format(evaluated))
#!/usr/bin/env python
import sys, re, os, argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true")
parser.add_argument("pattern")
args = parser.parse_args()
text = sys.stdin.read()
match = re.search(args.pattern, text)
if match is None:
if args.verbose:
sys.stderr.write("No match found.\n")
sys.exit(1)
groups = match.groups()
if len(groups) == 0 and args.verbose:
sys.stdout.write("No groups captured.\n")
print(', '.join(groups))
#!/usr/bin/env zsh
brew update
echo "\e[1m\e[31;1m==>\e[0m\e[1m Completed 1/5 steps\e[0m"
brew upgrade
echo "\e[1m\e[31;1m==>\e[0m\e[1m Completed 2/5 steps\e[0m"
brew cu
echo "\e[1m\e[31;1m==>\e[0m\e[1m Completed 3/5 steps\e[0m"
cachedir=$(brew --cache)
echo "\e[1m \e[32m$\e[0m sudo rm -rf $cachedir \e[0m"
if [ ! -d $cachedir ]; then
sudo mkdir -p $cachedir;
fi
beforeclean=$(ls -l $(dirname $cachedir) | matchgroup "\S+\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+\S+\s+\S+\s+Homebrew")
sudo rm -rf $cachedir
if [ ! -d $cachedir ]; then
sudo mkdir -p $cachedir;
fi
afterclean=$(ls -l $(dirname $cachedir) | matchgroup "\S+\s+\S+\s+\S+\s+\S+\s+(\S+)\s+\S+\s+\S+\s+\S+\s+Homebrew")
if [ $(($beforeclean-$afterclean)) -gt 0 ]; then
echo "\e[1m\e[36;1m==>\e[0m\e[1m \e[1mThis operation has freed approximately \e[36m$(formatbytes $beforeclean-$afterclean)\e[0m\e[1m of disk space.\e[0m";
fi
echo "\e[1m\e[31;1m==>\e[0m\e[1m Completed 4/5 steps\e[0m"
brew prune -v
echo "\e[1m\e[31;1m==>\e[0m\e[1m Completed 5/5 steps\e[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment