Design-Patterns-In-Python/interpreter at master · byjackchen/Design-Patterns-In-Python

Interpreter Design Pattern

Videos

Section Video Links
Interpreter Overview Interpreter Overview Interpreter Overview Interpreter Overview
Interpreter Use Case Interpreter Use Case Interpreter Use Case Interpreter Use Case
String Slicing String Slicing String Slicing String Slicing
repr Dunder Method __repr__ Dunder Method __repr__ Dunder Method __repr__ Dunder Method

Book

Cover Links
Design Patterns In Python (ASIN : B08XLJ8Z2J)    https://www.amazon.com/dp/B08XLJ8Z2J
   https://www.amazon.co.uk/dp/B08XLJ8Z2J
   https://www.amazon.in/dp/B08Z282SBC
   https://www.amazon.de/dp/B08XLJ8Z2J
   https://www.amazon.fr/dp/B08XLJ8Z2J
   https://www.amazon.es/dp/B08XLJ8Z2J
   https://www.amazon.it/dp/B08XLJ8Z2J
   https://www.amazon.co.jp/dp/B08XLJ8Z2J
   https://www.amazon.ca/dp/B08XLJ8Z2J
   https://www.amazon.com.au/dp/B08Z282SBC

Overview

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Terminology

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Interpreter UML Diagram

Interpreter UML Diagram

Source Code

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Output

python ./interpreter/interpreter_concept.py
5 + 4 - 3 + 7 - 2
['5', '+', '4', '-', '3', '+', '7', '-', '2']
11
((((5 Add 4) Subtract 3) Add 7) Subtract 2)

Example Use Case

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.

Abstract Syntax Tree Example

Example UML Diagram

Interpreter Pattern Overview

Output

python ./interpreter/client.py 
5 + IV - 3 + VII - 2
['5', '+', 'IV', '-', '3', '+', 'VII', '-', '2']
11
((((5 Add IV(4)) Subtract 3) Add VII(7)) Subtract 2)

New Coding Concepts

String Slicing

Sometimes you want part of a string. In the example code, when I am interpreting the roman numerals, I am comparing the first one or two characters in the context with IV or CM or many other roman numeral combinations. If the match is true then I continue with further commands.

The format is

E.g., the string may be

and I want the first three characters,

test = "MMMCMXCIX"
print(test[0: 3])

Outputs

or I want the last 4 characters

test = "MMMCMXCIX"
print(test[-4:])

Outputs

or I want a section in the middle

test = "MMMCMXCIX"
print(test[2:5])

Outputs

or stepped

test = "MMMCMXCIX"
print(test[2:9:2])

Outputs

or even reversed

test = "MMMCMXCIX"
print(test[::-1])

Outputs

The technique is very common in examples of Python source code throughout the internet. So, when you see the [] with numbers and colons inside, eg, [:-1:], it is likely to do with extracting a portion of a data structure.

Note that the technique also works on Lists and Tuples.

test = [1,2,3,4,5,6,7,8,9]
print(test[0: 3])
print(test[-4:])
print(test[2:5])
print(test[2:9:2])
print(test[::-1])
print(test[:-1:])

Outputs

[1, 2, 3]
[6, 7, 8, 9]
[3, 4, 5]
[3, 5, 7, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7, 8]

Summary

... Refer to Book, pause Video Lectures or subscribe to Medium Membership to read textual content.