Skip to content

Instantly share code, notes, and snippets.

@nb
Created February 26, 2015 19:30
Show Gist options
  • Save nb/814b2f6fb548bfdba5d4 to your computer and use it in GitHub Desktop.
Save nb/814b2f6fb548bfdba5d4 to your computer and use it in GitHub Desktop.
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