patternpythonMinor
Lightweight tabulation written in Python
Viewed 0 times
lightweighttabulationwrittenpython
Problem
I wrote this module in python for some minor tabulation needs in my science projects. This was very helpful for testing, debugging and plotting various data. I often use dictionaries for storing such data, either as an array or as in a spreadsheet (column and row). This tiny script helps me to plot such data in a similar fashion. Since I find myself using it a lot, I thought of improving it in terms of performance and readability. Also, I do understand that there are several third party libraries available for python to do the same with much more functionality, but I did it anyway for learning the language and ... well, and for fun. How could I refactor this code so that I could optimize it and possibly expand it in the future?
Summary
Dictionaries represent columns. Their keys marks the rows and their values contains the cell data. This produces the structure cell=column[row] which is very much similar to the A1 structure in spreadsheet programs.
I have added doc strings and comments to make this somewhat readable, but I would very much appreciate it if you could suggest an improvement. The
The
```
from shutil import get_terminal_size as termsize
class Table(object):
"""Lightweight tabulation class for dictionary type containers.
Dictionaries represents columns of the table, and their values the cells.
Keys identifies the rows of the
Summary
- For dictionaries only
- Rows can be specified separately for excluding some data
- Automatic column spacing
- Automatic table breaking (terminal size)
- Specify headers and alignment
Dictionaries represent columns. Their keys marks the rows and their values contains the cell data. This produces the structure cell=column[row] which is very much similar to the A1 structure in spreadsheet programs.
I have added doc strings and comments to make this somewhat readable, but I would very much appreciate it if you could suggest an improvement. The
Table class could be instantiated to specify rows and other features. User could then add columns using AddColumn method and that's pretty much it. Use Show to view the final table.The
[[]] in the code are used for containing info about each breaks in the table. [[break1], [break2], [break3] ...]. Each of these breaks contains columns that could fit inside the terminal window. [[c1,c2,c3...c7], [c8,c9], [c10,c11...] ...].```
from shutil import get_terminal_size as termsize
class Table(object):
"""Lightweight tabulation class for dictionary type containers.
Dictionaries represents columns of the table, and their values the cells.
Keys identifies the rows of the
Solution
I think you shouldn't use the word
Also I think
Your naming in general should match the style guide (PEP0008), which suggests that function names be all lowercase with underscores as separators, not CamelCase. So
threshold as a GetMaxLength parameter. It implies to me that it's a maximum value, but it's instead treated as a minimum. You then say it's the "cut-off value of length" which is ambiguous to me again. I'd just call it min and then it's self explanatory.def GetMaxLength(self, *iterables, min=0):
"""Returns the length of the longest item among the iterables.
'min', if specified, is the minimum possible value of length.
"""Also I think
draw is a more appropriate name for printing the table. You could possibly even implement it as __repr__ so that the table could be drawn with repr(Table). Though in that case I'd implement a nice __str__ method too in case people try to print it unwittingly in a sentence because '{}'.format(Table) would call __repr__ if you didn't have a __str__ method defined.Your naming in general should match the style guide (PEP0008), which suggests that function names be all lowercase with underscores as separators, not CamelCase. So
get_max_length would be more appropriate.Code Snippets
def GetMaxLength(self, *iterables, min=0):
"""Returns the length of the longest item among the iterables.
'min', if specified, is the minimum possible value of length.
"""Context
StackExchange Code Review Q#62151, answer score: 3
Revisions (0)
No revisions yet.