Skip to content

Commit

Permalink
Fix example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
tjorim committed Jan 3, 2025
1 parent 65135bf commit fb3352b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
59 changes: 34 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,45 @@ pip install pyrail
```

## Usage
Here is an example of how to use pyRail (async):
Here is an example of how to use pyRail asynchronously:

```python
import asyncio
from pyrail.irail import iRail

# Create an instance of the iRail class
async with iRail(format='json', lang='en') as api:
try:
# Get all stations
stations = await api.get_stations()
print("Stations:", stations)

# Get connections between stations
connections = await api.get_connections(
from_station='Antwerpen-Centraal',
to_station='Brussel-Centraal'
async def main():
# Sequential requests example
async with iRail() as api:
try:
async with iRail() as client:
# Get the total number of stations
stations = await api.get_stations()
if stations:
print(f"Total stations: {len(stations)}")
# Get the liveboard for a specific station
liveboard = await client.get_liveboard(station='Brussels-South')
if liveboard:
print(f"Liveboard for Brussels-South: {liveboard}")
except Exception as e:
print(f"Error occurred: {e}")
# Parallel requests example
async with iRail() as api:
connections, vehicle_info = await asyncio.gather(
# Get connections between stations
api.get_connections(
from_station='Antwerpen-Centraal',
to_station='Brussel-Centraal'
),
# Get vehicle information
api.get_vehicle("BE.NMBS.IC1832")
)
print("Connections:", connections)
except Exception as e:
print(f"Error occurred: {e}")

# Example of concurrent requests with asyncio.gather
async with iRail() as api:
stations, connections = await asyncio.gather(
api.get_stations(),
api.get_connections(
from_station='Antwerpen-Centraal',
to_station='Brussel-Centraal'
)
)
print("Parallel results:")
print(f"Connections from Antwerpen-Centraal to Brussel-Centraal: {connections}")
print(f"Vehicle information for BE.NMBS.IC1832: {vehicle_info}")

# Run the async code
if __name__ == "__main__":
asyncio.run(main())
```

## Configuration
Expand Down
2 changes: 1 addition & 1 deletion pyrail/irail.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ async def get_vehicle(self, id: str, date: Optional[str] = None, alerts: bool =
Example:
async with iRail() as client:
vehicle_info = await client.get_vehicle("BE.NMBS.IC1832", date="2024-01-15", alerts=True)
vehicle_info = await client.get_vehicle("BE.NMBS.IC1832")
"""
extra_params: Dict[str, Optional[Any]] = {"id": id, "date": date, "alerts": "true" if alerts else "false"}
Expand Down

0 comments on commit fb3352b

Please sign in to comment.