~homeworkprod/byceps

ref: 7f53f8da79a41591498b73356770ff1cf417adf4 byceps/byceps/services/country/service.py -rw-r--r-- 768 bytes
7f53f8da — Jochen Kupperschmidt Remove unnecessary codec reader, fixes type issue 2 months ago
                                                                                
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
"""
byceps.services.country.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from __future__ import annotations
from dataclasses import dataclass
import json

from flask import current_app


@dataclass(frozen=True)
class Country:
    name: str
    alpha2: str
    alpha3: str


def get_countries() -> list[Country]:
    """Load countries from JSON file."""
    path = 'services/country/resources/countries.json'
    with current_app.open_resource(path) as f:
        records = json.load(f)

    return [Country(**record) for record in records]


def get_country_names() -> list[str]:
    """Return country names."""
    return [country.name for country in get_countries()]