|
|
@@ -72,6 +72,7 @@ class Parser(object): |
|
|
|
|
|
|
|
self.layout = named[layout] |
|
|
|
self.data = [] |
|
|
|
self.nrows = 0 |
|
|
|
|
|
|
|
def parse(self, textdata): |
|
|
|
"""Parse the data, provided as lines of text, using the current |
|
|
@@ -80,16 +81,20 @@ class Parser(object): |
|
|
|
# This currently takes about 0.1 seconds for 1 megabyte of prep data, |
|
|
|
# 85 klines/sec. Could clearly be optimized a lot... |
|
|
|
indata = cStringIO.StringIO(textdata) |
|
|
|
n = 0 |
|
|
|
self.nrows = 0 |
|
|
|
# Assume any parsing error is a real error. |
|
|
|
# In the future we might want to skip completely empty lines, |
|
|
|
# or partial lines right before EOF? |
|
|
|
try: |
|
|
|
for line in indata: |
|
|
|
n += 1 |
|
|
|
self.nrows += 1 |
|
|
|
fields = line.partition('#')[0].split() |
|
|
|
self.data.append(self.layout.parse(fields)) |
|
|
|
except (ValueError, TypeError, IndexError) as e: |
|
|
|
raise TypeError("line " + n + ": " + e.message) |
|
|
|
raise TypeError("line " + self.nrows + ": " + e.message) |
|
|
|
|
|
|
|
def fillrow(self, row): |
|
|
|
"""Fill a PyTables row object that matches the layout with the |
|
|
|
stored data""" |
|
|
|
|
|
|
|
def fillrow(self, tablerow, rownum): |
|
|
|
"""Fill a PyTables row object with the parsed data. |
|
|
|
The row must match the parser's layout""" |
|
|
|
for (n, (name, type)) in enumerate(self.layout.fields): |
|
|
|
tablerow[name] = self.data[rownum][n] |