/usr/share/pyshared/pytools/datatable.py is in python-pytools 2013.5.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | from pytools import Record
class Row(Record):
pass
class DataTable:
"""An in-memory relational database table."""
def __init__(self, column_names, column_data=None):
"""Construct a new table, with the given C{column_names}.
@arg column_names: An indexable of column name strings.
@arg column_data: None or a list of tuples of the same length as
C{column_names} indicating an initial set of data.
"""
if column_data is None:
self.data = []
else:
self.data = column_data
self.column_names = column_names
self.column_indices = dict(
(colname, i) for i, colname in enumerate(column_names))
if len(self.column_indices) != len(self.column_names):
raise RuntimeError("non-unique column names encountered")
def __bool__(self):
return bool(self.data)
def __len__(self):
return len(self.data)
def __iter__(self):
return self.data.__iter__()
def __str__(self):
"""Return a pretty-printed version of the table."""
def col_width(i):
width = len(self.column_names[i])
if self:
width = max(width, max(len(str(row[i])) for row in self.data))
return width
col_widths = [col_width(i) for i in range(len(self.column_names))]
def format_row(row):
return "|".join([str(cell).ljust(col_width)
for cell, col_width in zip(row, col_widths)])
lines = [format_row(self.column_names),
"+".join("-"*col_width for col_width in col_widths)] + \
[format_row(row) for row in self.data]
return "\n".join(lines)
def insert(self, **kwargs):
values = [None for i in range(len(self.column_names))]
for key, val in kwargs.iteritems():
values[self.column_indices[key]] = val
self.insert_row(tuple(values))
def insert_row(self, values):
assert isinstance(values, tuple)
assert len(values) == len(self.column_names)
self.data.append(values)
def insert_rows(self, rows):
for row in rows:
self.insert_row(row)
def filtered(self, **kwargs):
if not kwargs:
return self
criteria = tuple(
(self.column_indices[key], value)
for key, value in kwargs.iteritems())
result_data = []
for row in self.data:
satisfied = True
for idx, val in criteria:
if row[idx] != val:
satisfied = False
break
if satisfied:
result_data.append(row)
return DataTable(self.column_names, result_data)
def get(self, **kwargs):
filtered = self.filtered(**kwargs)
if len(filtered) < 1:
raise RuntimeError("no matching entry for get()")
if len(filtered) > 1:
raise RuntimeError("more than one matching entry for get()")
return Row(dict(zip(self.column_names, filtered.data[0])))
def clear(self):
del self.data[:]
def copy(self):
"""Make a copy of the instance, but leave individual rows untouched.
If the rows are modified later, they will also be modified in the copy.
"""
return DataTable(self.column_names, self.data[:])
def deep_copy(self):
"""Make a copy of the instance down to the row level.
The copy's rows may be modified independently from the original.
"""
return DataTable(self.column_names, [row[:] for row in self.data])
def sort(self, columns, reverse=False):
col_indices = [self.column_indices[col] for col in columns]
def mykey(row):
return tuple(
row[col_index]
for col_index in col_indices)
self.data.sort(reverse=reverse, key=mykey)
def aggregated(self, groupby, agg_column, aggregate_func):
gb_indices = [self.column_indices[col] for col in groupby]
agg_index = self.column_indices[agg_column]
first = True
result_data = []
# to pacify pyflakes:
last_values = None
agg_values = None
for row in self.data:
this_values = tuple(row[i] for i in gb_indices)
if first or this_values != last_values:
if not first:
result_data.append(last_values + (aggregate_func(agg_values),))
agg_values = [row[agg_index]]
last_values = this_values
first = False
else:
agg_values.append(row[agg_index])
if not first and agg_values:
result_data.append(this_values + (aggregate_func(agg_values),))
return DataTable(
[self.column_names[i] for i in gb_indices] + [agg_column],
result_data)
def join(self, column, other_column, other_table, outer=False):
"""Return a tabled joining this and the C{other_table} on C{column}.
The new table has the following columns:
- C{column}, titled the same as in this table.
- the columns of this table, minus C{column}.
- the columns of C{other_table}, minus C{other_column}.
Assumes both tables are sorted ascendingly by the column
by which they are joined.
"""
def without(indexable, idx):
return indexable[:idx] + indexable[idx+1:]
this_key_idx = self.column_indices[column]
other_key_idx = other_table.column_indices[other_column]
this_iter = self.data.__iter__()
other_iter = other_table.data.__iter__()
result_columns = [self.column_names[this_key_idx]] + \
without(self.column_names, this_key_idx) + \
without(other_table.column_names, other_key_idx)
result_data = []
this_row = this_iter.next()
other_row = other_iter.next()
this_over = False
other_over = False
while True:
this_batch = []
other_batch = []
if this_over:
run_other = True
elif other_over:
run_this = True
else:
this_key = this_row[this_key_idx]
other_key = other_row[other_key_idx]
run_this = this_key < other_key
run_other = this_key > other_key
if this_key == other_key:
run_this = run_other = True
if run_this and not this_over:
key = this_key
while this_row[this_key_idx] == this_key:
this_batch.append(this_row)
try:
this_row = this_iter.next()
except StopIteration:
this_over = True
break
else:
if outer:
this_batch = [(None,) * len(self.column_names)]
if run_other and not other_over:
key = other_key
while other_row[other_key_idx] == other_key:
other_batch.append(other_row)
try:
other_row = other_iter.next()
except StopIteration:
other_over = True
break
else:
if outer:
other_batch = [(None,) * len(other_table.column_names)]
for this_batch_row in this_batch:
for other_batch_row in other_batch:
result_data.append((key,) +
without(this_batch_row, this_key_idx) +
without(other_batch_row, other_key_idx))
if outer:
if this_over and other_over:
break
else:
if this_over or other_over:
break
return DataTable(result_columns, result_data)
def restricted(self, columns):
col_indices = [self.column_indices[col] for col in columns]
return DataTable(columns,
[[row[i] for i in col_indices] for row in self.data])
def column_data(self, column):
col_index = self.column_indices[column]
return [row[col_index] for row in self.data]
def write_csv(self, filelike, **kwargs):
from csv import writer
csvwriter = writer(filelike, **kwargs)
csvwriter.writerow(self.column_names)
csvwriter.writerows(self.data)
|