<s> import sqlite3 <EOL> import itertools <EOL> class Connection ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , isolation_level = None ) : <EOL> self . filename = filename <EOL> self . isolation_level = isolation_level <EOL> self . _db = None <EOL> try : <EOL> self . reconnect ( ) <EOL> except : <EOL> raise <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . _db . close ( ) <EOL> self . _db = None <EOL> def reconnect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . close ( ) <EOL> self . _db = sqlite3 . connect ( self . filename ) <EOL> self . _db . isolation_level = self . isolation_level <EOL> def _cursor ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _db is None : <EOL> self . reconnect ( ) <EOL> return self . _db . cursor ( ) <EOL> def __del__ ( self ) : <EOL> self . close ( ) <EOL> def execute ( self , query , * parameters ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _cursor ( ) <EOL> try : <EOL> self . _execute ( cursor , query , parameters ) <EOL> return cursor . lastrowid <EOL> finally : <EOL> pass <EOL> def executemany ( self , query , parameters ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _cursor ( ) <EOL> try : <EOL> cursor . executemany ( query , parameters ) <EOL> return cursor . lastrowid <EOL> finally : <EOL> cursor . close ( ) <EOL> def _execute ( self , cursor , query , parameters ) : <EOL> try : <EOL> return cursor . execute ( query , parameters ) <EOL> except OperationalError : <EOL> self . close ( ) <EOL> raise <EOL> def query ( self , query , * parameters ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _cursor ( ) <EOL> try : <EOL> self . _execute ( cursor , query , parameters ) <EOL> column_names = [ d [ <NUM_LIT:0> ] for d in cursor . description ] <EOL> return [ Row ( itertools . izip ( column_names , row ) ) for row in cursor ] <EOL> finally : <EOL> pass <EOL> def get ( self , query , * parameters ) : <EOL> """<STR_LIT>""" <EOL> rows = self . query ( query , * parameters ) <EOL> if not rows : <EOL> return None <EOL> elif len ( rows ) > <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> return rows [ <NUM_LIT:0> ] <EOL> class Row ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __getattr__ ( self , name ) : <EOL> try : <EOL> return self [ name ] <EOL> except KeyError : <EOL> raise AttributeError ( name ) <EOL> OperationalError = sqlite3 . OperationalError </s>
