From 2cfb27cc03ac336df28e5c12ed148310bcd8c357 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 2 Dec 2020 05:54:01 +0000 Subject: [PATCH] equation.Expression.py v 0.0.1-alpha added corresponding .pyc to .gitignore --- .gitignore | 4 +- .../tra_analysis/equation/Expression.py | 68 +++++++++++++++++++ .../tra_analysis/equation/__init__.py | 1 + 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 analysis-master/tra_analysis/equation/Expression.py create mode 100644 analysis-master/tra_analysis/equation/__init__.py diff --git a/.gitignore b/.gitignore index 2545560b..0c66ec45 100644 --- a/.gitignore +++ b/.gitignore @@ -38,4 +38,6 @@ analysis-master/tra_analysis/.ipynb_checkpoints .pytest_cache analysis-master/tra_analysis/metrics/__pycache__ analysis-master/dist -data-analysis/config/ \ No newline at end of file +data-analysis/config/ +analysis-master/tra_analysis/equation/__pycache__/Expression.cpython-38.pyc +analysis-master/tra_analysis/equation/__pycache__/__init__.cpython-38.pyc diff --git a/analysis-master/tra_analysis/equation/Expression.py b/analysis-master/tra_analysis/equation/Expression.py new file mode 100644 index 00000000..df2f73ff --- /dev/null +++ b/analysis-master/tra_analysis/equation/Expression.py @@ -0,0 +1,68 @@ +# Titan Robotics Team 2022: Expression submodule +# Written by Arthur Lu +# Notes: +# this should be imported as a python module using 'from tra_analysis import equation' +# setup: + +__version__ = "0.0.1-alpha" + +__changelog__ = """changelog: + 0.0.1-alpha: + - took items from equation.ipynb and ported here +""" + +__author__ = ( + "Arthur Lu ", +) + +import re +from decimal import Decimal +from functools import reduce + +class Expression: + + def __init__(self, string): + + self.string = string + + def add(self, string): + while(len(re.findall("[+]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[+]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x + y), [Decimal(i) for i in re.split("[+]{1}", re.search("[-]?\d+[.]?\d*[+]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def sub(self, string): + while(len(re.findall("\d+[.]?\d*[-]{1,2}\d+[.]?\d*", string)) != 0): + g = re.search("\d+[.]?\d*[-]{1,2}\d+[.]?\d*", string).group() + if(re.search("[-]{1,2}", g).group() == "-"): + r = re.sub("[-]{1}", "+-", g, 1) + string = re.sub(g, r, string, 1) + elif(re.search("[-]{1,2}", g).group() == "--"): + r = re.sub("[-]{2}", "+", g, 1) + string = re.sub(g, r, string, 1) + else: + pass + return string + + def mul(self, string): + while(len(re.findall("[*]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[*]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x * y), [Decimal(i) for i in re.split("[*]{1}", re.search("[-]?\d+[.]?\d*[*]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def div(self, string): + while(len(re.findall("[/]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[/]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x / y), [Decimal(i) for i in re.split("[/]{1}", re.search("[-]?\d+[.]?\d*[/]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def exp(self, string): + while(len(re.findall("[\^]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[\^]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x ** y), [Decimal(i) for i in re.split("[\^]{1}", re.search("[-]?\d+[.]?\d*[\^]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def evaluate(self): + string = self.string + string = self.exp(string) + string = self.div(string) + string = self.mul(string) + string = self.sub(string) + string = self.add(string) + return string \ No newline at end of file diff --git a/analysis-master/tra_analysis/equation/__init__.py b/analysis-master/tra_analysis/equation/__init__.py new file mode 100644 index 00000000..5948e0da --- /dev/null +++ b/analysis-master/tra_analysis/equation/__init__.py @@ -0,0 +1 @@ +from .Expression import Expression \ No newline at end of file