📌  相关文章
📜  Python中Shelve类的子类和方法

📅  最后修改于: 2022-05-13 01:54:21.664000             🧑  作者: Mango

Python中Shelve类的子类和方法

Shelve 是 Python 标准库中的一个模块,可以用作非关系型数据库。 dbm 和 shelve 之间的主要区别在于,shelve 可以将其值作为任何可以由pickle 模块处理的对象提供服务,而在 dbm 数据库中,我们只能将Python的标准数据类型作为其数据库值。 shelve 中的键始终是一个字符串对象,而值可以是任意 pickle 对象。

在本文中,我们将了解 shelve 的子类,并且可以使用内置方法来处理“Shelf”对象。请通过 本文旨在了解从货架对象中检索数据(插入、更新和检索)的基本方法

这些是 shelve 的以下三个子类:

Class NameDescription
 ShelfThis is the base class of shelve which stores pickled object values in dict objects and string objects as key.
BsdDbShelf

The pickle object passed to dict object of this sub class must support these functions : next(), previous(), 

first(), last() and set_location(key), these are available in pybsddb (a third party module) but not in 

other database modules.

DbfilenameShelf

This is also a base class of shelve and it accepts the filename as it’s parameter not a dict object like others. 

It opens  a file using dbm.open(), by default for both read and write.

例子 :

python3
# In this example we will see
# the different types of objects
# of sub class of shelve class
import shelve
 
# importing bsddb module to create
# a bsddb object which can support
# mentioned next(), first() methods..
import bsddb
 
# Shelve Class :
d = {1 : 'geeks', 2 : 'GfG'}
 
# Dictionary object as parameter
Shelf_obj = shelve.Shelf(d)
 
print(type(Shelf_obj))
 
# BsdDbShelf Class :
bsddb_file = bsddb.btopen('spam.db', 'c')
 
# bsddb object as parameter
bsddb_obj = shelve.BsdDbShelf(bsddb_file)
 
print(type(bsddb_obj))
 
# DbfilenameShelf Class :
# File name as parameter
Dbfile_obj = shelve.open('file_name', writeback = True)
 
print(type(Dbfile_obj))


输出 :



类 shelve.Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8' )

collections.abc.MutableMapping的子类,将泡菜值存储为字典对象。

  • 默认情况下,使用 pickle 版本 3 协议,如果使用任何其他协议,则在构造函数的协议参数中指定。
  • 如果 writeback 参数设置为 True,Shelf 会将所有条目保存为缓存,并在同步或关闭时将它们释放回字典。但它会使用更多内存,因此同步和关闭可能需要更长的时间。
  • keyencoding 参数 用于在将键提供给字典对象之前对键进行编码的编码类型。

以下是为此类定义的方法:

MethodDescription
Shelf.__contains__(key)

Returns a bool class object, True if key given in parameter is present in collable dict 

object otherwise returns False.

Shelf.__del__()Deletes the Shelf dictionary object from the disk.
Shelf.__getitem__(key)

Returns the corresponding value in the dict object corresponding to the key given 

as parameter.

Shelf.__len__()Returns the total number of items present in dictionary.
Shelf.__delitem__(key)Deletes the particular item from the dict corresponding to the key given as parameter.
Shelf.__setitem__(key, value)

This method is used to update the existing item in dictionary to a new value  

corresponding to key given as parameter to a new value also given as second argument.

Shelf.get(key, default=None)

This method will return the value corresponds to key given as parameter if present

 else return the value given as second parameter which was set to None as default.

Shelf.__iter__()Returns a iterator for the dictionary object.
Shelf.close()Closes the current opened Shelf object file.
Shelf.clear()

Does not deletes the dictionary object rather removes all the items present 

in it and makes it empty.

Shelf.pop(key, default = ‘KeyError’)

Removes the item corresponds to given key parameter and returns the key value ,

if present else if key not found then returns the second parameter given if given else 

raise the key error.

Shelf.popitem()

Remove and return some (key, value) pair as tuple from dict otherwise raise a KeyError 

if dict object is empty.

Shelf.setdefault(key,default = None)

Sets second parameter as value to key in first parameter, also sets D[key] = d if key is 

not present in D, or default as None.

Shelf.items()Returns list of 2-tuples of key, value pair for all the items in dictionary object.
Shelf.keys()An iterator containing keys of object.
Shelf.values()An iterator containing value in the object.

它上面的方法可以很容易地实现,因为我们对一个简单的字典对象进行操作。