def csv_convert(self, val, checker=None, **kw): if type(val) != list: val = [val] elif len(val) == 0: return '' import csv buf = io.StringIO() if isinstance(val, pandas_df_type): val.to_csv(buf, **keep(kw, [])) else: val = self.schema_convert(val, checker) t = val[0] if isinstance(t, Mapping): if checker: checker(val) w = csv.DictWriter(buf, sorted(t.keys()), lineterminator='\n', **keep(kw, [])) w.writeheader() w.writerows(val) elif isinstance(t, Iterable): if checker: checker(val) w = csv.writer(buf, lineterminator='\n', **keep(kw, [])) w.writerows(val) else: raise errors.BadRequest(type=t.__name__, status='unknown') return buf.getvalue()
def msgpack_convert(self, val, checker=None, **kw): if pandas_df_type and isinstance(val, pandas_df_type): buf = io.BytesIO() val.to_msgpack(buf, **keep(kw, [])) return buf.getvalue() val = self.schema_convert(val, checker=checker) import msgpack return msgpack.dumps(val, **keep(kw, []))
def json_convert(self, val, checker=None, **kw): import json if pandas_df_type and isinstance(val, pandas_df_type): buf = io.StringIO() val.to_json(buf, **keep(kw, [])) res = buf.getvalue() return res val = self.schema_convert(val, checker=checker) return json.dumps(val, cls=JSONEncoder2, **keep(kw, []))
def msgpack_revert(self, val, cls=dict, checker=None, **kw): if cls == pandas_df_type.__name__ or cls == pandas_df_type: import pandas if isinstance(val, requests.Response): return pandas.read_msgpack(val.content, **keep(kw, [])) else: return pandas.read_msgpack(io.BytesIO(val), **keep(kw, [])) if isinstance(val, requests.Response): val = val.content import msgpack val = msgpack.loads(val, **keep(kw, [])) val = self.schema_revert(val, cls, checker=checker) return val
def pickle_revert(self, val, cls=dict, checker=None, **kw): if isinstance(val, requests.Response): val = val.text import pickle val = pickle.loads(val, **keep(kw, [])) val = self.schema_revert(val, cls, checker=checker) return val
def xml_revert(self, val, cls, checker=None, **kw): from xml.dom import minidom def revert_node(n): res = {} txt = '' for c in n.childNodes: if c.nodeType == c.TEXT_NODE: txt += c.data else: e = revert_node(c) if c.hasAttribute('type'): e = self.str_revert(e, c.getAttribute('type'), False) if c.nodeName in res: v = res[c.nodeName] if type(v) == list: v.append(e) else: res[c.nodeName] = [v, e] else: res[c.nodeName] = e if len(res) > 0: return res return txt if isinstance(val, requests.Response): val = val.text elif isinstance(val, bytes): val = val.decode(**keep(kw, [])) elif not isinstance(val, str): raise errors.BadRequest(type=type(val).__name__, status='unknown') res = revert_node(minidom.parse(io.StringIO(val)).firstChild) return self.schema_revert(res, cls, checker=checker)
def json_revert(self, val, cls=dict, checker=None, **kw): if cls == pandas_df_type.__name__ or cls == pandas_df_type: import pandas if isinstance(val, requests.Response): return pandas.read_json(val.content, **keep(kw, [])) elif isinstance(val, bytes): return pandas.read_json(io.BytesIO(val), **keep(kw, [])) elif isinstance(val, str): return pandas.read_json(io.StringIO(val), **keep(kw, [])) else: raise errors.BadRequest(type=type(val).__name__, status='unknown') if isinstance(val, requests.Response): val = val.text import json val = json.loads(val, **keep(kw, ['encoding'])) val = self.schema_revert(val, cls, checker=checker) return val
def csv_revert(self, val, cls=dict, checker=None, **kw): if cls == pandas_df_type or cls == pandas_df_type.__name__: import pandas if isinstance(val, requests.Response): return pandas.read_csv(val.content, **keep(kw, [])) elif isinstance(val, bytes): return pandas.read_csv(io.BytesIO(val), **keep(kw, [])) else: return pandas.read_csv(io.StringIO(val), **keep(kw, [])) import csv if isinstance(val, requests.Response): inp = val.iter_content() elif isinstance(val, bytes): inp = io.StringIO(val.decode(**keep(kw, []))) else: inp = io.StringIO(val) data = list(dict(x) for x in csv.DictReader(inp, delimiter=',', **kw)) data = self.schema_revert(data, cls, checker=checker) return data
def pickle_convert(self, val, checker=None, **kw): val = self.schema_convert(val, checker) import pickle return pickle.dumps(val, **keep(kw, []))