Python設(shè)計(jì)模式編程中解釋器模式的簡單程序示例分享
模式特點(diǎn):給定一個(gè)語言,定義它的文法的一種表示,并定義一個(gè)解釋器,這個(gè)解釋器使用該表示來解釋語言中的句子。
我們來看一下下面這樣的程序結(jié)構(gòu):
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)
那么它所體現(xiàn)出的類圖是這樣的:
再來看一個(gè)例子:
#encoding=utf-8 # #by panda #解釋器模式 def printInfo(info): print unicode(info, 'utf-8').encode('gbk'), #上下文類:演奏內(nèi)容 class PlayContext(): text = None PlayText = None #抽象表達(dá)式類 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(' ') #找出第一個(gè)空格出現(xiàn)的位置 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();
類圖:
相關(guān)文章
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)篩選及提取序列中元素的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)篩選及提取序列中元素的方法,涉及Python列表推導(dǎo)式、生成器表達(dá)式及filter()函數(shù)相關(guān)使用技巧,需要的朋友可以參考下2018-03-03總結(jié)python 三種常見的內(nèi)存泄漏場景
這篇文章主要介紹了總結(jié)python 三種常見的內(nèi)存泄漏場景,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11Python中使用format函數(shù)的小結(jié)
在Python中,format()函數(shù)是一種用于格式化字符串的方法主要介紹了Python中使用format函數(shù)的小結(jié),本文就來介紹一下format()函數(shù)的使用示例,感興趣的可以了解一下2023-08-08Python字符串的15個(gè)基本操作(小結(jié))
這篇文章主要介紹了Python字符串的15個(gè)基本操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python項(xiàng)目運(yùn)行導(dǎo)致內(nèi)存越來越大的原因詳析
最近在跑python程序時(shí),出現(xiàn)占用的內(nèi)存不斷增加的情況,下面這篇文章主要給大家介紹了關(guān)于python項(xiàng)目運(yùn)行導(dǎo)致內(nèi)存越來越大的原因詳析,本文通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11Python正則獲取、過濾或者替換HTML標(biāo)簽的方法
這篇文章主要介紹了Python通過正則表達(dá)式獲取、過濾或者替換HTML標(biāo)簽的方法,感興趣的小伙伴們可以參考一下2016-01-01python?使用ctypes調(diào)用C/C++?dll詳情
這篇文章主要介紹了python?使用ctypes調(diào)用C/C++?dll詳情,文章首先通過導(dǎo)入ctypes模塊,加載C/C++?dll到python進(jìn)程空間展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04