Outils pour utilisateurs

Outils du site


issue164:python

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
issue164:python [2020/12/28 18:48] d52frissue164:python [2020/12/29 15:48] (Version actuelle) andre_domenech
Ligne 4: Ligne 4:
  
 If we use an example from last month and have the two datetime objects st and et (meaning start time and end time), and try to add them – which really makes no sense, but let’s try it anyway – you will receive the text shown below.** If we use an example from last month and have the two datetime objects st and et (meaning start time and end time), and try to add them – which really makes no sense, but let’s try it anyway – you will receive the text shown below.**
 +
 +Le mois dernier, nous avons étudié la possibilité d'utiliser la bibliothèque datetime pour, entre autres, calculer un coût de facturation des heures travaillées. Malheureusement, je n'ai eu ni le temps ni
 +la place nécessaire pour montrer l'ajout d'heures de travail multiples afin d'obtenir un « total général » à facturer au client.
 +
 +On pourrait supposer que puisque vous pouvez soustraire deux objets datetime, vous pourriez ajouter deux objets datetime tout aussi facilement. Mais ce n'est pas le cas. 
 +
 +Si nous utilisons un exemple du mois dernier et que nous avons les deux objets datetime st et et (qui signifient heure de début et heure de fin), et que nous essayons de les ajouter - ce qui n'a vraiment aucun sens, mais essayons quand même - vous recevrez le texte ci-dessous.
  
 **While there are a few ways to actually add times using datediff, they are very clumsy, and I really don’t think I could properly explain it, so I started looking for a better solution. **While there are a few ways to actually add times using datediff, they are very clumsy, and I really don’t think I could properly explain it, so I started looking for a better solution.
Ligne 10: Ligne 17:
  
 It seems the solution is “simply” to use the numpy library. After playing around with the code, I realized that, for my needs, it didn’t quite give me what I needed. Here (top right) is his original code, including his comments.** It seems the solution is “simply” to use the numpy library. After playing around with the code, I realized that, for my needs, it didn’t quite give me what I needed. Here (top right) is his original code, including his comments.**
 +
 +Bien qu'il existe quelques moyens d'ajouter des heures en utilisant datediff, ils sont très lourdingues, et je ne pense pas pouvoir les expliquer correctement ; aussi, j'ai commencé à chercher une meilleure solution.
 +
 +Après avoir fouillé sur Internet, j'ai trouvé cette discussion sur stackoverflow.com. (https://stackoverflow.com/questions/2410454/adding-up-time-durations-in-python). C'est Banderlog013 qui a le mieux répondu à la question, aussi j'ai pris une copie de son code.
 +
 +Il semble que la solution soit « simplement » d'utiliser la bibliothèque numpy. Après avoir joué avec le code, j'ai réalisé que, pour mes besoins, il ne me donnait pas tout à fait ce dont j'avais besoin. Voici (en haut à droite) son code original, y compris ses commentaires.
  
 **While this worked on a basic level, it wasn’t really what I wanted. So I started modifying the code. But, before we get too deep into the code, I will remind you that you need to have the numpy library installed. Most of my regular readers already have done this, but let’s go through the motions – just in case you haven’t done this yet. You can simply use pip (or pip3) to install numpy … **While this worked on a basic level, it wasn’t really what I wanted. So I started modifying the code. But, before we get too deep into the code, I will remind you that you need to have the numpy library installed. Most of my regular readers already have done this, but let’s go through the motions – just in case you haven’t done this yet. You can simply use pip (or pip3) to install numpy …
Ligne 18: Ligne 31:
  
 Requirement already satisfied: numpy in ./.local/lib/python3.8/site-packages (1.19.2)** Requirement already satisfied: numpy in ./.local/lib/python3.8/site-packages (1.19.2)**
 +
 +Bien que cela ait fonctionné à un niveau basique, ce n'était pas vraiment ce que je voulais. J'ai donc commencé à modifier le code. Mais, avant d'y entrer trop à fond, je vous rappelle que vous devez installer la bibliothèque numpy. La plupart de mes lecteurs réguliers l'ont déjà fait, mais passons à l'action - au cas où vous ne l'auriez pas encore fait. Vous pouvez simplement utiliser pip (ou pip3) pour installer numpy...
 +
 +$ pip3 install numpy
 +
 +Si numpy est déjà installé, ce n'est pas grave. Vous recevrez juste un petit message vous indiquant que vous avez déjà fait cela...
 +
 +Exigence déjà satisfaite : numpy in ./.local/lib/python3.8/site-packages (1.19.2)
  
 **The program assumes that you have the hours put into a text file. Here ‘s the one that I will use for this project. It’s simply just a series of task times for our mythical employee. One entry per line. **The program assumes that you have the hours put into a text file. Here ‘s the one that I will use for this project. It’s simply just a series of task times for our mythical employee. One entry per line.
Ligne 29: Ligne 50:
  
 Make sure that you press <Enter> after you make the last entry in the text file. You can use any text editor you wish, from Vim, to nano, or your favorite IDE. Save the file as “hours-11-20-20.txt” .** Make sure that you press <Enter> after you make the last entry in the text file. You can use any text editor you wish, from Vim, to nano, or your favorite IDE. Save the file as “hours-11-20-20.txt” .**
 +
 +Le programme suppose que vos heures sont mises dans un fichier texte. Voici celui que j'utiliserai pour ce projet. Il s'agit simplement d'une série de durées de tâches pour notre employé mythique. Une entrée par ligne.
 +
 +06:00:00
 +03:00:00
 +02:08:00
 +03:10:00
 +11:10:00
 +08:00:00
 +
 +Veillez à appuyer sur <Entrée> après avoir effectué la dernière entrée dans le fichier texte. Vous pouvez utiliser n'importe quel éditeur de texte, de Vim à nano, ou votre IDE préféré. Enregistrez le fichier sous le nom « hours-11-20-20.txt » .
  
 **Now that is taken care of, let’s look at the code (after I modified it), block by block, with some explanations along the way. To run the program, you will need to use a version of Python that is 3.7 or later, since I use “f-strings” throughout. **Now that is taken care of, let’s look at the code (after I modified it), block by block, with some explanations along the way. To run the program, you will need to use a version of Python that is 3.7 or later, since I use “f-strings” throughout.
  
 First, we need to import numpy into our program, and then read the file. The data from the file is going to be put into a variable named “x”. At this point, most of this this is the code from Banderlog013, including his original comments (top right). First, we need to import numpy into our program, and then read the file. The data from the file is going to be put into a variable named “x”. At this point, most of this this is the code from Banderlog013, including his original comments (top right).
- 
  
 At this point, the data that is in the variable “tmp” is: At this point, the data that is in the variable “tmp” is:
  
 ['06:00:00', '03:00:00', '02:08:00', '03:10:00', '11:10:00', '08:00:00', '']** ['06:00:00', '03:00:00', '02:08:00', '03:10:00', '11:10:00', '08:00:00', '']**
 +
 +Maintenant que cela est réglé, regardons le code (après que je l'ai modifié), bloc par bloc, avec quelques explications en cours de route. Pour exécuter le programme, vous devrez utiliser Python en version 3.7 ou ultérieure, puisque j'utilise des « f-strings » tout au long du programme.
 +
 +Tout d'abord, nous devons importer numpy dans notre programme, puis lire le fichier. Les données du fichier vont être placées dans une variable nommée « x ». À ce stade, il s'agit pour l'essentiel du code de Banderlog013, y compris ses commentaires originaux (en haut à droite).
 +
 +À ce stade, les données qui se trouvent dans la variable « tmp » sont :
 +
 +['06:00:00', '03:00:00', '02:08:00', '03:10:00', '11:10:00', '08:00:00', '']
  
 **I didn’t include microseconds in my data entries, so it’s just a list of the simple strings that we had entered into the file. Also notice that the last element of the list is a blank string, which is why he mentions that he needed to drop the last item in his comments. **I didn’t include microseconds in my data entries, so it’s just a list of the simple strings that we had entered into the file. Also notice that the last element of the list is a blank string, which is why he mentions that he needed to drop the last item in his comments.
Ligne 52: Ligne 91:
  
 tmp=[['06', '00', '00'], ['03', '00', '00'], ['02', '08', '00'], ['03', '10', '00'], ['11', '10', '00'], ['08', '00', '00'], ['']]** tmp=[['06', '00', '00'], ['03', '00', '00'], ['02', '08', '00'], ['03', '10', '00'], ['11', '10', '00'], ['08', '00', '00'], ['']]**
 +
 +Je n'ai pas inclus les microsecondes dans mes entrées de données, donc c'est simplement la liste des chaînes que nous avions entrées dans le fichier. Notez également que le dernier élément de la liste est une chaîne vide, c'est pourquoi il mentionne  dans ses commentaires qu'il a dû laisser tomber le dernier élément.
 +
 +Maintenant, nous devons prendre chaque entrée de notre liste et la convertir en une liste de listes.
 +
 +# get list of ['00', 02, '12.31']
 +
 +tmp = [i.split(" :") pour i dans tmp.copy()]
 +
 +print(f "tmp={tmp}")
 +
 +La déclaration d'impression est de moi, pour que nous puissions voir les données. La variable tmp contient maintenant :
 +
 +tmp=[['06', '00', '00'], ['03', '00', '00'], ['02', '08', '00'], ['03', '10', '00'], ['11', '10', '00'], ['08', '00', '00'], ['']]
 +
  
 **There is the list of lists I mentioned above. Next, we create an empty list that will hold each of the items and then walk through each item, convert that to a numpy float array, append that to the empty list (np_tims) see below. **There is the list of lists I mentioned above. Next, we create an empty list that will hold each of the items and then walk through each item, convert that to a numpy float array, append that to the empty list (np_tims) see below.
Ligne 60: Ligne 114:
 np_tmp=[3. 0. 0.] np_tmp=[3. 0. 0.]
 np_tmp=[2. 8. 0.] np_tmp=[2. 8. 0.]
-np_tmp=[ 3. 10. 0.]+np_tmp=[3. 10. 0.]
 np_tmp=[11. 10. 0.] np_tmp=[11. 10. 0.]
 np_tmp=[8. 0. 0.] np_tmp=[8. 0. 0.]
Ligne 67: Ligne 121:
  
 [array([6., 0., 0.]), array([3., 0., 0.]), array([2., 8., 0.]), array([ 3., 10., 0.]), array([11., 10., 0.]), array([8., 0., 0.])]** [array([6., 0., 0.]), array([3., 0., 0.]), array([2., 8., 0.]), array([ 3., 10., 0.]), array([11., 10., 0.]), array([8., 0., 0.])]**
 +
 +Voyez ci-dessus la liste des listes que j'ai mentionnée. Ensuite, nous créons une liste vide qui contiendra chacun des éléments, puis nous parcourons chaque élément, nous le convertissons en un tableau numpy en virgule flottante, nous l'ajoutons à la liste vide (np_tims) ; voir ci-dessous.
 +
 +C'est donc la fin de la boucle for. À ce stade, le programme a imprimé chacune des valeurs de np_tmp et les a ajoutées à la liste np_tims. Notre sortie, à ce stade, ressemble à ceci :
 +
 +np_tmp=[6. 0. 0.]
 +np_tmp=[3. 0. 0.]
 +np_tmp=[2. 8. 0.]
 +np_tmp=[3. 10. 0.]
 +np_tmp=[11. 10. 0.]
 +np_tmp=[8. 0. 0.]
 +
 +Et la liste np_tims ressemble à ceci :
 +
 +[array([6., 0., 0.]), array([3., 0., 0.]), array([2., 8., 0.]), array([ 3., 10., 0.]), array([11., 10., 0.]), array([8., 0., 0.])]
  
 **We are getting close to the “magic” of what the program does. Since we are done with the for loop, we’ll now use the .sum function of numpy. He originally divided the array sums by another array of [24, 60, 1000]. However when that happened, it threw the numbers off. So I changed the code to leave the array sums as it was, which worked for me. **We are getting close to the “magic” of what the program does. Since we are done with the for loop, we’ll now use the .sum function of numpy. He originally divided the array sums by another array of [24, 60, 1000]. However when that happened, it threw the numbers off. So I changed the code to leave the array sums as it was, which worked for me.
Ligne 79: Ligne 148:
  
 [33. 28. 0.]** [33. 28. 0.]**
 +
 +Nous approchons de la partie « magique » du programme. Comme nous en avons fini avec la boucle for, nous allons maintenant utiliser la fonction .sum de numpy. Au début, il divisait les sommes du tableau par un autre tableau de [24, 60, 1000]. Cependant, lorsque cela s'exécutait, il y avait des erreurs de valeurs. J'ai donc changé le code pour laisser les tableaux de sommes tels quels, ce qui a fonctionné pour moi.
 +
 +# X = np.array(np_tims).sum(axis=0) / np.array([24, 60, 1000])
 +
 +X = np.array(np_tims).sum(axis=0) # / np.array([1, 60, 1000])
 +
 +imprimer(X)
 +
 +Maintenant, lorsque nous appuyons sur la ligne print(X), la sortie de notre programme nous présente :
 +
 +[33. 28. 0.]
  
 **At this point, we pull the hour and minute values from the list so that we can easily deal with them. **At this point, we pull the hour and minute values from the list so that we can easily deal with them.
Ligne 91: Ligne 172:
  
 And, as you know, the value on the left is hours with the value on the right being the minutes. Notice that I don’t really care about seconds at this point, so they are ignored.** And, as you know, the value on the left is hours with the value on the right being the minutes. Notice that I don’t really care about seconds at this point, so they are ignored.**
 +
 +À ce stade, nous extrayons les valeurs des heures et des minutes de la liste afin de pouvoir les traiter facilement.
 +
 +hrs = X[0]
 +mins = X[1]
 +imprimer(hrs, mins)
 +
 +Et le résultat serait ici :
 +
 +33.0 28.0
 +
 +Et, comme vous le savez, la valeur de gauche est celle des heures et la valeur de droite celle des minutes. Remarquez que je ne m'occupe pas vraiment des secondes à ce stade, donc elles sont ignorées.
  
 **Next, we convert the hours to seconds by multiplying by 3600, and the minutes by 60, and then adding them together. **Next, we convert the hours to seconds by multiplying by 3600, and the minutes by 60, and then adding them together.
Ligne 105: Ligne 198:
  
 Which is much more intuitive and requires a lot less programming, but I wanted to break it down into easily “digestible chunks”. ** Which is much more intuitive and requires a lot less programming, but I wanted to break it down into easily “digestible chunks”. **
 +
 +Ensuite, nous convertissons en secondes les heures en les multipliant par 3600, et les minutes par 60, puis nous les additionnons.
 +
 +totalsecs = (hrs * 3600) + (mins * 60)
 +
 +print(f "TotalSecs : {totalsecs}")
 +
 +TotalSecs : 120480.0
 +
 +Vous POUVEZ les regrouper :
 +
 +Totalsecs = (X[0] * 3600) + (X[1] * 60)
 +
 +Ce qui est beaucoup plus intuitif et nécessite beaucoup moins de programmation, mais je voulais le décomposer en « morceaux facilement digestes ». 
  
 **As we did last month, we use divmod to convert the numbers into hours, minutes and seconds just in case we have more minutes than 60 so the values correlate correctly: **As we did last month, we use divmod to convert the numbers into hours, minutes and seconds just in case we have more minutes than 60 so the values correlate correctly:
Ligne 124: Ligne 231:
  
 Bill= $836.67** Bill= $836.67**
 +
 +Comme nous l'avons fait le mois dernier, nous utilisons divmod pour convertir les chiffres en heures, minutes et secondes, au cas où nous aurions plus de 60 minutes, afin que les valeurs soient correctement corrélées :
 +
 +min, sec = divmod(totalsecs, 60)
 +
 +hours, minutes = divmod(min, 60)
 +
 +print(f "{hours} heures et {minutes} minutes")
 +
 +33,0 heures et 28,0 minutes
 +
 +Heureusement, dans cet exemple, les chiffres correspondent. Enfin, nous appliquons notre taux de facturation. Dans ce cas, notre mythique programmeur facture 25 euros de l'heure, ET nous facturons des portions d'heure au lieu de tout arrondir à l'heure supérieure.
 +
 +billingratehours = 25
 +billingrateminutes = 25 / 60
 +billtotal = (hours * billingratehours) + (minutes * billingrateminutes)
 +print(f"Total à facturer = {billtotal:.2f} €")
 +
 +Total à facturer = 836,67 €
  
 **If you look at the print statement at the end of the code block, we format the total amount to be billed to only two decimal points by using the “:.2f” constructor. What if we didn’t format the billtotal variable? The program would print: **If you look at the print statement at the end of the code block, we format the total amount to be billed to only two decimal points by using the “:.2f” constructor. What if we didn’t format the billtotal variable? The program would print:
Ligne 134: Ligne 260:
  
 As always, until next time; stay safe, healthy, positive and creative!** As always, until next time; stay safe, healthy, positive and creative!**
 +
 +Si vous regardez la déclaration d'impression à la fin du bloc de code, nous formatons le montant total à facturer avec deux décimales seulement en utilisant le constructeur « :.2f ». Et que ce serait-il passé si nous n'avions pas formaté la variable « billtotal » ? Le programme aurait imprimé :
 +
 +Total à facturer = 836,6666666666666 €
 +
 +Ce qui n'a pas de sens pour un montant de facturation.
 +
 +Comme nous avons passé du temps à créer un programme, j'ai longuement réfléchi à la manière de fournir le code source. Si je le mettais sur pastebin comme je l'ai fait dans le passé, il faudrait faire deux téléchargements séparés, mais si je mettais le code sur mon dépôt github, alors il ne faudrait télécharger qu'un seul petit fichier zip. J'ai donc mis le code dans mon dépôt github. Vous pouvez le télécharger à l'adresse suivante : https://github.com/gregwa1953/FCM164.
 +
 +Comme toujours, jusqu'à la prochaine fois ; restez prudent, en bonne santé, positif et créatif !
  
issue164/python.1609177716.txt.gz · Dernière modification : 2020/12/28 18:48 de d52fr