File tree 2 files changed +47
-1
lines changed
2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import numbers
4
4
from abc import abstractmethod
5
+ from collections .abc import Iterator , Mapping
5
6
from typing import Any , Iterable , NamedTuple
6
7
7
8
import numpy as np
@@ -639,7 +640,7 @@ def to_integer_type(self):
639
640
640
641
641
642
@public
642
- class Struct (DataType ):
643
+ class Struct (DataType , Mapping ):
643
644
"""Structured values."""
644
645
645
646
fields = frozendict_of (instance_of (str ), datatype )
@@ -677,6 +678,12 @@ def types(self) -> tuple[DataType, ...]:
677
678
"""Return the types of the struct's fields."""
678
679
return tuple (self .fields .values ())
679
680
681
+ def __len__ (self ) -> int :
682
+ return len (self .fields )
683
+
684
+ def __iter__ (self ) -> Iterator [str ]:
685
+ return iter (self .fields )
686
+
680
687
def __getitem__ (self , key : str ) -> DataType :
681
688
return self .fields [key ]
682
689
Original file line number Diff line number Diff line change @@ -141,6 +141,45 @@ def test_struct_with_string_types():
141
141
)
142
142
143
143
144
+ def test_struct_mapping_api ():
145
+ s = dt .Struct (
146
+ {
147
+ 'a' : 'map<double, string>' ,
148
+ 'b' : 'array<map<string, array<int32>>>' ,
149
+ 'c' : 'array<string>' ,
150
+ 'd' : 'int8' ,
151
+ }
152
+ )
153
+
154
+ assert s ['a' ] == dt .Map (dt .double , dt .string )
155
+ assert s ['b' ] == dt .Array (dt .Map (dt .string , dt .Array (dt .int32 )))
156
+ assert s ['c' ] == dt .Array (dt .string )
157
+ assert s ['d' ] == dt .int8
158
+
159
+ assert 'a' in s
160
+ assert 'e' not in s
161
+ assert len (s ) == 4
162
+ assert tuple (s ) == s .names
163
+ assert tuple (s .keys ()) == s .names
164
+ assert tuple (s .values ()) == s .types
165
+ assert tuple (s .items ()) == tuple (zip (s .names , s .types ))
166
+
167
+ s1 = s .copy ()
168
+ s2 = dt .Struct (
169
+ {
170
+ 'a' : 'map<double, string>' ,
171
+ 'b' : 'array<map<string, array<int32>>>' ,
172
+ 'c' : 'array<string>' ,
173
+ }
174
+ )
175
+ assert s == s1
176
+ assert s != s2
177
+
178
+ # doesn't support item assignment
179
+ with pytest .raises (TypeError ):
180
+ s ['e' ] = dt .int8
181
+
182
+
144
183
@pytest .mark .parametrize (
145
184
'case' ,
146
185
[
You can’t perform that action at this time.
0 commit comments