def transformBack(obj): if type(obj) == dict: out = {} for k, v in obj.items(): out[str(k)] = transformBack(v) if "type" in out: if out['type'] == "Plot" \ or out['type'] == "TimePlot" \ or out['type'] == "NanoPlot" \ or out['type'] == "SimpleTimePlot" \ or out['type'] == "CombinedPlot": return chart.transformBack(out) if out['type'] == 'EasyForm': return easyform.transformBack(out) if out['type'] == "BeakerCodeCell": c = BeakerCodeCell(out['cellId'], out['evaluatorId']) if 'code' in out: c.code = out['code'] if 'outputtype' in out: c.outputtype = out['outputtype'] if 'output' in out: c.output = transformBack(out['output']) if 'tags' in out: c.tags = out['tags'] return c if out['type'] == "OutputContainer": c = OutputContainer() if 'items' in out: for i in out['items']: c.addItem(i) return c if out['type'] == "Date": return datetime.fromtimestamp(out["timestamp"] / 1000) if out['type'] == "TableDisplay": if 'subtype' in out: if out['subtype'] == "Dictionary": out2 = {} for r in out['values']: out2[r[0]] = fixNaNBack(r[1]) if out['columnNames'][0] == "Index": return pandas.Series(out2) return out2 if out['subtype'] == "Matrix": vals = out['values'] fixNaNsBack(vals) return numpy.matrix(vals) if out['subtype'] == "ListOfMaps": out2 = [] cnames = out['columnNames'] for r in out['values']: out3 = {} for i in range(len(cnames)): if r[i] != '': out3[cnames[i]] = r[i] out2.append(out3) return out2 # transform to dataframe if ('hasIndex' in out) and (out['hasIndex'] == "true"): # first column becomes the index vals = out['values'] is_standard_index = ('indexName' in out) and (len(out['indexName']) == 1) and (out['indexName'][0] == 'index') cnames = out['columnNames'][1:] index = [] for x in range(0, len(vals)): index.append(transformBack(vals[x][0])) v = vals[x][1:] fixNaNsBack(v) vals[x] = v if len(out['indexName']) > 1: index = pandas.MultiIndex.from_tuples(index, names=out['indexName']) else: index = pandas.Index(index, name=out['indexName']) frame = pandas.DataFrame(data=vals, columns=cnames, index=index) return frame else: vals = out['values'] cnames = out['columnNames'] for x in range(0, len(vals)): v = vals[x] fixNaNsBack(v) vals[x] = v return pandas.DataFrame(data=vals, columns=cnames) return out if type(obj) == list: out = [] for v in obj: out.append(transformBack(v)) return out try: if type(obj) == bytes: obj = str(obj) except Exception as e: return obj return obj
def transformBack(obj): if type(obj) == dict: out = {} for k, v in obj.items(): out[str(k)] = transformBack(v) if "type" in out: if out['type'] == "Plot" \ or out['type'] == "TimePlot" \ or out['type'] == "NanoPlot" \ or out['type'] == "SimpleTimePlot" \ or out['type'] == "CombinedPlot": return chart.transformBack(out) if out['type'] == 'EasyForm': return easyform.transformBack(out) if out['type'] == "BeakerCodeCell": c = BeakerCodeCell(out['cellId'], out['evaluatorId']) if 'code' in out: c.code = out['code'] if 'outputtype' in out: c.outputtype = out['outputtype'] if 'output' in out: c.output = transformBack(out['output']) if 'tags' in out: c.tags = out['tags'] return c if out['type'] == "OutputContainer": c = OutputContainer() if 'items' in out: for i in out['items']: c.addItem(i) return c if out['type'] == "Date": return datetime.datetime.fromtimestamp(out["timestamp"] / 1000) if out['type'] == "TableDisplay": if 'subtype' in out: if out['subtype'] == "Dictionary": out2 = {} for r in out['values']: out2[r[0]] = fixNaNBack(r[1]) if out['columnNames'][0] == "Index": return pandas.Series(out2) return out2 if out['subtype'] == "Matrix": vals = out['values'] fixNaNsBack(vals) return numpy.matrix(vals) if out['subtype'] == "ListOfMaps": out2 = [] cnames = out['columnNames'] for r in out['values']: out3 = {} for i in range(len(cnames)): if r[i] != '': out3[cnames[i]] = r[i] out2.append(out3) return out2 # transform to dataframe if ('hasIndex' in out) and (out['hasIndex'] == "true"): # first column becomes the index vals = out['values'] cnames = out['columnNames'][1:] index = [] for x in range(0, len(vals)): index.append(transformBack(vals[x][0])) v = vals[x][1:] fixNaNsBack(v) vals[x] = v return pandas.DataFrame(data=vals, columns=cnames, index=index) else: vals = out['values'] cnames = out['columnNames'] for x in range(0, len(vals)): v = vals[x] fixNaNsBack(v) vals[x] = v return pandas.DataFrame(data=vals, columns=cnames) return out if type(obj) == list: out = [] for v in obj: out.append(transformBack(v)) return out try: if type(obj) == bytes: obj = str(obj) except Exception as e: return obj return obj