0
0

前置き

私は、毎日の予定(スケジュール)を管理するのに、手帳とGoogle Calendar両方を使用している。今回は、Pythonを使って週間スケジュールを作成してみました。

スケジュールとは、予定や計画、時間割を意味する英単語です。
主に、あらかじめ決められた時間や順序に従って行われる活動やイベントのリストです。

引用元:https://www.weblio.jp/content/%E3%82%B9%E3%82%B1%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB

週間スケジュールのプログラムコード

from datetime import datetime, timedelta

class Event:
    def __init__(self, start_time, end_time, description):
        self.start_time = start_time
        self.end_time = end_time
        self.description = description

    def __str__(self):
        return f"{self.start_time.strftime('%H:%M')} - {self.end_time.strftime('%H:%M')} : {self.description}"

class WeeklyCalendar:
    def __init__(self):
        self.events = {}

    def add_event(self, date, event):
        if date in self.events:
            self.events[date].append(event)
        else:
            self.events[date] = [event]

    def print_weekly_calendar(self, start_date):
        # Print the weekly calendar starting from the provided start_date
        current_date = start_date
        for i in range(7):  # Print for 7 days (a week)
            print(f"{current_date.strftime('%Y-%m-%d')}:")
            if current_date in self.events:
                for event in self.events[current_date]:
                    print(f"  - {event}")
            else:
                print("  No events scheduled.")
            print()
            current_date += timedelta(days=1)  # Move to the next day

    def add_event_interactively(self):
        # Allow user to add an event interactively
        print("Add Event:")
        date_str = input("Enter date (YYYY-MM-DD): ")
        try:
            date = datetime.strptime(date_str, '%Y-%m-%d').date()
        except ValueError:
            print("Invalid date format. Please enter date in YYYY-MM-DD format.")
            return

        start_time_str = input("Enter start time (HH:MM): ")
        end_time_str = input("Enter end time (HH:MM): ")
        description = input("Enter event description: ")

        try:
            start_time = datetime.strptime(start_time_str, '%H:%M').time()
            end_time = datetime.strptime(end_time_str, '%H:%M').time()
        except ValueError:
            print("Invalid time format. Please enter time in HH:MM format.")
            return

        event = Event(start_time, end_time, description)
        self.add_event(date, event)
        print("Event added successfully.")

# Example usage:
calendar = WeeklyCalendar()

# Interactive adding events
while True:
    choice = input("Do you want to add an event? (yes/no): ").lower()
    if choice != 'yes':
        break
    calendar.add_event_interactively()

# Printing the weekly calendar starting from today's date
start_date = datetime.now().date()
calendar.print_weekly_calendar(start_date)

プログラムコードの説明

  1. Event クラス:
    init: start_time(開始時間)、end_time(終了時間)、およびdescription(説明)を初期化します。
    str メソッド: イベントをフォーマットして表示するためのメソッドです。

  2. WeeklyCalendar クラス:
    init: eventsという辞書を初期化します。この辞書は、日付をキーにして、イベントのリストを値として持ちます。
    add_event: 日付とイベントを受け取り、辞書に追加します。同じ日付に複数のイベントがある場合、そのリストに追加します。
    print_weekly_calendar: 指定された開始日から1週間のカレンダーを印刷します。各日付にスケジュールされたイベントを表示します。
    add_event_interactively: ユーザーがインタラクティブに日付、開始時間、終了時間、および説明を入力してイベントを追加できるようにします。入力された日付と時間の形式をチェックし、無効な場合はエラーメッセージを表示します。

実際の使用例:
WeeklyCalendarクラスのインスタンスcalendarを作成します。
ユーザーがイベントを追加するかどうかを尋ねるループを開始し、add_event_interactivelyメソッドを使用してイベントを追加します。
今日の日付から1週間のカレンダーをprint_weekly_calendarメソッドで印刷します。
このプログラムにより、ユーザーはイベントのスケジュール管理が容易になり、1週間分の予定を視覚的に確認することができます。

出力結果

Do you want to add an event? (yes/no): yes
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 6:15
Enter end time (HH:MM): 7:00
Enter event description: Wake up
Event added successfully.
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 7:15
Enter end time (HH:MM): 8:00
Enter event description: Morning Discussion
Event added successfully.
Do you want to add an event? (yes/no): yes
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 8:00
Enter end time (HH:MM): 9:00
Enter event description: Breakfast
Event added successfully.
Do you want to add an event? (yes/no): yes
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 9:15
Enter end time (HH:MM): 12:15
Enter event description: Morning Work 
Event added successfully.
Do you want to add an event? (yes/no): yes
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 12:15
Enter end time (HH:MM): 13:15
Enter event description: Lunch
Event added successfully.
Do you want to add an event? (yes/no): yes
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 13:15
Enter end time (HH:MM): 18:00
Enter event description: Afternoon Work
Event added successfully.
Do you want to add an event? (yes/no): yes
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 19:15
Enter end time (HH:MM): 21:00
Enter event description: Relax + Dinner
Event added successfully.
Do you want to add an event? (yes/no): yes
Add Event:
Enter date (YYYY-MM-DD): 2024-07-08
Enter start time (HH:MM): 22:00
Enter end time (HH:MM): 07:00
Enter event description: Sleep
Event added successfully.
Do you want to add an event? (yes/no): no
2024-07-08:
  - 06:15 - 07:00 : Wake up
  - 07:15 - 08:00 : Morning Discussion
  - 08:00 - 09:00 : Breakfast
  - 09:15 - 12:15 : Morning Work
  - 12:15 - 13:15 : Lunch
  - 13:15 - 18:00 : Afternoon Work
  - 19:00 - 21:00 : Relax + Dinner
  - 22:00 - 07:00 : Sleep
2024-07-09:
  No events scheduled.

2024-07-10:
  No events scheduled.

2024-07-11:
  No events scheduled.

2024-07-12:
  No events scheduled.

2024-07-13:
  No events scheduled.

2024-07-14:
  No events scheduled.

このように、私はこのプログラムコードを作って、昨日2024年7月8日の予定を出力してみた。2024年7月9日から2024年7月14日の予定をプログラムコードに入力していないので、"No events scheduled"と出力された。

終わりに

スケジュールを管理すれば、期限が設定された作業を期限内に終わらせることができます。スケジュール表に予定を書き込みながら情報を整理すれば、その日に行うべきことが明確化します。 同時に、今すぐには行わなくてもいいこと、数日後に行うべきことなど、「いつ」「何を」行うべきかを明確にできます。
引用元:https://slack.com/intl/ja-jp/blog/productivity/about-benefits-of-scheduling#:~:text=%E3%82%B9%E3%82%B1%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%E7%AE%A1%E7%90%86%E3%81%AE%E5%A4%A7%E3%81%8D%E3%81%AA%E7%9B%AE%E7%9A%84,%E3%82%92%E6%98%8E%E7%A2%BA%E3%81%AB%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%99%E3%80%82

皆さんもこのプログラムコードを参考にして、スケジュール帳を作って、1日1日を有意義に過ごしてみてはいかがでしょうか。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0