From 19d5db03c58b0d5d1dba610eff7014e6b68e0c91 Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 15:16:38 -0400 Subject: [PATCH 1/8] Add files via upload --- utils/metrics.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 utils/metrics.py diff --git a/utils/metrics.py b/utils/metrics.py new file mode 100644 index 0000000..070f18c --- /dev/null +++ b/utils/metrics.py @@ -0,0 +1,37 @@ +from typing import override + +from parser.ast import VarExpr, NotExpr, ParenExpr, AndExpr, OrExpr +from parser.visitor import Visitor + + +class OpCounter(Visitor): + + def __init__(self): + self._count = 0 + + @override + def visitVarExpr(self, vex: VarExpr) -> None: + OpCounter._count += 1 + + @override + def visitNotExpr(self, nex: NotExpr) -> None: + OpCounter._count += 1 + + @override + def visitParenExpr(self, pex: ParenExpr) -> None: + OpCounter._count += 1 + + @override + def visitAndExpr(self, aex: AndExpr) -> None: + OpCounter._count += 1 + + @override + def visitOrExpr(self, oex: OrExpr) -> None: + OpCounter._count += 1 + + @override + def visitVar(self, _) -> None: + OpCounter._count += 1 + + def get_count(self): + return self._count From 3289fea612dd0353709982d5c40a425e48077efd Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 15:22:46 -0400 Subject: [PATCH 2/8] fix: updated type definitions --- utils/metrics.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utils/metrics.py b/utils/metrics.py index 070f18c..a64d5dc 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -3,10 +3,9 @@ from parser.ast import VarExpr, NotExpr, ParenExpr, AndExpr, OrExpr from parser.visitor import Visitor - class OpCounter(Visitor): - def __init__(self): + def __init__(self) -> None: self._count = 0 @override @@ -33,5 +32,5 @@ def visitOrExpr(self, oex: OrExpr) -> None: def visitVar(self, _) -> None: OpCounter._count += 1 - def get_count(self): + def getCount(self) -> None: return self._count From 9c4b8847f3c6a0a753a072a002bf529552173470 Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 15:29:24 -0400 Subject: [PATCH 3/8] fix: update type definitions --- utils/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/metrics.py b/utils/metrics.py index a64d5dc..5ac2f57 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -6,7 +6,7 @@ class OpCounter(Visitor): def __init__(self) -> None: - self._count = 0 + self._count: int = 0 @override def visitVarExpr(self, vex: VarExpr) -> None: From b114050169267e0478d48152ff38cc3de49c1b14 Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 16:43:15 -0400 Subject: [PATCH 4/8] fix: counter now counts correctly --- utils/metrics.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/utils/metrics.py b/utils/metrics.py index 5ac2f57..a493558 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -3,34 +3,45 @@ from parser.ast import VarExpr, NotExpr, ParenExpr, AndExpr, OrExpr from parser.visitor import Visitor + class OpCounter(Visitor): - def __init__(self) -> None: + _count = 0 + + def __init__(self): self._count: int = 0 @override def visitVarExpr(self, vex: VarExpr) -> None: - OpCounter._count += 1 + vex.first.accept(self) + if vex.second: + vex.second.accept(self) @override def visitNotExpr(self, nex: NotExpr) -> None: - OpCounter._count += 1 + self.updateCount() + nex.first.accept(self) @override def visitParenExpr(self, pex: ParenExpr) -> None: - OpCounter._count += 1 + pex.first.accept(self) @override def visitAndExpr(self, aex: AndExpr) -> None: - OpCounter._count += 1 + self.updateCount() + aex.first.accept(self) @override def visitOrExpr(self, oex: OrExpr) -> None: - OpCounter._count += 1 + self.updateCount() + oex.first.accept(self) @override def visitVar(self, _) -> None: - OpCounter._count += 1 + pass + + def updateCount(self) -> None: + self._count += 1 def getCount(self) -> None: return self._count From 0e5af8f23d277009be0f8969523a4bf1efea609c Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 16:44:52 -0400 Subject: [PATCH 5/8] feat: added operation counter test case --- tests/test_metrics.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/test_metrics.py diff --git a/tests/test_metrics.py b/tests/test_metrics.py new file mode 100644 index 0000000..4cfd7ff --- /dev/null +++ b/tests/test_metrics.py @@ -0,0 +1,18 @@ +from typing import override +from unittest import TestCase + +from parser.parse import Parser +from utils.metrics import OpCounter + + +class TestCount(TestCase): + def test_counter(self) -> None: + p: Parser = Parser() + tree = p.parse(["!", "(", "A", "&", "!", "B", "|", "C", ")", ""]) + counter = OpCounter() + + self.assertEqual(counter.getCount(), 0) + + tree.accept(counter) + + self.assertEqual(counter.getCount(), 4) From 2f5543212151e7bcbd80a0a123a90ed93df0c82b Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 18:51:48 -0400 Subject: [PATCH 6/8] fix: simplified code and added doc string --- utils/metrics.py | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/utils/metrics.py b/utils/metrics.py index a493558..121d325 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -5,43 +5,24 @@ class OpCounter(Visitor): + """Counts the number of boolean operators visited""" - _count = 0 - - def __init__(self): - self._count: int = 0 - - @override - def visitVarExpr(self, vex: VarExpr) -> None: - vex.first.accept(self) - if vex.second: - vex.second.accept(self) + _count: int = 0 @override def visitNotExpr(self, nex: NotExpr) -> None: - self.updateCount() + OpCounter._count += 1 nex.first.accept(self) - @override - def visitParenExpr(self, pex: ParenExpr) -> None: - pex.first.accept(self) - @override def visitAndExpr(self, aex: AndExpr) -> None: - self.updateCount() + OpCounter._count += 1 aex.first.accept(self) @override def visitOrExpr(self, oex: OrExpr) -> None: - self.updateCount() + OpCounter._count += 1 oex.first.accept(self) - @override - def visitVar(self, _) -> None: - pass - - def updateCount(self) -> None: - self._count += 1 - def getCount(self) -> None: return self._count From 8c6029527e7b6753030477716ca64be105f47053 Mon Sep 17 00:00:00 2001 From: micah-0w0 Date: Thu, 18 Jul 2024 18:53:32 -0400 Subject: [PATCH 7/8] fix: added type definition --- tests/test_metrics.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 4cfd7ff..e5302ef 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -1,6 +1,6 @@ -from typing import override from unittest import TestCase +from parser.ast import Expr from parser.parse import Parser from utils.metrics import OpCounter @@ -8,9 +8,8 @@ class TestCount(TestCase): def test_counter(self) -> None: p: Parser = Parser() - tree = p.parse(["!", "(", "A", "&", "!", "B", "|", "C", ")", ""]) - counter = OpCounter() - + tree: Expr = p.parse(["!", "(", "A", "&", "!", "B", "|", "C", ")", ""]) + counter: OpCounter = OpCounter() self.assertEqual(counter.getCount(), 0) tree.accept(counter) From a967a57b3d50efa512d4029c83c13784eab18848 Mon Sep 17 00:00:00 2001 From: Benson Liu Date: Thu, 18 Jul 2024 17:47:24 -0700 Subject: [PATCH 8/8] Update metrics.py --- utils/metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/metrics.py b/utils/metrics.py index 121d325..31eecc8 100644 --- a/utils/metrics.py +++ b/utils/metrics.py @@ -24,5 +24,5 @@ def visitOrExpr(self, oex: OrExpr) -> None: OpCounter._count += 1 oex.first.accept(self) - def getCount(self) -> None: + def getCount(self) -> int: return self._count