issue134:mon_histoire
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 | ||
issue134:mon_histoire [2018/07/22 15:44] – auntiee | issue134:mon_histoire [2018/07/25 20:04] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 9: | Ligne 9: | ||
Je devais décider si je voulais écrire un programme en ligne de commande (CLI) ou avec une interface utilisateur graphique (GUI). Un programme écrit pour CLI est lancé dans un terminal, par exemple grep ou mp3diags alors qu'en GUI, il y a des fenêtres et des icônes, par exemple Audacity ou l' | Je devais décider si je voulais écrire un programme en ligne de commande (CLI) ou avec une interface utilisateur graphique (GUI). Un programme écrit pour CLI est lancé dans un terminal, par exemple grep ou mp3diags alors qu'en GUI, il y a des fenêtres et des icônes, par exemple Audacity ou l' | ||
- | Bien entendu, un programme en GUI est plus sympa et semble le choix préférable. Comme j'ai dit dans l' | + | Bien entendu, un programme en GUI est plus sympa et semble le choix préférable. Comme j'ai dit dans l' |
**I have also seen that some linux applications are CLI based and then they also have GUI version, which looked like it was using the command based program to do the job. For example some software management application, | **I have also seen that some linux applications are CLI based and then they also have GUI version, which looked like it was using the command based program to do the job. For example some software management application, | ||
Ligne 21: | Ligne 21: | ||
Je l'ai appelé fixrhy en supposant qu'il réparerait quelque chose que rhythmbox ne faisait pas comme je voulais. Je sais que c'est discutable, mais après tout, c'est mon application et je peux lui donner le nom que je veux. | Je l'ai appelé fixrhy en supposant qu'il réparerait quelque chose que rhythmbox ne faisait pas comme je voulais. Je sais que c'est discutable, mais après tout, c'est mon application et je peux lui donner le nom que je veux. | ||
- | Étant prêt à commencer mon tout premier programme en Python, j'ai lancé l' | + | Étant prêt à commencer mon tout premier programme en Python, j'ai lancé l' |
**The logic of the program is very simple. As input it requires the name of the artist, the name of the album, the sorting name of the artist and the sorting name of the album. After writing the basic code I added a fifth parameter which I’ll talk about later. The output would be just some confirmation messages, while the real action is actually manipulating the xml file that rhythmbox uses to store its database. The full code of the final version is available here: https:// | **The logic of the program is very simple. As input it requires the name of the artist, the name of the album, the sorting name of the artist and the sorting name of the album. After writing the basic code I added a fifth parameter which I’ll talk about later. The output would be just some confirmation messages, while the real action is actually manipulating the xml file that rhythmbox uses to store its database. The full code of the final version is available here: https:// | ||
Ligne 28: | Ligne 28: | ||
# | # | ||
- | Technically this is needed to execute the code as a Python program. To me it is the statement that I wanted to put in practice something I learned on Full Circle pages, and knowing that if I need help there’ll be a community out there ready to help. I could have used C (or C++) that I already knew, but that would have taken away a bit of the fun. Learning something new it is always challenging and it offers more opportunity of reaching out for others and live in the community. Case in point, I eventually wrote to Greg Walter and Ronnie Tucker (I’ll come back to it later on). | + | La logique du programme est très simple. Vous devez renseigner le nom de l' |
+ | |||
+ | La ligne 14 est : | ||
+ | # | ||
+ | |||
+ | **Technically this is needed to execute the code as a Python program. To me it is the statement that I wanted to put in practice something I learned on Full Circle pages, and knowing that if I need help there’ll be a community out there ready to help. I could have used C (or C++) that I already knew, but that would have taken away a bit of the fun. Learning something new it is always challenging and it offers more opportunity of reaching out for others and live in the community. Case in point, I eventually wrote to Greg Walter and Ronnie Tucker (I’ll come back to it later on). | ||
Line 18 is: | Line 18 is: | ||
Ligne 36: | Ligne 41: | ||
Line 22 is: | Line 22 is: | ||
+ | sys.setdefaultencoding( " | ||
+ | |||
+ | Techniquement, | ||
+ | |||
+ | la ligne 18 est : | ||
+ | import xml.etree.ElementTree as ET | ||
+ | |||
+ | Ceci est nécessaire pour pouvoir utiliser la bibliothèque ElementTree pour analyser les fichiers XML. J' | ||
+ | |||
+ | La ligne 22 est : | ||
sys.setdefaultencoding( " | sys.setdefaultencoding( " | ||
- | If I recall correctly this became necessary after struggling with accented letters (as in Beyoncé) that are quite common in names. | + | **If I recall correctly this became necessary after struggling with accented letters (as in Beyoncé) that are quite common in names. |
Next I have to check if I have enough information to go on. Remember we said we need 4 pieces of data, 2 to identify an album (artist and album name) and 2 to set their respective sort values. The fifth parameter is option (to override existing sorting information). We do it at line 25: | Next I have to check if I have enough information to go on. Remember we said we need 4 pieces of data, 2 to identify an album (artist and album name) and 2 to set their respective sort values. The fifth parameter is option (to override existing sorting information). We do it at line 25: | ||
Ligne 44: | Ligne 59: | ||
if len(sys.argv) < 5: | if len(sys.argv) < 5: | ||
- | Note that we check for 5 since the first value is always the script name (check https:// | + | Note that we check for 5 since the first value is always the script name (check https:// |
- | Line 42 and 43 looks like debugging lines: | + | Si je m'en souviens bien, cela est devenu obligatoire après m' |
+ | |||
+ | Ensuite, je dois vérifier si j'ai assez d' | ||
+ | |||
+ | if len(sys.argv) < 5: | ||
+ | |||
+ | Notez que nous vérifions pour 5, puisque la première valeur est toujours le nom du script (regardez à https:// | ||
+ | |||
+ | **Line 42 and 43 looks like debugging lines: | ||
print " | print " | ||
Ligne 63: | Ligne 86: | ||
if entry.find(' | if entry.find(' | ||
- | If any of them is false we can discard that given entry since we don’t need to amend it. | + | If any of them is false we can discard that given entry since we don’t need to amend it. ** |
- | From line 58 we check if the artist sort information is already there. The reason being that if the field is missing we need to create it new (60), if it is already there we have to change it (or leave it unchanged). Former case we could not modify something not existing, latter case we should not create something that is already there. | + | Les lignes 42 et 43 ressemblent à des lignes de débogage : |
+ | print " | ||
+ | |||
+ | print " | ||
+ | |||
+ | et, en fait, elles l' | ||
+ | |||
+ | De 45 à 50, nous réglons tout, chargeant la base de données de Rhythmbox dans un arbre (47) et déclarant quelques variables de comptage. | ||
+ | |||
+ | Le véritable traitement commence à la ligne 52 ou nous parcourons chaque entrée dans l' | ||
+ | |||
+ | if entry.attrib == {' | ||
+ | |||
+ | if entry.find(' | ||
+ | |||
+ | if entry.find(' | ||
+ | |||
+ | Si une entrée quelconque est fausse, nous pouvons la supprimer, puisque nous n' | ||
+ | |||
+ | **From line 58 we check if the artist sort information is already there. The reason being that if the field is missing we need to create it new (60), if it is already there we have to change it (or leave it unchanged). Former case we could not modify something not existing, latter case we should not create something that is already there. | ||
+ | |||
+ | The rest of the code builds on that, and there is a similar part of the album sort information. The only two points I want to make here are that I put in the code the parameter to choose if to modify existing data or not. Personally I would always change it, but I wanted to write a program that eventually could be used by others, maybe with different needs. Also note that since I couldn’t change the element if existing I destroy and create a new one (68-70). Most of the print commands are there for debug originally and then to be used by GUI as I said before about line 42/43.** | ||
+ | |||
+ | À partir de la ligne 58, nous vérifions si l' | ||
- | The rest of the code builds on that, and there is a similar part of the album sort information. The only two points I want to make here are that I put in the code the parameter to choose if to modify existing data or not. Personally I would always change it, but I wanted to write a program that eventually could be used by others, maybe with different needs. Also note that since I couldn’t change the element if existing I destroy and create a new one (68-70). | + | Le reste du code s' |
- | Finally before saving we make a backup copy (always important!): | + | **Finally before saving we make a backup copy (always important!): |
shutil.copy2(filename, | shutil.copy2(filename, | ||
Ligne 77: | Ligne 123: | ||
tree.write(filename, | tree.write(filename, | ||
- | I am quite happy with my first Python program ever. It does what it says on the tin but there is room for much improvements. Amongst them surely pass the file position as a parameter (like this you should run the script in the folder with the Rhythmbox DB file). The parsing maybe not the most efficient and fast. The naming could be done automatically, | + | I am quite happy with my first Python program ever. It does what it says on the tin but there is room for much improvements. Amongst them surely pass the file position as a parameter (like this you should run the script in the folder with the Rhythmbox DB file). The parsing maybe not the most efficient and fast. The naming could be done automatically, |
+ | |||
+ | Enfin, avant de l' | ||
+ | |||
+ | shutil.copy2(filename, | ||
+ | |||
+ | et l' | ||
+ | |||
+ | Mon tout premier programme en Python me plaît bien. Il fait ce qu'il est censé faire, mais il y a une marge de progression et il peut être amélioré. Parmi de telles améliorations, | ||
+ | |||
issue134/mon_histoire.1532267092.txt.gz · Dernière modification : 2018/07/22 15:44 de auntiee