issue164:python
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 | ||
issue164:python [2020/12/28 18:48] – d52fr | issue164: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' | ||
+ | la place nécessaire pour montrer l' | ||
+ | |||
+ | 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' | ||
+ | |||
+ | Après avoir fouillé sur Internet, j'ai trouvé cette discussion sur stackoverflow.com. (https:// | ||
+ | |||
+ | Il semble que la solution soit « simplement » d' | ||
**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 ./ | Requirement already satisfied: numpy in ./ | ||
+ | |||
+ | Bien que cela ait fonctionné à un niveau basique, ce n' | ||
+ | |||
+ | $ 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 ./ | ||
**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 < | Make sure that you press < | ||
+ | |||
+ | Le programme suppose que vos heures sont mises dans un fichier texte. Voici celui que j' | ||
+ | |||
+ | 06:00:00 | ||
+ | 03:00:00 | ||
+ | 02:08:00 | ||
+ | 03:10:00 | ||
+ | 11:10:00 | ||
+ | 08:00:00 | ||
+ | |||
+ | Veillez à appuyer sur < | ||
**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, | 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, | ||
- | |||
At this point, the data that is in the variable “tmp” is: | At this point, the data that is in the variable “tmp” is: | ||
[' | [' | ||
+ | |||
+ | 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, | ||
+ | |||
+ | Tout d' | ||
+ | |||
+ | À ce stade, les données qui se trouvent dans la variable « tmp » sont : | ||
+ | |||
+ | [' | ||
**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=[[' | tmp=[[' | ||
+ | |||
+ | 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 | ||
+ | |||
+ | Maintenant, nous devons prendre chaque entrée de notre liste et la convertir en une liste de listes. | ||
+ | |||
+ | # get list of [' | ||
+ | |||
+ | tmp = [i.split(" | ||
+ | |||
+ | print(f " | ||
+ | |||
+ | La déclaration d' | ||
+ | |||
+ | tmp=[[' | ||
+ | |||
**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' | ||
+ | |||
+ | 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' | ||
+ | |||
+ | # X = np.array(np_tims).sum(axis=0) / np.array([24, | ||
+ | |||
+ | X = np.array(np_tims).sum(axis=0) # / np.array([1, | ||
+ | |||
+ | 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, | ||
+ | |||
+ | 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' | ||
**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, | Which is much more intuitive and requires a lot less programming, | ||
+ | |||
+ | 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 : 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, | ||
**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' | ||
+ | |||
+ | min, sec = divmod(totalsecs, | ||
+ | |||
+ | hours, minutes = divmod(min, 60) | ||
+ | |||
+ | print(f " | ||
+ | |||
+ | 33,0 heures et 28,0 minutes | ||
+ | |||
+ | Heureusement, | ||
+ | |||
+ | billingratehours = 25 | ||
+ | billingrateminutes = 25 / 60 | ||
+ | billtotal = (hours * billingratehours) + (minutes * billingrateminutes) | ||
+ | print(f" | ||
+ | |||
+ | 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' | ||
+ | |||
+ | Total à facturer = 836, | ||
+ | |||
+ | 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' | ||
+ | |||
+ | Comme toujours, jusqu' | ||
issue164/python.1609177716.txt.gz · Dernière modification : 2020/12/28 18:48 de d52fr