<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import re <EOL> from six . moves import range <EOL> from pyparsing import CaselessLiteral , Combine , OneOrMore , Optional , TokenConverter , Word , nums , oneOf , printables , ParserElement , alphanums <EOL> import numpy as np <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _getformat ( val ) : <EOL> if int ( val ) == val : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> class _SubHelper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . newtext = "<STR_LIT>" <EOL> self . replace_location = <NUM_LIT:0> <EOL> self . current_location = <NUM_LIT:0> <EOL> self . counter = <NUM_LIT:0> <EOL> self . start_location = <NUM_LIT:0> <EOL> self . end_location = <NUM_LIT:0> <EOL> def set ( self , newtext , location ) : <EOL> """<STR_LIT>""" <EOL> self . newtext = newtext <EOL> self . replace_location = location <EOL> self . current_location = <NUM_LIT:0> <EOL> def set_array ( self , newtext , start_location , end_location ) : <EOL> """<STR_LIT>""" <EOL> self . newtext = newtext <EOL> self . start_location = start_location <EOL> self . end_location = end_location <EOL> self . current_location = <NUM_LIT:0> <EOL> def replace ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . current_location += <NUM_LIT:1> <EOL> if self . current_location == self . replace_location : <EOL> if isinstance ( self . newtext , float ) : <EOL> return _getformat ( self . newtext ) % self . newtext <EOL> else : <EOL> return str ( self . newtext ) <EOL> else : <EOL> return text . group ( ) <EOL> def replace_array ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . current_location += <NUM_LIT:1> <EOL> end = len ( self . newtext ) <EOL> if self . current_location >= self . start_location and self . current_location <= self . end_location and self . counter < end : <EOL> if isinstance ( self . newtext [ self . counter ] , float ) : <EOL> val = self . newtext [ self . counter ] <EOL> newval = _getformat ( val ) % val <EOL> else : <EOL> newval = str ( self . newtext [ self . counter ] ) <EOL> self . counter += <NUM_LIT:1> <EOL> return newval <EOL> else : <EOL> return text . group ( ) <EOL> class ToInteger ( TokenConverter ) : <EOL> """<STR_LIT>""" <EOL> def postParse ( self , instring , loc , tokenlist ) : <EOL> """<STR_LIT>""" <EOL> return int ( tokenlist [ <NUM_LIT:0> ] ) <EOL> class ToFloat ( TokenConverter ) : <EOL> """<STR_LIT>""" <EOL> def postParse ( self , instring , loc , tokenlist ) : <EOL> """<STR_LIT>""" <EOL> return float ( tokenlist [ <NUM_LIT:0> ] . replace ( '<STR_LIT:D>' , '<STR_LIT:E>' ) ) <EOL> class ToNan ( TokenConverter ) : <EOL> """<STR_LIT>""" <EOL> def postParse ( self , instring , loc , tokenlist ) : <EOL> """<STR_LIT>""" <EOL> return float ( '<STR_LIT>' ) <EOL> class ToInf ( TokenConverter ) : <EOL> """<STR_LIT>""" <EOL> def postParse ( self , instring , loc , tokenlist ) : <EOL> """<STR_LIT>""" <EOL> return float ( '<STR_LIT>' ) <EOL> class InputFileGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . template_filename = [ ] <EOL> self . output_filename = [ ] <EOL> self . delimiter = "<STR_LIT:U+0020>" <EOL> self . reg = re . compile ( '<STR_LIT>' ) <EOL> self . data = [ ] <EOL> self . current_row = <NUM_LIT:0> <EOL> self . anchored = False <EOL> def set_template_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . template_filename = filename <EOL> templatefile = open ( filename , '<STR_LIT:r>' ) <EOL> self . data = templatefile . readlines ( ) <EOL> templatefile . close ( ) <EOL> def set_generated_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . output_filename = filename <EOL> def set_delimiters ( self , delimiter ) : <EOL> """<STR_LIT>""" <EOL> self . delimiter = delimiter <EOL> self . reg = re . compile ( '<STR_LIT>' + delimiter + '<STR_LIT>' ) <EOL> def mark_anchor ( self , anchor , occurrence = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( occurrence , int ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> instance = <NUM_LIT:0> <EOL> if occurrence > <NUM_LIT:0> : <EOL> count = <NUM_LIT:0> <EOL> max_lines = len ( self . data ) <EOL> for index in range ( self . current_row , max_lines ) : <EOL> line = self . data [ index ] <EOL> if count == <NUM_LIT:0> and self . anchored : <EOL> line = line . split ( anchor ) [ - <NUM_LIT:1> ] <EOL> if line . find ( anchor ) > - <NUM_LIT:1> : <EOL> instance += <NUM_LIT:1> <EOL> if instance == occurrence : <EOL> self . current_row += count <EOL> self . anchored = True <EOL> return <EOL> count += <NUM_LIT:1> <EOL> elif occurrence < <NUM_LIT:0> : <EOL> max_lines = len ( self . data ) - <NUM_LIT:1> <EOL> count = max_lines <EOL> for index in range ( max_lines , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> line = self . data [ index ] <EOL> if count == max_lines and self . anchored : <EOL> line = line . split ( anchor ) [ <NUM_LIT:0> ] <EOL> if line . find ( anchor ) > - <NUM_LIT:1> : <EOL> instance += - <NUM_LIT:1> <EOL> if instance == occurrence : <EOL> self . current_row = count <EOL> self . anchored = True <EOL> return <EOL> count -= <NUM_LIT:1> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> raise RuntimeError ( "<STR_LIT>" % ( anchor , self . template_filename ) ) <EOL> def reset_anchor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . current_row = <NUM_LIT:0> <EOL> self . anchored = False <EOL> def transfer_var ( self , value , row , field ) : <EOL> """<STR_LIT>""" <EOL> j = self . current_row + row <EOL> line = self . data [ j ] <EOL> sub = _SubHelper ( ) <EOL> sub . set ( value , field ) <EOL> newline = re . sub ( self . reg , sub . replace , line ) <EOL> self . data [ j ] = newline <EOL> def transfer_array ( self , value , row_start , field_start , field_end , <EOL> row_end = None , sep = "<STR_LIT:U+002CU+0020>" ) : <EOL> """<STR_LIT>""" <EOL> if row_end is None : <EOL> row_end = row_start <EOL> sub = _SubHelper ( ) <EOL> for row in range ( row_start , row_end + <NUM_LIT:1> ) : <EOL> j = self . current_row + row <EOL> line = self . data [ j ] <EOL> if row == row_end : <EOL> f_end = field_end <EOL> else : <EOL> f_end = <NUM_LIT> <EOL> sub . set_array ( value , field_start , f_end ) <EOL> field_start = <NUM_LIT:0> <EOL> newline = re . sub ( self . reg , sub . replace_array , line ) <EOL> self . data [ j ] = newline <EOL> if sub . counter < len ( value ) : <EOL> for val in value [ sub . counter : ] : <EOL> newline = newline . rstrip ( ) + sep + str ( val ) <EOL> self . data [ j ] = newline <EOL> elif sub . counter > len ( value ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . data [ j ] += "<STR_LIT:\n>" <EOL> def transfer_2Darray ( self , value , row_start , row_end , field_start , <EOL> field_end ) : <EOL> """<STR_LIT>""" <EOL> sub = _SubHelper ( ) <EOL> i = <NUM_LIT:0> <EOL> for row in range ( row_start , row_end + <NUM_LIT:1> ) : <EOL> j = self . current_row + row <EOL> line = self . data [ j ] <EOL> sub . set_array ( value [ i , : ] , field_start , field_end ) <EOL> newline = re . sub ( self . reg , sub . replace_array , line ) <EOL> self . data [ j ] = newline <EOL> sub . current_location = <NUM_LIT:0> <EOL> sub . counter = <NUM_LIT:0> <EOL> i += <NUM_LIT:1> <EOL> def clearline ( self , row ) : <EOL> """<STR_LIT>""" <EOL> self . data [ self . current_row + row ] = "<STR_LIT:\n>" <EOL> def generate ( self ) : <EOL> """<STR_LIT>""" <EOL> infile = open ( self . output_filename , '<STR_LIT:w>' ) <EOL> infile . writelines ( self . data ) <EOL> infile . close ( ) <EOL> class FileParser ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , end_of_line_comment_char = None , full_line_comment_char = None ) : <EOL> self . filename = [ ] <EOL> self . data = [ ] <EOL> self . delimiter = "<STR_LIT>" <EOL> self . end_of_line_comment_char = end_of_line_comment_char <EOL> self . full_line_comment_char = full_line_comment_char <EOL> self . current_row = <NUM_LIT:0> <EOL> self . anchored = False <EOL> self . set_delimiters ( self . delimiter ) <EOL> def set_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . filename = filename <EOL> inputfile = open ( filename , '<STR_LIT:r>' ) <EOL> if not self . end_of_line_comment_char and not self . full_line_comment_char : <EOL> self . data = inputfile . readlines ( ) <EOL> else : <EOL> self . data = [ ] <EOL> for line in inputfile : <EOL> if line [ <NUM_LIT:0> ] == self . full_line_comment_char : <EOL> continue <EOL> self . data . append ( line . split ( self . end_of_line_comment_char ) [ <NUM_LIT:0> ] ) <EOL> inputfile . close ( ) <EOL> def set_delimiters ( self , delimiter ) : <EOL> """<STR_LIT>""" <EOL> self . delimiter = delimiter <EOL> if delimiter != "<STR_LIT>" : <EOL> ParserElement . setDefaultWhitespaceChars ( str ( delimiter ) ) <EOL> self . _reset_tokens ( ) <EOL> def mark_anchor ( self , anchor , occurrence = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( occurrence , int ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> instance = <NUM_LIT:0> <EOL> if occurrence > <NUM_LIT:0> : <EOL> count = <NUM_LIT:0> <EOL> max_lines = len ( self . data ) <EOL> for index in range ( self . current_row , max_lines ) : <EOL> line = self . data [ index ] <EOL> if count == <NUM_LIT:0> and self . anchored : <EOL> line = line . split ( anchor ) [ - <NUM_LIT:1> ] <EOL> if anchor in line : <EOL> instance += <NUM_LIT:1> <EOL> if instance == occurrence : <EOL> self . current_row += count <EOL> self . anchored = True <EOL> return <EOL> count += <NUM_LIT:1> <EOL> elif occurrence < <NUM_LIT:0> : <EOL> max_lines = len ( self . data ) - <NUM_LIT:1> <EOL> count = max_lines <EOL> for index in range ( max_lines , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> line = self . data [ index ] <EOL> if count == max_lines and self . anchored : <EOL> line = line . split ( anchor ) [ <NUM_LIT:0> ] <EOL> if anchor in line : <EOL> instance += - <NUM_LIT:1> <EOL> if instance == occurrence : <EOL> self . current_row = count <EOL> self . anchored = True <EOL> return <EOL> count -= <NUM_LIT:1> <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> raise RuntimeError ( "<STR_LIT>" % ( anchor , self . filename ) ) <EOL> def reset_anchor ( self ) : <EOL> """<STR_LIT>""" <EOL> self . current_row = <NUM_LIT:0> <EOL> self . anchored = False <EOL> def transfer_line ( self , row ) : <EOL> """<STR_LIT>""" <EOL> return self . data [ self . current_row + row ] . rstrip ( ) <EOL> def transfer_var ( self , row , field , fieldend = None ) : <EOL> """<STR_LIT>""" <EOL> j = self . current_row + row <EOL> line = self . data [ j ] <EOL> if self . delimiter == "<STR_LIT>" : <EOL> if not fieldend : <EOL> line = line [ ( field - <NUM_LIT:1> ) : ] <EOL> else : <EOL> line = line [ ( field - <NUM_LIT:1> ) : ( fieldend ) ] <EOL> data = self . _parse_line ( ) . parseString ( line ) <EOL> if len ( data ) > <NUM_LIT:1> : <EOL> return line <EOL> else : <EOL> return data [ <NUM_LIT:0> ] <EOL> else : <EOL> data = self . _parse_line ( ) . parseString ( line ) <EOL> return data [ field - <NUM_LIT:1> ] <EOL> def transfer_keyvar ( self , key , field , occurrence = <NUM_LIT:1> , rowoffset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( occurrence , int ) or occurrence == <NUM_LIT:0> : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> instance = <NUM_LIT:0> <EOL> if occurrence > <NUM_LIT:0> : <EOL> row = <NUM_LIT:0> <EOL> for line in self . data [ self . current_row : ] : <EOL> if line . find ( key ) > - <NUM_LIT:1> : <EOL> instance += <NUM_LIT:1> <EOL> if instance == occurrence : <EOL> break <EOL> row += <NUM_LIT:1> <EOL> elif occurrence < <NUM_LIT:0> : <EOL> row = - <NUM_LIT:1> <EOL> for line in reversed ( self . data [ self . current_row : ] ) : <EOL> if line . find ( key ) > - <NUM_LIT:1> : <EOL> instance += - <NUM_LIT:1> <EOL> if instance == occurrence : <EOL> break <EOL> row -= <NUM_LIT:1> <EOL> j = self . current_row + row + rowoffset <EOL> line = self . data [ j ] <EOL> fields = self . _parse_line ( ) . parseString ( line . replace ( key , "<STR_LIT>" ) ) <EOL> return fields [ field ] <EOL> def transfer_array ( self , rowstart , fieldstart , rowend = None , fieldend = None ) : <EOL> """<STR_LIT>""" <EOL> j1 = self . current_row + rowstart <EOL> if rowend is None : <EOL> j2 = j1 + <NUM_LIT:1> <EOL> else : <EOL> j2 = self . current_row + rowend + <NUM_LIT:1> <EOL> if not fieldend : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> lines = self . data [ j1 : j2 ] <EOL> data = np . zeros ( shape = ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> for i , line in enumerate ( lines ) : <EOL> if self . delimiter == "<STR_LIT>" : <EOL> line = line [ ( fieldstart - <NUM_LIT:1> ) : fieldend ] <EOL> line = line . strip ( ) <EOL> parsed = self . _parse_line ( ) . parseString ( line ) <EOL> newdata = np . array ( parsed [ : ] ) <EOL> if newdata . dtype . type is np . str_ : <EOL> newdata = np . array ( line ) <EOL> data = np . append ( data , newdata ) <EOL> else : <EOL> parsed = self . _parse_line ( ) . parseString ( line ) <EOL> if i == j2 - j1 - <NUM_LIT:1> : <EOL> data = np . append ( data , np . array ( parsed [ ( fieldstart - <NUM_LIT:1> ) : fieldend ] ) ) <EOL> else : <EOL> data = np . append ( data , np . array ( parsed [ ( fieldstart - <NUM_LIT:1> ) : ] ) ) <EOL> fieldstart = <NUM_LIT:1> <EOL> return data <EOL> def transfer_2Darray ( self , rowstart , fieldstart , rowend , fieldend = None ) : <EOL> """<STR_LIT>""" <EOL> if fieldend and ( fieldstart > fieldend ) : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> if rowstart > rowend : <EOL> msg = "<STR_LIT>" <EOL> raise ValueError ( msg ) <EOL> j1 = self . current_row + rowstart <EOL> j2 = self . current_row + rowend + <NUM_LIT:1> <EOL> lines = list ( self . data [ j1 : j2 ] ) <EOL> if self . delimiter == "<STR_LIT>" : <EOL> if fieldend : <EOL> line = lines [ <NUM_LIT:0> ] [ ( fieldstart - <NUM_LIT:1> ) : fieldend ] <EOL> else : <EOL> line = lines [ <NUM_LIT:0> ] [ ( fieldstart - <NUM_LIT:1> ) : ] <EOL> parsed = self . _parse_line ( ) . parseString ( line ) <EOL> row = np . array ( parsed [ : ] ) <EOL> data = np . zeros ( shape = ( abs ( j2 - j1 ) , len ( row ) ) ) <EOL> data [ <NUM_LIT:0> , : ] = row <EOL> for i , line in enumerate ( list ( lines [ <NUM_LIT:1> : ] ) ) : <EOL> if fieldend : <EOL> line = line [ ( fieldstart - <NUM_LIT:1> ) : fieldend ] <EOL> else : <EOL> line = line [ ( fieldstart - <NUM_LIT:1> ) : ] <EOL> parsed = self . _parse_line ( ) . parseString ( line ) <EOL> data [ i + <NUM_LIT:1> , : ] = np . array ( parsed [ : ] ) <EOL> else : <EOL> parsed = self . _parse_line ( ) . parseString ( lines [ <NUM_LIT:0> ] ) <EOL> if fieldend : <EOL> row = np . array ( parsed [ ( fieldstart - <NUM_LIT:1> ) : fieldend ] ) <EOL> else : <EOL> row = np . array ( parsed [ ( fieldstart - <NUM_LIT:1> ) : ] ) <EOL> data = np . zeros ( shape = ( abs ( j2 - j1 ) , len ( row ) ) ) <EOL> data [ <NUM_LIT:0> , : ] = row <EOL> for i , line in enumerate ( list ( lines [ <NUM_LIT:1> : ] ) ) : <EOL> parsed = self . _parse_line ( ) . parseString ( line ) <EOL> if fieldend : <EOL> try : <EOL> data [ i + <NUM_LIT:1> , : ] = np . array ( parsed [ ( fieldstart - <NUM_LIT:1> ) : fieldend ] ) <EOL> except : <EOL> print ( data ) <EOL> else : <EOL> data [ i + <NUM_LIT:1> , : ] = np . array ( parsed [ ( fieldstart - <NUM_LIT:1> ) : ] ) <EOL> return data <EOL> def _parse_line ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . line_parse_token <EOL> def _reset_tokens ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . delimiter . isspace ( ) : <EOL> textchars = printables <EOL> else : <EOL> textchars = alphanums <EOL> symbols = [ '<STR_LIT:.>' , '<STR_LIT:/>' , '<STR_LIT:+>' , '<STR_LIT:*>' , '<STR_LIT>' , '<STR_LIT:(>' , '<STR_LIT:)>' , '<STR_LIT:[>' , '<STR_LIT:]>' , '<STR_LIT:=>' , <EOL> '<STR_LIT::>' , '<STR_LIT:;>' , '<STR_LIT:?>' , '<STR_LIT:%>' , '<STR_LIT:&>' , '<STR_LIT:!>' , '<STR_LIT:#>' , '<STR_LIT:|>' , '<STR_LIT:<>' , '<STR_LIT:>>' , <EOL> '<STR_LIT:{>' , '<STR_LIT:}>' , '<STR_LIT:->' , '<STR_LIT:_>' , '<STR_LIT:@>' , '<STR_LIT:$>' , '<STR_LIT>' ] <EOL> for symbol in symbols : <EOL> if symbol not in self . delimiter : <EOL> textchars = textchars + symbol <EOL> digits = Word ( nums ) <EOL> dot = "<STR_LIT:.>" <EOL> sign = oneOf ( "<STR_LIT>" ) <EOL> ee = CaselessLiteral ( '<STR_LIT:E>' ) | CaselessLiteral ( '<STR_LIT:D>' ) <EOL> num_int = ToInteger ( Combine ( Optional ( sign ) + digits ) ) <EOL> num_float = ToFloat ( Combine ( Optional ( sign ) + <EOL> ( ( digits + dot + Optional ( digits ) ) | <EOL> ( dot + digits ) ) + <EOL> Optional ( ee + Optional ( sign ) + digits ) <EOL> ) ) <EOL> mixed_exp = ToFloat ( Combine ( digits + ee + Optional ( sign ) + digits ) ) <EOL> nan = ToInf ( oneOf ( "<STR_LIT>" ) ) | ToNan ( oneOf ( "<STR_LIT>" + "<STR_LIT>" ) ) <EOL> string_text = Word ( textchars ) <EOL> self . line_parse_token = ( OneOrMore ( ( nan | num_float | mixed_exp | num_int | <EOL> string_text ) ) ) </s>
