import anydbm
db = anydbm.open('integer.db', 'c')
import pickle
# A limitation of anydbm is that the keys and values have to be strings.
# If you try to use any other type, you get an error
# The pickle module can help. It translates almost any type of object into a string suitable
# for storage in a database, and then translates strings back into objects.
k = 1
f=pickle.dumps(k)
db[f] = 'Babu'
print 'Value:',db[f]
print 'Key:',f
db.close()
r=pickle.loads(f)
print 'Key Value back:',r
Solve Problems by Coding Solutions - A Complete solution for python programming
Create database and store key value pairs
import anydbm
db = anydbm.open('captions.db', 'c')
db['c1'] = 'Photo of John Cleese.'
print db['c1']
db['c2'] = 'Photo of John Cleese doing a silly walk.'
print db['c2']
for key in db:
print key
db.close()
Value Meaning
'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
anydbm in python.org package
dbm in anaconda package
db = anydbm.open('captions.db', 'c')
db['c1'] = 'Photo of John Cleese.'
print db['c1']
db['c2'] = 'Photo of John Cleese doing a silly walk.'
print db['c2']
for key in db:
print key
db.close()
Value Meaning
'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
anydbm in python.org package
dbm in anaconda package
To use try except for error handling
try:
s=0
g=s/0 # Division by zero creates error
print 'Value of g :',g
except:
print 'Something went wrong.'
try:
s=0
g=s/10 # Division by zero by 10 creates no error
print 'Value of g :',g
except:
print 'Something went wrong.'
s=0
g=s/0 # Division by zero creates error
print 'Value of g :',g
except:
print 'Something went wrong.'
try:
s=0
g=s/10 # Division by zero by 10 creates no error
print 'Value of g :',g
except:
print 'Something went wrong.'
Decimal Number to Binary number conversion
n=int(raw_input('Enter the Number'))
h=" "
while n>0:
n1=n%2
print 'R',n1
n=n/2
print 'V=',n
h=h+str(n1)
print 'Binary Number is:',h[::-1]
h=" "
while n>0:
n1=n%2
print 'R',n1
n=n/2
print 'V=',n
h=h+str(n1)
print 'Binary Number is:',h[::-1]
Binary Number to Decimal Conversion
k=int(raw_input('Enter the binary number:'))
b=k
c=0
while b>0:
b=b/10
c=c+1
print c
n=0
sum=0
while n<c:
r=k%10
k=k/10
sum=sum + (r*(2**n))
n=n+1
print 'Decimal Number is:', sum
b=k
c=0
while b>0:
b=b/10
c=c+1
print c
n=0
sum=0
while n<c:
r=k%10
k=k/10
sum=sum + (r*(2**n))
n=n+1
print 'Decimal Number is:', sum
Subscribe to:
Posts (Atom)