gpt4 book ai didi

python - 在 python 中从文件动态更改值

转载 作者:行者123 更新时间:2023-12-01 05:39:10 26 4
gpt4 key购买 nike

我有一个处理各种值的类,还有一个从外部文件读取值的类。我需要另一个将值类的值替换为从文件中获取的值。有办法做到这一点吗?

我想我可以在这里提交我的代码,但我认为这不会有太大帮助,因为它是用我的母语编写的,并且涉及很多你不熟悉的参数。

您看到的大数值是我需要更改的值。

class Properties:
'''Calculates the amount of insurance and/or remedies for a person
when given a certain set of parameters.
'''
def input(self, kt, aldur, heimilisfang, busetutimi, maki, born, sjomennska,
tekjur, ororkuhlutfall, ororkualdur, frestun_ellilifeyris,
byr_einn, bifreidarstyrkur):
self.kt = kt
self.aldur = aldur
self.heimilisfang = heimilisfang
self.maki = maki
self.born = born
self.busetutimi = busetutimi
self.sjomennska = sjomennska
self.tekjur = tekjur
self.ororkuhlutfall = ororkuhlutfall
self.ororkualdur = ororkualdur
self.frestun_ellilifeyris = frestun_ellilifeyris
self.byr_einn = byr_einn
self.bifreidarstyrkur = bifreidarstyrkur

#Ororkulifeyrir reiknadur.

baetur = 0
ororkustyrkur = 25175
ororkustyrkur_62 = 34053
ororkulifeyrir = 408636/12
ellilifeyrir = 408636/12 #Deilt med 12 thvi thetta a ad vera manadarlega.

if aldur < 67 and ororkuhlutfall >= 75 and not sjomennska: #Reiknar ororku.
baetur = baetur + ororkulifeyrir
print "-1 Ororkulifeyrir. Baeturnar eru nu samtals %s." % baetur

if 18 < aldur < 62 and 50 <= ororkuhlutfall < 75 and tekjur < 2575220 and aldur < 67:
baetur = baetur + ororkustyrkur
print "-1 Ororkustyrkur. Baeturnar eru nu samtals %s." % baetur
elif 18 < aldur < 62 and 50 <= ororkuhlutfall< 75 and 2575220 < tekjur < 4209764/12:
baetur = baetur + ororkustykur*0.75
print "-1 Skertur ororkustyrkur. Baeturnar eru nu samtals %s." % baetur
else:
print "-1 Enginn ororkustyrkur."

if 62 <= aldur and 50 <= ororkuhlutfall < 75 and not sjomennska:
baetur = baetur + ororkustyrkur_62
print "-1 Ororkustyrkur 62 ara og eldri. Baeturnar eru nu samtals %s." % baetur

#Ellilifeyrir, tekjutrygging, barnalifeyrir og aldurstengd ororkuuppbot reiknud.

if sjomennska:
print "Sjomennska valin."
if busetutimi < 40 and aldur >= 60:
ellilifeyrir = ellilifeyrir * busetutimi/40
print "0 Busetuskertur ellilifeyrir."

if 75 <= ororkuhlutfall:
if 2575220/12 < tekjur < 4209764/12 and aldur >= 67:
ellilifeyrir = ellilifeyrir * 0.75 #Ellilifeyrir skerdist um 25%
baetur = baetur + ellilifeyrir
print "1 Skertur ellilifeyrir. Baeturnar eru nu samtals %s kr." % baetur
elif tekjur < 2575220/12 and aldur >= 67:
baetur = baetur + ellilifeyrir
print "1 Oskertur ellilifeyrir. Baeturnar eru nu samtals %s kr." % baetur
else:
print "1 Enginn ellilifeyrir."

if aldur >= 67 and tekjur > 1315200/12:
tekjutrygging = 1289528/12 #Tekjutrygging manadarlega
baetur = baetur + tekjutrygging * (1-0.3835)
print "2 Skert tekjutrygging fyrir ellilifeyristhega. Baetur eru nu samtals %s kr." % baetur
elif aldur >= 67 and not tekjur > 2575220/12:
tekjutrygging = 1289528/12
baetur = baetur + tekjutrygging
print "2 Oskert tekjutrygging fyrir ellilifeyristhega. Baetur eru nu samtals %s kr." % baetur
else:
print "2 Engin tekjutrygging fyrir ellilifeyristhega."

# if skipunin her ad ofan er naudsynleg thvi utreikningurinn er mismunandi eftir thvi hvort
# sjomadurinn er fullur oryrki eda ekki. Ef hann er fullur oryrki er gert rad fyrir ad hann vilji
# vera oryrki til 67 ara aldurs thvi baetur oryrkja eru haerri en baetur ellilifeyristhega.

else:
if 2575220/12 < tekjur < 4209764/12 and aldur >= 60:
ellilifeyrir = ellilifeyrir * 0.75 #Ellilifeyrir skerdist um 25%
baetur = baetur + ellilifeyrir
print "1 Skertur ellilifeyrir. Baeturnar eru nu samtals %s kr." % baetur
elif tekjur < 2575220/12 and aldur >= 60:
baetur = baetur + ellilifeyrir
print "1 Oskertur ellilifeyrir. Baeturnar eru nu samtals %s kr." % baetur
else:
print "1 Enginn ellilifeyrir."

if aldur >= 60 and tekjur > 1315200/12:
tekjutrygging = 1289528/12 #Tekjutrygging manadarlega
baetur = baetur + tekjutrygging * (1-0.3835)
print "2 Skert tekjutrygging fyrir ellilifeyristhega. Baetur eru nu samtals %s kr." % baetur
elif aldur >= 60 and not tekjur > 2575220/12:
tekjutrygging = 1289528/12
baetur = baetur + tekjutrygging

print "2 Oskert tekjutrygging fyrir ellilifeyristhega. Baetur eru nu samtals %s kr." % baetur
else:
print "2 Engin tekjutrygging fyrir ellilifeyristhega."

if 75 <= ororkuhlutfall and aldur <= 67 and tekjur < 2575220/12:
tekjutrygging = 1308604/12
baetur = baetur + tekjutrygging
print "3 Oskert tekjutrygging fyrir oryrkja. Baetur eru nu samtals %s kr." % baetur
elif 75 <= ororkuhlutfall and aldur <= 67 and 2575220/12 < tekjur < 4209764/12:
tekjutrygging = 1308604/12
baetur = baetur + tekjutrygging * (1-0.3835)
print "3 Skert tekjutrygging fyrir oryrkja. Baetur eru nu samtals %s kr." % baetur
else:
print "3 Engin tekjutrygging fyrir oryrkja."

if 75 <= ororkuhlutfall and aldur <= 67 and born > 0:
barnalifeyrir = 25175
baetur = baetur + barnalifeyrir * born
print "4 Barnalifeyrir oryrkja. Baetur eru nu samtals %s kr." % baetur
else:
print "4 Enginn barnalifeyrir oryrkja."

# Naudsynlegt er ad adili bui einn til ad eiga rett a heimilisuppbot.
# Heimilisuppbot er skert likt og tekjutrygging.

frestunartugabrot = frestun_ellilifeyris / 200

# Frestunartugabrotid er notad i linunni her a eftir. Deilt er med 2 thvi baeturnar eru haekkadar
# um 0,5% fyrir hvern frestunarmanud. Deilt er med 100 til ad fa ut tugabrot.

baetur = baetur + baetur * frestunartugabrot

# Baetur eru haekkadar i hlutfalli vid frestunartugabrot og thar med fjolda frestunarmanada.

# Frestun ellilifeyris. Gert er rad fyrir ad allir sjomenn vilji 1) Ororkulifeyri til 67 ara aldurs,
# thvi ad baeturnar eru haerri hja oryrkjum en ellilifeyristhegum vegna aldurstengdrar ororkuuppbotar,
# og 2) Fresta ellilifeyri um 5 ar thvi their munu hafa ororkulifeyri fram til 67 ara aldurs.

for item in aldurstengd_ororka.keys():
for age in item:
if ororkualdur == age and aldur < 67 and 75 <= ororkuhlutfall:
baetur = baetur + ororkulifeyrir * aldurstengd_ororka[item]
print "5 Aldurstengd ororkuuppbot. Baetur eru nu samtals %s kr." % baetur
print "5.1 Reiknast sem %s prosent af ororkulifeyri." % aldurstengd_ororka[item]


else:
print "Sjomennska ekki valin."
if busetutimi < 40 and aldur >= 67:
ellilifeyrir = ellilifeyrir * busetutimi/40
print "0 Busetuskertur ellilifeyrir."
else:
print "0 Oskertur ellilifeyrir vegna busetu eda yngri en 67."

if 2056404/12 < tekjur < 4209764/12 and aldur >= 67: #Hamarkstekjur an skerdingar
ellilifeyrir = ellilifeyrir * 0.75 #Ellilifeyrir skerdist um 25%
baetur = baetur + ellilifeyrir
print "1 Skertur ellilifeyrir. Baeturnar eru nu samtals %s kr." % baetur
elif tekjur < 2056404/12 and aldur >= 67:
baetur = baetur + ellilifeyrir
print "1 Oskertur ellilifeyrir. Baeturnar eru nu samtals %s kr." % baetur
else:
print "1 Enginn ellilifeyrir."

if aldur >= 67 and tekjur < 2575220/12:
tekjutrygging = 1289528/12 #Tekjutrygging manadarlega
baetur = baetur + tekjutrygging
print "2 Oskert tekjutrygging fyrir ellilifeyristhega. Baeturnar eru nu samtals %s kr." % baetur
elif aldur >= 67 and 2575220/12 < tekjur < 4209764/12:
tekjutrygging = 1289528/12
baetur = baetur + tekjutrygging * (1-0.3835)
print "2 Skert tekjutrygging fyrir ellilifeyristhega. Baeturnar eru nu samtals %s kr." % baetur
else:
print "2 Engin tekjutrygging fyrir ellilifeyristhega."

if 75 <= ororkuhlutfall and aldur <= 67 and tekjur < 2575220/12:
tekjutrygging = 1308604/12
baetur = baetur + tekjutrygging
print "3 Oskert tekjutrygging fyrir oryrkja. Baetur eru nu samtals %s kr." % baetur
elif 75 <= ororkuhlutfall and aldur <= 67 and 2575220/12 < tekjur < 4209764/12:
tekjutrygging = 1308604/12
baetur = baetur + tekjutrygging * (1-0.3835)
print "3 Skert tekjutrygging fyrir oryrkja. Baetur eru nu samtals %s kr." % baetur
else:
print "3 Engin tekjutrygging fyrir oryrkja."

if 75 <= ororkuhlutfall and aldur <= 67 and born > 0:
barnalifeyrir = 302100/12
baetur = baetur + barnalifeyrir * born
print "4 Barnalifeyrir oryrkja. Baetur eru nu samtals %s kr." % baetur
else:
print "4 Enginn barnalifeyrir oryrkja."

# Naudsynlegt er ad adili bui einn til ad eiga rett a heimilisuppbot.

frestunartugabrot = frestun_ellilifeyris / 200

# Frestunartugabrotid er notad i linunni her a eftir. Deilt er med 2 thvi baeturnar eru haekkadar
# um 0,5% fyrir hvern frestunarmanud. Deilt er med 100 til ad fa ut tugabrot.

baetur = baetur + baetur * frestunartugabrot

# Baetur eru haekkadar i hlutfalli vid frestunartugabrot og thar med fjolda frestunarmanada.

for item in aldurstengd_ororka.keys():
for age in item:
if ororkualdur == age and aldur < 67 and 75 < ororkuhlutfall:
baetur = baetur + ororkulifeyrir * aldurstengd_ororka[item]
print "5 Aldurstengd ororkuuppbot. Baeturnar eru nu samtals %s kr." % baetur
print "5.1 Reiknast sem %s prosent af ororkulifeyri." % aldurstengd_ororka[item]

if byr_einn:
if tekjur < 2575220/12:
heimilisuppbot_var = 380028/12
baetur = baetur + heimilisuppbot_var
print "6 Oskert heimilisuppbot. Baetur eru nu samtals %s." % baetur
if 2575220/12 < tekjur < 4209764/12:
heimilisuppbot_varar = (3800289/12) * (1-0.3835)
baetur = baetur + heimilisuppbot_var
print "6 Skert heimilisuppbot. Baetur eru nu samtals %s." % baetur

if bifreidarstyrkur:
baetur = baetur + 12587
print "7 Styrkur vegna reksturs bifreidar. Baetur eru nu samtals %s kr." % baetur

if maki:
baetur = baetur + 37739
print "8 Framfærsluuppbot. Baetur eru nu samtals %s kr." % baetur

print "Baetur eru %s kr." % baetur

最佳答案

我不会使用很多属性,而是使用一个字典:

self.data = dict(baetur = 0,
ororkustyrkur = 1,
ororkustyrkur_62 = 2,
ororkulifeyrir = 3,
ellilifeyrir = 4)

然后,从文件加载新数据后:

newdata = dict(baetur = 10,
ororkustyrkur = 20,
ororkustyrkur_62 = 30,
ororkulifeyrir = 40,
ellilifeyrir = 50)

你可以update self.data 包含:

self.data.update(newdata)
<小时/>

如果您像这样定义输入:

class Properties:
'''Calculates the amount of insurance and/or remedies for a person
when given a certain set of parameters.
'''
def input(self, **data):
self.data = data

那么你仍然可以这样调用它:

prop.input(kt=0, adlur=1, ...)

或使用keyword-unpacking syntax :

prop.input(**data)

关于python - 在 python 中从文件动态更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18079620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com