nodes: 0, module; 1, function_definition; 2, function_name:paginate; 3, parameters; 4, block; 5, identifier:self; 6, identifier:model_class; 7, identifier:order_by; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, default_parameter; 12, expression_statement; 13, expression_statement; 14, expression_statement; 15, if_statement; 16, expression_statement; 17, expression_statement; 18, if_statement; 19, expression_statement; 20, expression_statement; 21, expression_statement; 22, return_statement; 23, identifier:page_num; 24, integer:1; 25, identifier:page_size; 26, integer:100; 27, identifier:conditions; 28, None; 29, identifier:settings; 30, None; 31, string; 32, assignment; 33, assignment; 34, comparison_operator:page_num == -1; 35, block; 36, elif_clause; 37, assignment; 38, assignment; 39, identifier:conditions; 40, block; 41, augmented_assignment; 42, augmented_assignment; 43, assignment; 44, call; 45, string_content:Selects records and returns a single page of model instances.
  
          - `model_class`: the model class matching the query's table,
            or `None` for getting back instances of an ad-hoc model.
          - `order_by`: columns to use for sorting the query (contents of the ORDER BY clause).
          - `page_num`: the page number (1-based), or -1 to get the last page.
          - `page_size`: number of records to return per page.
          - `conditions`: optional SQL conditions (contents of the WHERE clause).
          - `settings`: query settings to send as HTTP GET parameters
  
          The result is a namedtuple containing `objects` (list), `number_of_objects`,
          `pages_total`, `number` (of the current page), and `page_size`.; 46, identifier:count; 47, call; 48, identifier:pages_total; 49, call; 50, identifier:page_num; 51, unary_operator; 52, expression_statement; 53, comparison_operator:page_num < 1; 54, block; 55, identifier:offset; 56, binary_operator:(page_num - 1) * page_size; 57, identifier:query; 58, string; 59, expression_statement; 60, identifier:query; 61, binary_operator:' ORDER BY %s' % order_by; 62, identifier:query; 63, binary_operator:' LIMIT %d, %d' % (offset, page_size); 64, identifier:query; 65, call; 66, identifier:Page; 67, argument_list; 68, attribute; 69, argument_list; 70, identifier:int; 71, argument_list; 72, integer:1; 73, assignment; 74, identifier:page_num; 75, integer:1; 76, raise_statement; 77, parenthesized_expression; 78, identifier:page_size; 79, string_content:SELECT * FROM $table; 80, augmented_assignment; 81, string; 82, identifier:order_by; 83, string; 84, tuple; 85, attribute; 86, argument_list; 87, keyword_argument; 88, keyword_argument; 89, keyword_argument; 90, keyword_argument; 91, keyword_argument; 92, identifier:self; 93, identifier:count; 94, identifier:model_class; 95, identifier:conditions; 96, call; 97, identifier:page_num; 98, call; 99, call; 100, binary_operator:page_num - 1; 101, identifier:query; 102, binary_operator:' WHERE ' + conditions; 103, string_content:ORDER BY %s; 104, string_content:LIMIT %d, %d; 105, identifier:offset; 106, identifier:page_size; 107, identifier:self; 108, identifier:_substitute; 109, identifier:query; 110, identifier:model_class; 111, identifier:objects; 112, conditional_expression:list(self.select(query, model_class, settings)) if count else []; 113, identifier:number_of_objects; 114, identifier:count; 115, identifier:pages_total; 116, identifier:pages_total; 117, identifier:number; 118, identifier:page_num; 119, identifier:page_size; 120, identifier:page_size; 121, identifier:ceil; 122, argument_list; 123, identifier:max; 124, argument_list; 125, identifier:ValueError; 126, argument_list; 127, identifier:page_num; 128, integer:1; 129, string; 130, identifier:conditions; 131, call; 132, identifier:count; 133, list; 134, binary_operator:count / float(page_size); 135, identifier:pages_total; 136, integer:1; 137, binary_operator:'Invalid page number: %d' % page_num; 138, string_content:WHERE; 139, identifier:list; 140, argument_list; 141, identifier:count; 142, call; 143, string; 144, identifier:page_num; 145, call; 146, identifier:float; 147, argument_list; 148, string_content:Invalid page number: %d; 149, attribute; 150, argument_list; 151, identifier:page_size; 152, identifier:self; 153, identifier:select; 154, identifier:query; 155, identifier:model_class; 156, identifier:settings
code: def paginate(self, model_class, order_by, page_num=1, page_size=100, conditions=None, settings=None):
          '''
          Selects records and returns a single page of model instances.
  
          - `model_class`: the model class matching the query's table,
            or `None` for getting back instances of an ad-hoc model.
          - `order_by`: columns to use for sorting the query (contents of the ORDER BY clause).
          - `page_num`: the page number (1-based), or -1 to get the last page.
          - `page_size`: number of records to return per page.
          - `conditions`: optional SQL conditions (contents of the WHERE clause).
          - `settings`: query settings to send as HTTP GET parameters
  
          The result is a namedtuple containing `objects` (list), `number_of_objects`,
          `pages_total`, `number` (of the current page), and `page_size`.
          '''
          count = self.count(model_class, conditions)
          pages_total = int(ceil(count / float(page_size)))
          if page_num == -1:
              page_num = max(pages_total, 1)
          elif page_num < 1:
              raise ValueError('Invalid page number: %d' % page_num)
          offset = (page_num - 1) * page_size
          query = 'SELECT * FROM $table'
          if conditions:
              query += ' WHERE ' + conditions
          query += ' ORDER BY %s' % order_by
          query += ' LIMIT %d, %d' % (offset, page_size)
          query = self._substitute(query, model_class)
          return Page(
              objects=list(self.select(query, model_class, settings)) if count else [],
              number_of_objects=count,
              pages_total=pages_total,
              number=page_num,
              page_size=page_size
          )