Skip to content

Instantly share code, notes, and snippets.

@scj7t4
Forked from mnuck/options.py
Created June 15, 2012 15:28
Show Gist options
  • Save scj7t4/2937034 to your computer and use it in GitHub Desktop.
Save scj7t4/2937034 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