Data Handling and Custom File Formats

Most applications need to load and save data. Often the data format is predetermined because the application is reading data produced by some other application over which it has no control. But for applications where we create our own file formats, a lot of options are available.

In Chapter 6 we created a main-window-style application from which we learned how to create menus and toolbars, and how to handle file loading and saving. In this chapter we will work on another main-window-style application, but this time our focus will be on the application's data.

«

Movies

_ □ X

I File

Edit Help

H> u , J v %

Title

Year

Mlns

Acquired

Notes

24

Bride of Frankenstein

1935

80

Fri Feb 23. 2007

Elsa Lanchester

25

Bridget Jones' Diary

2001

97

Sun Apr 11, 2004

Hugh Grant, ReneeZelwegger, Colin Firth

26

Brief Encounter

1945

86

Sun Jul 24, 2005

Trevor Howard, Cella Johnson; Rachrranii

27

Brighton Rock

1947

92

Mon Sep 3. 2007

Richard Atten borough

28

A Canterbury Tale

1944

124

Sun May 28, 2006

Powell & Pressburger

29

Career Girls

1997

87

Tue Jan 8, 2002 MI ke Leig h jj[

30

Casablanca

1942

102

Sat Jul 8. 2000

Humphrey Bogart, Ingrid Bergman. Paul H 1

31

Catch Me If You Can

2002

141

Tue Sep 18, 2007

Spielberg. Leonardo DiCaprlo, Tom Hanks'-,

4

Saved 193 movie records to rryrrovies.mqb

Figure 8.1 The My Movies application

Figure 8.1 The My Movies application

The application we will take as our example is called My Movies; it is shown in Figure 8.1. It is used to store some basic information about the movies we might have in our collection. The application will allow us to view and edit a collection of custom Movie objects (or movie records as we will call them), and to load and save these records from and to disk in a variety of formats.

If you just want to dive straight into file handling, you can jump ahead to the relevant sections. Coverage of saving and loading binary files begins on page 240, of text files on page 249, and of XML files on page 256. You can always come back to the first two sections to cover the relationship between the GUI and file handling.

In all the previous examples we usually kept as much data as possible in Python data types and converted to and from PyQt types only when necessary. String And for strings in particular, we proposed a policy that meant that we always policy converted QStrings to Unicodes as soon as possible and always operated on uni-28« code strings. But in this chapter we are going to take the opposite approach, and keep all our data in PyQt types, converting to Python types only when necessary. One reason for doing this is that PyQt provides excellent support for binary data, and uses the same binary formats as C++/Qt, which is useful when working with files that must be accessed by both C++ and Python programs. Another reason is that this will also provide a contrast that will help us understand the pros and cons of each approach so that we can make the right decisions in applications we work on later.

One immediate benefit of holding data in PyQt types is that we do not have to keep converting data that we give to or get from the widgets we use for viewing and editing. When dealing with a large collection of data, this could be a significant saving of processing overhead.

When we have custom data to load and save five options are available to us. We can use binary, plain text, or XML files, or we can use QSettings objects with an explicit filename, or we can use a database. In this chapter we will cover the first three options, and briefly mention the fourth, QSettings, here. We will defer coverage of databases until Chapter 15.

All the options apart from QSettings can be implemented using either Python's standard library or PyQt. In this chapter, we will discuss loading and saving both binary and text formats using both libraries so that we can compare and contrast them. For XML, we will use PyQt for loading and parsing, and we will do the saving ourselves. Python's standard library also provides considerable XML support, but covering it would not show anything that cannot be done with PyQt's XML classes.

In Chapter 6, we saw how to use a QSettings object to save and load user settings, such as the main window's size and position, and a list of recently used files. The class stores all data as QVariants, but this is perfectly acceptable for small amounts of data. We can use this class to store custom data by creating a QSettings instance with a filename—for example, iniFile = QSettings("curvedata.ini", QSettings.IniFormat). Now we can use the iniFile object to write data using setValue() and to read data using value(), in both cases converting between QVariant and the relevant type.

In the following section we will look at the high-level file handling and data presentation that are performed by the application's main window subclass. In the second section, we will look at the application's data module, including the implementation of individual data items, and of the data item container in which the application's data is held.

Then, in the subsequent sections, we will look at saving and loading data in various formats. In the section on binary files, we will look at how to use PyQt's QDataStream class and also the standard Python library's cPickle module to load and save our collection of movie records. In the section on text files, we will see how to load and save our movie records in plain text using PyQt's QTextStream and the Python standard library's codecs module. And in the last section we will write the code to save the records as XML by hand, and see how to use both DOM and SAX parsers to read back the XML data.

Was this article helpful?

0 0
YouTube Saturation

YouTube Saturation

Uncover The Instant Traffic System That Will TRIPLE Your Income Overnight While Putting You In Direct Contact With THOUSANDS Of Hungry Buyers. How to generate instant traffic to your website, easily! Siphon high quality, ultra-targeted traffic to your offers. The easiest way to dominate your market with lase targeted video based campaigns. These video ads will suck in customers by the minute, driving your message out to a global audience of buyers.

Get My Free Ebook


Post a comment