<s> from __future__ import absolute_import , print_function , division <EOL> import operator <EOL> from petl . compat import text_type <EOL> from petl . util . base import Table , asindices , Record <EOL> def validate ( table , constraints = None , header = None ) : <EOL> """<STR_LIT>""" <EOL> return ProblemsView ( table , constraints = constraints , header = header ) <EOL> Table . validate = validate <EOL> class ProblemsView ( Table ) : <EOL> def __init__ ( self , table , constraints , header ) : <EOL> self . table = table <EOL> self . constraints = constraints <EOL> self . header = header <EOL> def __iter__ ( self ) : <EOL> return iterproblems ( self . table , self . constraints , self . header ) <EOL> def iterproblems ( table , constraints , expected_header ) : <EOL> outhdr = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:value>' , '<STR_LIT:error>' ) <EOL> yield outhdr <EOL> it = iter ( table ) <EOL> actual_header = next ( it ) <EOL> if expected_header is None : <EOL> flds = list ( map ( text_type , actual_header ) ) <EOL> else : <EOL> expected_flds = list ( map ( text_type , expected_header ) ) <EOL> actual_flds = list ( map ( text_type , actual_header ) ) <EOL> try : <EOL> assert expected_flds == actual_flds <EOL> except Exception as e : <EOL> yield ( '<STR_LIT>' , <NUM_LIT:0> , None , None , type ( e ) . __name__ ) <EOL> flds = expected_flds <EOL> if constraints : <EOL> constraints = [ dict ( ** c ) for c in constraints ] <EOL> for constraint in constraints : <EOL> if '<STR_LIT>' not in constraint : <EOL> if '<STR_LIT>' in constraint : <EOL> indices = asindices ( flds , constraint [ '<STR_LIT>' ] ) <EOL> getter = operator . itemgetter ( * indices ) <EOL> constraint [ '<STR_LIT>' ] = getter <EOL> expected_len = len ( flds ) <EOL> for i , row in enumerate ( it ) : <EOL> row = tuple ( row ) <EOL> l = None <EOL> try : <EOL> l = len ( row ) <EOL> assert l == expected_len <EOL> except Exception as e : <EOL> yield ( '<STR_LIT>' , i + <NUM_LIT:1> , None , l , type ( e ) . __name__ ) <EOL> if constraints : <EOL> row = Record ( row , flds ) <EOL> for constraint in constraints : <EOL> name = constraint . get ( '<STR_LIT:name>' , None ) <EOL> field = constraint . get ( '<STR_LIT>' , None ) <EOL> assertion = constraint . get ( '<STR_LIT>' , None ) <EOL> test = constraint . get ( '<STR_LIT:test>' , None ) <EOL> getter = constraint . get ( '<STR_LIT>' , lambda x : x ) <EOL> try : <EOL> target = getter ( row ) <EOL> except Exception as e : <EOL> yield ( name , i + <NUM_LIT:1> , field , None , type ( e ) . __name__ ) <EOL> else : <EOL> value = target if field else None <EOL> if test is not None : <EOL> try : <EOL> test ( target ) <EOL> except Exception as e : <EOL> yield ( name , i + <NUM_LIT:1> , field , value , type ( e ) . __name__ ) <EOL> if assertion is not None : <EOL> try : <EOL> assert assertion ( target ) <EOL> except Exception as e : <EOL> yield ( name , i + <NUM_LIT:1> , field , value , type ( e ) . __name__ ) </s>
