Add utils

This commit is contained in:
Csaba 2023-12-08 16:12:54 +01:00
parent 95da5221df
commit 89f217aebd
6 changed files with 47 additions and 3 deletions

View file

View file

@ -0,0 +1,2 @@
container = {}

View file

@ -0,0 +1,40 @@
import os
import re
from datetime import datetime, date, timedelta
from pyproj import Geod
def assert_folder_exists(foldername):
if not os.path.isdir(foldername):
os.makedirs(foldername)
def agency_carpool_ids_from_filename(carpool_filename):
"""
Returns agency_id, carpool_id from a carpool filename.
It is assumed, that carpool_filename matches the regex
/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).json$
"""
m = re.search(r'\/([a-zA-Z0-9_-]+)\/([a-zA-Z0-9_-]+)\.json$', carpool_filename)
if m:
return m[1], m[2]
else:
return None, None
def is_older_than_days(date_to_check, number_of_days):
if date_to_check is None:
return True
if isinstance(date_to_check, datetime):
date_to_check = date_to_check.date()
return date_to_check < date_days_ago(number_of_days)
def yesterday():
return date_days_ago(1)
def date_days_ago(number_of_days):
return date.today() - timedelta(days=number_of_days)
def geodesic_distance_in_m(coord1, coord2):
geod = Geod(ellps="WGS84")
lons = [coord1[0], coord2[0]]
lats = [coord1[1], coord2[1]]
return geod.line_lengths(lons, lats)[0]

View file

@ -86,7 +86,7 @@ app = FastAPI(title="Amarillo - The Carpooling Intermediary",
import importlib
import pkgutil
import amarillo.plugins # FIXME this namespace does not exist if there are 0 plugins installed
import amarillo.plugins

View file

@ -5,5 +5,6 @@ dependencies = [
"fastapi[all]==0.104.0",
"uvicorn[standard]==0.23.2",
"pydantic[dotenv]==2.4.2",
"starlette"
"starlette",
"pyproj==3.6.1",
]

View file

@ -2,3 +2,4 @@ fastapi[all]==0.104.0
uvicorn[standard]==0.23.2
pydantic[dotenv]==2.4.2
starlette
pyproj==3.6.1