Skip to content

Commit

Permalink
Merge pull request #5 from nolanbconaway/add-tz-option
Browse files Browse the repository at this point in the history
add tz option for extract stop dict.
  • Loading branch information
nolanbconaway authored Aug 18, 2019
2 parents 73412c5 + f7dd017 commit e0245aa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "underground"
version = "0.2.1"
version = "0.2.1.1"
description = ""
authors = ["Nolan Conaway <nolanbconaway@gmail.com>"]
packages = [{ include = "underground" }]
Expand Down
5 changes: 2 additions & 3 deletions underground/cli/stops.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Get upcoming stops along a train route."""

import click
import pytz

from underground import dateutils, metadata
from underground.models import SubwayFeed
Expand Down Expand Up @@ -45,15 +44,15 @@ def main(route, fmt, retries, api_key, timezone):
SubwayFeed.get(
api_key=api_key, feed_id=metadata.ROUTE_FEED_MAP.get(route), retries=retries
)
.extract_stop_dict()
.extract_stop_dict(timezone=timezone)
.get(route, dict())
)

# figure out how to format it
if fmt == "epoch":
format_fun = dateutils.datetime_to_epoch
else:
format_fun = lambda x: x.astimezone(pytz.timezone(timezone)).strftime(fmt)
format_fun = lambda x: x.strftime(fmt)

# echo the result
for stop_id, departures in stops.items():
Expand Down
13 changes: 11 additions & 2 deletions underground/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,14 @@ def get(feed_id: int, retries: int = 100, api_key: str = None) -> "SubwayFeed":
)
)

def extract_stop_dict(self) -> dict:
def extract_stop_dict(self, timezone: str = dateutils.DEFAULT_TIMEZONE) -> dict:
"""Get the departure times for all stops in the feed.
Parameters
----------
timezone : str
Name of the timezone to return within. Default to NYC time.
Returns
-------
dict
Expand All @@ -164,7 +169,11 @@ def extract_stop_dict(self) -> dict:
)
# create (route, stop, time) tuples from each trip
stops_flat = (
(trip.trip.route_id, stop.stop_id, stop.departure.time)
(
trip.trip.route_id,
stop.stop_id,
stop.departure.time.astimezone(pytz.timezone(timezone)),
)
for trip in trip_updates_with_stops
for stop in trip.stop_time_update
)
Expand Down

0 comments on commit e0245aa

Please sign in to comment.