Python
/
Storage
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Storage Databases
import dbm db = dbm.open("/db/images_db", 'c') #file for storing data db['01.jpg'] = '/images/01.jpg' db['02.png'] = '/images/02.png'
Dbm
p252 A database is a file for storing data.
# Many databases are like dictionaries (map keys to values).
# The biggest difference is that the database is on disk.
import os
import dbm
DIR = os.path.dirname(os.path.realpath(__file__))
IMAGES_DB = DIR + "/db/images_db"
db = dbm.open(IMAGES_DB, 'c')
db['myimage.jpg'] = '/images/myimage.jpg'
db['myimage.png'] = '/images/myimage.png'
print(db['myimage.jpg'])
# b'/images/myimage.jpg'
# the result is a bytes object (it begins with b)
db.close()
# You can use dictionaries methods ...
# to loop through items.
db = dbm.open(IMAGES_DB)
for key in db.keys():
print(key, db[key])
# b'myimage.png' b'/images/myimage.png'
# b'myimage.jpg' b'/images/myimage.jpg'
db.close()
Pickle
With pickle module you can store any type.
# Pickle storage:
#
# Databases have a limitation ...
# they works only with strings or bytes.
#
# With pickle module you can convert and store any type.
import os
import dbm
import pickle
DIR = os.path.dirname(os.path.realpath(__file__))
IMAGES_DB = DIR + "/db/images_db_pickle"
# Write
# -------------------------------------------
db = dbm.open(IMAGES_DB, 'c')
t = [1, 2, 3]
str = pickle.dumps(t) # b'\x80\x03]q\x00(K\x01K\x02K\x03e.'
db['mylist'] = str
db.close()
# Read
# -------------------------------------------
db = dbm.open(IMAGES_DB, 'r')
b = db['mylist']
str = pickle.loads(b)
assert str == [1, 2, 3]
db.close()
# Object copy
#
# Pickle then revers ....
# has the same effect as copying the object.
#-------------------------------------------
t1 = (1, 2, 3)
str = pickle.dumps(t1)
t2 = pickle.loads(str)
assert t2 == (1, 2, 3)
assert t1 == t2
assert (t1 is t2) == False
# Shelf:
#
# In a 'shelf' db the values can be any objects that pickle can handle.
# --------------------------------------------
import shelve
IMAGES_DB = DIR + "/db/images_db_shelf"
t = (1, 2, 3)
db = shelve.open(IMAGES_DB, 'c')
db['my_tuple'] = t
db.close()
db = shelve.open(IMAGES_DB, 'w')
assert db['my_tuple'] == (1, 2, 3) # pass
db.close()
➥ Questions