Python calendar 模块,monthcalendar() 实例源码

我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用calendar.monthcalendar()

项目:exercism-python    作者:amalshehu    | 项目源码 | 文件源码
def meetup_day(m_Year, m_Month, weekday, SPD):
    dayStart = dayNames.index(weekday)
    chanceDate = [
        weekItem[dayStart]
        for weekItem in calendar.monthcalendar(m_Year, m_Month)
        if weekItem[dayStart]]

    if SPD == 'teenth':
        for dayItem in chanceDate:
            if 13 <= dayItem <= 19:
                return date(m_Year, m_Month, dayItem)
    elif SPD == 'last':
        dayStart = -1
    elif SPD == 'first':
        dayStart = 0
    else:
        dayStart = int(SPD[0]) - 1
    return date(m_Year, m_Month, chanceDate[dayStart])
项目:code    作者:ActiveState    | 项目源码 | 文件源码
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
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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]))
项目:oil    作者:oilshell    | 项目源码 | 文件源码
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]))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
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]))
项目:pendulum    作者:sdispater    | 项目源码 | 文件源码
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.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.day_(day_of_month)
项目:pendulum    作者:sdispater    | 项目源码 | 文件源码
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.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.day_(day_of_month)
项目:pendulum    作者:sdispater    | 项目源码 | 文件源码
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: Pendulum
        """
        dt = self.start_of('day')

        if day_of_week is None:
            return dt.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.day_(day_of_month)
项目:pendulum    作者:sdispater    | 项目源码 | 文件源码
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: Pendulum
        """
        dt = self.start_of('day')

        if day_of_week is None:
            return dt.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.day_(day_of_month)
项目:calendar-telegram    作者:unmonoqueteclea    | 项目源码 | 文件源码
def create_calendar(year,month):
    markup = types.InlineKeyboardMarkup()
    #First row - Month and Year
    row=[]
    row.append(types.InlineKeyboardButton(calendar.month_name[month]+" "+str(year),callback_data="ignore"))
    markup.row(*row)
    #Second row - Week Days
    week_days=["M","T","W","R","F","S","U"]
    row=[]
    for day in week_days:
        row.append(types.InlineKeyboardButton(day,callback_data="ignore"))
    markup.row(*row)

    my_calendar = calendar.monthcalendar(year, month)
    for week in my_calendar:
        row=[]
        for day in week:
            if(day==0):
                row.append(types.InlineKeyboardButton(" ",callback_data="ignore"))
            else:
                row.append(types.InlineKeyboardButton(str(day),callback_data="calendar-day-"+str(day)))
        markup.row(*row)
    #Last row - Buttons
    row=[]
    row.append(types.InlineKeyboardButton("<",callback_data="previous-month"))
    row.append(types.InlineKeyboardButton(" ",callback_data="ignore"))
    row.append(types.InlineKeyboardButton(">",callback_data="next-month"))
    markup.row(*row)
    return markup
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
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]))
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
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]))
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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]))
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
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]))
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
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]))
项目:thunderingplains    作者:lyctc    | 项目源码 | 文件源码
def calendar_sql(gid, pid, year, month, show_subtasks):
    if show_subtasks:
        q = DBS.query(Task).filter_by(gid=gid, pid=pid)
    else:
        q = DBS.query(Task).filter_by(gid=gid, pid=pid, ptid=0)
    rs = q.all()

    task_d = {}
    for r in rs:
        if str(r.due_date) not in task_d:
            task_d[str(r.due_date)] = r.name
        else:
            task_d[str(r.due_date)] += "\n" + r.name

    calendar.setfirstweekday(6)
    calendar_a = calendar.monthcalendar(year, month)

    for week in range(len(calendar_a)):
        for day in range(7):
            date_num = calendar_a[week][day]
            if date_num == 0:
                calendar_a[week][day] = {'date_num': 0, 'tasks': ""}
                # TODO: show out of month days and tasks
                continue
            str_date = datetime.date(year, month, date_num)\
                .strftime('%Y-%m-%d')
            calendar_a[week][day] = {'date_num': date_num,
                                     'tasks': task_d[str_date]
                                     if str_date in task_d else ''}

    return calendar_a
项目:TelegramBots    作者:d-qoi    | 项目源码 | 文件源码
def create_calendar(self, year, month):
        self.logger.debug("in create calendar")
        markup = []
        markup.append([InlineKeyboardButton(calendar.month_name[month] + ", " + str(year), callback_data=' ')])
        week_days = ["M", "T", "W", "R", "F", "S", "U"]

        temp = []
        for day in week_days:
            temp.append(InlineKeyboardButton(day, callback_data=' '))
        markup.append(temp)

        cur_cal = calendar.monthcalendar(year, month)
        for week in cur_cal:
            temp = []
            for day in week:
                if day is 0:
                    temp.append(InlineKeyboardButton(" ", callback_data=' '))
                    continue
                temp.append(InlineKeyboardButton(str(day), callback_data='cal-d-%d-%d-%d' % (day, month, year)))

            markup.append(temp)
        temp = []
        temp.append(InlineKeyboardButton('<', callback_data='cal-p'))
        temp.append(InlineKeyboardButton('>', callback_data='cal-n'))
        markup.append(temp)
        self.logger.debug("Created calendar")
        return InlineKeyboardMarkup(markup)
        #return InlineKeyboardMarkup([[InlineKeyboardButton("This is a test", callback_data=' ')]])