def str_or_none(val): """Attempt to stringify a value, or None.""" if val is None: return val elif isinstance(val, basestring): return strutils.safe_encode(val) else: return str(val)
def _render_data(self, xml, el_name, data): el_name = _underscore_to_xmlcase(el_name) data_el = xml.createElement(el_name) if isinstance(data, list): for item in data: data_el.appendChild(self._render_data(xml, 'item', item)) elif isinstance(data, dict): self._render_dict(xml, data_el, data) elif hasattr(data, '__dict__'): self._render_dict(xml, data_el, data.__dict__) elif isinstance(data, bool): data_el.appendChild(xml.createTextNode(str(data).lower())) elif isinstance(data, datetime.datetime): data_el.appendChild( xml.createTextNode(_database_to_isoformat(data))) elif data is not None: data_el.appendChild( xml.createTextNode(strutils.safe_encode(six.text_type(data)))) return data_el
def _render_data(self, xml, el_name, data): el_name = _underscore_to_xmlcase(el_name) data_el = xml.createElement(el_name) if isinstance(data, list): for item in data: data_el.appendChild(self._render_data(xml, 'item', item)) elif isinstance(data, dict): self._render_dict(xml, data_el, data) elif hasattr(data, '__dict__'): self._render_dict(xml, data_el, data.__dict__) elif isinstance(data, bool): data_el.appendChild(xml.createTextNode(str(data).lower())) elif isinstance(data, datetime.datetime): data_el.appendChild( xml.createTextNode(_database_to_isoformat(data))) elif data is not None: data_el.appendChild(xml.createTextNode( strutils.safe_encode(six.text_type(data)))) return data_el