Skip to content

Instantly share code, notes, and snippets.

@bentappin
bentappin / weight.py
Created May 4, 2011 22:33
Python weight helper
from decimal import Decimal
class Weight(object):
"""
Store and convert between weights.
>>> me = Weight(st=13)
>>> me
Weight(st=13.0)
>>> me.kg
@bentappin
bentappin / gist:1003397
Created June 1, 2011 21:41
Generate checksum for a barcode/ean-13
def calc_ean_13_checksum(ean):
import math
sum = 0
for x, c in enumerate(str(ean)[::-1]):
if (x+1) % 2 == 0:
sum += int(c)
else:
sum += int(c) * 3
return int(math.ceil(sum / 10.0) * 10) - sum
@bentappin
bentappin / gist:1277722
Created October 11, 2011 09:48
My Lion dev setup
# Setup Python, postgres and Python Imaging on Mac OS X 10.7.1 (Lion) Build 11B26.
sudo easy_install pip
sudo pip install psycopg2
# Install homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
brew install wget
@bentappin
bentappin / gist:1811085
Created February 12, 2012 22:05
Grep without storing search terms (e.g. password)
# Using -f- for grep lets you type in a search on STDIN for stuff
# like passwords without saving in history or ps
grep -r -f- sourcecode/
@bentappin
bentappin / gist:2567007
Created May 1, 2012 10:12
Caesar shift
import string
def caesar_shift(s, shift):
t = string.maketrans(
string.lowercase + string.uppercase,
string.lowercase[shift:] + string.lowercase[:shift] + string.uppercase[shift:] + string.uppercase[:shift]
)
return s.translate(t)
def rot_13(s):
@bentappin
bentappin / id_rsa.pub
Last active October 11, 2015 05:08
Public key
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAgEAvSWMxjkPlQ6ti856EO+XDjc6ljWwtQn2h94l2uxtk/sSBXSrTWPCOhW7rTpyV8pCYok0Pa32YmK5lLztCHu5newKIKJC9zliNU2Yp4hE/ioOE+tPcx++ztyeFv7lyOjzUPVV6UzaoylUVpujSSHVCmLFnec/6W9Lhmo7hN8HV9xv7frN6a6IQ/TNCv6+HR8+pq+zwYQu5hU2+vpk+PhrMhlWr6A0c3XtfBle5nYaH9X+2oe5iMKGshR2xDTrBVeOtXg3AkaF4w5lID/3iyYRX2xLTOSM3nPYsGab+b6lvlge4H6/OWBosib9+LBAVxJvHINIiv+uAH8haLOTTOHfj7Rj2XpVCGgUMqzf53N2Q8+YWDaL3kMhwKXQmT3vr9n/pQqM7RNoRW6GpG6rPkc6H57W1LMJ5mSA42CfzkLsUQY3QutncWCN/qks7DYvaVJZft2JWSZnjaNAJyodZa1GWksW7EFLq6ZOKJsXSxwHNVD+bozQuz1S4h450OJKtOBLqJeAJMgUjPsRNrL/mb5zT3WcNz5QAfXYq6aK20x+wZNIVZlvLespbTHmv928vJHmsfjfZYvSfgYuPlhGxBfB3EEHqwMWfhufV9SRGnVz6CYujDjLU5oL0KGb2VjN7jgx8sol0jinKUD6VMLydGtwDwe1E2v8xSn+cNHQc13Jgns= [email protected]
@bentappin
bentappin / gist:5603785
Created May 18, 2013 08:59
Extra boxen.
github "alfred", "1.0.3", :repo => "mrben/puppet-alfred"
github "chrome", "1.1.0", :repo => "boxen/puppet-chrome"
github "dropbox", "1.1.0", :repo => "mrben/puppet-dropbox"
github "iterm2", "1.0.0", :repo => "boxen/puppet-iterm2"
github "onepassword", "1.0.1", :repo => "mrben/puppet-onepassword"
github "osx", "1.2.0", :repo => "mrben/puppet-osx"
github "sublime_text_2", "1.1.0", :repo => "boxen/puppet-sublime_text_2"
github "vlc", "1.0.1", :repo => "boxen/puppet-vlc"
github "handbrake", "1.0.0", :repo => "boxen/puppet-handbrake"
github "virtualbox", "1.0.3", :repo => "mrben/puppet-virtualbox"
# encoding: utf-8
from django import http
from django.conf import settings
try:
XS_SHARING_ALLOWED_ORIGINS = settings.XS_SHARING_ALLOWED_ORIGINS
except AttributeError:
XS_SHARING_ALLOWED_ORIGINS = '*'
try:
@bentappin
bentappin / models.py
Last active April 26, 2022 11:13
Checking if an object is a foreign key anywhere in Django.
from django.db import models
class Thing(models.Model):
# [ snip ]
def is_deletable(self):
for rel in self._meta.get_all_related_objects():
if rel.model.objects.filter(**{rel.field.name: self}).exists():
return False
return True

Keybase proof

I hereby claim:

  • I am mrben on github.
  • I am mrben (https://keybase.io/mrben) on keybase.
  • I have a public key whose fingerprint is 9B13 54F9 37F7 AF9E 6F36 A775 909C F8A8 2B93 2633

To claim this, I am signing this object: