def build_object(cls, parent, name, value): e = ET.Element(name) if isinstance(value, datetime): e.text = DateTime(value).as_utc().strftime('%Y-%m-%dT%H:%M:%SZ') elif isinstance(value, StringTypes): e.text = value elif isinstance(value, data): e.text = value.encode() elif isinstance(value, (int, float, long)): parent.set(name, unicode(value)) return elif isinstance(value, DictType): for k in value: cls.build_object(e, k, value[k]) elif isinstance(value, Entity): value = value.to_dict() for k in value: cls.build_object(e, k, value[k]) elif isinstance(value, (list, tuple)): item_name = inflection.singularize(name) for v in value: if isinstance(v, (int, float, long)): v = unicode(v) elif isinstance(v, bool): v = unicode(v).lower() cls.build_object(parent, item_name, v) return elif value is not None: e.text = unicode(value) parent.append(e)
def build_object(cls, parent, name, value, set_key=True): e = ET.Element(name) if isinstance(value, datetime): e = ET.Element(T_DATE) e.text = DateTime(value).as_utc().strftime('%Y-%m-%dT%H:%M:%SZ') elif isinstance(value, data): e = ET.Element(T_DATA) e.text = value.encode() elif isinstance(value, float): e = ET.Element(T_FLOAT) e.text = unicode(value) elif isinstance(value, bool): if value: e = ET.Element(T_TRUE) else: e = ET.Element(T_FALSE) elif isinstance(value, (int, long)): e = ET.Element(T_INT) e.text = unicode(value) elif value is None: e = ET.Element(T_NULL) elif isinstance(value, DictType): e = ET.Element(T_DICT) for k in value: cls.build_object(e, k, value[k]) elif isinstance(value, Entity): e = ET.Element(T_DICT) value = value.to_dict() for k in value: cls.build_object(e, k, value[k]) elif isinstance(value, (list, tuple)): e = ET.Element(T_ARRAY) item_tag = inflection.singularize(name) for v in value: cls.build_object(e, item_tag, v, False) else: e = ET.Element(T_STRING) e.text = unicode(value) if set_key: e.set('k', name) parent.append(e)