Do you have any suggestions to improve or extend this script? Any ideas for removing or adding comments/docstrings?
import dbm
import subprocess
import time
subprocess.call("clear")
try:
# Tries to import cPickle, a faster implementation of pickle.
import cPickle as pickle
except ImportError:
import pickle
class PickleDB:
def open_db(self):
"""Gets and opens the database"""
self.db_name = input("Enter a database name\n")
# Makes sure the database name is compatible
db_parts = self.db_name.split(".")
self.db_name = db_parts[0]
self.db = dbm.open(self.db_name, "n")
def get_data(self):
"""Gets data to write into the database"""
subprocess.call("clear")
self.raw_data = input("Enter your raw data\n")
# Pickles data
self.pickled_data = pickle.dumps(self.raw_data)
def write_to_db(self):
"""Writes data into database"""
# Logs time
self.start = time.time()
# Creates keys
self.db["raw"] = self.raw_data
self.db["pickled"] = self.pickled_data
def close_db(self):
"""Closes database"""
self.db.close()
# Logs end time
self.end = time.time()
def info(self):
"""Prints out info about the database"""
subprocess.call("clear")
print("Load data from database using dbm")
print("Keys: raw\tpickled")
# Prints total time
print("Speed:", self.end - self.start, "seconds")
# Function calls
pickled_db = PickleDB()
pickled_db.open_db()
pickled_db.get_data()
pickled_db.write_to_db()
pickled_db.close_db()
pickled_db.info()