utils

Utility functions and helpers for the PISA package.

This module contains utility functions that are used throughout the PISA package, including validation functions, caching mechanisms, and other helper functions that support the core functionality of the package.

See also

constants

Module containing constants used by these utility functions

Functions

disk_cache(→ Callable)

Implement disk-based caching for function results.

validate_distance_type(→ str)

Validate and normalize distance type input.

validate_mode_of_transport(→ str)

Validate and normalize mode of transport input.

validate_fallback_speed(→ int | float | None)

Validate that a fallback speed is within reasonable bounds for the given transport mode.

Module Contents

utils.disk_cache(cache_dir: str = 'cache') Callable

Implement disk-based caching for function results.

This decorator saves the result of a function call to a file on disk and loads it on subsequent calls with the same arguments, avoiding redundant computations.

Parameters:

cache_dir (str, optional) – Directory where cache files will be stored. (default: cache)

Returns:

A decorated function that implements caching behavior

Return type:

callable

Example

>>> @disk_cache()
>>> def expensive_computation(x):
>>>     # Some time-consuming calculation
>>>     return x ** 2

Notes

  • Cache files are stored as pickle files

  • Cache keys are generated using SHA-256 hash of function name and arguments

  • Creates cache directory if it doesn’t exist

  • Cache files are named using the hash of the function name and arguments

Raises:
  • pickle.PickleError – If there are issues serializing/deserializing the cached data

  • OSError – If there are issues with file operations

utils.validate_distance_type(distance_type: str) str

Validate and normalize distance type input.

Parameters:

distance_type (str) – The distance type to validate (length or travel_time)

Returns:

Normalized distance type (lowercase, stripped of whitespace)

Return type:

str

Raises:

ValueError – If distance_type is not one of the valid types defined in VALID_DISTANCE_TYPES

See also

VALID_DISTANCE_TYPES

Set of valid distance types

utils.validate_mode_of_transport(mode_of_transport: str) str

Validate and normalize mode of transport input.

Parameters:

mode_of_transport (str) – The mode of transport to validate (e.g., driving, walking, cycling)

Returns:

Normalized mode of transport (lowercase, stripped of whitespace)

Return type:

str

Raises:

ValueError – If mode_of_transport is not one of the valid modes defined in VALID_MODES_OF_TRANSPORT

Notes

This function normalizes the input by converting to lowercase and removing leading/trailing whitespace before validation.

See also

VALID_MODES_OF_TRANSPORT

Set of valid transport modes

utils.validate_fallback_speed(fallback_speed: int | float | None, network_type: str) int | float | None

Validate that a fallback speed is within reasonable bounds for the given transport mode.

Parameters:
  • fallback_speed (int, float, or None) – The fallback speed to validate, in kilometers per hour. If None, no validation is performed.

  • network_type (str) – The network type/mode of transport (drive, walk, bike)

Returns:

The validated fallback speed or None if no fallback speed was provided

Return type:

int, float, or None

Raises:

ValueError

  • If fallback_speed is not a number

  • If fallback_speed is not positive

  • If fallback_speed exceeds reasonable bounds for the given mode of transport:

    • For walking: speed must be <= 7 km/h

    • For cycling: speed must be <= 25 km/h

    • For driving: speed must be <= 130 km/h

Notes

This function is used to ensure that fallback speeds (used when OSM data doesn’t provide speed information) are within reasonable bounds for the mode of transport.