Skip to content

Commit 0293573

Browse files
Yet another kwargs: "equals" in savename (#252)
* equals kwarg * real world examples * Update docs/src/real_world.md to use backticks * bump version + changelog Co-authored-by: George Datseris <[email protected]>
1 parent 0f528b8 commit 0293573

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 2.0.3
2+
* Added a kwarg `equals` to `savename` to extend applicability.
13
# 2.0
24
## Breaking
35
* DrWatson has moved entirely on using JLD2.jl instead of BSON.jl for saving files, and also suggests the same to its users through the documentation.

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "2.0.2"
4+
version = "2.0.3"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

docs/src/real_world.md

+49
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,52 @@ end
465465

466466
special_list = [largestthree,]
467467
```
468+
469+
### Using `savename` to produce logfiles
470+
471+
When your code runs for a long time or even runs on different machines such as a cluster
472+
environment it becomes important to produce logfiles. Logfiles allow you to
473+
view the progress of your program while it is still running, or check later
474+
on if everything went according to plan.
475+
476+
```julia
477+
using Dates
478+
479+
function logmessage(n, error)
480+
# current time
481+
time = Dates.format(now(UTC), dateformat"yyyy-mm-dd HH:MM:SS")
482+
483+
# memory the process is using
484+
maxrss = "$(round(Sys.maxrss()/1048576, digits=2)) MiB"
485+
486+
logdata = (;
487+
n, # iteration n
488+
error, # some super important progress update
489+
maxrss) # lastly the amount of memory being used
490+
491+
println(savename(time, logdata; connector=" | ", equals=" = ", sort=false, digits=2))
492+
end
493+
494+
function expensive_computation(N)
495+
496+
for n = 1:N
497+
sleep(1) # heavy computation
498+
error = rand()/n # some super import progress update
499+
logmessage(n, error)
500+
end
501+
502+
end
503+
```
504+
505+
This yields output that is both easy to read *and* machine parseable.
506+
If you ever end up with too many logfiles to read, there is still `parse_savename` to
507+
help you.
508+
509+
```julia
510+
julia> expensive_computation(5)
511+
2021-05-19 19:20:25 | n = 1 | error = 0.65 | maxrss = 326.27 MiB
512+
2021-05-19 19:20:26 | n = 2 | error = 0.48 | maxrss = 326.27 MiB
513+
2021-05-19 19:20:27 | n = 3 | error = 0.08 | maxrss = 326.27 MiB
514+
2021-05-19 19:20:28 | n = 4 | error = 0.11 | maxrss = 326.27 MiB
515+
2021-05-19 19:20:29 | n = 5 | error = 0.15 | maxrss = 326.27 MiB
516+
```

src/naming.jl

+7-4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ See also [`parse_savename`](@ref) and [`@savename`](@ref).
6060
called with its default arguments (so customization here is possible only
6161
by rolling your own container type). Containers leading to empty `savename`
6262
are skipped.
63+
* `equals = "="` : Connector between name and value. Can be useful to modify for
64+
adding space `" = "`.
6365
6466
## Examples
6567
```julia
@@ -69,6 +71,7 @@ savename("n", d) == "n_a=0.153_b=5_mode=double"
6971
savename(d, "n") == "a=0.153_b=5_mode=double.n"
7072
savename("n", d, "n"; connector = "-") == "n-a=0.153-b=5-mode=double.n"
7173
savename(d, allowedtypes = (String,)) == "mode=double"
74+
savename(d, connector=" | ", equals=" = ") == "a = 0.153 | b = 5 | mode = double"
7275
7376
rick = (never = "gonna", give = "you", up = "!");
7477
savename(rick) == "give=you_never=gonna_up=!" # keys are sorted!
@@ -85,7 +88,7 @@ function savename(prefix::String, c, suffix::String;
8588
connector = "_", expand::Vector{String} = default_expand(c),
8689
sigdigits::Union{Int,Nothing}=nothing,
8790
val_to_string = nothing,
88-
sort = true)
91+
sort = true, equals = "=")
8992

9093
if any(sep in prefix for sep in ['/', '\\'])
9194
@warn """
@@ -116,11 +119,11 @@ function savename(prefix::String, c, suffix::String;
116119
if any(x -> (t <: x), allowedtypes)
117120
if label expand
118121
isempty(val) && continue
119-
sname = savename(val; connector=",", digits=digits, sigdigits=sigdigits)
122+
sname = savename(val; connector=",", digits=digits, sigdigits=sigdigits, equals = equals)
120123
isempty(sname) && continue
121-
entry = label*"="*'('*sname*')'
124+
entry = label*equals*'('*sname*')'
122125
else
123-
entry = label*"="*val2string(val)
126+
entry = label*equals*val2string(val)
124127
end
125128
!first && (s *= connector)
126129
s *= entry

test/naming_tests.jl

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ d = (a = 0.153456453, b = 5, mode = "double")
1111
@test savename("n", d, "n") == "n_a=0.153_b=5_mode=double.n"
1212
@test savename("n", d, "n"; connector = "-") == "n-a=0.153-b=5-mode=double.n"
1313
@test savename(d, allowedtypes = (String,)) == "mode=double"
14+
@test savename(d, connector=" | ", equals=" = ") == "a = 0.153 | b = 5 | mode = double"
15+
1416
tday = today()
1517
@test savename(@dict(tday)) == "tday=$(string(tday))"
1618

0 commit comments

Comments
 (0)