Skip to content

Commit 57deeb4

Browse files
authored
Refactor names of produce_or_load return values (#318)
The current documentation produce_or_load([path="",] config, f; kwargs...) -> file, s is quite confusing. It should be produce_or_load([path="",] config, f; kwargs...) -> data, filename or produce_or_load([path="",] config, f; kwargs...) -> data, file In principle, this only affects the documentation, but it seems like a good idea to refactor the names of the variables in the code to be consistent.
1 parent f6b3ca2 commit 57deeb4

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/saving_files.jl

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
export produce_or_load, @produce_or_load, tagsave, @tagsave, safesave
22

33
"""
4-
produce_or_load([path="",] config, f; kwargs...) -> file, s
5-
Let `s = joinpath(path, savename(prefix, config, suffix))` where `config` is some kind
6-
of named parameter container.
7-
If a file named `s` exists then load it and return it, along
8-
with the global path that it is saved at (`s`).
4+
produce_or_load([path="",] config, f; kwargs...) -> data, filename
5+
Let `filename = joinpath(path, savename(prefix, config, suffix))` where
6+
`config` is some kind of named parameter container.
7+
If `filename` exists then load it and return the contained `data`, along
8+
with the global path that it is saved at (`filename`).
99
10-
If the file does not exist then call `file = f(config)`, with `f` your function
11-
that produces your data. Then save the `file` as `s` and then return `file, s`.
10+
If the file does not exist then call `data = f(config)`, with `f` your function
11+
that produces your data. Then save the `data` as `filename` and then return
12+
`data, filename`.
1213
1314
The function `f` should return a dictionary if the data are saved in the default
1415
format of JLD2.jl., the macro [`@strdict`](@ref) can help with that.
@@ -27,11 +28,11 @@ end
2728
* `tag::Bool = DrWatson.readenv("DRWATSON_TAG", istaggable(suffix))` : Save the file
2829
using [`tagsave`](@ref) if `true` (which is the default).
2930
* `gitpath, storepatch` : Given to [`tagsave`](@ref) if `tag` is `true`.
30-
* `force = false` : If `true` then don't check if file `s` exists and produce
31+
* `force = false` : If `true` then don't check if `filename` exists and produce
3132
it and save it anyway.
3233
* `loadfile = true` : If `false`, this function does not actually load the
3334
file, but only checks if it exists. The return value in this case is always
34-
`nothing, s`, regardless of whether the file exists or not. If it doesn't
35+
`nothing, filename`, regardless of whether the file exists or not. If it doesn't
3536
exist it is still produced and saved.
3637
* `verbose = true` : print info about the process, if the file doesn't exist.
3738
* `wsave_kwargs = Dict()` : Keywords to pass to `wsave` (e.g. to enable
@@ -50,37 +51,37 @@ function produce_or_load(path, c, f::Function;
5051
kwargs...
5152
)
5253

53-
s = joinpath(path, savename(prefix, c, suffix; kwargs...))
54+
filename = joinpath(path, savename(prefix, c, suffix; kwargs...))
5455

55-
if !force && isfile(s)
56+
if !force && isfile(filename)
5657
if loadfile
57-
file = wload(s)
58-
return file, s
58+
data = wload(filename)
59+
return data, filename
5960
else
60-
return nothing, s
61+
return nothing, filename
6162
end
6263
else
6364
if force
64-
verbose && @info "Producing file $s now..."
65+
verbose && @info "Producing file $filename now..."
6566
else
66-
verbose && @info "File $s does not exist. Producing it now..."
67+
verbose && @info "File $filename does not exist. Producing it now..."
6768
end
68-
file = f(c)
69+
data = f(c)
6970
try
7071
if tag
71-
tagsave(s, file; safe = false, gitpath = gitpath, storepatch = storepatch, wsave_kwargs...)
72+
tagsave(filename, data; safe = false, gitpath = gitpath, storepatch = storepatch, wsave_kwargs...)
7273
else
73-
wsave(s, copy(file); wsave_kwargs...)
74+
wsave(filename, copy(data); wsave_kwargs...)
7475
end
75-
verbose && @info "File $s saved."
76+
verbose && @info "File $filename saved."
7677
catch er
7778
@warn "Could not save file. Error stacktrace:"
7879
Base.showerror(stderr, er, stacktrace(catch_backtrace()))
7980
end
8081
if loadfile
81-
return file, s
82+
return data, filename
8283
else
83-
return nothing, s
84+
return nothing, filename
8485
end
8586
end
8687
end

0 commit comments

Comments
 (0)