how to use python anydbm module

Namaste Friends,



In Today's Modern World, Mostly In all types of computers, a database always plays very important role because with database a program can records its data, info, procedures and other things in a special file from where program can again easily read and write data.

In this Post, I am going to describe about python anydbm module and i will also provide you practical examples of python anydbm module.

let's start our tutorials from some basic queries.


Q 1. What is Python Anydbm Module?


Ans. anydbm python module is a front-end for DBM Style database that use simple string values as a key to access records containing string data. In simple words, This module provides us capability to store our string data in a DBM-Style Database. With the help of this module we can save our string values in a safe database and can also access, read and write our stored data.


Q 2. Features Of Python Anydbm Module?


Ans. Python Anydbm module provides a very simple to use dictonery like interface to store our data in a database. with Anydbm dictonery like interface we can do our many functions easily like Writing Data, Reading Data, Deleting Data And Updating Data.
This Module is also used as a back end for shelve module. In simple way, shelve also works completely like Anydbm Module The Difference Between shelve and anydbm is, with shelve we can directly save our python objects  but with anydbm, we have to use python pickle module for pickling object into string.
Means shelve is also completely like anydbm but with extra ability/feature of pickle module.


Q 3. How Anydbm Python Module Works?


Ans. Anydbm Module also based on another DBM style Modules likes dbhash or gdbm. python dbhash module use the bsddb library to manage its databases and gdbm is an updated version of the dbhash library from the GNU project.

Python Anydbm Tutorial With Practical Example


Input : [1]  

#
# ==================================================
#          PYTHON Anydbm MODULE TUTORIAL
# ==================================================
# 
# author   : suraj singh bisht
# contact  : SSB
#            surajsinghbisht054@gmail.com
#            https://bitforestinfo.blogspot.com
#            
# Here, For This Tutorial
#
# I am using
#
# Operating System : Ubuntu 16.04
# Python Version   : python 2.7.12
# Editor           : ipython notebook
#

Output : [1]  

  

Input : [2]  

# Importing Modules
#
import anydbm   # anydbm : for database api 

import pprint   # pprint : for printing objects

Output : [2]  

  

Input : [3]  

#
# db_name = Name Of Database.
#
# You Can Use Any Name What You Want!
#
db_name = "example_database_anydbm.db"

Output : [3]  

  

Input : [4]  

#
# Open Database And Create Anydbm Object handler.
# Anydbm Object handler Is Very Easy To Use.
# because anydbm completely works like a 
# dictonery object means anydbm provides us
# dictonary like interface
#
# anydbm.open('filename', flag):
#            You also need to provide a flag into anydbm.open()
# anydbm Supported Flags ’r’ , ’w’ , ’c’ , 'f', 's', 'u' and ’n’.
#      
# --------------------------------------------------
# |           Anydbm Flags With Detail             |
# --------------------------------------------------
# |  r  | Read Only Access
# +++++++++++++++++++++++++++++++++++++++++++++++++|
# |  w  | Read And Write (Existing Database)
# +++++++++++++++++++++++++++++++++++++++++++++++++|
# |  c  | Read-Write (New Database only if required)
# +++++++++++++++++++++++++++++++++++++++++++++++++|
# |  f  | Fast mode (Write not synchronized)
# +++++++++++++++++++++++++++++++++++++++++++++++++|
# |  s  | Synchronized Mode
# +++++++++++++++++++++++++++++++++++++++++++++++++|
# |  u  | Open Unlocked Database
# +++++++++++++++++++++++++++++++++++++++++++++++++|
# |  n  | Read-Write (Always New Database)
# -------------------------------------------------
#

db = anydbm.open(db_name, 'c')

Output : [4]  

  

Input : [5]  

#
# Creating A List Only For Exmaple Purpose
#
storelist = ["Dog",
            "cat",
            "rat",
            "snake",
            "kite"]

Output : [5]  

  

Input : [6]  

#
# Print Example List
#
pprint.pprint(storelist)

Output : [6]  

['Dog', 'cat', 'rat', 'snake', 'kite']

Input : [7]  

#
# Create Example Dictonery
#
storedict = {"rat":"cat",
            "cat":"dog",
            "dog":"wolf",
            "wolf":"lion"}

Output : [7]  

  

Input : [8]  

#
# Print Example Dictonery
#
print storedict

Output : [8]  

{'rat': 'cat', 'wolf': 'lion', 'dog': 'wolf', 'cat': 'dog'}

Input : [9]  

# This Module Works Completely like Dictonery Object
# But Their Is A limit in Anydbm Module.
# With This Interface, We can't store any other object
# except strings.
# So, if you want to store any list,dict or tuple.
# then,first you have to convert that object into string form
#
db['store_list'] = str(storelist)

Output : [9]  

  

Input : [10]  

#
# Same Here As we do in previous example
#
db['store_dict'] = str(storedict)

Output : [10]  

  

Input : [11]  

#
# Close Database
#
db.close()

Output : [11]  

  

Input : [12]  

#
# Again, open database for reading data. 
# but at this time, open only for reading
# data. and for read-only, use "r"
# flag.
#
db = anydbm.open(db_name,  flag="r")

Output : [12]  

  

Input : [13]  

#
# Read All Items (Key and Value both)
#
recover_data = db.items()

Output : [13]  

  

Input : [14]  

#
# print data
#
print recover_data

Output : [14]  

[('store_dict', "{'rat': 'cat', 'wolf': 'lion', 'dog': 'wolf', 'cat': 'dog'}"), ('store_list', "['Dog', 'cat', 'rat', 'snake', 'kite']")]

Input : [15]  

#
# Print data in for loop
#
for data_string in recover_data:
    print data_string

Output : [15]  

('store_dict', "{'rat': 'cat', 'wolf': 'lion', 'dog': 'wolf', 'cat': 'dog'}")
('store_list', "['Dog', 'cat', 'rat', 'snake', 'kite']")

Input : [16]  

#
# Convert String Data Again In Their Original State  
#
# For List
l = eval(recover_data[1][1])
print l, "<-->" ,type(l)

# For Dictionery
d = eval(recover_data[0][1])
print d, "<-->", type(d)

Output : [16]  

['Dog', 'cat', 'rat', 'snake', 'kite'] <--> <type 'list'>
{'rat': 'cat', 'wolf': 'lion', 'dog': 'wolf', 'cat': 'dog'} <--> <type 'dict'>

Input : [17]  

#
# Close Database
#
db.close()

Output : [17]  

  


So, This Tutorial Ends Here.

Some Useful Links For You.

Sqlorm (sqlite3 Object Relational Mapper)
Shelve Tutorial



If You Like Our Tutorial,
Please Comment, Share and Follow

Written By
           SSB

Share this

Related Posts

Previous
Next Post »