MCPcopy Create free account
hub / github.com/ActiveState/code / Table

Class Table

recipes/Python/577825_Rudimentary_Database_Engine/recipe-577825.py:321–722  ·  view source on GitHub ↗

Table(*columns) -> Table

Source from the content-addressed store, hash-verified

319################################################################################
320
321class Table:
322
323 "Table(*columns) -> Table"
324
325 @classmethod
326 def from_iter(cls, iterator):
327 "Generates a table from a column / rows iterator."
328 title, test_row, *rows = iterator
329 table = cls(*zip(title, map(type, test_row)))
330 table.insert(*test_row)
331 for row in rows:
332 table.insert(*row)
333 return table
334
335 ########################################################################
336
337 __slots__ = _slots('columns data_area row_index')
338
339 def __init__(self, *columns):
340 "Initializes Table with columns and row storage area."
341 self.__columns = _Columns(columns)
342 self.__data_area = {}
343 self.__row_index = 1
344
345 def __len__(self):
346 "Returns the number of rows in the table."
347 return len(self.__data_area)
348
349 def __repr__(self):
350 "Creates a complete representation of the table."
351 buffer = [list(map(repr, ['ROW_ID'] + [name for index, name, data_type \
352 in self.__columns]))]
353 width = [0] * len(buffer[0])
354 for row in sorted(self.__data_area):
355 buffer.append(list(map(repr, [row] + [self.__data_area[row][index] \
356 for index, name, data_type \
357 in self.__columns])))
358 for row in buffer:
359 for index, string in enumerate(row):
360 width[index] = max(width[index], len(string))
361 string = ''
362 for index, value in enumerate(buffer[0]):
363 string += value.ljust(width[index]) + ' | '
364 string = string[:-3] + '\n'
365 for index in range(len(buffer[0])):
366 string += '-' * width[index] + '-+-'
367 string = string[:-3] + '\n'
368 for row in buffer[1:]:
369 for index, value in enumerate(row):
370 string += value.ljust(width[index]) + ' | '
371 string = string[:-3] + '\n'
372 return string[:-1]
373
374 def __str__(self):
375 names, *rows = self
376 columns = {name: [] for name in names}
377 for row in rows:
378 for key, value in zip(names, row):

Callers 12

__init__Method · 0.70
__setstate__Method · 0.70
createMethod · 0.70
_composite_tableFunction · 0.70
test_basic_sqlFunction · 0.70
test_all_joinsFunction · 0.70
test_table_additionFunction · 0.70
test_database_supportFunction · 0.70
category_sales_for_1997Function · 0.70
test_date_functionalityFunction · 0.70
test_column_functionsFunction · 0.70

Calls 1

_slotsFunction · 0.85

Tested by 7

test_basic_sqlFunction · 0.56
test_all_joinsFunction · 0.56
test_table_additionFunction · 0.56
test_database_supportFunction · 0.56
test_date_functionalityFunction · 0.56
test_column_functionsFunction · 0.56