Skip to content

Commit b46ba20

Browse files
authored
Justification (#59)
Adds the ability to justify the entire table, a row, a column, or an individual cell either left, centre, or right. Closes #50. Closes #52. * Included the enum in there and added a comment about turning the internal storage into a table * Linting * Updated teh settings so that self is displayed slightly differently * First pass at writing the code * Changed the code to catch the case wehre columns weren't being created * Included justification to the init thingies * Included comments on what I need to do next * Exposed the functions. Made some tests. Updated the example. Still some more work to be done * Got rid of a comment * udpated the names of some of the tests to better reflect that they're testing specifically the justification of the rows * Included Python doco for all of the methods * Updated to include columns setting the justification as well as lintint * Fixed the issue with the header row not having a border on the bottom * linting * Updated the tests to include testing the columns * linting * Updated the notes with what I have left to do * Updated the TODO comment * Updated the examples to include a more coherent example of what was being implemented * Changed a comment slightly * Fixed up and cleaned up some code * Linting * Updated the tests to match changing the default justification to the left justification * Updated the README * Got rid of comments that I had already completed
1 parent 1aa0335 commit b46ba20

7 files changed

Lines changed: 631 additions & 99 deletions

File tree

.vscode/settings.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
{
22
"python.analysis.autoImportCompletions": true,
3-
"python.analysis.typeCheckingMode": "basic"
3+
"python.analysis.typeCheckingMode": "basic",
4+
"editor.tokenColorCustomizations": {
5+
"textMateRules": [
6+
{
7+
"scope": [
8+
//"variable.language.special.self.python",
9+
"variable.parameter.function.language.special.self.python",
10+
//"variable.language.special.cls.python",
11+
//"variable.parameter.function.language.special.cls.python"
12+
],
13+
"settings": {
14+
"fontStyle": "italic",
15+
"foreground": "#658ABB"
16+
}
17+
}
18+
]
19+
}
420
}

README.md

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ A project to dynamically scale and print tables using text in Python
33

44
# Usage
55

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:
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 `get_table` method. An example can be found in `example.py`, but is copied here for convenience:
7+
8+
## Basic Usage
79

810
```python
911
from table import PrintTable
@@ -15,56 +17,146 @@ my_table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
1517
my_table.add_row("Another entry", "yay", "an entry in the table")
1618
my_table.add_row("Fun times", "This is kinda cool", "wooow")
1719

18-
my_table.print_table()
20+
print(my_table.get_table())
1921
```
2022

2123
Output:
2224
```
2325
**************************************************************
24-
* Col 1 * Col 2 * Col 3 *
26+
* Col 1 * Col 2 * Col 3 *
2527
**************************************************************
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 *
28+
* Entry 1 * Entry number 2 * Entry 3 baby *
29+
* Another entry * yay * an entry in the table *
30+
* Fun times * This is kinda cool * wooow *
2931
**************************************************************
3032
```
3133

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:
34+
As shown in the above example, getting the current table is as simple as calling the `get_table()` method. This returns the table as a simple string. This can then be passed to whatever function you like, in this case the `print` function.
35+
36+
## Header
37+
38+
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`:
3339

3440
```python
3541
from table import PrintTable
3642

3743
my_table = PrintTable()
3844

45+
my_table.add_row("Col 1", "Col 2", "Col 3")
46+
my_table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
47+
my_table.add_row("Another entry", "yay", "an entry in the table")
48+
my_table.add_row("Fun times", "This is kinda cool", "wooow")
49+
my_table.has_header_row = False
50+
51+
print(my_table.get_table())
52+
```
53+
54+
Output:
55+
```
56+
**************************************************************
57+
* Col 1 * Col 2 * Col 3 *
58+
* Entry 1 * Entry number 2 * Entry 3 baby *
59+
* Another entry * yay * an entry in the table *
60+
* Fun times * This is kinda cool * wooow *
61+
**************************************************************
62+
```
63+
64+
## Justification
65+
66+
Be default, all of the cells are justified to the left. However, justification can be changed for the entire table, for a row or column, or for individual cells. Whenever a justification is set it overrides any previous justification set on that/those cell/cells. Note that `Justification` also needs to be imported.
67+
68+
### Justification for the Table:
69+
70+
```python
71+
from table import PrintTable, Justification
72+
73+
my_table = PrintTable()
74+
75+
my_table.add_row("Col 1", "Col 2", "Col 3")
76+
my_table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
77+
my_table.add_row("Another entry", "yay", "an entry in the table")
78+
my_table.add_row("Fun times", "This is kinda cool", "wooow")
79+
80+
my_table.set_table_justification(Justification.CENTRE)
81+
82+
print(my_table.get_table())
83+
```
84+
85+
Output:
86+
```
87+
**************************************************************
88+
* Col 1 * Col 2 * Col 3 *
89+
**************************************************************
90+
* Entry 1 * Entry number 2 * Entry 3 baby *
91+
* Another entry * yay * an entry in the table *
92+
* Fun times * This is kinda cool * wooow *
93+
**************************************************************
94+
```
95+
96+
### Justification for a Row:
97+
98+
Continuing from the previous example above:
99+
```python
39100
...
40101

41-
str_table = my_table.get_table()
102+
my_table.set_row_justification(0, Justification.LEFT)
103+
104+
print(my_table.get_table())
42105
```
43106

44-
## Header
107+
Output:
108+
```
109+
**************************************************************
110+
* Col 1 * Col 2 * Col 3 *
111+
**************************************************************
112+
* Entry 1 * Entry number 2 * Entry 3 baby *
113+
* Another entry * yay * an entry in the table *
114+
* Fun times * This is kinda cool * wooow *
115+
**************************************************************
116+
```
45117

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`:
118+
### Justification for a Column:
47119

120+
Continuing from the previous example above:
48121
```python
49-
from table import PrintTable
122+
...
50123

51-
table = PrintTable()
124+
my_table.set_column_justification(0, Justification.RIGHT)
52125

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
126+
print(my_table.get_table())
127+
```
58128

59-
table.print_table()
129+
Output:
130+
```
131+
**************************************************************
132+
* Col 1 * Col 2 * Col 3 *
133+
**************************************************************
134+
* Entry 1 * Entry number 2 * Entry 3 baby *
135+
* Another entry * yay * an entry in the table *
136+
* Fun times * This is kinda cool * wooow *
137+
**************************************************************
138+
```
139+
140+
### Justification for a Cell:
141+
142+
Continuing from the previous example above:
143+
```python
144+
...
145+
146+
my_table.set_cell_justification(1, 1, Justification.LEFT)
147+
148+
print(my_table.get_table())
60149
```
61150

62151
Output:
63152
```
64153
**************************************************************
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 *
154+
* Col 1 * Col 2 * Col 3 *
155+
**************************************************************
156+
* Entry 1 * Entry number 2 * Entry 3 baby *
157+
* Another entry * yay * an entry in the table *
158+
* Fun times * This is kinda cool * wooow *
69159
**************************************************************
70-
```
160+
```
161+
162+
Notice how in the above examples the justifications of the edited cells were overwritten with the latest justification.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "python-table-print"
3-
version = "0.1.5"
3+
version = "0.2"
44
authors = [
55
{"name" = "Centauri_Prime"}
66
]

src/python_table_print/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .table import PrintTable
1+
from .table import PrintTable, Justification

src/python_table_print/example.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
1-
from table import PrintTable
1+
from table import PrintTable, Justification
22

3-
table = PrintTable()
3+
# Create a basic table
44

5-
table.add_row("Col 1", "Col 2", "Col 3", "Col 4", "Col 5")
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 = True
5+
my_table = PrintTable()
106

11-
table.print_table()
7+
my_table.add_row("Col 1", "Col 2", "Col 3")
8+
my_table.add_row("Entry 1", "Entry number 2", "Entry 3 baby")
9+
my_table.add_row("Another entry", "yay", "an entry in the table")
10+
my_table.add_row("Fun times", "This is kinda cool", "wooow")
11+
12+
print("Basic Table\n")
13+
print(my_table.get_table())
14+
print("\n\n")
15+
16+
17+
# The header row is optional and can be removed
18+
19+
my_table.has_header_row = False
20+
21+
print("Table Without Header\n")
22+
print(my_table.get_table())
23+
print("\n\n")
24+
25+
26+
# You can set the justification for the whole table, for a row or column, or for an individual cell.
27+
# Each justification command overrides any previous justification set for those cell(s)
28+
29+
my_table.has_header_row = True
30+
31+
my_table.set_table_justification(Justification.CENTRE)
32+
33+
print("Table With Centre Justification\n")
34+
print(my_table.get_table())
35+
print("\n\n")
36+
37+
my_table.set_row_justification(0, Justification.LEFT)
38+
39+
print("Table With Top Row Left-Justified\n")
40+
print(my_table.get_table())
41+
print("\n\n")
42+
43+
my_table.set_column_justification(0, Justification.RIGHT)
44+
45+
print("Table With First Column Right-Justified\n")
46+
print(my_table.get_table())
47+
print("\n\n")
48+
49+
my_table.set_cell_justification(1, 1, Justification.LEFT)
50+
51+
print("Table With One Cell Left-Justified\n")
52+
print(my_table.get_table())
53+
print("\n\n")

0 commit comments

Comments
 (0)