I have the following two classes MasterProgramList and ProgramList. MasterProgramList takes in a json array object which is defined below in the usage code. I am trying to iterate over each Program object and change the start_date property. When I do so, the value does not change but stays as what is defined in the json array object. Is there something wrong with my ProgramList/Program class? How can I iterate over the class objects and change the property value?
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
ProgramList.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']
)
Usage:
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'}
First, a few things to clean up to make this work:
In the Usage section, there are no imports for
ProgramListorMasterProgramList. I added them as:then, in
MasterProgramList.pyI added an import ofProgramList.Moving on, I see that
crm_programs_to_analyze.program_listis a list that contains oneProgramList.ProgramListobject. So your outerforloop is iterating once on that single object. Then the innerforloop iterates over the members of theProgramList's programs, and there it is setting thestart_dateto2020-01-22.The problem you are having is in the
__repr__method-- specifically where it's getting the values it returns. Here is a very short example that illustrates the problem:The value of
start_dateis being updated. But printingpby way of the__repr__is getting the original value from_config, which isn't updated whenp.start_dateis modified. To fix it, either add in an update to change thestart_dateinp._config, or have__repr__return the value ofstart_dateinstead.Like this: