issue84:python_p._53
                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 | ||
| issue84:python_p._53 [2014/10/17 20:30] – [6] fredphil91 | issue84:python_p._53 [2014/10/20 12:34] (Version actuelle) – andre_domenech | ||
|---|---|---|---|
| Ligne 22: | Ligne 22: | ||
| **Once we have validated the email, we then will create a “checksum character” which is based on the ascii value of each character in the entire email address, and then divide it by the number of characters in the email address. For example, let’s use a mythical email address of fredjones@someplace.com. If we walk through the email address, we can get the ascii value of each character by using the ord() function. When we add up each of the ascii values, we get a sum of 1670, then we divide that by the length of the email address (23); we get 72. Remember we are using integer division here, so our result will be an integer.** | **Once we have validated the email, we then will create a “checksum character” which is based on the ascii value of each character in the entire email address, and then divide it by the number of characters in the email address. For example, let’s use a mythical email address of fredjones@someplace.com. If we walk through the email address, we can get the ascii value of each character by using the ord() function. When we add up each of the ascii values, we get a sum of 1670, then we divide that by the length of the email address (23); we get 72. Remember we are using integer division here, so our result will be an integer.** | ||
| - | Une fois l' | + | Une fois l' | 
| ====== 4 ====== | ====== 4 ====== | ||
| Ligne 33: | Ligne 33: | ||
| Lets get started with the code. Since this is the 53rd article in the series, I won’t be quite as verbose from here on out.** | Lets get started with the code. Since this is the 53rd article in the series, I won’t be quite as verbose from here on out.** | ||
| - | Maintenant que nous avons notre somme de contrôle, on soustrait 68 de celle (ascii ' | + | Maintenant que nous avons notre somme de contrôle, on en soustrait 68 (ascii ' | 
| Donc, pour l' | Donc, pour l' | ||
| Ligne 39: | Ligne 39: | ||
| w_pKlyylk|wvu{Gx|lsx|lwhy{5my | w_pKlyylk|wvu{Gx|lsx|lwhy{5my | ||
| - | Commençons à écrire le code. Puisque c'est le 53ème  | + | Commençons à écrire le code. Puisque c'est le 53e article de la série, je vais commencer à être un peu moins explicite à partir de maintenant. | 
| ====== 5 ====== | ====== 5 ====== | ||
| Ligne 52: | Ligne 52: | ||
| import sys | import sys | ||
| - | Maintenant (ci-dessus à droite), nous allons créer une chaîne qui inclura tous nos caractères « autorisés » pour la fonction AdresseValide. Je l'ai découpée en 3 parties pour qu' | + | Maintenant (ci-dessus à droite), nous allons créer une chaîne qui inclura tous nos caractères « autorisés » pour la fonction AdresseValide. Je l'ai découpée en 3 parties pour qu' | 
| ====== 6 ====== | ====== 6 ====== | ||
| Ligne 59: | Ligne 59: | ||
| First we assign the passed in email address to the variable ‘email’ and find the ‘@’ character that separates the local from the domain portions of the email. We then assign the local portion of the email to (I think it’s appropriate) ‘local’, | First we assign the passed in email address to the variable ‘email’ and find the ‘@’ character that separates the local from the domain portions of the email. We then assign the local portion of the email to (I think it’s appropriate) ‘local’, | ||
| - | Maintenant, voici notre première fonction. C'est (ci-dessous) la routine AdresseValide.  | + | Maintenant, voici notre première fonction. C'est (ci-dessous) la routine AdresseValide.  | 
| - | D' | + | D' | 
| ====== 7 ====== | ====== 7 ====== | ||
| - | Next (top right) we simply walk through each character in the local portion of the email against the list of valid characters using the in keyword. If any character in the local portion of the email fails the test, we break out of the for loop, setting the ‘isgood’ flag to False. | + | **Next (top right) we simply walk through each character in the local portion of the email against the list of valid characters using the in keyword. If any character in the local portion of the email fails the test, we break out of the for loop, setting the ‘isgood’ flag to False. | 
| Finally, we look for any set of period characters that are contiguous. We use the string.find routine that will match anything that is like ‘..’ or ‘...’ and so on. Being a lazy programmer, I used only a single “double dot” check that works for anything more. | Finally, we look for any set of period characters that are contiguous. We use the string.find routine that will match anything that is like ‘..’ or ‘...’ and so on. Being a lazy programmer, I used only a single “double dot” check that works for anything more. | ||
| Ligne 70: | Ligne 70: | ||
|     r = email.find(" |     r = email.find(" | ||
| if r > -1: | if r > -1: | ||
| - | isgood = False | + |         isgood  | 
| + | |||
| + | Ensuite (en haut à droite) nous comparons tout simplement chaque caractère dans la partie locale de l' | ||
| + | |||
| + | Enfin, nous cherchons s'il y a des points qui se suivent. Nous utilisons la routine string.find qui trouvera tout ce qui ressemble à « .. » ou « ... » et ainsi de suite. Étant un programmeur paresseux, j'ai utilisé un seul contrôle de « double point » qui fonctionne pour tout le reste. | ||
| + | |||
| + |      r = adresse.find(" | ||
| + | if r > -1: | ||
| + |          valide  | ||
| ====== 8 ====== | ====== 8 ====== | ||
| - | The last thing we do in the routine is return the value of the ‘isgood’ flag. | + | **The last thing we do in the routine is return the value of the ‘isgood’ flag. | 
| return isgood | return isgood | ||
| - | The next routine (bottom right) is the CheckSum routine which is fairly short. We walk each character in the email and create a running sum of the ascii value of each using the built-in ‘ord’ type conversion. As I stated earlier, we take that sum and divide it by the length of the email address. We return the checksum value and the character represented by that checksum. | + | The next routine (bottom right) is the CheckSum routine which is fairly short. We walk each character in the email and create a running sum of the ascii value of each using the built-in ‘ord’ type conversion. As I stated earlier, we take that sum and divide it by the length of the email address. We return the checksum value and the character represented by that checksum.** | 
| + | |||
| + | La dernière chose que fait la routine est de retourner la valeur de l' | ||
| + | |||
| + | return valide | ||
| + | |||
| + | La prochaine routine (en bas à droite) est la routine de CheckSum qui est assez courte. Nous parcourons chaque caractère de l' | ||
| ====== 9 ====== | ====== 9 ====== | ||
| - | Now for the EncodeKey routine. While it looks simple, it requires some concentration so pay attention! We assign the Offset variable to global status so we can change it within the function and so it can be used in other functions. We then set the Offset variable to the checksum minus 68. As in the example presented at the beginning of the article, it would be 72-68 which equals 4. We then step through each character of the email address adding the offset to the ascii value of that character. For the ‘f’ in ‘fredjones’, | + | **Now for the EncodeKey routine. While it looks simple, it requires some concentration so pay attention! We assign the Offset variable to global status so we can change it within the function and so it can be used in other functions. We then set the Offset variable to the checksum minus 68. As in the example presented at the beginning of the article, it would be 72-68 which equals 4. We then step through each character of the email address adding the offset to the ascii value of that character. For the ‘f’ in ‘fredjones’, | 
| + | |||
| + | Maintenant, la routine EncodeCle. Elle paraît simple, mais elle nécessite une certaine concentration donc faites bien attention ! La variable decalage est mise à l' | ||
| ====== 10 ====== | ====== 10 ====== | ||
| - | The DecodeKey routine (bottom right) basically reverses the process we used in the EncodeKey routine. One thing you might notice here is that in the first ‘if debug’ statement of this function, I used ‘!= 0’ rather than ‘== 1’, simply to remind you that the two can be interchangeable. | + | **The DecodeKey routine (bottom right) basically reverses the process we used in the EncodeKey routine. One thing you might notice here is that in the first ‘if debug’ statement of this function, I used ‘!= 0’ rather than ‘== 1’, simply to remind you that the two can be interchangeable. | 
| The DoIt function (below) asks for an email address using ‘raw_input’, | The DoIt function (below) asks for an email address using ‘raw_input’, | ||
| Ligne 90: | Ligne 106: | ||
| if __name__ == " | if __name__ == " | ||
| - | DoIt() | + | DoIt()** | 
| + | |||
| + | La routine DecodeCle (en bas à droite) renverse simplement le processus utilisé dans la routine EncodeCle. Une chose à remarquer ici, c'est que dans la première déclaration « if debug » de cette fonction, j'ai utilisé « != 0 » plutôt que « == 1 », tout simplement pour vous rappeler que les deux sont interchangeables. | ||
| + | |||
| + | La fonction FaisLe (ci-dessous) demande une adresse de courrier électronique en utilisant « raw_input », puis appelle les fonctions afin de créer la clé de licence. | ||
| + | |||
| + | Enfin, nous appelons la routine FaisLe. | ||
| + | |||
| + | if __name__ == " | ||
| + | FaisLe() | ||
| ====== 11 ====== | ====== 11 ====== | ||
| - | Now, obviously the output is not super-encrypted, | + | **Now, obviously the output is not super-encrypted, | 
| - | As always, the full source is available at http:// | + | As always, the full source is available at http:// | 
| + | Bon, bien sûr le résultat n'est pas super-crypté et, si quelqu' | ||
| - | version française : http:// | + | Comme toujours, la source complète est disponible à http:// | 
| ====== Encadrés orangés ====== | ====== Encadrés orangés ====== | ||
issue84/python_p._53.1413570601.txt.gz · Dernière modification : 2014/10/17 20:30 de fredphil91
                
                