21
21
DBMStore , LMDBStore , atexit_rmtree , atexit_rmglob )
22
22
from zarr .core import Array
23
23
from zarr .errors import PermissionError
24
- from zarr .compat import PY2
24
+ from zarr .compat import PY2 , text_type , binary_type
25
25
from zarr .util import buffer_size
26
26
from numcodecs import (Delta , FixedScaleOffset , Zlib , Blosc , BZ2 , MsgPack , Pickle ,
27
27
Categorize , JSON , VLenUTF8 , VLenBytes , VLenArray )
@@ -941,6 +941,13 @@ def test_object_arrays_vlen_text(self):
941
941
z [:] = data
942
942
assert_array_equal (data , z [:])
943
943
944
+ # convenience API
945
+ z = self .create_array (shape = data .shape , dtype = text_type )
946
+ assert z .dtype == object
947
+ assert isinstance (z .filters [0 ], VLenUTF8 )
948
+ z [:] = data
949
+ assert_array_equal (data , z [:])
950
+
944
951
z = self .create_array (shape = data .shape , dtype = object , object_codec = MsgPack ())
945
952
z [:] = data
946
953
assert_array_equal (data , z [:])
@@ -967,6 +974,13 @@ def test_object_arrays_vlen_bytes(self):
967
974
z [:] = data
968
975
assert_array_equal (data , z [:])
969
976
977
+ # convenience API
978
+ z = self .create_array (shape = data .shape , dtype = binary_type )
979
+ assert z .dtype == object
980
+ assert isinstance (z .filters [0 ], VLenBytes )
981
+ z [:] = data
982
+ assert_array_equal (data , z [:])
983
+
970
984
z = self .create_array (shape = data .shape , dtype = object , object_codec = Pickle ())
971
985
z [:] = data
972
986
assert_array_equal (data , z [:])
@@ -977,18 +991,29 @@ def test_object_arrays_vlen_array(self):
977
991
np .array ([5 ]),
978
992
np .array ([2 , 8 , 12 ])] * 1000 , dtype = object )
979
993
994
+ def compare_arrays (expected , actual , item_dtype ):
995
+ assert isinstance (actual , np .ndarray )
996
+ assert actual .dtype == object
997
+ assert actual .shape == expected .shape
998
+ for e , a in zip (expected .flat , actual .flat ):
999
+ assert isinstance (a , np .ndarray )
1000
+ assert_array_equal (e , a )
1001
+ assert a .dtype == item_dtype
1002
+
980
1003
codecs = VLenArray (int ), VLenArray ('<u4' )
981
1004
for codec in codecs :
982
1005
z = self .create_array (shape = data .shape , dtype = object , object_codec = codec )
983
1006
z [:] = data
984
- a = z [:]
985
- assert isinstance (a , np .ndarray )
986
- assert a .dtype == object
987
- assert a .shape == data .shape
988
- for expected , actual in zip (data .flat , a .flat ):
989
- assert isinstance (actual , np .ndarray )
990
- assert_array_equal (expected , actual )
991
- assert actual .dtype == codec .dtype
1007
+ compare_arrays (data , z [:], codec .dtype )
1008
+
1009
+ # convenience API
1010
+ for item_type in 'int' , '<u4' :
1011
+ z = self .create_array (shape = data .shape , dtype = 'array:{}' .format (item_type ))
1012
+ assert z .dtype == object
1013
+ assert isinstance (z .filters [0 ], VLenArray )
1014
+ assert z .filters [0 ].dtype == np .dtype (item_type )
1015
+ z [:] = data
1016
+ compare_arrays (data , z [:], np .dtype (item_type ))
992
1017
993
1018
def test_object_arrays_danger (self ):
994
1019
0 commit comments