Skip to content

Instantly share code, notes, and snippets.

@tomwhoiscontrary
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomwhoiscontrary/9493303 to your computer and use it in GitHub Desktop.
Save tomwhoiscontrary/9493303 to your computer and use it in GitHub Desktop.
Script to interactively sort some options by repeated comparison
#! /usr/bin/env python
import sys
import itertools
def recording_in(d):
def recording(f):
def recording_f(*args):
result = f(*args)
d[args] = result
return result
return recording_f
return recording
comparisons = {}
@recording_in(comparisons)
def compare(a, b):
while True:
opinion = raw_input("(a) %s (b) %s (=) no preference? " % (a, b))
if opinion == 'a': return -1
elif opinion == 'b': return 1
elif opinion == '=': return 0
else: print "please enter a, b, or ="
if len(sys.argv) < 2:
raise ValueError, "you must give this script a list of options as arguments"
options = sys.argv[1:]
options.sort(cmp=compare)
print
COMPARISON_DESCRIPTIONS = {
-1: "%s > %s",
0: "%s = %s",
1: "%s < %s",
}
for comparands, result in sorted(comparisons.items()):
print COMPARISON_DESCRIPTIONS[result] % comparands
print
for position, option in zip(itertools.count(1), options):
print position, option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment