这是一个与我以前的代码有关的问题。感谢所有帮助我找出如何递归删除章节的人!
一些背景故事
我正在写一个电报机器人,你可以在里面用电报帖子创建pdf(一个帖子最多可以包含4096个字符(。确实是一项非常具体的任务。在电报机器人程序中,只有包含按钮名称和id的内联按钮。机器人程序将章节列为内联按钮。至于子章节,你会不断点击,直到你到达你想要访问的特定章节。
例如,我想在chapter 1 -> subchapter 2
中添加一个名为">"的章节;子章节1">:
- 单击
chapter 1
- Bot重新渲染按钮,在第1章中列出子章节,然后单击
subchapter 2
- 在那里你添加了你的章节
下面是我的草稿,我为这个问题提出了试错:
from dataclasses import dataclass, field
from typing import List
@dataclass
class Chapter:
title: str
text: str = ''
removed: bool = False
chapters: List['Chapter'] = field(default_factory=list)
def add_chapter(self, title: str, where: List[str] = []):
'''the where is a path to a chapter'''
if not where:
# check the occurrences of a chapter
for chapter in self.chapters:
if chapter.title == title:
return False
self.chapters.append(Chapter(title))
return True
# recursively reach to a selected chapter
for chapter in self.chapters:
for x in where:
if chapter.title == x:
if len(where) == 1:
# check the occurrences of a chapter
for _ in chapter.chapters:
if _.title == title:
return False
# my small brain couldn't function at all here
chapter.chapters.append(Chapter(title))
return True
else:
self.add_chapter(where[1:])
def remove(self):
self.removed = True
for chapter in self.chapters:
chapter.remove()
class Content:
'''A mini database handler'''
def __init__(self, content: List[Chapter]) -> None:
self.content = content
def __len__(self) -> int:
i = 0
for x in self.content:
if not x.removed:
i += 1
return i
def add_chapter_name(self, title: str, where: List[str] = None):
if where:
for chapter in self.content:
if chapter.title == where[0] and not chapter.removed:
return chapter.add_chapter(title, where[1:])
for x in self.content:
if x.title == title and not x.removed:
return False
self.content.append(Chapter(title))
return True
# there is more
content = Content([
Chapter(
title='chapter 1',
chapters=[
Chapter('subchapter 1', "Lorem ipsum dolor"),
Chapter('subchapter 2', "Nullam a ligula")
]
),
Chapter(
'chapter 2',
chapters=[
Chapter('subchapter 1', "Fusce eget commodo augue"),
Chapter('subchapter 2', "Pellentesque pretium")
]
),
Chapter(
'chapter 3', removed=True
),
Chapter(
'chapter 3',
chapters=[
Chapter('subchapter 1', "Duis sit amet tempus lectus"),
]
),
])
content.content[0].remove()
content.add_chapter_name('chapter 1')
content.add_chapter_name('subchapter 1', ['chapter 1'])
if not content.add_chapter_name('subchapter 1', ['chapter 1']):
print("you can't")
content.add_chapter_name('subsubchapter 1', ['chapter 1', 'subchapter 1'])
# this will not insert the chapter to the expected place
content.add_chapter_name(
'subsubsubchapter 1',
['chapter 1', 'subchapter 1', 'subsubsubchapter 1']
)
再次感谢您的任何建议。真的,只要一个建议就足够了。我不想占用你太多时间。
这只是一个原型,但该方法可以按预期工作。
from typing import List
from dataclasses import dataclass, field
from typing import List
@dataclass
class Chapter:
title: str
text: str = ''
removed: bool = False
chapters: List['Chapter'] = field(default_factory=list)
content: List[Chapter] = [
Chapter(
title='chapter 1',
chapters=[
Chapter('subchapter 1', "Lorem ipsum dolor"),
Chapter('subchapter 2', "Nullam a ligula")
]
),
Chapter(
'chapter 2',
chapters=[
Chapter('subchapter 1', "Fusce eget commodo augue"),
Chapter('subchapter 2', "Pellentesque pretium")
]
),
Chapter(
'chapter 3', removed=True
),
Chapter(
'chapter 3',
chapters=[
Chapter('subchapter 1', "Duis sit amet tempus lectus"),
]
),
]
def add_chapter(content: List[Chapter], title: str, where: List[str] = None):
if where is None:
content.append(Chapter(title))
return True
if len(where) != 0:
if isinstance(content, list):
x = content
else:
x = content.chapters
for chapter in x:
if chapter.title == where[0]:
for _ in chapter.chapters:
if _.title == title:
return False
if len(where) != 1:
add_chapter(chapter, title, where[1:])
else:
chapter.chapters.append(Chapter(title))
return True
else:
return
add_chapter(content, 'chapter 4')
add_chapter(content, 'subchapter 1', ['chapter 4'])
if not add_chapter(content, 'subchapter 1', ['chapter 4']):
print("you can't")
add_chapter(content, 'subsubchapter 1', ['chapter 4', 'subchapter 1'])
# this will not insert the chapter to the expected place
# edit: now it works
add_chapter(
content,
'subsubsubchapter 1',
['chapter 4', 'subchapter 1', 'subsubchapter 1']
)