Press enter to skip the top menu

Python Programming

Serialisation

Learning Outcomes

On completion of this page you will know:

Go to top

Introduction

In an earlier lesson we looked at saving data to a text file. In that lesson we were able to save only unstructured text and numeric data. Again we had to write each individual data item to the file. The unstructured data saved to those files could be easily viewed in any simple text editor.

Since then we have encountered structured data such as lists and even the more structured classes and objects. Serialisation allows us to save those structures directly to a file along with their data, and to subsequently retrieve them in their original structure.

For our example we shall use our payroll application again. Each class will contain private variables for the employee's name, hours worked, hourly rate, gross, tax and net. They will also contain the code for processing the payroll and displaying the results.

Go to top

Class and pickle

Here we shall look at the details of our serialisation example. the program is divided into it's main components in Listing 1, Listing 2 and Listing 3. The full code is shown in Listing 4.

Listing 1
                        import pickle
                        
                        class Employee:
                            __strName=""
                            __floatHours=0.0
                            __floatRate=0.0
                            __floatGross=0.0
                            __floatTax=0.0
                            __floatNet=0.0  
                            def __init__(self, nme, hrs, rt):
                                self.__strName = nme
                                self.__floatHours = hrs
                                self.__floatRate = rt
                                self.__calculate()
                            def __calculate(self):
                                self.__floatGross = self.__floatHours * self.__floatRate
                                self.__floatTax = self.__floatGross * 0.25
                                self.__floatNet = self.__floatGross - self.__floatTax        
                            def results(self):
                                return(f'Employee name: {self.__strName},\nHours worked: {self.__floatHours},\nHourly Rate: {self.__floatRate},\nGross: {self.__floatGross},\nTax: {self.__floatTax},\nNet: {self.__floatNet}')
                        
                    

The contents of Listing 1 should be familiar to you by now apart from Line 1 and Line 20

Line 1 imports the oddly named library pickle This library contains functions for processing structured data. In our case the structures will be a list that will contain objects of the class Employee and the individual payroll data that will be stored within each each object. The pickle functions that we shall use in this example will be load() and dump(). We shall discuss them in more detail below.

In line 20 a new technique for printing labelled values is used.

Notice that the return string contains four types of text:

Below we see a screen dump of a sample run of the program which illustrates how the formatted string works.

Go to top

Main Variables

Listing 2
                        
                        Salaries = []
                        WorkerName=""
                        
                    

Here we initialise two variables that we shall use to control the program.

The first, Salaries is a list that will contain objects of the class Employee. When we are serialising our data, it is this list that we serialise. All of the objects contained within it will be saved along with it. Also when we are deserialising data from our file, this same list along with its objects will be deserialised.

WorkerName is a string variable. It's value will be used to control a while loop, which terminates when the value of WorkerName is null.

Go to top

Main Processing

Listing 3
                        with open("Workers","rb") as infile:
                            Salaries = pickle.load(infile)
                        
                        WorkerName=input("Enter employee name")
                        while WorkerName !="":
                            WorkerHours = float(input("Enter hours worked"))
                            WorkerRate = float(input("Enter hourly rate"))
                            Worker = Employee(WorkerName, WorkerHours, WorkerRate)
                            Salaries.append(Worker)
                            WorkerName=input("Enter employee name")
                        
                        print("Saving data to file")
                        with open("Workers","wb") as outfile:
                            pickle.dump(Salaries,outfile)
                        print("Data saved to file")   
                        
                        for salaried in Salaries:
                            print(salaried.results())
                    

This section of the program can be divided into four subsections, each of which is discussed below.

Lines 25 - 26: Here the file "Workers" is opened at line 25 and the load() function from the library pickle is used to read the contents of the file and store them in the list Salaries

Lines 28 - 34: These lines form the body of a while loop. Line 28 forms the priming read. If the value of WorkerName is not a blank, then the values of hours and rate are received at lines 30 and 31 and stored in the variables WorkerHours and WorkerRate respectively.At line 32 an object of the class Employee called Worker is created and the values for the name, hours and rate are passed to its constructor. At line 33 the newly created object is appended to the list Salaries. At line 34 the user is asked for another value for the employee name and control passes back to line 29 for another iteration of the loop.

Program control moves to line 36 once the user enters a blank for the employee name at either line 29 or line 34. Lines 36 and 39 simply inform the user of the activities currently taking place. The main processing occurs at lines 37 and 38. The file "Workers" is open for writing and the dump() function from the pickle library is used to store the list Salaries and it's contents to the file.

Finally a for loop iterates through the list Salaries and for each object there calls the method results to print out the values stored in the current object.

Go to top

Complete Program

Listing 4
                        import pickle
                        
                        class Employee:
                            __strName=""
                            __floatHours=0.0
                            __floatRate=0.0
                            __floatGross=0.0
                            __floatTax=0.0
                            __floatNet=0.0  
                            def __init__(self, nme, hrs, rt):
                                self.__strName = nme
                                self.__floatHours = hrs
                                self.__floatRate = rt
                                self.__calculate()
                            def __calculate(self):
                                self.__floatGross = self.__floatHours * self.__floatRate
                                self.__floatTax = self.__floatGross * 0.25
                                self.__floatNet = self.__floatGross - self.__floatTax        
                            def results(self):
                                return(f'Employee name: {self.__strName},\nHours worked: {self.__floatHours},\nHourly Rate: {self.__floatRate},\nGross: {self.__floatGross},\nTax: {self.__floatTax},\nNet: {self.__floatNet}')
                        
                        Salaries = []
                        WorkerName=""
                        
                        with open("Workers","rb") as infile:
                            Salaries = pickle.load(infile)
                        
                        WorkerName=input("Enter employee name")
                        while WorkerName !="":
                            WorkerHours = float(input("Enter hours worked"))
                            WorkerRate = float(input("Enter hourly rate"))
                            Worker = Employee(WorkerName, WorkerHours, WorkerRate)
                            Salaries.append(Worker)
                            WorkerName=input("Enter employee name")
                        
                        print("Saving data to file")
                        with open("Workers","wb") as outfile:
                            pickle.dump(Salaries,outfile)
                        print("Data saved to file")   
                        for salaried in Salaries:
                            print(salaried.results())
                    
Go to top

Summary

Serialisation is the practice of transferring structured data from the computer's memory to a disk storage. The structured data must be stored as a single unit.

In our example the structure was a list, which contained a series of objects of the class Employee. Notice at line 38 above that we 'dump' only the list Salaries; The objects stored in this list are automatically stored with it.

Go to top

Exercise

Part 1

Using Listing 3 above as an example create an application that processes a customer's bill.

For the bill the required data are:

From the above the total for this particular item is calculated.

The customer may purchase as many items as they wish.

On completion of their order, all the subtotals are added up to give a total for the order itself.

The data is serialised with the customer's name used as the file name.

Part 2

Extend the above application so that the customers' names are also added to a separate list. This list is then saved separately.

Now extend the application further so that a list of each customer is printed, and for each customer that their purchases are shown.

Go to top