Skip to content

Instantly share code, notes, and snippets.

View benhg's full-sized avatar
🚄

Ben Glick benhg

🚄
View GitHub Profile
@benhg
benhg / certbot_crontab
Created March 4, 2018 23:04
Crontab entry to auto-renew certbot certificates every week and restart apache server
43 6 * * * sudo certbot renew --post-hook "sudo systemctl restart apache2"
@benhg
benhg / httpd.conf
Created March 5, 2018 21:04
HTTPD Config for a virtual host for a flask app
<VirtualHost *>
ServerName FQDN HERE
WSGIDaemonProcess PROCESS_GROUP_NAME threads=5 display-name=%{GROUP} python-home=/usr/local/
WSGIProcessGroup PROCESS_GROUP_NAME
WSGIScriptAlias / /PATH/TO/APP/server_base.wsgi
<Directory /PATH/TO/APP>
WSGIProcessGroup PROCESS_GROUP_NAME
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
@benhg
benhg / server_base.wsgi
Created March 5, 2018 21:05
WSGI file to run basic python flask app
import sys
sys.path.insert(0, '/path/to/app/')
from app import app as application
@benhg
benhg / wordcount.py
Created May 11, 2018 01:36
Get total word count of all markdown cells in all .ipynb files in a given directory
import io
import glob
from IPython.nbformat import current
word_count = 0
directory = "dirpath"
for direc in glob.glob("{}*.ipynb".format(directory)):
with io.open(direc, 'r', encoding='utf-8') as f:
nb = current.read(f, 'json')
@benhg
benhg / python_parallelizer_decorator.py
Created May 19, 2018 00:02
parallel_function decorator for parallelizing python functions which take iterables as input
def parallel_function(f):
def easy_parallize(f, sequence):
""" assumes f takes sequence as input, easy w/ Python's scope """
from multiprocessing import Pool
pool = Pool(processes=4) # depends on available cores
result = pool.map(f, sequence) # for i in sequence: result[i] = f(i)
cleaned = [x for x in result if not x is None] # getting results
cleaned = asarray(cleaned)
pool.close() # not optimal! but easy
pool.join()
@benhg
benhg / download_dir_as_tgz.py
Created May 19, 2018 00:12
Run in a cell in a jupyter notebook to get all files in a tgz archive to download
import os
import tarfile
tarFileName='currdir.tar'
def RecursiveFiles(dn='.',ignoreTarFile=tarFileName):
ignore={'.pynb_checkpoints','pycache',ignoreTarFile}
for dirname,subdirs,files in os.walk(dn):
if os.path.basename(dirname) in ignore: continue
for fn in files:
fname=os.path.join(dirname,fn)
yield(fname)
@benhg
benhg / setup.md
Created June 26, 2018 15:53 — forked from developius/README.md
Set up GitHub push with SSH keys

Create a repo. Make sure there is at least one file in it (even just the README) Generate ssh key:

ssh-keygen -t rsa -C "your_email@example.com"

Copy the contents of the file ~/.ssh/id_rsa.pub to your SSH keys in your GitHub account settings. Test SSH key:

ssh -T git@github.com
@benhg
benhg / poembot-formatter.py
Created March 11, 2019 03:11
Formats text poems for poembot. Ensures that there are only 32 characters per line. Breaks up on whole words only.
strin = " ".join(open(input("Poem File?\n") + ".txt", "r").readlines())
def split_string(str, limit, sep=" "):
words = str.split()
if max(map(len, words)) > limit:
raise ValueError("limit is too small")
res, part, others = [], words[0], words[1:]
for word in others:
if len(sep) + len(word) > limit - len(part):
@benhg
benhg / install_toggle_umikaze.sh
Created March 19, 2019 20:28
Install toggle to umikaze
i need to finish this later
@benhg
benhg / remove_non_ascii.sh
Created March 25, 2019 17:49
Remove all non ascii characters with a little perl inline
perl -pi -e 's/[^[:ascii:]]//g' *.txt