From 44619a2025bbc4aab16aec23d618b5cafb55b2ce Mon Sep 17 00:00:00 2001 From: barteg Date: Mon, 13 Oct 2025 14:45:27 +0200 Subject: [PATCH 1/5] testing lukasz skills --- feature1/lukasz.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 feature1/lukasz.txt diff --git a/feature1/lukasz.txt b/feature1/lukasz.txt new file mode 100644 index 0000000..e69de29 From 2bd019bcfca73641b5895ffade58a73e9b5176e3 Mon Sep 17 00:00:00 2001 From: LuckyLuke255 <105583948+LuckyLuke255@users.noreply.github.com> Date: Mon, 13 Oct 2025 14:48:34 +0200 Subject: [PATCH 2/5] Update lukasz.txt --- feature1/lukasz.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/feature1/lukasz.txt b/feature1/lukasz.txt index e69de29..f91c576 100644 --- a/feature1/lukasz.txt +++ b/feature1/lukasz.txt @@ -0,0 +1 @@ +good From 9e4180cefeaacf37d917b16a834eef8cbb5ec205 Mon Sep 17 00:00:00 2001 From: LuckyLuke255 <105583948+LuckyLuke255@users.noreply.github.com> Date: Mon, 13 Oct 2025 14:48:56 +0200 Subject: [PATCH 3/5] Update lukasz.txt From 343be83806fd5a1b01c5cfc9e6a5b9f3c94f08e5 Mon Sep 17 00:00:00 2001 From: LuckyLuke255 <105583948+LuckyLuke255@users.noreply.github.com> Date: Mon, 20 Oct 2025 13:42:47 +0200 Subject: [PATCH 4/5] Create project.py --- feature1/project.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 feature1/project.py diff --git a/feature1/project.py b/feature1/project.py new file mode 100644 index 0000000..610a89e --- /dev/null +++ b/feature1/project.py @@ -0,0 +1,58 @@ +import ast, operator, math, sys + +_ops = { + ast.Add: operator.add, + ast.Sub: operator.sub, + ast.Mult: operator.mul, + ast.Div: operator.truediv, + ast.FloorDiv: operator.floordiv, + ast.Mod: operator.mod, + ast.Pow: operator.pow, + ast.USub: operator.neg, + ast.UAdd: operator.pos, +} + +_funcs = {k: getattr(math, k) for k in ( + "sin","cos","tan","asin","acos","atan","sqrt","log","log10","exp", + "fabs","factorial","degrees","radians","ceil","floor","gamma","lgamma" +)} +_consts = {"pi": math.pi, "e": math.e, "tau": math.tau, "inf": math.inf} + +def _eval(node): + if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)): + return node.value + if isinstance(node, ast.Num): + return node.n + if isinstance(node, ast.UnaryOp) and type(node.op) in _ops: + return _ops[type(node.op)](_eval(node.operand)) + if isinstance(node, ast.BinOp) and type(node.op) in _ops: + return _ops[type(node.op)](_eval(node.left), _eval(node.right)) + if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.keywords == []: + fname = node.func.id + if fname in _funcs: + return _funcs[fname](*[_eval(a) for a in node.args]) + if isinstance(node, ast.Name) and node.id in _consts: + return _consts[node.id] + raise ValueError("invalid expression") + +def calculate(expr): + tree = ast.parse(expr, mode="eval") + return _eval(tree.body) + +def main(): + if len(sys.argv) > 1: + print(calculate(" ".join(sys.argv[1:]))) + return + while True: + try: + s = input("> ").strip() + if s.lower() in {"exit","quit"}: + break + if not s: + continue + print(calculate(s)) + except Exception as e: + print(f"Error: {e}") + +if __name__ == "__main__": + main() From f06d6763d141b53f591ed7bdd57b184e63334470 Mon Sep 17 00:00:00 2001 From: barteg Date: Mon, 20 Oct 2025 13:53:35 +0200 Subject: [PATCH 5/5] feat: Add calculator mode selection to project.py --- feature1/project.py | 56 ++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/feature1/project.py b/feature1/project.py index 610a89e..8cb9183 100644 --- a/feature1/project.py +++ b/feature1/project.py @@ -1,6 +1,6 @@ import ast, operator, math, sys -_ops = { +_SCIENTIFIC_OPS = { ast.Add: operator.add, ast.Sub: operator.sub, ast.Mult: operator.mul, @@ -12,36 +12,60 @@ ast.UAdd: operator.pos, } -_funcs = {k: getattr(math, k) for k in ( +_SCIENTIFIC_FUNCS = {k: getattr(math, k) for k in ( "sin","cos","tan","asin","acos","atan","sqrt","log","log10","exp", "fabs","factorial","degrees","radians","ceil","floor","gamma","lgamma" )} -_consts = {"pi": math.pi, "e": math.e, "tau": math.tau, "inf": math.inf} +_SCIENTIFIC_CONSTS = {"pi": math.pi, "e": math.e, "tau": math.tau, "inf": math.inf} -def _eval(node): +_SIMPLE_OPS = { + ast.Add: operator.add, + ast.Sub: operator.sub, + ast.Mult: operator.mul, + ast.Div: operator.truediv, +} +_SIMPLE_FUNCS = {} +_SIMPLE_CONSTS = {} + +def _eval(node, ops, funcs, consts): if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)): return node.value if isinstance(node, ast.Num): return node.n - if isinstance(node, ast.UnaryOp) and type(node.op) in _ops: - return _ops[type(node.op)](_eval(node.operand)) - if isinstance(node, ast.BinOp) and type(node.op) in _ops: - return _ops[type(node.op)](_eval(node.left), _eval(node.right)) + if isinstance(node, ast.UnaryOp) and type(node.op) in ops: + return ops[type(node.op)](_eval(node.operand, ops, funcs, consts)) + if isinstance(node, ast.BinOp) and type(node.op) in ops: + return ops[type(node.op)](_eval(node.left, ops, funcs, consts), _eval(node.right, ops, funcs, consts)) if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.keywords == []: fname = node.func.id - if fname in _funcs: - return _funcs[fname](*[_eval(a) for a in node.args]) - if isinstance(node, ast.Name) and node.id in _consts: - return _consts[node.id] + if fname in funcs: + return funcs[fname](*[_eval(a, ops, funcs, consts) for a in node.args]) + if isinstance(node, ast.Name) and node.id in consts: + return consts[node.id] raise ValueError("invalid expression") -def calculate(expr): +def calculate(expr, ops, funcs, consts): tree = ast.parse(expr, mode="eval") - return _eval(tree.body) + return _eval(tree.body, ops, funcs, consts) def main(): + mode = input("Choose calculator mode (simple/scientific): ").lower() + if mode == "simple": + current_ops = _SIMPLE_OPS + current_funcs = _SIMPLE_FUNCS + current_consts = _SIMPLE_CONSTS + elif mode == "scientific": + current_ops = _SCIENTIFIC_OPS + current_funcs = _SCIENTIFIC_FUNCS + current_consts = _SCIENTIFIC_CONSTS + else: + print("Invalid mode. Defaulting to scientific.") + current_ops = _SCIENTIFIC_OPS + current_funcs = _SCIENTIFIC_FUNCS + current_consts = _SCIENTIFIC_CONSTS + if len(sys.argv) > 1: - print(calculate(" ".join(sys.argv[1:]))) + print(calculate(" ".join(sys.argv[1:]), current_ops, current_funcs, current_consts)) return while True: try: @@ -50,7 +74,7 @@ def main(): break if not s: continue - print(calculate(s)) + print(calculate(s, current_ops, current_funcs, current_consts)) except Exception as e: print(f"Error: {e}")