1
1
export produce_or_load, @produce_or_load , tagsave, @tagsave , safesave
2
2
3
3
"""
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 `).
4
+ produce_or_load([path="",] config, f; kwargs...) -> data, file
5
+ Let `file = joinpath(path, savename(prefix, config, suffix))` by default,
6
+ where `config` is some kind of named parameter container.
7
+ If `file ` exists then load it and return the contained `data`, along
8
+ with the global path that it is saved at (`file `).
9
9
10
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 `.
11
+ that produces your data. Then save the `data` as `file ` and then return
12
+ `data, file `.
13
13
14
14
The function `f` should return a dictionary if the data are saved in the default
15
15
format of JLD2.jl., the macro [`@strdict`](@ref) can help with that.
24
24
```
25
25
26
26
## Keywords
27
+ * `filename = savename(prefix, config, suffix; kwargs...)` : Name of the file
28
+ to produce or load, relative to `path`. This may be useful in situations
29
+ where `config` has too many parameters for [`savename`](@ref) to be useful, and an
30
+ explicitly specified name is more suitable.
27
31
* `suffix = "jld2", prefix = default_prefix(config)` : Used in [`savename`](@ref).
28
32
* `tag::Bool = DrWatson.readenv("DRWATSON_TAG", istaggable(suffix))` : Save the file
29
33
using [`tagsave`](@ref) if `true` (which is the default).
30
34
* `gitpath, storepatch` : Given to [`tagsave`](@ref) if `tag` is `true`.
31
- * `force = false` : If `true` then don't check if `filename ` exists and produce
35
+ * `force = false` : If `true` then don't check if `file ` exists and produce
32
36
it and save it anyway.
33
37
* `loadfile = true` : If `false`, this function does not actually load the
34
38
file, but only checks if it exists. The return value in this case is always
35
- `nothing, filename `, regardless of whether the file exists or not. If it doesn't
39
+ `nothing, file `, regardless of whether the file exists or not. If it doesn't
36
40
exist it is still produced and saved.
37
41
* `verbose = true` : print info about the process, if the file doesn't exist.
38
42
* `wsave_kwargs = Dict()` : Keywords to pass to `wsave` (e.g. to enable
@@ -48,40 +52,42 @@ function produce_or_load(path, c, f::Function;
48
52
gitpath = projectdir (), loadfile = true ,
49
53
storepatch:: Bool = readenv (" DRWATSON_STOREPATCH" , false ),
50
54
force = false , verbose = true , wsave_kwargs = Dict (),
55
+ filename:: Union{Nothing, AbstractString} = nothing ,
51
56
kwargs...
52
57
)
53
58
54
- filename = joinpath (path, savename (prefix, c, suffix; kwargs... ))
59
+ isnothing (filename) && (filename = savename (prefix, c, suffix; kwargs... ))
60
+ file = joinpath (path, filename)
55
61
56
- if ! force && isfile (filename )
62
+ if ! force && isfile (file )
57
63
if loadfile
58
- data = wload (filename )
59
- return data, filename
64
+ data = wload (file )
65
+ return data, file
60
66
else
61
- return nothing , filename
67
+ return nothing , file
62
68
end
63
69
else
64
70
if force
65
- verbose && @info " Producing file $filename now..."
71
+ verbose && @info " Producing file $file now..."
66
72
else
67
- verbose && @info " File $filename does not exist. Producing it now..."
73
+ verbose && @info " File $file does not exist. Producing it now..."
68
74
end
69
75
data = f (c)
70
76
try
71
77
if tag
72
- tagsave (filename , data; safe = false , gitpath = gitpath, storepatch = storepatch, wsave_kwargs... )
78
+ tagsave (file , data; safe = false , gitpath = gitpath, storepatch = storepatch, wsave_kwargs... )
73
79
else
74
- wsave (filename , copy (data); wsave_kwargs... )
80
+ wsave (file , copy (data); wsave_kwargs... )
75
81
end
76
- verbose && @info " File $filename saved."
82
+ verbose && @info " File $file saved."
77
83
catch er
78
84
@warn " Could not save file. Error stacktrace:"
79
85
Base. showerror (stderr , er, stacktrace (catch_backtrace ()))
80
86
end
81
87
if loadfile
82
- return data, filename
88
+ return data, file
83
89
else
84
- return nothing , filename
90
+ return nothing , file
85
91
end
86
92
end
87
93
end
0 commit comments