<commit_before>import threading
import time
from functools import partial
from queue import Empty

import msgpack
import zmq

import infupy.backends.fresenius as fresenius

zmqhost = '127.0.0.1'
zmqport = 4201
freseniusport = 'COM6'

def stateWorker(stopevent):
    context = zmq.Context()
    zmqsocket = context.socket(zmq.PUB)
    zmqsocket.connect(f'tcp://{zmqhost}:{zmqport}')
    comm = fresenius.FreseniusComm(freseniusport)
    base = fresenius.FreseniusBase(comm)

    paramVolumeWorker = partial(volumeWorker, comm.eventq, zmqsocket, stopevent)
    volumeThread = threading.Thread(target=paramVolumeWorker)
    volumeThread.start()

    while not stopevent.wait(5):
        # Check Base
        try:
            dtype = base.readDeviceType()
        except fresenius.CommandError as e:
            print(f'Base error: {e}')
            try:
                base = fresenius.FreseniusBase(comm)
            except fresenius.CommandError as e:
                print(f'Failed to connect to base: {e}')
        # Check Syringes
        try:
            modids = base.listModules()
            for modid in modids:
                if not modid in base.syringes:
                    s = base.connectSyringe(modid)
                s.registerEvent(fresenius.VarId.volume)
        except fresenius.CommandError as e:
            print(f'Attach syringe error: {e}')
    print('Stopping')
    volumeThread.join()
    print('volumeThread joined')
    for s in base.syringes.values():
        print(f'Disconnecting syringe {s.index}')
        s.disconnect()
    print(f'Disconnecting Base')
    base.disconnect()

def volumeWorker(queue, zmqsocket, stopevent):
    while not stopevent.is_set():
        try:
            dt, origin, inmsg = queue.get(timeout=1)
        except Empty:<commit_msg>Refactored by Sourcery<commit_after>import threading
import time
from functools import partial
from queue import Empty

import msgpack
import zmq

import infupy.backends.fresenius as fresenius

zmqhost = '127.0.0.1'
zmqport = 4201
freseniusport = 'COM6'

def stateWorker(stopevent):
    context = zmq.Context()
    zmqsocket = context.socket(zmq.PUB)
    zmqsocket.connect(f'tcp://{zmqhost}:{zmqport}')
    comm = fresenius.FreseniusComm(freseniusport)
    base = fresenius.FreseniusBase(comm)

    paramVolumeWorker = partial(volumeWorker, comm.eventq, zmqsocket, stopevent)
    volumeThread = threading.Thread(target=paramVolumeWorker)
    volumeThread.start()

    while not stopevent.wait(5):
        # Check Base
        try:
            dtype = base.readDeviceType()
        except fresenius.CommandError as e:
            print(f'Base error: {e}')
            try:
                base = fresenius.FreseniusBase(comm)
            except fresenius.CommandError as e:
                print(f'Failed to connect to base: {e}')
        # Check Syringes
        try:
            modids = base.listModules()
            for modid in modids:
                if modid not in base.syringes:
                    s = base.connectSyringe(modid)
                s.registerEvent(fresenius.VarId.volume)
        except fresenius.CommandError as e:
            print(f'Attach syringe error: {e}')
    print('Stopping')
    volumeThread.join()
    print('volumeThread joined')
    for s in base.syringes.values():
        print(f'Disconnecting syringe {s.index}')
        s.disconnect()
    print(f'Disconnecting Base')
    base.disconnect()

def volumeWorker(queue, zmqsocket, stopevent):
    while not stopevent.is_set():
        try:
            dt, origin, inmsg = queue.get(timeout=1)
        except Empty:
{'fraction_non_alphanumeric': 0.09690893901420217, 'fraction_numerical': 0.007240323029796714, 'mean_word_length': 2.5181194906953968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2056782', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1126, 'n_words': 255}