Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 10 ■ BATTERIES INCLUDED 233 pid = raw_input('Enter unique ID number: ') person = {} person['name'] = raw_input('Enter name: ') person['age'] = raw_input('Enter age: ') person['phone'] = raw_input('Enter phone number: ') db[pid] = person def lookup_person(db): """ Query user for ID and desired field, and fetch the corresponding data from the shelf object """ pid = raw_input('Enter ID number: ') field = raw_input('What would you like to know? (name, age, phone) ') field = field.strip().lower() print field.capitalize() + ':', \ db[pid][field] def print_help(): print 'The available commands are:' print 'store : Stores information about a person' print 'lookup : Looks up a person from ID number' print 'quit : Save changes and exit' print '? : Prints this message' def enter_command(): cmd = raw_input('Enter command (? for help): ') cmd = cmd.strip().lower() return cmd def main(): database = shelve.open('C:\\database.dat') try: while True: cmd = enter_command() if cmd == 'store': store_person(database) elif cmd == 'lookup': lookup_person(database) elif cmd == '?': print_help() elif cmd == 'quit': return finally: database.close() if __name__ == '__main__': main()
234 CHAPTER 10 ■ BATTERIES INCLUDED ■Caution As you can see, the program specifies the file name C:\database.dat. If you, by any chance, have a database by that name that the shelve module can use, it will—and that database will be modified. So make sure that you use a file name for your database that isn’t in use already. After running this program, the proper file appears. The program shown in Listing 10-8 has several interesting features: • I have wrapped everything in functions to make the program more structured. (A possible improvement is to group those functions as the methods of a class.) • I have put the main program in the main function, which is called only if __name__ == '__main__'. That means you can import this as a module and then call the main function from another program. • I open a database (shelf) in the main function, and then pass it as a parameter to the other functions that need it. I could have used a global variable, too, because this program is so small, but it’s better to avoid global variables in most cases, unless you have a reason to use them. • After reading in some values, I make a modified version by calling strip and lower on them because if a supplied key is to match one stored in the database, the two must be exactly alike. If you always use strip and lower on what the user enters, you can allow him or her to be sloppy about using uppercase or lowercase letters and additional whitespace. Also, note that I’ve used capitalize when printing the field name. • I have used try and finally to ensure that the database is closed properly. You never know when something might go wrong (and you get an exception), and if the program terminates without closing the database properly, you may end up with a corrupt database file that is essentially useless. By using try and finally, you avoid that. So, let’s take this database out for a spin. Here is the interaction between the program and me: Enter command (? for help): ? The available commands are: store : Stores information about a person lookup : Looks up a person from ID number quit : Save changes and exit ? : Prints this message Enter command (? for help): store Enter unique ID number: 001 Enter name: Mr. Gumby Enter age: 42 Enter phone number: 555-1234 Enter command (? for help): lookup Enter ID number: 001 What would you like to know? (name, age, phone) phone Phone: 555-1234 Enter command (? for help): quit
- Page 214 and 215: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 216 and 217: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 218 and 219: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 220 and 221: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 222 and 223: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 224 and 225: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 226 and 227: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 228 and 229: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 230 and 231: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 232 and 233: CHAPTER 9 ■ MAGIC METHODS, PROPER
- Page 234 and 235: CHAPTER 10 ■ ■ ■ Batteries In
- Page 236 and 237: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 238 and 239: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 240 and 241: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 242 and 243: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 244 and 245: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 246 and 247: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 248 and 249: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 250 and 251: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 252 and 253: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 256 and 257: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 262 and 263: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 266 and 267: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 268 and 269: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
234 CHAPTER 10 ■ BATTERIES INCLUDED<br />
■Caution As you can see, the program specifies the file name C:\database.dat. If you, by any chance,<br />
have a database by that name that the shelve module can use, it will—and that database will be modified.<br />
So make sure that you use a file name for your database that isn’t in use already. After running this program,<br />
the proper file appears.<br />
The program shown in Listing 10-8 has several interesting features:<br />
• I have wrapped everything in functions <strong>to</strong> make the program more structured. (A possible improvement<br />
is <strong>to</strong> group those functions as the methods of a class.)<br />
• I have put the main program in the main function, which is called only if __name__ == '__main__'.<br />
That means you can import this as a module and then call the main function from another program.<br />
• I open a database (shelf) in the main function, and then pass it as a parameter <strong>to</strong> the other functions<br />
that need it. I could have used a global variable, <strong>to</strong>o, because this program is so small, but it’s better <strong>to</strong><br />
avoid global variables in most cases, unless you have a reason <strong>to</strong> use them.<br />
• After reading in some values, I make a modified version by calling strip and lower on them because<br />
if a supplied key is <strong>to</strong> match one s<strong>to</strong>red in the database, the two must be exactly alike. If you always use<br />
strip and lower on what the user enters, you can allow him or her <strong>to</strong> be sloppy about using uppercase<br />
or lowercase letters and additional whitespace. Also, note that I’ve used capitalize when printing the<br />
field name.<br />
• I have used try and finally <strong>to</strong> ensure that the database is closed properly. You never know when<br />
something might go wrong (and you get an exception), and if the program terminates without closing<br />
the database properly, you may end up with a corrupt database file that is essentially useless. By using<br />
try and finally, you avoid that.<br />
So, let’s take this database out for a spin. Here is the interaction between the program and me:<br />
Enter command (? for help): ?<br />
The available commands are:<br />
s<strong>to</strong>re : S<strong>to</strong>res information about a person<br />
lookup : Looks up a person from ID number<br />
quit : Save changes and exit<br />
? : Prints this message<br />
Enter command (? for help): s<strong>to</strong>re<br />
Enter unique ID number: 001<br />
Enter name: Mr. Gumby<br />
Enter age: 42<br />
Enter phone number: 555-1234<br />
Enter command (? for help): lookup<br />
Enter ID number: 001<br />
What would you like <strong>to</strong> know? (name, age, phone) phone<br />
Phone: 555-1234<br />
Enter command (? for help): quit