From eb8d70a975689e71d0553ba7f29874cb41a8e870 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Fri, 22 Apr 2016 20:06:27 -0400 Subject: [PATCH] add BitArray conversions --- src/conversions.jl | 6 ++++-- src/numpy.jl | 5 ++--- test/runtests.jl | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/conversions.jl b/src/conversions.jl index 633e87be..2b4e0687 100644 --- a/src/conversions.jl +++ b/src/conversions.jl @@ -200,7 +200,7 @@ end """ PyVector(o::PyObject) -This returns a PyVector object, which is a wrapper around an arbitrary Python list or sequence object. +This returns a PyVector object, which is a wrapper around an arbitrary Python list or sequence object. Alternatively, `PyVector` can be used as the return type for a `pycall` that returns a sequence object (including tuples). """ @@ -381,6 +381,8 @@ end convert(::Type{Array}, o::PyObject) = py2array(PyAny, o) convert{T}(::Type{Array{T}}, o::PyObject) = py2array(T, o) +PyObject(a::BitArray) = PyObject(Array(a)) + # NumPy conversions (multidimensional arrays) include("numpy.jl") @@ -393,7 +395,7 @@ include("numpy.jl") This returns a PyDict, which is a no-copy wrapper around a Python dictionary. -Alternatively, you can specify the return type of a `pycall` as PyDict. +Alternatively, you can specify the return type of a `pycall` as PyDict. """ type PyDict{K,V} <: Associative{K,V} o::PyObject diff --git a/src/numpy.jl b/src/numpy.jl index 7ac69aca..f340ffc1 100644 --- a/src/numpy.jl +++ b/src/numpy.jl @@ -202,9 +202,8 @@ function PyObject{T<:NPY_TYPES}(a::StridedArray{T}) end end -function PyReverseDims{T<:NPY_TYPES}(a::StridedArray{T}) - return NpyArray(a, true) -end +PyReverseDims{T<:NPY_TYPES}(a::StridedArray{T}) = NpyArray(a, true) +PyReverseDims(a::BitArray) = PyReverseDims(Array(a)) """ PyReverseDims(array) diff --git a/test/runtests.jl b/test/runtests.jl index e711be7c..bdecfc53 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -54,12 +54,14 @@ testkw(x; y=0) = x + 2*y if PyCall.npy_initialized @test PyArray(PyObject([1. 2 3;4 5 6])) == [1. 2 3;4 5 6] - let A = rand(Int, 2,3,4) + let A = rand(Int, 2,3,4), B = rand(Bool, 2,3,4) @test convert(PyAny, PyReverseDims(A)) == permutedims(A, [3,2,1]) + @test convert(PyAny, PyReverseDims(BitArray(B))) == permutedims(B, [3,2,1]) end end @test PyVector(PyObject([1,3.2,"hello",true])) == [1,3.2,"hello",true] @test PyDict(PyObject(Dict(1 => "hello", 2 => "goodbye"))) == Dict(1 => "hello", 2 => "goodbye") +@test roundtripeq(BitArray([true, false, true, true])) let d = PyDict(Dict(1 => "hello", 34 => "yes" )) @test get(d.o, 1) == "hello"