MVC模式:python案例
quotes = ('A man is not complete until he is married. Then he is finished.',
'As I said before, I never repeat myself.',
'Behind a successful man is an exhausted woman.',
'Black holes really suck...', 'Facts are stubborn things.')
class QuoteModel:
def get_quote(self, n):
try:
value = quotes[n]
except IndexError as err:
value = 'Not found!'
return value
class QuoteTerminalView:
def show(self, quote):
print('And the quote is: "{}"'.format(quote))
def error(self, msg):
print('Error: {}'.format(msg))
def select_quote(self):
return input('Which quote number would you like to see?')
class QuoteTerminalController:
def __init__(self):
self.model = QuoteModel()
self.view = QuoteTerminalView()
def run(self):
valid_input = False
while not valid_input:
n = self.view.select_quote()
try:
n = int(n)
except ValueError as err:
self.view.error("Incorrect index '{}'".format(n))
else:
valid_input = True
quote = self.model.get_quote(n)
self.view.show(quote)
def main():
controller = QuoteTerminalController()
while True:
controller.run()
if __name__ == '__main__':
main()
MVC模式:python案例的更多相关文章
- MVC模式入门案例
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widg ...
- python的设计模式之MVC模式
模型-视图-控制器模式 关注点分离(Separation of Concerns,SoC)原则是软件工程相关的设计原则之一.SoC原则背后的思想是将一个应用切分成不同的部分,每个部分解决一个单独的关注 ...
- Python设计模式之MVC模式
# -*- coding: utf-8 -*- # author:baoshan quotes = ('A man is not complete until he is married. Then ...
- 【案例分享】使用ActiveReports报表工具,在.NET MVC模式下动态创建报表
提起报表,大家会觉得即熟悉又陌生,好像常常在工作中使用,又似乎无法准确描述报表.今天我们来一起了解一下什么是报表,报表的结构.构成元素,以及为什么需要报表. 什么是报表 简单的说:报表就是通过表格.图 ...
- python 设计模式之MVC模式
一.简单介绍 mvc模式 the model-view-controller pattern mvc模式是一个运用在软件工程中的设计模式.mvc模式脱离了以前简单的web服务设计逻辑,将开发,测试 ...
- Django的MVT模式与MVC模式
Django的MVT模式与MVC模式 在正式开始coding之前,我觉得有必要探讨下Django的MVT模式,理论和实践相结合,才能更好的掌握一门技术.Django中的MVT模式,Django就是属于 ...
- MVC模式-----struts2框架(2)
MVC模式-----struts2框架 第一个struts2程序 struts2框架是通过一个过滤器将struts2集成到Web应用程序中的,这个过滤器的对象是StrutsprepareAndExec ...
- Extjs MVC模式开发,循序渐进(一)
本文讲述extjs mvc的Helloworld,tabPanel,event,页面布局layout等内容. 本页包含:MVC模式案例(一)~MVC模式案例(六),从搭建extjs mvc到点击按钮生 ...
- 网页的MVC模式简介
#! /usr/bin/env python3 # -*- coding:utf-8 -*- #MVC:Model-View-Controller 模型-视图-控制器 #Python处理URL的函数就 ...
- [转载]WIKI MVC模式
MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller). MVC模式最 ...
随机推荐
- unity3d游戏开发学习分享之表面着色器讲解
一.三种着色器的书写格式: 1.surface shaders, 指的是表面着色器 2.vertex and fragment shaders and 指的是顶点和片段着色器 3.fixed func ...
- 搭建基于 HDFS 碎片文件存储服务
安装 JDK HDFS 依赖 Java 环境,这里我们使用 yum 安装 JDK 8,在终端中键入如下命令: yum -y install java-1.8.0-openjdk* 使用如下命令查看下 ...
- oracle函数学习_根据用户id获取用户角色
create or replace function FN_GET_ROLES(v_user_id varchar2) return varchar2 istype zy_emp_cursor is ...
- conn.setAutoCommit(false)数据回滚设置
前言:介绍一点爱混淆的概念. 1.mysql中默认 autocommit=1:事物自动提交. 可通过 select @@autocommit;查看 但是是设置事务自动提交模式为set autocomm ...
- OpenGL 4.0 GLSL 基础教程概览——VAO和VBO常用操作接口
(一) OpenGL 4.3 最新渲染管线图 从OpenGL 2.0 到 OpenGL 3.0变化非常大,但从OpenGL 3.0 到OpenGL 4.0 变化不是太大. 着色器程序直接运行在GPU ...
- iOS 录音功能的实现
这两天也调了一下ios的录音,原文链接:http://www.iphoneam.com/blog/index.php?title=using-the-iphone-to-record-audio-a- ...
- 《C++ Primer Plus》10.3 类的构造函数和析构函数 学习笔记
10.3.1 声明和定义构造函数构造函数原型:// constructor prototype with some default argumentsStock(const string &c ...
- android基础组件---->Button的使用
按钮由文本或图标(或文本和一个图标)组成,当用户触摸到它时,会发生一些动作.今天我们开始Button的学习.少年的爱情永远不够用,一杯酒足以了却一件心事. Button的简要说明 根据你是否想要一个带 ...
- web基础----->servlet中得到请求的数据
对tomcat的源码做一些分析,今天我们就开始servlet中的请求分析. form表单中的默认类型 一.在index.jsp中get请求: <form action="Paramet ...
- c++ 引用底层实现
红色是我添加的,其他地方是原作者的. 主要是看了上面的这篇“从底层汇编理解 c++ 引用实现机制“的文章之后,觉得不错.就转了过来,同时,对文中的程序都在自己的机器上验证了一下. 使用的G++版本:g ...