Skip to content

Commit f3f5f07

Browse files
committed
loading float values if exists
1 parent 02cf026 commit f3f5f07

File tree

4 files changed

+74
-18
lines changed

4 files changed

+74
-18
lines changed

functions-python/helpers/transform.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,40 @@ def to_enum(value, enum_class=None, default_value=None):
7777
except (ValueError, TypeError) as e:
7878
logging.warning("Failed to convert value to enum member: %s", e)
7979
return default_value
80+
81+
82+
def to_float(value, default_value: Optional[float] = None) -> Optional[float]:
83+
"""
84+
Convert a value to a float. If conversion fails, return the default value.
85+
"""
86+
try:
87+
return float(value)
88+
except (ValueError, TypeError):
89+
return default_value
90+
91+
92+
def get_safe_value(row, column_name, default_value):
93+
"""
94+
Get a safe value from the row. If the value is missing or empty, return the default value.
95+
"""
96+
import pandas
97+
98+
value = row.get(column_name, None)
99+
if not value or pandas.isna(value) or f"{value}".strip() == "":
100+
return default_value
101+
return f"{value}".strip()
102+
103+
104+
def get_safe_float(row, column_name, default_value=None):
105+
"""
106+
Get a safe float value from the row. If the value is missing or cannot be converted to float,
107+
"""
108+
import pandas
109+
110+
value = row.get(column_name, None)
111+
if not value or pandas.isna(value) or f"{value}".strip() == "":
112+
return default_value
113+
try:
114+
return float(value)
115+
except (ValueError, TypeError):
116+
return default_value

functions-python/helpers/verifier_common.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def download_to_local(
5050
data = BytesIO(response.content)
5151
blob.upload_from_file(data, rewind=True)
5252
else:
53-
logging.info(f"Blob already exists: gs://{EMULATOR_STORAGE_BUCKET_NAME}/{blob_path}")
53+
logging.info(
54+
f"Blob already exists: gs://{EMULATOR_STORAGE_BUCKET_NAME}/{blob_path}"
55+
)
5456

5557

5658
@with_db_session
@@ -87,24 +89,29 @@ def setup_local_storage_emulator():
8789
"""
8890
from gcp_storage_emulator.server import create_server
8991

90-
os.environ["STORAGE_EMULATOR_HOST"] = f"http://{EMULATOR_HOST}:{EMULATOR_STORAGE_PORT}"
92+
os.environ[
93+
"STORAGE_EMULATOR_HOST"
94+
] = f"http://{EMULATOR_HOST}:{EMULATOR_STORAGE_PORT}"
9195
os.environ["DATASETS_BUCKET_NAME_GBFS"] = EMULATOR_STORAGE_BUCKET_NAME
9296
os.environ["DATASETS_BUCKET_NAME_GTFS"] = EMULATOR_STORAGE_BUCKET_NAME
9397
os.environ["DATASTORE_EMULATOR_HOST"] = "localhost:8081"
9498
server = create_server(
95-
host=EMULATOR_HOST, port=EMULATOR_STORAGE_PORT, in_memory=False, default_bucket=EMULATOR_STORAGE_BUCKET_NAME
99+
host=EMULATOR_HOST,
100+
port=EMULATOR_STORAGE_PORT,
101+
in_memory=False,
102+
default_bucket=EMULATOR_STORAGE_BUCKET_NAME,
96103
)
97104
server.start()
98105
return server
99106

100107

101108
def shutdown_local_storage_emulator(server):
102-
""" Shutdown the Google Cloud Storage emulator."""
109+
"""Shutdown the Google Cloud Storage emulator."""
103110
server.stop()
104111

105112

106113
def is_datastore_emulator_running(host=EMULATOR_HOST, port=8081):
107-
""" Check if the Google Cloud Datastore emulator is running."""
114+
"""Check if the Google Cloud Datastore emulator is running."""
108115
try:
109116
with socket.create_connection((host, port), timeout=2):
110117
return True
@@ -113,19 +120,25 @@ def is_datastore_emulator_running(host=EMULATOR_HOST, port=8081):
113120

114121

115122
def start_datastore_emulator(project_id="test-project"):
116-
""" Start the Google Cloud Datastore emulator if it's not already running."""
123+
"""Start the Google Cloud Datastore emulator if it's not already running."""
117124
if not is_datastore_emulator_running():
118-
process = subprocess.Popen([
119-
"gcloud", "beta", "emulators", "datastore", "start",
120-
"--project={}".format(project_id),
121-
"--host-port=localhost:8081"
122-
])
125+
process = subprocess.Popen(
126+
[
127+
"gcloud",
128+
"beta",
129+
"emulators",
130+
"datastore",
131+
"start",
132+
"--project={}".format(project_id),
133+
"--host-port=localhost:8081",
134+
]
135+
)
123136
return process
124137
return None # Already running
125138

126139

127140
def shutdown_datastore_emulator(process):
128-
""" Shutdown the Google Cloud Datastore emulator."""
141+
"""Shutdown the Google Cloud Datastore emulator."""
129142
if process:
130143
process.terminate()
131-
process.wait()
144+
process.wait()

functions-python/pmtiles_builder/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ google-cloud-storage
2525
python-dotenv==1.0.0
2626
tippecanoe
2727
psutil
28+
pandas
2829

functions-python/pmtiles_builder/src/csv_cache.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
#
1616
import csv
1717
import os
18-
from shared.helpers.logger import get_logger
1918

19+
from shared.helpers.logger import get_logger
20+
from shared.helpers.transform import get_safe_value, get_safe_float
2021

2122
STOP_TIMES_FILE = "stop_times.txt"
2223
SHAPES_FILE = "shapes.txt"
@@ -127,10 +128,14 @@ def get_stops_from_trip(self, trip_id):
127128

128129
def get_coordinates_for_stop(self, stop_id) -> tuple[float, float] | None:
129130
if self.stop_to_coordinates is None:
130-
self.stop_to_coordinates = {
131-
s["stop_id"]: (float(s["stop_lon"]), float(s["stop_lat"]))
132-
for s in self.get_file(STOPS_FILE)
133-
}
131+
for s in self.get_file(STOPS_FILE):
132+
row_stop_id = get_safe_value(s, "stop_id")
133+
row_stop_lon = get_safe_float(s, "stop_lon")
134+
row_stop_lat = get_safe_float(s, "stop_lat")
135+
if row_stop_id is None or row_stop_lon is None or row_stop_lat is None:
136+
self.logger.warning("Invalid stop data: %s", s)
137+
continue
138+
self.stop_to_coordinates = {row_stop_id: (row_stop_lon, row_stop_lat)}
134139
return self.stop_to_coordinates.get(stop_id, None)
135140

136141
def set_workdir(self, workdir):

0 commit comments

Comments
 (0)