From b66f4a208854820d411ec82a115bb6d7d3e12c8b Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Wed, 13 Dec 2023 10:13:55 +0100 Subject: [PATCH] * and ** demos. --- args.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 args.py diff --git a/args.py b/args.py new file mode 100644 index 0000000..760c933 --- /dev/null +++ b/args.py @@ -0,0 +1,49 @@ +from dataclasses import dataclass +import json +import math + + +def mul(init, *args): + """Multiply all given arguments. + + >>> mul(3, 3) + 9 + >>> mul(3, 3, 3) + 27 + """ + total = init + for arg in args: + total *= arg # total = total * arg + return total + + +@dataclass +class Report: + date: str + current: int + last: int + total: int + + +api_result = """ +{ + "date": "2023-12-13", + "current": 123, + "last": 118, + "total": 12 +} +""" + + +api_data = json.loads(api_result) +report = Report(**api_data) +print(report) +print("Difference:", abs(report.last - report.current)) + + +def test_mul(): + assert mul() == 1 + assert mul(3) == 3 + assert mul(2, 3) == 6 + assert mul(2, 3, 2) == 12 + assert mul(123456, 654321, 12) == mul(654321, 12, 123456)