From caa39afd135f6d06c5b1e24a7c9d224732b6d43a Mon Sep 17 00:00:00 2001 From: snowykami Date: Sat, 31 Aug 2024 05:25:37 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=BC=82=E5=B8=B8=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- litedoc/docstring/docstring.py | 23 +++++++++++---------- litedoc/docstring/parser.py | 6 +----- litedoc/output.py | 2 ++ tests/test_modules/mbcp/mp_math/equation.py | 7 +++++++ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/litedoc/docstring/docstring.py b/litedoc/docstring/docstring.py index 73ddba8..5c87b4d 100644 --- a/litedoc/docstring/docstring.py +++ b/litedoc/docstring/docstring.py @@ -53,7 +53,7 @@ class Docstring(BaseModel): attrs: list[Attr] = [] return_: Optional[Return] = None raise_: list[Exception_] = [] - example: list[Example] = [] + example: Optional[str] = None def add_desc(self, desc: str): if self.desc == "": @@ -73,8 +73,11 @@ def add_return(self, desc: str = ""): def add_raise(self, name: str, desc: str = ""): self.raise_.append(Exception_(name=name, desc=desc)) - def add_example(self, desc: str = "", input_: str = "", output: str = ""): - self.example.append(Example(desc=desc, input=input_, output=output)) + def add_example(self, desc: str = ""): + if self.example is None: + self.example = desc + else: + self.example += "\n" + desc def reduction(self, style: str = "google") -> str: """ @@ -106,15 +109,13 @@ def reduction(self, style: str = "google") -> str: if self.example: ret += "Examples:\n" - for example in self.example: - ret += f" {example.desc}\n Input: {example.input}\n Output: {example.output}\n" + ret += f" {self.example}\n" return ret - def markdown(self, lang: str, indent: int = 4, is_classmethod: bool = False) -> str: + def markdown(self, lang: str, indent: int = 4) -> str: """ 生成markdown文档 Args: - is_classmethod: lang: indent: @@ -144,14 +145,14 @@ def markdown(self, lang: str, indent: int = 4, is_classmethod: bool = False) -> if self.return_ is not None: ret += PREFIX + f"\n**{get_text(lang, 'docstring.return')}**: {self.return_.desc}\n" # 复数属性 + if self.example: + ret += PREFIX + f"\n**{get_text(lang, 'docstring.example')}**:\n" + ret += self.example + "\n" if self.raise_: ret += PREFIX + f"\n**{get_text(lang, 'docstring.raises')}**:\n" for exception in self.raise_: ret += PREFIX + f"> - {exception.name} {exception.desc}\n" - if self.example: - ret += PREFIX + f"\n**{get_text(lang, 'docstring.example')}**:\n" - for example in self.example: - ret += PREFIX + f" - {example.desc}\n> **{get_text(lang, 'docs.input')}**: {example.input}\n> **{get_text(lang, 'docs.output')}**: {example.output}\n" + return ret def __str__(self): diff --git a/litedoc/docstring/parser.py b/litedoc/docstring/parser.py index 8b00924..6ceb666 100644 --- a/litedoc/docstring/parser.py +++ b/litedoc/docstring/parser.py @@ -113,11 +113,7 @@ def parse_example(self): 解析示例行 """ while line := self.match_next_line(): - if ":" in line: - name, desc = line.split(":", 1) - self.docstring.add_example(name.strip(), desc.strip()) - else: - self.docstring.add_example(line.strip()) + self.docstring.add_example(line.strip()) def parse_attrs(self): """ diff --git a/litedoc/output.py b/litedoc/output.py index 5563bcc..920eed3 100644 --- a/litedoc/output.py +++ b/litedoc/output.py @@ -9,6 +9,7 @@ @Software: PyCharm """ import os.path +import traceback from typing import Optional from litedoc.style.markdown import generate @@ -127,6 +128,7 @@ def generate_from_module(module_folder: str, generate_file_count += 1 except Exception as e: print(f"Error in {pyfile_path}: {e}") + traceback.print_exc() for fn, content in file_data.items(): write_to_file(content, fn) diff --git a/tests/test_modules/mbcp/mp_math/equation.py b/tests/test_modules/mbcp/mp_math/equation.py index c6112b9..301c8f1 100644 --- a/tests/test_modules/mbcp/mp_math/equation.py +++ b/tests/test_modules/mbcp/mp_math/equation.py @@ -126,6 +126,13 @@ def curry(func: MultiVarsFunc, *args: Var) -> OneVarFunc: *args: 参数 Returns: 柯里化后的函数 + Examples: + ```python + def add(a: int, b: int, c: int) -> int: + return a + b + c + add_curried = curry(add, 1, 2) + add_curried(3) # 6 + ``` """ def curried_func(*args2: Var) -> Var: