<s> import datetime <EOL> from time import time <EOL> class CursorDebugWrapper ( object ) : <EOL> def __init__ ( self , cursor , db ) : <EOL> self . cursor = cursor <EOL> self . db = db <EOL> def execute ( self , sql , params = ( ) ) : <EOL> start = time ( ) <EOL> try : <EOL> return self . cursor . execute ( sql , params ) <EOL> finally : <EOL> stop = time ( ) <EOL> if not isinstance ( params , ( tuple , dict ) ) : <EOL> params = tuple ( params ) <EOL> self . db . queries . append ( { <EOL> '<STR_LIT>' : sql % params , <EOL> '<STR_LIT:time>' : "<STR_LIT>" % ( stop - start ) , <EOL> } ) <EOL> def executemany ( self , sql , param_list ) : <EOL> start = time ( ) <EOL> try : <EOL> return self . cursor . executemany ( sql , param_list ) <EOL> finally : <EOL> stop = time ( ) <EOL> self . db . queries . append ( { <EOL> '<STR_LIT>' : '<STR_LIT>' + sql + '<STR_LIT:U+0020>' + str ( tuple ( param_list ) ) , <EOL> '<STR_LIT:time>' : "<STR_LIT>" % ( stop - start ) , <EOL> } ) <EOL> def __getattr__ ( self , attr ) : <EOL> if self . __dict__ . has_key ( attr ) : <EOL> return self . __dict__ [ attr ] <EOL> else : <EOL> return getattr ( self . cursor , attr ) <EOL> def typecast_date ( s ) : <EOL> return s and datetime . date ( * map ( int , s . split ( '<STR_LIT:->' ) ) ) or None <EOL> def typecast_time ( s ) : <EOL> if not s : return None <EOL> hour , minutes , seconds = s . split ( '<STR_LIT::>' ) <EOL> if '<STR_LIT:.>' in seconds : <EOL> seconds , microseconds = seconds . split ( '<STR_LIT:.>' ) <EOL> else : <EOL> microseconds = '<STR_LIT:0>' <EOL> return datetime . time ( int ( hour ) , int ( minutes ) , int ( seconds ) , int ( float ( '<STR_LIT:.>' + microseconds ) * <NUM_LIT> ) ) <EOL> def typecast_timestamp ( s ) : <EOL> if not s : return None <EOL> if not '<STR_LIT:U+0020>' in s : return typecast_date ( s ) <EOL> d , t = s . split ( ) <EOL> if '<STR_LIT:->' in t : <EOL> t , tz = t . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> tz = '<STR_LIT:->' + tz <EOL> elif '<STR_LIT:+>' in t : <EOL> t , tz = t . split ( '<STR_LIT:+>' , <NUM_LIT:1> ) <EOL> tz = '<STR_LIT:+>' + tz <EOL> else : <EOL> tz = '<STR_LIT>' <EOL> dates = d . split ( '<STR_LIT:->' ) <EOL> times = t . split ( '<STR_LIT::>' ) <EOL> seconds = times [ <NUM_LIT:2> ] <EOL> if '<STR_LIT:.>' in seconds : <EOL> seconds , microseconds = seconds . split ( '<STR_LIT:.>' ) <EOL> else : <EOL> microseconds = '<STR_LIT:0>' <EOL> return datetime . datetime ( int ( dates [ <NUM_LIT:0> ] ) , int ( dates [ <NUM_LIT:1> ] ) , int ( dates [ <NUM_LIT:2> ] ) , <EOL> int ( times [ <NUM_LIT:0> ] ) , int ( times [ <NUM_LIT:1> ] ) , int ( seconds ) , int ( float ( '<STR_LIT:.>' + microseconds ) * <NUM_LIT> ) ) <EOL> def typecast_boolean ( s ) : <EOL> if s is None : return None <EOL> if not s : return False <EOL> return str ( s ) [ <NUM_LIT:0> ] . lower ( ) == '<STR_LIT:t>' <EOL> def rev_typecast_boolean ( obj , d ) : <EOL> return obj and '<STR_LIT:1>' or '<STR_LIT:0>' <EOL> def _dict_helper ( desc , row ) : <EOL> "<STR_LIT>" <EOL> return dict ( zip ( [ col [ <NUM_LIT:0> ] for col in desc ] , row ) ) <EOL> def dictfetchone ( cursor ) : <EOL> "<STR_LIT>" <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> return None <EOL> return _dict_helper ( cursor . description , row ) <EOL> def dictfetchmany ( cursor , number ) : <EOL> "<STR_LIT>" <EOL> desc = cursor . description <EOL> for row in cursor . fetchmany ( number ) : <EOL> yield _dict_helper ( desc , row ) <EOL> def dictfetchall ( cursor ) : <EOL> "<STR_LIT>" <EOL> desc = cursor . description <EOL> for row in cursor . fetchall ( ) : <EOL> yield _dict_helper ( desc , row ) </s>
