def read(self, filename): # @@ 2000-11-24 ce: split into readTable() self._filename = filename # PickleCache is used at the Model level, so we don't use it here: table = DataTable(filename, usePickleCache=False) # in case we want to look at these later: self._tableHeadings = table.headings() try: line = 2 for row in table: row = ExpandDictWithExtras(row, dictForArgs=PyDictForArgs) for key in ['Class', 'Attribute']: if key not in row: print 'ERROR' print 'Required key %s not found in row:' % key print 'row:', row print 'keys:', row.keys() print row[key] # throws exception if row['Class']: pyClass = self._model.coreClass('Klass') klass = pyClass(self, row) self.addKlass(klass) else: name = row['Attribute'] if name and name[0] != '#' and name[-1] != ':': pyClassName = self.pyClassNameForAttrDict(row) pyClass = self._model.coreClass(pyClassName) klass.addAttr(pyClass(row)) line += 1 except ModelError, e: e.setLine(line) raise
def read(self, filename): # @@ 2000-11-24 ce: split into readTable() self._filename = filename # PickleCache is used at the Model level, so we don't use it here: table = DataTable(filename, usePickleCache=False) # in case we want to look at these later: self._tableHeadings = table.headings() try: line = 2 for row in table: row = ExpandDictWithExtras(row, dictForArgs=PyDictForArgs) for key in ['Class', 'Attribute']: if key not in row: print 'ERROR' print 'Required key %s not found in row:' % key print 'row:', row print 'keys:', row.keys() print row[key] # throws exception if row['Class']: pyClass = self._model.coreClass('Klass') klass = pyClass(self, row) self.addKlass(klass) else: name = row['Attribute'] if name and name[0] != '#' and name[-1] != ':': pyClassName = self.pyClassNameForAttrDict(row) pyClass = self._model.coreClass(pyClassName) klass.addAttr(pyClass(row)) line += 1 except ModelError as e: e.setLine(line) raise
def writeContent(self): if not os.path.exists(self._filename): self.writeln('<p>File does not exist.</p>') return table = DataTable(self._filename) plural = len(table) != 1 and 's' or '' self.writeln('<p>%d row%s</p>' % (len(table), plural)) self.writeln('<table class="NiceTable" cellpadding="2" cellspacing="2">') # Head row gets special formatting self._headings = map(lambda col: col.name().strip(), table.headings()) self._numCols = len(self._headings) self.writeln('<tr>') for value in self._headings: self.writeln('<th>', value, '</th>') self.writeln('</tr>') # Data rows rowIndex = 1 for row in table: self.writeln('<tr>') colIndex = 0 for value in row: if colIndex < self._numCols: # for those cases where a row has more columns that the header row. self.writeln('<td>', self.cellContents(rowIndex, colIndex, value), '</td>') colIndex += 1 self.writeln('</tr>') rowIndex += 1 self.writeln('</table>')
def writeContent(self): if not os.path.exists(self._filename): self.writeln('<p>File does not exist.</p>') return table = DataTable(self._filename) plural = 's' if len(table) != 1 else '' self.writeln('<p>%d row%s</p>' % (len(table), plural)) self.writeln('<table class="NiceTable">') # Head row gets special formatting self._headings = map(lambda col: col.name().strip(), table.headings()) self._numCols = len(self._headings) self.writeln('<tr>') for value in self._headings: self.writeln('<th>', value, '</th>') self.writeln('</tr>') # Data rows rowIndex = 1 for row in table: self.writeln('<tr>') colIndex = 0 for value in row: if colIndex < self._numCols: # for those cases where a row has more columns that the header row. self.writeln('<td>', self.cellContents(rowIndex, colIndex, value), '</td>') colIndex += 1 self.writeln('</tr>') rowIndex += 1 self.writeln('</table>')
def writeContent(self): if not os.path.exists(self._filename): self.writeln('<p>File does not exist.</p>') return table = DataTable(self._filename) plural = '' if len(table) == 1 else 's' self.writeln(f'<p>{len(table)} row{plural}</p>') self.writeln('<table class="NiceTable">') # Head row gets special formatting self._headings = [col.name().strip() for col in table.headings()] self._numCols = len(self._headings) self.writeln('<tr>') for value in self._headings: self.writeln('<th>', value, '</th>') self.writeln('</tr>') # Data rows for rowIndex, row in enumerate(table, 1): self.writeln('<tr>') for colIndex, value in enumerate(row): if colIndex >= self._numCols: break # skip surplus columns self.writeln('<td>', self.cellContents(rowIndex, colIndex, value), '</td>') self.writeln('</tr>') self.writeln('</table>')
def _testSource(self, name, src, headings, data): dataTable = DataTable() lines = src.splitlines() dataTable.readLines(lines) self.assertEqual([c.name() for c in dataTable.headings()], headings) for i, values in enumerate(dataTable): match = data[i] asList = values.asList() self.assertEqual( asList, match, f'{name}: Row {i}: Expected {match!r}, but got {asList!r}')
def _testSource(self, name, src, headings, data): # print name dt = DataTable() lines = src.splitlines() dt.readLines(lines) self.assertEqual([col.name() for col in dt.headings()], headings) for i, values in enumerate(dt): match = data[i] self.assertEqual(values.asList(), match, 'For element %d, I expected "%s" but got "%s"' % (i, match, values.asList()))
def _testSource(self, name, src, headings, data): # print name dt = DataTable() lines = src.splitlines() dt.readLines(lines) self.assertEqual([col.name() for col in dt.headings()], headings) for i, values in enumerate(dt): match = data[i] self.assertEqual(values.asList(), match, 'For element %d, I expected "%s" but got "%s"' % (i, match, values.asList()))
def writeContent(self): if not os.path.exists(self._filename): self.writeln('<p> File does not exist.') return table = DataTable(self._filename) if len(table)==1: plural = '' else: plural = 's' self.writeln('<p>%d row%s' % (len(table), plural)) self.writeln('<br><table align=center border=0 cellpadding=2 cellspacing=2>') # Head row gets special formatting self._headings = map(lambda col: string.strip(col.name()), table.headings()) self._numCols = len(self._headings) self.writeln('<tr bgcolor=black>') for value in self._headings: self.writeln('<td><font face="Arial, Helvetica" color=white><b> ', value, ' </b></font></td>') self.writeln('</tr>') # Data rows rowIndex = 1 for row in table: self.writeln('<tr bgcolor=#EEEEEE>') colIndex = 0 for value in row: if colIndex<self._numCols: # for those cases where a row has more columns that the header row. self.writeln('<td> ', self.cellContents(rowIndex, colIndex, value), ' </td>') colIndex = colIndex + 1 self.writeln('</tr>') rowIndex = rowIndex + 1 self.writeln('</table>')