<s> from django . db . models . manager import Manager <EOL> from django . db . models . query import QuerySet <EOL> from django . db . models . fields import AutoField , IntegerField <EOL> from django . db . models import Min , Max <EOL> from dbutils . helpers import attach_foreignkey <EOL> class QuerySetDoubleIteration ( Exception ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class SkinnyManager ( Manager ) : <EOL> def get_query_set ( self ) : <EOL> return SkinnyQuerySet ( self . model , using = self . _db ) <EOL> class SkinnyQuerySet ( QuerySet ) : <EOL> """<STR_LIT>""" <EOL> def __len__ ( self ) : <EOL> if getattr ( self , '<STR_LIT>' , False ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> return super ( SkinnyQuerySet , self ) . __len__ ( ) <EOL> def __iter__ ( self ) : <EOL> if self . _result_cache is not None : <EOL> return iter ( self . _result_cache ) <EOL> has_run_before = getattr ( self , '<STR_LIT>' , False ) <EOL> if has_run_before : <EOL> raise QuerySetDoubleIteration ( "<STR_LIT>" ) <EOL> self . has_run_before = True <EOL> return self . iterator ( ) <EOL> def list ( self ) : <EOL> return list ( self ) <EOL> class InvalidQuerySetError ( ValueError ) : <EOL> pass <EOL> class IterableQuerySetWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queryset , step = <NUM_LIT> , limit = None ) : <EOL> self . limit = limit <EOL> if limit : <EOL> self . step = min ( limit , step ) <EOL> else : <EOL> self . step = step <EOL> self . queryset = queryset <EOL> def __iter__ ( self ) : <EOL> at = <NUM_LIT:0> <EOL> results = list ( self . queryset [ at : ( at + self . step ) ] ) <EOL> while results and ( not self . limit or at < self . limit ) : <EOL> for result in results : <EOL> yield result <EOL> at += self . step <EOL> results = list ( self . queryset [ at : ( at + self . step ) ] ) <EOL> def iterator ( self ) : <EOL> return iter ( self ) <EOL> class RangeQuerySet ( SkinnyQuerySet ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model , step = <NUM_LIT> , sorted = False , * args , ** kwargs ) : <EOL> super ( SkinnyQuerySet , self ) . __init__ ( model , * args , ** kwargs ) <EOL> self . step = step <EOL> self . sorted = sorted <EOL> def iterator ( self , bypass = False ) : <EOL> if not bypass and self . query . low_mark == <NUM_LIT:0> and not ( self . query . order_by or self . query . extra_order_by ) : <EOL> high_mark = self . query . high_mark <EOL> self . query . clear_limits ( ) <EOL> results = RangeQuerySetWrapper ( self , step = self . step , limit = high_mark , sorted = self . sorted ) <EOL> elif not bypass : <EOL> results = IterableQuerySetWrapper ( self , step = self . step ) <EOL> else : <EOL> results = super ( RangeQuerySet , self ) . iterator ( ) <EOL> for result in results : <EOL> yield result <EOL> class RangeQuerySetWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queryset , step = <NUM_LIT:1000> , limit = None , min_id = None , max_id = None , sorted = True , <EOL> select_related = [ ] , callbacks = [ ] ) : <EOL> if queryset . query . low_mark == <NUM_LIT:0> and not ( queryset . query . order_by or queryset . query . extra_order_by ) : <EOL> if limit is None : <EOL> limit = queryset . query . high_mark <EOL> queryset . query . clear_limits ( ) <EOL> else : <EOL> raise InvalidQuerySetError <EOL> self . limit = limit <EOL> if limit : <EOL> self . step = min ( limit , abs ( step ) ) <EOL> self . desc = step < <NUM_LIT:0> <EOL> else : <EOL> self . step = abs ( step ) <EOL> self . desc = step < <NUM_LIT:0> <EOL> self . queryset = queryset <EOL> self . min_id , self . max_id = min_id , max_id <EOL> self . sorted = sorted or not max_id <EOL> self . select_related = select_related <EOL> self . callbacks = callbacks <EOL> def __iter__ ( self ) : <EOL> pk = self . queryset . model . _meta . pk <EOL> if not isinstance ( pk , ( IntegerField , AutoField ) ) : <EOL> for result in iter ( IterableQuerySetWrapper ( self . queryset , self . step , self . limit ) ) : <EOL> yield result <EOL> else : <EOL> max_id = self . max_id <EOL> if self . min_id is not None : <EOL> at = self . min_id <EOL> elif not self . sorted : <EOL> at = <NUM_LIT:0> <EOL> else : <EOL> at = None <EOL> num = <NUM_LIT:0> <EOL> limit = self . limit or max_id <EOL> if isinstance ( self . queryset , RangeQuerySet ) : <EOL> extra_kwargs = { '<STR_LIT>' : True } <EOL> else : <EOL> extra_kwargs = { } <EOL> has_results = True <EOL> while ( ( max_id and at <= max_id ) or has_results ) and ( not self . limit or num < self . limit ) : <EOL> start = num <EOL> if at is None : <EOL> results = self . queryset <EOL> elif self . desc : <EOL> results = self . queryset . filter ( id__lte = at ) <EOL> elif not self . desc : <EOL> results = self . queryset . filter ( id__gte = at ) <EOL> if self . sorted : <EOL> if self . desc : <EOL> results = results . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> results = results . order_by ( '<STR_LIT:id>' ) <EOL> if self . max_id : <EOL> results = results . filter ( id__lte = max_id ) <EOL> results = results [ : self . step ] . iterator ( ** extra_kwargs ) <EOL> if self . select_related : <EOL> results = list ( results ) <EOL> for fkey in self . select_related : <EOL> if '<STR_LIT>' in fkey : <EOL> fkey , related = fkey . split ( '<STR_LIT>' ) <EOL> else : <EOL> related = [ ] <EOL> attach_foreignkey ( results , getattr ( self . queryset . model , fkey , related ) ) <EOL> if self . callbacks : <EOL> results = list ( results ) <EOL> for callback in self . callbacks : <EOL> callback ( results ) <EOL> for result in results : <EOL> yield result <EOL> num += <NUM_LIT:1> <EOL> at = result . id <EOL> if ( max_id and result . id >= max_id ) or ( limit and num >= limit ) : <EOL> break <EOL> if at is None : <EOL> break <EOL> has_results = num > start <EOL> if self . desc : <EOL> at -= <NUM_LIT:1> <EOL> else : <EOL> at += <NUM_LIT:1> </s>
