[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版
Example 1-4. PP4E\Preview\update_db_file.py from make_db_file import loadDbase, storeDbase db = loadDbase() db['sue']['pay'] *= ...
data’s text, but creating and parsing XML would complicate the program substantially too. Perhaps worst of all, the formatted te ...
...\PP4E\Preview> python make_db_pickle.py ...\PP4E\Preview> python dump_db_pickle.py bob => {'pay': 30000, 'job': 'dev ...
be used to transfer native Python objects to a variety of media. Using a network socket, for instance, allows us to ship pickled ...
sue['pay'] *= 1.10 suefile = open('sue.pkl', 'wb') pickle.dump(sue, suefile) suefile.close() Here are our file-per-record script ...
The shelve interface is just as simple as pickle: it is identical to dictionaries, with extra open and close calls. In fact, to ...
later, shelve.open also has a newer writeback keyword argument, which, if passed True, causes all records loaded from the shelve ...
from the Monty Python series or movies. Some of these phrases might even pop up in this book. You don’t have to run out and rent ...
avoid code redundancy, but still wouldn’t naturally associate them with the records themselves. What we’d like is a way to bind ...
self.name = name self.age = age self.pay = pay self.job = job if name == 'main': bob = Person('Bob Smith', 42, 30000, 'software' ...
records persistently in a shelve, which already encapsulates stores and fetches behind an interface for us. Before we do, though ...
section’s Person class in order to give a 10 percent bonus by default to managers when- ever they receive a raise (any relation ...
>>> for obj in db: obj.giveRaise(.10) # default or custom >>> for obj in db: print(obj.lastName(), '=>', ob ...
to give the display format for our objects when they are printed as a whole—much better than the default display we get for an i ...
Example 1-17. PP4E\Preview\person_alternative.py """ Alternative implementation of person classes, with data, behavior, and oper ...
Such refactoring (restructuring) of code is common as class hierarchies grow and evolve. In fact, as is, we still can’t give som ...
Note that we don’t need to reimport the Person class here in order to fetch its instances from the shelve or run their methods. ...
Tom and Sue both get a raise this time around, because they are persistent objects in the shelve database. Although shelves can ...
But Python’s user base helps support the language, work on extensions, fix bugs, and so on. It is a true community project. In f ...
except: print('No such key "%s"!' % key) else: for field in fieldnames: print(field.ljust(maxfield), '=>', getattr(record, fi ...
«
1
2
3
4
5
6
7
8
9
10
»
Free download pdf