|
1 | 1 | from ..library import Class |
2 | 2 |
|
3 | 3 |
|
4 | | -def to_string(structure: Class) -> str: |
5 | | - indent = " " * 4 |
6 | | - |
7 | | - def attributes(attributes): |
8 | | - attrs = [] |
9 | | - for name, definitions in attributes.items(): |
10 | | - value = definitions[-1].value |
11 | | - attrs.append(f"{indent}{name} = {value}\n") |
12 | | - return "".join(attrs) |
13 | | - |
14 | | - def declaration(name, parents): |
15 | | - parents = ", ".join([p.__name__ for p in parents]) |
16 | | - return f"class {name}({parents}):" |
17 | | - |
18 | | - def docstring(docstring): |
19 | | - quotes = f'{indent}"""\n' |
20 | | - lines = docstring.split("\n") |
21 | | - block = "".join([f"{indent}{line}\n" for line in lines]) |
22 | | - return f"{quotes}{block}{quotes}" |
23 | | - |
24 | | - def methods(methods): |
25 | | - content = "" |
26 | | - for definitions in methods.values(): |
27 | | - for d in definitions: |
28 | | - lines = d.code.split("\n")[:-1] |
29 | | - for line in lines: |
30 | | - content += f"{indent}{line[4:]}\n" |
31 | | - content += "\n" |
32 | | - return content |
| 4 | +indent = " " * 4 |
| 5 | + |
| 6 | + |
| 7 | +def attributes(attributes): |
| 8 | + attrs = [] |
| 9 | + for name, definitions in attributes.items(): |
| 10 | + value = definitions[-1].value |
| 11 | + attrs.append(f"{indent}{name} = {value}\n") |
| 12 | + return "".join(attrs) |
| 13 | + |
| 14 | + |
| 15 | +def declaration(name, parents): |
| 16 | + parents = ", ".join([p.__name__ for p in parents]) |
| 17 | + return f"class {name}({parents}):" |
| 18 | + |
| 19 | + |
| 20 | +def docstring(docstring): |
| 21 | + if not docstring: |
| 22 | + return "" |
33 | 23 |
|
| 24 | + quotes = f'{indent}"""\n' |
| 25 | + lines = docstring.split("\n") |
| 26 | + block = "".join([f"{indent}{line}\n" for line in lines]) |
| 27 | + return f"{quotes}{block}{quotes}" |
| 28 | + |
| 29 | + |
| 30 | +def methods(methods): |
| 31 | + content = "" |
| 32 | + for definitions in methods.values(): |
| 33 | + for d in definitions: |
| 34 | + lines = d.code.split("\n")[:-1] |
| 35 | + for line in lines: |
| 36 | + content += f"{indent}{line[4:]}\n" |
| 37 | + content += "\n" |
| 38 | + return content |
| 39 | + |
| 40 | + |
| 41 | +def to_string(structure: Class) -> str: |
34 | 42 | content = declaration(structure.name, structure.parents) |
35 | 43 | content += "\n" |
36 | 44 | content += docstring(structure.docstring) if docstring else "" |
|
0 commit comments