From 95571c5cf0cb09a3bbf8247058a24240cded8a5e Mon Sep 17 00:00:00 2001 From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de> Date: Wed, 13 May 2015 13:44:03 +0000 Subject: [PATCH] Changed handling of dates * Changed split_date to also handle YYYYMMDD dates * Added function add_years to handle year offsets for namelist settings --- expconfig.py | 22 +++++++++++++++++---- test.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/expconfig.py b/expconfig.py index 52c6c41..0cf64cc 100644 --- a/expconfig.py +++ b/expconfig.py @@ -139,10 +139,20 @@ class ExpConfig(ConfigObj): match = re.match(r'^0*(\d+)-0*(\d+)-0*(\d+)' r'([T ]0*(\d+)(:0*(\d+)(:0*(\d+))?)?)?$', value) if match: - numbers = match.groups('0') - else: - raise ValueError("invalid date/time '{0}'".format(value)) - return [numbers[i] for i in [0,1,2,4,6,8]] + return [match.groups('0')[i] for i in [0,1,2,4,6,8]] + + match = re.match(r'^0*(\d+?)(\d{2})(\d{2})' + r'([T ]0*(\d+)(:0*(\d+)(:0*(\d+))?)?)?$', value) + if match: + return [match.groups('0')[i] for i in [0,1,2,4,6,8]] + + raise ValueError("invalid date/time '{0}'".format(value)) + + def add_years(value, years): + '''Add specified number of years (possible negative) to date''' + years = int(years) + dt = map(int, split_date(value)) + return "{0:04}-{1:02}-{2:02}".format(dt[0]+years, dt[1], dt[2]) def eval_value(value): ''' @@ -179,6 +189,10 @@ class ExpConfig(ConfigObj): if match: return eval_value_string(match.group(1)) + match = re.match(r'^add_years\(\s*([-\d]+([T ][\d:]+)?)\s*,\s*([-+]?\d+)\s*\)$', value, re.S) + if match: + return add_years(match.group(1), match.group(3)) + match = re.match(r'^split_date\((.*)\)$', value, re.S) if match: return split_date(match.group(1)) diff --git a/test.py b/test.py index 86b47e8..ea712ad 100644 --- a/test.py +++ b/test.py @@ -234,6 +234,60 @@ class ContentTestCase(MkexpTestCase): result = align(result) self.assertMultiLineEqual(expected, result) + def test_split_date(self): + exp_id = 'test_split_date' + job_id = 'job' + writeconfig(exp_id, """ + EXP_TYPE = + DATE_ISO = 1234-05-06 + DATE_RAW = 12340506 + DATE_LIST_ISO = split_date($DATE_ISO) + DATE_LIST_RAW = split_date($DATE_RAW) + [jobs] + [["""+job_id+"""]] + """) + writetemplate(exp_id, job_id, """ + %{DATE_LIST_ISO|join(',')} + %{DATE_LIST_RAW|join(',')} + """) + expected = align(""" + 1234,5,6,0,0,0 + 1234,05,06,0,0,0 + """) + ignore = output(script("mkexp "+exp_id+".config")) + result = readfile(join("test", "experiments", exp_id, exp_id+"."+job_id)) + result = align(result) + self.assertMultiLineEqual(expected, result) + + def test_add_years(self): + exp_id = 'test_add_years' + job_id = 'job' + writeconfig(exp_id, """ + EXP_TYPE = + DATE = 1234-05-06 + NEXT_DATE = 'add_years($DATE, 1)' + PREVIOUS_DATE = 'add_years($DATE, -1)' + NEGATIVE_DATE = 'add_years($DATE, -2000)' + LONGYEAR_DATE = 'add_years($DATE, 10000)' + [jobs] + [["""+job_id+"""]] + """) + writetemplate(exp_id, job_id, """ + %{NEXT_DATE} + %{PREVIOUS_DATE} + %{NEGATIVE_DATE} + %{LONGYEAR_DATE} + """) + expected = align(""" + 1235-05-06 + 1233-05-06 + -766-05-06 + 11234-05-06 + """) + ignore = output(script("mkexp "+exp_id+".config")) + result = readfile(join("test", "experiments", exp_id, exp_id+"."+job_id)) + result = align(result) + self.assertMultiLineEqual(expected, result) if __name__ == '__main__': unittest.main() -- GitLab