Skip to content

Commit

Permalink
added python usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenhagelgans committed Sep 28, 2023
1 parent e07a8a1 commit 9252c86
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Build directories/files
/*build*
**/__pycache__/

# Dependencies
/deps
Expand Down Expand Up @@ -45,3 +46,6 @@ config.ini
*.zip
*.7z
/pathdb*

# dotenv
.env
34 changes: 22 additions & 12 deletions modules/transfers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 3.21)
project(motis)

find_package(Python3 REQUIRED
COMPONENTS Interpreter Development)

include_directories(include)

build_flatbuffers(
Expand All @@ -13,22 +16,29 @@ build_flatbuffers(
"${motis-transfers-generated-headers-dir}" # generated_includes_dir
"" # binary_schemas_dir
"" # copy_text_schemas_dir
)
)

file(GLOB_RECURSE motis-transfers-files src/*.cc)
add_library(motis-transfers STATIC ${motis-transfers-files})
target_include_directories(motis-transfers PUBLIC include)
target_include_directories(motis-transfers PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../ppr/include)

# Python 3
target_include_directories(motis-transfers PRIVATE ${Python3_INCLUDE_DIRS})
target_link_directories(motis-transfers PRIVATE ${Python3_LIBRARY_DIRS})

target_compile_features(motis-transfers PRIVATE cxx_std_20)
target_link_libraries(motis-transfers
boost-system
boost-thread
motis-module
motis-libjson
lmdb
nigiri
osmium
geo
ppr-routing
)
target_link_libraries(motis-transfers PRIVATE
boost-system
boost-thread
motis-module
motis-libjson
lmdb
nigiri
osmium
geo
ppr-routing
${Python3_LIBRARIES}
)

target_compile_options(motis-transfers PRIVATE ${MOTIS_CXX_FLAGS})
File renamed without changes.
1 change: 1 addition & 0 deletions modules/transfers/dbapi/stada/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .stations import *
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

from dotenv import load_dotenv

__all__ = ["get_stations_mobility_service_availability_info",
"export_multiple_mobility_service_info_to_csv"]


def get_all_stations() -> http.client.HTTPResponse:
""" Sends an request to the DB API to get information about all stations in
Expand Down Expand Up @@ -51,7 +54,6 @@ def get_stations_mobility_service_availability_info() -> List[Dict[str, Any]]:

stations = get_all_stations().read()
stations_data: Dict[str, Any] = json.loads(stations.decode("utf-8"))
print(len(stations_data.get("result")))

for station in stations_data.get("result"):
service_staff_info = {}
Expand Down Expand Up @@ -182,13 +184,16 @@ def export_multiple_mobility_service_info_to_csv(
stations.
"""
set_station_mobility_service_availability_csv_header(path)
for station_staff_info in stations_mobility_service_info:
export_single_mobility_service_info_to_csv(path, station_staff_info)
for station_mobility_service_info in stations_mobility_service_info:
export_single_mobility_service_info_to_csv(
path,
station_mobility_service_info)


if __name__ == "__main__":
load_dotenv()
staff_infos = get_stations_mobility_service_availability_info()
stations_mobility_service_info =\
get_stations_mobility_service_availability_info()
export_multiple_mobility_service_info_to_csv(
"db_mobility_service_availability.csv",
staff_infos)
stations_mobility_service_info)
22 changes: 22 additions & 0 deletions modules/transfers/test/python_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "gtest/gtest.h"

#include "python3.10/Python.h"

TEST(python_db_api_example, stations_api_call) {
// The following is an example of Python usage in C++
Py_Initialize();
PyRun_SimpleString("import os, sys");
PyRun_SimpleString("from dotenv import load_dotenv");
PyRun_SimpleString(
"sys.path.append(os.path.join(os.getcwd(), 'modules', 'transfers', "
"'dbapi', 'stada'))");
PyRun_SimpleString(
"from stations import get_stations_mobility_service_availability_info, "
"export_multiple_mobility_service_info_to_csv");
PyRun_SimpleString("load_dotenv()");
PyRun_SimpleString(
"export_multiple_mobility_service_info_to_csv("
"'build/data/transfers/db_mobility_service_availability.csv',"
"get_stations_mobility_service_availability_info())");
Py_Finalize();
}

0 comments on commit 9252c86

Please sign in to comment.