mirror of
https://github.com/Wan-Video/Wan2.1.git
synced 2025-11-04 22:26:36 +00:00
feat: Add command-line calculator tool
This change adds a new command-line calculator tool. The tool can perform basic arithmetic operations (add, subtract, multiply, divide) and is invoked from the command line. It includes a full suite of unit tests.
This commit is contained in:
parent
7c81b2f27d
commit
608d98e833
37
calculator.py
Normal file
37
calculator.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import argparse
|
||||||
|
|
||||||
|
def add(x, y):
|
||||||
|
"""Adds two numbers together."""
|
||||||
|
return x + y
|
||||||
|
|
||||||
|
def subtract(x, y):
|
||||||
|
"""Subtracts two numbers."""
|
||||||
|
return x - y
|
||||||
|
|
||||||
|
def multiply(x, y):
|
||||||
|
"""Multiplies two numbers."""
|
||||||
|
return x * y
|
||||||
|
|
||||||
|
def divide(x, y):
|
||||||
|
"""Divides two numbers."""
|
||||||
|
if y == 0:
|
||||||
|
raise ValueError("Cannot divide by zero.")
|
||||||
|
return x / y
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="A simple command-line calculator.")
|
||||||
|
parser.add_argument("x", type=float, help="The first number.")
|
||||||
|
parser.add_argument("operation", choices=["+", "-", "*", "/"], help="The operation to perform.")
|
||||||
|
parser.add_argument("y", type=float, help="The second number.")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.operation == "+":
|
||||||
|
result = add(args.x, args.y)
|
||||||
|
elif args.operation == "-":
|
||||||
|
result = subtract(args.x, args.y)
|
||||||
|
elif args.operation == "*":
|
||||||
|
result = multiply(args.x, args.y)
|
||||||
|
elif args.operation == "/":
|
||||||
|
result = divide(args.x, args.y)
|
||||||
|
|
||||||
|
print(f"Result: {result}")
|
||||||
29
test_calculator.py
Normal file
29
test_calculator.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import unittest
|
||||||
|
from calculator import add, subtract, multiply, divide
|
||||||
|
|
||||||
|
class TestCalculator(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_add(self):
|
||||||
|
self.assertEqual(add(2, 3), 5)
|
||||||
|
self.assertEqual(add(-1, 1), 0)
|
||||||
|
self.assertEqual(add(-1, -1), -2)
|
||||||
|
|
||||||
|
def test_subtract(self):
|
||||||
|
self.assertEqual(subtract(3, 2), 1)
|
||||||
|
self.assertEqual(subtract(-1, 1), -2)
|
||||||
|
self.assertEqual(subtract(-1, -1), 0)
|
||||||
|
|
||||||
|
def test_multiply(self):
|
||||||
|
self.assertEqual(multiply(2, 3), 6)
|
||||||
|
self.assertEqual(multiply(-1, 1), -1)
|
||||||
|
self.assertEqual(multiply(-1, -1), 1)
|
||||||
|
|
||||||
|
def test_divide(self):
|
||||||
|
self.assertEqual(divide(6, 3), 2)
|
||||||
|
self.assertEqual(divide(-1, 1), -1)
|
||||||
|
self.assertEqual(divide(-1, -1), 1)
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
divide(1, 0)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Loading…
Reference in New Issue
Block a user