Skip to main content

Trafiklab-Sl

version python version test license

A data model describing Storstockholms Lokaltrafik (SL) data.

Also contains an async client for fetching data from the Trafiklab API.

Installation

Install using pip install -U trafiklab-sl

Development

To install the package for development, clone the repository and run:

python -m venv .venv
source .venv/bin/activate
pip install -e '.[dev,test]'

Usage

The client is based on the aiohttp library and is async. It is used to fetch data from the Trafiklab API. The library supports following SL APIs:

Example

Here is an example of how to use the client to get upcoming train departures at Stockholm Central (site Id 1002).

import asyncio
import aiohttp

from tsl.clients.transport import TransportClient
from tsl.clients.journey import JourneyPlannerClient, SearchLeg
from tsl.models.common import TransportMode
from tsl.utils import global_id_to_site_id
from tsl.tools.journey import SimpleJourneyInterpreter, leg_display_str


async def main():
    async with aiohttp.ClientSession() as session:

        # perform stop lookup to get the site id for Stockholm Central
        journey_client = JourneyPlannerClient(session)
        stops = await journey_client.find_stops("Stockholm Central")
        if (central_station := next(iter(stops), None)) is None:
            raise RuntimeError(r"Could not find Stockholm Central. Weird ¯\_(ツ)_/¯")

        # get the transport API site id for Stockholm Central
        transport_api_siteid = global_id_to_site_id(central_station['id'])

        # get upcoming train departures
        client = TransportClient(session)
        reponse = await client.get_site_departures(transport_api_siteid, transport=TransportMode.TRAIN)

        # get step by step journey from Stockholm Central to a specific location
        params = journey_client.build_request_params(
            origin=SearchLeg.from_stop_finder(central_station),
            destination=SearchLeg.from_coordinates("59.274695", "18.033901"),
            calc_number_of_trips=1
        )
        ways_to_get_there = await journey_client.search_trip(params)
        # use `SimpleJourneyInterpreter` to interpret the journey data in a human-readable way

    print(f"Upcoming trains at {central_station['disassembledName']}:")
    for departure in sorted(reponse["departures"], key=lambda d: d.get("expected", "")):
        print(
            f"[{departure['line'].get('designation')}] platform {departure['stop_point'].get('designation')}"
            f" to {departure.get('destination')} ({departure['display']})"
        )

    print(f"\nNumber of ways to get from Stockholm Central to 59.274695, 18.033901 - {len(ways_to_get_there)}")
    interpreter = SimpleJourneyInterpreter(ways_to_get_there[0])

    print("Itinerary of the first way:")
    for leg in interpreter.get_itinerary():
        print("  - " + leg_display_str(leg))


asyncio.run(main())

Contributing

Both bug reports and pull requests are appreciated.

Development

pre-commit hooks

To help ensure code style and import order are consistent across the project, this project uses pre-commit to automatically check and format code before it reaches the repository.

This project uses pre-commit to automatically check and format code before it reaches the repository.

To set up pre-commit hooks locally:

pip install pre-commit
pre-commit install

After this, every time you commit, formatting tools will be run automatically on your code.

You can also run all hooks manually on all files with:

pre-commit run --all-files

Release files for trafiklab-sl 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trafiklab-sl 2.2.0
File Size Uploaded
trafiklab_sl-2.2.0.tar.gz 35.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trafiklab-sl 2.2.0
File Interpreter ABI Platform
trafiklab_sl-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 69.6 kB

Release files / trafiklab_sl-2.2.0.tar.gz

Download URL trafiklab_sl-2.2.0.tar.gz
Size 35.8 kB
Tags Source
SHA-256 checksum
How to use checksums
eecf0e84d000a792bc29dfb83dc4668e51b22e5086596b73324f5708eb989169
BLAKE2b-256 checksum
How to use checksums
c234de871057cc965ad571d5466114983530744b26f25c5e9c46f45c3ed606a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 7, 2025.

Transparency log

Release files / trafiklab_sl-2.2.0-py3-none-any.whl

Download URL trafiklab_sl-2.2.0-py3-none-any.whl
Size 33.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d90d1a9a562177095a679f12ac01c73b4e86ba0b98e29fb5a0f2dd3e02c54692
BLAKE2b-256 checksum
How to use checksums
1c15dd88f25556680c6253c65286f3609d18750a1cf0e1f7ff1275718c793c8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 7, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files

2.1.1

2 release files

2.0.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page