Revision 1768
Added by Aaron Marcuse-Kubitza over 12 years ago
maps.py | ||
---|---|---|
34 | 34 |
|
35 | 35 |
def stripped_root(col_name): |
36 | 36 |
'''@return NamedTuple (labels, root)''' |
37 |
label, root = col_info(col_name)[:2] |
|
38 |
return util.do_ignore_none(strip_root, root) |
|
37 |
return util.do_ignore_none(strip_root, col_info(col_name)[1]) |
|
39 | 38 |
|
40 |
def roots_combinable(*roots):
|
|
41 |
roots = map(stripped_root, roots)
|
|
39 |
def cols_root_combinable(*col_names):
|
|
40 |
roots = map(stripped_root, col_names)
|
|
42 | 41 |
return roots[0] == None or roots[1] == None or roots[0] == roots[1] |
43 | 42 |
|
43 |
def cols_fully_combinable(*col_names): |
|
44 |
return cols_combinable(*col_names) and cols_root_combinable(*col_names) |
|
45 |
|
|
44 | 46 |
def join_combinable(*headers): |
45 |
cols = [headers[0][1], headers[1][0]] |
|
46 |
return combinable(*cols) and roots_combinable(*cols) |
|
47 |
return cols_fully_combinable(headers[0][1], headers[1][0]) |
|
47 | 48 |
|
48 | 49 |
##### Merging |
49 | 50 |
|
... | ... | |
64 | 65 |
return [merge_values(*[get(row, i) for row in rows]) |
65 | 66 |
for i in xrange(max(map(len, rows)))] |
66 | 67 |
|
67 |
def merge_mappings(in_, out): |
|
68 |
def merge_mapping_cols(in_, out, prefer=None): |
|
69 |
'''@param prefer None = [in_[0], out[1]]; 0 = in_; 1 = out''' |
|
70 |
if prefer == None: return [in_[0], out[1]] |
|
71 |
elif prefer == 0: return in_ |
|
72 |
elif prefer == 1: return out |
|
73 |
else: raise Parser.SyntaxError('Invalid prefer: '+repr(prefer)) |
|
74 |
|
|
75 |
def merge_mappings(in_, out, **kw_args): |
|
68 | 76 |
'''e.g. ['in','join','in_comments'] + ['join','out','out_comments'] = |
69 | 77 |
['in','out','in_comments; out_comments']''' |
70 |
new = [in_[0], out[1]] # mapping |
|
71 |
new[2:] = merge_rows(in_[2:], out[2:]) # comments |
|
72 |
return new |
|
78 |
return (merge_mapping_cols(in_[:2], out[:2], **kw_args) |
|
79 |
+ merge_rows(in_[2:], out[2:])) |
|
80 |
|
|
81 |
def merge_headers(in_, out, **kw_args): |
|
82 |
out_cols = [in_[1], out[1]] |
|
83 |
if cols_fully_combinable(*out_cols): out[1] = util.longest(*out_cols) |
|
84 |
return merge_mappings(in_, out, **kw_args) |
Also available in: Unified diff
maps.py: join_combinable(): Fixed roots_combinable() to run on col names instead of roots, which were passed in. merge_mappings(): Factored out mapping column combining into merge_mapping_cols(), which handles an optional prefer param as well to take the header_num env var. Added merge_headers().