Python Data Persistence - DBM Package

Vijay Mane

a year ago

Python Data Persistence - DBM Package | Insideaiml
Python Data Persistence - DBM Package | Insideaiml
The DBM bundle presents a word reference like interface DBM style databases. DBM represents the Database Manager. This is utilized by UNIX (and UNIX like) working framework. The dbbm library is a basic database motor composed by Ken Thompson. These databases utilize double encoded string objects as key, just as worthy.
The database stores information by utilization of a solitary key (an essential key) in fixed-size basins and utilization hashing strategies to empower quick recovery of the information by key.
The dbm bundle contains following modules −
  • dbm.gnu module is an interface to the DBM library variant as actualized by the GNU venture.
  • dbm.ndbm module gives an interface to UNIX nbdm usage.
  • dbm.dumb is utilized as a fallback choice on the occasion, other DBM executions are not found. This requires no outer conditions however is slower than others.

>>> dbm.whichdb('mydbm.db')
'dbm.dumb'
>>> import dbm
>>> db=dbm.open('mydbm.db','n')
>>> db['name']=Raj Deshmane'
>>> db['address']='Kirtinagar Pune'
>>> db['PIN']='431101'
>>> db.close()
The open() function allows mode these flags −
  • 'r'
Open existing database for reading only (default)
  • 'w'
Open existing database for reading and writing
  • 'c'
Open database for reading and writing, creating it if it doesn’t exist
  • 'n'
Always create a new, empty database, open for reading and writing
The dbm object is a word reference like item, similarly as rack object. Consequently, all word reference activities can be performed. The dbm article can summon get(), pop(), attach() and update() techniques. The following code opens 'mydbm.db' with an 'r' flag and emphasizes an assortment of key-value pairs.

>>> db=dbm.open('mydbm.db','r')
>>> for k,v in db.items():
   print (k,v)
b'name' : b'Raj Deshmane'
b'address' : b'Kirtinagar Pune'
b'PIN' : b'431101'
Therefore, dbm is a generic interface to variations of the DBM database — dbm.gnu or dbm.ndbm. On the off chance that none of these modules is introduced, the moderate however straightforward execution in module dbm.dumb will be utilized. There is an outsider interface to the Oracle Berkeley DB.
exception dbm.error
A tuple containing the special cases that can be raised by every one of the bolstered modules, with a one of a kind exemption additionally named dbm.error as the primary thing the last is utilized when dbm.error is raised.
dbm.whichdb(filename)
This capacity endeavours to figure which of the few basic database modules accessible — dbm.gnu, dbm.ndbm or dbm.dumb — ought to be utilized to open a given document.
Returns one of the accompanying qualities: none if the record can't be opened on the grounds that it's mixed up or doesn't exist; the vacant string ('') if the document's configuration can't be speculated; or a string containing the necessary module name, for example, 'dbm.ndbm' or 'dbm.gnu'.
dbm.open(file, flag='r', mode=0o666)
Open the database record document and return a comparing object.
On the off chance that the database record as of now exists, the whichdb() work is utilized to decide its sort and the suitable module is utilized; in the event that it doesn't exist, the principal module recorded over that can be imported is utilized.
I hope you enjoyed reading this article and finally, you came to know about Python Data Persistence - DBM Package.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review

BOT
Agent(Online)
We're Online!

Chat now for any query