<commit_before>"""
Convenience wrapper for the functional heapq library.
"""
import heapq


# TODO: add a __contains__ method
class heap(object):
    '''A tree-based data structure that satisfies the heap property.

    A heap can be used as priority queue by pushing tuples onto the heap.

    >>> import trees
    >>> h = trees.heap()
    >>> h.push(1)
    >>> h.push(3)
    >>> h.push(2)
    >>> h.top
    1
    >>> h.pop()
    1
    >>> h.top
    2
    >>> h.pop()
    2
    >>> h.pop()
    3
    >>> h.empty
    True
    '''
    def __init__(self, items=None):
        if items is None:
            items = []
        self._items = list(items)
        heapq.heapify(self._items)

    @property
    def top(self):
        return self._items[0]

    @property
    def empty(self):
        return len(self) == 0

    def pop(self):
        '''Pop and return the smallest item from the heap, maintaining the heap
        invariant. If the heap is empty, IndexError is raised.
        '''
        try:
            return heapq.heappop(self._items)
        except IndexError as e:
            raise e

    def push(self, item):
        '''Push the value item onto the heap, maintaining the heap invariant.
        If the item is not hashable, a TypeError is raised.
        '''
        try:
            hash(item)
        except TypeError as e:
            raise e
        else:
            heapq.heappush(self._items, item)

    def __len__(self):
        return len(self._items)

    def __iter__(self):
        while len(self) > 0:
            yield self.pop()


if __name__ == "__main__":
    import doctest
    doctest.testmod()<commit_msg>That's not how exceptions work<commit_after>"""
Convenience wrapper for the functional heapq library.
"""
import heapq


# TODO: add a __contains__ method
class heap(object):
    '''A tree-based data structure that satisfies the heap property.

    A heap can be used as priority queue by pushing tuples onto the heap.

    >>> import trees
    >>> h = trees.heap()
    >>> h.push(1)
    >>> h.push(3)
    >>> h.push(2)
    >>> h.top
    1
    >>> h.pop()
    1
    >>> h.top
    2
    >>> h.pop()
    2
    >>> h.pop()
    3
    >>> h.empty
    True
    '''
    def __init__(self, items=None):
        if items is None:
            items = []
        self._items = list(items)
        heapq.heapify(self._items)

    @property
    def top(self):
        return self._items[0]

    @property
    def empty(self):
        return len(self) == 0

    def pop(self):
        '''Pop and return the smallest item from the heap, maintaining the heap
        invariant. If the heap is empty, IndexError is raised.
        '''
        
        return heapq.heappop(self._items)

    def push(self, item):
        '''Push the value item onto the heap, maintaining the heap invariant.
        If the item is not hashable, a TypeError is raised.
        '''
        hash(item)
        heapq.heappush(self._items, item)

    def __len__(self):
        return len(self._items)

    def __iter__(self):
        while len(self) > 0:
            yield self.pop()


if __name__ == "__main__":
    import doctest
    doctest.testmod()
{'fraction_non_alphanumeric': 0.1133270022158911, 'fraction_numerical': 0.006964229186451409, 'mean_word_length': 1.917820867959372, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3570514', 'n_tokens_mistral': 973, 'n_tokens_neox': 947, 'n_words': 318}