def test_resolve_selective_type(self): @add_metaclass(StructType) class StructWithSelectiveType(object): @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=SelectiveType(ref_member('a'), {0: UINT16, 1: self.BasicStruct})) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithSelectiveType)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual(STARTEVENT, ev) self.assertEqual(StructWithSelectiveType, struct['type']) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=0) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((None, dict(name='c', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(struct, value=dict(a=0))), next(events)) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual(STARTEVENT, ev) self.assertEqual(StructWithSelectiveType, struct['type']) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=1) self.assertEqual((STARTEVENT, dict(name='b', type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(name='b', type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='c', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(struct, value=dict(a=1))), next(events))
def test_resolve_xarray(self): xarray_type = X_ARRAY(UINT16, ref_member('a')) class StructWithXArray(object): __metaclass__ = StructType @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=xarray_type) static_events = bintype_map_events(dict(type=StructWithXArray)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals((STARTEVENT, struct), (ev, struct)) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=0) self.assertEquals( (STARTEVENT, dict(name='b', count=0, type=xarray_type)), events.next()) self.assertEquals( (ENDEVENT, dict(name='b', count=0, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, struct), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals((STARTEVENT, struct), (ev, struct)) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=1) self.assertEquals( (STARTEVENT, dict(name='b', count=1, type=xarray_type)), events.next()) self.assertEquals((None, dict(type=UINT16)), events.next()) self.assertEquals( (ENDEVENT, dict(name='b', count=1, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, struct), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals((STARTEVENT, struct), (ev, struct)) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=2) self.assertEquals( (STARTEVENT, dict(name='b', count=2, type=xarray_type)), events.next()) self.assertEquals((None, dict(type=UINT16)), events.next()) self.assertEquals((None, dict(type=UINT16)), events.next()) self.assertEquals( (ENDEVENT, dict(name='b', count=2, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, struct), events.next())
def test_resolve_conditional_struct(self): def if_a_is_1(context, values): return values['a'] == 1 @add_metaclass(StructType) class StructWithCondition(object): @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=self.BasicStruct, condition=if_a_is_1) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithCondition)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual(STARTEVENT, ev) self.assertEqual(StructWithCondition, struct['type']) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=0) self.assertEqual((None, dict(name='c', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(struct, value=dict(a=0))), next(events)) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual(STARTEVENT, ev) self.assertEqual(StructWithCondition, struct['type']) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=1) self.assertEqual((STARTEVENT, dict(name='b', type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(name='b', type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='c', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(struct, value=dict(a=1))), next(events))
def test_resolve_conditional_simple(self): from hwp5.treeop import STARTEVENT, ENDEVENT from hwp5.dataio import StructType from hwp5.dataio import UINT16 from hwp5.bintype import bintype_map_events from hwp5.bintype import static_to_mutable from hwp5.bintype import resolve_typedefs def if_a_is_1(context, values): return values['a'] == 1 class StructWithCondition(object): __metaclass__ = StructType @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=UINT16, condition=if_a_is_1) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithCondition)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithCondition, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=0) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=0))), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithCondition, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=1) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=1))), events.next())
def test_resolve_selective_type(self): from hwp5.treeop import STARTEVENT, ENDEVENT from hwp5.dataio import StructType from hwp5.dataio import SelectiveType from hwp5.dataio import ref_member from hwp5.dataio import UINT16 from hwp5.bintype import bintype_map_events from hwp5.bintype import static_to_mutable from hwp5.bintype import resolve_typedefs class StructWithSelectiveType(object): __metaclass__ = StructType @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=SelectiveType(ref_member('a'), { 0: UINT16, 1: self.BasicStruct })) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithSelectiveType)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithSelectiveType, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=0) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=0))), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithSelectiveType, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=1) self.assertEquals((STARTEVENT, dict(name='b', type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(name='b', type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=1))), events.next())
def test_resolve_conditional_struct(self): from hwp5.treeop import STARTEVENT, ENDEVENT from hwp5.dataio import StructType from hwp5.dataio import UINT16 from hwp5.bintype import bintype_map_events from hwp5.bintype import static_to_mutable from hwp5.bintype import resolve_typedefs def if_a_is_1(context, values): return values['a'] == 1 class StructWithCondition(object): __metaclass__ = StructType @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=self.BasicStruct, condition=if_a_is_1) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithCondition)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithCondition, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=0) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=0))), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithCondition, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=1) self.assertEquals((STARTEVENT, dict(name='b', type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(name='b', type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=1))), events.next())
def test_resolve_selective_type(self): @add_metaclass(StructType) class StructWithSelectiveType(object): @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=SelectiveType(ref_member('a'), { 0: UINT16, 1: self.BasicStruct })) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithSelectiveType)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual(STARTEVENT, ev) self.assertEqual(StructWithSelectiveType, struct['type']) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=0) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((None, dict(name='c', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(struct, value=dict(a=0))), next(events)) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual(STARTEVENT, ev) self.assertEqual(StructWithSelectiveType, struct['type']) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=1) self.assertEqual((STARTEVENT, dict(name='b', type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(name='b', type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='c', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(struct, value=dict(a=1))), next(events))
def test_resolve_selective_type(self): from hwp5.treeop import STARTEVENT, ENDEVENT from hwp5.dataio import StructType from hwp5.dataio import SelectiveType from hwp5.dataio import ref_member from hwp5.dataio import UINT16 from hwp5.bintype import bintype_map_events from hwp5.bintype import static_to_mutable from hwp5.bintype import resolve_typedefs class StructWithSelectiveType(object): __metaclass__ = StructType @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=SelectiveType(ref_member('a'), {0: UINT16, 1: self.BasicStruct})) yield UINT16, 'c' static_events = bintype_map_events(dict(type=StructWithSelectiveType)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithSelectiveType, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=0) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=0))), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals(STARTEVENT, ev) self.assertEquals(StructWithSelectiveType, struct['type']) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=1) self.assertEquals((STARTEVENT, dict(name='b', type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(name='b', type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='c', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(struct, value=dict(a=1))), events.next())
def test_resolve_xarray_struct(self): from hwp5.treeop import STARTEVENT, ENDEVENT from hwp5.dataio import StructType from hwp5.dataio import X_ARRAY from hwp5.dataio import UINT16 from hwp5.dataio import ref_member from hwp5.bintype import bintype_map_events from hwp5.bintype import static_to_mutable from hwp5.bintype import resolve_typedefs xarray_type = X_ARRAY(self.BasicStruct, ref_member('a')) class StructWithXArray(object): __metaclass__ = StructType @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=xarray_type) static_events = bintype_map_events(dict(type=StructWithXArray)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals((STARTEVENT, struct), (ev, struct)) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=0) self.assertEquals((STARTEVENT, dict(name='b', count=0, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, dict(name='b', count=0, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, struct), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals((STARTEVENT, struct), (ev, struct)) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=1) self.assertEquals((STARTEVENT, dict(name='b', count=1, type=xarray_type)), events.next()) self.assertEquals((STARTEVENT, dict(type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(type=self.BasicStruct)), events.next()) self.assertEquals((ENDEVENT, dict(name='b', count=1, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, struct), events.next()) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = events.next() self.assertEquals((STARTEVENT, struct), (ev, struct)) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) struct['value'] = dict(a=2) self.assertEquals((STARTEVENT, dict(name='b', count=2, type=xarray_type)), events.next()) self.assertEquals((STARTEVENT, dict(type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(type=self.BasicStruct)), events.next()) self.assertEquals((STARTEVENT, dict(type=self.BasicStruct)), events.next()) self.assertEquals((None, dict(name='a', type=UINT16)), events.next()) self.assertEquals((None, dict(name='b', type=UINT16)), events.next()) self.assertEquals((ENDEVENT, dict(type=self.BasicStruct)), events.next()) self.assertEquals((ENDEVENT, dict(name='b', count=2, type=xarray_type)), events.next()) self.assertEquals((ENDEVENT, struct), events.next())
def test_resolve_xarray_struct(self): xarray_type = X_ARRAY(self.BasicStruct, ref_member('a')) @add_metaclass(StructType) class StructWithXArray(object): @staticmethod def attributes(): yield UINT16, 'a' yield dict(name='b', type=xarray_type) static_events = bintype_map_events(dict(type=StructWithXArray)) static_events = list(static_events) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual((STARTEVENT, struct), (ev, struct)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=0) self.assertEqual((STARTEVENT, dict(name='b', count=0, type=xarray_type)), next(events)) self.assertEqual((ENDEVENT, dict(name='b', count=0, type=xarray_type)), next(events)) self.assertEqual((ENDEVENT, struct), next(events)) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual((STARTEVENT, struct), (ev, struct)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=1) self.assertEqual((STARTEVENT, dict(name='b', count=1, type=xarray_type)), next(events)) self.assertEqual((STARTEVENT, dict(type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(type=self.BasicStruct)), next(events)) self.assertEqual((ENDEVENT, dict(name='b', count=1, type=xarray_type)), next(events)) self.assertEqual((ENDEVENT, struct), next(events)) events = static_to_mutable(iter(static_events)) events = resolve_typedefs(events, dict()) ev, struct = next(events) self.assertEqual((STARTEVENT, struct), (ev, struct)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) struct['value'] = dict(a=2) self.assertEqual((STARTEVENT, dict(name='b', count=2, type=xarray_type)), next(events)) self.assertEqual((STARTEVENT, dict(type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(type=self.BasicStruct)), next(events)) self.assertEqual((STARTEVENT, dict(type=self.BasicStruct)), next(events)) self.assertEqual((None, dict(name='a', type=UINT16)), next(events)) self.assertEqual((None, dict(name='b', type=UINT16)), next(events)) self.assertEqual((ENDEVENT, dict(type=self.BasicStruct)), next(events)) self.assertEqual((ENDEVENT, dict(name='b', count=2, type=xarray_type)), next(events)) self.assertEqual((ENDEVENT, struct), next(events))