I have a daily process that writes a json
file:
{
"oldfield1": 1,
"oldfield2": "a",
}
and a python script that reads these files, each into a single object
import collections
import json
import os
MyRecord = collections.namedtuple("MyRecord",["oldfield1","oldfield2"])
mydata = {}
for fname in os.listdir("mydir"):
with open(fname) as fd:
mydata[fname] = MyRecord(**json.load(fd))
Tempora mutantur, and newer files now have an extra field:
{
"oldfield1": 3,
"oldfield2": "f",
"newfield": [1,2,3],
}
and now the code above fails with this error:
TypeError: __new__() got an unexpected keyword argument 'newfield'
I can add newfield
to MyRecord, but then it will fail on the old files.
What is the best approach?
I can add
newfield
toMyRecord
and setMyRecord.__new__.__defaults__
.I can sanitize the dict.
Priorities:
- I want to do as little as possible: minimize my code modifications (this implies option 1).
- I also want to minimize future code modifications: when
"futurefield"
is added, I want, ideally, not to have to do anything (this implies option 2). - Most important, I want to keep code maintainable (option 1?)
Personally, I like the first approach better. However, I would love to hear what people think.