Simple program examples of interpreter mode in Python Design Mode Programming, python Design Mode

Source: Internet
Author: User

Simple program examples of interpreter mode in Python Design Mode Programming, python Design Mode

Pattern features: Given a language, define a representation of its syntax and define an interpreter which uses this representation to interpret sentences in the language.

Let's take a look at the following program structure:

class Context:  def __init__(self):    self.input=""    self.output=""class AbstractExpression:  def Interpret(self,context):    passclass 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)

The class diagram it shows is as follows:

Let's look at an example:

# Encoding = UTF-8 ## by panda # interpreter mode def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk'), # context class: Playing content class PlayContext (): text = None PlayText = None # Abstract Expression 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 ('') # locate the position where the first space appears playValue = context. playText [0: tmp] context. playText = context. playText [tmp + 1:] self. excute (playKey, playValue) def Excute (self, playKey, playValue): pass # Pitch class pitch (Expression): Pitch = None def Excute (self, key, value ): value = int (value) if value = 1: self. pitch = 'bass 'elif value = 2: self. pitch = 'sound' elif value = 3: self. pitch = 'tweeter 'printinfo (self. pitch) # Note 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.5G 0.5 A 3 E 0.5G 0.5 D 3 E 0.5G 0.5 A 0.5 O 3 C 1 O 2 A 0.5G 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 ();


Class diagram:

Articles you may be interested in:
  • Measure the test taker's knowledge about the observer mode and Strategy Mode in Python Design Mode Programming.
  • An example of how to use the design pattern in Python
  • Analysis of the key points of the Decorator pattern in Python
  • Instance parsing Python Design Mode Programming-Application of Bridge Mode
  • Examples of Adapter mode in Python Design Mode Programming
  • Application Example of prototype in design mode in Python Program
  • In-depth analysis of the use of builder mode in Python Design Mode Programming
  • An example of how to use the abstract factory mode in Python Design Mode Programming
  • The example explains how to use the factory method mode of programming in Python design mode.
  • Detailed description of the use of the factory method mode in the design mode in the Python Program
  • Use the simple factory mode for Python Design Mode Programming
  • Example of using the responsibility chain mode and iterator mode in the design mode in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.