1
1
export produce_or_load, @produce_or_load , tagsave, @tagsave , safesave
2
2
3
3
"""
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 `).
9
9
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`.
12
13
13
14
The function `f` should return a dictionary if the data are saved in the default
14
15
format of JLD2.jl., the macro [`@strdict`](@ref) can help with that.
27
28
* `tag::Bool = DrWatson.readenv("DRWATSON_TAG", istaggable(suffix))` : Save the file
28
29
using [`tagsave`](@ref) if `true` (which is the default).
29
30
* `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
31
32
it and save it anyway.
32
33
* `loadfile = true` : If `false`, this function does not actually load the
33
34
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
35
36
exist it is still produced and saved.
36
37
* `verbose = true` : print info about the process, if the file doesn't exist.
37
38
* `wsave_kwargs = Dict()` : Keywords to pass to `wsave` (e.g. to enable
@@ -50,37 +51,37 @@ function produce_or_load(path, c, f::Function;
50
51
kwargs...
51
52
)
52
53
53
- s = joinpath (path, savename (prefix, c, suffix; kwargs... ))
54
+ filename = joinpath (path, savename (prefix, c, suffix; kwargs... ))
54
55
55
- if ! force && isfile (s )
56
+ if ! force && isfile (filename )
56
57
if loadfile
57
- file = wload (s )
58
- return file, s
58
+ data = wload (filename )
59
+ return data, filename
59
60
else
60
- return nothing , s
61
+ return nothing , filename
61
62
end
62
63
else
63
64
if force
64
- verbose && @info " Producing file $s now..."
65
+ verbose && @info " Producing file $filename now..."
65
66
else
66
- verbose && @info " File $s does not exist. Producing it now..."
67
+ verbose && @info " File $filename does not exist. Producing it now..."
67
68
end
68
- file = f (c)
69
+ data = f (c)
69
70
try
70
71
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... )
72
73
else
73
- wsave (s , copy (file ); wsave_kwargs... )
74
+ wsave (filename , copy (data ); wsave_kwargs... )
74
75
end
75
- verbose && @info " File $s saved."
76
+ verbose && @info " File $filename saved."
76
77
catch er
77
78
@warn " Could not save file. Error stacktrace:"
78
79
Base. showerror (stderr , er, stacktrace (catch_backtrace ()))
79
80
end
80
81
if loadfile
81
- return file, s
82
+ return data, filename
82
83
else
83
- return nothing , s
84
+ return nothing , filename
84
85
end
85
86
end
86
87
end
0 commit comments