Skip to content

Instantly share code, notes, and snippets.

@rdhyee
Created October 18, 2018 22:35
Show Gist options
  • Save rdhyee/3dc90b4b4969e116dd534620c75e33f8 to your computer and use it in GitHub Desktop.
Save rdhyee/3dc90b4b4969e116dd534620c75e33f8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If I were to pick the 5th Wednesday of each month, how screwy would our schedule be?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import calendar\n",
"\n",
"from itertools import islice\n",
"from collections import defaultdict\n",
"\n",
"def itermonthday2_trunc(year, month):\n",
" cal = calendar.Calendar()\n",
" for (date, wkday) in cal.itermonthdays2(year, month):\n",
" if date > 0:\n",
" yield (date, wkday)\n",
"\n",
"def weekdays_for_month(year, month):\n",
" wkdays = defaultdict(list)\n",
" for (date, wkday) in itermonthday2_trunc(year, month):\n",
" wkdays[wkday].append(date)\n",
" \n",
" return wkdays"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"defaultdict(list,\n",
" {0: [1, 8, 15, 22, 29],\n",
" 1: [2, 9, 16, 23, 30],\n",
" 2: [3, 10, 17, 24, 31],\n",
" 3: [4, 11, 18, 25],\n",
" 4: [5, 12, 19, 26],\n",
" 5: [6, 13, 20, 27],\n",
" 6: [7, 14, 21, 28]})"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"weekdays_for_month(2018,10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's just make use of `calendar.monthrange` instead"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def nth_wkday_of_month(year, month, wkday, n):\n",
" (wkday0, lastdate) = calendar.monthrange(year,month)\n",
" \n",
" date = (wkday-wkday0) % 7 + (n-1)* 7 + 1\n",
" if date > lastdate:\n",
" return None\n",
" else:\n",
" return date\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def year_month_iter(y0,m0):\n",
" \"\"\"\n",
" convenience iterator for march through the months\n",
" allow for modulo\n",
" \"\"\"\n",
" m1 = (m0 - 1)\n",
" \n",
" while True:\n",
" y = y0 + m1 // 12\n",
" m = m1 % 12 + 1\n",
" \n",
" yield(y,m)\n",
" \n",
" m1 += 1\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019/1/30\n",
"2019/5/29\n",
"2019/7/31\n",
"2019/10/30\n",
"2020/1/29\n",
"2020/4/29\n",
"2020/7/29\n",
"2020/9/30\n",
"2020/12/30\n",
"2021/3/31\n",
"2021/6/30\n",
"2021/9/29\n",
"2021/12/29\n",
"2022/3/30\n",
"2022/6/29\n",
"2022/8/31\n",
"2022/11/30\n",
"2023/3/29\n",
"2023/5/31\n",
"2023/8/30\n",
"2023/11/29\n",
"2024/1/31\n",
"2024/5/29\n",
"2024/7/31\n",
"2024/10/30\n",
"2025/1/29\n",
"2025/4/30\n",
"2025/7/30\n",
"2025/10/29\n",
"2025/12/31\n",
"2026/4/29\n",
"2026/7/29\n",
"2026/9/30\n",
"2026/12/30\n",
"2027/3/31\n",
"2027/6/30\n",
"2027/9/29\n",
"2027/12/29\n",
"2028/3/29\n",
"2028/5/31\n",
"2028/8/30\n",
"2028/11/29\n"
]
}
],
"source": [
"# 5th wednesdays of the month\n",
"\n",
"\n",
"for (year, month) in islice(year_month_iter(2019,1),120):\n",
" # looking for 5th wednesdays\n",
" date = nth_wkday_of_month(year, month, 2, 5)\n",
" if date is not None:\n",
" print (\"{}/{}/{}\".format(year, month, date))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment