<commit_before>"""Utility module

Some useful gevent functions.

"""

import random

import gevent.socket

from gevent_tools.service import Service

class ServiceWrapper(Service):
    def __init__(self, klass, *args, **kwargs):
        super(ServiceWrapper, self).__init__()
        self.wrapped = klass(*args, **kwargs)
    
    def do_start(self):
        self.spawn(self.wrapped.start)
    
    def do_stop(self):
        self.wrapped.stop()

def line_protocol(socket_or_file, strip=True):
    """Generator for looping line-based protocol
    
    Takes a socket or file-from-socket and yields on every line. Disconnecting
    or connection errors will end the loop.
    
    """
    
    if hasattr(socket_or_file, 'makefile'):
        fileobj = socket_or_file.makefile()
    else:
        fileobj = socket_or_file
    while True:
        try:
            line = fileobj.readline() # returns None on EOF
            if line is not None and strip:
                line = line.strip()
        except IOError:
            line = None
        if line:
            yield line<commit_msg>hopefully now you can put instances into ServiceWrapper<commit_after>"""Utility module

Some useful gevent functions.

"""

import random

import gevent.socket
import gevent.baseserver

from gevent_tools.service import Service

class ServiceWrapper(Service):
    def __init__(self, klass_or_server, *args, **kwargs):
        super(ServiceWrapper, self).__init__()
        if isinstance(klass_or_server, gevent.baseserver.BaseServer):
            self.wrapped = klass_or_server
        else:
            self.wrapped = klass(*args, **kwargs)
    
    def do_start(self):
        self.spawn(self.wrapped.start)
    
    def do_stop(self):
        self.wrapped.stop()

def line_protocol(socket_or_file, strip=True):
    """Generator for looping line-based protocol
    
    Takes a socket or file-from-socket and yields on every line. Disconnecting
    or connection errors will end the loop.
    
    """
    
    if hasattr(socket_or_file, 'makefile'):
        fileobj = socket_or_file.makefile()
    else:
        fileobj = socket_or_file
    while True:
        try:
            line = fileobj.readline() # returns None on EOF
            if line is not None and strip:
                line = line.strip()
        except IOError:
            line = None
        if line:
            yield line
{'fraction_non_alphanumeric': 0.08487394957983194, 'fraction_numerical': 0.0, 'mean_word_length': 2.377304964539007, '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': '4584855', 'n_tokens_mistral': 694, 'n_tokens_neox': 696, 'n_words': 198}