Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

http://kbrooks.ath.cx

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Model, View, Controller HOWTO

MVC howto
Original vrsion can be found at http://kbrooks.ath.cx/mvchowto/intro.py

   1  
   2  #!/usr/bin/python
   3  
   4  # MVC Howto - Introduction & code
   5  # What is "MVC"?
   6  #   MVC stands for "Model", "View" & "Controller", a way of seperating your code into
   7  #   manageable and independent chunks that can be changed easily.
   8  # Why use MVC?
   9  #   Because code is independent from each other, it becomes easier for you to change part of the code to do what you want.
  10  #   For example, you could change where data is shown without changing anything else!
  11  # Copyright (c) 2006 Kyle Brooks. Licensed under the Creative Commons Attribution License.
  12  # We will be creating a command line calculator in this HOWTO.
  13  import sys
  14  # Model: a object that interfaces with the real world and returns information.
  15  
  16  class Model:
  17      def calculate(self, left, op, right):
  18          if op == "+":
  19              return left + right
  20          elif op == "-":
  21              return left - right
  22  # As you see in the code above, the model has the action "calculate".
  23  
  24  # View: a object that gets passed the result from the model via the controller. Displays data.
  25  class View:
  26      def display_data(self, result):
  27          print result
  28  # As you see in the code above, ALL the view does is display data from the controller.
  29  
  30  # Controller: a object that holds a reference to the model and view. Communicates with the model and view. Handles data from the user.
  31  class Controller:
  32      operands = ["+", "-"]
  33      def __init__(self, model, view):
  34          self.model = model
  35          self.view = view
  36      def run(self):
  37          sys.stdout.write(">> ")
  38          sys.stdout.flush()
  39          # read a line
  40          line = sys.stdin.readline()
  41          line = line.strip()
  42          if line == "":
  43              sys.stdout.write("\n>> ")
  44              sys.stdout.flush()
  45              return
  46          for operand in self.operands:
  47              larray = line.split(operand)
  48              if larray[0] == line:
  49                  continue
  50              else:
  51                  break
  52          else:
  53              sys.stdout.write("NaN\n")
  54              sys.stdout.flush()
  55              return
  56          larray[0] = int(larray[0])
  57          larray[2] = int(larray[0])
  58          result = self.model.calculate(*larray)
  59          self.view.display_data(result)
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS