forked from ChelseaKR/tods-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.py
More file actions
67 lines (51 loc) · 2.04 KB
/
Copy pathread.py
File metadata and controls
67 lines (51 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Curated read API: load TODS/GTFS packages without running the validator.
For callers who want the parsed feed data itself (a report generator, a
notebook, a data pipeline) rather than validation findings:
from tods_validate.read import load_package, build_companion, to_rows
tods = load_package("exports/tods")
gtfs = load_package("exports/gtfs")
companion = build_companion(gtfs, tods, source=tods.source)
rows = to_rows(tods.get("vehicles.txt"))
This module re-exports the existing loader and GTFS-companion surface under
one namespace, plus a small pandas-free tabulation helper. It carries the
project's semantic-versioning promise: fields are only added within a major
version, never removed or renamed.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from .gtfs_companion import CompanionGTFS, build_companion, merge_supplement
from .loader import FeedFile, Package, PackageNotFoundError, Row, load_package
if TYPE_CHECKING:
import pandas as pd
def to_rows(feed: FeedFile | None) -> list[dict[str, str]]:
"""Tabulate a :class:`FeedFile` as a list of plain dicts, one per row.
A pandas-free view for callers who just want the values; returns ``[]``
when ``feed`` is ``None`` (a file that was not present in the package).
"""
if feed is None:
return []
return [dict(r.values) for r in feed.rows]
def to_dataframe(feed: FeedFile | None) -> pd.DataFrame:
"""Tabulate a :class:`FeedFile` as a pandas ``DataFrame``.
Requires the ``dataframe`` extra (``pip install tods-validate[dataframe]``).
"""
try:
import pandas as pd
except ImportError as exc:
raise ImportError(
"to_dataframe() requires pandas; install it with "
"`pip install tods-validate[dataframe]`."
) from exc
return pd.DataFrame(to_rows(feed))
__all__ = [
"CompanionGTFS",
"FeedFile",
"Package",
"PackageNotFoundError",
"Row",
"build_companion",
"load_package",
"merge_supplement",
"to_dataframe",
"to_rows",
]