From 6a598b52aa4b2237762ea78562b0d99eb88226d7 Mon Sep 17 00:00:00 2001 From: Panos Mavrogiorgos Date: Mon, 29 Jan 2024 16:31:07 +0200 Subject: [PATCH] fixup: define itertools.pairwise for python < 3.10 --- searvey/ioc.py | 4 ++-- searvey/utils.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/searvey/ioc.py b/searvey/ioc.py index 0b88aa1..944b0d5 100644 --- a/searvey/ioc.py +++ b/searvey/ioc.py @@ -16,7 +16,6 @@ import collections import functools import io -import itertools import logging import typing as T import warnings @@ -48,6 +47,7 @@ from .utils import get_region from .utils import merge_datasets from .utils import NOW +from .utils import pairwise from .utils import resolve_timestamp @@ -498,7 +498,7 @@ def _generate_urls( periods = duration.days // 30 + 2 urls = [] date_range = pd.date_range(start_date, end_date, periods=periods, unit="us", inclusive="both") - for start, stop in itertools.pairwise(date_range): + for start, stop in pairwise(date_range): timestart = _ioc_date(start) timestop = _ioc_date(stop) url = BASE_URL.format(ioc_code=station_id, timestart=timestart, timestop=timestop) diff --git a/searvey/utils.py b/searvey/utils.py index 9631534..db5e089 100644 --- a/searvey/utils.py +++ b/searvey/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations import itertools +import typing as T from typing import Dict from typing import Final from typing import Iterable @@ -25,11 +26,20 @@ NOW: Final = "now" +try: + from itertools import pairwise +except ImportError: + + def pairwise(iterable: T.Iterable[_T]) -> T.Iterator[tuple[_T, _T]]: + # pairwise('ABCDEFG') --> AB BC CD DE EF FG + a, b = itertools.tee(iterable) + next(b, None) + return zip(a, b) + + # https://gis.stackexchange.com/questions/201789/verifying-formula-that-will-convert-longitude-0-360-to-180-to-180 # lon1 is the longitude varying from -180 to 180 or 180W-180E # lon3 is the longitude variable from 0 to 360 (all positive) - - def lon1_to_lon3(lon1: ScalarOrArray) -> ScalarOrArray: return lon1 % 360