<commit_before>""" Actor proxy for rodario framework """

# stdlib
import inspect
import types
import pickle
from multiprocessing import Queue
from threading import Thread
from uuid import uuid4
from time import sleep

# 3rd party
import redis


class ActorProxy(object):  # pylint: disable=I0011,R0903

    """ Proxy object that fires calls to an actor over redis pubsub """

    def __init__(self, actor=None, uuid=None):
        """
        Initialize instance of ActorProxy.

        Accepts either an Actor object to clone or a UUID, but not both.

        :param rodario.actors.Actor actor: Actor to clone
        :param str uuid: UUID of Actor to clone
        """

        #: Redis connection
        self._redis = redis.StrictRedis()
        #: Redis PubSub client
        self._pubsub = None
        #: This proxy object's UUID for creating unique channels
        self.proxyid = str(uuid4())
        #: Dict of response queues for sandboxing method calls
        self._response_queues = {}

        # avoid cyclic import
        actor_module = __import__('rodario.actors', fromlist=('Actor',))
        # pylint: disable=I0011,E1123
        self._pubsub = self._redis.pubsub(ignore_subscribe_messages=True)
        self._pubsub.subscribe(**{'proxy:%s' % self.proxyid: self._handler})

        methods = []

        def pubsub_thread():
            """ Call get_message in loop to fire _handler. """

            while True and self._pubsub:
                self._pubsub.get_message()
                sleep(0.01)

        # fire up the message handler thread as a daemon
        proc = Thread(target=pubsub_thread)
        proc.daemon = True
        proc.start()

        if isinstance(actor, actor_module.Actor):
            # proxying an Actor directly
            self.uuid = actor.uuid
            methods = inspect.getmembers(actor, predicate=inspect.ismethod)
        elif isinstance(uuid, str):
            # proxying by UUID; get actor methods over pubsub
            self.uuid = uuid
            pre_methods = self._proxy('_get_methods').get()

            for name in pre_methods:
                methods.append((name, None,))
        else:
            raise Exception('No actor or UUID provided')

        def get_lambda(name):
            """<commit_msg>eliminate unnecessary while condition in message handler loop<commit_after>""" Actor proxy for rodario framework """

# stdlib
import inspect
import types
import pickle
from multiprocessing import Queue
from threading import Thread
from uuid import uuid4
from time import sleep

# 3rd party
import redis


class ActorProxy(object):  # pylint: disable=I0011,R0903

    """ Proxy object that fires calls to an actor over redis pubsub """

    def __init__(self, actor=None, uuid=None):
        """
        Initialize instance of ActorProxy.

        Accepts either an Actor object to clone or a UUID, but not both.

        :param rodario.actors.Actor actor: Actor to clone
        :param str uuid: UUID of Actor to clone
        """

        #: Redis connection
        self._redis = redis.StrictRedis()
        #: Redis PubSub client
        self._pubsub = None
        #: This proxy object's UUID for creating unique channels
        self.proxyid = str(uuid4())
        #: Dict of response queues for sandboxing method calls
        self._response_queues = {}

        # avoid cyclic import
        actor_module = __import__('rodario.actors', fromlist=('Actor',))
        # pylint: disable=I0011,E1123
        self._pubsub = self._redis.pubsub(ignore_subscribe_messages=True)
        self._pubsub.subscribe(**{'proxy:%s' % self.proxyid: self._handler})

        methods = []

        def pubsub_thread():
            """ Call get_message in loop to fire _handler. """

            while self._pubsub:
                self._pubsub.get_message()
                sleep(0.01)

        # fire up the message handler thread as a daemon
        proc = Thread(target=pubsub_thread)
        proc.daemon = True
        proc.start()

        if isinstance(actor, actor_module.Actor):
            # proxying an Actor directly
            self.uuid = actor.uuid
            methods = inspect.getmembers(actor, predicate=inspect.ismethod)
        elif isinstance(uuid, str):
            # proxying by UUID; get actor methods over pubsub
            self.uuid = uuid
            pre_methods = self._proxy('_get_methods').get()

            for name in pre_methods:
                methods.append((name, None,))
        else:
            raise Exception('No actor or UUID provided')

        def get_lambda(name):
            """
{'fraction_non_alphanumeric': 0.08585748529091305, 'fraction_numerical': 0.009588145565482676, 'mean_word_length': 2.25531914893617, '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': '82502', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1221, 'n_words': 397}