<s> import re <EOL> try : <EOL> import unittest2 as unittest <EOL> except ImportError : <EOL> import unittest <EOL> from csvkit . grep import FilteringCSVReader <EOL> from csvkit . exceptions import ColumnIdentifierError <EOL> class TestGrep ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . tab1 = [ <EOL> [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] , <EOL> [ u'<STR_LIT:1>' , u'<STR_LIT>' , u'<STR_LIT>' ] , <EOL> [ u'<STR_LIT:2>' , u'<STR_LIT>' , u'<STR_LIT>' ] , <EOL> [ u'<STR_LIT:3>' , u'<STR_LIT>' , u'<STR_LIT>' ] , <EOL> [ u'<STR_LIT:1>' , u'<STR_LIT>' , u'<STR_LIT>' ] ] <EOL> self . tab2 = [ <EOL> [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ u'<STR_LIT:1>' , u'<STR_LIT>' , u'<STR_LIT:0>' ] , <EOL> [ u'<STR_LIT:4>' , u'<STR_LIT>' , u'<STR_LIT:0>' ] , <EOL> [ u'<STR_LIT:1>' , u'<STR_LIT>' , u'<STR_LIT:0>' ] , <EOL> [ u'<STR_LIT:2>' , u'<STR_LIT>' , u'<STR_LIT:0>' , u'<STR_LIT:0>' ] ] <EOL> def test_pattern ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab1 ) , patterns = [ '<STR_LIT:1>' ] ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:1> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:4> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_no_header ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab1 ) , patterns = { <NUM_LIT:2> : '<STR_LIT>' } , header = False ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:2> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:3> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_regex ( self ) : <EOL> pattern = re . compile ( "<STR_LIT>" ) <EOL> fcr = FilteringCSVReader ( iter ( self . tab1 ) , patterns = { <NUM_LIT:1> : pattern } ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:1> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:3> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab1 [ <NUM_LIT:4> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_inverse ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab2 ) , patterns = [ '<STR_LIT:1>' ] , inverse = True ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:2> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:4> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_column_names_in_patterns ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab2 ) , patterns = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:2> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:4> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_mixed_indices_and_column_names_in_patterns ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab2 ) , patterns = { '<STR_LIT>' : '<STR_LIT>' , <NUM_LIT:0> : '<STR_LIT:2>' } ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:4> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_duplicate_column_ids_in_patterns ( self ) : <EOL> try : <EOL> FilteringCSVReader ( iter ( self . tab2 ) , patterns = { '<STR_LIT>' : '<STR_LIT>' , <NUM_LIT:1> : '<STR_LIT>' } ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except ColumnIdentifierError : <EOL> pass <EOL> def test_any_match ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab2 ) , patterns = { '<STR_LIT>' : '<STR_LIT>' , <NUM_LIT:0> : '<STR_LIT:2>' } , any_match = True ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:2> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:4> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_any_match_and_inverse ( self ) : <EOL> fcr = FilteringCSVReader ( iter ( self . tab2 ) , patterns = { '<STR_LIT>' : '<STR_LIT>' , <NUM_LIT:0> : '<STR_LIT:2>' } , any_match = True , inverse = True ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:1> ] , next ( fcr ) ) <EOL> self . assertEqual ( self . tab2 [ <NUM_LIT:3> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass <EOL> def test_multiline ( self ) : <EOL> table = [ <EOL> [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , <EOL> [ '<STR_LIT:1>' , '<STR_LIT>' ] <EOL> ] <EOL> fcr = FilteringCSVReader ( iter ( table ) , patterns = { '<STR_LIT:b>' : re . compile ( '<STR_LIT:bar>' ) } ) <EOL> self . assertEqual ( table [ <NUM_LIT:0> ] , next ( fcr ) ) <EOL> self . assertEqual ( table [ <NUM_LIT:1> ] , next ( fcr ) ) <EOL> try : <EOL> next ( fcr ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> except StopIteration : <EOL> pass </s>
