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
by : Created on Nov. 11, 2005 : Last modified Nov. 11, 2005 : (permalink)
If you've sent me email in the last 18 hours, I haven't been able to read it and may not be able to do so for some time. My mail provider has had an outage (8+ hours so far) and is claiming that it will still be hours before service is returned.
UPDATE (2005-11-12): Email is still down. Could turn out to be days. Looks like Merlin Mann uses the same mail host.
UPDATE (2005-11-13): I woke up this morning and mail was working. Unfortunately iMac isn't.
by : Created on Nov. 11, 2005 : Last modified Nov. 12, 2005 : (permalink)