I have a class in python that is used to generate parameter files for a piece of software. This software is used in an iterative process and requires a new set of parameter files for each iteration. As such the class PropGen
is called upon to create the new files just before each iteration.
The class is feed the default parameters for these files once before the entire process and then given the current iteration modifies these parameters and writes them to the new file. The way I have been accomplishing this is by storing the defaults into an OrderedDict
self.params
and creating another OrderedDict
self.output_params
that collects the modified values before being used to write to a file.
My problem is that no matter how I move the values from self.params
to self.output_params
the two dictionaries have the same object id and thus any changes to self.output_params
are reflected in self.params
. So far I have tried the following:
EDIT Found error with missing call to deepcopy at end of file.
class A(object):
def __init__(self):
self.a = OrderedDict({'a':1, 'b':2})
self.b = deepcopy(self.a)
self.iter = 0
def do_some_work(self, key):
val = self.a[key]
self.b[key] = val.replace('#', self.iter)
def create(self):
lines = []
for item in self.output_params.items():
lines.append('='.join(item) + '\n')
with open(filename, 'w') as file_obj:
file_obj.writelines(lines)
# Here was the error
self.b = self.a
# should have been self.b = deepcopy(self.a)