<commit_before>import clay
from .message import Message


class MessageFactory(object):
    def __init__(self, serializer, catalog):
        self.serializer = serializer
        self.catalog = catalog
        clay.add_catalog(self.catalog)

    def create(self, message_type):
        return Message(message_type, self.catalog, self.serializer)

    def retrieve(self, message):
        def _fill_obj(obj, payload):
            for k, v in payload.iteritems():
                try:
                    setattr(obj, k, v)
                except ValueError:  # complex datatype
                    attr = getattr(message, k)
                    for index, item in enumerate(v):
                        attr.add()
                        _fill_obj(attr[index], item)

        payload_id, payload, payload_schema = self.serializer.deserialize(message, self.catalog)
        message = Message(payload_schema['name'], self.catalog, self.serializer)
        _fill_obj(message, payload)

        return message

# vim:tabstop=4:expandtab<commit_msg>Minor fixes on HL7 deserialization<commit_after>import clay
from .message import Message


class MessageFactory(object):
    def __init__(self, serializer, catalog):
        self.serializer = serializer
        self.catalog = catalog
        clay.add_catalog(self.catalog)

    def create(self, message_type):
        return Message(message_type, self.catalog, self.serializer)

    def retrieve(self, message):
        def _fill_obj(obj, payload):
            for k, v in payload.iteritems():
                try:
                    setattr(obj, k, v)
                except ValueError:  # complex datatype
                    attr = getattr(message, k)
                    for index, item in enumerate(v):
                        attr.add()
                        _fill_obj(attr[index], item)

        payload, payload_id, payload_schema = self.serializer.deserialize(message, self.catalog)
        message = Message(payload_schema['name'], self.catalog, self.serializer)
        _fill_obj(message, payload)

        return message

# vim:tabstop=4:expandtab
{'fraction_non_alphanumeric': 0.0884450784593438, 'fraction_numerical': 0.0014265335235378032, 'mean_word_length': 2.1449925261584455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2525159', 'n_tokens_mistral': 571, 'n_tokens_neox': 548, 'n_words': 139}