<commit_before>import json
import io
import requests

from pykechain.exceptions import APIError
from pykechain.models.property import Property


class AttachmentProperty(Property):
    """A virtual object representing a KE-chain attachment property."""

    @property
    def value(self):
        """Retrieve the data value of this attachment.

        Will show the filename of the attachment if there is an attachment available otherwise None
        Use save_as in order to download as a file.

        Example
        -------

        >>> file_attachment_property = project.part('Bike').property('file_attachment')
        >>> if file_attachment_property.value:
        ...     file_attachment_property.save_as('file.ext')
        ... else:
        ...     print('file attachment not set, its value is None')

        """
        if 'value' in self._json_data and self._json_data['value']:
            return "[Attachment: {}]".format(self._json_data['value'].split('/')[-1])
        else:
            return None

    @value.setter
    def value(self, value):
        raise RuntimeError("Cannot set the value of an attachment property, use upload()")

    def json_load(self):
        """Download the data from the attachment and deserialise the contained json.

        :return: deserialised json data
        :raises: APIError, JSONDecodeError

        Example
        -------
        Ensure that the attachment is valid json data

        >>> json_attachment = project.part('Bike').property('json_attachment')
        >>> deserialised_json = json_attachment.json_load()
        """
        return self._download().json()

    def upload(self, data, **kwargs):
        """Upload a file to the attachment property.

        :param filename: File path
        :raises: APIError
        """
        try:
            import matplotlib.figure

            if isinstance(data, matplotlib.figure.Figure):
                self._upload_plot(data, **kwargs)
                return
        except ImportError:
            pass<commit_msg>added clear() method of the attachment field<commit_after>import json
import io
import requests

from pykechain.exceptions import APIError
from pykechain.models.property import Property


class AttachmentProperty(Property):
    """A virtual object representing a KE-chain attachment property."""

    @property
    def value(self):
        """Retrieve the data value of this attachment.

        Will show the filename of the attachment if there is an attachment available otherwise None
        Use save_as in order to download as a file.

        Example
        -------

        >>> file_attachment_property = project.part('Bike').property('file_attachment')
        >>> if file_attachment_property.value:
        ...     file_attachment_property.save_as('file.ext')
        ... else:
        ...     print('file attachment not set, its value is None')

        """
        if 'value' in self._json_data and self._json_data['value']:
            return "[Attachment: {}]".format(self._json_data['value'].split('/')[-1])
        else:
            return None

    @value.setter
    def value(self, value):
        raise RuntimeError("Cannot set the value of an attachment property, use upload() to upload a new attachment or "
                           "clear() to clear the attachment from the field")

    def clear(self):
        """Clears the attachment from the attachment field"""

    def json_load(self):
        """Download the data from the attachment and deserialise the contained json.

        :return: deserialised json data
        :raises: APIError, JSONDecodeError

        Example
        -------
        Ensure that the attachment is valid json data

        >>> json_attachment = project.part('Bike').property('json_attachment')
        >>> deserialised_json = json_attachment.json_load()
        """
        return self._download().json()

    def upload(self, data, **kwargs):
        """Upload a file to the attachment property.

        :param filename: File path
        :raises: APIError
        """
        try:
            import matplotlib.figure

            if isinstance(data, matplotlib.figure.Figure):
                self._upload_plot(data, **kwargs)
                return
        except ImportError:
            pass
{'fraction_non_alphanumeric': 0.09906759906759907, 'fraction_numerical': 0.0004662004662004662, 'mean_word_length': 2.293169608595549, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2635597', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1042, 'n_words': 356}