Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit 4fc7ab2

Browse files
Add rewrite version
1 parent 8a7d3bf commit 4fc7ab2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Diff for: markdowntable/__init__.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
from __future__ import print_function, unicode_literals
1+
from dataclasses import dataclass, field
2+
from typing import Iterable
23

3-
import markdowntable.errors
4-
from markdowntable.row import Row, Column
5-
from markdowntable.table import Table
4+
5+
@dataclass
6+
class Table:
7+
headers: list[str] = field(default_factory=list)
8+
rows: list[Iterable[str]] = field(default_factory=list, init=False)
9+
10+
def add_row(self, row: Iterable[str]):
11+
self.rows.append(row)
12+
13+
def set_headers(self, headers: list[str]):
14+
self.headers = headers
15+
16+
def get_table(self) -> str:
17+
header_row = "|" + "|".join(self.headers) + "|"
18+
separator_row = "|" + "|".join(["-"] * len(self.headers)) + "|"
19+
body_rows = [f"|{'|'.join(row)}|" for row in self.rows]
20+
return "\n".join([header_row, separator_row] + body_rows)

0 commit comments

Comments
 (0)