我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用calendar.calendar()。
def test_localecalendars(self): # ensure that Locale{Text,HTML}Calendar resets the locale properly # (it is still not thread-safe though) old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) try: cal = calendar.LocaleTextCalendar(locale='') local_weekday = cal.formatweekday(1, 10) local_month = cal.formatmonthname(2010, 10, 10) except locale.Error: # cannot set the system default locale -- skip rest of test raise unittest.SkipTest('cannot set the system default locale') # should be encodable local_weekday.encode('utf-8') local_month.encode('utf-8') self.assertEqual(len(local_weekday), 10) self.assertGreaterEqual(len(local_month), 10) cal = calendar.LocaleHTMLCalendar(locale='') local_weekday = cal.formatweekday(1) local_month = cal.formatmonthname(2010, 10) # should be encodable local_weekday.encode('utf-8') local_month.encode('utf-8') new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) self.assertEqual(old_october, new_october)
def test_locale_calendars(self): # ensure that Locale{Text,HTML}Calendar resets the locale properly # (it is still not thread-safe though) old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) try: cal = calendar.LocaleTextCalendar(locale='') local_weekday = cal.formatweekday(1, 10) local_month = cal.formatmonthname(2010, 10, 10) except locale.Error: # cannot set the system default locale -- skip rest of test raise unittest.SkipTest('cannot set the system default locale') self.assertIsInstance(local_weekday, str) self.assertIsInstance(local_month, str) self.assertEqual(len(local_weekday), 10) self.assertGreaterEqual(len(local_month), 10) cal = calendar.LocaleHTMLCalendar(locale='') local_weekday = cal.formatweekday(1) local_month = cal.formatmonthname(2010, 10) self.assertIsInstance(local_weekday, str) self.assertIsInstance(local_month, str) new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) self.assertEqual(old_october, new_october)
def test_output(self): self.assertEqual( self.normalize_calendar(calendar.calendar(2004)), self.normalize_calendar(result_2004_text) )
def test_output_textcalendar(self): self.assertEqual( calendar.TextCalendar().formatyear(2004).strip(), result_2004_text.strip() )
def test_output_htmlcalendar(self): encoding = 'ascii' cal = calendar.HTMLCalendar() self.assertEqual( cal.formatyearpage(2004, encoding=encoding).strip(b' \t\n'), result_2004_html.strip(' \t\n').encode(encoding) )
def test_isleap(self): # Make sure that the return is right for a few years, and # ensure that the return values are 1 or 0, not just true or # false (see SF bug #485794). Specific additional tests may # be appropriate; this tests a single "cycle". self.assertEqual(calendar.isleap(2000), 1) self.assertEqual(calendar.isleap(2001), 0) self.assertEqual(calendar.isleap(2002), 0) self.assertEqual(calendar.isleap(2003), 0)
def test_setfirstweekday(self): self.assertRaises(TypeError, calendar.setfirstweekday, 'flabber') self.assertRaises(ValueError, calendar.setfirstweekday, -1) self.assertRaises(ValueError, calendar.setfirstweekday, 200) orig = calendar.firstweekday() calendar.setfirstweekday(calendar.SUNDAY) self.assertEqual(calendar.firstweekday(), calendar.SUNDAY) calendar.setfirstweekday(calendar.MONDAY) self.assertEqual(calendar.firstweekday(), calendar.MONDAY) calendar.setfirstweekday(orig)
def test_days(self): for attr in "day_name", "day_abbr": value = getattr(calendar, attr) self.assertEqual(len(value), 7) self.assertEqual(len(value[:]), 7) # ensure they're all unique self.assertEqual(len(set(value)), 7) # verify it "acts like a sequence" in two forms of iteration self.assertEqual(value[::-1], list(reversed(value)))
def test_months(self): for attr in "month_name", "month_abbr": value = getattr(calendar, attr) self.assertEqual(len(value), 13) self.assertEqual(len(value[:]), 13) self.assertEqual(value[0], "") # ensure they're all unique self.assertEqual(len(set(value)), 13) # verify it "acts like a sequence" in two forms of iteration self.assertEqual(value[::-1], list(reversed(value)))
def test_localecalendars(self): # ensure that Locale{Text,HTML}Calendar resets the locale properly # (it is still not thread-safe though) old_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) try: calendar.LocaleTextCalendar(locale='').formatmonthname(2010, 10, 10) except locale.Error: # cannot set the system default locale -- skip rest of test return calendar.LocaleHTMLCalendar(locale='').formatmonthname(2010, 10) new_october = calendar.TextCalendar().formatmonthname(2010, 10, 10) self.assertEqual(old_october, new_october)
def setUp(self): self.oldfirstweekday = calendar.firstweekday() calendar.setfirstweekday(self.firstweekday)
def tearDown(self): calendar.setfirstweekday(self.oldfirstweekday)
def test_timegm(self): for secs in self.TIMESTAMPS: tuple = time.gmtime(secs) self.assertEqual(secs, calendar.timegm(tuple))
def test_january(self): # Tests valid lower boundary case. self.assertEqual(calendar.monthrange(2004,1), (3,31))
def test_february_leap(self): # Tests February during leap year. self.assertEqual(calendar.monthrange(2004,2), (6,29))
def test_february_nonleap(self): # Tests February in non-leap year. self.assertEqual(calendar.monthrange(2010,2), (0,28))
def test_december(self): # Tests valid upper boundary case. self.assertEqual(calendar.monthrange(2004,12), (2,31))
def test_thirteenth_month(self): # Tests high invalid boundary case. with self.assertRaises(calendar.IllegalMonthError): calendar.monthrange(2004, 13)
def test_no_range(self): # test when no range i.e. two identical years as args self.assertEqual(calendar.leapdays(2010,2010), 0)
def test_no_leapdays(self): # test when no leap years in range self.assertEqual(calendar.leapdays(2010,2011), 0)
def test_no_leapdays_upper_boundary(self): # test no leap years in range, when upper boundary is a leap year self.assertEqual(calendar.leapdays(2010,2012), 0)
def test_one_leapday_lower_boundary(self): # test when one leap year in range, lower boundary is leap year self.assertEqual(calendar.leapdays(2012,2013), 1)
def test_outputs_bytes(self): (return_code, stdout, stderr) = assert_python_ok('-m', 'calendar', '--type=html', '2010') self.assertEqual(stdout[:6], b'<?xml ')
def test_output_htmlcalendar(self): self.assertEqual( calendar.HTMLCalendar().formatyearpage(2004).strip(), result_2004_html.strip() )
def test_setfirstweekday(self): self.assertRaises(ValueError, calendar.setfirstweekday, 'flabber') self.assertRaises(ValueError, calendar.setfirstweekday, -1) self.assertRaises(ValueError, calendar.setfirstweekday, 200) orig = calendar.firstweekday() calendar.setfirstweekday(calendar.SUNDAY) self.assertEqual(calendar.firstweekday(), calendar.SUNDAY) calendar.setfirstweekday(calendar.MONDAY) self.assertEqual(calendar.firstweekday(), calendar.MONDAY) calendar.setfirstweekday(orig)
def test_itermonthdates(self): # ensure itermonthdates doesn't overflow after datetime.MAXYEAR # see #15421 list(calendar.Calendar().itermonthdates(datetime.MAXYEAR, 12))
def test_itermonthdays(self): for firstweekday in range(7): cal = calendar.Calendar(firstweekday) # Test the extremes, see #28253 and #26650 for y, m in [(1, 1), (9999, 12)]: days = list(cal.itermonthdays(y, m)) self.assertIn(len(days), (35, 42)) # Test a short month cal = calendar.Calendar(firstweekday=3) days = list(cal.itermonthdays(2001, 2)) self.assertEqual(days, list(range(1, 29)))
def check_weeks(self, year, month, weeks): cal = calendar.monthcalendar(year, month) self.assertEqual(len(cal), len(weeks)) for i in xrange(len(weeks)): self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
def test_zeroth_month(self): # Tests low invalid boundary case. with self.assertRaises(calendar.IllegalMonthError): calendar.monthrange(2004, 0)
def test_several_leapyears_in_range(self): self.assertEqual(calendar.leapdays(1997,2020), 5)
def run_ok(self, *args): return assert_python_ok('-m', 'calendar', *args)[1]