<commit_before>import types
import turtle
import json
import socket


def send_through_socket(sock):
    """A decorator that encodes all commands in json (function, args, kwargs) tuples, and sends it on a socket before calling the command."""
    def decorator(func):
        def wrapper(*args, **kwargs):
            command = (func.__name__, args, kwargs)
            func_json = json.dumps(command)
            sock.send(func_json.encode())
            return func(*args, **kwargs)

        return wrapper
    return decorator


class EchoClient(object):

    def __init__(self, ip='localhost', port=8000):
        """
        A TCP client socket that can send serialized method calls on bound objects.

        Arguments:
            -ip (str): ip address of the server to connect to. Default: 'localhost'
            -port (int): port number of the server to connect to. Default: 8000
        """

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((ip, port))

    def bind(self, target):
        """Modifies a target object to send command packets."""

        for name in dir(target):

            # Filter out private methods and attributes
            if name[0] == '_':
                continue
            if not callable(getattr(target, name)):
                continue
            if name[:2] == 'on':
                continue

            # Apply decorator to method
            setattr(target, name, send_through_socket(self.sock)(getattr(target, name)))
<commit_msg>Refactored code<commit_after>import json
import socket


def send_through_socket(sock):
    """
    A decorator that encodes all commands in json (function, args, kwargs) tuples, and echos it on a socket.
    The command's format will be semt as (function_name <str>, *args <tuple>, **kwargs <dict>)
    """
    def decorator(func):
        def wrapper(*args, **kwargs):
            command = (func.__name__, args, kwargs)
            func_json = json.dumps(command)
            sock.send(func_json.encode())
            return func(*args, **kwargs)

        return wrapper
    return decorator


class EchoClient(object):

    def __init__(self, ip='localhost', port=8000):
        """
        A TCP client socket that can send serialized method calls on bound objects.

        Arguments:
            -ip (str): ip address of the server to connect to. Default: 'localhost'
            -port (int): port number of the server to connect to. Default: 8000
        """

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((ip, port))

    def bind(self, target):
        """Modifies a target object to send command packets."""
        socket_sender = send_through_socket(self.sock)
        targ_methods = [name for name in dir(target) if not callable(getattr(target, name)) or name[:2] in ['_', 'on']]
        for name in targ_methods:
            setattr(target, name, socket_sender(getattr(target, name)))
{'fraction_non_alphanumeric': 0.10278804165267047, 'fraction_numerical': 0.006382264024185421, 'mean_word_length': 2.450753186558517, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '7517253', 'n_tokens_mistral': 827, 'n_tokens_neox': 785, 'n_words': 282}