Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # One thing you might notice about this code is that, despite being a
- # cheap one-shot program, it still accommodates more than just "LOLSpeak".
- # You could even implement ROT13 by simply defining it in the
- # 'translations' file.
- # Surprised as I am to say it, this challenge is actually an excellent
- # example of separating your code from your data. The suggestion to put the
- # translation table in a separate file prevents you from making a big, ugly,
- # inflexible clusterfuck of code.
- # So props to the guys behind that 11-hour-long video. You actually did address
- # quite a lot of issues that most beginner tutorials tend to neglect.
- import sys
- class NoTransError: pass
- class NoFileError: pass
- def seek_back(string, pos, char):
- s = pos
- e = pos
- while string[s] != char and s >= 0:
- s -= 1
- return string[s+1:e]
- def get_text(fname):
- try:
- f = open(fname, 'r')
- buf = f.read()
- f.close()
- except IOError:
- print "Couldn't read file '%s'." % fname
- return 0
- return buf
- def get_translations(fname):
- # Using CSV would be simple and all, but I prefer a challenge, so I made up my own format.
- # Also, coding isn't about laziness; it's about not repeating yourself. Worry about data duplication and readability; not how many keystrokes/lines you can save.
- sbuf = get_text(fname)
- if sbuf == 0:
- return 0
- keys = []
- vals = []
- i = 1
- while 1:
- if i >= len(sbuf): break
- if sbuf[i] == ':':
- keys.append(seek_back(sbuf, i, '\n'))
- elif sbuf[i] == '\n':
- vals.append(seek_back(sbuf, i, ':'))
- i += 1
- return zip(keys,vals)
- def objectify(arr):
- # This is literally just to make it easier to read. trans[i]['key'] is more obvious than trans[i][0].
- if arr == 0:
- return 0
- buf = []
- for el in arr:
- buf.append({
- "key": el[0],
- "value": el[1]
- })
- # Interesting thing to point out here.
- # Python calls these ~dictionaries~
- # JavaScript calls them ~objects~
- # PHP calls them ~associative arrays~
- # C++ calls them ~structs~
- return buf
- def print_usage():
- print "Usage: %s [options]" % sys.argv[0]
- print "Options:"
- print " -f [file] specify file to use for message string. Default: msg"
- print " -t [file] specify file to use for translation table. Default: translations"
- print " -m [text] pass the message string as an argument"
- print " -h show this help"
- print "Example: %s -m \"LOL WTF was that!?\" -t translations" % sys.argv[0]
- def main():
- if len(sys.argv) > 1:
- i = 0
- msg = 0
- while 1:
- if i >= len(sys.argv):
- break
- if sys.argv[i] == '-f':
- msg = get_text(sys.argv[i+1])
- if msg == 0:
- raise NoFileError
- i += 1
- elif sys.argv[i] == '-t':
- trans = objectify(get_translations(sys.argv[i+1]))
- if trans == 0:
- raise NoTransError
- i += 1
- elif sys.argv[i] == '-m':
- msg = sys.argv[i+1]
- i += 1
- elif sys.argv[i] == '-h':
- print_usage()
- return 0
- i += 1
- else:
- print_usage()
- return 1
- for pair in trans:
- msg = msg.replace(pair['key'], pair['value'])
- print msg
- try:
- ret = main()
- if ret:
- print "Shell returned an error code '%u'" % ret
- except NoTransError:
- print "Please define a 'translations' file (or use -t) to remedy this."
- print "The format is 'Symbol:Translation'"
- print "For example, \"LOL:laughing out loud\""
- except NoFileError:
- print "Bad File."
- except:
- print "Unknown Error."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement