<commit_before>__all__ = [
    'Client',
]

import nng
import nng.asyncs

from g1.bases import classes
from g1.bases import collections
from g1.bases.assertions import ASSERT

from . import utils


class Client:

    def __init__(self, request_type, response_type, wiredata):
        self._request_type = request_type
        self._response_type = response_type
        self._wiredata = wiredata
        # You may (optionally) use context to close socket on exit.
        self.socket = nng.asyncs.Socket(nng.Protocols.REQ0)
        self.m = collections.Namespace(
            **{name: _make_transceiver(self, name)
               for name in request_type.m}
        )

    __repr__ = classes.make_repr('{self.socket!r}')

    def __enter__(self):
        self.socket.__enter__()
        return self

    def __exit__(self, *args):
        return self.socket.__exit__(*args)

    async def transceive(self, request):
        with nng.asyncs.Context(ASSERT.not_none(self.socket)) as context:
            await context.send(self._wiredata.to_lower(request))
            wire_response = await context.recv()
            return self._wiredata.to_upper(self._response_type, wire_response)


def _make_transceiver(self, name):

    make = self._request_type.m[name]

    async def transceive(**kwargs):
        response = await self.transceive(
            self._request_type(args=make(**kwargs))
        )
        if response.error is not None:
            raise utils.select(response.error)[1]
        else:
            return getattr(response.result, name)

    return transceive<commit_msg>Break Client class into smaller classes for extending method interface<commit_after>__all__ = [
    'Client',
]

import nng
import nng.asyncs

from g1.bases import classes
from g1.bases import collections
from g1.bases.assertions import ASSERT

from . import utils


class Client:

    def __init__(self, request_type, response_type, wiredata):
        self.socket = nng.asyncs.Socket(nng.Protocols.REQ0)
        self.transceive = Transceiver(self.socket, response_type, wiredata)
        self.m = collections.Namespace(
            **{
                name: Method(name, request_type, self.transceive)
                for name in request_type.m
            }
        )

    __repr__ = classes.make_repr('{self.socket!r}')

    def __enter__(self):
        self.socket.__enter__()
        return self

    def __exit__(self, *args):
        return self.socket.__exit__(*args)


class Transceiver:

    def __init__(self, socket, response_type, wiredata):
        self._socket = socket
        self._response_type = response_type
        self._wiredata = wiredata

    async def __call__(self, request):
        with nng.asyncs.Context(ASSERT.not_none(self._socket)) as context:
            await context.send(self._wiredata.to_lower(request))
            wire_response = await context.recv()
            return self._wiredata.to_upper(self._response_type, wire_response)


class Method:

    def __init__(self, name, request_type, transceive):
        self._name = name
        self._request_type = request_type
        self._transceive = transceive

    async def __call__(self, **kwargs):
        response = await self._transceive(
            self._request_type(
                args=self._request_type.m[self._name](**kwargs)
            )
        )
        if response.error is not None:
            raise utils.select(response.error)[1]
        else:
            return getattr(response.result, self._name)
{'fraction_non_alphanumeric': 0.0923120891683338, 'fraction_numerical': 0.002857959416976279, 'mean_word_length': 2.5460992907801416, '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': '3623064', 'n_tokens_mistral': 1030, 'n_tokens_neox': 1021, 'n_words': 234}