Created
February 26, 2015 19:30
-
-
Save nb/814b2f6fb548bfdba5d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
def is_interval(interval, **kwargs): | |
to_list = lambda value: [value] if isinstance(value, basestring) else value | |
list_from_kwarg = lambda key: to_list(kwargs.get(key, [])) | |
units = ['y', 'm', 'd', 'h', 'i', 's'] | |
only = list_from_kwarg('only') | |
if only: | |
return is_interval(interval, with_ = only, without = list(set(units) - set(only))) | |
with_ = list_from_kwarg('with_') | |
without = list_from_kwarg('without') | |
return all([interval.get(unit) > 0 for unit in with_]) and all([interval.get(unit) == 0 for unit in without]) | |
class TestIsInterval(unittest.TestCase): | |
def interval(self, **kwargs): | |
zeroes = {'y': 0, 'm': 0, 'd': 0, 'h': 0, 'i': 0, 's': 0} | |
zeroes.update(kwargs) | |
return zeroes | |
def testOnlyTrueWithSingleSeconds(self): | |
self.assertTrue(is_interval(self.interval(s=5), only = ['s'])); | |
def testOnlyFalseWithAllZeroesInterval(self): | |
self.assertFalse(is_interval(self.interval(), only = ['s'])); | |
def testOnlyFalseWithSecondAndHours(self): | |
self.assertFalse(is_interval(self.interval(s=5, h=10), only = ['s'])); | |
def testWithSecondsAndAlsoHours(self): | |
self.assertTrue(is_interval(self.interval(s=5, h=10), with_ = ['s'])) | |
def testOnlyString(self): | |
self.assertTrue(is_interval(self.interval(s=5), only = 's')) | |
if __name__ == '__main__': | |
unittest.main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment