forked from mergeos-bounties/PlantGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_solution.py
More file actions
5 lines (4 loc) · 8.12 KB
/
Copy pathai_solution.py
File metadata and controls
5 lines (4 loc) · 8.12 KB
1
2
3
4
5
I'll analyze this bounty and implement a care calendar export feature for PlantGuide that generates fertilize/repot schedules in JSON and ICS formats.
```json
{
"solution_code": "# plantguide/calendar.py\n\"\"\"Care calendar generation for plant species collections.\n\nGenerates multi-month fertilize and repot schedules based on species care requirements.\n\"\"\"\n\nfrom dataclasses import dataclass\nfrom datetime import datetime, timedelta\nfrom typing import List, Dict, Any\nimport json\nfrom enum import Enum\n\n\nclass CareEventType(Enum):\n \"\"\"Types of care events.\"\"\"\n FERTILIZE = \"fertilize\"\n REPOT = \"repot\"\n\n\nclass Season(Enum):\n \"\"\"Seasons for care scheduling.\"\"\"\n SPRING = \"spring\"\n SUMMER = \"summer\"\n FALL = \"fall\"\n WINTER = \"winter\"\n\n\n@dataclass\nclass CareEvent:\n \"\"\"A single care event in the calendar.\"\"\"\n species: str\n event_type: CareEventType\n date: datetime\n notes: str = \"\"\n \n def to_dict(self) -> Dict[str, Any]:\n \"\"\"Convert to dictionary for JSON export.\"\"\"\n return {\n \"species\": self.species,\n \"event_type\": self.event_type.value,\n \"date\": self.date.isoformat(),\n \"notes\": self.notes\n }\n\n\nclass CareScheduleRules:\n \"\"\"Rules for generating care schedules based on plant needs.\"\"\"\n \n # Fertilize frequency (weeks between applications)\n FERTILIZE_INTERVALS = {\n \"heavy_feeder\": 2, # Every 2 weeks during growing season\n \"moderate\": 4, # Monthly\n \"light_feeder\": 8, # Every 2 months\n \"minimal\": 12 # Every 3 months\n }\n \n # Repot frequency (months)\n REPOT_INTERVALS = {\n \"fast_growing\": 12, # Annually\n \"moderate\": 24, # Every 2 years\n \"slow_growing\": 36 # Every 3 years\n }\n \n # Growing seasons (fertilize active during these)\n GROWING_SEASONS = [Season.SPRING, Season.SUMMER]\n \n @staticmethod\n def get_season(date: datetime) -> Season:\n \"\"\"Determine season from date (Northern Hemisphere).\"\"\"\n month = date.month\n if month in [3, 4, 5]:\n return Season.SPRING\n elif month in [6, 7, 8]:\n return Season.SUMMER\n elif month in [9, 10, 11]:\n return Season.FALL\n else:\n return Season.WINTER\n \n @staticmethod\n def is_growing_season(date: datetime) -> bool:\n \"\"\"Check if date is in growing season.\"\"\"\n return CareScheduleRules.get_season(date) in CareScheduleRules.GROWING_SEASONS\n\n\nclass CareCalendar:\n \"\"\"Generate care calendars for plant collections.\"\"\"\n \n def __init__(self, start_date: datetime = None):\n \"\"\"Initialize calendar with optional start date.\n \n Args:\n start_date: Start date for calendar generation (defaults to today)\n \"\"\"\n self.start_date = start_date or datetime.now()\n self.events: List[CareEvent] = []\n \n def add_species(\n self,\n species_name: str,\n fertilize_needs: str = \"moderate\",\n growth_rate: str = \"moderate\",\n months: int = 12,\n last_repot_date: datetime = None\n ) -> None:\n \"\"\"Add a species to the care calendar.\n \n Args:\n species_name: Name of the plant species\n fertilize_needs: Fertilization needs (heavy_feeder, moderate, light_feeder, minimal)\n growth_rate: Growth rate (fast_growing, moderate, slow_growing)\n months: Number of months to generate schedule for\n last_repot_date: Date of last repotting (defaults to start_date)\n \"\"\"\n # Generate fertilize events\n if fertilize_needs in CareScheduleRules.FERTILIZE_INTERVALS:\n interval_weeks = CareScheduleRules.FERTILIZE_INTERVALS[fertilize_needs]\n self._generate_fertilize_events(\n species_name, interval_weeks, months, fertilize_needs\n )\n \n # Generate repot events\n if growth_rate in CareScheduleRules.REPOT_INTERVALS:\n interval_months = CareScheduleRules.REPOT_INTERVALS[growth_rate]\n self._generate_repot_events(\n species_name, interval_months, months, last_repot_date or self.start_date\n )\n \n def _generate_fertilize_events(\n self, \n species_name: str, \n interval_weeks: int, \n total_months: int,\n fertilize_needs: str\n ) -> None:\n \"\"\"Generate fertilize events for growing season.\"\"\"\n current_date = self.start_date\n end_date = self.start_date + timedelta(days=total_months * 30)\n \n while current_date <= end_date:\n if CareScheduleRules.is_growing_season(current_date):\n notes = f\"Fertilize ({fertilize_needs} - every {interval_weeks} weeks during growing season)\"\n self.events.append(\n CareEvent(\n species=species_name,\n event_type=CareEventType.FERTILIZE,\n date=current_date,\n notes=notes\n )\n )\n current_date += timedelta(weeks=interval_weeks)\n \n def _generate_repot_events(\n self,\n species_name: str,\n interval_months: int,\n total_months: int,\n last_repot_date: datetime\n ) -> None:\n \"\"\"Generate repot events based on growth rate.\"\"\"\n # Calculate next repot date from last repot\n next_repot = last_repot_date + timedelta(days=interval_months * 30)\n end_date = self.start_date + timedelta(days=total_months * 30)\n \n while next_repot <= end_date:\n # Try to schedule repot in spring (March-May)\n repot_date = next_repot\n month = repot_date.month\n if month < 3:\n repot_date = repot_date.replace(month=3)\n elif month > 5:\n repot_date = repot_date.replace(month=4)\n \n notes = f\"Repot (every {interval_months} months, preferably in spring)\"\n self.events.append(\n CareEvent(\n species=species_name,\n event_type=CareEventType.REPOT,\n date=repot_date,\n notes=notes\n )\n )\n next_repot += timedelta(days=interval_months * 30)\n \n def export_json(self) -> str:\n \"\"\"Export calendar as JSON.\n \n Returns:\n JSON string of all care events\n \"\"\"\n sorted_events = sorted(self.events, key=lambda e: e.date)\n return json.dumps(\n {\n \"generated_at\": datetime.now().isoformat(),\n \"start_date\": self.start_date.isoformat(),\n \"events\": [event.to_dict() for event in sorted_events]\n },\n indent=2\n )\n \n def export_ics(self) -> str:\n \"\"\"Export calendar as ICS (iCalendar) format.\n \n Returns:\n ICS format string\n \"\"\"\n lines = [\n \"BEGIN:VCALENDAR\",\n \"VERSION:2.0\",\n \"PRODID:-//PlantGuide//Care Calendar//EN\",\n \"CALSCALE:GREGORIAN\",\n \"METHOD:PUBLISH\",\n \"X-WR-CALNAME:Plant Care Calendar\",\n \"X-WR-TIMEZONE:UTC\"\n ]\n \n sorted_events = sorted(self.events, key=lambda e: e.date)\n \n for event in sorted_events:\n lines.extend(self._event_to_ics(event))\n \n lines.append(\"END:VCALENDAR\")\n return \"\\r\\n\".join(lines)\n \n def _event_to_ics(self, event: CareEvent) -> List[str]:\n \"\"\"Convert a care event to ICS VEVENT format.\"\"\"\n # Generate unique ID\n uid = f\"{event.species}-{event.event_type.value}-{event.date.strftime('%Y%m%d')}@plantguide\"\n \n # Format datetime for ICS (YYYYMMDD)\n date_str