Skip to content

Instantly share code, notes, and snippets.

@karlrwjohnson
karlrwjohnson / RewrapLineCommand.py
Last active February 26, 2017 01:26
Sublime plugin that re-wraps plain text so it just fits under 80 characters. Supports indented blocks, too!
# Sublime plugin that re-wraps text blocks so that each line contains the
# maximum number of characters before line 80
# To install (Sublime 3?), find your Packages/User directory by going to
# Preferences Menu > Browse Packages...
#
# Save this file into that directory.
#
# To enable it in the [Ctrl+Shift+P] command palette, create a file called
# "mine.sublime-commands" (exact name before extension doesn't matter),
@karlrwjohnson
karlrwjohnson / In-SQL Rules Engine Demo.sql
Created September 13, 2017 20:56
In-SQL Rules Engine Demo
---------------------------------------------------------
-- Boilerplate mocking out what we already have today. --
-- Just scroll down to the bottom. --
---------------------------------------------------------
-- Cleanup
drop table if exists
import_log,
my_data_source,
meta_record,
@karlrwjohnson
karlrwjohnson / message_queue_demo.py
Created November 7, 2017 22:41
PostgreSQL Message Queue Demo
'''
Postgres automatic message queue demo
Wherein a record-insertion trigger notifies a worker to process new table records
Requires:
- Python 3.6
- Postgres 9.6
- Psycopg2
Usage:
@karlrwjohnson
karlrwjohnson / generate_pinch_pot.py
Created December 22, 2017 06:02
Procedural STL file generation in Python
import functools
import numpy
import struct
import svg.path # pip install svg.path==2.2
from typing import List, Dict, Tuple, Callable # kitchen sink?
array=numpy.array
# SVG <path> string describing the undistorted profile of the pot
PATH_DATA='''
m 0,50.916666 9.1801683,0 c 0.8686257,-1.2561 2.0631007,-1.983971 3.2272117,-2.612425 6.345522,-3.425674 6.113453,-5.441741 9.183746,-10.130426 3.920877,-5.987623 6.247456,-11.575049 11.615424,-15.913572 1.361948,-1.10076 2.477231,0.565914 1.236122,1.532279 -5.445873,4.240321 -7.508074,9.124891 -11.047477,15.345625 C 20.372917,44.45 19.05,46.566667 12.975328,49.970155 11.821211,50.616778 10.583333,51.59375 10.583333,52.916667 L 0,52.916667
@karlrwjohnson
karlrwjohnson / minimal_pyqt_pyopengl.py
Last active December 21, 2019 09:45
Python + QT + OpenGL 3
"""
Minimal demo of OpenGL >= 3.0, Qt >= 5.0 in Python 3.6
Uses a programmable shader pipeline to draw a pair of triangles.
Eschews PyQt's wrapper functions in favor of PyOpenGL's because I couldn't get it to draw
anything other than a black screen (which means any one of a hundred details were incorrect).
Besides, you'd have to fall back on PyOpenGL to get glDrawArrays anyway, so we may as well
use it for everything.
@karlrwjohnson
karlrwjohnson / Lodash FP.md
Last active February 27, 2018 16:17
Lodash FP Proposal

Lodash FP Proposal

Premises

  • Immutable: All functions return a copy of the data instead of modifying it. Even functions which normally mutate data, like _set, are wrapped to make a copy first.
@karlrwjohnson
karlrwjohnson / lockfile_demo.sh
Last active April 3, 2024 06:24
Lockfile Demo - Demonstrates how to use Linux's "flock" command to create mutexes inside of a shell script
#!/bin/bash
# Lockfiles are a feature in Linux which allows multiple processes to use a file as a mutex.
# This script contains example code based on this blog post, but with a bugfix: https://dmorgan.info/posts/linux-lock-files/
# Wrapper function for easy reuse.
# I've heavily annotated it, but without the comments or arguments it's only 13 lines long.
# Parameters (you can hard-code them instead if you want)
# - LOCK_FILE: Which file to use for the lock.
@karlrwjohnson
karlrwjohnson / cycles.py
Last active May 1, 2018 03:27
Solution to Adam Babcock's brain-teaser
'''
Solution to Adam Babcock's brain-teaser
Here's the basic gist of the problem: A principal places one hundred students'
names into one hundred identical boxes, which are sitting in a row in the principal's
office. Each box contains exactly one name, but the names are not guaranteed to
be arranged in any particular order within those boxes.
Students are called in one at a time and are permitted to open up to fifty of
those boxes. If one of the boxes they open contains their name, they get a reward.
@karlrwjohnson
karlrwjohnson / pattern.java
Created October 19, 2018 14:44
Action pattern for turn-based games
// Let's define a couple of dependencies to get started.
interface GameContext {
// Methods which Actions can use to query the state of the game
}
interface ActionGameController {
// Methods which Actions can use to affect the game
// - In Redux, this is the dispatch() method and various actionCreators.
// - In Java, this is a controller.
@karlrwjohnson
karlrwjohnson / Show_nav_labels_in_bitbucket.js
Created November 26, 2018 16:05
Show navbar links in BitBucket (Greasemonkey script)
// ==UserScript==
// @name Show nav labels in Bitbucket
// @version 0.1
// @description Add labels below icons on the left nav bar
// @match (insert URL of bitbucket server)
// @grant none
// ==/UserScript==
console.log('user script loaded');
(function() {