<commit_before>import logging
import time
from threading import RLock
from connections import Connection

__all__ = ['ServoInterface', ]


class ServoInterface(object):

    _servoInterfaces = {}
    _globalLock = RLock()
    _interfaces = {}
    disconnected = False

    """have to do it this way to get around circular referencing in the parser"""
    @staticmethod
    def _getInterfaceClasses():
        return {
                "AX12": AX12,
                "MINISSC": MINISSC,
                "SSC32": SSC32,
                "HERKULEX": HerkuleX,
                "ROBOT": Robot,
                "HS82MG": HS82MG,
                "DUMMY": Dummy,
                "VIRTUAL": Virtual,
               }

    @staticmethod
    def getServoInterface(servo):
        with ServoInterface._globalLock:
            if servo.id not in ServoInterface._interfaces:
                if not ServoInterface.disconnected:
                    try:
                        servoIntClass = ServoInterface._getInterfaceClasses()[servo.model.name]
                    except:
                        logging.getLogger(__name__).critical("No known interface for servo model: %s", servo.model.name)
                        raise ValueError("No known interface for servo type: %s" % servo.model.name)
                    else:
                        servoInt = servoIntClass(servo)
                else:
                    servoInt = Dummy(servo)

                ServoInterface._interfaces[servo.id] = servoInt

            return ServoInterface._interfaces[servo.id]

    def __init__(self, servo):

        # servo type properties
        configs = filter(lambda c: c.model_id == servo.model_id, servo.robot.servoConfigs)
        if not configs:
            raise ValueError('Config could not be found for model %s on robot %s' % (servo.model, servo.robot))
        else:
            config = configs[0]
        self._port = config.port
        self._portSpeed = config.portSpeed

        # servo properties
        self._moving = False
        self._servoId = servo.id
        self._minPos = servo.minPosition if servo.minPosition != None else servo.model.minPosition
        self._maxPos = servo.maxPosition if servo.maxPosition != None else servo.model.maxPosition
        self._defaultPos = servo.defaultPosition if servo.defaultPosition != None else servo.model.defaultPosition
        self._minSpeed = servo.minSpeed if servo.minSpeed != None else servo.model.minSpeed
        self._maxSpeed = servo.maxSpeed if servo.maxSpeed != None else servo.model.maxSpeed
        self._defaultSpeed = servo.defaultSpeed if servo.defaultSpeed != None else servo.model.defaultSpeed
        self._posOffset = servo.positionOffset if servo.positionOffset != None else servo.model.positionOffset
        self._speedScaleValue = servo.model.speedScale
        self._posScaleValue = servo.model.positionScale
        self._tolerance = 10  # Max diff to be considered the same position

        self._logger = logging.getLogger(self.__class__.__name__)

    @property
    def servoId(self):
        return self._servoId

    def isMoving(self):
        return self._moving

    def setPositioning(self, enablePositioning):
        pass

    def getPositioning(self):
        return False

    def setPosition(self, position, speed):
        raise ValueError('Setting position not supported on servo %s' % self._servoId)

    def getPosition(self):
        raise ValueError('Getting position not supported on servo %s' % self._servoId)

    def _isInPosition(self, position):
        return abs(self._tolerance - self.getPosition()) >= self._tolerance

    def _scaleToRealPos(self, value):
        try:
            real = (float(value) / self._posScaleValue) + self._posOffset
            return round(real, 2)
        except:<commit_msg>cast init vars to floats<commit_after>import logging
import time
from threading import RLock
from connections import Connection

__all__ = ['ServoInterface', ]


class ServoInterface(object):

    _servoInterfaces = {}
    _globalLock = RLock()
    _interfaces = {}
    disconnected = False

    """have to do it this way to get around circular referencing in the parser"""
    @staticmethod
    def _getInterfaceClasses():
        return {
                "AX12": AX12,
                "MINISSC": MINISSC,
                "SSC32": SSC32,
                "HERKULEX": HerkuleX,
                "ROBOT": Robot,
                "HS82MG": HS82MG,
                "DUMMY": Dummy,
                "VIRTUAL": Virtual,
               }

    @staticmethod
    def getServoInterface(servo):
        with ServoInterface._globalLock:
            if servo.id not in ServoInterface._interfaces:
                if not ServoInterface.disconnected:
                    try:
                        servoIntClass = ServoInterface._getInterfaceClasses()[servo.model.name]
                    except:
                        logging.getLogger(__name__).critical("No known interface for servo model: %s", servo.model.name)
                        raise ValueError("No known interface for servo type: %s" % servo.model.name)
                    else:
                        servoInt = servoIntClass(servo)
                else:
                    servoInt = Dummy(servo)

                ServoInterface._interfaces[servo.id] = servoInt

            return ServoInterface._interfaces[servo.id]

    def __init__(self, servo):

        # servo type properties
        configs = filter(lambda c: c.model_id == servo.model_id, servo.robot.servoConfigs)
        if not configs:
            raise ValueError('Config could not be found for model %s on robot %s' % (servo.model, servo.robot))
        else:
            config = configs[0]
        self._port = config.port
        self._portSpeed = config.portSpeed

        # servo properties
        self._moving = False
        self._servoId = servo.id
        self._minPos = float(servo.minPosition if servo.minPosition != None else servo.model.minPosition)
        self._maxPos = float(servo.maxPosition if servo.maxPosition != None else servo.model.maxPosition)
        self._defaultPos = float(servo.defaultPosition if servo.defaultPosition != None else servo.model.defaultPosition)
        self._minSpeed = float(servo.minSpeed if servo.minSpeed != None else servo.model.minSpeed)
        self._maxSpeed = float(servo.maxSpeed if servo.maxSpeed != None else servo.model.maxSpeed)
        self._defaultSpeed = float(servo.defaultSpeed if servo.defaultSpeed != None else servo.model.defaultSpeed)
        self._posOffset = float(servo.positionOffset if servo.positionOffset != None else servo.model.positionOffset)
        self._speedScaleValue = float(servo.model.speedScale)
        self._posScaleValue = float(servo.model.positionScale)
        self._tolerance = 10  # Max diff to be considered the same position

        self._logger = logging.getLogger(self.__class__.__name__)

    @property
    def servoId(self):
        return self._servoId

    def isMoving(self):
        return self._moving

    def setPositioning(self, enablePositioning):
        pass

    def getPositioning(self):
        return False

    def setPosition(self, position, speed):
        raise ValueError('Setting position not supported on servo %s' % self._servoId)

    def getPosition(self):
        raise ValueError('Getting position not supported on servo %s' % self._servoId)

    def _isInPosition(self, position):
        return abs(self._tolerance - self.getPosition()) >= self._tolerance

    def _scaleToRealPos(self, value):
        try:
            real = (float(value) / self._posScaleValue) + self._posOffset
            return round(real, 2)
        except:
{'fraction_non_alphanumeric': 0.08422147127631921, 'fraction_numerical': 0.004159085001299714, 'mean_word_length': 2.2318353632927344, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 5, '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': '5815853', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2018, 'n_words': 537}