<commit_before>try:
  from queue import Queue
except ImportError:
  from Queue import Queue
from mirobot.socket_handler import SocketHandler
import time
import string
import random

_sentinel = object()

class Mirobot:
  def __init__(self, address, debug = False):
    self.__send_q = Queue()
    self.recv_q = Queue()
    self.socket = SocketHandler(address, self.__send_q, self.recv_q, debug=debug, sentinel = _sentinel)
    self.nonce  = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(4))
    self.n      = 0
    self.socket.start()

  def forward(self, distance):
    self.__send({'cmd':'forward', 'arg':str(distance)}, distance/20)

  def back(self, distance):
    self.__send({'cmd':'back', 'arg':str(distance)}, distance/20)

  def left(self, degrees):
    self.__send({'cmd':'left', 'arg':str(degrees)}, degrees/20)

  def right(self, degrees):
    self.__send({'cmd':'right', 'arg':str(degrees)}, degrees/20)

  def penup(self):
    self.__send({'cmd':'penup'}, 1)

  def pendown(self):
    self.__send({'cmd':'pendown'}, 1)

  def beep(self, milliseconds):
    self.__send({'cmd':'beep', 'arg':str(milliseconds)}, milliseconds / 500)
  
  def disconnect(self):
    self.__send_q.put(_sentinel)
  
  def __send(self, msg, timeout):
    msg['id'] = self.generate_id()
    self.__send_q.put(msg)
    timeout += time.time()
    while True:
      if self.recv_q.qsize() > 0:
        incoming = self.recv_q.get()
        if incoming['status'] == 'complete' and incoming['id'] == msg['id']:
          return
      if time.time() >= timeout:
        raise IOError("Mirobot timed out")
      time.sleep(0.05)

  def generate_id(self):
    self.n = (self.n + 1) % 0x10000
    return '%s%04x' % (self.nonce, self.n)<commit_msg>'fix' timeout for short commands<commit_after>try:
  from queue import Queue
except ImportError:
  from Queue import Queue
from mirobot.socket_handler import SocketHandler
import time
import string
import random

_sentinel = object()

class Mirobot:
  def __init__(self, address, debug = False):
    self._debug = debug
    self.__send_q = Queue()
    self.recv_q = Queue()
    self.socket = SocketHandler(address, self.__send_q, self.recv_q, debug=debug, sentinel = _sentinel)
    self.nonce  = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(4))
    self.n      = 0
    self.socket.start()

  def forward(self, distance):
    self.__send({'cmd':'forward', 'arg':str(distance)}, distance/20)

  def back(self, distance):
    self.__send({'cmd':'back', 'arg':str(distance)}, distance/20)

  def left(self, degrees):
    self.__send({'cmd':'left', 'arg':str(degrees)}, degrees/20)

  def right(self, degrees):
    self.__send({'cmd':'right', 'arg':str(degrees)}, degrees/20)

  def penup(self):
    self.__send({'cmd':'penup'}, 1)

  def pendown(self):
    self.__send({'cmd':'pendown'}, 1)

  def beep(self, milliseconds):
    self.__send({'cmd':'beep', 'arg':str(milliseconds)}, milliseconds / 500)

  def disconnect(self):
    self.__send_q.put(_sentinel)

  def __send(self, msg, timeout):
    msg['id'] = self.generate_id()
    if self._debug:
      print('<< %r' % msg)
    self.__send_q.put(msg)
    deadline = max(timeout, 1) + time.time()
    while True:
      if self.recv_q.qsize() > 0:
        incoming = self.recv_q.get()
        if self._debug:
          print(incoming)
        if incoming['status'] == 'complete' and incoming['id'] == msg['id']:
          return
      if time.time() >= deadline:
        raise IOError("Mirobot timed out")
      time.sleep(0.05)

  def generate_id(self):
    self.n = (self.n + 1) % 0x10000
    return '%s%04x' % (self.nonce, self.n)
{'fraction_non_alphanumeric': 0.16352716352716354, 'fraction_numerical': 0.015561015561015561, 'mean_word_length': 3.5515527950310557, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '875660', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1241, 'n_words': 281}