Skip to content

Commit

Permalink
Patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Dec 18, 2020
1 parent 5987ec1 commit effba69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
26 changes: 26 additions & 0 deletions climetlab/utils/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#

import re
import itertools
from .datetime import to_datetime

RE1 = re.compile(r"{([^}]*)}")
Expand Down Expand Up @@ -137,6 +138,13 @@ def substitute(self, *args, **kwargs):
params.update(a)
params.update(kwargs)

for k, v in params.items():
if isinstance(v, list):
return self._substitute_many(params)

return self._substitute_one(params)

def _substitute_one(self, params):
used = set(params.keys())
result = []
for p in self.pattern:
Expand All @@ -146,3 +154,21 @@ def substitute(self, *args, **kwargs):
raise ValueError("Unused parameter(s): {}".format(used))

return "".join([str(x) for x in result])

def _substitute_many(self, params):

for k, v in list(params.items()):
if not isinstance(v, list):
params[k] = [v]

seen = set()
result = []
for n in list(
dict(zip(params, x)) for x in itertools.product(*params.values())
):
m = self.substitute(n)
if m not in seen:
seen.add(m)
result.append(m)

return result
7 changes: 6 additions & 1 deletion tests/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ def test_patterns():

p = Pattern("{variable:enum(2t,tp)}.{type:enum(rt,hc)}.{date:date(%Y%m%d)}.grib")
assert (
p.substitute(dict(date="2000-01-01", variable="tp", type='rt'))
p.substitute(dict(date="2000-01-01", variable="tp", type="rt"))
== "tp.rt.20000101.grib"
)

assert p.substitute(dict(date="2000-01-01", variable=["tp", "2t"], type="rt")) == [
"tp.rt.20000101.grib",
"2t.rt.20000101.grib",
]


if __name__ == "__main__":
test_patterns()

0 comments on commit effba69

Please sign in to comment.