<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def selectOptimalChunk(self, peer):
 """ select an optimal chunk to send to a peer. @return: int(chunkNumber), str(chunkData) if there is data to be sent, otherwise None, None """


        # stuff I have
        have = sets.Set(self.mask.positions(1))
        # stuff that this peer wants
        want = sets.Set(self.peers[peer].mask.positions(0))
        exchangeable = have.intersection(want)
        finalSet = dict.fromkeys(exchangeable, 0)

        # taking a page from bittorrent, rarest-first
        for chunkNumber in exchangeable:
            for otherPeer in self.peers.itervalues():
                finalSet[chunkNumber] += not otherPeer.mask[chunkNumber]
        rarityList = [(rarity, random.random(), chunkNumber)
                      for (chunkNumber, rarity)
                      in finalSet.iteritems()]
        if not rarityList:
            return None, None
        rarityList.sort()
        chunkNumber = rarityList[-1][-1] # sorted in ascending order of rarity

        # sanity check
        assert self.mask[chunkNumber], "I wanted to send a chunk I didn't have"

        self.file.seek(chunkNumber * CHUNK_SIZE)
        chunkData = self.file.read(CHUNK_SIZE)
        self.sha1sums[chunkNumber] = sha.new(chunkData).digest()
        return chunkNumber, chunkData