<s> from __future__ import print_function <EOL> from wcwidth import wcwidth <EOL> class pylsytable ( object ) : <EOL> def __init__ ( self , attributes ) : <EOL> """<STR_LIT>""" <EOL> self . StrTable = "<STR_LIT>" <EOL> self . Table = [ ] <EOL> self . AttributesLength = [ ] <EOL> self . Lines_num = <NUM_LIT:0> <EOL> if type ( attributes ) != list : <EOL> attributes = [ attributes ] <EOL> self . Attributes = [ u"<STR_LIT>" . format ( attr ) for attr in attributes ] <EOL> self . Cols_num = len ( self . Attributes ) <EOL> for attribute in self . Attributes : <EOL> col = dict ( ) <EOL> col [ attribute ] = [ ] <EOL> self . Table . append ( col ) <EOL> def _print_divide ( self ) : <EOL> """<STR_LIT>""" <EOL> for space in self . AttributesLength : <EOL> self . StrTable += "<STR_LIT>" + "<STR_LIT>" * space <EOL> self . StrTable += "<STR_LIT:+>" + "<STR_LIT:\n>" <EOL> def append_data ( self , attribute , values ) : <EOL> """<STR_LIT>""" <EOL> found = False <EOL> if type ( values ) != list : <EOL> values = [ values ] <EOL> for col in self . Table : <EOL> if attribute in col : <EOL> dict_values = [ u"<STR_LIT>" . format ( value ) for value in values ] <EOL> col [ attribute ] += dict_values <EOL> found = True <EOL> if not found : <EOL> raise KeyError ( attribute ) <EOL> def add_data ( self , attribute , values ) : <EOL> """<STR_LIT>""" <EOL> found = False <EOL> if type ( values ) != list : <EOL> values = [ values ] <EOL> for col in self . Table : <EOL> if attribute in col : <EOL> dict_values = [ u"<STR_LIT>" . format ( value ) for value in values ] <EOL> col [ attribute ] = dict_values <EOL> found = True <EOL> if not found : <EOL> raise KeyError ( attribute ) <EOL> def _create_table ( self ) : <EOL> """<STR_LIT>""" <EOL> self . StrTable = "<STR_LIT>" <EOL> self . AttributesLength = [ ] <EOL> self . Lines_num = <NUM_LIT:0> <EOL> for col in self . Table : <EOL> values = list ( col . values ( ) ) [ <NUM_LIT:0> ] <EOL> self . Lines_num = max ( self . Lines_num , len ( values ) ) <EOL> key_length = max ( [ self . _disp_width ( v ) for v in values ] or [ <NUM_LIT:0> ] ) <EOL> key_length = max ( key_length , self . _disp_width ( list ( col . keys ( ) ) [ <NUM_LIT:0> ] ) ) <EOL> self . AttributesLength . append ( key_length ) <EOL> self . _print_head ( ) <EOL> self . _print_value ( ) <EOL> def _print_head ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _print_divide ( ) <EOL> self . StrTable += "<STR_LIT>" <EOL> for colwidth , attr in zip ( self . AttributesLength , self . Attributes ) : <EOL> self . StrTable += self . _pad_string ( attr , colwidth * <NUM_LIT:2> ) <EOL> self . StrTable += "<STR_LIT>" <EOL> self . StrTable += '<STR_LIT:\n>' <EOL> self . _print_divide ( ) <EOL> def _print_value ( self ) : <EOL> """<STR_LIT>""" <EOL> for line in range ( self . Lines_num ) : <EOL> for col , length in zip ( self . Table , self . AttributesLength ) : <EOL> vals = list ( col . values ( ) ) [ <NUM_LIT:0> ] <EOL> val = vals [ line ] if len ( vals ) != <NUM_LIT:0> and line < len ( vals ) else '<STR_LIT>' <EOL> self . StrTable += "<STR_LIT>" <EOL> self . StrTable += self . _pad_string ( val , length * <NUM_LIT:2> ) <EOL> self . StrTable += "<STR_LIT:|>" + '<STR_LIT:\n>' <EOL> self . _print_divide ( ) <EOL> def _disp_width ( self , pwcs , n = None ) : <EOL> """<STR_LIT>""" <EOL> end = len ( pwcs ) if n is None else n <EOL> idx = slice ( <NUM_LIT:0> , end ) <EOL> width = <NUM_LIT:0> <EOL> for char in pwcs [ idx ] : <EOL> width += max ( <NUM_LIT:0> , wcwidth ( char ) ) <EOL> return width <EOL> def _pad_string ( self , str , colwidth ) : <EOL> """<STR_LIT>""" <EOL> width = self . _disp_width ( str ) <EOL> prefix = ( colwidth - <NUM_LIT:1> - width ) // <NUM_LIT:2> <EOL> suffix = colwidth - prefix - width <EOL> return '<STR_LIT:U+0020>' * prefix + str + '<STR_LIT:U+0020>' * suffix <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _create_table ( ) <EOL> return self . StrTable </s>
