规范代码

This commit is contained in:
2025-08-06 06:11:22 +08:00
parent 2cf2cdb33f
commit dd74dddf00
7 changed files with 344 additions and 261 deletions

View File

@@ -1,6 +1,16 @@
from textual.app import App, ComposeResult
from textual.events import Event
from textual.widgets import Collapsible, Header, Footer, Markdown, ListView, ListItem, Label, Static, Button
from textual.widgets import (
Collapsible,
Header,
Footer,
Markdown,
ListView,
ListItem,
Label,
Static,
Button,
)
from textual.containers import Container, Horizontal, Center
from textual.screen import Screen
from textual.widget import Widget
@@ -12,56 +22,70 @@ import re
import random
import copy
class Composition():
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict] = pt.Atom.placeholder()):
class Composition:
def __init__(
self,
screen: Screen,
reactor,
atom: Tuple[pt.Electron, pt.Nucleon, Dict] = pt.Atom.placeholder(),
):
self.screen = screen
self.atom = atom
self.atom = atom
from reactor import Reactor
self.reactor: Reactor = reactor
self.reg = dict()
def regid(self, id_):
self.reg[id_] = id_ + str(uuid.uuid4())
return self.reg[id_]
def getid(self, id_):
if id_ not in self.reg.keys():
return "None"
return self.reg[id_]
def recid(self, id_):
return id_[:-36]
def compose(self):
yield Label("示例标签", id="testlabel")
yield Button("示例按钮", id="testbtn")
def handler(self, event, type_):
return 1
#if hasattr(event, "button"):
#print(event.button.id)
# self.screen.query_one("#testlabel", Label).update("hi")
class Finished(Composition):
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
super().__init__(screen, reactor, atom)
def compose(self):
yield Label("本次记忆进程结束", id=self.regid("msg"))
#yield Button("示例按钮", id="testbtn")
class Placeholder(Composition):
def __init__(self, screen: Screen):
self.screen = screen
def compose(self):
yield Label("示例标签", id="testlabel")
yield Button("示例按钮", id="testbtn", classes="choice")
def handler(self, event, type_):
#print(event.button.id)
self.screen.query_one("#testlabel", Label).update("hi")
class Recognition(Composition):
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
super().__init__(screen, reactor, atom)
def compose(self):
with Center():
yield Static(f"[dim]{self.atom[1]['translation']}[/]")
yield Label(f"")
s = str(self.atom[1]['content'])
s = str(self.atom[1]["content"])
replace_dict = {
", ": ",",
". ": ".",
@@ -75,132 +99,153 @@ class Recognition(Composition):
}
for old, new in replace_dict.items():
s = s.replace(old, new)
result = re.split(r"(?<=[,;:|])", s.replace('/', ' '))
result = re.split(r"(?<=[,;:|])", s.replace("/", " "))
for i in result:
with Center():
yield Label(f"[b][b]{i.replace("/", " ")}[/][/]", id=self.regid("sentence"+str(hash(i)))) # 致敬传奇去重串 uuid
#with Collapsible(title="附加信息", collapsed=True):
yield Label(
f"[b][b]{i.replace('/', ' ')}[/][/]",
id=self.regid("sentence" + str(hash(i))),
)
for i in self.atom[2]["testdata"]["additional_inf"]:
if self.atom[1][i]:
#print(type(self.atom[1][i]))
#print(self.atom[1][i])
if isinstance(self.atom[1][i], list):
for j in self.atom[1][i]:
yield Markdown(f"### {self.atom[2]['keydata'][i]}: {j}")
continue
if isinstance(self.atom[1][i], Dict):
t = ""
for j, k in self.atom[1][i].items():
for j, k in self.atom[1][i].items(): # type: ignore
# 弱智的 Pylance 类型推导
t += f"> **{j}**: {k} \n"
yield Markdown(t, id=self.regid("tran"))
with Center():
yield Button("我已知晓", id=self.regid("ok"))
def handler(self, event, type_):
##print(event)
if type_ == "button":
#print(event.button.id)
if event.button.id == self.getid("ok"):
#print(1)
""""""#assessment = self.reactor.report(self.reactor.current_atom, -1)
return 0
return 0
if type_ == 1:
pass
return -1
class BasicEvaluation(Composition):
# 不再使用, 仅作为测试
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
super().__init__(screen, reactor, atom)
def compose(self):
yield Label(self.atom[1]["content"], id="sentence")
with Container(id="button_container"):
btn = {}
btn['5'] = Button("完美回想", variant="success", id=self.regid("feedback5"), classes="choice")
btn['4'] = Button("犹豫后正确", variant="success", id=self.regid("feedback4"), classes="choice")
btn['3'] = Button("困难地正确", variant="warning", id=self.regid("feedback3"), classes="choice")
btn['2'] = Button("错误但熟悉", variant="warning", id=self.regid("feedback2"), classes="choice")
btn['1'] = Button("错误且不熟", variant="error", id=self.regid("feedback1"), classes="choice")
btn['0'] = Button("完全空白", variant="error", id=self.regid("feedback0"), classes="choice")
yield Horizontal(btn['5'], btn['4'])
yield Horizontal(btn['3'], btn['2'])
yield Horizontal(btn['1'], btn['0'])
btn["5"] = Button(
"完美回想", variant="success", id=self.regid("feedback5"), classes="choice"
)
btn["4"] = Button(
"犹豫后正确", variant="success", id=self.regid("feedback4"), classes="choice"
)
btn["3"] = Button(
"困难地正确", variant="warning", id=self.regid("feedback3"), classes="choice"
)
btn["2"] = Button(
"错误但熟悉", variant="warning", id=self.regid("feedback2"), classes="choice"
)
btn["1"] = Button(
"错误且不熟", variant="error", id=self.regid("feedback1"), classes="choice"
)
btn["0"] = Button(
"完全空白", variant="error", id=self.regid("feedback0"), classes="choice"
)
yield Horizontal(btn["5"], btn["4"])
yield Horizontal(btn["3"], btn["2"])
yield Horizontal(btn["1"], btn["0"])
def handler(self, event, type_):
if "feedback" in event.button.id:
#print(self.recid(event.button.id)[8:9])
assess = int(self.recid(event.button.id)[8:9])
ret = self.reactor.report(self.atom, assess)
return ret
class FillBlank(Composition):
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
super().__init__(screen, reactor, atom)
self.inputlist = []
self.hashtable = {}
self._work()
def _work(self):
self.puzzle = pz.BlankPuzzle(self.atom[1]["content"], 4)
self.puzzle.refresh()
self.ans = copy.copy(self.puzzle.answer)
random.shuffle(self.ans)
def compose(self):
yield Label(self.puzzle.wording, id=self.regid("sentence"))
yield Label(f"当前输入: {self.inputlist}", id=self.regid("inputpreview"))
#yield Label(renderable=f"答案: {self.puzzle.answer}", id=self.regid("ans"))
for i in self.ans:
self.hashtable[str(hash(i))] = i
yield Button(i, id=self.regid(f"select{hash(i)}"))
yield Button("退格", id=self.regid(f"delete"))
def handler(self, event, type_):
if type_ == "button":
if self.recid(event.button.id) == "delete" and len(self.inputlist) > 0:
self.inputlist.pop()
if self.recid(event.button.id) == "delete":
if len(self.inputlist) > 0:
self.inputlist.pop()
else:
return 1
else:
self.inputlist.append(self.hashtable[self.recid(event.button.id)[6:]])
if len(self.inputlist) < len(self.puzzle.answer):
return 1
else:
if self.inputlist == self.puzzle.answer:
print("ok")
return 0
else:
self.inputlist = []
return 1
class DrawCard(Composition):
def __init__(self, screen: Screen, reactor, atom: Tuple[pt.Electron, pt.Nucleon, Dict]):
super().__init__(screen, reactor, atom)
self.inputlist = []
self.hashtable = {}
self._work()
def _work(self):
self.puzzle = pz.SelectionPuzzle(self.atom[1]["keyword_note"], [], 2, "选择正确词义: ")
self.puzzle = pz.SelectionPuzzle(self.atom[1]["keyword_note"], [], 2, "选择正确词义: ") # type: ignore
self.puzzle.refresh()
def compose(self):
print(len(self.inputlist))
yield Label(self.atom[1].content, id=self.regid("sentence"))
yield Label(self.atom[1].content.replace("/",""), id=self.regid("sentence"))
yield Label(self.puzzle.wording[len(self.inputlist)], id=self.regid("puzzle"))
yield Label(f"当前输入: {self.inputlist}", id=self.regid("inputpreview"))
yield Label(renderable=f"答案: {self.puzzle.answer}", id=self.regid("ans"))
for i in self.puzzle.options[len(self.inputlist)]:
self.hashtable[str(hash(i))] = i
yield Button(i, id=self.regid(f"select{hash(i)}"))
yield Button("退格", id=self.regid(f"delete"))
def handler(self, event, type_):
if type_ == "button":
if self.recid(event.button.id) == "delete" and len(self.inputlist) > 0:
self.inputlist.pop()
if self.recid(event.button.id) == "delete":
if len(self.inputlist) > 0:
self.inputlist.pop()
else:
return 1
else:
self.inputlist.append(self.hashtable[self.recid(event.button.id)[6:]])
if len(self.inputlist) < len(self.puzzle.answer):
return 1
else:
if self.inputlist == self.puzzle.answer:
print("ok")
return 0
else:
self.inputlist = []
return 1
registry = {
"sample": Composition,
"recognition": Recognition,
@@ -210,16 +255,16 @@ registry = {
}
# TEST
class TestScreen(Screen):
def __init__(self):
super().__init__(name=None, id=None, classes=None)
self.comp = Recognition(self, None, pt.Atom.advanced_placeholder())
self.comp = Recognition(self, None, pt.Atom.advanced_placeholder())
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
yield from self.comp.compose()
yield Footer()
def on_mount(self) -> None:
pass
@@ -229,9 +274,10 @@ class TestScreen(Screen):
def action_quit_app(self) -> None:
self.app.exit()
class AppLauncher(App):
CSS_PATH = "styles.tcss"
TITLE = '测试布局'
TITLE = "测试布局"
BINDINGS = [("escape", "quit", "退出"), ("d", "toggle_dark", "改变色调")]
SCREENS = {
"testscreen": TestScreen,
@@ -241,6 +287,7 @@ class AppLauncher(App):
self.action_toggle_dark()
self.push_screen("testscreen")
if __name__ == "__main__":
app = AppLauncher()
app.run()
app.run()