Skip to content

Commit efd68fc

Browse files
authored
Merge pull request #33 from Zandwhich/staging
The base implementation of printing the table. Creates a basic table with everything being right-justified.
2 parents 39272cf + a3e9b17 commit efd68fc

8 files changed

Lines changed: 157 additions & 8 deletions

File tree

README.md

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,70 @@
1-
# Python-Table-Print
1+
# Python Table Print
22
A project to dynamically scale and print tables using text in Python
33

4-
## More documentation to come soon!
4+
# Usage
5+
6+
Python Table Print is object-oriented. This means that a `PrintTable` object gets instantiated and added to/edited. When the table is ready to be printed/created, this can be done by calling the `print_table`/`get_table` methods. An example can be found in `example.py`, but is copied here for convenience:
7+
8+
```python
9+
from table import PrintTable
10+
11+
my_table = PrintTable()
12+
13+
my_table.add_row("Col 1", "Col 2", "Col 3")
14+
my_table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
15+
my_table.add_row("Another entry", "yay", "an entry in the table")
16+
my_table.add_row("Fun times", "This is kinda cool", "wooow")
17+
18+
my_table.print_table()
19+
```
20+
21+
Output:
22+
```
23+
**************************************************************
24+
* Col 1 * Col 2 * Col 3 *
25+
**************************************************************
26+
* Entry 1 * Entry number 2 * Entry 3 baby *
27+
* Another entry * yay * an entry in the table *
28+
* Fun times * This is kinda cool * wooow *
29+
**************************************************************
30+
```
31+
32+
In the above example, the `print_table` method was called which prints using the default `print` function. However, if the table in string form is preferred, then this is also easily achievable:
33+
34+
```python
35+
from table import PrintTable
36+
37+
my_table = PrintTable()
38+
39+
...
40+
41+
str_table = my_table.get_table()
42+
```
43+
44+
## Header
45+
46+
By default, the first row of the table is treated as the header of the table, and is printed with an extra border around it, not done for any other row. This behaviour can be turned off/on by setting the property `has_header` to `True`/`False`:
47+
48+
```python
49+
from table import PrintTable
50+
51+
table = PrintTable()
52+
53+
table.add_row("Col 1", "Col 2", "Col 3")
54+
table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
55+
table.add_row("Another entry", "yay", "an entry in the table")
56+
table.add_row("Fun times", "This is kinda cool", "wooow")
57+
table.has_header_row = False
58+
59+
table.print_table()
60+
```
61+
62+
Output:
63+
```
64+
**************************************************************
65+
* Col 1 * Col 2 * Col 3 *
66+
* Entry 1 * Entry number 2 * Entry 3 baby *
67+
* Another entry * yay * an entry in the table *
68+
* Fun times * This is kinda cool * wooow *
69+
**************************************************************
70+
```

python_table_print/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from table import PrintTable

python_table_print/example.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from table import PrintTable
2+
3+
table = PrintTable()
4+
5+
table.add_row("Col 1", "Col 2", "Col 3")
6+
table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
7+
table.add_row("Another entry", "yay", "an entry in the table")
8+
table.add_row("Fun times", "This is kinda cool", "wooow")
9+
table.has_header_row = False
10+
11+
table.print_table()

python_table_print/table.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
BASE_BORDER = "*"
2+
3+
4+
class PrintTable:
5+
def __init__(self) -> None:
6+
self.has_header_row = True
7+
8+
self._max_column_lengths = []
9+
self._text = []
10+
11+
def add_row(self, *row: str) -> None:
12+
for col_i in range(0, len(row)):
13+
if (
14+
len(row[col_i]) > self._max_column_lengths[col_i]
15+
if col_i < len(self._max_column_lengths)
16+
else -1
17+
):
18+
if col_i < len(self._max_column_lengths):
19+
self._max_column_lengths[col_i] = len(row[col_i])
20+
else:
21+
self._max_column_lengths += [len(row[col_i])]
22+
23+
self._text += [row]
24+
25+
def _total_border_length(self) -> int:
26+
total_border_length = 0
27+
28+
for col_len in self._max_column_lengths:
29+
total_border_length += col_len + 3 if not col_len == 0 else 2
30+
31+
return total_border_length + 1
32+
33+
def _get_border_row(self) -> str:
34+
return BASE_BORDER * self._total_border_length() + "\n"
35+
36+
def _get_header(self, header: tuple[str, ...]) -> str:
37+
header = self._get_row(header)
38+
return header + self._get_border_row()
39+
40+
def _get_row(self, columns: tuple[str, ...]) -> str:
41+
row = BASE_BORDER
42+
for col_i in range(0, len(columns)):
43+
if self._max_column_lengths[col_i] == 0:
44+
row += " " + BASE_BORDER
45+
continue
46+
47+
row += (
48+
" " * (self._max_column_lengths[col_i] - len(columns[col_i]) + 1)
49+
+ columns[col_i]
50+
+ " "
51+
+ BASE_BORDER
52+
)
53+
54+
return row + "\n"
55+
56+
def get_table(self) -> str:
57+
if len(self._text) == 0:
58+
# TODO: Throw an error here
59+
return "ERROR: No text passed in for the table"
60+
61+
table = self._get_border_row()
62+
63+
if self.has_header_row:
64+
table += self._get_header(self._text[0])
65+
66+
for r_row in range(1 if self.has_header_row else 0, len(self._text)):
67+
table += self._get_row(self._text[r_row])
68+
69+
return table + self._get_border_row()
70+
71+
def print_table(self) -> None:
72+
print(self.get_table())

src/python-table-print/__init__.py

Whitespace-only changes.

src/python-table-print/example.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/base_test.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/test_table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TODO: Add testing for the table
2+
3+
4+
def test_temp():
5+
assert True

0 commit comments

Comments
 (0)