-
Notifications
You must be signed in to change notification settings - Fork 143
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
Add save and load functions that operate on dictionaries #101
Conversation
These functions are similar to their macro counterparts, except that they operate on and return dictionaries instead of variables directly in the global namespace. save takes a filename and a dictionary with bytestring keys. The dictionary key-value pairs are saved in the file, with the keys as the variable names and the values as their contents. load takes a filename and (optionally) a list of variable names to read. If no variable names are given, all names in the file are read. It reads the variables into a dictionary that is returned.
Previously, this macro would simply return the value of the last variable it read. This is more useful.
Love it, thanks for doing this! Definitely these should be exported---I'd really rather encourage people towards the functions, because the macros are very fragile. The internal functions that support the macros can be renamed to something private so they are not exported. I don't have any reservations about any aspect of your implementation---nice work! |
"r+" requires the file to already exist. Appending data to files is tricky and not terribly intuitive (what if data with the same names are already there?). I think "w" is better.
Glad you like it. After playing with it a bit more, I reverted the "r+" mode in save as it requires that the file already exist. And the semantics there are tricky. We can just clobber and document that it clobbers. This is also the default mode for Matlab's |
Given that the typing of dict comprehensions is poor at the top-level, it is silly to make folks jump through hoops to make sure that they String keys really are strings. The bytestring function will error just fine by itself, although perhaps it could use a nicer error message.
Ok, updated with documentation and tests. But I come back with two issues:
|
|
Well, the way operating systems do this is by the extension. Are there other top-level functions that are like this? Is it too feature-y to have Base Julia register filetypes, define save and load, and do the "dispatch" to non-exported functions? Or we could define a new string type that's parametric with its extension! Single-quote 'strings' are available, no? 😈 |
Your idea about the file extension is interesting. In Images there is a mechanism for on-demand loading of support-code for particular file formats. This makes me wonder if that should become more generic. |
Implement save/load such that the following is possible: vardict = load(filename) var1, var2 = load(filename, "var1", "var2") save(filename, vardict) save(filename, "var1", var1, "var2", var2) as suggested by Tim Holy.
Updated and good-to-go. We can punt on the name clashes for now, I think. But this could be very powerful with some base language support. |
Discussion started at JuliaLang/julia#7299 |
Very cool. I forgot to mention that I deleted the |
WIP: Add save and load functions that operate on dictionaries
Thanks so much for tackling this; I think it was the biggest API wart in JLD. |
These functions are similar to their macro counterparts, except that they operate on and return dictionaries instead of variables directly in the global namespace.
save
takes a filename and a dictionary with bytestring keys. The dictionary key-value pairs are saved in the file, with the keys as the variable names and the values as their contents.load
takes a filename and (optionally) a list of variable names to read. If no variable names are given, all names in the file are read. It reads the variables into a dictionary that is returned.Furthermore, the
@load
macro now returns a list of variables that it loaded.Would fix #98. Still needs tests and documentation. And perhaps a bike-shed: Should
save
andload
be exported?I needed this for myself; it's very simple but I figured I may as well push it upstream, too.