<s> """<STR_LIT>""" <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> __author__ = '<STR_LIT>' <EOL> __license__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> __credits__ = """<STR_LIT>""" <EOL> import sys <EOL> import string <EOL> try : <EOL> if sys . version >= '<STR_LIT>' : <EOL> import textwrap <EOL> elif sys . version >= '<STR_LIT>' : <EOL> from optparse import textwrap <EOL> else : <EOL> from optik import textwrap <EOL> except ImportError : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> raise <EOL> try : <EOL> True , False <EOL> except NameError : <EOL> ( True , False ) = ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def len ( iterable ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( iterable , str ) : <EOL> return iterable . __len__ ( ) <EOL> try : <EOL> return len ( unicode ( iterable , '<STR_LIT>' ) ) <EOL> except : <EOL> return iterable . __len__ ( ) <EOL> class ArraySizeError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg ) : <EOL> self . msg = msg <EOL> Exception . __init__ ( self , msg , '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . msg <EOL> class Texttable : <EOL> BORDER = <NUM_LIT:1> <EOL> HEADER = <NUM_LIT:1> << <NUM_LIT:1> <EOL> HLINES = <NUM_LIT:1> << <NUM_LIT:2> <EOL> VLINES = <NUM_LIT:1> << <NUM_LIT:3> <EOL> def __init__ ( self , max_width = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> if max_width <= <NUM_LIT:0> : <EOL> max_width = False <EOL> self . _max_width = max_width <EOL> self . _precision = <NUM_LIT:3> <EOL> self . _deco = Texttable . VLINES | Texttable . HLINES | Texttable . BORDER | Texttable . HEADER <EOL> self . set_chars ( [ '<STR_LIT:->' , '<STR_LIT:|>' , '<STR_LIT:+>' , '<STR_LIT:=>' ] ) <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _hline_string = None <EOL> self . _row_size = None <EOL> self . _header = [ ] <EOL> self . _rows = [ ] <EOL> def set_chars ( self , array ) : <EOL> """<STR_LIT>""" <EOL> if len ( array ) != <NUM_LIT:4> : <EOL> raise ArraySizeError , "<STR_LIT>" <EOL> array = [ x [ : <NUM_LIT:1> ] for x in [ str ( s ) for s in array ] ] <EOL> ( self . _char_horiz , self . _char_vert , <EOL> self . _char_corner , self . _char_header ) = array <EOL> def set_deco ( self , deco ) : <EOL> """<STR_LIT>""" <EOL> self . _deco = deco <EOL> def set_cols_align ( self , array ) : <EOL> """<STR_LIT>""" <EOL> self . _check_row_size ( array ) <EOL> self . _align = array <EOL> def set_cols_valign ( self , array ) : <EOL> """<STR_LIT>""" <EOL> self . _check_row_size ( array ) <EOL> self . _valign = array <EOL> def set_cols_dtype ( self , array ) : <EOL> """<STR_LIT>""" <EOL> self . _check_row_size ( array ) <EOL> self . _dtype = array <EOL> def set_cols_width ( self , array ) : <EOL> """<STR_LIT>""" <EOL> self . _check_row_size ( array ) <EOL> try : <EOL> array = map ( int , array ) <EOL> if reduce ( min , array ) <= <NUM_LIT:0> : <EOL> raise ValueError <EOL> except ValueError : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> raise <EOL> self . _width = array <EOL> def set_precision ( self , width ) : <EOL> """<STR_LIT>""" <EOL> if not type ( width ) is int or width < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _precision = width <EOL> def header ( self , array ) : <EOL> """<STR_LIT>""" <EOL> self . _check_row_size ( array ) <EOL> self . _header = map ( str , array ) <EOL> def add_row ( self , array ) : <EOL> """<STR_LIT>""" <EOL> self . _check_row_size ( array ) <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _dtype = [ "<STR_LIT:a>" ] * self . _row_size <EOL> cells = [ ] <EOL> for i , x in enumerate ( array ) : <EOL> cells . append ( self . _str ( i , x ) ) <EOL> self . _rows . append ( cells ) <EOL> def add_rows ( self , rows , header = True ) : <EOL> """<STR_LIT>""" <EOL> if header : <EOL> if hasattr ( rows , '<STR_LIT>' ) and hasattr ( rows , '<STR_LIT>' ) : <EOL> self . header ( rows . next ( ) ) <EOL> else : <EOL> self . header ( rows [ <NUM_LIT:0> ] ) <EOL> rows = rows [ <NUM_LIT:1> : ] <EOL> for row in rows : <EOL> self . add_row ( row ) <EOL> def draw ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _header and not self . _rows : <EOL> return <EOL> self . _compute_cols_width ( ) <EOL> self . _check_align ( ) <EOL> out = "<STR_LIT>" <EOL> if self . _has_border ( ) : <EOL> out += self . _hline ( ) <EOL> if self . _header : <EOL> out += self . _draw_line ( self . _header , isheader = True ) <EOL> if self . _has_header ( ) : <EOL> out += self . _hline_header ( ) <EOL> length = <NUM_LIT:0> <EOL> for row in self . _rows : <EOL> length += <NUM_LIT:1> <EOL> out += self . _draw_line ( row ) <EOL> if self . _has_hlines ( ) and length < len ( self . _rows ) : <EOL> out += self . _hline ( ) <EOL> if self . _has_border ( ) : <EOL> out += self . _hline ( ) <EOL> return out [ : - <NUM_LIT:1> ] <EOL> def _str ( self , i , x ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> f = float ( x ) <EOL> except : <EOL> return str ( x ) <EOL> n = self . _precision <EOL> dtype = self . _dtype [ i ] <EOL> if dtype == '<STR_LIT:i>' : <EOL> return str ( int ( round ( f ) ) ) <EOL> elif dtype == '<STR_LIT:f>' : <EOL> return '<STR_LIT>' % ( n , f ) <EOL> elif dtype == '<STR_LIT:e>' : <EOL> return '<STR_LIT>' % ( n , f ) <EOL> elif dtype == '<STR_LIT:t>' : <EOL> return str ( x ) <EOL> else : <EOL> if f - round ( f ) == <NUM_LIT:0> : <EOL> if abs ( f ) > <NUM_LIT> : <EOL> return '<STR_LIT>' % ( n , f ) <EOL> else : <EOL> return str ( int ( round ( f ) ) ) <EOL> else : <EOL> if abs ( f ) > <NUM_LIT> : <EOL> return '<STR_LIT>' % ( n , f ) <EOL> else : <EOL> return '<STR_LIT>' % ( n , f ) <EOL> def _check_row_size ( self , array ) : <EOL> """<STR_LIT>""" <EOL> if not self . _row_size : <EOL> self . _row_size = len ( array ) <EOL> elif self . _row_size != len ( array ) : <EOL> raise ArraySizeError , "<STR_LIT>" % self . _row_size <EOL> def _has_vlines ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _deco & Texttable . VLINES > <NUM_LIT:0> <EOL> def _has_hlines ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _deco & Texttable . HLINES > <NUM_LIT:0> <EOL> def _has_border ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _deco & Texttable . BORDER > <NUM_LIT:0> <EOL> def _has_header ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _deco & Texttable . HEADER > <NUM_LIT:0> <EOL> def _hline_header ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _build_hline ( True ) <EOL> def _hline ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _hline_string : <EOL> self . _hline_string = self . _build_hline ( ) <EOL> return self . _hline_string <EOL> def _build_hline ( self , is_header = False ) : <EOL> """<STR_LIT>""" <EOL> horiz = self . _char_horiz <EOL> if ( is_header ) : <EOL> horiz = self . _char_header <EOL> s = "<STR_LIT>" % ( horiz , [ horiz , self . _char_corner ] [ self . _has_vlines ( ) ] , <EOL> horiz ) <EOL> l = string . join ( [ horiz * n for n in self . _width ] , s ) <EOL> if self . _has_border ( ) : <EOL> l = "<STR_LIT>" % ( self . _char_corner , horiz , l , horiz , <EOL> self . _char_corner ) <EOL> else : <EOL> l += "<STR_LIT:\n>" <EOL> return l <EOL> def _len_cell ( self , cell ) : <EOL> """<STR_LIT>""" <EOL> cell_lines = cell . split ( '<STR_LIT:\n>' ) <EOL> maxi = <NUM_LIT:0> <EOL> for line in cell_lines : <EOL> length = <NUM_LIT:0> <EOL> parts = line . split ( '<STR_LIT:\t>' ) <EOL> for part , i in zip ( parts , range ( <NUM_LIT:1> , len ( parts ) + <NUM_LIT:1> ) ) : <EOL> length = length + len ( part ) <EOL> if i < len ( parts ) : <EOL> length = ( length / <NUM_LIT:8> + <NUM_LIT:1> ) * <NUM_LIT:8> <EOL> maxi = max ( maxi , length ) <EOL> return maxi <EOL> def _compute_cols_width ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> return <EOL> maxi = [ ] <EOL> if self . _header : <EOL> maxi = [ self . _len_cell ( x ) for x in self . _header ] <EOL> for row in self . _rows : <EOL> for cell , i in zip ( row , range ( len ( row ) ) ) : <EOL> try : <EOL> maxi [ i ] = max ( maxi [ i ] , self . _len_cell ( cell ) ) <EOL> except ( TypeError , IndexError ) : <EOL> maxi . append ( self . _len_cell ( cell ) ) <EOL> items = len ( maxi ) <EOL> length = reduce ( lambda x , y : x + y , maxi ) <EOL> if self . _max_width and length + items * <NUM_LIT:3> + <NUM_LIT:1> > self . _max_width : <EOL> maxi = [ ( self . _max_width - items * <NUM_LIT:3> - <NUM_LIT:1> ) / items for n in range ( items ) ] <EOL> self . _width = maxi <EOL> def _check_align ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _align = [ "<STR_LIT:l>" ] * self . _row_size <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _valign = [ "<STR_LIT:t>" ] * self . _row_size <EOL> def _draw_line ( self , line , isheader = False ) : <EOL> """<STR_LIT>""" <EOL> line = self . _splitit ( line , isheader ) <EOL> space = "<STR_LIT:U+0020>" <EOL> out = "<STR_LIT>" <EOL> for i in range ( len ( line [ <NUM_LIT:0> ] ) ) : <EOL> if self . _has_border ( ) : <EOL> out += "<STR_LIT>" % self . _char_vert <EOL> length = <NUM_LIT:0> <EOL> for cell , width , align in zip ( line , self . _width , self . _align ) : <EOL> length += <NUM_LIT:1> <EOL> cell_line = cell [ i ] <EOL> fill = width - len ( cell_line ) <EOL> if isheader : <EOL> align = "<STR_LIT:c>" <EOL> if align == "<STR_LIT:r>" : <EOL> out += "<STR_LIT>" % ( fill * space + cell_line ) <EOL> elif align == "<STR_LIT:c>" : <EOL> out += "<STR_LIT>" % ( fill / <NUM_LIT:2> * space + cell_line + ( fill / <NUM_LIT:2> + fill % <NUM_LIT:2> ) * space ) <EOL> else : <EOL> out += "<STR_LIT>" % ( cell_line + fill * space ) <EOL> if length < len ( line ) : <EOL> out += "<STR_LIT>" % [ space , self . _char_vert ] [ self . _has_vlines ( ) ] <EOL> out += "<STR_LIT>" % [ '<STR_LIT>' , self . _char_vert ] [ self . _has_border ( ) ] <EOL> return out <EOL> def _splitit ( self , line , isheader ) : <EOL> """<STR_LIT>""" <EOL> line_wrapped = [ ] <EOL> for cell , width in zip ( line , self . _width ) : <EOL> array = [ ] <EOL> for c in cell . split ( '<STR_LIT:\n>' ) : <EOL> try : <EOL> c = unicode ( c , '<STR_LIT>' ) <EOL> except UnicodeDecodeError , strerror : <EOL> sys . stderr . write ( "<STR_LIT>" % ( c , strerror ) ) <EOL> c = unicode ( c , '<STR_LIT>' , '<STR_LIT:replace>' ) <EOL> array . extend ( textwrap . wrap ( c , width ) ) <EOL> line_wrapped . append ( array ) <EOL> max_cell_lines = reduce ( max , map ( len , line_wrapped ) ) <EOL> for cell , valign in zip ( line_wrapped , self . _valign ) : <EOL> if isheader : <EOL> valign = "<STR_LIT:t>" <EOL> if valign == "<STR_LIT:m>" : <EOL> missing = max_cell_lines - len ( cell ) <EOL> cell [ : <NUM_LIT:0> ] = [ "<STR_LIT>" ] * ( missing / <NUM_LIT:2> ) <EOL> cell . extend ( [ "<STR_LIT>" ] * ( missing / <NUM_LIT:2> + missing % <NUM_LIT:2> ) ) <EOL> elif valign == "<STR_LIT:b>" : <EOL> cell [ : <NUM_LIT:0> ] = [ "<STR_LIT>" ] * ( max_cell_lines - len ( cell ) ) <EOL> else : <EOL> cell . extend ( [ "<STR_LIT>" ] * ( max_cell_lines - len ( cell ) ) ) <EOL> return line_wrapped <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> table = Texttable ( ) <EOL> table . set_cols_align ( [ "<STR_LIT:l>" , "<STR_LIT:r>" , "<STR_LIT:c>" ] ) <EOL> table . set_cols_valign ( [ "<STR_LIT:t>" , "<STR_LIT:m>" , "<STR_LIT:b>" ] ) <EOL> table . add_rows ( [ [ "<STR_LIT:Name>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , <NUM_LIT:32> , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , <NUM_LIT:1> , "<STR_LIT>" ] ] ) <EOL> print table . draw ( ) + "<STR_LIT:\n>" <EOL> table = Texttable ( ) <EOL> table . set_deco ( Texttable . HEADER ) <EOL> table . set_cols_dtype ( [ '<STR_LIT:t>' , <EOL> '<STR_LIT:f>' , <EOL> '<STR_LIT:e>' , <EOL> '<STR_LIT:i>' , <EOL> '<STR_LIT:a>' ] ) <EOL> table . set_cols_align ( [ "<STR_LIT:l>" , "<STR_LIT:r>" , "<STR_LIT:r>" , "<STR_LIT:r>" , "<STR_LIT:l>" ] ) <EOL> table . add_rows ( [ [ "<STR_LIT:text>" , "<STR_LIT:float>" , "<STR_LIT>" , "<STR_LIT:int>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> print table . draw ( ) </s>
