Skip to content

Commit

Permalink
add get method for whole community, and default for default (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
kescobo authored Dec 7, 2021
1 parent ff40b24 commit a0976b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/profiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,14 @@ Base.haskey(commp::CommunityProfile, sample::AbstractString, key::Symbol) = in(k
Return the value of the metadata in a `sample` stored for the given `key`, or the given `default` value if no mapping for the key is present.
"""
Base.get(commp::CommunityProfile, sample::AbstractString, key::Symbol, default) = get(metadata(samples(commp, sample)), key, default)
Base.get(commp::CommunityProfile, sample::AbstractString, key::Symbol, default=missing) = get(metadata(samples(commp, sample)), key, default)

"""
get(commp::CommunityProfile, key::Symbol, default)
Return the value of the metadata in a `sample` stored for the given `key`, or the given `default` value if no mapping for the key is present.
"""
Base.get(commp::CommunityProfile, key::Symbol, default=missing) = [get(commp, sample, key, default) for sample in samplenames(commp)]


"""
Expand Down
3 changes: 3 additions & 0 deletions test/MicrobiomeTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ end
@test delete!(c4, "sample1", :something) isa MicrobiomeSample
@test !haskey(c4, "sample1", :something)
@test get(c4, "sample1", :something_else, 42) == 42
@test ismissing(get(c4, "sample1", :something_else))
@test insert!(c4, "sample1", :something, 3.0) isa MicrobiomeSample
@test get(c4, "sample1", :something, 42) == 3.0
set!(c4, "sample1", :something, 1.0)
Expand Down Expand Up @@ -275,6 +276,8 @@ end
insert!(c5, "sample2", (; graw="gnaw", biff="boof"))
@test_throws IndexError insert!(c5, [(;sample="sample1", still_other="yes"), (;sample="sample2", still_other="no")])
@test !haskey(c5, "sample1", :still_other)
@test all(get(c5, :something) .=== [1.0, missing])
@test all(get(c5, :something, 42.0) .== [1.0, 42.0])
end

@testset "set!" begin
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ include("MicrobiomeTests.jl")
MicrobiomeTests.runtests()

using Microbiome
Microbiome.runtests()
Microbiome.runtests()

0 comments on commit a0976b6

Please sign in to comment.