Skip to content

Commit

Permalink
Update docs and examples to reflect renamed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ptmcg committed May 4, 2021
1 parent 6e07e4b commit 343815f
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 39 deletions.
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# plusminus

The **plusminus** package provides a ready-to-run arithmetic parser and evaluator, based on [`pyparsing`](https://pyparsing-docs.readthedocs.io/en/latest/index.html)'s
[`infixNotation`](https://pyparsing-docs.readthedocs.io/en/latest/pyparsing.html#pyparsing.infixNotation) helper method.
The **plusminus** package provides a ready-to-run arithmetic parser and evaluator,
based on [`pyparsing`](https://pyparsing-docs.readthedocs.io/en/latest/index.html)'s
[`infixNotation`](https://pyparsing-docs.readthedocs.io/en/latest/pyparsing.html#pyparsing.infixNotation)
helper method.

Strings containing 6-function arithmetic expressions can be parsed and evaluated using the [`BasicArithmeticParser`](https://github.com/pyparsing/plusminus/blob/master/doc/arithmetic_parser.md#the-core-basicarithmeticparser):
Strings containing 6-function arithmetic expressions can be parsed and evaluated using the
[`BaseArithmeticParser`](https://github.com/pyparsing/plusminus/blob/master/doc/arithmetic_parser.md#the-core-basicarithmeticparser):

```python
from plusminus import BasicArithmeticParser
from plusminus import BaseArithmeticParser

parser = BasicArithmeticParser()
parser = BaseArithmeticParser()
print(parser.evaluate("2+3/10"))
```

Expand Down Expand Up @@ -43,7 +46,7 @@ Functions can be called:
floor tan tanh log10


The Basic ArithmeticParser also supports assignment of variables:
The BaseArithmeticParser also supports assignment of variables:

r = 5
area = π × r²
Expand All @@ -57,8 +60,9 @@ This last expression could be assigned using `@=` formula assignment:
As `r` is updated, evaluating `area` will be reevaluated using the new value.


Custom expressions can be defined using a simple [`API`](https://github.com/pyparsing/plusminus/blob/master/doc/developer_api.md). Example parsers
are included for dice rolling, combination/permutation expressions, and
common business calculations. These parsers can be incorporated into
other applications to support the safe evaluation of user-defined
domain-specific expressions.
Custom expressions can be defined using a simple
[`API`](https://github.com/pyparsing/plusminus/blob/master/doc/developer_api.md).
Example parsers are included for dice rolling, combination/permutation expressions,
and common business calculations. These parsers can be incorporated into other
applications to support the safe evaluation of user-defined domain-specific
expressions.
11 changes: 6 additions & 5 deletions doc/arithmetic_parser.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## The core ArithmeticParser
## The core BaseArithmeticParser
- operators
- functions
- variables
Expand Down Expand Up @@ -81,10 +81,11 @@
dist = hypot(x₂-x₁, y₂-y₁)


## The core BasicArithmeticParser
## The core ArithmeticParser

The BasicArithmeticParser class inherits all the features and behavior of the
[`ArithmeticParser`](https://github.com/pyparsing/plusminus/blob/master/doc/arithmetic_parser.md#the-core-arithmeticparser) class. In addition, it also defines more operators and
The ArithmeticParser class inherits all the features and behavior of the
[`BaseArithmeticParser`](#the-core-basearithmeticparser)
class. In addition, it also defines more operators and
functions, and common mathematical variables

- operators
Expand Down Expand Up @@ -160,7 +161,7 @@ functions, and common mathematical variables
return math.log10(x)
return math.log(x, y)

class BasicArithmeticParser(ArithmeticParser):
class ArithmeticParser(BaseArithmeticParser):
def customize(self):
"""Entry point to define operators, functions and variables."""
import math
Expand Down
3 changes: 2 additions & 1 deletion doc/arithmetic_parser_user_guide.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# User Guide - Working with the Basic Arithmetic Parser
# User Guide - Working with the plusminus Arithmetic Parser

## Introduction

Expand All @@ -19,6 +19,7 @@
- set operators ``/`&`, ``/`|`, ``/`in`, ``/`not in`
- sets are defined using `{`, `}` symbols
- `` is intersection, `` is union
- `&` and `|` are equivalent to `` and ``
- `` is `is element of`, `` is `is not element of`

1 ∈ {1, 2, 3} ∪ {10, 11, 12} evaluates True
Expand Down
2 changes: 1 addition & 1 deletion doc/developer_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Defining an operator that is both unary and binary
- Be extremely careful if reading/writing to the file system
- Take care when exposing access to an underlying database or server files
- Some math functions may need to be constrained to avoid extended arithmetic processing (see
`constrained_factorial` in the [`BasicArithmeticParser`](https://github.com/pyparsing/plusminus/blob/master/doc/arithmetic_parser.md#the-core-basicarithmeticparser) as an example)
`constrained_factorial` in the [`BaseArithmeticParser`](https://github.com/pyparsing/plusminus/blob/master/doc/arithmetic_parser.md#the-core-basearithmeticparser) as an example)
- When populating web page elements with gathered input strings, be sure to escape potential quotation and control
characters (see [`bottle_repl.py`](https://github.com/pyparsing/plusminus/blob/master/plusminus/examples/bottle_repl.py) as an example)
- Be aware that your functions may get called recursively, or in an
Expand Down
8 changes: 4 additions & 4 deletions doc/example_parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Class implementation:

```python
class BusinessArithmeticParser(ArithmeticParser):
class BusinessArithmeticParser(BaseArithmeticParser):
def customize(self):
def pv(fv, rate, n_periods):
return fv / safe_pow(1 + rate, n_periods)
Expand Down Expand Up @@ -54,7 +54,7 @@ parser.runTests("""\
Class implementation:

```python
class CombinatoricsParser(BasicArithmeticParser):
class CombinatoricsParser(BaseArithmeticParser):
def customize(self):
super().customize()
self.add_operator(
Expand Down Expand Up @@ -100,7 +100,7 @@ Class implementation:
```python
from datetime import datetime

class DateTimeArithmeticParser(ArithmeticParser):
class DateTimeArithmeticParser(BaseArithmeticParser):
SECONDS_PER_MINUTE = 60
SECONDS_PER_HOUR = SECONDS_PER_MINUTE * 60
SECONDS_PER_DAY = SECONDS_PER_HOUR * 24
Expand Down Expand Up @@ -138,7 +138,7 @@ parser.runTests("""\
Class implementation:

```python
class DiceRollParser(ArithmeticParser):
class DiceRollParser(BaseArithmeticParser):
def customize(self):
import random
super().customize()
Expand Down
24 changes: 12 additions & 12 deletions plusminus/examples/bottle_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from datetime import datetime, timedelta
import textwrap
from plusminus import (
BasicArithmeticParser,
ArithmeticParser,
ArithmeticParseException,
__version__ as plusminus_version,
)
Expand Down Expand Up @@ -330,7 +330,7 @@ def write_html_table(self, rowlist):
buffer.append("</table>")
self.write_html("\n".join(buffer))

def _handle_app_request(self):
def handle_app_request(self):
title_string = "Plusminus +/- Parser/Evaluator Tester - {}".format(
plusminus_version
)
Expand All @@ -351,7 +351,7 @@ def _handle_app_request(self):
player, game = session_info.player, session_info.game
else:
if len(sessions) < MAX_SESSIONS:
game = Repl(BasicArithmeticParser)
game = Repl(ArithmeticParser)
# create player session
sessionkey = game.start_new_session()
else:
Expand Down Expand Up @@ -469,7 +469,7 @@ def button_row(s, label=""):
)
self.write_html("\n</body></html>")

def _handle_stats_request(self):
def handle_stats_request(self):
now = datetime.now()
self.write_html("<html><body>\n")
self.write_html("<h2>Stats as of {}</h2>\n<p>\n".format(time_to_str(now)))
Expand Down Expand Up @@ -586,7 +586,7 @@ def _handle_stats_request(self):
self.write_html("</table>\n")
self.write_html("\n</body></html>")

def _handle_cleanup_request(self):
def handle_cleanup_request(self):
with sessions_lock:
sessions_data = list(sessions.items())

Expand All @@ -606,28 +606,28 @@ def _handle_cleanup_request(self):
for key in deletes:
sessions.pop(key, None)

return self._handle_stats_request()
return self.handle_stats_request()


if __name__ == "__main__":
# bottle server "main"

@route("/plusminus/_stats")
def handle_stats_command():
handler = BottleArithReplRequestHandler()
handler._handle_stats_request()
handler = BottlePlusminusReplRequestHandler()
handler.handle_stats_request()
return "".join(handler.buffer)

@route("/plusminus/_cleanup")
def handle_cleanup_command():
handler = BottleArithReplRequestHandler()
handler._handle_cleanup_request()
handler = BottlePlusminusReplRequestHandler()
handler.handle_cleanup_request()
return "".join(handler.buffer)

@route("/plusminus")
def handle_app_command():
handler = BottleArithReplRequestHandler()
handler._handle_app_request()
handler = BottlePlusminusReplRequestHandler()
handler.handle_app_request()
return "".join(handler.buffer)

@route("/")
Expand Down
4 changes: 2 additions & 2 deletions plusminus/examples/console_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
from pprint import pprint
import textwrap
from plusminus import BasicArithmeticParser, ArithmeticParseException
from plusminus import ArithmeticParser, ArithmeticParseException


def usage(parser):
Expand Down Expand Up @@ -75,7 +75,7 @@ def run_repl(parser_class):


def main():
parser_class = BasicArithmeticParser
parser_class = ArithmeticParser
run_repl(parser_class)


Expand Down
6 changes: 3 additions & 3 deletions plusminus/examples/plusminus_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#
# Copyright 2020, Paul McGuire
#
from plusminus import BasicArithmeticParser, ArithmeticParseException
from plusminus.examples.example_parsers import *
from plusminus import ArithmeticParser, ArithmeticParseException
# from plusminus.examples.example_parsers import *
from pprint import pprint

try:
Expand All @@ -16,7 +16,7 @@

prompt = '> '
prompt_indent = ' ' * len(prompt)
parser = BasicArithmeticParser()
parser = ArithmeticParser()

while True:
expression = input(prompt).strip()
Expand Down

0 comments on commit 343815f

Please sign in to comment.