<commit_before>from primitives import Typed
from types import MethodType, FunctionType
import requests
import json


def _execute(self, verb, params):
    format_args = self.__dict__.pop('format_args') if 'format_args' in self.__dict__ else None
    json_body = self.__dict__.pop('json_body') if 'json_body' in self.__dict__ else None

    for k, v in params.items():
        setattr(self, k, v)

    if json_body:
        body = json.dumps(self.__dict__)
        headers = {'content-type': 'application/json'}
    else:
        body = self.__dict__.copy()
        headers = None

    endpoint = self.Meta.__dict__[verb].format(**format_args) if format_args else self.Meta.__dict__[verb]
    self.format_args = None # this should be handled in a cleaner way
    self.json_body = None
    self.__dict__ = {}
    return requests.__dict__[verb](endpoint, data=body, headers=headers)


def _format(self, json_body=False, **format_args):
    self.format_args = format_args
    self.json_body = json_body
    return self


class RestModelMeta(type):
    def __new__(cls, clsname, bases, clsdict):
        for name, value in clsdict.items():
            if isinstance(value, Typed):
                value._name = name

        new_class = type.__new__(cls, clsname, bases, clsdict)

        if clsname != "RestModel":
            if 'Meta' not in clsdict:
                raise Exception("Meta class must be provided")

            setattr(new_class, "format_args", None)
            setattr(new_class, "json_body", None)
            setattr(new_class, "format", _format)
            setattr(new_class, '_execute', _execute)

            verbs = [x for x,y in clsdict['Meta'].__dict__.items() if type(y) == str and not x.startswith("__")]

            method_definition = """
def _{0}(self, **params):
    self._execute('{0}', params)
            """
            for verb in verbs:
                supported_verb = method_definition.format(verb)
                exec(supported_verb)
                setattr(new_class, verb, locals()["_" + verb])

        return new_class<commit_msg>returns a RestModel instance after each method, stores the result in response attribute<commit_after>from primitives import Typed
from types import MethodType, FunctionType
import requests
import json


def _execute(self, verb, params):
    format_args = self.__dict__.pop('format_args') if 'format_args' in self.__dict__ else None
    json_body = self.__dict__.pop('json_body') if 'json_body' in self.__dict__ else None

    for k, v in params.items():
        setattr(self, k, v)

    if json_body:
        body = json.dumps(self.__dict__)
        headers = {'content-type': 'application/json'}
    else:
        body = self.__dict__.copy()
        headers = None

    endpoint = self.Meta.__dict__[verb].format(**format_args) if format_args else self.Meta.__dict__[verb]
    self.format_args = None # this should be handled in a cleaner way
    self.json_body = None
    self.__dict__ = {}
    self.response = requests.__dict__[verb](endpoint, data=body, headers=headers)
    return self


def _format(self, json_body=False, **format_args):
    self.format_args = format_args
    self.json_body = json_body
    return self


class RestModelMeta(type):
    def __new__(cls, clsname, bases, clsdict):
        for name, value in clsdict.items():
            if isinstance(value, Typed):
                value._name = name

        new_class = type.__new__(cls, clsname, bases, clsdict)

        if clsname != "RestModel":
            if 'Meta' not in clsdict:
                raise Exception("Meta class must be provided")

            setattr(new_class, "format_args", None)
            setattr(new_class, "json_body", None)
            setattr(new_class, "format", _format)
            setattr(new_class, '_execute', _execute)

            verbs = [x for x,y in clsdict['Meta'].__dict__.items() if type(y) == str and not x.startswith("__")]

            method_definition = """
def _{0}(self, **params):
    return self._execute('{0}', params)
            """
            for verb in verbs:
                supported_verb = method_definition.format(verb)
                exec(supported_verb)
                setattr(new_class, verb, locals()["_" + verb])

        return new_class
{'fraction_non_alphanumeric': 0.10873649600751527, 'fraction_numerical': 0.0009394081728511038, 'mean_word_length': 2.684256055363322, 'pattern_counts': {'":': 2, '<': 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': '5747634', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1236, 'n_words': 344}