<s> from StringIO import StringIO <EOL> def reptable ( tb ) : <EOL> if not tb : <EOL> return <NUM_LIT:0> , [ ] <EOL> maxlens = [ <NUM_LIT:0> ] * len ( tb [ <NUM_LIT:0> ] ) <EOL> for r in tb : <EOL> if r == '<STR_LIT:->' : <EOL> continue <EOL> for i , e in enumerate ( r ) : <EOL> maxlens [ i ] = max ( maxlens [ i ] , len ( str ( e ) ) + <NUM_LIT:1> ) <EOL> sumlens = len ( maxlens ) <EOL> for s in maxlens : <EOL> sumlens += s <EOL> out = [ ] <EOL> for r in tb : <EOL> if r == '<STR_LIT:->' : <EOL> out . append ( '<STR_LIT:->' * min ( sumlens , <NUM_LIT> ) ) <EOL> else : <EOL> so = '<STR_LIT>' <EOL> for i , e in enumerate ( r ) : <EOL> s = str ( e ) <EOL> if s . startswith ( '<STR_LIT>' ) : <EOL> s = s [ <NUM_LIT:2> : ] <EOL> fillright = <NUM_LIT:1> <EOL> elif s . isdigit ( ) : <EOL> fillright = <NUM_LIT:1> <EOL> else : <EOL> fillright = <NUM_LIT:0> <EOL> ml = maxlens [ i ] - <NUM_LIT:1> <EOL> fill = '<STR_LIT:U+0020>' * ( ml - len ( s ) ) <EOL> if fillright : <EOL> s = fill + s <EOL> else : <EOL> if <NUM_LIT:1> or i + <NUM_LIT:1> < len ( r ) : <EOL> s = s + fill <EOL> so += s + '<STR_LIT:U+0020>' <EOL> out . append ( so ) <EOL> return maxlens , out <EOL> def ptable ( tb , f = None ) : <EOL> if f is None : <EOL> import sys <EOL> f = sys . stdout <EOL> _ , lines = reptable ( tb ) <EOL> for line in lines : <EOL> line = line . rstrip ( ) <EOL> print >> f , line <EOL> def strtable ( tb ) : <EOL> f = StringIO ( ) <EOL> ptable ( tb , f ) <EOL> return f . getvalue ( ) <EOL> def str2int ( s , msg = '<STR_LIT>' ) : <EOL> if s . startswith ( '<STR_LIT:->' ) : <EOL> sign = - <NUM_LIT:1> <EOL> s = s [ <NUM_LIT:1> : ] <EOL> else : <EOL> sign = <NUM_LIT:1> <EOL> if not s . startswith ( '<STR_LIT>' ) : <EOL> raise ValueError , msg <EOL> s = s [ <NUM_LIT:2> : ] <EOL> if s . endswith ( '<STR_LIT:l>' ) or s . endswith ( '<STR_LIT:L>' ) : <EOL> s = s [ : - <NUM_LIT:1> ] <EOL> return int ( s , <NUM_LIT:16> ) * sign </s>
