I am from Java background, learning Python. Please review my code and guide me to learn Python properly.
This is a sample code to perform all CRUD operations of contact app.
import pymysql
from beautifultable import BeautifulTable
class Contact:
def __init__(self, name, email, mobile, cid=None):
self.cid = cid
self.name = name
self.email = email
self.mobile = mobile
class DbUtil:
@staticmethod
def get_connection():
try:
con = pymysql.connect("localhost", "root", "password", "counselor_db")
return con
except pymysql.DatabaseError as error:
print("While connecting with database :", error)
@staticmethod
def close_connection(conn, cursor):
try:
if conn:
conn.close()
if cursor:
cursor.close()
except pymysql.DatabaseError as error:
print("While closing connection ", error)
class ContactService:
def add_contact(self, new_contact):
try:
conn = DbUtil.get_connection()
cursor = conn.cursor()
cursor.execute("insert into contact(name,email,mobile) values(%s,%s,%s)", (new_contact.name
,new_contact.email
,new_contact.mobile))
conn.commit()
self.view_contacts()
except pymysql.DatabaseError as error:
print("While adding contact to DB :", error)
finally:
DbUtil.close_connection(conn, cursor)
def get_all_records(self):
try:
conn = DbUtil.get_connection()
cursor = conn.cursor()
cursor.execute("select cid,name,email,mobile from contact")
rows = cursor.fetchall()
lst = self.get_list_data(rows)
return lst
except pymysql.DatabaseError as error:
print("While geting data from DB :", error)
finally:
DbUtil.close_connection(conn, cursor)
def search_all_records(self, search_str):
try:
conn = DbUtil.get_connection()
cursor = conn.cursor()
cursor.execute("select cid,name,email,mobile from contact where name like %s ", ('%' + search_str + '%'))
rows = cursor.fetchall()
lst = self.get_list_data(rows);
return lst
except pymysql.DatabaseError as error:
print("While geting data from DB :", error)
finally:
DbUtil.close_connection(conn, cursor)
def get_list_data(self, rows):
lst = []
for row in rows:
cid = row[0]
name = row[1]
email = row[2]
mobile = row[3]
cont = Contact(name, email, mobile, cid)
lst.append(cont)
return lst
def view_contacts(self):
lst = self.get_all_records()
table = BeautifulTable()
table.column_headers = ["Cid", "Name", "Email", "Mobile"]
for li in lst:
table.append_row([li.id, li.name, li.email, li.mobile])
print(table)
def view_contact(self, con):
table = BeautifulTable()
table.column_headers = ["Cid", "Name", "Email", "Mobile"]
table.append_row([con.cid, con.name, con.email, con.mobile])
print(table)
def get_contact(self, cid):
try:
conn = DbUtil.get_connection()
cursor = conn.cursor()
cursor.execute("select cid,name,email,mobile from contact where cid = %s",cid)
row = cursor.fetchone()
cid = row[0]
name = row[1]
email = row[2]
mobile = row[3]
cont = Contact(name, email, mobile, cid)
self.view_contact(cont)
except pymysql.DatabaseError as error:
print("While geting data from DB :", error)
finally:
DbUtil.close_connection(conn, cursor)
def update_contact(self, contact):
try:
conn = DbUtil.get_connection()
cursor = conn.cursor()
cursor.execute("update contact set name = %s ,email = %s, mobile = %s where cid = %s", (contact.name,
contact.email, contact.mobile, contact.cid))
conn.commit()
except pymysql.DatabaseError as error:
print("While updating data of DB :", error)
conn.rollback()
finally:
DbUtil.close_connection(conn, cursor)
def delete_contact(self, cid):
try:
conn = DbUtil.get_connection()
cursor = conn.cursor()
cursor.execute("delete from contact where cid = %s ",cid)
conn.commit()
except pymysql.DatabaseError as error:
print("While deleting data from DB :", error)
finally:
DbUtil.close_connection(conn, cursor)
def search_contact(self, search_str):
lst = self.search_all_records(search_str)
table = BeautifulTable()
table.column_headers = ["Cid", "Name", "Email", "Mobile"]
for li in lst:
table.append_row([li.cid, li.name, li.email, li.mobile])
print(table)