<s> from functools import wraps <EOL> from operator import itemgetter <EOL> from collections import namedtuple <EOL> from contextlib import closing , contextmanager <EOL> from django . db import transaction , connections <EOL> __all__ = ( '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def commit_on_success_on ( * databases ) : <EOL> databases = set ( databases ) <EOL> def decorator ( func ) : <EOL> for db in databases : <EOL> func = transaction . commit_on_success ( db ) ( func ) <EOL> return wraps ( func ) ( func ) <EOL> return decorator <EOL> def queryset_iterator ( queryset , chunksize = <NUM_LIT:1000> ) : <EOL> pk = <NUM_LIT:0> <EOL> try : <EOL> last_pk = queryset . order_by ( '<STR_LIT>' ) [ <NUM_LIT:0> ] . pk <EOL> except IndexError : <EOL> raise StopIteration <EOL> queryset = queryset . order_by ( '<STR_LIT>' ) <EOL> while pk < last_pk : <EOL> for row in queryset . filter ( pk__gt = pk ) [ : chunksize ] . iterator ( ) : <EOL> pk = row . pk <EOL> yield row <EOL> def queryset_chunks ( queryset , chunksize = <NUM_LIT:1000> ) : <EOL> pk = <NUM_LIT:0> <EOL> while True : <EOL> chunk = list ( queryset . order_by ( '<STR_LIT>' ) . filter ( pk__gt = pk ) [ : chunksize ] ) <EOL> if chunk : <EOL> pk = chunk [ - <NUM_LIT:1> ] . pk <EOL> yield chunk <EOL> else : <EOL> break <EOL> def fetch_all ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as cursor : <EOL> return cursor . fetchall ( ) <EOL> def fetch_row ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as cursor : <EOL> return cursor . fetchone ( ) <EOL> def fetch_col ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> return [ row [ <NUM_LIT:0> ] for row in fetch_all ( sql , params , server ) ] <EOL> def fetch_val ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> return silent_first ( fetch_row ( sql , params , server ) ) <EOL> def do_sql ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as _ : <EOL> pass <EOL> @ contextmanager <EOL> def db_execute ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with closing ( connections [ server ] . cursor ( ) ) as cursor : <EOL> cursor . execute ( sql , params ) <EOL> yield cursor <EOL> def fetch_dicts ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as cursor : <EOL> field_names = _field_names ( cursor ) <EOL> return [ dict ( zip ( field_names , row ) ) for row in cursor . fetchall ( ) ] <EOL> def fetch_dict ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as cursor : <EOL> field_names = _field_names ( cursor ) <EOL> return dict ( zip ( field_names , cursor . fetchone ( ) ) ) <EOL> def fetch_named ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as cursor : <EOL> rec_class = _row_namedtuple ( cursor ) <EOL> return [ rec_class . _make ( row ) for row in cursor . fetchall ( ) ] <EOL> def fetch_named_row ( sql , params = ( ) , server = '<STR_LIT:default>' ) : <EOL> with db_execute ( sql , params , server ) as cursor : <EOL> rec_class = _row_namedtuple ( cursor ) <EOL> return rec_class . _make ( cursor . fetchone ( ) ) <EOL> def _row_namedtuple ( cursor ) : <EOL> return namedtuple ( '<STR_LIT>' , _field_names ( cursor ) ) <EOL> def _field_names ( cursor ) : <EOL> return [ name [ <NUM_LIT:0> ] for name in cursor . description ] <EOL> def silent_first ( seq ) : <EOL> try : <EOL> return seq [ <NUM_LIT:0> ] <EOL> except ( IndexError , TypeError ) : <EOL> return None </s>
