Skip to content

Instantly share code, notes, and snippets.

View tnibert's full-sized avatar

Timothy Nibert tnibert

View GitHub Profile
@tnibert
tnibert / getforeignkeys.py
Last active June 3, 2020 00:28
Get all foreign keys from SQLAlchemy schema
from sqlalchemy.ext.declarative import declarative_base
"""
This gist demonstrates ways to retrieve foreign keys in an SQLAlchemy database schema.
"""
Base = declarative_base()
metadata = Base.metadata
# then you must populate schema
@tnibert
tnibert / monitormem.sh
Created March 9, 2020 00:56
Monitor linux memory every minute
#! /bin/bash
#log memory each minute
while true; do
dt=`date`
mem=`cat /proc/meminfo | grep -E "MemFree"`
mem2=`cat /proc/meminfo | grep -E "MemAvailable"`
echo -e "$dt \t $mem \t $mem2" | tee -a memlog.txt
sleep 60
done
@tnibert
tnibert / sqlimplementation.py
Last active March 6, 2020 01:20
SQL implementations
"""
This contains various implementations of SQL functions in python
The two functions are not built for the same context
"""
def hash_join(table1, index1, table2, index2):
"""
Perform a sql like join operation on two dicts, on the specified indexes
"""
from collections import defaultdict
@tnibert
tnibert / stream.sh
Created September 29, 2019 00:52
Netcat audio stream
# this is just a dirty hack to play an mp3 over a network, need netcat and mplayer
# on the sending machine (replace localhost with ip):
nc localhost 1234 < x.mp3
# on the playing machine:
nc -l -p 1234 | mplayer -
@tnibert
tnibert / filldisk.sh
Created September 12, 2019 02:49
Fill a disk to 5 TB
# this will fill a disk to 5TB with one command, accounting for existing disk usage
# $a will be in kilobytes, so it must be converted in the expr to units of 150MB
# bs is set for efficiency, results on your disk may vary, if you change this, you'll need to change the number at the end in expr (currently 1024 * 150)
# the sed command specifies line 9 of the output pulled from df -k, you will need to change for your df output
dd if=/dev/zero of=/mnt/partition/bigfile bs=150MB count=$(let "a = 5368709120-`df -k | awk '{ print $3 "\t" }' | sed -n '9p'`"; expr $a / 153600)
@tnibert
tnibert / pdftricks.sh
Last active March 3, 2020 03:06
PDF shell manipulations
# this is a work in progress notepad, will include pdftk and ghostscript stuff
# replace page 13 of doc1.pdf with doc2.pdf
pdftk A=doc1.pdf B=doc2.pdf cat A1-12 B1 A14-end output out1.pdf
# remove the first 4 pages from in.pdf
pdftk A=in.pdf cat A5-end output out.pdf
# use ghostscript to combine PDFs
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=finished.pdf file1.pdf file2.pdf file3.pdf
@tnibert
tnibert / sigma.py
Last active May 4, 2019 23:33
Summation (sigma) notation in python 3
#! /usr/bin/env python3
#
# Σ i
# i=s
from functools import reduce
# the sequence to sum, you can use range(blah) as well
s = [1, 2, 3, 5]
@tnibert
tnibert / trunc.py
Created April 10, 2019 01:42
Truncate n bytes from end of file
# truncate num_bytes off the end of a file
import os
def trunc(filename, num_bytes):
with open(filename, 'rb+') as fi:
fi.seek(-1 * num_bytes, os.SEEK_END)
fi.truncate()
@tnibert
tnibert / convertebook.sh
Created January 13, 2019 05:48
Convert epub to mobi
sudo apt-get install calibre
ebook-convert book.epub book.mobi
@tnibert
tnibert / block-site-in-hosts-file
Created January 11, 2019 13:11
Block a website in /etc/hosts
0.0.0.0 www.example.com
0.0.0.0 example.com
::0 www.example.com
::0 example.com