issue80:python_-_partie_50
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue80:python_-_partie_50 [2014/05/04 11:49] – lecastillan | issue80:python_-_partie_50 [2014/05/07 10:55] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
s = str.translate(table[, | s = str.translate(table[, | ||
- | **Ce mois-ci , je vais vous parler de deux fonctions un peu moins connues : maketrans et translate. Nous allons commencer par la méthode translate. La méthode translate retourne une copie d'une chaîne, avec tous les caractères de la table translate remplacés (traduits) ou bien avec des caractères supprimés de la chaîne, s'ils sont contenus dans ' | + | **Ce mois-ci , je vais parler de deux fonctions un peu moins connues : maketrans et translate. Nous allons commencer par la méthode translate. La méthode translate retourne une copie d'une chaîne, avec tous les caractères de la table translate remplacés (traduits) ou bien avec des caractères supprimés de la chaîne, s'ils sont contenus dans ' |
s = str.translate(table[, | s = str.translate(table[, | ||
Ligne 19: | Ligne 19: | ||
“Th tm hs cm” | “Th tm hs cm” | ||
- | **Avant de passer à la partie ' | + | **Avant de passer à la partie ' |
astr = "The time has come" | astr = "The time has come" | ||
Ligne 35: | Ligne 35: | ||
“Th2 t3m2 h1s c4m2” | “Th2 t3m2 h1s c4m2” | ||
- | **Notez que nous avons inclus | + | **Notez que nous avons inclus |
Cela retourne : | Cela retourne : | ||
- | «Th2 t3m2 c4m2 H1S » | + | " |
** | ** | ||
Ligne 48: | Ligne 48: | ||
1bcd2fgh3jklmn4pqrst5vwxyz | 1bcd2fgh3jklmn4pqrst5vwxyz | ||
- | **Regardons comment ça marche... Nous attribuons | + | **Regardons comment ça marche... Nous attribuons |
Si vous regardez attentivement, | Si vous regardez attentivement, | ||
Ligne 59: | Ligne 59: | ||
So now it should be making sense. | So now it should be making sense. | ||
- | **Si vous regardez encore de plus près, vous verrez qu'il y a effectivement 256 entrées, en commençant par "\x00" | + | **Si vous regardez encore de plus près, vous verrez qu'il y a effectivement 256 entrées, en commençant par « \x00 » et en se terminant par « \xff ». Ainsi, le tableau contient l' |
- | Alors maintenant, à quoi tout cela peut-il servir ? | + | Maintenant, vous devez commencer |
- | ** | + | |
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: | 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: | ||
Ligne 72: | Ligne 71: | ||
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. | 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. | ||
- | **Faites un retour en arrière et pensez à la période où vous avez étudié le personnage de Jules César à l' | + | **Alors, à quoi tout cela peut-il servir ? |
cela se transformait en : DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC | cela se transformait en : DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC | ||
Ligne 83: | Ligne 82: | ||
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). | 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). | ||
- | **Personne ne sait si cette méthode fut un succès pour ce bon vieux Jules Cesar mais on peut penser que si quelqu' | + | **Personne ne sait si cette méthode fut un succès pour ce bon vieux Jules César |
- | Nous pouvons facilement utiliser la méthode | + | Nous pouvons facilement utiliser la méthode |
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. | 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. | ||
Ligne 91: | Ligne 90: | ||
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. | 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. | ||
- | **Tout ce que vous trouverez dans le code ci-dessus est à peu près ce que nous avons vu au début de cet article ou dans les précédents articles sur Python, mais je vais aller à l' | + | **Tout ce que vous trouverez dans le code ci-dessus est à peu près ce que nous avons vu au début de cet article ou dans les précédents articles sur Python, mais je vais expliquer rapidement... |
- | Les deux premières lignes représentent | + | Les deux premières lignes représentent |
** | ** | ||
Ligne 104: | Ligne 103: | ||
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). | 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). | ||
- | **Voilà, c'est comme si nous étions retournés | + | **Voilà, c'est comme à l' |
** | ** | ||
issue80/python_-_partie_50.1399196981.txt.gz · Dernière modification : 2014/05/04 11:49 de lecastillan