Skip to content

Instantly share code, notes, and snippets.

@micolous
Created October 12, 2015 09:48
Show Gist options
  • Save micolous/ef69a1f4a6ab08434fe1 to your computer and use it in GitHub Desktop.
Save micolous/ef69a1f4a6ab08434fe1 to your computer and use it in GitHub Desktop.
from abbreviations import abbreviations
from sys import argv
import re
# get all the commandline text
in_str = ' '.join(argv[1:]).upper()
# compile a regular expression for each of the stations, do this only on startup, because it is slow (~240 ms)
abb_re = {}
for k, v in abbreviations.iteritems():
abb_re[re.compile(r'[^A-Z0-9]' + k + r'[^A-Z0-9]')] = v
# Do the actual replacement, this is fairly fast (~10ms)
for k, v in abb_re.iteritems():
in_str = k.sub(v, in_str)
print in_str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment