<commit_before>import six
from six.moves.urllib_parse import quote

from delighted import get_shared_client


class Resource(dict):

    def __init__(self, attrs={}):
        super(Resource, self).__init__()

        if 'id' in attrs:
            object.__setattr__(self, 'id', attrs['id'])
            del attrs['id']

        if hasattr(self.__class__, 'expandable_attributes'):
            for attr, klass in six.iteritems(self.expandable_attributes):
                if attr in attrs and isinstance(attrs[attr], dict):
                    expandable_attrs = attrs.pop(attr)
                    item_id = expandable_attrs['id']
                    super(Resource, self).__setitem__(attr, item_id)
                    object.__setattr__(self, attr, klass(expandable_attrs))

        for k, v in six.iteritems(attrs):
            super(Resource, self).__setitem__(k, v)

    def __setattr__(self, k, v):
        if k[0] == '_' or k in self.__dict__:
            return super(Resource, self).__setattr__(k, v)
        else:
            self[k] = v

    def __getattr__(self, k):
        if k[0] == '_' and k is not '_attrs':
            raise AttributeError(k)

        try:
            return self[k]
        except KeyError as err:
            raise AttributeError(*err.args)

    @classmethod
    def _set_client(self, params):
        self.client = params.pop('client', None) or get_shared_client()


class AllResource(Resource):
    @classmethod
    def all(self, **params):
        self._set_client(params)
        j = self.client.request('get', self.path, {}, params)
        return [self(i) for i in j]


class CreateableResource(Resource):

    @classmethod
    def create(self, **params):
        self._set_client(params)
        j = self.client.request('post', self.path, {}, params)
        return self(j)


class RetrievableResource(Resource):

    @classmethod
    def retrieve(self, *args, **params):
        self._set_client(params)
        if (len(args) > 0 and
            not (hasattr(self.__class__, 'singleton_resource') and
                 self.singleton_resource)):
            path = '%s/%s' % (self.path, args[0])
        else:
            path = self.path
        j = self.client.request('get', path, {}, params)
        return self(j)


class UpdateableResource(Resource):
    def save(self, **params):
        self._set_client(params)
        if hasattr(self.__class__, 'expandable_attributes'):
            expand_attrs = list(self.__class__.expandable_attributes.keys())
            params.update({'expand': expand_attrs})
        path = '%s/%s' % (self.path, self.id) if self.id else self.path
        j = self.client.request('put', path, {}, dict(self))
        return type(self)(j)


class Metrics(RetrievableResource):
    path = 'metrics'
    singleton_resource = True


class Person(CreateableResource):
    path = 'people'


class SurveyRequest(Resource):<commit_msg>was not actually broken<commit_after>import six
from six.moves.urllib_parse import quote

from delighted import get_shared_client


class Resource(dict):

    def __init__(self, attrs={}):
        super(Resource, self).__init__()

        if 'id' in attrs:
            object.__setattr__(self, 'id', attrs['id'])
            del attrs['id']

        if hasattr(self.__class__, 'expandable_attributes'):
            for attr, klass in six.iteritems(self.expandable_attributes):
                if attr in attrs and isinstance(attrs[attr], dict):
                    expandable_attrs = attrs.pop(attr)
                    item_id = expandable_attrs['id']
                    super(Resource, self).__setitem__(attr, item_id)
                    object.__setattr__(self, attr, klass(expandable_attrs))

        for k, v in six.iteritems(attrs):
            super(Resource, self).__setitem__(k, v)

    def __setattr__(self, k, v):
        if k[0] == '_' or k in self.__dict__:
            return super(Resource, self).__setattr__(k, v)
        else:
            self[k] = v

    def __getattr__(self, k):
        if k[0] == '_' and k is not '_attrs':
            raise AttributeError(k)

        try:
            return self[k]
        except KeyError as err:
            raise AttributeError(*err.args)

    @classmethod
    def _set_client(self, params):
        self.client = params.pop('client', None) or get_shared_client()


class AllResource(Resource):
    @classmethod
    def all(self, **params):
        self._set_client(params)
        j = self.client.request('get', self.path, {}, params)
        return [self(i) for i in j]


class CreateableResource(Resource):

    @classmethod
    def create(self, **params):
        self._set_client(params)
        j = self.client.request('post', self.path, {}, params)
        return self(j)


class RetrievableResource(Resource):

    @classmethod
    def retrieve(self, *args, **params):
        self._set_client(params)
        path = '%s/%s' % (self.path, args[0]) if len(args) > 0 else self.path
        j = self.client.request('get', path, {}, params)
        return self(j)


class UpdateableResource(Resource):
    def save(self, **params):
        self._set_client(params)
        if hasattr(self.__class__, 'expandable_attributes'):
            expand_attrs = list(self.__class__.expandable_attributes.keys())
            params.update({'expand': expand_attrs})
        path = '%s/%s' % (self.path, self.id) if self.id else self.path
        j = self.client.request('put', path, {}, dict(self))
        return type(self)(j)


class Metrics(RetrievableResource):
    path = 'metrics'
    singleton_resource = True


class Person(CreateableResource):
    path = 'people'


class SurveyRequest(Resource):
{'fraction_non_alphanumeric': 0.12323446327683615, 'fraction_numerical': 0.0014124293785310734, 'mean_word_length': 2.460598656078192, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2091293', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1659, 'n_words': 405}