Skip to content

Commit

Permalink
Add 'calendar' article
Browse files Browse the repository at this point in the history
  • Loading branch information
smortezah committed May 6, 2024
1 parent 4a32578 commit 3d7703e
Show file tree
Hide file tree
Showing 3 changed files with 688 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ In addition to these projects, I regularly share my insights and learnings on th
## :snake: Python

- [Argument Parsing](python/argparse.ipynb): Master argument parsing using the `argparse` module.
- [Calendars](python/calendar.ipynb): Explore the `calendar` module in Python.
- [Generators](python/generator.ipynb): A hands-on guide to understanding and using generators.
- [Lambda](python/lambda.ipynb): Get introduced to lambda functions.
- [Pattern Matching](python/match-case.ipynb): Learn pattern matching with the `match-case` statement.
Expand Down
379 changes: 379 additions & 0 deletions python/calendar.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,379 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Click [here]() to access the associated Medium article."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setup"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"import calendar\n",
"import locale"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Calendar Operations\n",
"\n",
"## Displaying a Calendar for a Specific Month and Year"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" May 2024\n",
"Mo Tu We Th Fr Sa Su\n",
" 1 2 3 4 5\n",
" 6 7 8 9 10 11 12\n",
"13 14 15 16 17 18 19\n",
"20 21 22 23 24 25 26\n",
"27 28 29 30 31\n",
"\n"
]
}
],
"source": [
"# Specify the year and month\n",
"year = 2024\n",
"month = 5\n",
"\n",
"# Generate the calendar\n",
"cal = calendar.month(year, month)\n",
"print(cal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Determining the Day of the Week for a Given Date"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"May 15, 2024 is a Wednesday.\n"
]
}
],
"source": [
"# Get the weekday for May 15, 2024\n",
"weekday_index = calendar.weekday(year, month, 15)\n",
"\n",
"# Convert the index to the actual day name\n",
"day_name = calendar.day_name[weekday_index]\n",
"print(f\"May 15, 2024 is a {day_name}.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding Leap Years"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2024 is a leap year.\n"
]
}
],
"source": [
"# Check if 2024 is a leap year\n",
"is_leap = calendar.isleap(year)\n",
"if is_leap:\n",
" print(f\"{year} is a leap year.\")\n",
"else:\n",
" print(f\"{year} is not a leap year.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with Weekdays\n",
"\n",
"## Mapping Weekday Numbers to Day Names"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Monday is called Monday.\n"
]
}
],
"source": [
"# Get the day name for Monday (weekday index 0)\n",
"monday_name = calendar.day_name[0]\n",
"print(f\"Monday is called {monday_name}.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Identifying Weekends"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Weekend\n"
]
}
],
"source": [
"# Create a date object for May 5, 2024\n",
"date_for_checking = calendar.datetime.date(2024, 5, 5)\n",
"\n",
"# Check if the day is a weekend (Saturday or Sunday)\n",
"if date_for_checking.weekday() in [5, 6]:\n",
" print(\"Weekend\")\n",
"else:\n",
" print(\"Weekday\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced Features\n",
"\n",
"## Handling Different Locales and Languages"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" décembre 2021\n",
"Lu Ma Me Je Ve Sa Di\n",
" 1 2 3 4 5\n",
" 6 7 8 9 10 11 12\n",
"13 14 15 16 17 18 19\n",
"20 21 22 23 24 25 26\n",
"27 28 29 30 31\n",
"\n"
]
}
],
"source": [
"# Set the locale to French\n",
"locale.setlocale(locale.LC_TIME, 'fr_FR')\n",
"\n",
"# Generate a calendar for December 2021\n",
"cal = calendar.month(2021, 12)\n",
"print(cal)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Customizing the Calendar Display\n",
"\n",
"### First Day of the Week"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"calendar.setfirstweekday(calendar.SUNDAY)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Practical Examples\n",
"\n",
"## Building a Simple Event Scheduler"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" May 2024\n",
"Mo Tu We Th Fr Sa Su\n",
" 1 2 3 4 5\n",
" 6 7 8 9 10 11 12\n",
"13 14 15 16 17 18 19\n",
"20 21 22 23 24 25 26\n",
"27 28 29 30 31\n",
"\n",
"Event on 5/5/2024\n",
"Event on 5/12/2024\n",
"Event on 5/20/2024\n"
]
}
],
"source": [
"def display_events(year, month, event_dates):\n",
" cal = calendar.month(year, month)\n",
" print(cal)\n",
"\n",
" for day in event_dates:\n",
" print(f\"Event on {month}/{day}/{year}\")\n",
"\n",
"\n",
"# Example usage\n",
"event_dates = [5, 12, 20] # Dates with scheduled events\n",
"display_events(2024, 5, event_dates)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analyzing Historical Data"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Friday the 13th occurred 172 times in the past century.\n"
]
}
],
"source": [
"def count_friday_13(year_range):\n",
" count = 0\n",
" for year in range(*year_range):\n",
" for month in range(1, 13):\n",
" if calendar.weekday(year, month, 13) == calendar.FRIDAY:\n",
" count += 1\n",
" return count\n",
"\n",
"\n",
"# Example usage: Count Friday the 13ths from 1924 to 2024\n",
"friday_13_count = count_friday_13((1924, 2024))\n",
"print(f\"Friday the 13th occurred {friday_13_count} times in the past century.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating a Custom Holiday Tracker"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"December 25, 2024 is a holiday!\n"
]
}
],
"source": [
"def is_holiday(date):\n",
" # Define your list of holiday dates (e.g., New Year's Day, Christmas, etc.)\n",
" holiday_dates = [(1, 1), (12, 25)] # Example: January 1st and December 25th\n",
"\n",
" month, day = date\n",
" return (month, day) in holiday_dates\n",
"\n",
"\n",
"# Example usage: Check if December 25, 2024 is a holiday\n",
"holiday_check = is_holiday((12, 25))\n",
"if holiday_check:\n",
" print(\"December 25, 2024 is a holiday!\")\n",
"else:\n",
" print(\"December 25, 2024 is not a holiday.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 3d7703e

Please sign in to comment.