Revision 1765
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/maps.py | ||
---|---|---|
6 | 6 |
import strings |
7 | 7 |
import util |
8 | 8 |
|
9 |
##### Metadata |
|
10 |
|
|
9 | 11 |
def col_info(col_name, require_root=False): |
10 | 12 |
'''@return tuple (label, root, prefixes)''' |
11 | 13 |
def syntax_err(): raise Parser.SyntaxError('Column name must have ' |
... | ... | |
17 | 19 |
if require_root and root == None: syntax_err() |
18 | 20 |
return label, root, strings.split(',', util.coalesce(prefixes, '')) |
19 | 21 |
|
22 |
##### Combinability |
|
23 |
|
|
20 | 24 |
def col_formats(col_name): |
21 | 25 |
label, root, prefixes = col_info(col_name) |
22 | 26 |
return [label]+prefixes |
23 | 27 |
|
24 |
def combinable(*headers):
|
|
25 |
return strings.overlaps(*[','.join(col_formats(h[0])) for h in headers])
|
|
28 |
def cols_combinable(*col_names):
|
|
29 |
return strings.overlaps(*[','.join(col_formats(c)) for c in col_names])
|
|
26 | 30 |
|
31 |
def combinable(*headers): return cols_combinable(*[h[0] for h in headers]) |
|
32 |
|
|
33 |
def strip_root(root): return re.sub(r':\[[^\]]*?\]', r'', root) |
|
34 |
|
|
35 |
def stripped_root(col_name): |
|
36 |
'''@return NamedTuple (labels, root)''' |
|
37 |
label, root = col_info(col_name)[:2] |
|
38 |
return util.do_ignore_none(strip_root, root) |
|
39 |
|
|
40 |
def roots_combinable(*roots): |
|
41 |
roots = map(stripped_root, roots) |
|
42 |
return roots[0] == None or roots[1] == None or roots[0] == roots[1] |
|
43 |
|
|
44 |
def join_combinable(*headers): |
|
45 |
cols = [headers[0][1], headers[1][0]] |
|
46 |
return combinable(*cols) and roots_combinable(*cols) |
|
47 |
|
|
48 |
##### Merging |
|
49 |
|
|
27 | 50 |
def is_nonexplicit_empty_mapping(row): |
28 | 51 |
return reduce(util.and_, (v == '' for v in row[1:])) |
29 | 52 |
|
Also available in: Unified diff
maps.py: Added cols_combinable() and use it in combinable(). Added join_combinable() and associates helper functions. Added documentation labels to each section.