<s> """<STR_LIT>""" <EOL> import csv <EOL> import time <EOL> try : <EOL> from . . pylib import range_lib <EOL> except ( ImportError , ValueError ) : <EOL> from pylib import range_lib <EOL> class CsvWriter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename ) : <EOL> self . _filename = filename <EOL> self . _writer = None <EOL> def WriteRows ( self , rows ) : <EOL> listkeys = rows [ <NUM_LIT:0> ] . keys ( ) <EOL> if not self . _writer : <EOL> self . _writer = csv . DictWriter ( open ( self . _filename , '<STR_LIT:wb>' ) , listkeys ) <EOL> self . _writer . writerows ( rows ) <EOL> writer_method = { '<STR_LIT>' : CsvWriter } <EOL> class Willie ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dbh , database_name , table , condition , limit , <EOL> utilization_percent , dry_run , writer_type , filename ) : <EOL> """<STR_LIT>""" <EOL> self . _dbh = dbh <EOL> self . _database_name = database_name <EOL> self . _table = table <EOL> self . _condition = condition <EOL> self . _limit = limit <EOL> self . _utilization_percent = utilization_percent <EOL> self . _dry_run = dry_run <EOL> self . _writer_type = writer_type <EOL> self . _filename = filename <EOL> if not dry_run and writer_type : <EOL> self . _writer = writer_method [ writer_type ] ( filename ) <EOL> else : <EOL> self . _writer = None <EOL> def GetRange ( self ) : <EOL> """<STR_LIT>""" <EOL> rangeh = range_lib . PrimaryKeyRange ( self . _dbh , self . _database_name , <EOL> self . _table ) <EOL> first_key = rangeh . GetFirstPrimaryKeyValue ( self . _condition ) <EOL> nth_key = rangeh . GetNthPrimaryKeyValue ( self . _limit , first_key ) <EOL> if self . _writer : <EOL> rows = rangeh . RangePrimaryKeyValues ( first_key , nth_key , '<STR_LIT:*>' ) <EOL> self . _writer . WriteRows ( rows ) <EOL> return rangeh . GenerateRangeWhere ( first_key , nth_key ) <EOL> def PerformDelete ( self , where_clause ) : <EOL> """<STR_LIT>""" <EOL> start_time = time . time ( ) <EOL> delete_statement = ( '<STR_LIT>' <EOL> % ( self . _database_name , self . _table , where_clause ) ) <EOL> print '<STR_LIT>' % delete_statement <EOL> if not self . _dry_run : <EOL> self . _dbh . ExecuteOrDie ( delete_statement ) <EOL> end_time = time . time ( ) <EOL> delta_time = end_time - start_time <EOL> delay_time = delta_time * ( <NUM_LIT> / self . _utilization_percent ) - delta_time <EOL> print '<STR_LIT>' % delay_time <EOL> time . sleep ( delay_time ) <EOL> def Loop ( self ) : <EOL> while True : <EOL> where_clause = self . GetRange ( ) <EOL> if where_clause : <EOL> self . PerformDelete ( where_clause ) <EOL> if self . _dry_run : <EOL> return <EOL> else : <EOL> return </s>
