diff --git a/ReleaseNotes/release_notes.md b/ReleaseNotes/release_notes.md index b0c2d2a1..63944c23 100644 --- a/ReleaseNotes/release_notes.md +++ b/ReleaseNotes/release_notes.md @@ -4,6 +4,9 @@ ## Bug fixes + 1. Fix a tiny bug in `ostap.logger.table.the_table` for wrapped columns + + # v1.10.1.4 ## New features diff --git a/ostap/fitting/roofit.py b/ostap/fitting/roofit.py index 08e4c0fe..a403757c 100755 --- a/ostap/fitting/roofit.py +++ b/ostap/fitting/roofit.py @@ -326,15 +326,13 @@ def _name ( obj ) : ## row.append ( name ) - - row = tuple ( row ) table.append ( row ) import ostap.logger.table as T - return T.table ( table , title = title, prefix = prefix , alignment = 'clccllll' ) + return T.table ( table , title = title, prefix = prefix , alignment = 'clcclllw' ) ROOT.RooPlot.table = _rp_table_ ROOT.RooPlot.__str__ = _rp_table_ diff --git a/ostap/logger/table.py b/ostap/logger/table.py index a9e5c211..32366c21 100644 --- a/ostap/logger/table.py +++ b/ostap/logger/table.py @@ -78,7 +78,12 @@ # t = the_table ( table_data , 'Title' ) # print (t) # @endcode -def the_table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = -1 , indent = wrap_indent ) : +def the_table ( rows , + title = '' , + prefix = '' , + alignment = () , + wrap_width = -1 , + indent = wrap_indent , style = '' ) : """Format the list of rows as a table (home-made primitive) - Each row is a sequence of column cells. - The first row defines the column headers. @@ -91,7 +96,9 @@ def the_table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = """ ## calculate the number of columns - rows = list ( rows ) + title = allright ( decolorize ( title ).strip() ) + + rows = list ( rows ) nc = 0 for row in rows : nc = max ( nc , len ( row ) ) @@ -123,7 +130,10 @@ def the_table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = for k in widths : if not k in wraps : twidth += widths [ k ] + 2 - twidth += nc + 1 + twidth += nc + 1 + + ww = 12 + _ , w = terminal_size() if w <= twidth : break @@ -131,6 +141,7 @@ def the_table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = ww = ( w - twidth ) - 2 * nw ww , _ = divmod ( ww , nw ) + if 12 < wrap_width and wrap_width < ww : ww = wrap_width @@ -141,7 +152,6 @@ def the_table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = if len ( wraps ) == lw : break - for i in wraps : widths [ i ] = min ( ww , widths [ i ] ) @@ -186,8 +196,9 @@ def the_table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = totwidth = 0 for c in widths : totwidth += widths[c] totwidth += ( nc - 1 ) * 3 - if totwidth < len ( title ) : - delta = 1 + ( len ( title ) - totwidth ) // nc + titwidth = len ( decolorize ( title ) ) + if totwidth < titwidth : + delta = 1 + ( titwidth - totwidth ) // nc for c in widths : widths[c] += delta @@ -276,8 +287,7 @@ def table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = -1 , from ostap.utils.basic import isatty - - title = allright ( decolorize ( title ) ) + title = allright ( decolorize ( title ).strip() ) if rows : rows = list ( rows ) header_row = rows[0] @@ -287,9 +297,8 @@ def table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = -1 , rows = [ list(row) for row in rows ] - if not style : - style = '%s' % default_style - + if not style : style = '%s' % default_style + fmt = style.lower() if 'local' == fmt or not terminaltables : @@ -311,11 +320,11 @@ def table ( rows , title = '' , prefix = '' , alignment = () , wrap_width = -1 , table_instance = terminaltables.DoubleTable ( rows , title ) else : table_instance = terminaltables.DoubleTable ( rows , title ) - + cw = table_instance.column_widths nc = len ( cw ) - wraps = [ i for (i,a) in enumerate ( alignment ) if a in wrapped ] + wraps = [ i for ( i , a ) in enumerate ( alignment ) if a in wrapped ] if wraps : from terminaltables.width_and_alignment import max_dimensions diff --git a/ostap/logger/tests/tesst_logger_logger.py b/ostap/logger/tests/test_logger_logger.py similarity index 100% rename from ostap/logger/tests/tesst_logger_logger.py rename to ostap/logger/tests/test_logger_logger.py diff --git a/ostap/logger/tests/test_logger_table.py b/ostap/logger/tests/test_logger_table.py new file mode 100644 index 00000000..04013484 --- /dev/null +++ b/ostap/logger/tests/test_logger_table.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# ============================================================================= +# @file ostap/logger/tests/test_logger_table.py +# Copyright (c) Ostap developpers. +# ============================================================================= +""" Test module for tables +""" +# ============================================================================= +import ROOT +# ============================================================================= +# logging +# ============================================================================= +from ostap.logger.logger import getLogger +if '__main__' == __name__ : logger = getLogger ( 'test_logger_table' ) +else : logger = getLogger ( __name__ ) +# ============================================================================= +import ostap.logger.table as T + + + + +# ============================================================================= +## Test tables +def test_tables () : + "Test tables" + + + table1 = [ + ( 'Name' , 'Occupation' , 'Note' ) , + ( 'Alice' , '?' , '---' ) , + ( 'Bob' , 'unemployed person' , '' ) + ] + + + comment = 20 * 'This is a very long comment. ' + + table2 = [ + ( 'Name' , 'Occupation' , 'Note' , 'Comment' ) , + ( 'Alice' , 'unknown' , comment , ' ' ) , + ( 'Bob' , 'unemployed person' , '' , comment ) + ] + + styles = ( 'local' , 'ascii' , 'single' , 'porcelain' , + 'github' , 'markdown' , 'double' , 'default' ) + + + tabfuns = T.table , T.the_table + tabtypes = 'table' , 'the_table' + + for tabfun , tabtyp in zip ( tabfuns , tabtypes ) : + + for style in styles : + + title = "Test '%s' style: '%s'" % ( tabtyp , style ) + + t = tabfun ( table1 , title = title , prefix = '# ' , style = style ) + logger.info ( 'Table %s\n%s' % ( title , t ) ) + + + for tabfun , tabtyp in zip ( tabfuns , tabtypes ) : + + title = "Test '%s' for wrapped columns" % tabtyp + t = tabfun ( table2 , title = title , prefix = '# ' , alignment = 'llww') + logger.info ( 'Table %s\n%s' % ( title , t ) ) + +# ============================================================================= +if __name__ == '__main__' : + + test_tables () + + +# ============================================================================= +## The END +# ============================================================================= + +