Last active
August 29, 2015 13:57
-
-
Save tomwhoiscontrary/9493303 to your computer and use it in GitHub Desktop.
Script to interactively sort some options by repeated comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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) | |
COMPARISON_DESCRIPTIONS = { | |
-1: "%s > %s", | |
0: "%s = %s", | |
1: "%s < %s", | |
} | |
for comparands, result in sorted(comparisons.items()): | |
print COMPARISON_DESCRIPTIONS[result] % comparands | |
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