def _ToPb(self, limit=None, offset=None, count=None):

    if not self.__ancestor and _CurrentTransactionKey():
      raise datastore_errors.BadRequestError(
        'Only ancestor queries are allowed inside transactions.')

    pb = datastore_pb.Query()
    _MaybeSetupTransaction(pb, [self.__ancestor])

    if self.__kind is not None:
      pb.set_kind(self.__kind.encode('utf-8'))
    pb.set_keys_only(bool(self.__keys_only))
    if self.__app:
      pb.set_app(self.__app.encode('utf-8'))
    if self.__compile:
      pb.set_compile(True)
    if limit is not None:
      pb.set_limit(limit)
    if offset is not None:
      pb.set_offset(offset)
    if count is not None:
      pb.set_count(count)
    if self.__ancestor:
      pb.mutable_ancestor().CopyFrom(self.__ancestor._Key__reference)

    if ((self.__hint == self.ORDER_FIRST and self.__orderings) or
        (self.__hint == self.ANCESTOR_FIRST and self.__ancestor) or
        (self.__hint == self.FILTER_FIRST and len(self) > 0)):
      pb.set_hint(self.__hint)

    ordered_filters = [(i, f) for f, i in self.__filter_order.iteritems()]
    ordered_filters.sort()

    for i, filter_str in ordered_filters:
      if filter_str not in self:
        continue

      values = self[filter_str]
      match = self._CheckFilter(filter_str, values)
      name = match.group(1)

      props = datastore_types.ToPropertyPb(name, values)
      if not isinstance(props, list):
        props = [props]

      op = match.group(3)
      if op is None:
        op = '='

      for prop in props:
        filter = pb.add_filter()
        filter.set_op(self.OPERATORS[op])
        filter.add_property().CopyFrom(prop)

    for property, direction in self.__orderings:
      order = pb.add_order()
      order.set_property(property.encode('utf-8'))
      order.set_direction(direction)

    if self.__cursor:
      if (self.__cursor.has_primaryscan() and
          self.__cursor.primaryscan().has_start_key()):
        pb.set_start_key(self.__cursor.primaryscan().start_key())
        pb.set_start_inclusive(self.__cursor.primaryscan().start_inclusive())
      if self.__cursor.has_offset():
        pb.set_offset(pb.offset() + self.__cursor.offset())
      if self.__cursor.has_limit() and not pb.has_limit():
        pb.set_limit(self.__cursor.limit())

    return pb
Converts this Query to its protocol buffer representation. Not
dataset/ETHPy150Open CollabQ/CollabQ/.google_appengine/google/appengine/api/datastore.py/Query._ToPb