<commit_before># -*- coding: UTF-8 -*-

# Imports
import scarlett
#import thread
import threading
import time
import redis
import logging

from transitions import Machine

SCARLETT_ROLE = 'brain'

BRAIN_NAME = 'fsm'
CORE_OBJECT = 'ScarlettBrainFSM'

_INSTANCE = None

logging.basicConfig(level=logging.DEBUG,
                    format='(%(threadName)-9s) %(message)s',)

def setup_core(ss):
    print scarlett.config.get('redis', 'host')
    print scarlett.config.get('redis', 'port')
    print scarlett.config.getint('redis', 'db')

    global _INSTANCE
    if _INSTANCE == None:
        _INSTANCE = ScarlettBrainFSM(host=scarlett.config.get('redis', 'host'),
          port=scarlett.config.get('redis', 'port'),
          db=scarlett.config.getint('redis', 'db')
        )

    return _INSTANCE

class ScarlettBrainFSM(redis.Redis):
  """
  Wrapper for Redis buffered commands that uses a pipeline internally
  for buffering messages. A thread is run that
  periodically flushes the buffer pipeline.
  """
  states = ['initalize', 'ready','running', 'is_checking_states', 'time_change', 'done_checking_states']

  def __init__(self, *args, **kwargs):
    """ Constructor

    :type host: string
    :param host: ip address or dns name of redis server

    :type port: int
    :param port: port that redis is running on

    :type db: int
    :param db: default redis db
    """
    super(ScarlettBrainFSM, self).__init__(*args, **kwargs)

    self.name = scarlett.brain.scarlettbrainfsm.SCARLETT_ROLE

    # Initalize the state machine
    self.machine = Machine(model=self, states=scarlett.brain.scarlettbrainfsm.ScarlettBrainFSM.states, initial='initalize')

    # startup transition
    self.machine.add_transition(trigger='startup', source='initalize', dest='ready')

    # checking_states transition
    self.machine.add_transition(trigger='checking_states', source='ready', dest='is_checking_states', conditions=['is_ready'])

    # array / dict of state machines connected to scarlett
    self._machines = {}

    # Check interval, in seconds
    self.interval = 1

  def run(self):
      """ Method that runs forever """
      while True:
          # Do something
          print('Doing something imporant in the background')

          time.sleep(self.interval)

  def thread_runner(self):
    """ Call this function when we're ready to start this thread  """
    # function to run in background, self.run
    thread = threading.Thread(target=self.run, args=())
    thread.daemon = True                            # Daemonize thread
    thread.start()                                  # Start the execution

  def hello(self):
    print 'hello hello hello!'

  def is_ready(self):
    """ Ensures that  """
    if self.state == 'ready':
      return True
    else:
      return False
<commit_msg>chg: more logging for threads in brainfsm<commit_after># -*- coding: UTF-8 -*-

# Imports
import scarlett
#import thread
import threading
import time
import redis
import logging

from transitions import Machine

SCARLETT_ROLE = 'brain'

BRAIN_NAME = 'fsm'
CORE_OBJECT = 'ScarlettBrainFSM'

_INSTANCE = None

logging.basicConfig(level=logging.DEBUG,
                    format='(%(threadName)-9s) %(message)s',)

def setup_core(ss):

    logging.info("redis_host: {}".format(scarlett.config.get('redis', 'host')))
    logging.info("redis_port: {}".format(scarlett.config.get('redis', 'port')))
    logging.info("redis_db: {}".format(scarlett.config.getint('redis', 'db')))

    global _INSTANCE

    if _INSTANCE == None:
        _INSTANCE = ScarlettBrainFSM(host=scarlett.config.get('redis', 'host'),
          port=scarlett.config.get('redis', 'port'),
          db=scarlett.config.getint('redis', 'db')
        )

    return _INSTANCE

class ScarlettBrainFSM(redis.Redis):
  """
  Wrapper for Redis buffered commands that uses a pipeline internally
  for buffering messages. A thread is run that
  periodically flushes the buffer pipeline.
  """
  states = ['initalize', 'ready','running', 'is_checking_states', 'time_change', 'done_checking_states']

  def __init__(self, *args, **kwargs):
    """ Constructor

    :type host: string
    :param host: ip address or dns name of redis server

    :type port: int
    :param port: port that redis is running on

    :type db: int
    :param db: default redis db
    """
    super(ScarlettBrainFSM, self).__init__(*args, **kwargs)

    self.name = scarlett.brain.scarlettbrainfsm.SCARLETT_ROLE

    # Initalize the state machine
    self.machine = Machine(model=self, states=scarlett.brain.scarlettbrainfsm.ScarlettBrainFSM.states, initial='initalize')

    # startup transition
    self.machine.add_transition(trigger='startup', source='initalize', dest='ready')

    # checking_states transition
    self.machine.add_transition(trigger='checking_states', source='ready', dest='is_checking_states', conditions=['is_ready'])

    # array / dict of state machines connected to scarlett
    self._machines = {}

    # Check interval, in seconds
    self.interval = 1

  def run(self):
      """ Method that runs forever """
      logging.debug('running with %s and %s', self.args, self.kwargs)
      while True:
          # Do something
          print('Doing something imporant in the background')

          time.sleep(self.interval)

  def thread_runner(self):
    """ Call this function when we're ready to start this thread  """
    # function to run in background, self.run
    thread = threading.Thread(target=self.run, args=())
    thread.daemon = True                            # Daemonize thread
    thread.start()                                  # Start the execution

  def hello(self):
    print 'hello hello hello!'

  def is_ready(self):
    """ Ensures that  """
    if self.state == 'ready':
      return True
    else:
      return False

{'fraction_non_alphanumeric': 0.11749526922415277, 'fraction_numerical': 0.001032169275761225, 'mean_word_length': 3.2222222222222223, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '346084', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1741, 'n_words': 467}