<commit_before>"""
"""

from builtins import map
from builtins import object
from untwisted.network import core, Spin, xmap, spawn
from untwisted.iostd import Server, Stdin, Stdout, CLOSE, ACCEPT
from untwisted.splits import Terminator
from untwisted.tools import handle_exception
import operator
from functools import reduce

class CalcParser(object):
    """
    """

    def __init__(self, client):
        xmap(client, Terminator.FOUND, self.handle_found)

    @handle_exception(ValueError)
    def handle_found(client, data):
        op, args = data.split(' ', 1)
        args     = list(map(float, args.split(' ')))
        spawn(client, op, args)

class CalcServer(object):
    """ 
    """

    def __init__(self, server):
        xmap(server, ACCEPT, self.handle_accept)

    def handle_accept(self, server, client):
        Stdin(client)
        Stdout(client)
        Terminator(client, delim='\r\n')
        parser = CalcParser(client)
        
        xmap(client, 'add', self. on_add)    
        xmap(client, 'sub', self.on_sub)    
        xmap(client, 'mul', self.on_mul)    
        xmap(client, 'div', self.on_div)    
        xmap(client, (parser.handle_found, ValueError), self.on_error)    

        xmap(client, CLOSE, self.handle_close)

    def on_add(self, client, args):
        self.send_msg(client, reduce(operator.add, args, 0))

    def on_sub(self, client, args):
        self.send_msg(client, reduce(operator.sub, args, 0))

    def on_div(self, client, args):
        self.send_msg(client, reduce(operator.div, args, args.pop(0)))

    def on_mul(self, client, args):
        self.send_msg(client, reduce(operator.mul, args, args.pop(0)))

    def on_error(self, client, excpt):
        self.send_msg(client, excpt)

    def handle_close(self, client, err):
        client.destroy()
        client.close()

    def send_msg(self, client, msg):
        client.dump('%s\r\n' % msg)

if __name__ == '__main__':
    server = Spin()
    server.bind(('', 1234))
    server.listen(5)

    Server(server)
    CalcServer(server)
    core.gear.mainloop()




<commit_msg>Fixing example to work with py3.<commit_after>"""
"""

from builtins import map
from builtins import object
from untwisted.network import core, Spin, xmap, spawn
from untwisted.iostd import Server, Stdin, Stdout, CLOSE, ACCEPT
from untwisted.splits import Terminator
from untwisted.tools import handle_exception
import operator
from functools import reduce

class CalcParser(object):
    """
    """

    def __init__(self, client):
        xmap(client, Terminator.FOUND, self.handle_found)

    @handle_exception(ValueError)
    def handle_found(client, data):
        op, args = data.decode('utf8').split(' ', 1)
        args     = list(map(float, args.split(' ')))
        spawn(client, op, args)

class CalcServer(object):
    """ 
    """

    def __init__(self, server):
        xmap(server, ACCEPT, self.handle_accept)

    def handle_accept(self, server, client):
        Stdin(client)
        Stdout(client)
        Terminator(client, delim=b'\r\n')
        parser = CalcParser(client)
        
        xmap(client, 'add', self. on_add)    
        xmap(client, 'sub', self.on_sub)    
        xmap(client, 'mul', self.on_mul)    
        xmap(client, 'div', self.on_div)    
        xmap(client, (parser.handle_found, ValueError), self.on_error)    

        xmap(client, CLOSE, self.handle_close)

    def on_add(self, client, args):
        self.send_msg(client, reduce(operator.add, args, 0))

    def on_sub(self, client, args):
        self.send_msg(client, reduce(operator.sub, args, 0))

    def on_div(self, client, args):
        self.send_msg(client, reduce(operator.truediv, args, args.pop(0)))

    def on_mul(self, client, args):
        self.send_msg(client, reduce(operator.mul, args, args.pop(0)))

    def on_error(self, client, excpt):
        self.send_msg(client, excpt)

    def handle_close(self, client, err):
        client.destroy()
        client.close()

    def send_msg(self, client, msg):
        client.dump(('%s\r\n' % msg).encode('utf8'))

if __name__ == '__main__':
    server = Spin()
    server.bind(('', 1234))
    server.listen(5)

    Server(server)
    CalcServer(server)
    core.gear.mainloop()






{'fraction_non_alphanumeric': 0.13115140098893335, 'fraction_numerical': 0.005415587473510714, 'mean_word_length': 2.918819188191882, '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': '5173889', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1404, 'n_words': 323}