Mastering File Handling Part 2: Managing Data Records with Pickle.
Greetings Steemit friends
1)This task involves creating an application focusing on tracking and managing outdoor exercises like running, walking, and cycling.
This looks complicated, but it becomes exciting when you identify the right widget boxes to use. We already have the basics from our previous task on how to use and look for the widget box.
This week, I will use a new widget box. The QGroupBox
is the new widget I have come across. With this widget, I will be able to hold more than one widget box. It becomes pretty easy to achieve the design. As you can see I have been able to group input, display widgets, and button widgets in one widget box.
I have saved my file in the project directory as course.ui
. Now let's move Thonny's application to start development.
I start by creating my .py
file, which we have been doing. Let's start by importing the necessary libraries for the task. In this task, we will be introducing new libraries in addition to the pickle
. We had an explanation of the pickle libraries, which help serialize and deserialize Python objects to and from a file. This is the main objective of studies week on hold to serialize and deserialize data and manipulate.
I have created a file ( typeCourse.txt )
that will store the different race types needed for the dropdown. We have two dropdowns, which will be populated with the data on the file ( typeCourse.txt )
. So we need a function to handle the dropdown, which will need to open the file and read. This is not difficult to understand, it is the concept studied in the previous class. So my function will clear and populate the two dropdowns with the most recent read data from the file (typeCourse.txt)
.
The next function is to convert the duration from string to float. It was not possible to work with string format, so I passed the duration input as a parameter. I used the (:)
to break, converting the minutes and seconds to hours and adding to the hours to have the total hours.
Next, we had the ADD()
function. Here we list out all the input required, which is needed to add a new record. Here we are making use of the parse_duration
function To avoid reductants, the race type will either be from the dropdown or from the input to add a new type.
Note, that if we have a new type. We will need to write, and update the typesCourse.txt file. Now at this point, we have all the necessary data to add or let's say serialize and deserialize on the exercice.dat file. The pickle.load()
is used to deserialize the data on the exercise.dat file. The pickle.dump()
is used to serialize the records[]
, including the recently added record to the list.
The next section of our application is the DISPLAY()
function, which will handle the output results. We have the second dropdown which will be used to filter our results. I have checked if we have data on the exercise.dat file. If not, make use of an empty list.
Now, filter the records if the selected type is in the list. Here we make use of the QStandardItemModel()
to create a table format to populate the filter record. This will create variables to hold the data for the different records. Use the sum()
function to compute the total of each record. Here it will be possible to have the total distance multiplied by 85.
Now we can append and populate the history table. Same time we have the summary data to append and populate the overview table. From here it is not different from what we have been practicing in previous sessions.
Note, something new this week was replacing the default icon and title. For us to achieve the icon, we have to import the QIcon
library. Below the connect buttons, we have the function to populate the two dropdowns with the data from the typesCourse.txt when the application starts.
2)We aim to develop an application for managing a TV channel database stored in the file listChannels.dat.
Great, this will be a similar task as compared to the previous task. A lot will not change as I will be making use of lines of code from the previous application. In our QT design, we already have the concept of putting widget boxes in groups. So I think we should not have any problem adding fields to have the required input for our program. I will move directly to our Thonny application to create the .py file for PyQt5 codes.
I will make use of the populate_dropdown()
, which will be used to populate the dropdowns in the application. The only change here will be the file categories.txt holding the categories.
Next, we have the ADD()
function, which is triggered by the add button. Here we are using the pickle.load()
and pickle.dump()
function to deserialize and serialize data before saving on the file.
I have assigned all the inputs to their respective variables. Each variable is checked to validate the input and make sure all inputs are filed and valid. I made sure the frequency is within the range of 10,000 and 20,000, and sr fac is made up of five digits.
From here the rest code is pretty much the same, but I have appended the new channel to the channelList
. Now we can save the update list to listChannels.dat
using pickle.
FIND()
is not different from the previous application, but in this case, we want to search and display channels that match the input. Here I have added one more input channel_name
.
Here we get the channelList
and filter by looping to find-match based on the input values. The result is stored in the variable history_model
, which is then displayed by populating the table.
The rest from here is pretty much the same, but make sure to replace the .ui
file. Note, that your program will only run successfully if the connect buttons have the correct object names.
3)The application enables users to input, validate, save, and display data in a user-friendly manner using Python and PyQt5.
From the previous task, this one will not be complicated. Every widget box I have touched in the previous task. So it was pretty easy and less time-consuming to put every necessary widget box together.
We have our ui file, I move straight to the Thonny application and create my .py
file. Just like every project, you need to start with the necessary libraries. We can add others as the need may be within the scope of the project.
Following the task, it depends on whether you want to have a list of languages on a file or manually type in your code. So in this case we have two options, which is the option of creating a file to have the list of languages. It is easy since the previous task has enlightened us. I will make use of the function used in the task above, but note this time we have just one dropdown to populate.
The core of the project is the SAVE()
function. Here I have to collect all input data, validate, load, and append new information to the list. Here I had to put some emphasis on the checkbox widget. Checkboxes are different from radio boxes, you can select more than one checkbox. For this reason, I can only hold this data as an array and manipulate it as a list.
So that is why I had to introduce the programming_langs
to store the checkboxes. Now we should have all valid input to use the pickle.load()
function to load existing data on the list. We can then go ahead to Serialize and save the most recent developerSheetList
.
Just like the previous task, at this point, we have the most updated list to populate the table and display the records. I said this project was more straightforward than the other because we did not have any computing at any point. The most essential is checking and validating input.
Here comes the end of Season 6, I want to use this opportunity to thank the steemit team and @kouba01 for this particular course. I have gone ahead to research how to create an executable file, which will enable me to be able to run my program on another computer. I will encourage others to make use of this opportunity, in addition to the votes.
Cheers
Thanks for dropping by
@fombae
Upvoted! Thank you for supporting witness @jswit.