Skip to content

Jlvaluetruth #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/jlwrap/any.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ function init_jlwrap_any()
return ValueBase.__dir__(self) + self._jl_callmethod($(pyjl_methodnum(pyjlany_dir)))
def __call__(self, *args, **kwargs):
return self._jl_callmethod($(pyjl_methodnum(pyjlany_call)), args, kwargs)
def __bool__(self):
return True
def __len__(self):
return self._jl_callmethod($(pyjl_methodnum(pyjlany_op(length))))
def __getitem__(self, k):
Expand Down
2 changes: 2 additions & 0 deletions src/jlwrap/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ function init_jlwrap_array()
return self._jl_callmethod($(pyjl_methodnum(Py ∘ copy)))
def reshape(self, shape):
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_reshape)), shape)
def __bool__(self):
return bool(len(self))
def __getitem__(self, k):
return self._jl_callmethod($(pyjl_methodnum(pyjlarray_getitem)), k)
def __setitem__(self, k, v):
Expand Down
2 changes: 2 additions & 0 deletions src/jlwrap/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function init_jlwrap_dict()
class DictValue(AnyValue):
__slots__ = ()
_jl_undefined_ = object()
def __bool__(self):
return bool(len(self))
def __iter__(self):
return self._jl_callmethod($(pyjl_methodnum(pyjldict_iter)))
def __contains__(self, key):
Expand Down
2 changes: 2 additions & 0 deletions src/jlwrap/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ function init_jlwrap_set()
$("\n"^(@__LINE__()-1))
class SetValue(AnyValue):
__slots__ = ()
def __bool__(self):
return bool(len(self))
def add(self, value):
return self._jl_callmethod($(pyjl_methodnum(pyjlset_add)), value)
def discard(self, value):
Expand Down
124 changes: 124 additions & 0 deletions test/jlwrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
Base.:(>>)(x::Foo, y::Foo) = "$(x.value) >> $(y.value)"
Base.:(&)(x::Foo, y::Foo) = "$(x.value) & $(y.value)"
Base.:(|)(x::Foo, y::Foo) = "$(x.value) | $(y.value)"
@testset "type" begin
@test pyis(pytype(pyjl(Foo(1))), PythonCall.pyjlanytype)
@test pyis(pytype(pyjl(nothing)), PythonCall.pyjlanytype)
@test pyis(pytype(pyjl(missing)), PythonCall.pyjlanytype)
end
@testset "bool" begin
@test pytruth(pyjl(Foo(0)))
@test pytruth(pyjl(Foo(1)))
@test pytruth(pyjl(nothing))
@test pytruth(pyjl(missing))
end
@testset "pos" begin
z = pyjl(+Foo(1))
@test pyconvert(String, z) == "+ 1"
Expand Down Expand Up @@ -123,10 +134,123 @@
end
end

@testitem "array" begin
@testset "type" begin
@test pyis(pytype(pyjl(fill(nothing))), PythonCall.pyjlarraytype)
@test pyis(pytype(pyjl([1 2; 3 4])), PythonCall.pyjlarraytype)
end
@testset "bool" begin
@test !pytruth(pyjl(fill(nothing, 0, 1)))
@test !pytruth(pyjl(fill(nothing, 1, 0)))
@test pytruth(pyjl(fill(nothing)))
@test pytruth(pyjl(fill(nothing, 1, 2)))
@test pytruth(pyjl(fill(nothing, 1, 2, 3)))
end
end

@testitem "base" begin

end

@testitem "callback" begin

end

@testitem "dict" begin
@testset "type" begin
@test pyis(pytype(pyjl(Dict())), PythonCall.pyjldicttype)
end
@testset "bool" begin
@test !pytruth(pyjl(Dict()))
@test pytruth(pyjl(Dict("one"=>1, "two"=>2)))
end
end

@testitem "io" begin
@testset "type" begin
@test pyis(pytype(pyjl(devnull)), PythonCall.pyjlbinaryiotype)
@test pyis(pytype(pybinaryio(devnull)), PythonCall.pyjlbinaryiotype)
@test pyis(pytype(pytextio(devnull)), PythonCall.pyjltextiotype)
end
@testset "bool" begin
@test pytruth(pybinaryio(devnull))
@test pytruth(pytextio(devnull))
end
end

@testitem "iter" begin
x1 = [1,2,3,4,5]
x2 = pyjl(x1)
x3 = pylist(x2)
x4 = pyconvert(Vector{Int}, x3)
@test x1 == x4
end

@testitem "module" begin
@testset "type" begin
@test pyis(pytype(pyjl(PythonCall)), PythonCall.pyjlmoduletype)
end
@testset "bool" begin
@test pytruth(pyjl(PythonCall))
end
end

@testitem "number" begin
@testset "type" begin
@test pyis(pytype(pyjl(false)), PythonCall.pyjlintegertype)
@test pyis(pytype(pyjl(0)), PythonCall.pyjlintegertype)
@test pyis(pytype(pyjl(0//1)), PythonCall.pyjlrationaltype)
@test pyis(pytype(pyjl(0.0)), PythonCall.pyjlrealtype)
@test pyis(pytype(pyjl(Complex(0.0))), PythonCall.pyjlcomplextype)
end
@testset "bool" begin
@test !pytruth(pyjl(false))
@test !pytruth(pyjl(0))
@test !pytruth(pyjl(0//1))
@test !pytruth(pyjl(0.0))
@test !pytruth(pyjl(Complex(0.0)))
@test pytruth(pyjl(true))
@test pytruth(pyjl(3))
@test pytruth(pyjl(5//2))
@test pytruth(pyjl(2.3))
@test pytruth(pyjl(Complex(1.2, 3.4)))
end
end

@testitem "objectarray" begin

end

@testitem "raw" begin

end

@testitem "set" begin
@testset "type" begin
@test pyis(pytype(pyjl(Set())), PythonCall.pyjlsettype)
end
@testset "bool" begin
@test !pytruth(pyjl(Set()))
@test pytruth(pyjl(Set([1,2,3])))
end
end

@testitem "type" begin
@testset "type" begin
@test pyis(pytype(pyjl(Int)), PythonCall.pyjltypetype)
end
@testset "bool" begin
@test pytruth(pyjl(Int))
end
end

@testitem "vector" begin
@testset "type" begin
@test pyis(pytype(pyjl([1, 2, 3, 4])), PythonCall.pyjlvectortype)
end
@testset "bool" begin
@test !pytruth(pyjl([]))
@test pytruth(pyjl([1]))
@test pytruth(pyjl([1,2]))
end
end