Here is the first pass of a method I wrote for displaying the relation in tabular form. One or two of the for loops could probably be replaced with a list comprehension and I should probably write to a file object that gets passed in rather than use print, but it serves the purpose of pretty printing during experimentation on the relational operators.
It will result in a display like:
+-----+-------+-----+--------+ | ENO | ENAME | DNO | SALARY | +-----+-------+-----+--------+ | E1 | Lopez | D1 | 40K | | E3 | Finzi | D2 | 30K | | E2 | Cheng | D1 | 42K | +-----+-------+-----+--------+
If it seems inefficient that display uses self.tuples() rather than self.tuples_, it is because that way it will work later on on views where tuples() is dynamic.
def display(self): columns = range(len(self.attributes_)) col_width = [len(self.attributes_[col]) for col in columns] for tupdict in self.tuples(): tup = self._convert_dict(tupdict) for col in columns: col_width[col] = max(col_width[col], len(tup[col])) hline = "" for col in columns: hline += "+-" + ("-" * col_width[col]) + "-" hline += "+" def line(row): l = "" for col in columns: value = row[col] l += "| " + value + (" " * (col_width[col] - len(value))) + " " l += "|" return l print hline print line(self.attributes_) print hline for tup in self.tuples(): print line(self._convert_dict(tup)) print hline
The original post was in the categories: python relational_python but I'm still in the process of migrating categories over.