issue164:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue164:python [2020/12/28 13:54] – créée auntiee | issue164:python [2020/12/29 15:48] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Last month, we looked at using the datetime library to, among other things, calculate a billing cost for hours worked. Unfortunately, | + | **Last month, we looked at using the datetime library to, among other things, calculate a billing cost for hours worked. Unfortunately, |
One would assume that since you can subtract two datetime objects, that you could add two datetime objects just as easily. But you can’t. | One would assume that since you can subtract two datetime objects, that you could add two datetime objects just as easily. But you can’t. | ||
- | 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.** |
- | 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. | + | 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. | ||
After digging around on the Internet, I found this discussion on stackoverflow.com. (https:// | After digging around on the Internet, I found this discussion on stackoverflow.com. (https:// | ||
- | 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.** |
- | 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 … | + | 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 … | ||
$ pip3 install numpy | $ pip3 install numpy | ||
Ligne 17: | Ligne 30: | ||
If numpy is already installed, that’s ok. You’ll just get a gentle message that you have already done this … | If numpy is already installed, that’s ok. You’ll just get a gentle message that you have already done this … | ||
- | 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 | ||
- | 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. |
06:00:00 | 06:00:00 | ||
Ligne 28: | Ligne 49: | ||
08:00:00 | 08:00:00 | ||
- | Make sure that you press < | + | Make sure that you press < |
- | 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. | + | Le programme suppose que vos heures sont mises dans un fichier texte. Voici celui que j' |
- | 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, | + | 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. | ||
+ | |||
+ | 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. |
Now, we need to take each entry in our list and convert that into a list of lists. | Now, we need to take each entry in our list and convert that into a list of lists. | ||
Ligne 50: | Ligne 89: | ||
The print statement is mine – so that we can see the data. The variable tmp now contains: | The print statement is mine – so that we can see the data. The variable tmp now contains: | ||
+ | |||
+ | 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=[[' | 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. | ||
So this is the end of the for loop. At this point, the program has printed each of the values for np_tmp and has appended each into the np_tims list. Our output, at this time, looks like this: | So this is the end of the for loop. At this point, the program has printed each of the values for np_tmp and has appended each into the np_tims list. Our output, at this time, looks like this: | ||
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.] | ||
And the np_tims list looks like this: | And the np_tims list looks like this: | ||
+ | |||
+ | [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.])] | [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. |
# X = np.array(np_tims).sum(axis=0) / np.array([24, | # X = np.array(np_tims).sum(axis=0) / np.array([24, | ||
Ligne 77: | Ligne 146: | ||
Now when we hit the print(X) line, the output from our program presents us with: | Now when we hit the print(X) line, the output from our program presents us with: | ||
+ | |||
+ | [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.] | [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. |
hrs = X[0] | hrs = X[0] | ||
Ligne 90: | Ligne 171: | ||
33.0 28.0 | 33.0 28.0 | ||
- | 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.** |
- | Next, we convert the hours to seconds by multiplying by 3600, and the minutes by 60, and then adding them together. | + | À 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. | ||
totalsecs = (hrs * 3600) + (mins * 60) | totalsecs = (hrs * 3600) + (mins * 60) | ||
Ligne 104: | Ligne 197: | ||
Totalsecs = (X[0] * 3600) + (X[1] * 60) | Totalsecs = (X[0] * 3600) + (X[1] * 60) | ||
- | Which is much more intuitive and requires a lot less programming, | + | Which is much more intuitive and requires a lot less programming, |
- | 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: | + | 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: | ||
min, sec = divmod(totalsecs, | min, sec = divmod(totalsecs, | ||
Ligne 123: | Ligne 230: | ||
print(f" | print(f" | ||
- | Bill= $836.67 | + | Bill= $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: | + | 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: | ||
Bill= $836.6666666666666 | Bill= $836.6666666666666 | ||
Ligne 133: | Ligne 259: | ||
Since we spent time creating a program, I thought long and hard about how to provide the source code. If I went to pastebin as I have in the past, you would have to make two separate downloads, but if I put the code on my github repository, then you have to download only one happy little zip file. So, I have put the code in my github repository. You can download them at https:// | Since we spent time creating a program, I thought long and hard about how to provide the source code. If I went to pastebin as I have in the past, you would have to make two separate downloads, but if I put the code on my github repository, then you have to download only one happy little zip file. So, I have put the code in my github repository. You can download them at https:// | ||
- | 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.1609160094.txt.gz · Dernière modification : 2020/12/28 13:54 de auntiee