<commit_before># racket.py
# aoneill - 04/24/17

from libs import psmoveapi

class SwingState(object):
  '''
  Enum-like object to store the swing state
  '''
  IDLE = 0
  BACKSWING = 1
  TRANSITION = 2
  SWING = 3

class Handedness(object):
  '''
  Enum-like object to store the handedness of the swing
  '''
  LEFT = 0
  RIGHT = 1

class Racket(psmoveapi.PSMoveAPI):
  '''
  Wrapper around the PSMoveAPI base class to give racket-based callbacks,
  rather than lower-level interactions.
  '''

  # Thresholds for swing strength
  SWING_SIZE = 1.5
  SWING_PAUSE = 0.75

  def __init__(self):
    super(Racket, self).__init__()

    # Initialize internal state mahine
    self._state = SwingState.IDLE
    self._hand = Handedness.RIGHT

  def _step_backswing(self, controller):
    # Transition from IDLE to BACKSWING if it makes sense
    if(self._state == SwingState.IDLE):
      if(controller.accelerometer.length() > Racket.SWING_SIZE):
        self._state = SwingState.BACKSWING

        # Assume that the hand stays the same throughout the swing
        # It would be awkward for the player otherwise
        self._hand = (
            Handedness.LEFT if(controller.gyroscope.z > 0) else Handedness.RIGHT
          )

        # Call our callback if defined
        (self.on_backswing or (lambda *args: None))(controller, self._hand)

  def _step_transition(self, controller):
    # Transition from BACKSWING to TRANSITION if it makes sense
    if(self._state == SwingState.BACKSWING):
      if(controller.accelerometer.length() < Racket.SWING_PAUSE):
        self._state = SwingState.TRANSITION

        # Call our callback if defined
        (self.on_transition or (lambda *args: None))(controller, self._hand)

  def _step_swing(self, controller):
    # Transition from TRANSITION to SWING if it makes sense
    if(self._state == SwingState.TRANSITION):
      if(controller.accelerometer.length() > Racket.SWING_SIZE):
        self._state = SwingState.SWING

        # Call our callback if defined
        (self.on_swing or (lambda *args: None))(controller, self._hand)

  def _step_idle(self, controller):
    # Transition from SWING to IDLE if it makes sense
    if(self._state == SwingState.SWING):
      if(controller.accelerometer.length() < Racket.SWING_PAUSE):
        self._state = SwingState.IDLE

        # Call our callback if defined
        (self.on_idle or (lambda *args: None))(controller, self._hand)

  def _step_state(self, controller):
    # Step through the states
    self._step_backswing(controller)
    self._step_transition(controller)
    self._step_swing(controller)
    self._step_idle(controller)

  def on_connect(self, controller):
    # Call our callback if defined
    (self.on_init or (lambda *args: None))(controller)

  def on_disconnect(self, controller):
    # Call our callback if defined
    (self.on_leave or (lambda *args: None))(controller)

  def on_update(self, controller):
    self._step_state(controller)

    # Call our callback if defined
    (self.on_refresh or (lambda *args: None))(controller)<commit_msg>Refined racket base class<commit_after># racket.py
# aoneill - 04/24/17

from libs import psmoveapi

class SwingState(object):
  '''
  Enum-like object to store the swing state
  '''
  IDLE = 0
  BACKSWING = 1
  SWING = 2

class Handedness(object):
  '''
  Enum-like object to store the handedness of the swing
  '''
  LEFT = 0
  RIGHT = 1

class Racket(psmoveapi.PSMoveAPI):
  '''
  Wrapper around the PSMoveAPI base class to give racket-based callbacks,
  rather than lower-level interactions.
  '''

  # Thresholds for swing strength
  SWING_PAUSE = 1.5
  TRIGGER_BACK = 0.5

  def __init__(self):
    super(Racket, self).__init__()

    # Initialize internal state mahine
    self._state = SwingState.IDLE
    self._hand = Handedness.RIGHT

  def _step_backswing(self, controller):
    # Transition from IDLE to BACKSWING if it makes sense
    if(self._state == SwingState.IDLE):
      if(controller.accelerometer.length() > Racket.SWING_PAUSE):
        if(controller.trigger > Racket.TRIGGER_BACK):
          self._state = SwingState.BACKSWING

          # Assume that the hand stays the same throughout the swing
          # It would be awkward for the player otherwise
          self._hand = (
              Handedness.LEFT if(controller.gyroscope.z > 0)
                else Handedness.RIGHT
            )

          # Call our callback if defined
          (self.on_backswing or (lambda *args: None))(controller, self._hand)

  def _step_swing(self, controller):
    # Transition from TRANSITION to SWING if it makes sense
    if(self._state == SwingState.BACKSWING):
      if(controller.accelerometer.length() > Racket.SWING_PAUSE):
        if(controller.trigger < Racket.TRIGGER_BACK):
          self._state = SwingState.SWING

          # Call our callback if defined
          (self.on_swing or (lambda *args: None))(controller, self._hand)

  def _step_idle(self, controller):
    # Transition from SWING to IDLE if it makes sense
    if(self._state == SwingState.SWING):
      if(controller.accelerometer.length() < Racket.SWING_PAUSE):
        self._state = SwingState.IDLE

        # Call our callback if defined
        (self.on_idle or (lambda *args: None))(controller, self._hand)

  def _step_state(self, controller):
    # Step through the states
    self._step_backswing(controller)
    self._step_swing(controller)
    self._step_idle(controller)

  def on_connect(self, controller):
    # Call our callback if defined
    (self.on_init or (lambda *args: None))(controller)

  def on_disconnect(self, controller):
    # Call our callback if defined
    (self.on_leave or (lambda *args: None))(controller)

  def on_update(self, controller):
    self._step_state(controller)

    # Call our callback if defined
    (self.on_refresh or (lambda *args: None))(controller)
{'fraction_non_alphanumeric': 0.0939207650273224, 'fraction_numerical': 0.005806010928961749, 'mean_word_length': 3.1421499292786423, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '3355292', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1779, 'n_words': 508}