<commit_before>import binascii


class Memory(object):
    """Generic memory abstaction."""

    def __init__(self, size):
        self.memory = bytearray([0] * size)

    def read(self, addr, count):
        if addr < 0 or addr + count > self.size():
            raise Exception('read outside memory bounds')
        elif count < 0:
            raise Exception('negative memory read')
        return str(self.memory[addr:addr+count])

    def write(self, addr, buffer):
        if addr < 0 or addr + len(buffer) > self.size():
            raise Exception('write outside memory bounds')
        elif len(buffer) == 0:
            raise Exception('empty buffer to write')
        self.memory[addr:addr+len(buffer)] = buffer

    def size(self):
        return len(self.memory)

    def dump(self):
        for i in range(self.size() - 1, 0, -8):
            word = self.memory[i:i-4:-1]
            word2 = self.memory[i-4:i-8:-1]
            print '{:04x}  {} {}  {} {}'.format(i, binascii.hexlify(word),
                                                binascii.hexlify(word2),
                                                self._print_version(word),
                                                self._print_version(word2))

    def _print_version(self, word):
        ret = ''
        for char in word:
            ret += chr(char) if char > 0x20 and char < 0x7f  else '.'
        return ret
<commit_msg>Perfect hexdump<commit_after>import binascii


class Memory(object):
    """Generic memory abstaction."""

    def __init__(self, size):
        self.memory = bytearray([0] * size)

    def read(self, addr, count):
        if addr < 0 or addr + count > self.size():
            raise Exception('read outside memory bounds')
        elif count < 0:
            raise Exception('negative memory read')
        return str(self.memory[addr:addr+count])

    def write(self, addr, buffer):
        if addr < 0 or addr + len(buffer) > self.size():
            raise Exception('write outside memory bounds')
        elif len(buffer) == 0:
            raise Exception('empty buffer to write')
        self.memory[addr:addr+len(buffer)] = buffer

    def size(self):
        return len(self.memory)

    def dump(self):
        fmt = '{:04x}  {}{} {}{}  {} {}'
        for i in range(self.size()-1, -1, -8):
            word = self.memory[i:i-4:-1] if i >= 4 else self.memory[i::-1]
            word2 = self.memory[i-4:i-8:-1] if i >= 8 else self.memory[i-4::-1] if i >= 5 else ''

            print fmt.format(i, binascii.hexlify(word), self._pad(word),
                             binascii.hexlify(word2), self._pad(word2),
                             self._print_version(word),
                             self._print_version(word2))

    def _pad(self, str):
        # str is the 4 byte string, occupying 8 characters when hexlified.
        return ' ' * (8-len(str)*2)

    def _print_version(self, word):
        ret = ''
        for char in word:
            ret += chr(char) if char > 0x20 and char < 0x7f else '.'
        return ret

{'fraction_non_alphanumeric': 0.1303348653972423, 'fraction_numerical': 0.01871306631648063, 'mean_word_length': 1.818686401480111, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3458670', 'n_tokens_mistral': 923, 'n_tokens_neox': 871, 'n_words': 247}