Closed ledm closed 8 months ago
Working on this here: https://github.com/valeriupredoi/bgcval2/tree/dev_fixing_shelve_opens
Requires PR #124
Okay, made some progress on changing the shOpen pattern from:
sh = open(filename)
sh['data'] = data
sh.close()
to the "correct" pattern:
with open(filename) as sh:
sh['data'] = data
Early testing seems to have worked. As discussed via email, I don't think it's worth changing every instance, as several scripts in bgcval2 are not used for most purposes.
The python module shelve has some limitations:
I've also not been great at implementing it. My pattern has been:
but the preferred style uses context managers:
This is safer, clearer and more reusable.
Step 1 is to move all the shelve writes into separate functions.
Step 2 is to convert the shelve open commands into context managers.
Step 3 is to write a json version of the separate function.
Step 4 is to write a shelve to json conversion function.
Step 5: Write some tests?