Python设计模式编程中解释器模式的简单程序示例分享

yipeiwu_com5年前Python基础

模式特点:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。

我们来看一下下面这样的程序结构:

class Context:
  def __init__(self):
    self.input=""
    self.output=""

class AbstractExpression:
  def Interpret(self,context):
    pass

class Expression(AbstractExpression):
  def Interpret(self,context):
    print "terminal interpret"

class NonterminalExpression(AbstractExpression):
  def Interpret(self,context):
    print "Nonterminal interpret"

if __name__ == "__main__":
  context= ""
  c = []
  c = c + [Expression()]
  c = c + [NonterminalExpression()]
  c = c + [Expression()]
  c = c + [Expression()]
  for a in c:
    a.Interpret(context)

那么它所体现出的类图是这样的:

201632150316773.png (682×450)

再来看一个例子:

#encoding=utf-8 
# 
#by panda 
#解释器模式 
 
def printInfo(info): 
  print unicode(info, 'utf-8').encode('gbk'), 
 
#上下文类:演奏内容 
class PlayContext(): 
  text = None 
  PlayText = None 
 
#抽象表达式类 
class Expression(): 
  def Interpret(self, context): 
    if len(context.PlayText) == 0: 
      return 
    else: 
      playKey = context.PlayText[0:1] 
      context.PlayText = context.PlayText[2:] 
      tmp = context.PlayText.index(' ') #找出第一个空格出现的位置 
      playValue = context.PlayText[0:tmp] 
      context.PlayText = context.PlayText[tmp+1:] 
      self.Excute(playKey,playValue) 
   
  def Excute(self,playKey,playValue): 
    pass 
 
#音高 
class Pitch(Expression): 
  pitch = None 
  def Excute(self, key, value): 
    value = int(value) 
    if value == 1: 
      self.pitch = '低音' 
    elif value == 2: 
      self.pitch = '中音' 
    elif value == 3: 
      self.pitch = '高音' 
    printInfo(self.pitch) 
     
#音符 
class Note(Expression): 
  Notes = { 
  'C':1,   
  'D':2, 
  'E':3,   
  'F':4,   
  'G':5,   
  'A':6,   
  'B':7,   
  } 
  note = None 
  def Excute(self, key, value):    
    self.note = self.Notes[key] 
    printInfo('%d' % self.note) 
 
 
def clientUI(): 
  context = PlayContext() 
  context.PlayText = "O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3 " 
  expression = None; 
  while(len(context.PlayText) > 0): 
    str = context.PlayText[0:1]; 
    if(str == 'O'): 
      expression = Pitch() 
    elif(str == 'C' or str == 'D' or str == 'E' or str == 'F' or str == 'G' or str == 'A' or str == 'B' or str == 'P'): 
      expression = Note() 
    expression.Interpret(context) 
       
  return 
 
if __name__ == '__main__': 
  clientUI(); 


类图:

201632150401221.gif (695×368)

相关文章

Python实现将一个大文件按段落分隔为多个小文件的简单操作方法

本文实例讲述了Python实现将一个大文件按段落分隔为多个小文件的简单操作方法。分享给大家供大家参考,具体如下: 今天帮同学处理一点语料。语料文件有点大,并且是以连续两个换行符作为段落标...

Python基于scipy实现信号滤波功能

Python基于scipy实现信号滤波功能

​ 1.背景介绍 在深度学习中,有时会使用Matlab进行滤波处理,再将处理过的数据送入神经网络中。这样是一般的处理方法,但是处理起来却有些繁琐,并且有时系统难以运行Matl...

Python列表切片操作实例总结

本文实例讲述了Python列表切片操作。分享给大家供大家参考,具体如下: 切片指的是列表的一部分。 1 基本用法 指定第一个元素和最后一个元素的索引,即可创建切片 。Python 会在到...

Python pandas.DataFrame 找出有空值的行

Python pandas.DataFrame 找出有空值的行

0.摘要 pandas中DataFrame类型中,找出所有有空值的行,可以使用.isnull()方法和.any()方法。 1.找出含有空值的行 方法:DataFrame[DataFram...

django框架自定义模板标签(template tag)操作示例

本文实例讲述了django框架自定义模板标签(template tag)操作。分享给大家供大家参考,具体如下: django 提供了丰富的模板标签(template tag)和过滤器(t...