Skip to content

Instantly share code, notes, and snippets.

@mnuck
Created June 15, 2012 14:55
Show Gist options
  • Save mnuck/2936857 to your computer and use it in GitHub Desktop.
Save mnuck/2936857 to your computer and use it in GitHub Desktop.
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':
assign_perm('execute', anon, rs)
else:
remove_perm('execute', anon, rs)
# option 2 (factoring out commonality)
for perm in ['modify', 'view', 'execute']:
if request.POST[perm] == "public":
assign_perm(perm, anon, rs)
else:
remove_perm(perm, anon, rs)
# option 3 (first class functions and a ternary)
for perm in ['modify', 'view', 'execute']:
fn = assign_perm if request.POST[perm] == "public" else remove_perm
fn(perm, anon, rs)
# option 4 (Nate Eloe Ballmer Peak Special)
[apply(assign_perm if request.POST[perm] == "public" else remove_perm,
(perm, anon, rs)) for perm
in ['modify', 'view', 'execute']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment