Skip to content

Commit

Permalink
Merge pull request #84 from zaanposni/fix/82-skip-station-enum-import…
Browse files Browse the repository at this point in the history
…-runtime

Fix/82 skip station enum import runtime
  • Loading branch information
zaanposni authored Jan 31, 2025
2 parents 37669e7 + 45af6b9 commit c9ebac1
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ jobs:
Flask8Test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: 3.12
python-version: 3.13

- name: Install dependencies
run: |
Expand Down
36 changes: 26 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ name: tests
on: [pull_request]

jobs:
Python_3_13:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.13
uses: actions/setup-python@v5
with:
python-version: 3.13
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Tests
working-directory: tests
run: python start.py

Python_3_12:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python 3.12
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install dependencies
Expand All @@ -21,9 +37,9 @@ jobs:
Python_3_11:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python 3.11
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Install dependencies
Expand All @@ -37,9 +53,9 @@ jobs:
Python_3_10:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install dependencies
Expand All @@ -53,9 +69,9 @@ jobs:
Python_3_9:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python 3.9
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.9
- name: Install dependencies
Expand All @@ -69,9 +85,9 @@ jobs:
Python_3_8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Set up Python 3.8
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.8
- name: Install dependencies
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pip install vvspy

## Examples

> [!TIP]
> For optimal performance on low-spec hardware such as Raspberry Pi, it is advisable to use the string values of the enum directly to avoid the overhead associated with loading the full enum.
- Detect delay in upcoming departures:

```python
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="vvspy",
version="2.2.0",
version="2.3.0",
license="MIT",
description="API Wrapper for VVS (Verkehrsverbund Stuttgart)",
author="zaanposni",
Expand All @@ -30,6 +30,7 @@
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Operating System :: OS Independent",
],
long_description=long_description,
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/arrivals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import unittest
import sys

Expand All @@ -13,7 +13,7 @@ class ArrivalE2E(unittest.TestCase):
def get_next_monday_noon(self) -> datetime:
now = datetime.now()
while now.weekday() != 0:
now = now.replace(day=now.day + 1)
now += timedelta(days=1)
return now.replace(hour=12, minute=0, second=0, microsecond=0)

def arrivals_enum(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/departures.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import unittest
import sys

Expand All @@ -13,7 +13,7 @@ class DepartureE2E(unittest.TestCase):
def get_next_monday_noon(self) -> datetime:
now = datetime.now()
while now.weekday() != 0:
now = now.replace(day=now.day + 1)
now += timedelta(days=1)
return now.replace(hour=12, minute=0, second=0, microsecond=0)

def departures_enum(self):
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/trips.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timedelta
import unittest
import sys

Expand All @@ -13,7 +13,7 @@ class TripE2E(unittest.TestCase):
def get_next_monday_noon(self) -> datetime:
now = datetime.now()
while now.weekday() != 0:
now = now.replace(day=now.day + 1)
now += timedelta(days=1)
return now.replace(hour=12, minute=0, second=0, microsecond=0)

def trips_enum_enum(self):
Expand Down
17 changes: 10 additions & 7 deletions vvspy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from datetime import datetime as __datetime
from typing import List as __List
from typing import Union as __Union
from typing import Union as __Union, TYPE_CHECKING
from requests.models import Response as __Response
from requests import Session
import logging as __logging

from .enums import Station
if TYPE_CHECKING:
from .enums import Station

from .models import Arrival as __Arrival
from .models import Departure as __Departure
from .models import Trip as __Trip
Expand All @@ -16,8 +18,9 @@

__logger = __logging.getLogger("vvspy")


def departures_now(
station_id: __Union[str, int, Station],
station_id: __Union[str, int, "Station"],
limit: int = 100,
return_resp: bool = False,
session: Session = None,
Expand All @@ -42,7 +45,7 @@ def departures_now(


def get_departure(
station_id: __Union[str, int, Station],
station_id: __Union[str, int, "Station"],
check_time: __datetime = None,
debug: bool = False,
request_params: dict = None,
Expand Down Expand Up @@ -90,7 +93,7 @@ def get_departure(


def get_arrival(
station_id: __Union[str, int, Station],
station_id: __Union[str, int, "Station"],
check_time: __datetime = None,
debug: bool = False,
request_params: dict = None,
Expand Down Expand Up @@ -138,8 +141,8 @@ def get_arrival(


def get_trip(
origin_station_id: __Union[str, int, Station],
destination_station_id: __Union[str, int, Station],
origin_station_id: __Union[str, int, "Station"],
destination_station_id: __Union[str, int, "Station"],
check_time: __datetime = None,
debug: bool = False,
request_params: dict = None,
Expand Down
24 changes: 17 additions & 7 deletions vvspy/arrivals.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from typing import List, Union
from typing import List, Union, TYPE_CHECKING
from datetime import datetime
import requests
from requests.models import Response
from enum import Enum
import requests
import json
import logging as __logging

from .enums import Station
if TYPE_CHECKING:
from .enums import Station
from .models import Arrival

_API_URL = "http://www3.vvs.de/vvs/widget/XML_DM_REQUEST?"
__logger = __logging.getLogger("vvspy")


def get_arrivals(
station_id: Union[str, int, Station],
station_id: Union[str, int, "Station"],
check_time: datetime = None,
limit: int = 100,
request_params: dict = None,
Expand Down Expand Up @@ -81,7 +84,9 @@ def get_arrivals(
"type_dm": kwargs.get("type_dm", "any"),
"anyObjFilter_dm": kwargs.get("anyObjFilter_dm", 2),
"deleteAssignedStops": kwargs.get("deleteAssignedStops", 1),
"name_dm": station_id.value if isinstance(station_id, Station) else str(station_id),
"name_dm": (
station_id.value if isinstance(station_id, Enum) else str(station_id)
),
"mode": kwargs.get("mode", "direct"),
"dmLineSelectionAll": kwargs.get("dmLineSelectionAll", 1),
"useRealtime": kwargs.get("useRealtime", 1), # live delay
Expand All @@ -101,7 +106,9 @@ def get_arrivals(
else:
r = requests.get(_API_URL, **{**request_params, **{"params": params}})

__logger.debug(f"Request took {r.elapsed.total_seconds()}s and returned {r.status_code}")
__logger.debug(
f"Request took {r.elapsed.total_seconds()}s and returned {r.status_code}"
)

if r.status_code != 200:
__logger.error("Error in API request")
Expand All @@ -118,7 +125,10 @@ def get_arrivals(
r.encoding = "UTF-8"
return _parse_response(r.json())
except json.decoder.JSONDecodeError as e:
__logger.error("Error in API request. Received invalid JSON. Status code: %s", r.status_code)
__logger.error(
"Error in API request. Received invalid JSON. Status code: %s",
r.status_code,
)
raise e


Expand Down
24 changes: 17 additions & 7 deletions vvspy/departures.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
from typing import List, Union
from typing import List, Union, TYPE_CHECKING
from datetime import datetime
import requests
from requests.models import Response
from enum import Enum
import requests
import json
import logging as __logging

from .enums import Station
if TYPE_CHECKING:
from .enums import Station
from vvspy.models import Departure

__API_URL = "http://www3.vvs.de/vvs/widget/XML_DM_REQUEST?"
__logger = __logging.getLogger("vvspy")


def get_departures(
station_id: Union[str, int, Station],
station_id: Union[str, int, "Station"],
check_time: datetime = None,
limit: int = 100,
request_params: dict = None,
Expand Down Expand Up @@ -81,7 +84,9 @@ def get_departures(
"type_dm": kwargs.get("type_dm", "any"),
"anyObjFilter_dm": kwargs.get("anyObjFilter_dm", 2),
"deleteAssignedStops": kwargs.get("deleteAssignedStops", 1),
"name_dm": station_id.value if isinstance(station_id, Station) else str(station_id),
"name_dm": (
station_id.value if isinstance(station_id, Enum) else str(station_id)
),
"mode": kwargs.get("mode", "direct"),
"dmLineSelectionAll": kwargs.get("dmLineSelectionAll", 1),
"useRealtime": kwargs.get("useRealtime", 1), # live delay
Expand All @@ -99,7 +104,9 @@ def get_departures(
else:
r = requests.get(__API_URL, **{**request_params, **{"params": params}})

__logger.debug(f"Request took {r.elapsed.total_seconds()}s and returned {r.status_code}")
__logger.debug(
f"Request took {r.elapsed.total_seconds()}s and returned {r.status_code}"
)

if r.status_code != 200:
__logger.error("Error in API request")
Expand All @@ -116,7 +123,10 @@ def get_departures(
r.encoding = "UTF-8"
return _parse_response(r.json())
except json.decoder.JSONDecodeError as e:
__logger.error("Error in API request. Received invalid JSON. Status code: %s", r.status_code)
__logger.error(
"Error in API request. Received invalid JSON. Status code: %s",
r.status_code,
)
raise e


Expand Down
4 changes: 0 additions & 4 deletions vvspy/models/trip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
from datetime import datetime

from .departure import Departure
from .arrival import Arrival
from .connection import Connection


Expand Down
Loading

0 comments on commit c9ebac1

Please sign in to comment.