Guest User

Untitled

a guest
Oct 19th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "If I were to pick the 5th Wednesday of each month, how screwy would our schedule be?"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {
  14. "collapsed": true
  15. },
  16. "outputs": [],
  17. "source": [
  18. "import calendar\n",
  19. "\n",
  20. "from itertools import islice\n",
  21. "from collections import defaultdict\n",
  22. "\n",
  23. "def itermonthday2_trunc(year, month):\n",
  24. " cal = calendar.Calendar()\n",
  25. " for (date, wkday) in cal.itermonthdays2(year, month):\n",
  26. " if date > 0:\n",
  27. " yield (date, wkday)\n",
  28. "\n",
  29. "def weekdays_for_month(year, month):\n",
  30. " wkdays = defaultdict(list)\n",
  31. " for (date, wkday) in itermonthday2_trunc(year, month):\n",
  32. " wkdays[wkday].append(date)\n",
  33. " \n",
  34. " return wkdays"
  35. ]
  36. },
  37. {
  38. "cell_type": "code",
  39. "execution_count": 2,
  40. "metadata": {},
  41. "outputs": [
  42. {
  43. "data": {
  44. "text/plain": [
  45. "defaultdict(list,\n",
  46. " {0: [1, 8, 15, 22, 29],\n",
  47. " 1: [2, 9, 16, 23, 30],\n",
  48. " 2: [3, 10, 17, 24, 31],\n",
  49. " 3: [4, 11, 18, 25],\n",
  50. " 4: [5, 12, 19, 26],\n",
  51. " 5: [6, 13, 20, 27],\n",
  52. " 6: [7, 14, 21, 28]})"
  53. ]
  54. },
  55. "execution_count": 2,
  56. "metadata": {},
  57. "output_type": "execute_result"
  58. }
  59. ],
  60. "source": [
  61. "weekdays_for_month(2018,10)"
  62. ]
  63. },
  64. {
  65. "cell_type": "markdown",
  66. "metadata": {},
  67. "source": [
  68. "Let's just make use of `calendar.monthrange` instead"
  69. ]
  70. },
  71. {
  72. "cell_type": "code",
  73. "execution_count": 3,
  74. "metadata": {
  75. "collapsed": true
  76. },
  77. "outputs": [],
  78. "source": [
  79. "def nth_wkday_of_month(year, month, wkday, n):\n",
  80. " (wkday0, lastdate) = calendar.monthrange(year,month)\n",
  81. " \n",
  82. " date = (wkday-wkday0) % 7 + (n-1)* 7 + 1\n",
  83. " if date > lastdate:\n",
  84. " return None\n",
  85. " else:\n",
  86. " return date\n"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": 4,
  92. "metadata": {
  93. "collapsed": true
  94. },
  95. "outputs": [],
  96. "source": [
  97. "def year_month_iter(y0,m0):\n",
  98. " \"\"\"\n",
  99. " convenience iterator for march through the months\n",
  100. " allow for modulo\n",
  101. " \"\"\"\n",
  102. " m1 = (m0 - 1)\n",
  103. " \n",
  104. " while True:\n",
  105. " y = y0 + m1 // 12\n",
  106. " m = m1 % 12 + 1\n",
  107. " \n",
  108. " yield(y,m)\n",
  109. " \n",
  110. " m1 += 1\n",
  111. " "
  112. ]
  113. },
  114. {
  115. "cell_type": "code",
  116. "execution_count": 5,
  117. "metadata": {},
  118. "outputs": [
  119. {
  120. "name": "stdout",
  121. "output_type": "stream",
  122. "text": [
  123. "2019/1/30\n",
  124. "2019/5/29\n",
  125. "2019/7/31\n",
  126. "2019/10/30\n",
  127. "2020/1/29\n",
  128. "2020/4/29\n",
  129. "2020/7/29\n",
  130. "2020/9/30\n",
  131. "2020/12/30\n",
  132. "2021/3/31\n",
  133. "2021/6/30\n",
  134. "2021/9/29\n",
  135. "2021/12/29\n",
  136. "2022/3/30\n",
  137. "2022/6/29\n",
  138. "2022/8/31\n",
  139. "2022/11/30\n",
  140. "2023/3/29\n",
  141. "2023/5/31\n",
  142. "2023/8/30\n",
  143. "2023/11/29\n",
  144. "2024/1/31\n",
  145. "2024/5/29\n",
  146. "2024/7/31\n",
  147. "2024/10/30\n",
  148. "2025/1/29\n",
  149. "2025/4/30\n",
  150. "2025/7/30\n",
  151. "2025/10/29\n",
  152. "2025/12/31\n",
  153. "2026/4/29\n",
  154. "2026/7/29\n",
  155. "2026/9/30\n",
  156. "2026/12/30\n",
  157. "2027/3/31\n",
  158. "2027/6/30\n",
  159. "2027/9/29\n",
  160. "2027/12/29\n",
  161. "2028/3/29\n",
  162. "2028/5/31\n",
  163. "2028/8/30\n",
  164. "2028/11/29\n"
  165. ]
  166. }
  167. ],
  168. "source": [
  169. "# 5th wednesdays of the month\n",
  170. "\n",
  171. "\n",
  172. "for (year, month) in islice(year_month_iter(2019,1),120):\n",
  173. " # looking for 5th wednesdays\n",
  174. " date = nth_wkday_of_month(year, month, 2, 5)\n",
  175. " if date is not None:\n",
  176. " print (\"{}/{}/{}\".format(year, month, date))"
  177. ]
  178. },
  179. {
  180. "cell_type": "code",
  181. "execution_count": null,
  182. "metadata": {
  183. "collapsed": true
  184. },
  185. "outputs": [],
  186. "source": []
  187. }
  188. ],
  189. "metadata": {
  190. "kernelspec": {
  191. "display_name": "Python 3",
  192. "language": "python",
  193. "name": "python3"
  194. },
  195. "language_info": {
  196. "codemirror_mode": {
  197. "name": "ipython",
  198. "version": 3
  199. },
  200. "file_extension": ".py",
  201. "mimetype": "text/x-python",
  202. "name": "python",
  203. "nbconvert_exporter": "python",
  204. "pygments_lexer": "ipython3",
  205. "version": "3.6.6"
  206. },
  207. "toc": {
  208. "base_numbering": 1,
  209. "nav_menu": {},
  210. "number_sections": false,
  211. "sideBar": false,
  212. "skip_h1_title": false,
  213. "title_cell": "Table of Contents",
  214. "title_sidebar": "Contents",
  215. "toc_cell": false,
  216. "toc_position": {},
  217. "toc_section_display": false,
  218. "toc_window_display": false
  219. }
  220. },
  221. "nbformat": 4,
  222. "nbformat_minor": 2
  223. }
Add Comment
Please, Sign In to add comment