summaryrefslogtreecommitdiff
path: root/examples/shapes.py
blob: 418ade286f013b226485835f8317b9196d2353e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# shapes.py
#
#   A sample program showing how parse actions can convert parsed
# strings into a data type or object.
#
# Copyright 2012, 2019 Paul T. McGuire
#

# define class hierarchy of Shape classes, with polymorphic area method
class Shape:
    def __init__(self, tokens):
        self.__dict__.update(tokens.asDict())

    def area(self):
        raise NotImplemented()

    def __str__(self):
        return "<{}>: {}".format(self.__class__.__name__, vars(self))


class Square(Shape):
    def area(self):
        return self.side ** 2


class Rectangle(Shape):
    def area(self):
        return self.width * self.height


class Circle(Shape):
    def area(self):
        return 3.14159 * self.radius ** 2


import pyparsing as pp

ppc = pp.pyparsing_common

# use pyparsing-defined numeric expression that converts all parsed
# numeric values as floats
number = ppc.fnumber()

# Shape expressions:
#   square : S <centerx> <centery> <side>
#   rectangle: R <centerx> <centery> <width> <height>
#   circle : C <centerx> <centery> <diameter>

squareDefn = "S" + number("centerx") + number("centery") + number("side")
rectDefn = (
    "R" + number("centerx") + number("centery") + number("width") + number("height")
)
circleDefn = "C" + number("centerx") + number("centery") + number("diameter")

squareDefn.setParseAction(Square)
rectDefn.setParseAction(Rectangle)


def computeRadius(tokens):
    tokens["radius"] = tokens.diameter / 2.0


circleDefn.setParseAction(computeRadius, Circle)

shapeExpr = squareDefn | rectDefn | circleDefn

tests = """\
C 0 0 100
R 10 10 20 50
S -1 5 10""".splitlines()

for t in tests:
    shape = shapeExpr.parseString(t)[0]
    print(shape)
    print("Area:", shape.area())
    print()