From 519484233cada6f7836a471a9742807e9eabbbe0 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 3 Nov 2015 21:14:45 +0000 Subject: [PATCH] =?UTF-8?q?can=20now=20deal=20with=20=E2=80=9Cempty?= =?UTF-8?q?=E2=80=9D=20header=20line.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit example: # some comment # further stuff 1_dim 2_dim 34 33 1 22 would work with “labeled”=True and stores the first two lines as “info”. --- lib/damask/asciitable.py | 44 ++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/damask/asciitable.py b/lib/damask/asciitable.py index 9ebc3bf71..7836f4fb4 100644 --- a/lib/damask/asciitable.py +++ b/lib/damask/asciitable.py @@ -152,24 +152,37 @@ class ASCIItable(): firstline = self.__IO__['in'].readline() m = re.search('(\d+)\s+head', firstline.lower()) # search for "head" keyword - if self.__IO__['labeled']: # table features labels - if m: # found header info + + if m: # proper ASCIItable format + + if self.__IO__['labeled']: # table features labels + self.info = [self.__IO__['in'].readline().strip() for i in xrange(1,int(m.group(1)))] self.labels = self.__IO__['in'].readline().split() # store labels found in last line - else: # no header info (but labels) - self.labels = firstline.split() # store labels from first line + else: + + self.info = [self.__IO__['in'].readline().strip() for i in xrange(0,int(m.group(1)))] # all header is info ... + + else: # other table format + try: + self.__IO__['in'].seek(0) # try to rewind + except: + self.__IO__['readBuffer'] = [firstline] # or at least save data in buffer + + while self.data_read(advance = False, respectLabels = False): + if self.line[0] in ['#','!','%','/','|','*','$']: # "typical" comment indicators + self.info_append(self.line) # store comment as info + self.data_read() # wind forward one line + else: break # last line of comments + + if self.__IO__['labeled']: # table features labels + self.labels = self.data # get labels from last line in "header"... + self.data_read() # ...and remove from buffer + + if self.__IO__['labeled']: # table features labels self.__IO__['labels'] = list(self.labels) # backup labels (make COPY, not link) - else: # no labels present in table - if m: # found header info - self.info = [self.__IO__['in'].readline().strip() for i in xrange(0,int(m.group(1)))] # all header is info ... - # ... without any labels - else: # otherwise file starts with data right away - try: - self.__IO__['in'].seek(0) # try to rewind - except: - self.__IO__['readBuffer'] = [firstline] # or at least save data in buffer try: self.__IO__['dataStart'] = self.__IO__['in'].tell() # current file position is at start of data except IOError: @@ -403,7 +416,8 @@ class ASCIItable(): # ------------------------------------------------------------------ def data_read(self, - advance = True): + advance = True, + respectLabels = True): ''' read next line (possibly buffered) and parse it into data array ''' @@ -415,7 +429,7 @@ class ASCIItable(): self.line = self.line.rstrip('\n') - if self.__IO__['labeled']: # if table has labels + if self.__IO__['labeled'] and respectLabels: # if table has labels items = self.line.split()[:len(self.__IO__['labels'])] # use up to label count (from original file info) self.data = items if len(items) == len(self.__IO__['labels']) else [] # take entries if correct number, i.e. not too few compared to label count else: