Skip to main content

Airports Library

A comprehensive Python library for airport data management and querying heavily influenced by Tim Rogers' Airports gem. This library provides fast, efficient lookups for airport information using IATA codes, ICAO codes, cities, regions, and countries.

Features

  • Efficient lookup of airports by:
    • IATA code (e.g., "LAX")
    • ICAO code (e.g., "KLAX")
    • City name (e.g., "Los Angeles")
    • City code (e.g., "LAX")
    • Region/State (e.g., "California")
    • Region code (e.g., "US-CA")
    • Country (e.g., "United States")
    • Country code (e.g., "US")
  • Fuzzy matching for city names
  • Distance calculation between airports using the Haversine formula
  • Lazy loading of data for optimal memory usage
  • Type hints for better IDE support
  • Comprehensive error handling

Installation

pip install airports

Quick Start

from airports import Airports

# Initialize the library
airports = Airports()

# Look up an airport by IATA code
lax = airports.find_by_iata_code("LAX")
print(f"Found {lax['name']} in {lax['city']}, {lax['country']}")

# Find all airports in a city
nyc_airports = airports.find_all_by_city_code("NYC")
for airport in nyc_airports:
    print(f"{airport['name']} ({airport['iata']})")

# Calculate distance between airports
distance = airports.distance_between("JFK", "LAX")
print(f"Distance: {distance:.1f} km")

API Reference

Main Class: Airports

Basic Lookups

  • find_by_iata_code(iata_code: str) -> Optional[Dict]

    • Find airport by 3-letter IATA code
    • Returns None if not found
  • find_by_icao_code(icao_code: str) -> Optional[Dict]

    • Find airport by 4-letter ICAO code
    • Returns None if not found

City-based Lookups

  • find_all_by_city(city_name: str) -> List[Dict]

    • Find all airports in a given city
    • Case-insensitive search
  • find_all_by_city_code(city_code: str) -> List[Dict]

    • Find all airports with a specific 3-letter city code
    • Returns empty list if none found
  • fuzzy_find_city(city_name: str, cutoff: float = 0.6) -> List[Dict]

    • Find airports in cities with names similar to input
    • Adjustable similarity threshold (cutoff)

Region and Country Lookups

  • find_all_by_region(region_name: str) -> List[Dict]

    • Find all airports in a given region/state
    • Case-insensitive search
  • find_all_by_region_code(region_code: str) -> List[Dict]

    • Find all airports by region code (e.g., "US-NY")
    • Returns empty list if none found
  • find_all_by_country(country_name: str) -> List[Dict]

    • Find all airports in a given country
    • Case-insensitive search
  • find_all_by_country_code(country_code: str) -> List[Dict]

    • Find all airports by 2-letter country code
    • Returns empty list if none found

Utility Methods

  • distance_between(airport1: Union[str, Dict], airport2: Union[str, Dict]) -> Optional[float]
    • Calculate distance between two airports in kilometers
    • Accept either IATA codes or airport dictionaries
    • Returns None if either airport not found or missing coordinates

Properties

  • iata_codes: List[str] - Get all IATA codes in dataset
  • icao_codes: List[str] - Get all ICAO codes in dataset
  • city_codes: List[str] - Get all city codes in dataset

Airport Data Structure

Each airport is represented as a dictionary with the following fields:

{
    # Basic Information
    "name": str,             # Full airport name
    "city": str,             # City name
    "city_code": str,        # IATA city code
    "country": str,          # Country name
    "iso_country_code": str, # ISO 3166-1 country code
    "region": str,           # Region/state name
    "iso_region_code": str,  # ISO 3166-2 region code

    # Airport Codes
    "iata": str,             # IATA airport code
    "icao": str,             # ICAO airport code

    # Location Information
    "latitude": float,       # Latitude in decimal degrees
    "longitude": float,      # Longitude in decimal degrees
    "altitude": int,         # Altitude in feet

    # Timezone Information
    "tz_name": str,          # Timezone
    "utc_offset": float,     # UTC offset
    "dst": str,              # Daylight savings region type

    # Other
    "type": str,             # Airport type
}

Error Handling

The library implements robust error handling:

  • Invalid IATA codes (not 3 letters) raise ValueError
  • Invalid ICAO codes (not 4 letters) raise ValueError
  • Invalid city codes (not 3 letters) raise ValueError
  • Invalid region codes (not 4-6 characters) raise ValueError
  • Invalid country codes (not 2 letters) raise ValueError

Development

Project Structure

.
├── LICENSE.txt
├── README.md
├── airports
│   ├── __init__.py
│   ├── airports.py
│   ├── data
│   │   ├── airports.json
│   │   └── incoming
│   └── merge.py
├── pyproject.toml
├── requirements-dev.txt
└── tests
    └── test_airports.py

Setting Up Development Environment

  1. Clone the repository:
git clone https://github.com/yourusername/airports.git
cd airports
  1. Install development dependencies:
pip install -r requirements-dev.txt
  1. Run tests:
pytest tests/

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

Acknowledgments

Release files for airports 0.1.2

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

Source distribution (sdist)

Source distribution for airports 0.1.2
File Size Uploaded
airports-0.1.2.tar.gz 420.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for airports 0.1.2
File Interpreter ABI Platform
airports-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 858.1 kB

Release files / airports-0.1.2.tar.gz

Download URL airports-0.1.2.tar.gz
Size 420.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4eba99c25c5cc903560752c9400384d247fc8cd6f87fcc7cb0a79978810b8470
BLAKE2b-256 checksum
How to use checksums
1cc9227112d16f3c16ea1e125ef1454472f21e8eb957fa4b4ae64ed745be008f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.4

Release files / airports-0.1.2-py3-none-any.whl

Download URL airports-0.1.2-py3-none-any.whl
Size 437.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6456241529d0dbf279604841ce6ecc7014cbf464cd64bffbdfc7fe19ab44eeea
BLAKE2b-256 checksum
How to use checksums
63d010be1a712ca02a654e31679bd410877a49ffb43b316375938773ccc891f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

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