Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing python.shelve with json #125

Closed
ledm opened this issue Mar 20, 2024 · 2 comments
Closed

Replacing python.shelve with json #125

ledm opened this issue Mar 20, 2024 · 2 comments

Comments

@ledm
Copy link
Collaborator

ledm commented Mar 20, 2024

The python module shelve has some limitations:

  • It's not great with disk errors,
  • It's not great for parrallelisation
  • it's confusing for some users
  • It's python exclusive
  • iot's not that portable.
  • Shelve now writes three files instead of one - making everything a bit hard.

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
Copy link
Collaborator Author

ledm commented Mar 21, 2024

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

Requires PR #124

@ledm
Copy link
Collaborator Author

ledm commented Mar 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant