Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

4.4 Immutable Classes

Section 3.5 first defined immutable classes. An immutable class is a type with no mutator methods. If an object has no mutator methods then it is impossible to tell if two references point to the same object or if they point to different objects. In fact it doesn't really matter since neither reference can be used to change the object. This may happen frequently in Python for objects of type int, float, string, and bool. All these classes are immutable. These classes of objects can never be changed once they are created since they have no mutator methods!

Practice 4.7 If strings cannot be changed, what happens in the following code? Draw a picture to show what happens in the following code.

x = "hello"

x = x + "world"

p r i n t (x)

While string objects can't be changed, references can be. That's what happens in the exercise above. str objects never change once they are created. Immutable classes are nice to work with because we can forget about their being objects and references and just concentrate on using them without fear of changing the object accidentally.

4.5 Object-Oriented Programming

Turtles are fun to program because they make drawing easy by remembering many of the details of generating computer graphics for us. That's really the motivation behind object-oriented programming and using objects. What we've seen in this short chapter are all the mechanics for creating and using objects. Objects make our lives as programmers simpler. Every object maintains some state information, its data, and every object lets us either access that data through an accessor function or it allows its data to be changed by calling mutator methods. Many objects have both accessor and mutator methods.

The power of object-oriented programming is in the ability to organize the data in our programs into logical entities that somehow make sense. A turtle is a great way to embody many of the elements of graphics programming while giving us a way of visualizing how the turtle works by thinking about how a real turtle might leave marks in the sand.

4.6 Working with XML Files

Now that you know how to use objects and in particular how to use turtle graphics you can put it to use. There are many applications for Turtle graphics. It can be used to create more advanced drawing applications like the one pictured in Fig. 4.4.

The drawing application shown in Fig. 4.4 can save pictures in a file format called XML. XML stands for eXtensible Markup Language. Computer Scientists devised the XML format so data could be stored in a consistent format. Many applications store their data in XML format. Some that you might be familiar with include the Apple iTunes application or the registry in Microsoft Windows. Mac OS X uses it as well in its application structure. XML is popular because the definition of XML makes it possible to add additional elements to an XML file later without affecting code that was written before the new fields were added. This ability to add to an XML file without breaking existing code means there is a huge advantage to using XML as the format for data in practically any application. Being able to write code to extract data from an XML file is a very practical skill.

XML files have a fairly straight-forward structure but also contain a lot of formatting information that is not really part of the data. It would be painful to have to write code that reads an XML file and extracts just the information you need. Fortunately, it is because XML files contain this extra formatting information, often

Fig. 4.4 Flower power by Denise M. Lee

called meta-data, that it is possible for someone else to write code that we can use to read an XML file. That code is called an XML parser. Parsing refers to reading data and selecting out the individual components or elements of that data.

To parse an XML file you must import an XML parser. We'll use the minidom XML parser in this text. The import statement looks like this:

from xml . dom import minidom

Once you have imported the XML parser you create an Document object by telling minidom to parse the XML file.

xmldoc = minidom . parse (" flowerandbg . xml ")

That's all there is to reading an entire XML file. Looking at Fig. 4.4 it should be clear that the picture is fairly complex. There are many colors and elements to the drawing. Just how is all that data organized?

An XML file starts with a line at the top that helps the parser identify the contents of the file as an XML file. The parser looks for a line that looks something like this.

<? xml version =" 1 . 0 " encoding =" UTF -8 " standalone =" no " ?>

The rest of an XML file is composed of one or more elements. And, elements may be nested inside of other elements. Elements almost always consist of two tags with text or other elements nested between the tags. A tag in an XML file is a string of characters that appears within angle brackets (i.e. a less than/greater than sign pair). For instance, this is one element from an XML file with a start-tag and end-tag and the text “PenUp” nested inside the element.

< Command > PenUp </ Command >

Each start-tag has a matching end-tag that ends one element of an XML file. The

<Command> is the start-tag of this element and the </Command> is the end-tag. The matching end-tag always has the same name as the start-tag but is preceeded by a slash. An XML element may also contain attributes. The attributes appear within the XML element's start-tag as shown here.

< Command x=" 299.0 " y=" -45.0 " width =" 1 . 0 " color =" #804000 " > GoTo </ Command >

This element contains the attributes x, y, width, and color. Each of these attributes has a value inside the quotes associated with the attribute.

Start-tags and end-tags almost always occur in matching pairs. However, there is one other type of element that consists of just one tag. An element with no nested elements may be written like this.

< Command type =" PenUp "/ >

There are no occurrences of empty elements like this in the graphics file in the following example. Most of the time XML elements consist of a start-tag and end-tag pair with possibly nested elements or text between the tags.

Example 4.7 Here is an example of a file saved by a drawing program. This file contains one XML element called GraphicsCommands. Within this single XML element are many Command elements. These elements represent a subset of the drawing commands used to produce the picture in Fig. 4.4.

1 <? xml version =" 1 . 0 " encoding =" UTF -8 " standalone =" no " ?>

2 < GraphicsCommands >

3 < Command color =" #804000 " > BeginFill </ Command >

4 < Command x=" 299.0 " y=" -45.0 " width =" 1 . 0 "

5 color =" #804000 " > GoTo </ Command >

6 < Command x=" 302.0 " y=" -297.0 " width =" 1.0 "

7 color =" #804000 " > GoTo </ Command >

8 < Command x=" -301.0 " y=" -298.0 " width =" 1.0 "

9 color =" #804000 " > GoTo </ Command >

10 < Command x=" -300.0 " y=" -53.0 " width =" 1 . 0 "

11 color =" #804000 " > GoTo </ Command >

12 < Command > EndFill </ Command >

13 < Command color =" #004080 " > BeginFill </ Command >

14 < Command x=" -300.0 " y=" 300.0 " width =" 1.0 "

15 color =" #004080 " > GoTo </ Command >

16 < Command x=" 299.0 " y=" 299.0 " width =" 1 . 0 "

17 color =" #004080 " > GoTo </ Command >

18 < Command x=" 300.0 " y=" -45.0 " width =" 1 . 0 "

19 color =" #004080 " > GoTo </ Command >

20 < Command > EndFill </ Command >

21 < Command > PenUp </ Command >

22 < Command x=" 0 . 0 " y=" 0 . 0 " width =" 1 . 0 "

23 color =" #000000 " > GoTo </ Command >

24 < Command > PenDown </ Command >

25 < Command radius =" 10.0 " width =" 10 "

26 color ="# ffffff " > Circle </ Command >

27 < Command radius =" 20.0 " width =" 10 "

28 color ="# ff0080 " > Circle </ Command >

29 < Command x=" 2 . 0 " y=" -4.0 " width =" 10.0 "

30 color ="# ff0080 " > GoTo </ Command >

31 < Command x=" 2 . 0 " y=" -5.0 " width =" 10.0 "

32 color ="# ff0080 " > GoTo </ Command >

33 < Command x=" 2 . 0 " y=" -6.0 " width =" 10.0 "

34 color ="# ff0080 " > GoTo </ Command >

35 </ GraphicsCommands >

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel