The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a day_week_year planner with Python

Creating a day/week/year planner in Python involves designing a program that can organize tasks or events by day, week, and year. Below is a detailed example of a simple command-line planner with the ability to add, view, and save plans for days, weeks, and years using Python.

This example will cover:

  • Storing tasks/events by date

  • Viewing tasks by day, week, or year

  • Saving and loading data to/from a file (JSON format)


python
import json from datetime import datetime, timedelta from collections import defaultdict class Planner: def __init__(self, filename="planner_data.json"): self.filename = filename self.tasks = defaultdict(list) # key: 'YYYY-MM-DD', value: list of tasks self.load() def add_task(self, date_str, task): """Add a task to a specific date.""" try: datetime.strptime(date_str, '%Y-%m-%d') # validate date format self.tasks[date_str].append(task) print(f"Task added on {date_str}.") except ValueError: print("Invalid date format. Use YYYY-MM-DD.") def view_day(self, date_str): """View tasks for a single day.""" tasks = self.tasks.get(date_str, []) print(f"Tasks for {date_str}:") if tasks: for i, task in enumerate(tasks, 1): print(f" {i}. {task}") else: print(" No tasks for this day.") def view_week(self, start_date_str): """View tasks for a week starting from the given date.""" try: start_date = datetime.strptime(start_date_str, '%Y-%m-%d') print(f"Tasks for the week starting {start_date_str}:") for i in range(7): current_date = start_date + timedelta(days=i) date_str = current_date.strftime('%Y-%m-%d') tasks = self.tasks.get(date_str, []) print(f"n{date_str} ({current_date.strftime('%A')}):") if tasks: for j, task in enumerate(tasks, 1): print(f" {j}. {task}") else: print(" No tasks.") except ValueError: print("Invalid date format. Use YYYY-MM-DD.") def view_year(self, year): """View tasks for the entire year.""" print(f"Tasks for the year {year}:") found = False for date_str in sorted(self.tasks.keys()): if date_str.startswith(str(year)): found = True tasks = self.tasks[date_str] date_obj = datetime.strptime(date_str, '%Y-%m-%d') print(f"n{date_str} ({date_obj.strftime('%A')}):") for i, task in enumerate(tasks, 1): print(f" {i}. {task}") if not found: print(" No tasks for this year.") def save(self): """Save tasks to JSON file.""" with open(self.filename, 'w') as f: json.dump(self.tasks, f) print("Planner data saved.") def load(self): """Load tasks from JSON file.""" try: with open(self.filename, 'r') as f: data = json.load(f) # Convert keys to str and values to list self.tasks = defaultdict(list, {str(k): v for k, v in data.items()}) print("Planner data loaded.") except (FileNotFoundError, json.JSONDecodeError): print("No saved planner data found. Starting fresh.") def main(): planner = Planner() while True: print("nPlanner Menu:") print("1. Add Task") print("2. View Day") print("3. View Week") print("4. View Year") print("5. Save and Exit") choice = input("Choose an option (1-5): ").strip() if choice == '1': date_str = input("Enter date (YYYY-MM-DD): ").strip() task = input("Enter task description: ").strip() planner.add_task(date_str, task) elif choice == '2': date_str = input("Enter date (YYYY-MM-DD): ").strip() planner.view_day(date_str) elif choice == '3': start_date_str = input("Enter start date of the week (YYYY-MM-DD): ").strip() planner.view_week(start_date_str) elif choice == '4': year_str = input("Enter year (YYYY): ").strip() if year_str.isdigit() and len(year_str) == 4: planner.view_year(year_str) else: print("Invalid year format.") elif choice == '5': planner.save() print("Goodbye!") break else: print("Invalid choice. Please select 1-5.") if __name__ == "__main__": main()

How this planner works:

  • You can add tasks on any date by entering the date in YYYY-MM-DD format.

  • You can view tasks for a specific day, week (7 days starting from a given date), or the entire year.

  • Tasks are stored in a JSON file (planner_data.json) so they persist across sessions.

  • The planner uses defaultdict(list) to store multiple tasks per date.

  • The interface is command-line based and simple to extend.

You can run this script in any Python 3 environment. It provides a basic but effective planning tool organized by day, week, and year.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About