code: def sample(self, cursor):
          
          count = cursor.count()
  
          # special case: empty collection
          if count == 0:
              self._empty = True
              raise ValueError("Empty collection")
  
          # special case: entire collection
          if self.p >= 1 and self.max_items <= 0:
              for item in cursor:
                  yield item
              return
  
          # calculate target number of items to select
          if self.max_items <= 0:
              n_target = max(self.min_items, self.p * count)
          else:
              if self.p <= 0:
                  n_target = max(self.min_items, self.max_items)
              else:
                  n_target = max(self.min_items, min(self.max_items, self.p * count))
          if n_target == 0:
              raise ValueError("No items requested")
  
          # select first `n_target` items that pop up with
          # probability self.p
          # This is actually biased to items at the beginning
          # of the file if n_target is smaller than (p * count),
          n = 0
          while n < n_target:
              try:
                  item = next(cursor)
              except StopIteration:
                  # need to keep looping through data until
                  # we get all our items!
                  cursor.rewind()
                  item = next(cursor)
              if self._keep():
                  yield item
                  n += 1
docs: Extract records randomly from the database.
          Continue until the target proportion of the items have been
          extracted, or until `min_items` if this is larger.
          If `max_items` is non-negative, do not extract more than these.
  
          This function is a generator, yielding items incrementally.
  
          :param cursor: Cursor to sample
          :type cursor: pymongo.cursor.Cursor
          :return: yields each item
          :rtype: dict
          :raise: ValueError, if max_items is valid and less than `min_items`
                  or if target collection is empty