Thanks to Yoshi for pointing out some redundancies I had in my code. In the original code that I was posting, I was using a “for” loop along with a counter. I was aware that the “for” loop acted as a counter itself, but I wasn’t aware that it actually took on the variable it was counting. This bit of information explains a lot. Thanks again Yoshi.
This is a better version, and less code.
#Python morse code translator
morse_code = {'a':'.-','b':'-...','c':'-.-.','d':'-..','e':'.',
'f':'..-.','g':'--.','h':'....','i':'..','j':'.---',
'k':'-.-','l':'.-..','m':'--','n':'-.','o':'---',
'p':'.--.','q':'--.-','r':'.-.','s':'...','t':'_',
'u':'..-','v':'...-','w':'.--','x':'-..-','y':'-.--',
'z':'--..','0':'-----','1':'.----','2':'..---',
'3':'...--','4':'....-','5':'.....','6':'-.... ',
'7':'--...','8':'---..','9':'----.',' ':' | '}
#Function to translate string to morse code
def translate(data):
text = data.lower()
#guardian code
for i in text:
print morse_code[i] + ' ',
to_translate = raw_input("Type in the text that you would like
to translate into Morse code. ")
translate(to_translate)
March 27th, 2008 at 9:54 pm
something to think about: a function is usually supposed to return it’s results. That way, you can use those results any way you like. This expands the usefulness of the function. You could then not only print the translated text, but also save it to a file or keep it in your program for later editing.
this is how I would have coded this:
def translate(text):
text = text.lower()
return reduce(str.__add__, (morsecode[i] + ‘ ‘ for i in text))
in = raw_input(”some text:”)
#print the results
print translate(in)
#save to a file
f = open(”trans.txt”, “w”)
f.write(translate(in))
#save to variable to use later
translated = translate(in)
now, there is a lot of advanced stuff in here (reduce, passing a function to a function, generator expressions), so don’t worry about not getting that yet. The important thing is, by returning the translated text instead of printing it within the function, I can now use the function easily for a wider range of uses. In general programming philosophy, a function should do only one thing, and not have any side-effects. This enhances re-usability of the function, and since every programmer is a lazy bastard in his heart, he wants re-usability.
Like I said, this is a really minor point. The function you wrote fulfills its purpose, it’s correctly written, and pretty pythonic (though not quite as elegant as mine ;-).
P.S.: if you want to know what I did there in my function: I used the reduce function. This function takes as it’s arguments a function and a sequence. It applies the function to the first two elements of the sequence, then applies it to the result of that and the third element, so on and so on until there are no more elements. The final result is returned by reduce.
In this case the function is str.__add__, which is basically the function that is used behind the scenes if you write something like ’str’ + ’str2′. It adds strings together. the generator expression is a bit more complicated and I couldn’t do it justice in this small comment space. I will, therefore point you to your own books and the python docs on the subject.
March 27th, 2008 at 10:51 pm
right, I should introduce a few errate here. I checked the python documentation and there is a shortcut for adding a sequence of strings together that doesn’t require reduce. it’s the string.join method. basically it works like this:
separator.join(stringsequence)
using this, we can create the definitive pythonic and fastest translate function:
def translate(text):
return ‘ ‘.join(morse_code[c] for c in text.lower())
post on my recently created blog detailing this comment (it’s already gotten quite too long for a comment really). I also go into a bit more detail with reduce.
March 27th, 2008 at 10:52 pm
arg!! damn comment system…
right, here’s hoping for a correctly formed function:
def translate(text):
return ‘ ‘.join(morse_code[c] for c in text.lower())
and also a more direct link to the post I was talking about (shameless plug)
http://goingamerica.blogspot.com/2008/03/using-reduce.html
March 28th, 2008 at 9:45 pm
Don’t worry about the shameless plug. With all the help you’ve given me I should put a big banner on my front page with your picture on it.
I read the post on your blog and the reduce function makes complete sense to me. I was able to follow most of the rest of the code too. (surprisingly)
So as a rule of thumb, I should code functions to return a result. I definitely see your point about being able to save and reuse the data somewhere else in the program. I’ll try to make this a habit.
By the way, I (hopefully) fixed the code problem with wordpress. I tweaked the CSS a bit for the pre tags. So any long code, the pre tag should be used. For a short code snippet, the code tag should work fine. FYI.
Thanks again Yoshi. I’m going to take you code, throw it in Idle, and mess around with it.