Skip to content

Instantly share code, notes, and snippets.

@mnuck
mnuck / switch_case_considered_harmful.py
Created June 15, 2012 16:35
Why if/else will eventually bite you
# option 3 (ternary)
for perm in ['modify', 'view', 'execute']:
fn = assign_perm if request.POST[perm] == "public" else remove_perm
fn(perm, anon, rs)
# option 3.1 (switch/case-in-a-dict)
fn_selector = {"public" : assign_perm,
"private" : remove_perm }
for perm in ['modify', 'view', 'execute']:
fn_selector[request.POST[perm]](perm, anon, rs)
@mnuck
mnuck / options.py
Created June 15, 2012 14:55
Three ways to do the same thing (now Four!)
# option 1
if request.POST['modify'] == 'public':
assign_perm('modify', anon, rs)
else:
remove_perm('modify', anon, rs)
if request.POST['view'] == 'public':
assign_perm('view', anon, rs)
else:
remove_perm('view', anon, rs)
if request.POST['execute'] == 'public':
@mnuck
mnuck / getAPOD.py
Created June 11, 2012 19:53
Grabs the Astronomy Picture of the Day
#!/usr/bin/env python
# Grab the Astronomical Picture of the Day from NASA's site.
# Make it the desktop background, if it's a jpg
#
# Matthew Nuckolls
import urllib2
import re
import subprocess
base_url = "http://apod.nasa.gov/apod/"
@mnuck
mnuck / gist:2788358
Created May 25, 2012 14:14
Python oddity
#
# Credit for finding this one goes to Brian Goldman
#
broken = 'broken'
works = 'works'
def test():
print 'In Test', works
from tater.models import FakeGlobal
class needsScriptingQueue(object):
def __init__(self, f):
self.f = f
self.__name__ = f.__name__
def __call__(self, *args):
global scriptingQueue
sq_db = FakeGlobal.objects.get(name='scriptingQueue')
scriptingQueue = json.loads(sq_db.value)
class fakeGlobal(object):
def __init__(self, f):
self.f = f
self.__name__ = f.__name__
def __call__(self, *args):
global derpadil
derpadil = "blarg"
retval = self.f(*args)
del derpadil