<commit_before>import time
import grpc
from platoai_protos import api_pb2_grpc, api_pb2, phone_call_pb2


class PushRequestIter(object):
    """Wrapper class for api_pb2.PushRequest that conforms to the iterator
    protocol to support streaming in the API.
    """

    def __init__(self, audio, metadata, chunk_size=1024, callbacks=[]):
        self.audio = audio

        self.metadata = metadata
        dt = metadata['timestamp']
        dt = time.mktime(dt.timetuple()) * 1e3 + dt.microsecond / 1e3
        metadata['timestamp'] = long(dt)

        self.chunk_size = chunk_size
        self.callbacks = callbacks

    def __iter__(self):
        return self

    def next(self):
        chunk = self.audio.read(self.chunk_size)
        if chunk != '':
            phone_call = phone_call_pb2.PhoneCall(
                audio=chunk,
                metadata=phone_call_pb2.PhoneCallMetadata(**self.metadata))

            for fn in self.callbacks:
                fn(self.chunk_size)

            return api_pb2.PushRequest(phoneCall=phone_call)
        else:
            raise StopIteration


def push(audio,
         metadata,
         host='api.platoai.com',
         port=9000,
         channel=None,
         callbacks=[]):
    """Enqueue a call to be processed by Plato AI.

    Args:
        audio (:obj:`file-like object`): The raw audio bytes of the call.
        metadata (:obj:`dictionary`): The metadata for the call.
        channel (:obj:`grpc.channel`, optional): The gRPC channel.
        callbacks (list): A list of 1-arity functions, called with the chunk
            size after each chunk of audio is streamed to the API server.

    Returns:
        metadata (:obj:`dictionary`): The metadata for the uploaded call or
            error details.
    """

    if not channel:
        channel = grpc.secure_channel('{}:{}'.format(host, port),
                                      grpc.ssl_channel_credentials())

    stub = api_pb2_grpc.ScoringStub(channel)
    return stub.Push(PushRequestIter(audio, metadata, callbacks=callbacks))
<commit_msg>Improve code readability<commit_after>import time
import grpc
from platoai_protos import api_pb2_grpc, api_pb2, phone_call_pb2


class PushRequestIter(object):
    """Wrapper class for api_pb2.PushRequest that conforms to the iterator
    protocol to support streaming in the API.
    """

    def __init__(self, audio, metadata, chunk_size=1024, callbacks=[]):
        self.audio = audio

        self.metadata = metadata
        dt = metadata['timestamp']
        dt = time.mktime(dt.timetuple()) * 1e3 + dt.microsecond / 1e3
        metadata['timestamp'] = long(dt)

        self.chunk_size = chunk_size
        self.callbacks = callbacks

    def __iter__(self):
        return self

    def next(self):
        chunk = self.audio.read(self.chunk_size)
        if chunk:
            phone_call = phone_call_pb2.PhoneCall(
                audio=chunk,
                metadata=phone_call_pb2.PhoneCallMetadata(**self.metadata))

            for fn in self.callbacks:
                fn(self.chunk_size)

            return api_pb2.PushRequest(phoneCall=phone_call)
        else:
            raise StopIteration


def push(audio,
         metadata,
         host='api.platoai.com',
         port=9000,
         channel=None,
         callbacks=[]):
    """Enqueue a call to be processed by Plato AI.

    Args:
        audio (:obj:`file-like object`): The raw audio bytes of the call.
        metadata (:obj:`dictionary`): The metadata for the call.
        channel (:obj:`grpc.channel`, optional): The gRPC channel.
        callbacks (list): A list of 1-arity functions, called with the chunk
            size after each chunk of audio is streamed to the API server.

    Returns:
        metadata (:obj:`dictionary`): The metadata for the uploaded call or
            error details.
    """

    if not channel:
        channel = grpc.secure_channel('{}:{}'.format(host, port),
                                      grpc.ssl_channel_credentials())

    stub = api_pb2_grpc.ScoringStub(channel)
    return stub.Push(PushRequestIter(audio, metadata, callbacks=callbacks))

{'fraction_non_alphanumeric': 0.09461742698527636, 'fraction_numerical': 0.010137581462708182, 'mean_word_length': 2.3285140562248996, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1239180', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1156, 'n_words': 328}