Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankrb committed Sep 20, 2023
1 parent c96c666 commit e0dcc7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions climetlab/sources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,18 @@ def metadata(self, name):
def make_datetime(date, time):
if time is None:
return date
assert str(time).isdigit(), (type(time), time)
return datetime.datetime(date.year, date.month, date.day, int(time) // 100)


class Constants(FieldSet):
@normalize("date", "date-list")
def __init__(self, source_or_dataset, request={}, repeat=1, **kwargs):
request = dict(**request)
request.update(kwargs)

request.setdefault("time", [None])

self.request = self._request(request)
self.request = self._request(**request)

if "date" in self.request:
self.dates = [
Expand All @@ -238,7 +238,7 @@ def __init__(self, source_or_dataset, request={}, repeat=1, **kwargs):
@normalize("date", "date-list")
@normalize("time", "int-list")
@normalize("number", "int-list")
def _request(self, request):
def _request(self, **request):
return request

def __len__(self):
Expand Down
33 changes: 33 additions & 0 deletions tests/normalize/test_normalize_int_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

# (C) Copyright 2020 ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#

from climetlab.decorators import normalize


def x_no_default(x):
return x


def test_int_list_normalizers():
g = x_no_default
g = normalize("x", "int-list")(g)
assert g(x=1) == [1]
assert g(x=(1, 2)) == [1, 2]
assert g(x=("1", 2)) == [1, 2]
assert g(x="1") == [1]
assert g(x="1/to/3") == [1, 2, 3]
assert g(x="1/to/3/by/2") == [1, 3]


if __name__ == "__main__":
from climetlab.testing import main

main(__file__)

0 comments on commit e0dcc7b

Please sign in to comment.