valeriupredoi / bgcval2

Package for BGCVal v2.0
3 stars 0 forks source link

Replacing python.shelve with json #125

Closed ledm closed 8 months ago

ledm commented 8 months ago

The python module shelve has some limitations:

I've also not been great at implementing it. My pattern has been:

sh = shelve,open('path')
sh[field] = data
sh.close()

but the preferred style uses context managers:

with shelve.open(path, mode="w") as file:
    file[field] = data

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?

ledm commented 8 months ago

Working on this here: https://github.com/valeriupredoi/bgcval2/tree/dev_fixing_shelve_opens

Requires PR #124

ledm commented 8 months ago

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.