Skip to content

Commit 0b7d0a0

Browse files
authored
Add ignore to savename #123 from xukai92/master
2 parents a607719 + 2e22284 commit 0b7d0a0

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 1.9.0
2+
* `savename` now has the `ignore` option.
3+
14
# 1.8.0
25
* `@quickactivate` was enhanced to allow projects that also represent a module.
36
* `initialize_project` no resolves the folder name for naming the project if the path is given as "." or ".."

Project.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
name = "DrWatson"
22
uuid = "634d3b9d-ee7a-5ddf-bec9-22491ea816e1"
33
repo = "https://github.com/JuliaDynamics/DrWatson.jl.git"
4-
version = "1.8.1"
5-
6-
4+
version = "1.9.0"
75

86
[deps]
97
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"

docs/src/name.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,21 @@ Notice that this naming scheme integrates perfectly with Parameters.jl.
1818
Convenience functions are provided to shorten common function calls and easily create named tuples, dictionaries as well as switch between them:
1919
```@docs
2020
@dict
21-
@savename
2221
@strdict
2322
@ntuple
23+
@savename
2424
ntuple2dict
2525
dict2ntuple
2626
```
2727

2828
## Customizing `savename`
2929
You can customize [`savename`](@ref) for your own Types. For example you could make it so that it only uses some specific keys instead of all of them, only specific types, or you could make it access data in a different way (maybe even loading files!). You can even make it have a custom `prefix`!
3030

31-
To do that you may extend the following functions:
31+
To do that you may extend any of the following functions:
3232
```@docs
3333
DrWatson.allaccess
3434
DrWatson.access
35+
DrWatson.allingore
3536
DrWatson.default_allowed
3637
DrWatson.default_prefix
3738
DrWatson.default_expand

src/naming.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,19 @@ See [`default_prefix`](@ref) for more.
2727
2828
`savename` can be very conveniently combined with
2929
[`@dict`](@ref) or [`@ntuple`](@ref).
30-
See also [`parse_savename`](@ref).
30+
See also [`parse_savename`](@ref) and [`@savename`](@ref).
3131
3232
## Keywords
3333
* `allowedtypes = default_allowed(c)` : Only values of type subtyping
3434
anything in `allowedtypes` are used in the name. By default
3535
this is `(Real, String, Symbol)`.
36-
* `accesses = allaccess(c)` : You can also specify which specific keys you want
36+
* `accesses = allaccess(c)` : pecify which specific keys you want
3737
to use with the keyword `accesses`. By default this is all possible
3838
keys `c` can be accessed with, see [`allaccess`](@ref).
39+
* `ignores = allignore(c)` : You can also specify keys that you want
40+
to ignore with the keyword `ignores`. By default this is an empty
41+
tuple, see [`allignore`](@ref).
42+
(keys in `ignore` are ignored even if they are in `accesses`)
3943
* `digits = 3` : Floating point values are rounded to `digits`.
4044
In addition if the following holds:
4145
```julia
@@ -44,7 +48,7 @@ See also [`parse_savename`](@ref).
4448
then the integer value is used in the name instead.
4549
* `scientific = nothing` : Number of significant digits used for rounding of
4650
floating point values using scientific notation (e.g. `1.65e-7`).
47-
If `nothing`, normal rounding is done.
51+
If `nothing`, normal rounding is done.
4852
* `connector = "_"` : string used to connect the various entries.
4953
* `expand::Vector{String} = default_expand(c)` : keys that will be expanded
5054
to the `savename` of their contents, to allow for nested containers.
@@ -68,6 +72,7 @@ savename(d, allowedtypes = (String,)) == "mode=double"
6872
6973
rick = (never = "gonna", give = "you", up = "!");
7074
savename(rick) == "give=you_never=gonna_up=!" # keys are sorted!
75+
savename(rick; ignores = ["up"]) == "give=you_never=gonna"
7176
```
7277
"""
7378
savename(c; kwargs...) = savename(default_prefix(c), c, ""; kwargs...)
@@ -76,7 +81,7 @@ savename(c::Any, suffix::String; kwargs...) =
7681
savename(prefix::String, c::Any; kwargs...) = savename(prefix, c, ""; kwargs...)
7782
function savename(prefix::String, c, suffix::String;
7883
allowedtypes = default_allowed(c),
79-
accesses = allaccess(c), digits = 3,
84+
accesses = allaccess(c), ignores = allignore(c), digits = 3,
8085
connector = "_", expand::Vector{String} = default_expand(c),
8186
scientific::Union{Int,Nothing}=nothing)
8287

@@ -86,13 +91,16 @@ function savename(prefix::String, c, suffix::String;
8691
prefix = joinpath(prefix, dpre)
8792
end
8893

94+
# Perform access and ignore logic and sort
8995
labels = vecstring(accesses) # make it vector of strings
96+
ignored_labels = vecstring(ignores)
9097
p = sortperm(labels)
9198
first = prefix == "" || endswith(prefix, PATH_SEPARATOR)
9299
s = prefix
93100
for j p
94-
val = access(c, accesses[j])
95101
label = labels[j]
102+
label ignored_labels && continue
103+
val = access(c, accesses[j])
96104
t = typeof(val)
97105
if any(x -> (t <: x), allowedtypes)
98106
val = roundval(val,digits=digits,scientific=scientific)
@@ -173,6 +181,13 @@ access(c, keys...) = access(access(c, keys[1]), Base.tail(keys)...)
173181
access(c::AbstractDict, key) = getindex(c, key)
174182
access(c, key) = getproperty(c, key)
175183

184+
"""
185+
allignore(c)
186+
Return all the keys `c` that will be ignored in [`savename`](@ref).
187+
This is an empty tuple by default.
188+
"""
189+
allignore(c::Any) = ()
190+
176191
"""
177192
default_allowed(c) = (Real, String, Symbol)
178193
Return the (super-)Types that will be used as `allowedtypes`

test/naming_tests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ d = (a = 0.153456453, b = 5.0, mode = "double")
1414

1515
rick = (never = "gonna", give = "you", up = "!");
1616
@test savename(rick) == "give=you_never=gonna_up=!"
17+
@test savename(rick; ignores = ["up"]) == "give=you_never=gonna"
1718

1819
x = 3; y = 5.0;
1920
d = Dict(:x => x, :y => y)
@@ -26,11 +27,15 @@ n = (x = x, y = y)
2627

2728
z = "lala"
2829
d2 = Dict(:x => x, :y => y, :z => z)
29-
n2 = (x = x, y = y, z= z)
30+
n2 = (x = x, y = y, z = z)
3031

3132
@test d2 == @dict x y z
3233
@test n2 == @ntuple x y z
3334

35+
@test savename(n2; ignores=(:y,)) == "x=3_z=lala"
36+
@test savename(n2; ignores=("y",)) == "x=3_z=lala"
37+
@test savename(n2; accesses=(:x, :y), ignores=(:y,)) == "x=3"
38+
3439
@test savename(@dict x y) == "x=3_y=5"
3540
@test savename(@ntuple x y) == "x=3_y=5"
3641
w = rand(50)

0 commit comments

Comments
 (0)