try to make pre-commit pass
MODIFY
.pre-commit-config.yaml
.pre-commit-config.yaml
@@ -27,10 +27,11 @@ repos:
hooks:
- id: seed-isort-config
-- repo: https://github.com/pre-commit/mirrors-isort
- rev: v5.2.0
+- repo: https://github.com/pycqa/isort
+ rev: 5.5.4
hooks:
- id: isort
+ args: ["--profile", "black"]
- repo: https://github.com/myint/rstcheck
rev: master

ADD
pangeo_forge/patterns.py
+"""
+Filename patterns.
+"""
+
+import itertools
+import string
+from abc import ABC
+from dataclasses import dataclass
+from typing import Dict, Generator, Iterable, List, Tuple
+
+
+class BasePattern(ABC):
+ def __iter__(self) -> Generator[Tuple[Tuple, str], None, None]:
+ pass
+
+
+@dataclass
+class URLPattern(BasePattern):
+ fmt_string: str
+ keys: Dict[str, Iterable]
+
+ def __post_init__(self):
+ fmt_string_keys = [
+ t[1] for t in string.Formatter().parse(self.fmt_string) if t[1] is not None
+ ]
+ if not set(fmt_string_keys) == set(self.keys):
+ raise KeyError("Specified keys don't match fmt_string")
+
+ def __iter__(self):
+ for keys in itertools.product(*self.keys.values()):
+ format_kwargs = {k: v for k, v in zip(self.keys, keys)}
+ yield keys, self.fmt_string.format(**format_kwargs)
+
+
+@dataclass
+class ExplicitURLSequence(BasePattern):
+ urls: List[str]
+
+ def __iter__(self):
+ for n, item in enumerate(self.urls):
+ yield n, item

ADD
tests/test_patterns.py
+from pangeo_forge.patterns import ExplicitURLSequence, URLPattern
+
+
+def test_url_pattern():
+ pattern = URLPattern("{foo}:{bar}", {"foo": [1, 2], "bar": ["a", "b", "c"]})
+ assert list(pattern) == [
+ ((1, "a"), "1:a"),
+ ((1, "b"), "1:b"),
+ ((1, "c"), "1:c"),
+ ((2, "a"), "2:a"),
+ ((2, "b"), "2:b"),
+ ((2, "c"), "2:c"),
+ ]
+
+
+def test_explicit_url_sequence():
+ seq = ExplicitURLSequence(["a", "b", "c"])
+ assert list(seq) == [(0, "a"), (1, "b"), (2, "c")]

try to make pre-commit pass