from datetime import datetime from typing import Any, Iterable, Optional import flag from apollo_notifications.constants import APOLLO_GLOBAL_MARKET, DATETIME_FORMAT, GLOBAL_MARKET def get_country_code(country_code: str or None) -> str: """Prepare country code for using in queries.""" if not country_code: return '' _country_code = country_code.lower() if _country_code == GLOBAL_MARKET: return APOLLO_GLOBAL_MARKET return _country_code def get_country_flag(country_code: str or None) -> str: """Returns flag symbol by country_code""" if not country_code: return '' return '🌍' if country_code == APOLLO_GLOBAL_MARKET else flag.flag(country_code) def filter_by(result: Iterable[Any], by: Optional[Iterable[Any]]) -> Iterable[Any]: """Filter some collection by passed values.""" if filter_by: result = list(set(result) & set(by)) return result def parse_global_to_gl(market: Optional[str]) -> Optional[str]: if not market: return if market == GLOBAL_MARKET: return APOLLO_GLOBAL_MARKET return market.lower() def parse_gl_to_global(market: Optional[str]) -> Optional[str]: if not market: return if market == APOLLO_GLOBAL_MARKET: return GLOBAL_MARKET return market.lower() def load_datetime(dt_str: Optional[str], format=DATETIME_FORMAT) -> Optional[datetime]: if dt_str: return datetime.strptime(dt_str, format) def dump_datetime(dt: datetime, format=DATETIME_FORMAT) -> Optional[str]: if datetime: return dt.strftime(format)