def test_overlap(self): other = DateRange(date(2011, 10, 1), date(2012, 1, 1)) self.assertTrue( self.date_range.overlap(other) == 1, "Ranges overlap by one day.") other = DateRange(date(2012, 2, 1), date(2012, 2, 25)) self.assertTrue( self.date_range.overlap(other) == 25, "Ranges overlap by 25 days.")
def test_overlaps(self): other = DateRange(date(2011, 10, 1), date(2012, 1, 1)) self.assertTrue(self.date_range.overlaps(other), "Ranges overlap by one day.") other = DateRange(date(2011, 10, 1), date(2012, 12, 31)) self.assertFalse(self.date_range.overlaps(other), "Ranges do not overlap.") other = DateRange(date(2012, 1, 1), date(2012, 3, 31)) self.assertTrue(self.date_range.overlaps(other), "Ranges overlap by one day.")
def dateranges_for_week_str(self, weeks_str): """ >>> weeks('Sem2 wk3-Sem2 wk5, Sem2 wk6-Sem2 wk11') ['Sem2 wk3', 'Sem2 wk4', 'Sem2 wk5', 'Sem2 wk6', 'Sem2 wk7', 'Sem2 wk8', 'Sem2 wk9', 'Sem2 wk10', 'Sem2 wk11'] """ week_ranges = weeks_str.split(', ') date_ranges = [] for week_range in week_ranges: first_week_str, last_week_str = week_range.split('-') first_week = self.week_dateranges[first_week_str] last_week = self.week_dateranges[last_week_str] date_ranges.append(DateRange(first_week.date, last_week.to)) return date_ranges
def __init__(self): """ Initialise T@Ed session and download course list. """ self.session = Session() # Get ASPX session variables from default page: response = self.session.get(Client.base_url, verify=False) index = html.document_fromstring(response.text) self.aspx_session = ASPXSession(index) logging.info('Fetching course list webpage...') parameters = { '__EVENTTARGET': 'LinkBtn_modules', 'tLinkType': 'information', } course_page = self.post(Client.base_url, parameters=parameters) course_options = course_page.xpath('//select[@name="dlObject"]/option') logging.info('Building Course list') self.courses = [] for option in course_options: try: title, identifier = option.text.strip().rsplit(' - ', 1) except ValueError as e: title = identifier = option.text.strip() logging.warning('Error in splitting {0}: {1}'.format(title, e)) logging.info( 'Title and identifier will be the same for {0}'.format( title)) code = identifier[:9] self.courses.append( Course(title=title, identifier=identifier, code=code)) logging.info('Fetching academic-week/date conversion webpage...') week_date_page = self.get(Client.weeklist_url) logging.info('Building academic-week/date dictionary...') self.week_dateranges = dict() week_date_rows = week_date_page.xpath( '/html/body/table[@class="weektable"]//tr[./td]') for row in week_date_rows: week_str, date_str = row.xpath('./td/text()') dtstart = datetime.strptime(date_str[4:], '%a %d %b %Y') dtend = dtstart + timedelta(days=7) self.week_dateranges[week_str] = DateRange(dtstart, dtend) return
class TestDateRange(unittest.TestCase): """ Test the functionality by the DateRange utility class. """ def setUp(self): start_date = date(2012, 1, 1) end_date = date(2012, 3, 31) self.date_range = DateRange(start_date, end_date) def test_days_count(self): self.assertTrue(self.date_range.days_count() == 91, "Failed to calculate number of days in date range.") def test_includes(self): self.assertTrue(self.date_range.includes(date(2012, 1, 1)), "Range includes date.") self.assertFalse(self.date_range.includes(date(2011, 12, 31)), "Range does not include date.") def test_overlaps(self): other = DateRange(date(2011, 10, 1), date(2012, 1, 1)) self.assertTrue(self.date_range.overlaps(other), "Ranges overlap by one day.") other = DateRange(date(2011, 10, 1), date(2012, 12, 31)) self.assertFalse(self.date_range.overlaps(other), "Ranges do not overlap.") other = DateRange(date(2012, 1, 1), date(2012, 3, 31)) self.assertTrue(self.date_range.overlaps(other), "Ranges overlap by one day.") def test_overlap(self): other = DateRange(date(2011, 10, 1), date(2012, 1, 1)) self.assertTrue(self.date_range.overlap(other) == 1, "Ranges overlap by one day.") other = DateRange(date(2012, 2, 1), date(2012, 2, 25)) self.assertTrue(self.date_range.overlap(other) == 25, "Ranges overlap by 25 days.")
class TestDateRange(unittest.TestCase): """ Test the functionality by the DateRange utility class. """ def setUp(self): start_date = date(2012, 1, 1) end_date = date(2012, 3, 31) self.date_range = DateRange(start_date, end_date) def test_days_count(self): self.assertTrue(self.date_range.days_count() == 91, "Failed to calculate number of days in date range.") def test_includes(self): self.assertTrue(self.date_range.includes(date(2012, 1, 1)), "Range includes date.") self.assertFalse(self.date_range.includes(date(2011, 12, 31)), "Range does not include date.") def test_overlaps(self): other = DateRange(date(2011, 10, 1), date(2012, 1, 1)) self.assertTrue(self.date_range.overlaps(other), "Ranges overlap by one day.") other = DateRange(date(2011, 10, 1), date(2012, 12, 31)) self.assertFalse(self.date_range.overlaps(other), "Ranges do not overlap.") other = DateRange(date(2012, 1, 1), date(2012, 3, 31)) self.assertTrue(self.date_range.overlaps(other), "Ranges overlap by one day.") def test_overlap(self): other = DateRange(date(2011, 10, 1), date(2012, 1, 1)) self.assertTrue( self.date_range.overlap(other) == 1, "Ranges overlap by one day.") other = DateRange(date(2012, 2, 1), date(2012, 2, 25)) self.assertTrue( self.date_range.overlap(other) == 25, "Ranges overlap by 25 days.")
def setUp(self): start_date = date(2012, 1, 1) end_date = date(2012, 3, 31) self.date_range = DateRange(start_date, end_date)
from daterange import DateRange # Class 1: Normal situation class_1 = [ DateRange('D-1/3/90', 'D-12/3/90'), DateRange('D-16/3/90', 'D-30/3/90'), DateRange('D-1/2/90', 'D-19/2/90'), DateRange('D-21/2/90', 'D-28/2/90'), DateRange('D-1/1/90', 'D-26/1/90'), DateRange('D-29/1/90', 'D-31/1/90'), DateRange('D-1/6/90', 'D-4/6/90'), DateRange('D-6/6/90', 'D-8/6/90'), 'D-24/6/90', 'D-25/6/90', 'D-28/6/90', 'D-29/6/90', DateRange('D-1/5/90', 'D-6/5/90'), DateRange('D-8/5/90', 'D-20/5/90'), 'D-24/5/90', 'D-25/5/90', 'D-29/5/90', 'D-1/4/90', DateRange('D-4/4/90', 'D-8/4/90'), DateRange('D-10/4/90', 'D-20/4/90'), 'D-27/4/90', 'D-2/7/90', DateRange('D-4/7/90', 'D-8/7/90'), DateRange('D-12/7/90', 'D-15/7/90'), 'D-19/7/90', 'D-23/7/90', 'D-26/7/90', 'D-4/9/90', 'D-5/9/90', 'D-23/9/90', 'D-28/9/90', 'D-30/9/90', 'D-17/8/90', DateRange('D-21/8/90', 'D-25/8/90'), 'D-29/8/90', 'D-30/8/90', 'D-3/12/90', 'D-9/12/90', DateRange('D-16/12/90', 'D-20/12/90'), 'D-23/12/90', 'D-24/12/90', DateRange('D-27/12/90', 'D-30/12/90'), DateRange('D-6/11/90', 'D-8/11/90'), 'D-14/11/90', 'D-16/11/90', 'D-18/11/90', 'D-20/11/90', 'D-21/11/90', 'D-27/11/90', 'D-10/10/90', 'D-18/10/90', 'D-29/10/90', 'D-30/10/90', DateRange('D-3/3/91', 'D-6/3/91'), DateRange('D-10/3/91', 'D-12/3/91'), 'D-18/3/91', 'D-20/3/91', 'D-27/3/91', 'D-29/3/91',