How to use the choochoo.fit.profile.BaseType function in choochoo

To help you get started, we’ve selected a few choochoo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github andrewcooke / choochoo / choochoo / fit / profile.py View on Github external
    @abstractmethod
    def parse(self, bytes, count, endian):
        raise NotImplementedError('%s: %s' % (self.__class__.__name__, self.name))


class BaseType(AbstractType):

    def __init__(self, log, name, size, func):
        super().__init__(log, name, size)
        self.__func = func

    def profile_to_internal(self, cell_contents):
        return self.__func(cell_contents)


class StructSupport(BaseType):

    def _pack_bad(self, value):
        bad = (bytearray(self.size), bytearray(self.size))
        for endian in (LITTLE, BIG):
            bytes = value
            for i in range(self.size):
                j = i if endian == LITTLE else self.size - i - 1
                bad[endian][j] = bytes & 0xff
                bytes >>= 8
        return bad

    def _is_bad(self, data, bad):
        size = len(bad)
        count = len(data) // size
        return all(bad == data[size*i:size*(i+1)] for i in range(count))
github andrewcooke / choochoo / choochoo / fit / profile.py View on Github external
bytes >>= 8
        return bad

    def _is_bad(self, data, bad):
        size = len(bad)
        count = len(data) // size
        return all(bad == data[size*i:size*(i+1)] for i in range(count))

    def _unpack(self, data, formats, bad, count, endian):
        if self._is_bad(data, bad[endian]):
            return None
        else:
            return unpack(formats[endian] % count, data[0:count * self.size])


class String(BaseType):

    def __init__(self, log, name):
        super().__init__(log, name, 1, str)

    def parse(self, bytes, count, endian):
        return [str(b''.join(unpack('%dc' % count, bytes)), encoding='utf-8')]


class Boolean(BaseType):

    def __init__(self, log, name):
        super().__init__(log, name, 1, bool)

    def parse(self, bytes, count, endian):
        return tuple(bool(byte) for byte in bytes)
github andrewcooke / choochoo / choochoo / fit / profile.py View on Github external
if self._is_bad(data, bad[endian]):
            return None
        else:
            return unpack(formats[endian] % count, data[0:count * self.size])


class String(BaseType):

    def __init__(self, log, name):
        super().__init__(log, name, 1, str)

    def parse(self, bytes, count, endian):
        return [str(b''.join(unpack('%dc' % count, bytes)), encoding='utf-8')]


class Boolean(BaseType):

    def __init__(self, log, name):
        super().__init__(log, name, 1, bool)

    def parse(self, bytes, count, endian):
        return tuple(bool(byte) for byte in bytes)


class AutoInteger(StructSupport):

    pattern = compile(r'^([su]?)int(\d{1,2})(z?)$')

    size_to_format = {1: 'b', 2: 'h', 4: 'i', 8: 'q'}

    def __init__(self, log, name):
        match = self.pattern.match(name)