Skip to content

Instantly share code, notes, and snippets.

View catichenor's full-sized avatar

Christopher Tichenor catichenor

View GitHub Profile
@catichenor
catichenor / larson_genetic_algorithm_py3.py
Last active August 15, 2017 19:13
Will Larson - Genetic Algorithm converted to Python 3
#!/usr/bin/python3.6
# Conversion of genetic algorithm from Will Larson's example in
# https://lethain.com/genetic-algorithms-cool-name-damn-simple/ to Python 3
"""
# Example usage
target = 371
p_count = 100
i_length = 6
@catichenor
catichenor / subl.sh
Created August 14, 2017 18:55
How to make `subl` on a Mac
#!/usr/bin/env bash
exec /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text "$@"
@catichenor
catichenor / enumerate.py
Created August 2, 2017 20:37
Using enumerate() in Python
print('Output:')
for x in enumerate('hello world'):
print x
# Output:
# (0, 'h')
# (1, 'e')
# (2, 'l')
# (3, 'l')
# (4, 'o')
@catichenor
catichenor / copy_contents.sh
Last active July 14, 2018 00:22
Copy contents of file via "Run Shell Script" in Automator (macOS)
cat "${1}" | pbcopy
@catichenor
catichenor / fabric-jinja.py
Last active January 6, 2019 12:53
Create a file from a Jinja template with Fabric
# Simple example of Python Fabric + Jinja deployment of a file using templating
from datetime import datetime
from fabric.tasks import execute
from fabric.contrib.files import upload_template
def create_setup_from_template():
today = datetime.now()
today_string = today.strftime('%m%d%Y')
@catichenor
catichenor / remove-old-directories.py
Created March 22, 2017 20:33
Watch current directory, delete all directories but the most recent
from __future__ import print_function
import os
import time
import shutil
def get_path_and_mod_date(file_entry):
"""
Get the full path and the modification date of a file entry.
:param file_entry: The input file entry (could be a directory or file,
though this script only deals with directories).
@catichenor
catichenor / convert_crop.sh
Created March 17, 2017 18:02
ImageMagick crop a 410x312 section of an image at an offset of 476x291
convert big_image.png -crop 410x312+476+291 cropped_image.png
@catichenor
catichenor / dask-combine-csv-files.py
Last active August 25, 2023 16:55
Combine CSV files using Dask
import csv
import datetime
import dask.bag as db
b = db.read_text('*.csv', blocksize=10000000) # Read in a bunch of CSV files from the current directory.
records = b.str.strip().str.split(',')
header = records.compute()[0] # Get the first line from the records to retrieve the header.
combined_bag = db.from_sequence(records.compute(), npartitions=1)
# ^ Join the bag into one partition, so the CSV file is not separated.
@catichenor
catichenor / stitch-pngs.py
Created March 4, 2017 00:28
Stitch together numbered PNG files in separate directories under the current path.
import os
import fnmatch
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
dirs = filter(os.path.isdir, os.listdir(os.curdir))
has_files = filter(lambda this_dir: os.listdir(this_dir), dirs) # Get all directories that actually contain files.
count_pngs = len(filter(lambda this_file: this_file.endswith('.png'), os.listdir(has_files[0])))
@catichenor
catichenor / relative_to_absolute.js
Created February 28, 2017 00:21
Change relative URL to absolute URL in Javascript
// Adapted from https://muaz-khan.blogspot.com/2012/02/absolute-or-relative-url-issues-and.html
function qualifyURL(url) {
var a = document.createElement('a');
a.href = url; // set string url
url = a.href; // get qualified/absolute url
a.href = null; // no server request
return url;
}