Skip to content

Instantly share code, notes, and snippets.

@HYChou0515
Last active November 25, 2023 15:02
Show Gist options
  • Save HYChou0515/2fbed4f4aa9f57a9344ffbeb2326b121 to your computer and use it in GitHub Desktop.
Save HYChou0515/2fbed4f4aa9f57a9344ffbeb2326b121 to your computer and use it in GitHub Desktop.
from artest import autoreg, automock
@autoreg("adder")
def adder(a, b):
return a + b
# @autoreg("summation")
# def summation(args):
# # a complex way to implement summation
# if len(args) == 1:
# return args[0]
# s_left = summation(args[:len(args) // 2])
# s_right = summation(args[len(args) // 2:])
# s = adder(s_left, s_right)
# return s
@autoreg("summation")
def summation(args):
s = 0
for arg in args:
s = adder(s, arg)
return s
@autoreg("summation")
def summation(args):
s = 0
for arg in args:
s = adder(s, arg)
return s
# # Before Modification
# db = [
# {"date": "2020-01-05", "price": 29},
# {"date": "2020-01-06", "price": 50},
# {"date": "2020-01-07", "price": 12},
# {"date": "2020-01-08", "price": 53},
# {"date": "2020-01-09", "price": 24},
# {"date": "2020-01-10", "price": 15},
# {"date": "2020-01-11", "price": 5},
# {"date": "2020-01-12", "price": 10},
# ]
# After Modification
db = [
{"date": "2020-01-05", "price": 129},
{"date": "2020-01-06", "price": 150},
{"date": "2020-01-07", "price": 112},
{"date": "2020-01-08", "price": 153},
{"date": "2020-01-09", "price": 124},
{"date": "2020-01-10", "price": 115},
{"date": "2020-01-11", "price": 105},
{"date": "2020-01-12", "price": 110},
]
@automock("select_db")
def select_db(start_date, end_date):
rows = []
for row in db:
if start_date <= row["date"] <= end_date:
rows.append(row)
return rows
@autoreg("total_costs")
def total_costs(start_date, end_date):
rows = select_db(start_date, end_date)
costs = [row["price"] for row in rows]
return summation(costs)
if __name__ == "__main__":
print(total_costs("2020-01-06", "2020-01-10"))
print(total_costs("2020-01-10", "2020-01-12"))
print(total_costs("2020-01-05", "2020-01-07"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment