<s> """<STR_LIT>""" <EOL> import re <EOL> from tablib . compat import unicode <EOL> title = '<STR_LIT>' <EOL> extensions = ( '<STR_LIT>' , ) <EOL> TABLE_TEMPLATE = """<STR_LIT>""" <EOL> TEX_RESERVED_SYMBOLS_MAP = dict ( [ <EOL> ( '<STR_LIT:\\>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:{>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:}>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:$>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:&>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:#>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:_>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:%>' , '<STR_LIT>' ) , <EOL> ] ) <EOL> TEX_RESERVED_SYMBOLS_RE = re . compile ( <EOL> '<STR_LIT>' % '<STR_LIT:|>' . join ( map ( re . escape , TEX_RESERVED_SYMBOLS_MAP . keys ( ) ) ) ) <EOL> def export_set ( dataset ) : <EOL> """<STR_LIT>""" <EOL> caption = '<STR_LIT>' % dataset . title if dataset . title else '<STR_LIT:%>' <EOL> colspec = _colspec ( dataset . width ) <EOL> header = _serialize_row ( dataset . headers ) if dataset . headers else '<STR_LIT>' <EOL> midrule = _midrule ( dataset . width ) <EOL> body = '<STR_LIT:\n>' . join ( [ _serialize_row ( row ) for row in dataset ] ) <EOL> return TABLE_TEMPLATE % dict ( CAPTION = caption , COLSPEC = colspec , <EOL> HEADER = header , MIDRULE = midrule , BODY = body ) <EOL> def _colspec ( dataset_width ) : <EOL> """<STR_LIT>""" <EOL> spec = '<STR_LIT:l>' <EOL> for _ in range ( <NUM_LIT:1> , dataset_width ) : <EOL> spec += '<STR_LIT:r>' <EOL> return spec <EOL> def _midrule ( dataset_width ) : <EOL> """<STR_LIT>""" <EOL> if not dataset_width or dataset_width == <NUM_LIT:1> : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT:U+0020>' . join ( [ _cmidrule ( colindex , dataset_width ) for colindex in <EOL> range ( <NUM_LIT:1> , dataset_width + <NUM_LIT:1> ) ] ) <EOL> def _cmidrule ( colindex , dataset_width ) : <EOL> """<STR_LIT>""" <EOL> rule = '<STR_LIT>' <EOL> if colindex == <NUM_LIT:1> : <EOL> return rule % ( '<STR_LIT:r>' , colindex , colindex ) <EOL> if colindex == dataset_width : <EOL> return rule % ( '<STR_LIT:l>' , colindex , colindex ) <EOL> return rule % ( '<STR_LIT>' , colindex , colindex ) <EOL> def _serialize_row ( row ) : <EOL> """<STR_LIT>""" <EOL> new_row = [ _escape_tex_reserved_symbols ( unicode ( item ) ) if item else '<STR_LIT>' for <EOL> item in row ] <EOL> return <NUM_LIT:6> * '<STR_LIT:U+0020>' + '<STR_LIT>' . join ( new_row ) + '<STR_LIT>' <EOL> def _escape_tex_reserved_symbols ( input ) : <EOL> """<STR_LIT>""" <EOL> def replace ( match ) : <EOL> return TEX_RESERVED_SYMBOLS_MAP [ match . group ( ) ] <EOL> return TEX_RESERVED_SYMBOLS_RE . sub ( replace , input ) </s>
