issue77:tutoriel_-_python_47
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 | ||
issue77:tutoriel_-_python_47 [2014/02/21 17:18] – [8] fredphil91 | issue77:tutoriel_-_python_47 [2014/03/07 12:13] (Version actuelle) – [8] fredphil91 | ||
---|---|---|---|
Ligne 49: | Ligne 49: | ||
In the line above right, we call the GetSeasonEpisode routine to pull the series name, season number and episode number from the filename. If everything parses correctly, the variable isok is set to true, and the data we are looking for is placed into a list and then returned to us.** | In the line above right, we call the GetSeasonEpisode routine to pull the series name, season number and episode number from the filename. If everything parses correctly, the variable isok is set to true, and the data we are looking for is placed into a list and then returned to us.** | ||
- | Jetons un coup d' | + | Jetons un coup d’œil |
- | Les deux premières lignes ici initialisent les choses pour la routine ParcourirChemin, | + | Les deux premières lignes ici initialisent les choses pour la routine ParcourirChemin, |
Dans la ligne en haut à droite, nous appelons la routine RecupereSaisonEpisode pour récupérer le nom de la série et les numéros de la saison et de l' | Dans la ligne en haut à droite, nous appelons la routine RecupereSaisonEpisode pour récupérer le nom de la série et les numéros de la saison et de l' | ||
Ligne 60: | Ligne 60: | ||
We want to get the episode number of each file and put it into the elist list. Once we are done with all the files within the folder we are currently in, we can then make the assumption that we have been pretty much keeping up with the files and the highest numbered episode is the latest one available. As we discussed last month, we can then create a set that is numbered from 1 to the last episode, and convert the list to a set and pull a difference. While that is great in theory, there is a bit of a “hitch in our git-a-long” when it comes down to actual practice. We don’t actually get a nice and neat indication as to when we are done with any particular folder. What we do have though, is the knowledge that when we get done with each file, the code right after the “for file in [...” gets run. If we know the name of the last folder visited, and the current folder name, we can compare the two and, if they are different, we have finished a folder and our episode list should be complete. That’s what the ‘lastroot’ variable is for.** | We want to get the episode number of each file and put it into the elist list. Once we are done with all the files within the folder we are currently in, we can then make the assumption that we have been pretty much keeping up with the files and the highest numbered episode is the latest one available. As we discussed last month, we can then create a set that is numbered from 1 to the last episode, and convert the list to a set and pull a difference. While that is great in theory, there is a bit of a “hitch in our git-a-long” when it comes down to actual practice. We don’t actually get a nice and neat indication as to when we are done with any particular folder. What we do have though, is the knowledge that when we get done with each file, the code right after the “for file in [...” gets run. If we know the name of the last folder visited, and the current folder name, we can compare the two and, if they are different, we have finished a folder and our episode list should be complete. That’s what the ‘lastroot’ variable is for.** | ||
- | Ensuite (ci-dessous), | + | Ensuite (ci-dessous), |
- | Nous voulons obtenir le numéro d' | + | Nous voulons obtenir le numéro d' |
====== 5 ====== | ====== 5 ====== | ||
- | Just after the ‘for file in[‘ line is where we’ll put the majority of our new code. It’s only seven lines. Here are the seven lines. (The black lines are the existing lines for your convenience.) | + | **Just after the ‘for file in[‘ line is where we’ll put the majority of our new code. It’s only seven lines. Here are the seven lines. (The black lines are the existing lines for your convenience.) |
Line by line of the new code, here is the logic: | Line by line of the new code, here is the logic: | ||
- | First, we check to see if the variable lastroot has the same value as root (the current folder name). If so, we are in the same folder, so we don’t run any of the code. If not, we then assign the current folder name to the lastroot variable. Next, we check to see if the episode list (elist) has any entries (len(elist) > 0). This is to make sure we weren’t in an empty directory. If we have items in the list, then we call the Missing routine. We pass the episode list, the highest episode number, the current season number, and the name of the season, so we can print that out later on. The last three lines clear the list, the current show name, and the current season, and we move on as we did before. | + | First, we check to see if the variable lastroot has the same value as root (the current folder name). If so, we are in the same folder, so we don’t run any of the code. If not, we then assign the current folder name to the lastroot variable. Next, we check to see if the episode list (elist) has any entries (len(elist) > 0). This is to make sure we weren’t in an empty directory. If we have items in the list, then we call the Missing routine. We pass the episode list, the highest episode number, the current season number, and the name of the season, so we can print that out later on. The last three lines clear the list, the current show name, and the current season, and we move on as we did before.** |
+ | |||
+ | Nous allons mettre la majorité de notre nouveau code juste après la ligne « for fic in [… ». Cela ne représente que sept lignes, que voici (j'ai mis en noir les lignes existantes pour plus de clarté). | ||
+ | |||
+ | Voici la logique du nouveau code, ligne par ligne : | ||
+ | |||
+ | Tout d' | ||
====== 6 ====== | ====== 6 ====== | ||
- | Next we have to change two lines and add one line of code into the if isok: code, a few lines down. Again, right, the black lines are the existing code: | + | **Next we have to change two lines and add one line of code into the if isok: code, a few lines down. Again, right, the black lines are the existing code: |
Here, we have just come back from the GetSeasonEpisode routine. If we had a parsable file name, we want to get the show name and season number, and add the current episode into the list. Notice, we are converting the episode number to an integer before we add it to the list. | Here, we have just come back from the GetSeasonEpisode routine. If we had a parsable file name, we want to get the show name and season number, and add the current episode into the list. Notice, we are converting the episode number to an integer before we add it to the list. | ||
- | We are done with this portion of the code. Now, all we have to do is add the Missing routine. Just after the WalkThePath routine, we’ll add the following code. | + | We are done with this portion of the code. Now, all we have to do is add the Missing routine. Just after the WalkThePath routine, we’ll add the following code.** |
+ | |||
+ | Ensuite, nous devons changer deux lignes et ajouter une ligne de code dans le morceau de code « if estok » quelques lignes plus bas. Encore une fois, les lignes noires sont le code existant : | ||
+ | |||
+ | Ici, nous venons de revenir de la routine RecupereSaisonEpisode. Si nous avions un nom de fichier analysable, nous voulons obtenir le nom de l' | ||
+ | |||
+ | Nous en avons fini avec cette partie du code. Maintenant, tout ce qui nous reste à faire est d' | ||
====== 7 ====== | ====== 7 ====== | ||
- | Again, it is a very simple set of code and we pretty much went over it last month, but we’ll walk through it just in case you missed it. | + | **Again, it is a very simple set of code and we pretty much went over it last month, but we’ll walk through it just in case you missed it. |
We define the function and set up four parameters. We will be passing the episode list (eplist), the number of episodes we should expect (shouldhave) which is the highest episode number in the episode list, the season number (season), and the show name (showname). | We define the function and set up four parameters. We will be passing the episode list (eplist), the number of episodes we should expect (shouldhave) which is the highest episode number in the episode list, the season number (season), and the show name (showname). | ||
- | Next, we create a set that contains a list of numbers using the range built-in function, starting with 1 and going to the value in shouldhave + 1. We then call the difference function – on this set and a converted set from the episode list (temp-set(eplist)) – and convert it back to a list. We then check to see if there is anything in the list – so we don’t print a line with an empty list, and if there’s anything there, we print it out. | + | Next, we create a set that contains a list of numbers using the range built-in function, starting with 1 and going to the value in shouldhave + 1. We then call the difference function – on this set and a converted set from the episode list (temp-set(eplist)) – and convert it back to a list. We then check to see if there is anything in the list – so we don’t print a line with an empty list, and if there’s anything there, we print it out.** |
+ | |||
+ | Encore une fois, c'est du code très simple et nous l' | ||
+ | |||
+ | Nous définissons la fonction et mettons en place quatre paramètres. Nous passerons la liste des épisodes (episode_liste), | ||
+ | |||
+ | Ensuite, nous créons un ensemble qui contient une liste de numéros en utilisant la fonction intégrée « range », en allant de 1 à la valeur nombre_therorique + 1. Nous appelons ensuite la fonction de différence - sur cet ensemble et un ensemble converti depuis la liste des épisodes (temp - set(episode_liste)) - et le reconvertissons en liste. Nous vérifions ensuite s'il y a quelque chose dans la liste - afin de ne pas afficher une ligne avec une liste vide, et s'il y a quelque chose, nous l' | ||
====== 8 ====== | ====== 8 ====== | ||
Ligne 91: | Ligne 109: | ||
Have a good month and we’ll see you soon.** | Have a good month and we’ll see you soon.** | ||
+ | |||
+ | C'est tout. La seule faille dans cette logique est qu' | ||
+ | |||
+ | J'ai mis les deux routines sur pastebin au cas où vous voudriez juste faire un remplacement rapide dans votre code. Vous pouvez les consulter ici : http:// | ||
+ | |||
+ | Passez un bon mois et nous vous reverrons bientôt. | ||
+ | |||
+ | ====== Morceaux de code dans les encadrés ====== | ||
+ | |||
+ | ==== page 7 en haut ==== | ||
+ | |||
+ | for racine, reps, fichiers in os.walk(chemin, | ||
+ | for fic in [f for f in fichiers if f.endswith ((' | ||
+ | |||
+ | ==== page 7 juste en dessous ==== | ||
+ | |||
+ | # Combine chemin et nom de fichier pour creer une seule variable | ||
+ | fn = join(racine, | ||
+ | NomFicOriginal, | ||
+ | fl = fic | ||
+ | estok, | ||
+ | |||
+ | ==== page 7 en bas ==== | ||
+ | |||
+ | if estok: | ||
+ | nomEmission = data[0] | ||
+ | saison = data[1] | ||
+ | episode = data[2] | ||
+ | print(" | ||
+ | |||
+ | ==== page 8 en haut ==== | ||
+ | |||
+ | for fic in [f for f in fichiers if f.endswith ((' | ||
+ | # Combine chemin et nom de fichier pour creer une seule variable | ||
+ | if derniere_racine != racine: | ||
+ | derniere_racine = racine | ||
+ | if len(ep_liste) > 0: | ||
+ | Manquants(ep_liste, | ||
+ | ep_liste = [] | ||
+ | emission_courante = '' | ||
+ | saison_courante = '' | ||
+ | fn = join(racine, | ||
+ | |||
+ | ==== page 8 juste en dessous ==== | ||
+ | |||
+ | estok, | ||
+ | if estok: | ||
+ | emission_courante = nomEmission = data[0] | ||
+ | saison_courante = saison = data[1] | ||
+ | episode = data[2] | ||
+ | ep_liste.append(int(episode)) | ||
+ | else: | ||
+ | |||
+ | ==== page 8 en bas ==== | ||
+ | |||
+ | # | ||
+ | def Manquants(episode_liste, | ||
+ | temp = set(range(1, | ||
+ | ret = list(temp-set(episode_liste)) | ||
+ | if len(ret) > 0: | ||
+ | print(' | ||
- | code en français : | ||
- | http:// |
issue77/tutoriel_-_python_47.1392999521.txt.gz · Dernière modification : 2014/02/21 17:18 de fredphil91