Design Patterns in Python is a free online book written by Rahul Verma and Chetan Giridhar on the subject of employing design patterns in the world of object-oriented programming using Python. The book is written in a way to keep the discussion and the related examples simple. This is done to provide a text so that the concept of design patterns can reach to those who are not used to formal programming practices. It’s an on-going effort, so bookmark this page for more!
To know about design patterns at a very high level, checkout the following link:
Preface
As of now, the book covers the following design patterns:
The number of design patterns demonstrated would increase over the next few months. This book would be published as a PDFversion on 1-1-11 (Jan 1, 2011)
very good introduduction to design patterns, below python 3 code for MVC pattern, it’s my opinion that in the original version there were some bugs
best regards
Guido
import sqlite3
import types
class DefectModel:
def getDefects(self, component):
query = ”’select ID from defects where component = ‘%s’ ”’ % component
defectlist = self._dbselect(query)
list = []
for row in defectlist:
list.append(row[0])
return list
def getSummary(self, id):
query = ”’select summary from defects where ID = ‘%d’ ”’ % id
summary = self._dbselect(query)
return summary[0][0]
def _dbselect(self, query):
connection = sqlite3.connect(‘TMS.db’)
cursorObj = connection.cursor()
#fetchall() added, cursorObj.execute(query) is an sqlite3.cursor object
results = cursorObj.execute(query).fetchall()
# save (commit) the changes
connection.commit()
cursorObj.close()
return results
class DefectView:
def summary(self, summary, defectid):
print (“#### Defect Summary for defect# %d####\n%s” % (defectid,summary))
def defectList(self, list, category):
print (“#### Defect List for %s ####” % category)
for defect in list:
print (defect)
class Controller:
def __init__(self):
pass
def getDefectSummary(self, defectid):
model = DefectModel()
view = DefectView()
summary_data = model.getSummary(defectid)
return view.summary(summary_data, defectid)
def getDefectList(self, component):
model = DefectModel()
view = DefectView()
defectlist_data = model.getDefects(component)
return view.defectList(defectlist_data, component)
if __name__ == ‘__main__’:
controller = Controller()
# Displaying summary for defect ID 2
controller.getDefectSummary(2)
# Displaying defect list for ‘net’ Component
controller.getDefectList(‘net’)