<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_history(self, ticket_id, transaction_id=None):
 """ Get set of history items. :param ticket_id: ID of ticket :keyword transaction_id: If set to None, all history items are returned, if set to ID of valid transaction just one history item is returned :returns: List of history items ordered increasingly by time of event. Each history item is dictionary with following keys: Description, Creator, Data, Created, TimeTaken, NewValue, Content, Field, OldValue, Ticket, Type, id, Attachments All these fields are strings, just 'Attachments' holds list of pairs (attachment_id,filename_with_size). Returns None if ticket or transaction does not exist. :raises UnexpectedMessageFormat: Unexpected format of returned message. """

        if transaction_id is None:
            # We are using "long" format to get all history items at once.
            # Each history item is then separated by double dash.
            msgs = self.__request('ticket/{}/history?format=l'.format(str(ticket_id), ))
        else:
            msgs = self.__request('ticket/{}/history/id/{}'.format(str(ticket_id), str(transaction_id)))
        lines = msgs.split('\n')
        if (len(lines) > 2) and (
                self.RE_PATTERNS['does_not_exist_pattern'].match(lines[2]) or self.RE_PATTERNS['not_related_pattern'].match(
                lines[2])):
            return None
        msgs = msgs.split('\n--\n')
        items = []
        for msg in msgs:
            pairs = {}
            msg = msg.split('\n')
            cont_matching = [i for i, m in enumerate(msg) if self.RE_PATTERNS['content_pattern'].match(m)]
            cont_id = cont_matching[0] if cont_matching else None
            if not cont_id:
                raise UnexpectedMessageFormat('Unexpected history entry. \
                                               Missing line starting with `Content:`.')
            atta_matching = [i for i, m in enumerate(msg) if self.RE_PATTERNS['attachments_pattern'].match(m)]
            atta_id = atta_matching[0] if atta_matching else None
            if not atta_id:
                raise UnexpectedMessageFormat('Unexpected attachment part of history entry. \
                                               Missing line starting with `Attachements:`.')
            for i in range(cont_id):
                if ': ' in msg[i]:
                    header, content = self.split_header(msg[i])
                    pairs[header.strip()] = content.strip()
            content = msg[cont_id][9:]
            cont_id += 1
            while (cont_id < len(msg)) and (msg[cont_id][:9] == ' ' * 9):
                content += '\n' + msg[cont_id][9:]
                cont_id += 1
            pairs['Content'] = content
            for i in range(cont_id, atta_id):
                if ': ' in msg[i]:
                    header, content = self.split_header(msg[i])
                    pairs[header.strip()] = content.strip()
            attachments = []
            for i in range(atta_id + 1, len(msg)):
                if ': ' in msg[i]:
                    header, content = self.split_header(msg[i])
                    attachments.append((int(header),
                                        content.strip()))
            pairs['Attachments'] = attachments
            items.append(pairs)
        return items