<commit_before>from couchdbkit.ext.django.schema import Document


class LazyAttachmentDoc(Document):

    @classmethod
    def wrap(cls, data):
        self = super(LazyAttachmentDoc, cls).wrap(data)
        if self._attachments:
            for name, attachment in self._attachments.items():
                if isinstance(attachment, basestring):
                    del self._attachments[name]
                    self.lazy_put_attachment(attachment, name)
        return self

    def __store_lazy_attachment(self, content, name=None, content_type=None,
                                content_length=None):
        if not hasattr(self, '_LAZY_ATTACHMENTS'):
            self._LAZY_ATTACHMENTS = {}
        info = {
            'content': content,
            'content_type': content_type,
            'content_length': content_length,
        }
        self._LAZY_ATTACHMENTS[name] = info
        return info

    def put_attachment(self, content, name=None, content_type=None,
                       content_length=None):
        info = self.__store_lazy_attachment(content, name, content_type,
                                            content_length)
        return super(LazyAttachmentDoc, self).put_attachment(name=name, **info)

    def lazy_put_attachment(self, content, name=None, content_type=None,
                            content_length=None):
        """
        Ensure the attachment is available through lazy_fetch_attachment
        and that upon self.save(), the attachments are put to the doc as well

        """
        info = self.__store_lazy_attachment(content, name, content_type,
                                            content_length)

        def put_attachment():
            self.put_attachment(name=name, **info)

        self.register_post_save(put_attachment)

    def lazy_fetch_attachment(self, name):
        if not hasattr(self, '_LAZY_ATTACHMENTS'):
            self._LAZY_ATTACHMENTS = {}
        try:
            return self._LAZY_ATTACHMENTS[name]['content']
        except KeyError:
            return self.fetch_attachment(name)

    def lazy_list_attachments(self):
        keys = set()
        keys.update(getattr(self, '_LAZY_ATTACHMENTS', None) or {})
        keys.update(getattr(self, '_attachments', None) or {})
        return keys<commit_msg>split a line in two for better stacktrace<commit_after>from couchdbkit.ext.django.schema import Document


class LazyAttachmentDoc(Document):

    @classmethod
    def wrap(cls, data):
        self = super(LazyAttachmentDoc, cls).wrap(data)
        if self._attachments:
            for name, attachment in self._attachments.items():
                if isinstance(attachment, basestring):
                    del self._attachments[name]
                    self.lazy_put_attachment(attachment, name)
        return self

    def __store_lazy_attachment(self, content, name=None, content_type=None,
                                content_length=None):
        if not hasattr(self, '_LAZY_ATTACHMENTS'):
            self._LAZY_ATTACHMENTS = {}
        info = {
            'content': content,
            'content_type': content_type,
            'content_length': content_length,
        }
        self._LAZY_ATTACHMENTS[name] = info
        return info

    def put_attachment(self, content, name=None, content_type=None,
                       content_length=None):
        info = self.__store_lazy_attachment(content, name, content_type,
                                            content_length)
        return super(LazyAttachmentDoc, self).put_attachment(name=name, **info)

    def lazy_put_attachment(self, content, name=None, content_type=None,
                            content_length=None):
        """
        Ensure the attachment is available through lazy_fetch_attachment
        and that upon self.save(), the attachments are put to the doc as well

        """
        info = self.__store_lazy_attachment(content, name, content_type,
                                            content_length)

        def put_attachment():
            self.put_attachment(name=name, **info)

        self.register_post_save(put_attachment)

    def lazy_fetch_attachment(self, name):
        if not hasattr(self, '_LAZY_ATTACHMENTS'):
            self._LAZY_ATTACHMENTS = {}
        try:
            info = self._LAZY_ATTACHMENTS[name]
            return info['content']
        except KeyError:
            return self.fetch_attachment(name)

    def lazy_list_attachments(self):
        keys = set()
        keys.update(getattr(self, '_LAZY_ATTACHMENTS', None) or {})
        keys.update(getattr(self, '_attachments', None) or {})
        return keys
{'fraction_non_alphanumeric': 0.08601458601458602, 'fraction_numerical': 0.0, 'mean_word_length': 2.1274312541918174, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7137793', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1232, 'n_words': 270}