def test_typed_struct_attributes(self): from hwp5.dataio import typed_struct_attributes class SomeRandomStruct(Struct): @staticmethod def attributes(): yield INT32, 'a' yield BSTR, 'b' yield ARRAY(INT32, 3), 'c' attributes = dict(a=1, b=u'abc', c=(4,5,6)) typed_attributes = typed_struct_attributes(SomeRandomStruct, attributes, dict()) typed_attributes = list(typed_attributes) expected = [dict(name='a', type=INT32, value=1), dict(name='b', type=BSTR, value='abc'), dict(name='c', type=ARRAY(INT32, 3), value=(4,5,6))] self.assertEquals(expected, typed_attributes)
def test_typed_struct_attributes(self): class SomeRandomStruct(Struct): @staticmethod def attributes(): yield INT32, 'a' yield BSTR, 'b' yield ARRAY(INT32, 3), 'c' attributes = dict(a=1, b=u'abc', c=(4, 5, 6)) typed_attributes = typed_struct_attributes(SomeRandomStruct, attributes, dict()) typed_attributes = list(typed_attributes) expected = [ dict(name='a', type=INT32, value=1), dict(name='b', type=BSTR, value='abc'), dict(name='c', type=ARRAY(INT32, 3), value=(4, 5, 6)) ] self.assertEqual(expected, typed_attributes)
def test_typed_struct_attributes_inherited(self): from hwp5.dataio import typed_struct_attributes class Hello(Struct): @staticmethod def attributes(): yield INT32, 'a' class Hoho(Hello): @staticmethod def attributes(): yield BSTR, 'b' attributes = dict(a=1, b=u'abc', c=(2, 2)) result = typed_struct_attributes(Hoho, attributes, dict()) result = list(result) expected = [dict(name='a', type=INT32, value=1), dict(name='b', type=BSTR, value='abc'), dict(name='c', type=tuple, value=(2, 2))] self.assertEquals(expected, result)
def test_typed_struct_attributes_inherited(self): class Hello(Struct): @staticmethod def attributes(): yield INT32, 'a' class Hoho(Hello): @staticmethod def attributes(): yield BSTR, 'b' attributes = dict(a=1, b=u'abc', c=(2, 2)) result = typed_struct_attributes(Hoho, attributes, dict()) result = list(result) expected = [dict(name='a', type=INT32, value=1), dict(name='b', type=BSTR, value='abc'), dict(name='c', type=tuple, value=(2, 2))] self.assertEquals(expected, result)
else: p[name] = item except Exception, e: logger.error('%s', (name, t, value)) logger.error('%s', t.__dict__) logger.exception(e) raise e return d, p def startelement(context, (model, attributes)): from hwp5.dataio import StructType if isinstance(model, StructType): typed_attributes = ( (v['name'], (v['type'], v['value'])) for v in typed_struct_attributes(model, attributes, context)) else: typed_attributes = ((k, (type(v), v)) for k, v in attributes.iteritems()) typed_attributes, plainvalues = separate_plainvalues(typed_attributes) if model is Text: text = plainvalues.pop('text')[1] elif '<text>' in plainvalues: text = plainvalues.pop('<text>')[1] else: text = None yield STARTEVENT, (model.__name__, xmlattributes_for_plainvalues(context, plainvalues))
d.append(named_item) else: p[name] = item except Exception, e: logger.error('%s', (name, t, value)) logger.error('%s', t.__dict__) logger.exception(e) raise e return d, p def startelement(context, (model, attributes)): from hwp5.dataio import StructType if isinstance(model, StructType): typed_attributes = ((v['name'], (v['type'], v['value'])) for v in typed_struct_attributes(model, attributes, context)) else: typed_attributes = ((k, (type(v), v)) for k, v in attributes.iteritems()) typed_attributes, plainvalues = separate_plainvalues(typed_attributes) if model is Text: text = plainvalues.pop('text')[1] elif '<text>' in plainvalues: text = plainvalues.pop('<text>')[1] else: text = None yield STARTEVENT, (model.__name__, xmlattributes_for_plainvalues(context, plainvalues))