<s> from __future__ import absolute_import , print_function , division <EOL> import datetime <EOL> from petl . compat import long <EOL> def datetimeparser ( fmt , strict = True ) : <EOL> """<STR_LIT>""" <EOL> def parser ( value ) : <EOL> try : <EOL> return datetime . datetime . strptime ( value . strip ( ) , fmt ) <EOL> except Exception as e : <EOL> if strict : <EOL> raise e <EOL> else : <EOL> return value <EOL> return parser <EOL> def dateparser ( fmt , strict = True ) : <EOL> """<STR_LIT>""" <EOL> def parser ( value ) : <EOL> try : <EOL> return datetime . datetime . strptime ( value . strip ( ) , fmt ) . date ( ) <EOL> except Exception as e : <EOL> if strict : <EOL> raise e <EOL> else : <EOL> return value <EOL> return parser <EOL> def timeparser ( fmt , strict = True ) : <EOL> """<STR_LIT>""" <EOL> def parser ( value ) : <EOL> try : <EOL> return datetime . datetime . strptime ( value . strip ( ) , fmt ) . time ( ) <EOL> except Exception as e : <EOL> if strict : <EOL> raise e <EOL> else : <EOL> return value <EOL> return parser <EOL> def boolparser ( true_strings = ( '<STR_LIT:true>' , '<STR_LIT:t>' , '<STR_LIT:yes>' , '<STR_LIT:y>' , '<STR_LIT:1>' ) , <EOL> false_strings = ( '<STR_LIT:false>' , '<STR_LIT:f>' , '<STR_LIT>' , '<STR_LIT:n>' , '<STR_LIT:0>' ) , <EOL> case_sensitive = False , <EOL> strict = True ) : <EOL> """<STR_LIT>""" <EOL> if not case_sensitive : <EOL> true_strings = [ s . lower ( ) for s in true_strings ] <EOL> false_strings = [ s . lower ( ) for s in false_strings ] <EOL> def parser ( value ) : <EOL> value = value . strip ( ) <EOL> if not case_sensitive : <EOL> value = value . lower ( ) <EOL> if value in true_strings : <EOL> return True <EOL> elif value in false_strings : <EOL> return False <EOL> elif strict : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % value ) <EOL> else : <EOL> return value <EOL> return parser <EOL> def numparser ( strict = False ) : <EOL> """<STR_LIT>""" <EOL> def f ( v ) : <EOL> try : <EOL> return int ( v ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> try : <EOL> return long ( v ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> try : <EOL> return float ( v ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> try : <EOL> return complex ( v ) <EOL> except ( ValueError , TypeError ) as e : <EOL> if strict : <EOL> raise e <EOL> return v <EOL> return f </s>
