def join(keys, tables):
<EOL>fields = OrderedDict()<EOL>for table in tables:<EOL><INDENT>fields.update(table.fields)<EOL><DEDENT>fields_keys = set(fields.keys())<EOL>for key in keys:<EOL><INDENT>if key not in fields_keys:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(key))<EOL><DEDENT><DEDENT>none_fields = lambda: OrderedDict({field: None for field in fields.keys()})<EOL>data = OrderedDict()<EOL>for table in tables:<EOL><INDENT>for row in table:<EOL><INDENT>row_key = tuple([getattr(row, key) for key in keys])<EOL>if row_key not in data:<EOL><INDENT>data[row_key] = none_fields()<EOL><DEDENT>data[row_key].update(row._asdict())<EOL><DEDENT><DEDENT>merged = Table(fields=fields)<EOL>merged.extend(data.values())<EOL>return merged<EOL>
Merge a list of `Table` objects using `keys` to group rows