JuliaIO / JLD2.jl

HDF5-compatible file format in pure Julia
Other
560 stars 92 forks source link

Automatic saving & load of whole workspace #361

Closed VoSiLk closed 2 years ago

VoSiLk commented 2 years ago

Hi,

is there a function to automatically load or save the whole workspace. Sometimes when I get off work, I would like to comfortably reload my stand from the day before.

Something like this, but a bit more advanced and robust.

function get_all_vars()
    names_list = names(Main)[5:end]
    dict_vars = Dict{String,Any}
    for (i,name) in enumerate(names_list)
        try
            dict_vars = merge!(dict_vars, Dict{String,Any}(string(name)=>eval(name)) )
        catch
        end
    end
    return dict_vars
end

function extract(d)
   expr = quote end
   for (k, v) in d
       push!(expr.args, :($(Symbol(k)) = $v))
   end
   eval(expr)
   return
end

Best regards

JonasIsensee commented 2 years ago

Hi @VoSiLk ,

there is JLD2.@save and JLD2.@load which try exactly that but they are no longer documented because it is not recommended practice. You've already made the point yourself

Something like this, but a bit more advanced and robust.

this is ridiculously hard. In particular, it is not possible to store and load functions or packages, so it would always be incomplete.

WouterJRV commented 2 years ago

I would second that this is a useful feature to have even if imperfect. I remember the @save macro to be useful for this in the past, but it seems to no longer work

opening file "... .jld2": Invalid argument 

Perhaps it can't be perfect, but in the imperfect world of prototyping and scientific computing it can still be convenient. Especially in MATLAB for example, this is standard functionality...