Outils pour utilisateurs

Outils du site


issue79:python

This month, I thought I’d talk about a couple of lesser known functions, maketrans and translate. We’ll start with the translate method. The translate method returns a copy of a string – with all characters in the translate table replaced, or has the characters in the optional parameter deletechars removed from the string. Here’s the syntax.

s = str.translate(table[,deletecharacters])

Before we get to the table portion of the method, let’s look at the delete portion. Let’s say that you have the string “The time has come”. And you want to delete all the vowels (for some weird reason) from that string. You can code it like this:

astr = “The time has come”

astr.translate(None,’aeiou’)

will return:

“Th tm hs cm”

Notice that we included None as the translate table. While this part is cool, it gets better. There is a function called maketrans. It takes an input string and an output string as parameters and returns a table that is used as the first parameter into the translate method. Here (top right) is a very simple example.

It returns:

“Th2 t3m2 h1s c4m2”

Let’s look at what this does. We assign intable to a string of vowels as before. outtable is assigned the numbers 1,2,3,4,5 as a string. When we make the call to maketrans, our actual trantable is as follows (shown below. The “\x” means that it is hexadecimal char):

If you look at it carefully, you’ll see that the lowercase vowel letters are replaced with the numbers we specified:

1bcd2fgh3jklmn4pqrst5vwxyz

If you look even closer, you’ll see that there actually 256 entries starting with “\x00” and ending with “\xff”. So the table contains the entire 256 possible ascii character set. So, when the translate method gets the table, it iterates (or walks through) each character, getting that characters value in Hex, and then finds that value in the translate table and substitutes it in the output string. The Hex representation of our original astr string (‘The time has come’) is shown below.

So now it should be making sense.

Now the purpose of this whole thing. Think back to your schooling where you learned about Julius Ceasar. Whenever he wanted to send a message of a confidential matter, he would use a cipher that would shift all the letters of the alphabet three characters to the right. So, using todays english alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

becomes: DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC

While this seems very simple by today’s standards, when I was a school kid, we used this all the time to send messages to each other. We used a different index into the string to start the encryption string, the logic behind it was the same.

No one knows how effective this actually was for good old Julius. One would think that if someone intercepted the message, they would have thought that it was in some foreign language. We can only speculate.

We can easily use the translate method and the maketrans helper function to allow us to have fun with this. Let’s say we want to make a simple program that allows us to enter a string of “plain text” and get back an encrypted string using the same side right method that Caesar used. For simplicity sake, let’s only use uppercase characters (shown top right).

Everything in the above code is pretty much what we’ve covered above or in earlier Python articles, but I’ll go over it quickly.

The first two lines are the in and out strings. We’ve just shifted the characters and wrapped around to create the out string. The next two lines create a table for encoding and one for decoding. Line 5 prompts the user to enter a string to encode. We then encode that string (EncString) in the next line. To decode it, we simply use the translate method on the encoded string to get the plain text back. Finally we print both strings out. Here’s the output of the program.

Enter the plaintext string → THE TIME HAS COME Encoded string is - WKH WLPH KDV FRPH Decoded string is - THE TIME HAS COME

Just like back in school. But let’s flesh it out just a bit to make it a bit more usable. The code is almost the same with a few exceptions. First, we have added a space to the end of the intab string and in between the “Z” and the “A” in the outtab string. This helps keep the actual words from being too obvious in the encrypted string. The next change is where we ask if the user wants to encode or decode the string. Finally we added an if statement to control what we print (shown bottom right).

The output from the program is: Encode or Decode (E or D) → E Enter the string → THE TIME HAS COME Encoded string is - WKHCWLPHCKDVCFRPH

And to test the decode side of things: Encode or Decode (E or D) → D Enter the string → WKHCWLPHCKDVCFRPH Decoded string is - THE TIME HAS COME

Well, hopefully you are starting to get ideas about how to use this new information in your own code. See you next time!

Blocs de code : p. 8 haut :

intable = ‘aeiou’ outtable = ‘12345’ trantable = maketrans(intable,outtable) astr = “The time has come” astr.translate(trantable)

p. 8 bas :

\x54\x68\x65\x20\x74\x69\x6d\x65\x20\x68\x61\x73\x20\x63\x6f\x6d\x65

T   h   e       t   i   m   e       h   a   s       c   o   m   e

'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\ x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !“#$%&\'()*+,-./0123456789:;⇔?@ABC DEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`1bcd2fgh3jklmn4pqrst5vwxyz{|}~\x7f\x80\x81\x82\x83 \x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97 \x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab \xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf \xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3 \xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7 \xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb \xfc\xfd\xfe\xff'

Page 9 haut :

from string import maketrans #———————- intab = “ABCDEFGHIJKLMNOPQRSTUVWXYZ” outtab = “DEFGHIJKLMNOPQRSTUVWXYZABC” EncTrantab = maketrans(intab,outtab) #Encode DecTrantab = maketrans(outtab,intab) #Decode instring = raw_input(“Enter the plaintext string → ”) EncString = instring.translate(EncTrantab) DecString = EncString.translate(DecTrantab) print(“Encoded string is - %s” % EncString) print(“Decoded string is - %s” % DecString)

Page 9 bas : from string import maketrans

#Be sure to include the space character in the strings intab = “ABCDEFGHIJKLMNOPQRSTUVWXYZ ” outtab = “DEFGHIJKLMNOPQRSTUVWXYZ ABC” EncTrantab = maketrans(intab,outtab) #Encode DecTrantab = maketrans(outtab,intab) #Decode

which = raw_input(“Encode or Decode (E or D) → ”) instring = raw_input(“Enter the string → ”) EncString = instring.translate(EncTrantab) DecString = instring.translate(DecTrantab)

if which == “E”:

  print("Encoded string is - %s" % EncString)

else:

  print("Decoded string is - %s" % DecString)  
issue79/python.txt · Dernière modification : 2014/01/21 12:00 de andre_domenech