如果速率随时间变化,则必须使用时间相关函数进行建模。
这个功能如何确定给定的时间点或时间范围右边的数值是下一个步骤:
- 如果你硬编码值到程序,你必须每次更改后更改程序。
- 如果将其放入数据库中,则会有一个额外的数据库查询,但可以在“实施”中对其进行更改。
- 您还可以 - 在广泛的程序中 - 让程序从中央服务器加载正确的值并将其存储在某个地方。
根据您所使用的解决方案,你可以做
def calculate_tax_for_year(self, income, year):
return _calculate_tax_for_year(self, income, tax_for_year(year)):
def tax_for_year(year): # solution 1
if year < 2010: return .2
if year < 2013: return .18
# etc.
def tax_for_year(year): # solution 2
with dbconn as cursor:
cursor.execute("SELECT rate FROM taxrate WHERE year == %s", year)
return cursor.fetch_one_row() # don't remember the syntax; something like this...
# solution 3:
def tax_for_year(year):
# just describing the steps:
with open("path/to/taxfile", "r") as f:
taxdata = f.read()
# parse table file, e. g. CSV
# find the right entry
return entry_matching_for_given_year
def update_tax_file():
import urllib
conn = urllib.urlopen("http://my.taxfile/url.csv")
# check if the file has changed since last check
# if so:
data = conn.read()
with open("path/to/taxfile", "w") as f:
f.write(data)
您能详细介绍一下代码示例(2,3)吗? – Dominik
@Dominik我添加了一些代码示例。 – glglgl