<commit_before>from .utils import unpack
from .exceptions import DoesNotExist


class DRESTRecord(object):
    FIELD_BLACKLIST = {'links'}

    def __init__(self, resource, **data):
        self._resource = resource
        self._load(data)

    def _get_data(self):
        return {
            k: v for k, v in self.__dict__.items()
            if not k.startswith('_') and k not in self.FIELD_BLACKLIST
        }

    def _load(self, data):
        for key, value in data.items():
            if key.startswith('_'):
                data.pop(key)
            setattr(self, key, value)

        self.id = data.get('_meta', {}).get('id', data.get('id', None))

    def __repr__(self):
        return '%s.%s' % (self._resource.name, self.id if self.id else '')

    def save(self):
        id = self.id
        response = self._resource.request(
            'patch' if id else 'post',
            id=self.id,
            data=self._get_data()
        )
        self._load(unpack(response))
        return response<commit_msg>only send changed fields<commit_after>from .utils import unpack
from .exceptions import DoesNotExist


class DRESTRecord(object):
    # TODO: use metadata to figure out which fields to ignore
    FIELD_BLACKLIST = {'links'}

    def __init__(self, resource, **data):
        self._resource = resource
        self._load(data)

    @property
    def _data(self):
        return {
            k: v for k, v in self.__dict__.items()
            if not k.startswith('_') and k not in self.FIELD_BLACKLIST
        }

    @property
    def _diff(self):
        data = self._data
        for key, value in data.items():
            if key in self._clean and value == self._clean[key]:
                data.pop(key)

        return data

    def _load(self, data):
        for key, value in data.items():
            if key.startswith('_'):
                data.pop(key)
            setattr(self, key, value)

        self._clean = self._data
        self.id = data.get('_meta', {}).get('id', data.get('id', None))

    def __repr__(self):
        return '%s.%s' % (self._resource.name, self.id if self.id else '')

    def save(self):
        id = self.id
        data = self._diff if id else self._data
        if data:
            response = self._resource.request(
                'patch' if id else 'post',
                id=id,
                data=data
            )
            self._load(unpack(response))
{'fraction_non_alphanumeric': 0.11883223684210527, 'fraction_numerical': 0.0, 'mean_word_length': 1.8522860492379836, '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': '6513345', 'n_tokens_mistral': 745, 'n_tokens_neox': 714, 'n_words': 203}