Simple Example of MVC mode in Python design mode, and mvc mode in python Design Mode

Source: Internet
Author: User

Simple Example of MVC mode in Python design mode, and mvc mode in python Design Mode

This article describes the MVC mode of the Python design mode. We will share this with you for your reference. The details are as follows:

1. Brief Introduction

Mvc mode the model-view-controller pattern

The mvc pattern is a design pattern used in software engineering. The mvc pattern separates development, testing, and maintenance from the previous simple web service design logic. In the MVC mode, applications are decomposed into interaction modules, models, views, and controls. The objective is to separate the input (control), processing logic (model), and output format (view ).

Simple understanding:

1. the control module is used to obtain user input and establish a connection between the model and the view.
2. The model mainly obtains data from the storage area.
3. Views are used to display the data obtained from the model to users.

Details:

Control Module: it can be seen as a man-in-the-middle between users, models, and views. It is the entry of user requests and the entry of application processing. The control module accepts user input, parses, and determines which model and view are used for processing. Therefore, it determines which view and model are selected for user requests.

Model module: the application that processes the business. The model operates the database, such as insert, update, and delete. Each model provides a fixed type of data to the control module. On the other hand, the control module can call different methods of the model to process data and return the processed results to the view model.

View module: This module is mainly used for display. The control module obtains the data processed by the model module and displays it in a formatted manner. Select view through the control module and display the feedback to the user. The view model is selected based on model module l selection and user configuration.

Ii. Simple Example

The test management system is used to query the Error List.

Scenario Description:

If a user queries a specific error, the Test Management System displays the description of the error in a certain format.
If you search for key values of related errors, the Test Management System displays a list of all related errors.

Create SQLite database, database name TMS, and create a table

ID Component Summary
1 XYZ File doesn' t get deleted
2 XYZ Registry doesn' t get created
3 ABC Wrong title gets displayed

The Code is as follows:

#mvc.pyimport sqlite4import typesclass DefectModel:  def getDefectList(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)    for row in summary:      return row[0]  def _dbselect(self, query):    connection = sqlite3.connect('TMS')    cursorObj = connection.cursor()    results = cursorObj.execute(query)    connection.commit()    cursorObj.close()    return resultsclass DefectView:  def summary(self, summary, defectid):    print "#### Defect Summary for defect# %d####%s\n" %(defectid, summary)  def defectList(self, list, category):    print "#### Defect List for %s ####\n" % category    for defect in list:      print defectclass 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.getDefectList(component)    return view.defectList(defectlist_data, component)

Modules:

import mvccontroller = mvc.Controller()print controller.getDefectSummary(2)print controller.getDefectList('ABC')

Summary:Through the mvc design method, we can see the benefits of decoupling. Each module is independent and does not affect each other. You can also add modules. Convenient combination and convenient disassembly. Have a good experience!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.