### Navigation

- index
- modules |
- CCHR 1.0 documentation »
- Module code »
- src »

# Source code for src.compare

    #python imports
    from os import path
    from datetime import datetime
    from itertools import izip_longest, izip
    from re import sub
    #third-party
    import cardsharp as cs
    #src
    from .log import Log
    from .configuration import config

    def _row_test(i, r1, r2, vars, log, segment):
        err = 'Row %i does not match after saving and loading' % i

        if segment != 'summary':
            region = 'cstate' if 'cstate' in vars else 'astate'

        for var in vars:
            if r1[var.name] != r2[var.name]:
                if var.name in ('aori_invalid', 'cori_invalid'):
                    continue

                if segment == 'summary': #if summary difference report
                    log.write_compare('diff', [int(r1['casenum']), var.name, str(r1[var.name]), str(r2[var.name])], 78)
                else:
                    log.write_compare('diff', [r1['id'], r1[region], r1['rapstatex'], var.name, r1[var.name], str(r2[var.name])], 77)

    def compare(ds1, ds2, **kw):
        '''
        ds1: one of two datasets to compare
        ds2: two of two datasets to compare
        out_format: the format for the output to log files,
            var = only output variable difference (default)
            row = output full row difference
        verbose: print output default = False
        '''
        log = Log(**kw)

        #validate same number of variables in both datasets
        if len(ds1.variables) != len(ds2.variables):
            print 'Number of variables do not match, unable to compare'

        #compare all rows of the datasets
        for i, (r1, r2) in enumerate(izip_longest(ds1, ds2)):
            assert r1 is not None and r2 is not None, "Datasets have unequal number of rows"

            _row_test(i, r1, r2, ds1.variables, log, kw['segment'])

### Navigation

- index
- modules |
- CCHR 1.0 documentation »
- Module code »
- src »