Skip to content

Commit

Permalink
bump to version 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdihaghverdi committed Dec 24, 2022
1 parent 31e62ea commit 4250564
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
- `v0.8.0`
- bump to version `v0.8.0`
- add str_repr_timeout to `Calc` type

- `v0.7.2`
- bump to version `v0.7.2`
- fix error message of str repr
Expand Down
26 changes: 13 additions & 13 deletions postfixcalc/pyeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ def evaluate(postfix: "ListExpression") -> int | float:


class Calc:
def __init__(self, expr: str, timeout: int | float = 0.1):
def __init__(
self,
expr: str,
calc_timeout: int | float = 0.1,
str_repr_timeout: int | float = 0.2,
):
self.expr = expr
self.timeout = timeout
self.calc_timeout = calc_timeout
self.str_repr_timeout = str_repr_timeout

@cached_property
def parsed(self):
Expand Down Expand Up @@ -91,35 +97,29 @@ def answer(self):
"""
process = multiprocessing.Process(target=evaluate, args=(self.postfix,))
process.start()
process.join(timeout=self.timeout)
process.join(timeout=self.calc_timeout)
if process.is_alive():
process.terminate()
raise TimeoutError(
f"Calculations of {self.strparenthesized!r} took longer than {self.timeout} seconds",
f"Calculations of {self.strparenthesized!r} took longer than {self.calc_timeout} seconds",
) from None
return evaluate(self.postfix)

@cached_property
def stranswer(self) -> str:
process = multiprocessing.Process(target=str, args=(self.answer,))
process.start()
process.join(self.timeout * 2)
process.join(self.str_repr_timeout)
if process.is_alive():
process.terminate()
raise TimeoutError(
(
f"Generating a string representation of {self.strparenthesized!r} "
f"took longer than {self.timeout * 2} seconds"
),
f"Generating a string representation of {self.strparenthesized!r} took longer than {self.str_repr_timeout} seconds",
) from None
try:
return str(self.answer)
except ValueError:
raise TimeoutError(
(
f"Generating a string representation of {self.strparenthesized!r} "
f"took longer than {self.timeout * 2} seconds"
),
f"Generating a string representation of {self.strparenthesized!r} took longer than {self.str_repr_timeout} seconds",
) from None

def __repr__(self):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "postfixcalc"
version = "0.7.2"
version = "0.8.0"
description = "the stupid postfix evaluator"
authors = ["Mahdi Haghverdi <mahdihaghverdiliewpl@gmail.com>"]
license = "Apache2.0"
Expand Down
18 changes: 16 additions & 2 deletions tests/test_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,24 @@ def test_termination():
print(c.answer)

assert (
f"Calculations of {c.strparenthesized!r} took longer than {c.timeout} seconds"
f"Calculations of {c.strparenthesized!r} took longer than {c.calc_timeout} seconds"
== f"{e.value}"
)

c = Calc("(2 ^ 32) ^ (2 ^ 15)")
with pytest.raises(expected_exception=(TimeoutError, ValueError)): # noqa
with pytest.raises(expected_exception=(TimeoutError, ValueError)) as e: # noqa
print(c.stranswer)

assert (
f"Generating a string representation of {c.strparenthesized!r} "
f"took longer than {c.str_repr_timeout} seconds"
) == f"{e.value}"

c = Calc("(2 ^ 32) ^ (2 ^ 18)", calc_timeout=2, str_repr_timeout=4)
with pytest.raises(expected_exception=(TimeoutError, ValueError)) as e: # noqa
print(c.stranswer)

assert (
f"Generating a string representation of {c.strparenthesized!r} "
f"took longer than {c.str_repr_timeout} seconds"
) == f"{e.value}"

0 comments on commit 4250564

Please sign in to comment.