Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/649431892798898176
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 27 characters in body
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469
Academic year     Tuition        Increase    
---------------   ------------   ------------
2015-2016             2,520.00         120.00
2016-2017             2,646.00         126.00
2017-2018             2,778.30         132.30
2018-2019             2,917.22         138.92
       Total tuition increase:   $     517.22 

Academic year  |Tuition     |Increase    
---------------|------------|------------
2015-2016      |   42,000.00|    2,000.00
2016-2017      |   44,100.00|    2,100.00
2017-2018      |   46,305.00|    2,205.00
2018-2019      |   48,620.00|    2,315.25
       Total tuition increase:   $   8,620.25 
Academic year     Tuition        Increase    
---------------   ------------   ------------
2015-2016             2,520.00         120.00
2016-2017             2,646.00         126.00
2017-2018             2,778.30         132.30
2018-2019             2,917.22         138.92
       Total tuition increase:   $     517.22 

Academic year  |Tuition     |Increase    
---------------|------------|------------
2015-2016      |   42,000.00|    2,000.00
2016-2017      |   44,100.00|    2,100.00
2017-2018      |   46,305.00|    2,205.00
2018-2019      |   48,620.00|    2,315.25
       Total tuition increase:   $   8,620.25 
Academic year     Tuition        Increase    
---------------   ------------   ------------
2015-2016             2,520.00         120.00
2016-2017             2,646.00         126.00
2017-2018             2,778.30         132.30
2018-2019             2,917.22         138.92
       Total tuition increase:   $     517.22 

Academic year  |Tuition     |Increase    
---------------|------------|------------
2015-2016      |   42,000.00|    2,000.00
2016-2017      |   44,100.00|    2,100.00
2017-2018      |   46,305.00|    2,205.00
2018-2019      |   48,620.00|    2,315.25
       Total tuition increase:   $   8,620.25 
Academic year     Tuition        Increase    
---------------   ------------   ------------
2015-2016             2,520.00         120.00
2016-2017             2,646.00         126.00
2017-2018             2,778.30         132.30
2018-2019             2,917.22         138.92
       Total tuition increase:   $     517.22 

Academic year  |Tuition     |Increase    
---------------|------------|------------
2015-2016      |   42,000.00|    2,000.00
2016-2017      |   44,100.00|    2,100.00
2017-2018      |   46,305.00|    2,205.00
2018-2019      |   48,620.00|    2,315.25
       Total tuition increase:   $   8,620.25 
Source Link
holroy
  • 11.8k
  • 1
  • 27
  • 59

Using changeable column formatting to display table

When answering python tuition calculator v2, I got a little carried away, and started building a print_table() function which I don't like the resulting code for, and I would love guidance into improving (and/or enhancing) my code.

My ambitions were as follows:

  • Have a print_table() with as little formatting as possible, whilst still maintaining a similar output
  • Have an easy way to change the column layout, in case I later on decide to change the titles, number of columns, width of columns
  • Ensure proper alignment in columns, i.e. titles are left justified, whilst numbers are right justified. If currency I would also like for the currency sign to be correctly inserted (just in front of the number for $

The code works, but I don't like the look of it, and I would like for it to be improved in the following aspects:

  • It doesn't add the $ in front of the numbers
  • It is not dynamic in number of columns (or width of columns)
  • I would love for a better way of templating the column format, for use both in the header and the actual lines of the table, but got into issues related to partially expanding the format string

Here is my current code:

YEAR_V1 = [
    (2015, 2520.00, 120.00),
    (2016, 2646.00, 126.00),
    (2017, 2778.30, 132.30),
    (2018, 2917.22, 138.92) ]

YEAR_V2 = [
    (2015, 42000.00, 2000.00),
    (2016, 44100.00, 2100.00),
    (2017, 46305.00, 2205.00),
    (2018, 48620.00, 2315.25) ]


def print_columns(year, tuition, increase,
                  header=False, column_separator="   "):
    COL1_WIDTH = 15
    COL2_WIDTH = 12
    COL3_WIDTH = 12
    
    COLUMN_FORMAT = '{year:{col_year}}' + column_separator + \
                    '{tuition:{col_tuition}}' + column_separator + \
                    '{increase:{col_increase}}'
    
    if header:
        print(COLUMN_FORMAT.format(year=year,
                                   col_year='<{}'.format(COL1_WIDTH),
                                   tuition=tuition,
                                   col_tuition='<{}'.format(COL2_WIDTH),
                                   increase=increase,
                                   col_increase='<{}'.format(COL2_WIDTH)))
        print(COLUMN_FORMAT.format(year="-" * COL1_WIDTH,
                                   col_year='<{}'.format(COL1_WIDTH),
                                   tuition = "-" * COL2_WIDTH,
                                   col_tuition='<{}'.format(COL2_WIDTH),
                                   increase="-" * COL3_WIDTH,
                                   col_increase='<{}'.format(COL3_WIDTH)))
    else:
        print(COLUMN_FORMAT.format(year=year,
                                   col_year='<{}'.format(COL1_WIDTH),
                                   tuition=tuition,
                                   col_tuition='>{},.2f'.format(COL2_WIDTH),
                                   increase=increase,
                                   col_increase='>{},.2f'.format(COL3_WIDTH)))


def print_table(academic_year, column_separator="   "):
    print_columns("Academic year", "Tuition", "Increase",
                  header=True, column_separator=column_separator)
    
    sum_interest = 0
    for (year, tuition, interest) in academic_year:
        sum_interest += interest
        print_columns('{}-{}'.format(year, year+1), tuition, interest,
                      column_separator = column_separator)

    print('{:>30}   ${:11,.2f} '.format('Total tuition increase:', sum_interest))


print_table(YEAR_V1)
print
print_table(YEAR_V2, column_separator = "|")

Which produces the following output:

Academic year     Tuition        Increase    
---------------   ------------   ------------
2015-2016             2,520.00         120.00
2016-2017             2,646.00         126.00
2017-2018             2,778.30         132.30
2018-2019             2,917.22         138.92
       Total tuition increase:   $     517.22 

Academic year  |Tuition     |Increase    
---------------|------------|------------
2015-2016      |   42,000.00|    2,000.00
2016-2017      |   44,100.00|    2,100.00
2017-2018      |   46,305.00|    2,205.00
2018-2019      |   48,620.00|    2,315.25
       Total tuition increase:   $   8,620.25