Skip to content

Commit f0c5eb1

Browse files
committed
use temporary file for sql query results
1 parent 468ffe8 commit f0c5eb1

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

mapswipe_workers/mapswipe_workers/generate_stats/project_stats.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
from typing import List
44
import gzip
5-
import io
5+
import tempfile
66

77
import pandas as pd
88
from psycopg2 import sql
@@ -28,22 +28,20 @@ def add_metadata_to_csv(filename: str):
2828
logger.info(f"added metadata to {filename}.")
2929

3030

31-
def write_sql_to_csv(filename: str, sql_query: sql.SQL):
31+
def write_sql_to_gzipped_csv(filename: str, sql_query: sql.SQL):
3232
"""
3333
Use the copy statement to write data from postgres to a csv file.
3434
"""
3535

36-
temp_file = "temp.csv"
36+
# generate temporary file which will be automatically deleted at the end
37+
tmp_csv_file = os.path.join(tempfile._get_default_tempdir(), 'tmp.csv')
3738
pg_db = auth.postgresDB()
38-
with open(temp_file, "w") as f:
39+
with open(tmp_csv_file, "w") as f:
3940
pg_db.copy_expert(sql_query, f)
4041

41-
with open(temp_file, 'rb') as f_in, gzip.open(filename, 'wb') as f_out:
42+
with open(tmp_csv_file, 'rb') as f_in, gzip.open(filename, 'wb') as f_out:
4243
f_out.writelines(f_in)
4344

44-
# remove temp file
45-
os.remove(temp_file)
46-
4745
logger.info(f"wrote gzipped csv file from sql: {filename}")
4846

4947

@@ -87,7 +85,7 @@ def get_results(filename: str, project_id: str) -> pd.DataFrame:
8785
) TO STDOUT WITH CSV HEADER
8886
"""
8987
).format(sql.Literal(project_id))
90-
write_sql_to_csv(filename, sql_query)
88+
write_sql_to_gzipped_csv(filename, sql_query)
9189

9290
df = load_df_from_csv(filename)
9391

@@ -131,7 +129,7 @@ def get_tasks(filename: str, project_id: str) -> pd.DataFrame:
131129
) TO STDOUT WITH CSV HEADER
132130
"""
133131
).format(sql.Literal(project_id))
134-
write_sql_to_csv(filename, sql_query)
132+
write_sql_to_gzipped_csv(filename, sql_query)
135133

136134
df = load_df_from_csv(filename)
137135
return df
@@ -166,7 +164,7 @@ def get_groups(filename: str, project_id: str) -> pd.DataFrame:
166164
) TO STDOUT WITH CSV HEADER
167165
"""
168166
).format(sql.Literal(project_id))
169-
write_sql_to_csv(filename, sql_query)
167+
write_sql_to_gzipped_csv(filename, sql_query)
170168

171169
df = load_df_from_csv(filename)
172170
return df

mapswipe_workers/mapswipe_workers/utils/geojson_functions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import shutil
55
import subprocess
66
import tempfile
7-
from pathlib import Path
87

98
from osgeo import ogr, osr
109

@@ -22,7 +21,7 @@ def gzipped_csv_to_gzipped_geojson(
2221
Then the unzipped csv file is converted into a geojson file with ogr2ogr.
2322
Last, the generated geojson file is again compressed using gzip.
2423
"""
25-
# generate tempory files which will be automatically deleted at the end
24+
# generate temporary files which will be automatically deleted at the end
2625
tmp_csv_file = os.path.join(tempfile._get_default_tempdir(), 'tmp.csv')
2726
tmp_geojson_file = os.path.join(tempfile._get_default_tempdir(), 'tmp.geojson')
2827

0 commit comments

Comments
 (0)