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 21:12] – fredphil91 | 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 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 » et en se terminant par « \xff ». Ainsi, le tableau contient l' | + | **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' |
Maintenant, vous devez commencer à comprendre.** | Maintenant, vous devez commencer à comprendre.** | ||
Ligne 71: | 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. | ||
- | **Alors | + | **Alors, à quoi tout cela peut-il servir ? Faites un retour en arrière et pensez à la période où vous avez étudié le personnage de Jules César à l' |
cela se transformait en : DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC | cela se transformait en : DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC | ||
Ligne 82: | 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 « translate » et la fonction « maketrans » pour nous amuser un peu avec ça. Partons du principe que nous voulons faire un programme simple qui nous permet d' | Nous pouvons facilement utiliser la méthode « translate » et la fonction « maketrans » pour nous amuser un peu avec ça. Partons du principe que nous voulons faire un programme simple qui nous permet d' | ||
Ligne 90: | 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 103: | 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.1399230747.txt.gz · Dernière modification : 2014/05/04 21:12 de fredphil91