Skip to content

Commit

Permalink
🐛 修复示例解析异常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
snowykami committed Aug 30, 2024
1 parent acee820 commit caa39af
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
23 changes: 12 additions & 11 deletions litedoc/docstring/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "":
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 1 addition & 5 deletions litedoc/docstring/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 2 additions & 0 deletions litedoc/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@Software: PyCharm
"""
import os.path
import traceback
from typing import Optional

from litedoc.style.markdown import generate
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_modules/mbcp/mp_math/equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit caa39af

Please sign in to comment.