本文整理匯總了Python中calendar.monthcalendar方法的典型用法代碼示例。如果您正苦於以下問題:Python calendar.monthcalendar方法的具體用法?Python calendar.monthcalendar怎麽用?Python calendar.monthcalendar使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類calendar
的用法示例。
在下文中一共展示了calendar.monthcalendar方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _week_day
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def _week_day(date, week, weekday):
"""
特定の月の第1月曜日などを返します。
"""
if week < 1 or week > 5:
return None
if weekday < 1 or weekday > 7:
return None
lines = calendar.monthcalendar(date.year, date.month)
days = []
for line in lines:
if line[weekday - 1] == 0:
continue
days.append(line[weekday - 1])
return datetime.date(date.year, date.month, days[week - 1])
示例2: check_weeks
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
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]))
示例3: get_calendar
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def get_calendar(year, month):
blank_week = [0, 0, 0, 0, 0, 0, 0]
calendar.setfirstweekday(calendar.SUNDAY)
c = calendar.monthcalendar(year, month)
if len(c) == 4:
c.append(blank_week)
if len(c) == 5:
c.append(blank_week)
return c
示例4: month_calendar
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def month_calendar(year, month, days, service):
calendar.setfirstweekday(calendar.SUNDAY)
month_calendar = calendar.monthcalendar(int(year), int(month))
month_name = calendar.month_name[int(month)]
month_href = reverse('services:month', args=(service.slug, year, month))
html = '<table><thead>'
html += '<tr><th colspan="7"><a href="{0}">{1}</a></th></tr>'.format(month_href, month_name)
html += '<tr><th>s</th><th>m</th><th>t</th><th>w</th><th>t</th><th>f</th><th>s</th></tr>'
html += '</thead><tbody>'
for week in month_calendar:
html += '<tr>'
for day in week:
if day == 0:
str_day = ''
else:
str_day = str(day).zfill(2)
if str_day in days:
day_href = reverse('services:day', args=(service.slug, year, month, str_day))
html += '<td><a href="{0}">{1}</a></td>'.format(day_href, str_day)
else:
html += '<td>{0}</td>'.format(str_day)
html += '</tr>'
html += '</tbody></table>'
return mark_safe(html)
示例5: check_weeks
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def check_weeks(self, year, month, weeks):
cal = calendar.monthcalendar(year, month)
self.assertEqual(len(cal), len(weeks))
for i in range(len(weeks)):
self.assertEqual(weeks[i], sum(day != 0 for day in cal[i]))
示例6: create_calendar
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def create_calendar(year=None,month=None):
"""
Create an inline keyboard with the provided year and month
:param int year: Year to use in the calendar, if None the current year is used.
:param int month: Month to use in the calendar, if None the current month is used.
:return: Returns the InlineKeyboardMarkup object with the calendar.
"""
now = datetime.datetime.now()
if year == None: year = now.year
if month == None: month = now.month
data_ignore = create_callback_data("IGNORE", year, month, 0)
keyboard = []
#First row - Month and Year
row=[]
row.append(InlineKeyboardButton(calendar.month_name[month]+" "+str(year),callback_data=data_ignore))
keyboard.append(row)
#Second row - Week Days
row=[]
for day in ["Mo","Tu","We","Th","Fr","Sa","Su"]:
row.append(InlineKeyboardButton(day,callback_data=data_ignore))
keyboard.append(row)
my_calendar = calendar.monthcalendar(year, month)
for week in my_calendar:
row=[]
for day in week:
if(day==0):
row.append(InlineKeyboardButton(" ",callback_data=data_ignore))
else:
row.append(InlineKeyboardButton(str(day),callback_data=create_callback_data("DAY",year,month,day)))
keyboard.append(row)
#Last row - Buttons
row=[]
row.append(InlineKeyboardButton("<",callback_data=create_callback_data("PREV-MONTH",year,month,day)))
row.append(InlineKeyboardButton(" ",callback_data=data_ignore))
row.append(InlineKeyboardButton(">",callback_data=create_callback_data("NEXT-MONTH",year,month,day)))
keyboard.append(row)
return InlineKeyboardMarkup(keyboard)
示例7: get_last_message_id
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def get_last_message_id(messages_ids, M, last_wanted):
for i in messages_ids:
try:
message_lines = M.top( str(i), '0')[1]
except poplib.error_proto:
print 'Problem in pop top call...'
continue
for line in message_lines:
if line.startswith('Date:'):
date_hdr = line.partition('Date: ')[2]
# print date_hdr
try:
(y, month, d, \
h, min, sec, \
_, _, _, tzoffset) = parsedate_tz(date_hdr)
except (TypeError): continue
except (ValueError): continue
# Python range builtin ?
if month < 0 or month > 12: continue
max_day_per_month = max(calendar.monthcalendar(y, month)[-1])
if d <= 0 or d > max_day_per_month: continue
if h < 0 or h > 23: continue
if min < 0 or min > 59: continue
date = datetime.datetime(y, month, d, h, min, sec)
print date
if date < last_wanted:
return i
示例8: __init__
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def __init__(self, month, year, indent_level, indent_style):
'x.__init__(...) initializes x'
calendar.setfirstweekday(calendar.SUNDAY)
matrix = calendar.monthcalendar(year, month)
self.__table = HTML_Table(len(matrix) + 1, 7, indent_level, indent_style)
for column, text in enumerate(calendar.day_name[-1:] + calendar.day_name[:-1]):
self.__table.mutate(0, column, '<b>%s</b>' % text)
for row, week in enumerate(matrix):
for column, day in enumerate(week):
if day:
self.__table.mutate(row + 1, column, '<b>%02d</b>\n<hr>\n' % day)
self.__weekday, self.__alldays = calendar.monthrange(year, month)
self.__weekday = ((self.__weekday + 1) % 7) + 6
示例9: _first_of_month
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def _first_of_month(self, day_of_week):
"""
Modify to the first occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the first day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. pendulum.MONDAY.
:type day_of_week: int
:rtype: Date
"""
dt = self
if day_of_week is None:
return dt.set(day=1)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[0][calendar_day] > 0:
day_of_month = month[0][calendar_day]
else:
day_of_month = month[1][calendar_day]
return dt.set(day=day_of_month)
示例10: _last_of_month
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def _last_of_month(self, day_of_week=None):
"""
Modify to the last occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the last day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. pendulum.MONDAY.
:type day_of_week: int or None
:rtype: Date
"""
dt = self
if day_of_week is None:
return dt.set(day=self.days_in_month)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[-1][calendar_day] > 0:
day_of_month = month[-1][calendar_day]
else:
day_of_month = month[-2][calendar_day]
return dt.set(day=day_of_month)
示例11: _first_of_month
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def _first_of_month(self, day_of_week):
"""
Modify to the first occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the first day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. DateTime.MONDAY.
:type day_of_week: int
:rtype: DateTime
"""
dt = self.start_of("day")
if day_of_week is None:
return dt.set(day=1)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[0][calendar_day] > 0:
day_of_month = month[0][calendar_day]
else:
day_of_month = month[1][calendar_day]
return dt.set(day=day_of_month)
示例12: _last_of_month
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def _last_of_month(self, day_of_week=None):
"""
Modify to the last occurrence of a given day of the week
in the current month. If no day_of_week is provided,
modify to the last day of the month. Use the supplied consts
to indicate the desired day_of_week, ex. DateTime.MONDAY.
:type day_of_week: int or None
:rtype: DateTime
"""
dt = self.start_of("day")
if day_of_week is None:
return dt.set(day=self.days_in_month)
month = calendar.monthcalendar(dt.year, dt.month)
calendar_day = (day_of_week - 1) % 7
if month[-1][calendar_day] > 0:
day_of_month = month[-1][calendar_day]
else:
day_of_month = month[-2][calendar_day]
return dt.set(day=day_of_month)
示例13: get_nth_kday_of_month
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def get_nth_kday_of_month(gday, gmonth, gyear):
r"""Convert Gregorian date to RDate format.
Parameters
----------
gday : int
Gregorian day.
gmonth : int
Gregorian month.
gyear : int
Gregorian year.
Returns
-------
nth : int
Ordinal number of a given day's occurrence within the month,
for example, the third Friday of the month.
"""
this_month = calendar.monthcalendar(gyear, gmonth)
nth_kday_tuple = next(((i, e.index(gday)) for i, e in enumerate(this_month) if gday in e), None)
tuple_row = nth_kday_tuple[0]
tuple_pos = nth_kday_tuple[1]
nth = tuple_row + 1
if tuple_row > 0 and this_month[0][tuple_pos] == 0:
nth -= 1
return nth
#
# Function get_rdate
#
示例14: fill_calendar
# 需要導入模塊: import calendar [as 別名]
# 或者: from calendar import monthcalendar [as 別名]
def fill_calendar(self):
init_x = 40
y = 70
step_x = 27
step_y = 20
self.canvas.delete(self.base_number_tag)
self.canvas.update()
if self.selected_date is None:
month_calender = calendar.monthcalendar(self.empty_date.year, self.empty_date.month)
else:
month_calender = calendar.monthcalendar(self.selected_date.year, self.selected_date.month)
for row in month_calender:
x = init_x
for item in row:
if item > 0:
self.canvas.create_text(x,
y,
text=str(item),
font=Calendar.FONT,
tags=(self.base_number_tag, item))
if not self.selected_date is None:
if self.selected_date.day == item:
self.move_to(self.circle_selected, (x, y))
self.show(self.circle_selected)
x+= step_x
y += step_y
self.canvas.tag_bind(self.base_number_tag, "<ButtonRelease-1>", self.click_number)
self.canvas.tag_bind(self.base_number_tag, "<Enter>", self.on_mouse_over)
self.canvas.tag_bind(self.base_number_tag, "<Leave>", self.on_mouse_out)