设为首页 收藏本站
查看: 871|回复: 0

[经验分享] python开发_calendar

[复制链接]

尚未签到

发表于 2015-4-24 06:19:14 | 显示全部楼层 |阅读模式
  如果你用过linux,你可能知道在linux下面的有一个强大的calendar功能,即日历
  在python中,同样也有这样的一个强大的calendar
  下面是我做的demo:



1 #python中的calendar
2
3 import calendar
4
5 #返回指定年的某月
6 def get_month(year, month):
7     return calendar.month(year, month)
8
9 #返回指定年的日历
10 def get_calendar(year):
11     return calendar.calendar(year)
12
13 #判断某一年是否为闰年,如果是,返回True,如果不是,则返回False
14 def is_leap(year):
15     return calendar.isleap(year)
16
17 #返回某个月的weekday的第一天和这个月的所有天数
18 def get_month_range(year, month):
19     return calendar.monthrange(year, month)
20
21 #返回某个月以每一周为元素的序列
22 def get_month_calendar(year, month):
23     return calendar.monthcalendar(year, month)
24
25 def main():
26     year = 2013
27     month = 8
28     test_month = get_month(year, month)
29     print(test_month)
30     print('#' * 50)
31     #print(get_calendar(year))
32     print('{0}这一年是否为闰年?:{1}'.format(year, is_leap(year)))
33     print(get_month_range(year, month))
34     print(get_month_calendar(year, month))
35     
36 if __name__ == '__main__':
37     main()
  运行效果:



Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
August 2013
Mo Tu We Th Fr Sa Su
1  2  3  4
5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
##################################################
2013这一年是否为闰年?:False
(3, 31)
[[0, 0, 0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24, 25], [26, 27, 28, 29, 30, 31, 0]]
>>>
  再附上calendar.py的源码:



  1 """Calendar printing functions
  2
  3 Note when comparing these calendars to the ones printed by cal(1): By
  4 default, these calendars have Monday as the first day of the week, and
  5 Sunday as the last (the European convention). Use setfirstweekday() to
  6 set the first day of the week (0=Monday, 6=Sunday)."""
  7
  8 import sys
  9 import datetime
10 import locale as _locale
11
12 __all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
13            "firstweekday", "isleap", "leapdays", "weekday", "monthrange",
14            "monthcalendar", "prmonth", "month", "prcal", "calendar",
15            "timegm", "month_name", "month_abbr", "day_name", "day_abbr"]
16
17 # Exception raised for bad input (with string parameter for details)
18 error = ValueError
19
20 # Exceptions raised for bad input
21 class IllegalMonthError(ValueError):
22     def __init__(self, month):
23         self.month = month
24     def __str__(self):
25         return "bad month number %r; must be 1-12" % self.month
26
27
28 class IllegalWeekdayError(ValueError):
29     def __init__(self, weekday):
30         self.weekday = weekday
31     def __str__(self):
32         return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
33
34
35 # Constants for months referenced later
36 January = 1
37 February = 2
38
39 # Number of days per month (except for February in leap years)
40 mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
41
42 # This module used to have hard-coded lists of day and month names, as
43 # English strings.  The classes following emulate a read-only version of
44 # that, but supply localized names.  Note that the values are computed
45 # fresh on each call, in case the user changes locale between calls.
46
47 class _localized_month:
48
49     _months = [datetime.date(2001, i+1, 1).strftime for i in range(12)]
50     _months.insert(0, lambda x: "")
51
52     def __init__(self, format):
53         self.format = format
54
55     def __getitem__(self, i):
56         funcs = self._months
57         if isinstance(i, slice):
58             return [f(self.format) for f in funcs]
59         else:
60             return funcs(self.format)
61
62     def __len__(self):
63         return 13
64
65
66 class _localized_day:
67
68     # January 1, 2001, was a Monday.
69     _days = [datetime.date(2001, 1, i+1).strftime for i in range(7)]
70
71     def __init__(self, format):
72         self.format = format
73
74     def __getitem__(self, i):
75         funcs = self._days
76         if isinstance(i, slice):
77             return [f(self.format) for f in funcs]
78         else:
79             return funcs(self.format)
80
81     def __len__(self):
82         return 7
83
84
85 # Full and abbreviated names of weekdays
86 day_name = _localized_day('%A')
87 day_abbr = _localized_day('%a')
88
89 # Full and abbreviated names of months (1-based arrays!!!)
90 month_name = _localized_month('%B')
91 month_abbr = _localized_month('%b')
92
93 # Constants for weekdays
94 (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
95
96
97 def isleap(year):
98     """Return True for leap years, False for non-leap years."""
99     return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
100
101
102 def leapdays(y1, y2):
103     """Return number of leap years in range [y1, y2).
104        Assume y1

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-60045-1-1.html 上篇帖子: 轻松自动化---selenium-webdriver(python) (三) 下篇帖子: python 与数据结构
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表