SoFunction
Updated on 2024-11-15

Python design patterns of the MVC pattern simple example

This article is an example of Python design patterns of the MVC pattern. Shared for your reference, as follows:

I. Brief introduction

mvc pattern the model-view-controller pattern

The MVC pattern is a design pattern used in software engineering. mvc pattern is a departure from the previous simple web service design logic, separating development, testing and maintenance. In the MVC pattern, the application is decomposed into interacting modules, models, views, and controls. The goal is to separate input (control), processing logic (model), and output format (view).

Simple to understand:

1. The control module is used to obtain user input and to link the model to the views
2. The model mainly obtains data from the storage area
3. Views are used to present to the user the data obtained from the model

Specifics:

control module: can be seen as an intermediary between the user, processing (model), display (view). It is the entry point for user requests and for application processing. The control module accepts user input, parses it, and decides which model and view are to be involved in the processing, so it decides which view and model are to be selected for the user request.

Modeling Module:Applications that deal with business, models manipulate databases such as insert, update, delete. Each model will provide 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 the data and return the processed results to the view model

view moduleThe main purpose is to display the data, which is processed by the model module and formatted for display through the control module. The control module selects the view and displays it back to the user. the selection of the view model is based on the selection of the model module and the user configuration, and so on.

II. Simple examples

The test management system is used to query the error list

Scenario Description:

If a user queries for a specific error, the test management system displays a description of this error in some format
If the user searches for the key value of the relevant error, the test management system displays a list of all relevant errors

Create a SQLite database with the library 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:

#
import sqlite4
import types
class DefectModel:
  def getDefectList(self, component):
    query = "select ID from defects where Component= '%s' " % component
    defectlist = self._dbselect(query)
    list = []
    for row in defectlist:
      (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 = ('TMS')
    cursorObj = ()
    results = (query)
    ()
    ()
    return results
class 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 defect
class Controller:
  def __init__(self):
    pass
  def getDefectSummary(self, defectid):
    model = DefectModel()
    view = DefectView()
    summary_data = (defectid)
    return (summary_data, defectid)
  def getDefectList(self, component):
    model = DefectModel()
    view = DefectView()
    defectlist_data = (component)
    return (defectlist_data, component)

Use the module:

import mvc
controller = ()
print (2)
print ('ABC')

Summary:Through this mvc design methodology, see the benefits of decoupling, individual modules are independent, do not affect each other, you can also add modules. Easy to combine, easy to disassemble. Experience it well!

More about Python related content can be viewed on this site's topic: thePython Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.