Zhilong Li 5 anni fa
commit
f1023520b5
3 ha cambiato i file con 177 aggiunte e 0 eliminazioni
  1. 4 0
      .gitignore
  2. 153 0
      Jt.py
  3. 20 0
      test_coord.py

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+/.pytest_cache
+/.vscode
+/__pycache__
+/coord

+ 153 - 0
Jt.py

@@ -0,0 +1,153 @@
+from hashlib import md5
+from os import listdir
+import pickle
+
+import random
+import time
+
+
+class Base:
+    def __init__(self):
+        self.create_time = time.localtime()
+        self.str_time = time.strftime("%Y%m%d%H%M%S", self.create_time)
+        self.__gene_id()
+
+    def __gene_id(self):
+        self.__id_seed: str = self.str_time + str(random.randint(10000, 99999))
+        self.id: str = md5(self.__id_seed.encode("UTF-8")).hexdigest()
+
+
+class Entry(Base):
+    def __init__(self):
+        Base.__init__(self)
+        self.title: str = "未命名事项" + self.str_time[4:8]
+        Base.__gene_id(self)
+        self.content: str = "在这里输入事项内容"
+        self.set_priority()
+        self.set_deadline()
+
+        self.sub_entries_dict: dict = {}
+
+    def set_priority(self, priority: float = 0):
+        if priority < -10:
+            print("最低优先级为-10,已自动设置为-10")
+            priority = -10.0
+        elif priority > 10:
+            print("最高优先级为10,已自动设置为10")
+            priority = 10.0
+        self.priority: float = priority
+
+    def set_deadline(self, deadline_date: time.struct_time = time.localtime()):
+        if deadline_date < time.localtime():
+            print("\033[1;31mERROR: Let it RIP if it's dead already\033[0m")
+            return 1
+        self.deadline = deadline_date
+        return 0
+
+    def set_content(self, content: str):
+        self.content = content
+
+
+class Coordinate(Base):
+    def __init__(self):
+        Base.__init__(self)
+        self.name = "未命名坐标系" + self.str_time
+        # Base.__gene_id(self)
+        self.entries_dict = {}
+        self.entries_num = 0
+
+    def show_entries(self):
+        print(self.entries_dict)
+
+    def add_entry(self, entry_in: Entry):
+        self.entries_dict[entry_in.entry_id] = entry_in
+        self.entries_num += 1
+
+    def del_entry(self, entry_in: Entry):
+        try:
+            del self.entries_dict[entry_in.entry_id]
+            self.entries_num -= 1
+        except KeyError:
+            print("\033[1;31mERROR: Entry doesn't exist\033[0m")
+
+    def save_coord(self):
+        with open("coord/" + self.name + ".crd", "wb") as f:
+            pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
+
+    def __str__(self) -> str:
+        return (
+            "Title: "
+            + self.name
+            + "\nId: "
+            + self.id
+            + "\nEntries number: "
+            + str(self.entries_num)
+        )
+
+
+class Interface:
+    def __init__(self):
+        self.coordinate_dict: dict = {}
+        self.exit: bool = False
+        self.func_dict = {
+            "exit": self.__exit,
+            "open": self.__open_coord,
+            "ls": self.__list_coord,
+            "show": self.__show_now_coord,
+            "entry": self.__new_entry,
+        }
+
+    def start(self):
+        print("Welcome to Jt mission manager")
+        print("Please open a coordinate")
+        self.__list_coord()
+        coord_file = self.files[
+            int(input("\033[1;32mType in the number of coordinate:\033[0m"))
+        ]
+        self.__open_coord("coord/" + coord_file)
+        while not (self.exit):
+            command = input("\033[1;32m> \033[0m").split()
+            func_name = command[0]
+            try:
+                func = self.func_dict[func_name]
+                func(command[1:])
+            except KeyError:
+                print("\033[1;31mERROR: Command not found\033[0m")
+
+    def __open_coord(self, coord_file: str):
+        with open(coord_file, "rb") as f:
+            self.now_coord = pickle.load(f)
+
+    def __list_coord(self):
+        self.files = listdir("coord")
+        if self.files:
+            i = 0
+            for f in self.files:
+                print(i, f)
+                i += 1
+
+    def __new_coord(self, arg: list):
+        c = Coordinate()
+        c.name = arg[0]
+        raise NotImplementedError
+
+    def __show_now_coord(self, arg: list):
+        print(self.now_coord)
+
+    def __add_coordinate(self, c: Coordinate):
+        if not c.id in self.coordinate_dict:
+            self.coordinate_dict[c.id] = c
+        else:
+            print("\033[1;31mERROR: It's already in there\033[0m")
+
+    def __exit(self, arg):
+        self.exit = True
+
+
+def main():
+    i = Interface()
+    i.start()
+
+
+if __name__ == "__main__":
+    main()

+ 20 - 0
test_coord.py

@@ -0,0 +1,20 @@
+import pickle
+from Jt import Coordinate
+import os
+
+
+def test_save_and_load():
+    coord_list = os.listdir("coord")
+    if "testCoord.crd" in coord_list:
+        print("Removing existing test file...")
+        os.remove("coord/testCoord.crd")
+    c = Coordinate()
+    c.name = "testCoord"
+    c.save_coord()
+
+    with open("coord/" + c.name + ".crd", "rb") as f:
+        cc = pickle.load(f)
+
+    os.remove("coord/testCoord.crd")
+    assert cc.name == "testCoord"
+