Python shelve module

Source: Internet
Author: User
Shelve
Shelve is a simple data storage scheme, he has only one function is open (), the function receives a parameter is the file name, and then return a shelf object, you can use him to store things, you can simply think of him as a dictionary, when you are stored, Just call the close function to close the
This has a potential minor problem, as follows:
[Python] View plaincopy
>>> Import Shelve
>>> s = shelve.open (' Test.dat ')
>>> s[' x '] = [' A ', ' B ', ' C ']
>>> s[' x '].append (' d ')
>>> s[' x ']
[' A ', ' B ', ' C ']
Where does the stored d go? Actually very simple, D did not write back, you save [' A ', ' B ', ' C '] to X, and when you read s[' x ' Again, s[' x ' is just a copy, and you don't write the copy back, so when you read s[' x ' again, it reads a copy from the source, so Your newly modified content does not appear in the copy, the solution is that the first one is to take advantage of a cached variable, as shown below
[Python] View plaincopy
>>> temp = s[' x ']
>>> temp.append (' d ')
>>> s[' x '] = Temp
>>> s[' x ']
[' A ', ' B ', ' C ', ' d ']
In python2.4, there is another way to do this is to assign the value of the writeback parameter of the Open method to true, so that all the contents of your open will be in the cache, and when you close, it will all be written to the hard disk once. If the amount of data is not very large, it is advisable to do so.

Here's the code for a simple database based on shelve
[Python] View plaincopy
#database. py
Import SYS, shelve

def store_person (db):
"""
Query user for data and store it in the shelf object
"""
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 do you like to know? (name, age, phone) ')
field = Field.strip (). Lower ()
Print field.capitalize () + ': ', \
Db[pid][field]

Def print_help ():
Print ' The available Commons is: '
print ' store:stores information about a person '
print ' lookup:looks up a person from ID number '
print ' Quit:save changes and exit '
print '? Rint this message '

Def enter_command ():
cmd = raw_input (' Enter command (? For help): ')
cmd = Cmd.strip (). Lower ()
return cmd

def main ():
Database = Shelve.open (' 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 ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.