使用Shelve在Python中保存对象

Shelve is a powerful Python module for object persistence. When you shelve an object, you must assign a key by which the object value is known. In this way, the shelve file becomes a database of stored values, any of which can be accessed at any time.

Shelve是用于对象持久化的功能强大的Python模块。 搁置对象时,必须分配一个密钥,通过该密钥可以知道对象的值。 这样,搁置文件将成为存储值的数据库,可以随时访问其中的任何一个。

Python搁置示例代码 ( Sample Code for Shelve in Python )

To shelve an object, first import the module and then assign the object value as follows:

要搁置对象,请首先导入模块,然后按如下所示分配对象值:

 import shelve 
database = shelve.open(filename.suffix)
object = Object()
database['key'] = object

If you want to keep a database of stocks, for example, you could adapt the following code:

例如,如果您想保留一个库存数据库,则可以修改以下代码:

 import shelve 
stockvalues_db = shelve.open('stockvalues.db')
object_ibm = Values.ibm()
stockvalues_db['ibm'] = object_ibm
object_vmw = Values.vmw()
stockvalues_db['vmw'] = object_vmw
object_db = Values.db()
stockvalues_db['db'] = object_db

A "stock values.db" is already opened, you don't need to open it again. Rather, you can open multiple databases at a time, write to each at will, and leave Python to close them when the program terminates. You could, for example, keep a separate database of names for each symbol, appending the following to the preceding code:

一个“ stock values.db”已经打开,您无需再次打开它。 相反,您可以一次打开多个数据库,随意写入每个数据库,而让Python在程序终止时关闭它们。 例如,您可以为每个符号保留一个单独的名称数据库,将以下内容附加到前面的代码中:

 ## assuming shelve is already imported 
stocknames_db = shelve.open('stocknames.db')
objectname_ibm = Names.ibm()
stocknames_db['ibm'] = objectname_ibm
objectname_vmw = Names.vmw()
stocknames_db['vmw'] = objectname_vmw
objectname_db = Names.db()
stocknames_db['db'] = objectname_db

Note that any change in the name or suffix of the database file constitutes a different file and, therefore, a different database.

请注意,数据库文件名称或后缀的任何更改均构成一个不同的文件,因此也构成一个不同的数据库。

The result is a second database file containing the given values. Unlike most files written in self-styled formats, shelved databases are saved in binary form.

结果是包含给定值的第二个数据库文件。 与大多数以自定义样式格式编写的文件不同,搁置的数据库以二进制形式保存

After the data is written to the file, it can be recalled at any time. If you want to restore the data in a later session, you re-open the file. If it is the same session, simply recall the value; shelve database files are opened in read-write mode. The following is the basic syntax for achieving this:

将数据写入文件后,可以随时对其进行调用。 如果要在以后的会话中还原数据,请重新打开该文件。 如果是同一会话,只需调用该值即可; 搁架数据库文件以读写模式打开。 以下是实现此目的的基本语法:

 import shelve 
database = shelve.open(filename.suffix)
object = database['key']

So a sample from the preceding example would read:

因此,前面示例中的示例将显示为:

 import shelve 
stockname_file = shelve.open('stocknames.db')
stockname_ibm = stockname_file['ibm']
stockname_db = stockname_file['db']

搁置注意事项 ( Considerations With Shelve )

It is important to note that the database remains open until you close it (or until the program terminates). Therefore, if you are writing a program of any size, you want to close the database after working with it. Otherwise, the entire database (not just the value you want) sits in memory and consumes computing resources.

重要的是要注意,直到关闭数据库(或程序终止),数据库才会保持打开状态。 因此,如果要编写任何大小的程序,都希望在使用数据库后将其关闭。 否则,整个数据库(不仅是所需的值)都位于内存中并消耗计算资源

To close a shelve file, use the following syntax:

要关闭搁置的文件,请使用以下语法:

 database.close() 

If all of the code examples above were incorporated into one program, we would have two database files open and consuming memory at this point. So, after having read the stock names in the previous example, you could then close each database in turn as follows:

如果上面的所有代码示例都合并到一个程序中,那么此时我们将打开两个数据库文件并占用内存。 因此,在阅读了上一个示例中的股票名称之后,您可以依次关闭每个数据库,如下所示:

 stockvalues_db.close() 
stocknames_db.close()
stockname_file.close()

翻译自: https://www.thoughtco.com/using-shelve-to-save-objects-2813668

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值