issue82:programmer_en_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 | ||
issue82:programmer_en_python [2014/06/22 21:05] – [3] fredphil91 | issue82:programmer_en_python [2014/07/25 20:46] (Version actuelle) – [10] fredphil91 | ||
---|---|---|---|
Ligne 4: | Ligne 4: | ||
Now on to bigger and better things.** | Now on to bigger and better things.** | ||
- | Avant de commencer l' | + | Avant de commencer l' |
Passons maintenant à des choses plus importantes et passionnantes. | Passons maintenant à des choses plus importantes et passionnantes. | ||
Ligne 15: | Ligne 15: | ||
The part in square brackets (IF NOT EXISTS) is totally optional, which will create the table only if it doesn’t exist already. The part in curly brackets, however, is not. The first is the new table name and the second is the query that you want to use to pull data and create the new table.** | The part in square brackets (IF NOT EXISTS) is totally optional, which will create the table only if it doesn’t exist already. The part in curly brackets, however, is not. The first is the new table name and the second is the query that you want to use to pull data and create the new table.** | ||
- | Alors que je travaillais sur mon dernier livre pour Apress, j'ai redécouvert une commande SQL dont je n' | + | Alors que je travaillais sur mon dernier livre pour Apress, j'ai redécouvert une commande SQL dont je n' |
CREATE TABLE [IF NOT EXISTS] {Nouveau nom de la table} AS SELECT {requête} | CREATE TABLE [IF NOT EXISTS] {Nouveau nom de la table} AS SELECT {requête} | ||
- | La partie entre crochets (IF NOT EXISTS) est totalement optionnelle, et créera la table seulement si elle n' | + | La partie entre crochets (IF NOT EXISTS) est totalement optionnelle et créera la table seulement si elle n' |
====== 3 ====== | ====== 3 ====== | ||
Ligne 49: | Ligne 49: | ||
====== 4 ====== | ====== 4 ====== | ||
- | So the data is showing that during the study of 65 days, only one weekday had 31 packages but 3 weekdays had 48 packages and so on. Similar queries could be created that would cover holidays and weekends. | + | **So the data is showing that during the study of 65 days, only one weekday had 31 packages but 3 weekdays had 48 packages and so on. Similar queries could be created that would cover holidays and weekends. |
While having the data simply as a returned dataset from the query, we might want to do further analysis on the data, so we want to put the resulting data from the query into a table. That's why we would create a table from the query. So in the following example, shown above right, we create a table named " | While having the data simply as a returned dataset from the query, we might want to do further analysis on the data, so we want to put the resulting data from the query into a table. That's why we would create a table from the query. So in the following example, shown above right, we create a table named " | ||
- | Now anytime we need the data for that weekday result set, we can just run a query on the weekdays table. | + | Now anytime we need the data for that weekday result set, we can just run a query on the weekdays table.** |
+ | |||
+ | Ainsi, les données montrent que lors de l' | ||
+ | |||
+ | On reçoit simplement un ensemble de données retourné par la requête, mais nous pourrions avoir envie de faire une analyse plus approfondie des données, donc nous voulons mettre les données résultant de la requête dans une table. C'est pourquoi nous aimerions créer une table à partir de la requête. Ainsi, dans l' | ||
+ | |||
+ | Maintenant, quand nous aurons besoin de cet ensemble de données des jours de semaine, nous pourrons simplement exécuter une requête sur la table « JoursDeSemaine ». | ||
====== 5 ====== | ====== 5 ====== | ||
- | Once we know what we need, and have tested the query, then we can begin our code. Assuming we already have the study table created and populated, we can use Python to then create our new table in the main database. Just as an FYI, I am using the APSW SQLite library to do the database work. | + | **Once we know what we need, and have tested the query, then we can begin our code. Assuming we already have the study table created and populated, we can use Python to then create our new table in the main database. Just as an FYI, I am using the APSW SQLite library to do the database work. |
We, of course, have to open a connection (right) and create a cursor to the SQLite database. We have covered this in a number of past articles. | We, of course, have to open a connection (right) and create a cursor to the SQLite database. We have covered this in a number of past articles. | ||
- | Now we need to create the routine that will actually create the table with the returned dataset from the query, shown below, then alter it and run some calculations. | + | Now we need to create the routine that will actually create the table with the returned dataset from the query, shown below, then alter it and run some calculations.** |
+ | |||
+ | Une fois que nous savons ce que nous voulons, et que nous avons testé la requête, nous pouvons commencer notre code. En supposant que nous avons déjà la table « étude » créée et remplie, nous pouvons utiliser Python pour créer notre nouvelle table dans la base de données principale. Pour votre information, | ||
+ | |||
+ | Nous devons, bien sûr, ouvrir une connexion (à droite) et créer un curseur pour la base de données SQLite. Nous avons vu ceci dans de nombreux articles précédents. | ||
+ | |||
+ | Maintenant, nous devons créer la routine qui crée la table avec l' | ||
====== 6 ====== | ====== 6 ====== | ||
- | As you can see, we want to create a second cursor, so that we don’t run any risk of the first cursor having data we need to maintain. We will be using it in the final part of the code. We then drop the table if it exists and run our query on the “study” table. | + | **As you can see, we want to create a second cursor, so that we don’t run any risk of the first cursor having data we need to maintain. We will be using it in the final part of the code. We then drop the table if it exists and run our query on the “study” table. |
Now we create three more columns (shown below) within the weekdays table named “probability”, | Now we create three more columns (shown below) within the weekdays table named “probability”, | ||
- | The next step (top right) will be to sum the data in the CountOfDOW field. | + | The next step (top right) will be to sum the data in the CountOfDOW field.** |
+ | |||
+ | Comme vous pouvez le voir, nous créons un deuxième curseur pour ne pas risquer que le premier curseur contienne des données que nous devons conserver. Nous l' | ||
+ | |||
+ | Maintenant, nous créons trois colonnes de plus (ci-dessous) dans la table des jours de semaine, nommées « probabilité », « inférieur » et « supérieur ». Nous faisons cela en utilisant la commande SQL « ALTER TABLE ». | ||
+ | |||
+ | L' | ||
====== 7 ====== | ====== 7 ====== | ||
- | There is only one record returned, but we do the for loop thing anyway. Remember from the above discussion that the “CountOfDow” field holds the number of days during the study that a particular number of packages came in. This gives us a value that contains the sum of all of the “CountOfDow” entries. Just so you have a reference as we go forward, the number I got from all my dummy data is 44. | + | **There is only one record returned, but we do the for loop thing anyway. Remember from the above discussion that the “CountOfDow” field holds the number of days during the study that a particular number of packages came in. This gives us a value that contains the sum of all of the “CountOfDow” entries. Just so you have a reference as we go forward, the number I got from all my dummy data is 44. |
upquery = " | upquery = " | ||
+ | |||
+ | c1 = cursor.execute(upquery)** | ||
+ | |||
+ | Il n'y a qu'un seul enregistrement retourné, mais nous faisons quand même une boucle for. Rappelez-vous de ce qui précède que le champ « NombreDeJoursDeSemaine » contient le nombre de jours au cours de l' | ||
+ | |||
+ | upquery = " | ||
c1 = cursor.execute(upquery) | c1 = cursor.execute(upquery) | ||
====== 8 ====== | ====== 8 ====== | ||
- | Here we have done a ‘SELECT all’ query so every record in the datatable is in the ‘c1’ cursor. We’ll walk through each row of the dataset, pulling the pkgs (row[0]) and CountOfDow (row[1]) data into variables. | + | **Here we have done a ‘SELECT all’ query so every record in the datatable is in the ‘c1’ cursor. We’ll walk through each row of the dataset, pulling the pkgs (row[0]) and CountOfDow (row[1]) data into variables. |
LastUpper = .0 | LastUpper = .0 | ||
Ligne 84: | Ligne 108: | ||
pkg = row[0] | pkg = row[0] | ||
- | Now we will create a probability of each daily package count in the database and calculate an upper and lower value that will be used in another process later on. Notice that we check to see if the LastUpper variable contains ‘.0’. If it does, we set it to the probability value, otherwise we set it to the lower plus the probability value. | + | Now we will create a probability of each daily package count in the database and calculate an upper and lower value that will be used in another process later on. Notice that we check to see if the LastUpper variable contains ‘.0’. If it does, we set it to the probability value, otherwise we set it to the lower plus the probability value.** |
+ | Ici, nous avons fait une requête « SELECT all », aussi chaque enregistrement de la table de données est dans le curseur « c1 ». Nous allons parcourir chaque ligne de l' | ||
+ | |||
+ | LastUpper = .0 | ||
+ | for row in c1: | ||
+ | cod = row[1] | ||
+ | pkg = row[0] | ||
+ | |||
+ | Maintenant, nous allons créer une probabilité pour chaque compte quotidien de paquets dans la base de données et calculer une valeur supérieure et inférieure qui seront utilisées dans un autre processus plus tard. Notez que nous vérifions pour voir si la variable LastUpper contient « .0 ». Si c'est le cas, nous la réglons à la valeur de probabilité, | ||
====== 9 ====== | ====== 9 ====== | ||
- | Finally we use an update SQL statement to put the new computed values into the database. | + | **Finally we use an update SQL statement to put the new computed values into the database. |
What we end up with is a package count (pkgs), a count of the number of days that package count came in, a probability of that occurring within the whole of the study (31 packages on 1 day out of a total of 44 (weekdays in that 60+ day study), will have a probability of 0.02.). | What we end up with is a package count (pkgs), a count of the number of days that package count came in, a probability of that occurring within the whole of the study (31 packages on 1 day out of a total of 44 (weekdays in that 60+ day study), will have a probability of 0.02.). | ||
- | If we add up all the probability values in the table it should add up to 1.0 . | + | If we add up all the probability values in the table it should add up to 1.0 .** |
+ | |||
+ | Enfin, nous utilisons l' | ||
+ | |||
+ | Nous nous retrouvons avec un nombre de paquet (pkgs), le décompte du nombre de jours où ce nombre de paquets est arrivé, une probabilité que cela se produise dans l' | ||
+ | |||
+ | Si l'on additionne toutes les valeurs de probabilité de la table, on devrait trouver 1. | ||
====== 10 ====== | ====== 10 ====== | ||
- | The upper and lower values then reflect a number between floating point number 0 and 1 that will mirror the possibility of any random number within that range that will give us a randomized number of packages. This number can then be used for a statistics analysis of this data. A “normal real-world” example would be to predict the number of cars that arrive at a carwash based on observational data done in the field. If you want to understand more, you could look at http:// | + | **The upper and lower values then reflect a number between floating point number 0 and 1 that will mirror the possibility of any random number within that range that will give us a randomized number of packages. This number can then be used for a statistics analysis of this data. A “normal real-world” example would be to predict the number of cars that arrive at a carwash based on observational data done in the field. If you want to understand more, you could look at http:// |
The code for the two routines that we presented this time is at: http:// | The code for the two routines that we presented this time is at: http:// | ||
- | Until next time. | + | Until next time.** |
+ | |||
+ | Les valeurs supérieures et inférieures reflètent alors un nombre compris entre 0 et 1 qui mime la possibilité qu'un nombre aléatoire soit dans cette plage et qui va nous donner un nombre aléatoire de paquets. Ce nombre peut alors être utilisé pour une analyse statistique de ces données. Un exemple du « monde réel normal » serait de prévoir le nombre de voitures qui arrivent à un centre de lavage auto sur la base d' | ||
+ | |||
+ | Le code pour les deux routines que nous avons présentées cette fois-ci est ici : http:// | ||
+ | |||
+ | À la prochaine fois. | ||
+ | |||
+ | ====== Encadrés sur fond orangés ====== | ||
+ | ===== page 11 en haut ===== | ||
+ | SELECT pkgs, Count(DOW) as NombreDeJoursDeSemaine FROM etude | ||
+ | WHERE (Holiday <> 1) | ||
+ | AND DayName in (" | ||
+ | GROUP BY pkgs | ||
+ | |||
+ | ===== page 11 en bas ===== | ||
+ | pkID - Integer, Primary Key, AutoIncrement | ||
+ | DOM - Integer - Jour du mois (1-31) | ||
+ | DOW - Integer - Jour de la semaine (1-7 (dimanche = 1, lundi = 2, etc.)) | ||
+ | pkgs - Integer - Nombre de colis reçus ce jour | ||
+ | DayName - TEXT - " | ||
+ | Holiday - Integer 0 ou 1 (Est-ce que ce jour est considéré comme congé ou pas) 1 pour oui | ||
+ | |||
+ | ===== page 12 en haut ===== | ||
+ | CREATE TABLE IF NOT EXISTS JoursDeSemaine AS | ||
+ | | ||
+ | WHERE (Holiday <> 1) | ||
+ | AND DayName in (" | ||
+ | GROUP BY pkgs | ||
+ | |||
+ | ===== page 12 au milieu haut ===== | ||
+ | def OpenDB(): | ||
+ | global connection | ||
+ | global curseur | ||
+ | connection = apsw.Connection(" | ||
+ | curseur = connection.cursor() | ||
+ | |||
+ | ===== page 12 au milieu bas ===== | ||
+ | addcolquery = 'ALTER TABLE JoursDeSemaine ADD COLUMN probability REAL' | ||
+ | cursor.execute(addcolquery) | ||
+ | addcolquery = 'ALTER TABLE JoursDeSemaine ADD COLUMN lower REAL' | ||
+ | cursor.execute(addcolquery) | ||
+ | addcolquery = 'ALTER TABLE JoursDeSemaine ADD COLUMN upper REAL' | ||
+ | cursor.execute(addcolquery) | ||
+ | |||
+ | ===== page 12 en bas ===== | ||
+ | def TraiterJoursDeSemaine(): | ||
+ | # on cree un second curseur pour mettre a jour la nouvelle table | ||
+ | | ||
+ | q1 = "DROP TABLE IF EXISTS JoursDeSemaine" | ||
+ | | ||
+ | query = ''' | ||
+ | Count(DOW) as NombreDeJoursDeSemaine FROM etude WHERE (Holiday <> 1) | ||
+ | AND DayName in | ||
+ | (" | ||
+ | GROUP BY pkgs''' | ||
+ | | ||
+ | |||
+ | ===== page 13 ===== | ||
+ | sumquery = " | ||
+ | tmp = curseur.execute(sumquery) | ||
+ | for t in tmp: | ||
+ | | ||
+ | |||
+ | prob = cod / float(DaySum) | ||
+ | if LastUpper != .0: | ||
+ | lower = LastUpper | ||
+ | | ||
+ | else: | ||
+ | lower = .0 | ||
+ | | ||
+ | nquery = ' | ||
+ | lower = %f, upper = %f WHERE pkgs = %d' \ | ||
+ | % (prob, | ||
+ | u = cursor2.execute(nquery) | ||
+ | # | ||
+ | # | ||
+ | End of TraiterJoursDeSemaine | ||
+ | # | ||
issue82/programmer_en_python.1403463914.txt.gz · Dernière modification : 2014/06/22 21:05 de fredphil91