better sanity checking on decomposed intervals

This commit is contained in:
codl 2017-08-07 14:05:38 +02:00
parent a7f68fde9a
commit 1ba30bdb0a
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
1 changed files with 7 additions and 1 deletions

View File

@ -4,6 +4,7 @@ SCALES = [
('minutes', timedelta(minutes=1)), ('minutes', timedelta(minutes=1)),
('hours', timedelta(hours=1)), ('hours', timedelta(hours=1)),
('days', timedelta(days=1)), ('days', timedelta(days=1)),
('weeks', timedelta(days=7)),
('months', timedelta(days=30)), ('months', timedelta(days=30)),
('years', timedelta(days=365)), ('years', timedelta(days=365)),
] ]
@ -40,7 +41,12 @@ def decompose_interval(attrname):
@significand.setter @significand.setter
def significand(self, value): def significand(self, value):
setattr(self, attrname, int(value) * getattr(self, scl_name)) try:
value = int(value)
assert value >= 0
except (ValueError, AssertionError):
raise ValueError("Incorrect time interval")
setattr(self, attrname, value * getattr(self, scl_name))
setattr(cls, scl_name, scale) setattr(cls, scl_name, scale)