{"id":17385,"date":"2024-01-21T06:27:21","date_gmt":"2024-01-20T21:27:21","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=17385"},"modified":"2024-01-21T06:27:23","modified_gmt":"2024-01-20T21:27:23","slug":"python-%e3%82%bf%e3%82%b9%e3%82%af","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=17385","title":{"rendered":"python \u30bf\u30b9\u30af"},"content":{"rendered":"\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>import os\r\nimport json\r\nfrom datetime import datetime\r\n\r\nclass Task:\r\n    def __init__(self, description, deadline=None, completed=False):\r\n        self.description = description\r\n        self.deadline = deadline\r\n        self.completed = completed\r\n\r\n    def to_dict(self):\r\n        return {\r\n            &quot;description&quot;: self.description,\r\n            &quot;deadline&quot;: self.deadline.strftime(&quot;%Y-%m-%d %H:%M&quot;) if self.deadline else None,\r\n            &quot;completed&quot;: self.completed\r\n        }\r\n\r\n    @classmethod\r\n    def from_dict(cls, task_dict):\r\n        deadline_str = task_dict.get(&quot;deadline&quot;)\r\n        deadline = datetime.strptime(deadline_str, &quot;%Y-%m-%d %H:%M&quot;) if deadline_str else None\r\n        return cls(task_dict[&quot;description&quot;], deadline, task_dict[&quot;completed&quot;])\r\n\r\nclass TaskList:\r\n    def __init__(self, filename):\r\n        self.filename = filename\r\n        self.tasks = []\r\n        self.load_tasks()\r\n\r\n    def load_tasks(self):\r\n        if os.path.exists(self.filename):\r\n            with open(self.filename, &#39;r&#39;) as file:\r\n                task_data = json.load(file)\r\n                self.tasks = [Task.from_dict(task_dict) for task_dict in task_data]\r\n\r\n    def save_tasks(self):\r\n        with open(self.filename, &#39;w&#39;) as file:\r\n            task_data = [task.to_dict() for task in self.tasks]\r\n            json.dump(task_data, file, indent=4)\r\n\r\n    def add_task(self, description, deadline_str=None):\r\n        deadline = datetime.strptime(deadline_str, &quot;%Y-%m-%d %H:%M&quot;) if deadline_str else None\r\n        task = Task(description, deadline)\r\n        self.tasks.append(task)\r\n        self.save_tasks()\r\n        print(&quot;\u30bf\u30b9\u30af\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f&quot;)\r\n\r\n    def edit_task(self, task_index, description=None, deadline_str=None):\r\n        if 1 &lt;= task_index &lt;= len(self.tasks):\r\n            task = self.tasks[task_index - 1]\r\n            if description:\r\n                task.description = description\r\n            if deadline_str:\r\n                task.deadline = datetime.strptime(deadline_str, &quot;%Y-%m-%d %H:%M&quot;) if deadline_str else None\r\n            self.save_tasks()\r\n            print(&quot;\u30bf\u30b9\u30af\u3092\u7de8\u96c6\u3057\u307e\u3057\u305f&quot;)\r\n        else:\r\n            print(&quot;\u7121\u52b9\u306a\u30bf\u30b9\u30af\u756a\u53f7\u3067\u3059&quot;)\r\n\r\n    def display_tasks(self):\r\n        if not self.tasks:\r\n            print(&quot;\u30bf\u30b9\u30af\u306f\u3042\u308a\u307e\u305b\u3093&quot;)\r\n        else:\r\n            print(&quot;\u30bf\u30b9\u30af\u4e00\u89a7:&quot;)\r\n            for i, task in enumerate(self.tasks, 1):\r\n                status = &quot;\u5b8c\u4e86&quot; if task.completed else &quot;\u672a\u5b8c\u4e86&quot;\r\n                deadline = task.deadline.strftime(&quot;%Y-%m-%d %H:%M&quot;) if task.deadline else &quot;\u306a\u3057&quot;\r\n                print(f&quot;{i}. [{status}] {task.description} (\u7de0\u5207: {deadline})&quot;)\r\n\r\n    def mark_task_completed(self, task_index):\r\n        if 1 &lt;= task_index &lt;= len(self.tasks):\r\n            task = self.tasks[task_index - 1]\r\n            task.completed = True\r\n            self.save_tasks()\r\n            print(f&quot;\u30bf\u30b9\u30af &#39;{task.description}&#39; \u3092\u5b8c\u4e86\u306b\u3057\u307e\u3057\u305f&quot;)\r\n        else:\r\n            print(&quot;\u7121\u52b9\u306a\u30bf\u30b9\u30af\u756a\u53f7\u3067\u3059&quot;)\r\n\r\n    def delete_task(self, task_index):\r\n        if 1 &lt;= task_index &lt;= len(self.tasks):\r\n            deleted_task = self.tasks.pop(task_index - 1)\r\n            self.save_tasks()\r\n            print(f&quot;\u30bf\u30b9\u30af &#39;{deleted_task.description}&#39; \u3092\u524a\u9664\u3057\u307e\u3057\u305f&quot;)\r\n        else:\r\n            print(&quot;\u7121\u52b9\u306a\u30bf\u30b9\u30af\u756a\u53f7\u3067\u3059&quot;)\r\n\r\ndef main():\r\n    filename = &quot;tasks.json&quot;\r\n    task_list = TaskList(filename)\r\n\r\n    while True:\r\n        print(&quot;\\n\u64cd\u4f5c\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044:&quot;)\r\n        print(&quot;1. \u30bf\u30b9\u30af\u3092\u8ffd\u52a0&quot;)\r\n        print(&quot;2. \u30bf\u30b9\u30af\u3092\u7de8\u96c6&quot;)\r\n        print(&quot;3. \u30bf\u30b9\u30af\u4e00\u89a7\u3092\u8868\u793a&quot;)\r\n        print(&quot;4. \u30bf\u30b9\u30af\u3092\u5b8c\u4e86\u306b\u3059\u308b&quot;)\r\n        print(&quot;5. \u30bf\u30b9\u30af\u3092\u524a\u9664\u3059\u308b&quot;)\r\n        print(&quot;6. \u7d42\u4e86&quot;)\r\n\r\n        choice = input(&quot;\u9078\u629e (1\/2\/3\/4\/5\/6): &quot;)\r\n\r\n        if choice == &#39;6&#39;:\r\n            break\r\n        elif choice == &#39;1&#39;:\r\n            description = input(&quot;\u65b0\u3057\u3044\u30bf\u30b9\u30af\u306e\u8aac\u660e\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044: &quot;)\r\n            deadline_str = input(&quot;\u7de0\u5207\u65e5\u6642 (YYYY-MM-DD HH:MM) \u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044 (\u672a\u5165\u529b\u53ef): &quot;)\r\n            task_list.add_task(description, deadline_str)\r\n        elif choice == &#39;2&#39;:\r\n            task_list.display_tasks()\r\n            task_index = int(input(&quot;\u7de8\u96c6\u3059\u308b\u30bf\u30b9\u30af\u306e\u756a\u53f7\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044: &quot;))\r\n            description = input(&quot;\u65b0\u3057\u3044\u8aac\u660e\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044 (\u672a\u5165\u529b\u3067\u5909\u66f4\u306a\u3057): &quot;)\r\n            deadline_str = input(&quot;\u65b0\u3057\u3044\u7de0\u5207\u65e5\u6642 (YYYY-MM-DD HH:MM) \u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044 (\u672a\u5165\u529b\u3067\u5909\u66f4\u306a\u3057): &quot;)\r\n            task_list.edit_task(task_index, description, deadline_str)\r\n        elif choice == &#39;3&#39;:\r\n            task_list.display_tasks()\r\n        elif choice == &#39;4&#39;:\r\n            task_list.display_tasks()\r\n            task_index = int(input(&quot;\u5b8c\u4e86\u306b\u3059\u308b\u30bf\u30b9\u30af\u306e\u756a\u53f7\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044: &quot;))\r\n            task_list.mark_task_completed(task_index)\r\n        elif choice == &#39;5&#39;:\r\n            task_list.display_tasks()\r\n            task_index = int(input(&quot;\u524a\u9664\u3059\u308b\u30bf\u30b9\u30af\u306e\u756a\u53f7\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044: &quot;))\r\n            task_list.delete_task(task_index)\r\n        else:\r\n            print(&quot;\u7121\u52b9\u306a\u9078\u629e\u3067\u3059&quot;)\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    main()\r\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[4,65],"tags":[3,39],"class_list":["post-17385","post","type-post","status-publish","format-standard","hentry","category-programming","category-python","tag-programming","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17385"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17385\/revisions"}],"predecessor-version":[{"id":17386,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17385\/revisions\/17386"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17385"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}