Skip to content

Instantly share code, notes, and snippets.

@lovevn
Last active December 27, 2015 09:49
Show Gist options
  • Save lovevn/7306704 to your computer and use it in GitHub Desktop.
Save lovevn/7306704 to your computer and use it in GitHub Desktop.
Python MVC Example
import sqlite3
class MVCModel:
def request(self, id):
# Would query database...
conn = sqlite3.connect('querydb')
c = conn.cursor()
results = c.execute('''select name from data where id = %d''' %id)
conn.commit()
c.close()
for row in results:
name = row[0]
return { "id" : id, "name": name}
class MVCController:
def __init__(self):
self.model = MVCModel()
self.view = MVCView()
def main(self):
post = self.model.request(1)
self.view.show(post)
class MVCView:
def show(self, post):
print "%(id)s %(name)s" % post
Controller = MVCController()
Controller.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment