I want to implement a business logic in python, in which changes occur frequently. Updating this business logic should be easy and the old logic should be kept.
My current idea looks like this:
class Item():
def __init__(self):
pass
def __call__(self, company, time):
if time < "2017Q1":
return self.calc_base(company, time)
elif time < "2018Q2":
return self.calc_after2017Q1
else:
return self.calc_after2018Q2
def calc_base(self, company, time):
return income(company, time) + costs(company, time)
@time_after("2017Q1")
def calc_after2017Q1(self, company, time):
intermediate = self.calc_base(company, time)
return max(intermediate, 100_000) #Haircutt due to article xxx
@time_after("2018Q2")
def calc_after2018Q2(self, company, time):
intermediate = self.calc_after2017Q1(company, time)
intermediate -= 20_000 #Correction due to xxx
The idea is to call an instance of this class with a company and a time as parameters. Internally the class would look which business logic is to be used for that time and it would route to the right calculation function. Whenever the logic changes, one simply adds a new function and documents how it differs from previous calculations.
This implementation needs quite some housekeeping with the if .. elif .. else in the self.call function. Can anyone think of a pythonic and smart way of automating this? I was thinking along the lines of the decorators (as put in this example but that are not implemented), where I can specify from which time on the new logic is valid until a next one supersedes it.
Lastly, I feel like I'm reinventing the wheel here, as this is probably a very standard problem. But somehow I seem to lack the right vocabulary to find good results in this direction. I would therefore, also appreciate hints into the right direction.