diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-10-31 21:10:28 -0700 |
---|---|---|
committer | Paul McGuire <ptmcg@users.noreply.github.com> | 2019-10-31 23:10:28 -0500 |
commit | 53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch) | |
tree | 088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/shapes.py | |
parent | 41752aa52cc97c710474bb2972cceab057b52ad4 (diff) | |
download | pyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz |
Blacken the project (#141)
Diffstat (limited to 'examples/shapes.py')
-rw-r--r-- | examples/shapes.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/examples/shapes.py b/examples/shapes.py index b0fe979..5f621b1 100644 --- a/examples/shapes.py +++ b/examples/shapes.py @@ -17,37 +17,45 @@ class Shape: def __str__(self): return "<{}>: {}".format(self.__class__.__name__, vars(self)) + class Square(Shape): def area(self): - return self.side**2 + 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 + return 3.14159 * self.radius ** 2 import pyparsing as pp -number = pp.Regex(r'-?\d+(\.\d*)?').setParseAction(lambda t: float(t[0])) +number = pp.Regex(r"-?\d+(\.\d*)?").setParseAction(lambda t: float(t[0])) # 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 = "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 + tokens["radius"] = tokens.diameter / 2.0 + + circleDefn.setParseAction(computeRadius, Circle) shapeExpr = squareDefn | rectDefn | circleDefn |