<s> def get_where_clause_for_images ( keys , is_sorted = False ) : <EOL> '''<STR_LIT>''' <EOL> def in_sequence ( k1 , k2 ) : <EOL> if len ( k1 ) > <NUM_LIT:1> : <EOL> if k1 [ : - <NUM_LIT:1> ] != k2 [ : - <NUM_LIT:1> ] : <EOL> return False <EOL> return k1 [ - <NUM_LIT:1> ] == ( k2 [ - <NUM_LIT:1> ] - <NUM_LIT:1> ) <EOL> def optimize_for_query ( keys , is_sorted = False ) : <EOL> if not is_sorted : <EOL> keys . sort ( ) <EOL> groups = [ ] <EOL> in_run = False <EOL> for i in xrange ( len ( keys ) ) : <EOL> if i == len ( keys ) - <NUM_LIT:1> : <EOL> if in_run : <EOL> groups [ - <NUM_LIT:1> ] += [ keys [ i ] ] <EOL> else : <EOL> groups += [ [ keys [ i ] ] ] <EOL> break <EOL> if in_run : <EOL> if in_sequence ( keys [ i ] , keys [ i + <NUM_LIT:1> ] ) : <EOL> continue <EOL> else : <EOL> groups [ - <NUM_LIT:1> ] += [ keys [ i ] ] <EOL> in_run = False <EOL> else : <EOL> if in_sequence ( keys [ i ] , keys [ i + <NUM_LIT:1> ] ) : <EOL> in_run = True <EOL> groups += [ [ keys [ i ] ] ] <EOL> return groups <EOL> groups = optimize_for_query ( keys ) <EOL> wheres = [ ] <EOL> for k in groups : <EOL> if len ( k ) == <NUM_LIT:1> : <EOL> wheres += [ '<STR_LIT>' % ( col , value ) for col , value in zip ( object_key_columns ( ) , k [ <NUM_LIT:0> ] ) ] <EOL> else : <EOL> first , last = k <EOL> if p . table_id : <EOL> wheres += [ '<STR_LIT>' % <EOL> ( p . table_id , first [ <NUM_LIT:0> ] , p . image_id , first [ <NUM_LIT:1> ] , last [ <NUM_LIT:1> ] ) ] <EOL> else : <EOL> wheres += [ '<STR_LIT>' % <EOL> ( p . image_id , first [ <NUM_LIT:0> ] , last [ <NUM_LIT:0> ] ) ] <EOL> return '<STR_LIT>' . join ( wheres ) </s>
