From 111b2f3f85ed872cb299b0137d5334f98cdf0e11 Mon Sep 17 00:00:00 2001 From: andrewrosemberg Date: Wed, 28 Oct 2020 16:37:10 +0000 Subject: [PATCH 1/6] overload haskey for AbstractModel --- src/JuMP.jl | 8 ++++++++ test/model.jl | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/JuMP.jl b/src/JuMP.jl index 7295c9f800b..8566239fbf1 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -954,6 +954,14 @@ function Base.setindex!(m::JuMP.Model, value, name::Symbol) m.obj_dict[name] = value end +""" + + +""" +function Base.haskey(m::JuMP.AbstractModel, name::Symbol) + haskey(object_dictionary(m), name) +end + """ operator_warn(model::AbstractModel) operator_warn(model::Model) diff --git a/test/model.jl b/test/model.jl index 79fa39b557e..407efaefd85 100644 --- a/test/model.jl +++ b/test/model.jl @@ -492,6 +492,13 @@ function test_copy_direct_mode() @test_throws ErrorException JuMP.copy(model) end +function test_haskey() + m = Model() + @variable(m, p[i=1:10] >= 0) + @test haskey(m, :p) + @test !haskey(m, :i) +end + function runtests() for name in names(@__MODULE__; all = true) if !startswith("$(name)", "test_") From ce36c11163477a8eb78a8f91038b91b6efbded2d Mon Sep 17 00:00:00 2001 From: andrewrosemberg Date: Wed, 28 Oct 2020 16:41:39 +0000 Subject: [PATCH 2/6] add docstring --- src/JuMP.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/JuMP.jl b/src/JuMP.jl index 8566239fbf1..78492e695b0 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -955,8 +955,9 @@ function Base.setindex!(m::JuMP.Model, value, name::Symbol) end """ + Base.haskey(m::JuMP.AbstractModel, name::Symbol) - +To allow easy verification if a key is present in a JuMP model. """ function Base.haskey(m::JuMP.AbstractModel, name::Symbol) haskey(object_dictionary(m), name) From 5bc31b5e55cb8b1409b471d1eef18313d0e0d376 Mon Sep 17 00:00:00 2001 From: andrewrosemberg Date: Wed, 28 Oct 2020 16:49:35 +0000 Subject: [PATCH 3/6] fix style --- src/JuMP.jl | 8 ++++---- test/model.jl | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/JuMP.jl b/src/JuMP.jl index 78492e695b0..92d9281871a 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -955,12 +955,12 @@ function Base.setindex!(m::JuMP.Model, value, name::Symbol) end """ - Base.haskey(m::JuMP.AbstractModel, name::Symbol) + haskey(model::JuMP.AbstractModel, name::Symbol) -To allow easy verification if a key is present in a JuMP model. +Determine whether the model has a mapping for a given name.. """ -function Base.haskey(m::JuMP.AbstractModel, name::Symbol) - haskey(object_dictionary(m), name) +function Base.haskey(model::JuMP.AbstractModel, name::Symbol) + haskey(object_dictionary(model), name) end """ diff --git a/test/model.jl b/test/model.jl index 407efaefd85..87ba131d163 100644 --- a/test/model.jl +++ b/test/model.jl @@ -493,10 +493,10 @@ function test_copy_direct_mode() end function test_haskey() - m = Model() - @variable(m, p[i=1:10] >= 0) - @test haskey(m, :p) - @test !haskey(m, :i) + model = Model() + @variable(model, p[i=1:10] >= 0) + @test haskey(model, :p) + @test !haskey(model, :i) end function runtests() From de6086416a5d6f1d83cb2ffc89065e668c070d70 Mon Sep 17 00:00:00 2001 From: andrewrosemberg Date: Wed, 28 Oct 2020 16:50:03 +0000 Subject: [PATCH 4/6] fix typo --- src/JuMP.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JuMP.jl b/src/JuMP.jl index 92d9281871a..ed27c822623 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -957,7 +957,7 @@ end """ haskey(model::JuMP.AbstractModel, name::Symbol) -Determine whether the model has a mapping for a given name.. +Determine whether the model has a mapping for a given name. """ function Base.haskey(model::JuMP.AbstractModel, name::Symbol) haskey(object_dictionary(model), name) From 07b85fba79a61f0de0e57cb131f539d9738d4a9c Mon Sep 17 00:00:00 2001 From: andrewrosemberg Date: Wed, 28 Oct 2020 17:36:08 +0000 Subject: [PATCH 5/6] add return to follow style --- src/JuMP.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JuMP.jl b/src/JuMP.jl index ed27c822623..37834f4aa1d 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -960,7 +960,7 @@ end Determine whether the model has a mapping for a given name. """ function Base.haskey(model::JuMP.AbstractModel, name::Symbol) - haskey(object_dictionary(model), name) + return haskey(object_dictionary(model), name) end """ From 99147d1b49d4545c0ea8b0eb16086d5a9a2ea19a Mon Sep 17 00:00:00 2001 From: andrewrosemberg Date: Wed, 28 Oct 2020 18:33:03 +0000 Subject: [PATCH 6/6] rm JuMP prefix --- src/JuMP.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JuMP.jl b/src/JuMP.jl index 37834f4aa1d..3efedba9767 100644 --- a/src/JuMP.jl +++ b/src/JuMP.jl @@ -955,11 +955,11 @@ function Base.setindex!(m::JuMP.Model, value, name::Symbol) end """ - haskey(model::JuMP.AbstractModel, name::Symbol) + haskey(model::AbstractModel, name::Symbol) Determine whether the model has a mapping for a given name. """ -function Base.haskey(model::JuMP.AbstractModel, name::Symbol) +function Base.haskey(model::AbstractModel, name::Symbol) return haskey(object_dictionary(model), name) end