Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

LISTING 15.10 The script1503.py Interpreted


Click here to view code image


pi@raspberrypi ~ $ python3 /home/pi/py3prog/script1503.py

The following characteristics are listed
in order to help you determine what swallow
you have sighted.
A barn swallow is a bird that is migratory.
A cliff swallow is a bird that is not migratory.
Which did you see?
European/Barn Swallow - 1
African Cliff Swallow - 2
Type number & press Enter: 1

Where did you see the birds? Indianapolis, Indiana, USA

Approximately, how many did you see? 21

Thank you.
The following data will be forwarded to
the Great Backyard Bird Count.
http://www.birdsource.org/gbbc
Sighting Date: 2013-11-06
Location: Indianapolis, Indiana, USA
Species: European/Barn Swallow
Flock Size: 21
pi@raspberrypi ~ $

Good job! You’ve done quite a bit of work here, but if you are like most other script writers, you
probably already see several things to improve in this script. For instance, there are no checks on
user-input data. The data should be output to a file, not just displayed to the screen. In addition, to
make the data useful, the time of day should be recorded, and more bird species should be added.
You can make all sorts of changes to this script!


Here is an idea to start. Start with script1503.py and try adding better bird descriptions to the
swallow subclass object module files to aid in their species identification. Now that you know how
to create subclasses using inheritance, you can get the script’s “ducks in a row.”


Summary


In this hour, you read about the class problem, Python subclasses, and the inheritance solution. You
learned how to create an object subclass in the same object module file as the base class. Also, you
learned how to create a subclass in its own object module file. Finally, you saw some practical
examples of using the base classes and subclasses in a few Python scripts. In Hour 16, “Regular
Expressions,” you will explore regular expressions.


Q&A


Q. What is the difference between an “is a” relationship and a “has a” relationship in
Free download pdf