gpt4 book ai didi

python - 如何在 Python 中迭代类对象并更改属性值

转载 作者:行者123 更新时间:2023-12-01 00:11:07 25 4
gpt4 key购买 nike

我有以下两个类MasterProgramList和ProgramList。MasterProgramList 接受一个 json 数组对象,该对象在下面的使用代码中定义。我试图迭代每个 Program 对象并更改 start_date 属性。当我这样做时,该值不会改变,而是保持为 json 数组对象中定义的值。我的 ProgramList/Program 类有问题吗?如何迭代类对象并更改属性值?

MasterProgramList.py

import json
import datetime
class MasterProgramList(object):
def __init__(self, program_list, today=None):
self.program_list = [ProgramList(program) for program in program_list]
self.today = today

def current_programs(self):
if self.today is not None:
def filter_if_between_start_and_end(program):
return (datetime.datetime.strptime(program.start_date, '%Y-%m-%d').date()) <= self.today.date() <= (
datetime.datetime.strptime(program.end_date, '%Y-%m-%d').date())

program_list = self.program_list
for x in program_list:
x.programs = filter(filter_if_between_start_and_end, x.programs)

filtered_program_list = program_list
else:
filtered_program_list = self.program_list
return filtered_program_list

程序列表.py

class ProgramList(object):
"""
CRM-specific version of the ProgramList Configuration object
"""
def __init__(self, config):
self._config = config
self.brand = config['brand']
self.programs = self._parse_programs(config['brand'], config['programs'])

def _parse_programs(cls, brand, program_list):
return [Program.from_json(brand, p) for p in program_list]

class Program(object):
def __init__(self, **kwargs):
self._config = kwargs.get('config') #required json data
self.name = kwargs.get('name') #required json data
self.start_date = kwargs.get('start_date') #required json data
self.end_date = kwargs.get('end_date') #required json data

def __repr__(self):
return str(self._config)

@classmethod
def from_json(cls, brand, program_json):
program_json['name'] = '{0}_{1}'.format(brand.capitalize(), program_json['name'])
program_json['department'] = 'CRM'
return Program(
config = program_json,
name = program_json['name'],
start_date = program_json['start_date'],
end_date = program_json['end_date']
)

用法:

from datetime import datetime, timedelta

crm_program_master_list = [
{
"brand": "testbrand",
"programs": [
{
"name": "Program1",
"start_date": "2020-01-02",
"end_date": "2999-12-31"
},
{
"name": "Program2",
"start_date": "2020-01-04",
"end_date": "2999-12-31"
}
]
}
]

today_date = (datetime.strptime('2020-01-06', '%Y-%m-%d'))
crm_programs_to_analyze = MasterProgramList(crm_program_master_list, today=today_date)
crm_programs_to_analyze.program_list = crm_programs_to_analyze.current_programs()

programs = []
for x in crm_programs_to_analyze.program_list:
for p in x.programs:
#change the start_date for each program
p.start_date = '2020-01-22'
print('program: {}'.format(p))
programs.append(p)
output:
program: {'name': 'Program1', 'end_date': '2999-12-31', 'department': 'CRM', 'start_date': '2020-01-02'}
program: {'name': 'Program2', 'end_date': '2999-12-31', 'department': 'CRM', 'start_date': '2020-01-04'}

expected output:
program: {'name': 'Program1', 'end_date': '2999-12-31', 'department': 'CRM', 'start_date': '2020-01-22'}
program: {'name': 'Program2', 'end_date': '2999-12-31', 'department': 'CRM', 'start_date': '2020-01-22'}

最佳答案

首先,为了使这项工作正常进行,需要清理一些东西:

在“使用”部分中,没有导入 ProgramListMasterProgramList。我将它们添加为:

from MasterProgramList import MasterProgramList
from ProgramList import ProgramList

然后,在 MasterProgramList.py 中,我添加了 ProgramList 的导入。

继续,我看到 crm_programs_to_analyze.program_list 是一个包含一个 ProgramList.ProgramList 对象的列表。因此,您的外部 for 循环在该单个对象上迭代一次。然后,内部 for 循环迭代 ProgramList 程序的成员,并将 start_date 设置为 2020- 01-22.

您遇到的问题出在 __repr__ 方法中 - 特别是它获取返回值的地方。这是一个说明问题的非常简短的示例:

>>> class f:
... def __init__(self, config):
... self.config = config
... self.start_date = config['start_date']
... self.end_date = config['end_date']
... def __repr__(self):
... return str(self.config)
...
>>> conf = {'start_date': '2020-10-10', 'end_date': '2021-12-03'}
>>> foo = f(conf)
>>> foo
{'start_date': '2020-10-10', 'end_date': '2021-12-03'}
>>> foo.start_date = '2019-10-10' # <-- Changes start_date, but not config
>>> foo
{'start_date': '2020-10-10', 'end_date': '2021-12-03'} # <-- From config
>>> foo.start_date # <-- but this as updated as expected
'2019-10-10'
>>> foo.config
{'start_date': '2020-10-10', 'end_date': '2021-12-03'} # <-- unchanged
>>> foo.config['start_date'] = foo.start_date
>>> foo
{'start_date': '2019-10-10', 'end_date': '2021-12-03'}
>>> foo.start_date
'2019-10-10'
>>> foo.config # <-- now it's changed and they match
{'start_date': '2019-10-10', 'end_date': '2021-12-03'}

start_date 的值正在 正在更新。但是通过 __repr__ 打印 p 是从 _config 获取原始值,当 p.start_date< 时该值不会更新 已修改。要修复此问题,请添加更新以更改 p._config 中的 start_date,或者让 __repr__ 返回 start_date 的值 相反。

像这样:

    def __repr__(self):
rv = f'Start Date: {self.start_date}, End Date: {self.end_date}, Config: '
for k, v in self._config.items():
rv += f'{k} : {v}, '
return rv

关于python - 如何在 Python 中迭代类对象并更改属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59622886/

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