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:56] – d52fr | issue164:python [2020/12/29 15:48] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 5: | Ligne 5: | ||
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' | + | Le mois dernier, nous avons étudié la possibilité d' |
+ | la place nécessaire pour montrer | ||
- | On pourrait supposer que puisque vous pouvez soustraire deux objets | + | On pourrait supposer que puisque vous pouvez soustraire deux objets |
- | Si nous utilisons un exemple du mois dernier et que nous avons les deux objets | + | Si nous utilisons un exemple du mois dernier et que nous avons les deux objets |
**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 17: | Ligne 18: | ||
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' | + | Bien qu'il existe quelques moyens d' |
- | Après avoir fouillé sur Internet, j'ai trouvé cette discussion sur stackoverflow.com. (https:// | + | Après avoir fouillé sur Internet, j'ai trouvé cette discussion sur stackoverflow.com. (https:// |
- | Il semble que la solution soit "simplement" | + | Il semble que la solution soit « simplement |
**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 31: | Ligne 32: | ||
Requirement already satisfied: numpy in ./ | Requirement already satisfied: numpy in ./ | ||
- | Bien que cela ait fonctionné à un niveau | + | Bien que cela ait fonctionné à un niveau |
$ pip3 install numpy | $ pip3 install numpy | ||
- | Si numpy est déjà installé, | + | Si numpy est déjà installé, |
Exigence déjà satisfaite : numpy in ./ | Exigence déjà satisfaite : numpy in ./ | ||
Ligne 50: | Ligne 51: | ||
Make sure that you press < | Make sure that you press < | ||
- | Le programme suppose que vous avez les heures mises dans un fichier texte. Voici celui que j' | + | Le programme suppose que vos heures |
06:00:00 | 06:00:00 | ||
Ligne 59: | Ligne 60: | ||
08:00:00 | 08:00:00 | ||
- | Veillez à appuyer sur <Enter> après avoir effectué la dernière entrée dans le fichier texte. Vous pouvez utiliser n' | + | Veillez à appuyer sur <Entrée> après avoir effectué la dernière entrée dans le fichier texte. Vous pouvez utiliser n' |
**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: | ||
Ligne 70: | Ligne 70: | ||
[' | [' | ||
- | Maintenant que c'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 | + | 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 |
- | + | ||
- | Tout d' | + | |
+ | Tout d' | ||
- | À ce stade, les données qui se trouvent dans la variable | + | À ce stade, les données qui se trouvent dans la variable |
[' | [' | ||
Ligne 92: | 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 100: | 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 108: | Ligne 122: | ||
[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.])]** | ||
- | Je n' | + | Voyez ci-dessus la liste des listes que j' |
- | Maintenant, nous devons prendre chaque entrée | + | C'est donc la fin de la boucle for. À ce stade, le programme a imprimé chacune des valeurs |
- | # get list of [' | + | 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.] | ||
- | tmp = [i.split(" | + | Et la liste np_tims ressemble à ceci : |
- | print(f " | + | [array([6., 0., 0.]), array([3., 0., 0.]), array([2., 8., 0.]), array([ 3., 10., 0.]), array([11., 10., 0.]), array([8., 0., 0.])] |
- | + | ||
- | La déclaration imprimée est à moi - pour que nous puissions voir les données. La variable tmp contient maintenant : | + | |
- | + | ||
- | tmp=[[' | + | |
**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 134: | Ligne 149: | ||
[33. 28. 0.]** | [33. 28. 0.]** | ||
- | Nous nous rapprochons | + | Nous approchons |
# X = np.array(np_tims).sum(axis=0) / np.array([24, | # X = np.array(np_tims).sum(axis=0) / np.array([24, | ||
Ligne 158: | Ligne 173: | ||
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 retirons | + | À ce stade, nous extrayons |
hrs = X[0] | hrs = X[0] | ||
Ligne 168: | Ligne 183: | ||
33.0 28.0 | 33.0 28.0 | ||
- | Et, comme vous le savez, la valeur | + | Et, comme vous le savez, la valeur |
**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 184: | Ligne 199: | ||
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 les heures | + | Ensuite, nous convertissons |
totalsecs = (hrs * 3600) + (mins * 60) | totalsecs = (hrs * 3600) + (mins * 60) | ||
Ligne 192: | Ligne 207: | ||
TotalSecs : 120480.0 | TotalSecs : 120480.0 | ||
- | Vous POUVEZ | + | Vous POUVEZ |
Totalsecs = (X[0] * 3600) + (X[1] * 60) | Totalsecs = (X[0] * 3600) + (X[1] * 60) | ||
- | Ce qui est beaucoup plus intuitif et nécessite beaucoup moins de programmation, | + | 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 217: | Ligne 232: | ||
Bill= $836.67** | Bill= $836.67** | ||
- | Comme nous l' | + | Comme nous l' |
min, sec = divmod(totalsecs, | min, sec = divmod(totalsecs, | ||
- | heures, minutes = divmod(min, 60) | + | hours, minutes = divmod(min, 60) |
- | print(f "{heures} heures et {minutes} minutes" | + | print(f "{hours} heures et {minutes} minutes" |
33,0 heures et 28,0 minutes | 33,0 heures et 28,0 minutes | ||
- | Heureusement, | + | Heureusement, |
- | heures de facturation | + | billingratehours |
billingrateminutes = 25 / 60 | billingrateminutes = 25 / 60 | ||
- | total de la facture | + | billtotal |
- | print(f "Bill= ${billtotal : | + | print(f" |
- | Projet de loi = 836,67 | + | Total à facturer |
**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 246: | Ligne 261: | ||
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 | + | Si vous regardez la déclaration |
- | Facture | + | Total à facturer |
Ce qui n'a pas de sens pour un montant de facturation. | 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 j' | + | 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 |
- | Comme toujours, jusqu' | + | Comme toujours, jusqu' |
issue164/python.1609178198.txt.gz · Dernière modification : 2020/12/28 18:56 de d52fr