Project

General

Profile

« Previous | Next » 

Revision 1137

subtract: Support custom column numbers to compare on (instead of just input col). Added ignore option to continue even if input columns don't match.

View differences:

bin/subtract
2 2
# Subtracts map spreadsheet A1->C from A0->B to A->B
3 3

  
4 4
import csv
5
import os.path
5 6
import sys
6 7

  
8
sys.path.append(os.path.dirname(__file__)+"/../lib")
9

  
10
import opts
11
import util
12

  
7 13
def main():
8
    try: _prog_name, map_1_path = sys.argv
14
    ignore = opts.env_flag('ignore')
15
    try: _prog_name, map_1_path = sys.argv[:2]
9 16
    except ValueError:
10
        raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
11
            +' map_2]... >subtraction_map')
17
        raise SystemExit('Usage: env [ignore=1] '+sys.argv[0]+' <from_map '
18
            'subtract_map [compare_col_num...] [| '+sys.argv[0]
19
            +' subtract_map_2]... >difference_map')
20
    compare_col_nums = map(int, sys.argv[2:]) # 0-based
21
    if compare_col_nums == []: compare_col_nums = [0]
12 22
    
23
    def compare_on(row): return tuple(util.list_subset(row, compare_col_nums))
24
    
13 25
    # Get map 1
14
    map_1 = {}
26
    compare_cols = set()
15 27
    stream = open(map_1_path, 'rb')
16 28
    reader = csv.reader(stream)
17 29
    map_1_cols = reader.next()
18 30
    for row in reader:
19
        if row[0] != '': map_1[row[0]] = row
31
        if row[0] != '': compare_cols.add(compare_on(row))
20 32
    stream.close()
21 33
    
22 34
    # Open map 0
23 35
    reader = csv.reader(sys.stdin)
24 36
    map_0_cols = reader.next()
25
    if map_0_cols[0] != map_1_cols[0]: raise SystemExit('Map error: '
26
        'Map 1 column 0 name doesn\'t match map 0 column 0 name')
37
    if not ignore and map_0_cols[0] != map_1_cols[0]: raise SystemExit(
38
        'Map error: Map 1 column 0 name doesn\'t match map 0 column 0 name')
27 39
    
28 40
    # Subtract map 1 from map 0
29 41
    writer = csv.writer(sys.stdout)
30 42
    writer.writerow(map_0_cols)
31 43
    for row in reader:
32
        if row[0] not in map_1: writer.writerow(row)
44
        if compare_on(row) not in compare_cols: writer.writerow(row)
33 45

  
34 46
main()

Also available in: Unified diff