Skip to content

Instantly share code, notes, and snippets.

View jsocol's full-sized avatar
🙃
for oss: hoping to get back to a monthly check-in/update cadence

James Socol jsocol

🙃
for oss: hoping to get back to a monthly check-in/update cadence
View GitHub Profile
@jsocol
jsocol / data-uri.py
Created July 18, 2011 14:48
Give an image, get a data-uri
#!/usr/bin/env python
"""Command line script to convert a file, usually an image, into a data URI
for use on the web."""
import base64
import mimetypes
import os
import sys
@jsocol
jsocol / resources.md
Last active November 15, 2021 19:16
Choosing an Open Source License Wisely
@jsocol
jsocol / sockolepsy.js
Created March 16, 2011 01:20
slow TCP proxy
/**
* Usage: node sockolepsy.js <listen> <forward> <delay>
*
* Creates a generic TCP proxy that can introduce a controllable degree of
* delay per packet.
*
* Params:
* listen - the port to listen on.
* forward - the port to foward to.
* delay - the amount of delay to introduce, in ms.
@jsocol
jsocol / fib.go
Created August 12, 2020 16:43
People love using Fibonacci as an example of recursion but the closed form solution is great
package main
import (
"fmt"
"math"
)
const Psi = -1.0 / math.Phi
var Sqrt5 = math.Pow(5.0, 0.5)
@jsocol
jsocol / dice.py
Last active June 22, 2020 01:58
A Monte Carlo dice roller to investigate ability score generation methods and other probability questions
import argparse
import random
__all__ = ['d4', 'd6', 'd8', 'd10', 'd12', 'd20', 'd100']
class D():
"""A die.
@jsocol
jsocol / rand_gems.py
Created June 22, 2020 01:57
Generate random gemstones for D&D 5e
"""Generate random gemstones for D&D 5e
It always bothered me that the tables in the DMG won't let you randomly
generate a diamond worth 50gp, or 300gp, or 500gp, when these are all common
spell components.
This script generates random gemstones and values. It considers that you might
have a small diamond worth 50gp, but probably not a giant azurite worth 5000gp.
First, we get a base and a multiplier. A base could be e.g. 10, 100, 1000. A
@jsocol
jsocol / good-forms.py
Created July 31, 2012 14:01
Django Mass Assignment
from django import forms
from myapp.models import Whatzit
class WhatzitForm(forms.ModelForm):
class Meta(object):
model = Whatzit
fields = ('foo', 'bar', 'baz')
@jsocol
jsocol / pimport
Created June 8, 2010 17:11
pvimport - load a gzipped sql dump through pv
#!/bin/sh
# Import a (g|b)zipped SQL dump into a specified database through pipeviewer.
if [ $# -lt 2 ]; then
echo "USAGE: $0 <filename> <database>"
exit 1
fi
EXT=${1/*./}
@jsocol
jsocol / ex_test.go
Created July 22, 2019 19:13
#golang test subcases
package ex
import (
"fmt"
"testing"
)
func Multiplier(in int) int {
return in * 2
}
@jsocol
jsocol / mymodel.py
Last active June 3, 2019 14:21
django caching pattern examples
from django.core.cache import cache
from django.db import models
EMPTY = '-' # Not "None"
class MyModel(models.Model):
@classmethod
def _cache_key(cls, key):
return 'mymodel:{}'.format(key)