<commit_before>import sys
import json
import time
import socket

from ws4py.exc import WebSocketException
from ws4py.client.threadedclient import WebSocketClient
from pyee import EventEmitter

DDP_VERSIONS = ["pre1"]

class DDPSocket(WebSocketClient, EventEmitter):
    """DDPSocket"""
    def __init__(self, url, debug=False):
        self.debug = debug
        WebSocketClient.__init__(self, url)
        EventEmitter.__init__(self)

    def opened(self):
        """Set the connect flag to true and send the connect message to
        the server."""
        self.emit('opened')

    def closed(self, code, reason=None):
        """Called when the connection is closed"""
        self.emit('closed', code, reason)

    def send(self, msg_dict):
        """Send a message through the websocket client and wait for the
        answer if the message being sent contains an id attribute."""
        message = json.dumps(msg_dict)
        super(DDPSocket, self).send(message)
        self._debug_log('<<<{}'.format(message))

    def received_message(self, data):
        self._debug_log('>>>{}'.format(data))
        self.emit('received_message', data)

    def _debug_log(self, msg):
        """Debug log messages if debug=True"""
        if not self.debug:
            return
        sys.stderr.write('{}\n'.format(msg))

    def once(self):
        """ OVERRIDE FROM WebSocket
        Performs the operation of reading from the underlying
        connection in order to feed the stream of bytes.
        We start with a small size of two bytes to be read
        from the connection so that we can quickly parse an
        incoming frame header. Then the stream indicates
        whatever size must be read from the connection since
        it knows the frame payload length.
        It returns `False` if an error occurred at the
        socket level or during the bytes processing. Otherwise,
        it returns `True`."""
        # check for self.sock existence
        # https://github.com/hharnisc/python-meteor/issues/5
        if self.terminated or not self.sock:
            return False

        try:
            b = self.sock.recv(self.reading_buffer_size)
        except socket.error:
            return False
        else:
            if not self.process(b):
                return False



class DDPClient(EventEmitter):
    """An event driven ddp client"""
    def __init__(self, url, auto_reconnect=True, auto_reconnect_timeout=0.5, debug=False):
        EventEmitter.__init__(self)
        self.ddpsocket = None
        self._is_closing = False
        self.url = url
        self.auto_reconnect = auto_reconnect
        self.auto_reconnect_timeout = auto_reconnect_timeout
        self.debug = debug
        self._session = None
        self._uniq_id = 0
        self._callbacks = {}
        self._init_socket()

    def _init_socket(self):
        """Initialize the ddp socket"""
        # destroy the connection if it already exists
        if self.ddpsocket:
            self.ddpsocket.remove_all_listeners('received_message')
            self.ddpsocket.remove_all_listeners('closed')
            self.ddpsocket.remove_all_listeners('opened')
            self.ddpsocket.close_connection()
            self.ddpsocket = None
<commit_msg>fix checking for existence of socket in DDPSocket<commit_after>import sys
import json
import time
import socket

from ws4py.exc import WebSocketException
from ws4py.client.threadedclient import WebSocketClient
from pyee import EventEmitter

DDP_VERSIONS = ["pre1"]

class DDPSocket(WebSocketClient, EventEmitter):
    """DDPSocket"""
    def __init__(self, url, debug=False):
        self.debug = debug
        WebSocketClient.__init__(self, url)
        EventEmitter.__init__(self)

    def opened(self):
        """Set the connect flag to true and send the connect message to
        the server."""
        self.emit('opened')

    def closed(self, code, reason=None):
        """Called when the connection is closed"""
        self.emit('closed', code, reason)

    def send(self, msg_dict):
        """Send a message through the websocket client and wait for the
        answer if the message being sent contains an id attribute."""
        message = json.dumps(msg_dict)
        super(DDPSocket, self).send(message)
        self._debug_log('<<<{}'.format(message))

    def received_message(self, data):
        self._debug_log('>>>{}'.format(data))
        self.emit('received_message', data)

    def _debug_log(self, msg):
        """Debug log messages if debug=True"""
        if not self.debug:
            return
        sys.stderr.write('{}\n'.format(msg))

    def once(self):
        # check for self.sock existence
        # https://github.com/hharnisc/python-meteor/issues/5
        if self.sock:
            return super(DDPSocket, self).once()
        return False


class DDPClient(EventEmitter):
    """An event driven ddp client"""
    def __init__(self, url, auto_reconnect=True, auto_reconnect_timeout=0.5, debug=False):
        EventEmitter.__init__(self)
        self.ddpsocket = None
        self._is_closing = False
        self.url = url
        self.auto_reconnect = auto_reconnect
        self.auto_reconnect_timeout = auto_reconnect_timeout
        self.debug = debug
        self._session = None
        self._uniq_id = 0
        self._callbacks = {}
        self._init_socket()

    def _init_socket(self):
        """Initialize the ddp socket"""
        # destroy the connection if it already exists
        if self.ddpsocket:
            self.ddpsocket.remove_all_listeners('received_message')
            self.ddpsocket.remove_all_listeners('closed')
            self.ddpsocket.remove_all_listeners('opened')
            self.ddpsocket.close_connection()
            self.ddpsocket = None

{'fraction_non_alphanumeric': 0.09323646427953641, 'fraction_numerical': 0.0024217263449230237, 'mean_word_length': 2.5363914373088683, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '1487073', 'n_tokens_mistral': 1613, 'n_tokens_neox': 1583, 'n_words': 464}