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

Custom types for wload to be used with produce_or_load #304

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/DrWatson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,21 @@ function wsave(filename, data...; kwargs...)
return _wsave(filename, data...; kwargs...)
end

"Currently equivalent with `FileIO.load`."
"""
wload([::DataType], args...; kwargs...)

Default fallback is `FileIO.load`, discarding the (optional) first argument.
Extend this for your types as the first argument:

```julia
struct TheAnswer end

DrWatson.wload(::Type{TheAnswer}) = TheAnswer()
```
"""
function wload end
wload(data...; kwargs...) = FileIO.load(data...; kwargs...)
wload(::DataType, data...; kwargs...) = FileIO.load(data...; kwargs...)

include("saving_files.jl")
include("dict_list.jl")
Expand Down
10 changes: 9 additions & 1 deletion src/saving_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ end
* `verbose = true` : print info about the process, if the file doesn't exist.
* `wsave_kwargs = Dict()` : Keywords to pass to `wsave` (e.g. to enable
compression).
* `returntype::DataType = Any` : First argument passed to `wload`.
Set this if you defined a custom method for `wload`.
If `f` does not return a `returntype`, this function will issue a warning.
The data is still produced and saved.
* `kwargs...` : All other keywords are propagated to `savename`.
"""
produce_or_load(c, f; kwargs...) = produce_or_load("", c, f; kwargs...)
Expand All @@ -47,14 +51,15 @@ function produce_or_load(path, c, f::Function;
gitpath = projectdir(), loadfile = true,
storepatch::Bool = get(ENV, "DRWATSON_STOREPATCH", false),
force = false, verbose = true, wsave_kwargs = Dict(),
returntype::DataType=Any,
kwargs...
)

s = joinpath(path, savename(prefix, c, suffix; kwargs...))

if !force && isfile(s)
if loadfile
file = wload(s)
file = wload(returntype, s)
return file, s
else
return nothing, s
Expand All @@ -66,6 +71,9 @@ function produce_or_load(path, c, f::Function;
verbose && @info "File $s does not exist. Producing it now..."
end
file = f(c)
if !(file isa returntype)
@warn "Data returned from `f` is not a `$returntype`. Saving anyway, but `produce_or_load` might not be able to load the data back."
end
try
if tag
tagsave(s, file; safe = false, gitpath = gitpath, storepatch = storepatch, wsave_kwargs...)
Expand Down
47 changes: 47 additions & 0 deletions test/savefiles_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,53 @@ using DataFrames
@test !isfile(spath)
end

@testset "produce_or_load with custom type" begin
struct Custom
a
b
simulation
end

Custom(d::Dict{String}) = Custom(d["a"], d["b"], d["simulation"])

function DrWatson._wsave(path, c::Custom)
wsave(joinpath(path, "a.jld2"), c.a)
wsave(joinpath(path, "b.jld2"), c.b)
wsave(joinpath(path, "simulation.jld2"), c.simulation)
end

function DrWatson.wload(::Type{Custom}, path)
a = wload(joinpath(path, "a.jld2"))
b = wload(joinpath(path, "b.jld2"))
simulation = wload(joinpath(path, "simulation.jld2"))
return Custom(a, b, simulation)
end

Base.copy(c::Custom) = c

@test !ispath(savename(simulation))
sim, path = produce_or_load(simulation, Custom∘f; returntype=Custom, suffix="dir")
@test isdir(savename(simulation))
@test sim.simulation.T == T
@test path == savename(simulation)
@test sim == wload(Custom, path)

# Not specifying a return type while having an overload for `_wsave` defined issues a warning:
msg = r"^The return type of `f` does not match"
rm(savename(simulation))
@test !isfile(savename(simulation))
@test_logs (:warn, msg) sim, path = produce_or_load(simulation, Custom∘f, suffix="dir")
@test isfile(savename(simulation))
@test sim == wload(Custom, path)

# Specifying the wrong return type issues a warning:
rm(savename(simulation))
@test !isfile(savename(simulation))
@test_logs (:warn, msg) sim, path = produce_or_load(simulation, Custom∘f; returntype=Int, suffix="dir")
@test isfile(savename(simulation))
@test sim == wload(Custom, path)
end

################################################################################
# Backup files before saving #
################################################################################
Expand Down