def test_unicode_with_nones(self): file = table2spss.table2sav(self.unicode_with_none_table) pspp = subprocess.Popen( ["pspp", "-b"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) input = self.get_pspp_command(file) stdout, stderr = pspp.communicate(input=input, timeout=30) self.assertIn(b"N is 4.", stdout)
def run(self, form): selection = SelectionSearch(form) queries = selection.get_article_ids_per_query() if form.cleaned_data["output_type"] == "application/json+clustermap": clusters, articles = zip(*get_clusters(queries).items()) cluster_queries = get_cluster_queries(clusters) image, html = get_clustermap_image(queries) coords = tuple(clustermap_html_to_coords(html)) return json.dumps({ "coords": coords, "image": b64encode(image).decode("ascii"), "clusters": [{ "query": q, "articles": tuple(a) } for q, a in zip(cluster_queries, articles)] }) headers, rows = get_clustermap_table(queries) if form.cleaned_data["output_type"] == "application/spss-sav": # *sigh*.. this code is fugly. _headers = {str(h): i for i, h in enumerate(headers)} return table2sav( Table(rows=list(rows), columns=list(map(str, headers)), columnTypes=[int] * len(headers), cellfunc=lambda row, col: row[_headers[col]])) dialect = 'excel' if form.cleaned_data["output_type"] == "text/csv+tab": dialect = 'excel-tab' result = StringIO() csvf = csv.writer(result, dialect=dialect) csvf.writerow(list(map(str, headers))) csvf.writerows(sorted(rows)) if form.cleaned_data[ "output_type"] == "application/json+clustermap+table": return json.dumps({ "csv": result.getvalue(), "queries": {q.label: q.query for q in queries} }) return result.getvalue()
def run(self, form): selection = SelectionSearch.get_instance(form) queries = selection.get_article_ids_per_query() if form.cleaned_data["output_type"] == "application/json+clustermap": try: clusters, articles = zip(*get_clusters(queries).items()) except ValueError as e: raise ValueError("Cannot build clustermap of empty query result.") cluster_queries = get_cluster_queries(clusters) image, html = get_clustermap_image(queries) coords = tuple(clustermap_html_to_coords(html)) return json.dumps( {"coords": coords, "image": b64encode(image).decode("ascii"), "clusters": [ {"query": q, "articles": tuple(a)} for q, a in zip(cluster_queries, articles) ]} ) headers, rows = get_clustermap_table(queries) if form.cleaned_data["output_type"] == "application/spss-sav": # *sigh*.. this code is fugly. _headers = {str(h): i for i, h in enumerate(headers)} return table2sav(Table( rows=list(rows), columns=list(map(str, headers)), columnTypes=[int]*len(headers), cellfunc=lambda row, col: row[_headers[col]] )) dialect = 'excel' if form.cleaned_data["output_type"] == "text/csv+tab": dialect = 'excel-tab' result = StringIO() csvf = csv.writer(result, dialect=dialect) csvf.writerow(list(map(str, headers))) csvf.writerows(sorted(rows)) if form.cleaned_data["output_type"] == "application/json+clustermap+table": return json.dumps({ "csv": result.getvalue(), "queries": {q.label: q.query for q in queries} }) return result.getvalue()
def test_date_hack(self): data = [[datetime.datetime(2020, 9, 8, 7, 6, 5).isoformat()]] table = table3.ListTable(columnTypes=[str], colnames=["date"], data=data) file = table2spss.table2sav(table) pspp = subprocess.Popen( ["pspp", "-b"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) input = self.get_pspp_command(file) stdout, stderr = pspp.communicate(input=input) self.assertIn(b"07-SEP-2020 16:53:55", stdout)
def test_asciitable2sav(self): file = table2spss.table2sav(self.ascii_table) pspp = subprocess.Popen( ["pspp", "-b"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) input = self.get_pspp_command(file) stdout, stderr = pspp.communicate(input=input, timeout=30) self.assertIn(b"N is 3.", stdout) self.assertIn(b"74321", stdout)
def test_unitable2sav(self): file = table2spss.table2sav(self.unicode_table) pspp = subprocess.Popen( ["pspp", "-b"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) input = self.get_pspp_command(file) stdout, stderr = pspp.communicate(input=input, timeout=30) self.assertIn(b"N is 4.", stdout) self.assertIn(b"31-DEC-2020 00:00:00", stdout) self.assertIn(b"06-JUL-2015 00:00:00", stdout) self.assertIn(b"04-MAY-2010 00:00:00", stdout) self.assertIn(b"74321", stdout) self.assertIn("♜".encode("utf-8"), stdout) self.assertIn("♝".encode("utf-8"), stdout) self.assertIn("✄".encode("utf-8"), stdout)
def run(self, tableObj): filename = table2spss.table2sav(tableObj) data = open(filename, 'rb').read() log.info(filename) return data
def test_table2sav(self): print("".join(table2spss.table2spss(self.ascii_table, 'abc'))) print(table2spss.table2sav(self.ascii_table))
def run(self, tableObj): filename = table2spss.table2sav(tableObj) data = open(filename, "rb").read() log.info(filename) return data