Created
May 22, 2011 19:37
-
-
Save wandernauta/985790 to your computer and use it in GitHub Desktop.
A quick Python port of the Ruby Abbrev module
This file contains hidden or 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/python | |
""" | |
Copyright (c) 2011 Wander Nauta http://wandernauta.nl | |
This is a based on the Abbrev module that ships with Ruby, by Akinori Mushi. | |
Thanks, Akinori! | |
This snippet is distributed in the hope that it will be useful, but WITHOUT | |
WARRANTIES OR CONDITIONS OF ANY KIND. Have fun. | |
""" | |
def abbrev(*words): | |
""" | |
Calculate the set of unique abbreviations for a given set of strings. | |
Useful for calculating abbreviations for commands. | |
""" | |
table = {} | |
seen = {} | |
for word in words: | |
for count in range(1, len(word)): | |
abbr = word[0:count] | |
if abbr in seen: | |
del table[abbr] | |
del seen[abbr] | |
else: | |
table[abbr] = word | |
seen[abbr] = word | |
for word in words: | |
table[word] = word | |
return table | |
if __name__ == "__main__": | |
import pprint | |
pprint.pprint(abbrev('install', 'update', 'upgrade', 'home', 'help')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment