Revision 4508
Added by Aaron Marcuse-Kubitza over 12 years ago
subtract | ||
---|---|---|
7 | 7 |
# Multi-safe (supports an input appearing multiple times). Note that if there is |
8 | 8 |
# *any* mapping for an input in subtract_map, all mappings for that input in |
9 | 9 |
# from_map will be excluded. |
10 |
# Case- and punctuation-insensitive. |
|
10 | 11 |
|
11 | 12 |
import csv |
12 | 13 |
import operator |
... | ... | |
31 | 32 |
compare_col_nums = None # list_subset() value for all columns |
32 | 33 |
|
33 | 34 |
def compare_on(row): |
34 |
return tuple(util.list_subset(row, compare_col_nums, default=None)) |
|
35 |
return tuple(map(maps.simplify, util.list_subset(row, compare_col_nums, |
|
36 |
default=None))) |
|
35 | 37 |
|
36 | 38 |
headers = [None]*2 |
37 | 39 |
|
... | ... | |
44 | 46 |
headers[1] = reader.next() |
45 | 47 |
for row in reader: |
46 | 48 |
if row[0] != '': |
47 |
input_cols.add(row[0])
|
|
49 |
input_cols.add(maps.simplify(row[0]))
|
|
48 | 50 |
compare_cols.add(compare_on(row)) |
49 | 51 |
if reduce(operator.and_, (v == '' for v in row[1:])): # all empty |
50 | 52 |
map_[row[0]] = row[1] |
... | ... | |
65 | 67 |
writer.writerow(headers[0]) |
66 | 68 |
for row in reader: |
67 | 69 |
if not combinable or not ( |
68 |
(maps.is_nonexplicit_empty_mapping(row) and row[0] in input_cols) |
|
70 |
(maps.is_nonexplicit_empty_mapping(row) |
|
71 |
and maps.simplify(row[0]) in input_cols) |
|
69 | 72 |
or compare_on(row) in compare_cols |
70 | 73 |
or util.have_same_value(map_, row[0], row[1]) # map to same place |
71 | 74 |
): |
Also available in: Unified diff
subtract: Made it case- and punctuation-insensitive