diff options
author | Owen W. Taylor <otaylor@fishsoup.net> | 2009-08-30 11:48:39 -0400 |
---|---|---|
committer | Owen W. Taylor <otaylor@fishsoup.net> | 2009-08-30 11:50:46 -0400 |
commit | 7f92c1b30f4d0e56ff8840cbdd7fc109085af696 (patch) | |
tree | 3a1d7bfd569a29d835d6ab253ff09086bfb99cd7 | |
parent | 66da60a337ed559fc85144b3fa65bee654219ce2 (diff) |
Add helper functions for handling abbrevations
abbreviation_help_string(['apple', 'pear', 'potato']) =>
'[a]pple, [pe]ar, [po]tato'
expand_abbreviation('pea', ['apple', 'pear', 'potato']) =>
'pear'
-rwxr-xr-x | git-bz | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -706,6 +706,29 @@ def split_subject_body(lines): return subject, "".join(lines[i + 1:]).strip() +def _shortest_unique_abbreviation(full, l): + for i in xrange(1, len(full) + 1): + abbrev = full[0:i] + if not any((x != full and x.startswith(abbrev) for x in l)): + return abbrev + # Duplicate items or one item is a prefix of another + raise ValueError("%s has no unique abbreviation in %s" % (full, l)) + +def _abbreviation_item_help(full, l): + abbrev = _shortest_unique_abbreviation(full, l) + return '[%s]%s' % (abbrev, full[len(abbrev):]) + +# Return '[a]pple, [pe]ar, [po]tato' +def abbreviation_help_string(l): + return ", ".join((_abbreviation_item_help(full, l) for full in l)) + +# Find the unique element in l that starts with abbrev +def expand_abbreviation(abbrev, l): + for full in l: + if full.startswith(abbrev) and len(abbrev) >= len(_shortest_unique_abbreviation(full, l)): + return full + raise ValueError("No unique abbreviation expansion") + def prompt(message): print message, "[yn] ", line = sys.stdin.readline().strip() |