datatypes

This module defines classes representing different data types used in telegrams.

turboctl.telegram.datatypes.BYTESIZE = 8

The number of bits in a byte.

turboctl.telegram.datatypes.maxuint(bits)

Return the largest unsigned integer that can be expressed with bits bits.

If bits is 0, 0 is returned.

Parameters:

bits – A non-negative int.

turboctl.telegram.datatypes.maxsint(bits)

Return the largest (i.e. most positive) signed integer that can be expressed with bits bits.

If bits is 0, 0 is returned.

Parameters:

bits – A non-negative int.

turboctl.telegram.datatypes.minsint(bits)

Return the smallest (i.e. most negative) signed integer that can be expressed with bits bits.

If bits is 0, 0 is returned.

Parameters:

bits – A non-negative int.

class turboctl.telegram.datatypes.Data

Bases: object

A superclass for all telegram data types.

This class is not meant to be initialized, since it lacks an __init__ method.

property value

The value represented by this data object. This is a read-only property.

property bits: int

How many bits of data this object represents. This is a read-only property.

property n_bytes: int

How many bytes are needed to store the data in the object; equal to bits divided by BYTESIZE and rounded up.

__add__(other)

Return self + other.

Appends the binary data in other to the end of self and returns an object of the same class as self with the combined binary data.

Example: Bin('010') + Bin('001') = Bin('010001')

__eq__(other)

Return self == other.

Returns True, if self and other have the same type, value and bits, otherwise False. Two objects with a value of NaN are equal, if their types and bits match.

__repr__()

Return repr(self).

The returned string uses the format 'ClassName(<value>, bits=<bits>).'

__getitem__(key)

Return self[key].

This method uses key to get an index or a slice of the binary data in self, and returns an object of the same class as self containing the binary data in the index or slice.

Example

>>> Bin('010001')[0:3]
Bin('010', bits=3)
__hash__ = None
class turboctl.telegram.datatypes.Uint(value, bits: int = 8)

Bases: Data

A data type for unsigned integers.

__init__(value, bits: int = 8)
__init__(value: int, bits: int = 8)
__init__(value: Data)
__init__(value: bytes)

Initialize a new Uint.

This method may be called with any of the following signatures:

Sint(value: int, bits: int=8)
Sint(value: Data)
Sint(value: bytes)

If value is an int, value and bits will be set to the values given as arguments.

If value is an instance of a subclass of Data, the initialized object will represent exactly the same binary data as value and have the same bits.

If value is a bytes object, the initialized object will represent the same binary data as value. Because bytes objects represent sequences of full bytes, bits will be BYTESIZE multiplied by len(value).

Raises:
__bytes__()

Return bytes(self).

Returns the binary data represented by this object as a bytes object with a length of n_bytes.

class turboctl.telegram.datatypes.Sint(value, bits: int = 8)

Bases: Data

A data type for signed integers formed with the two’s complement method.

__init__(value, bits: int = 8)
__init__(value: int, bits: int = 8)
__init__(value: Data)
__init__(value: bytes)

Initialize a new Sint.

This method works like Uint.__init__(), except that the range of valid int values it accepts is different.

__bytes__()

Return bytes(self).

See Uint.__bytes__() for details.

class turboctl.telegram.datatypes.Float(value, bits=32)

Bases: Data

A data type for IEEE 754 single-precision floating point numbers.

__init__(value, bits=32)
__init__(value: float, bits: int = 32)
__init__(value: int, bits: int = 32)
__init__(value: Data)
__init__(value: bytes)

Initialize a new Float.

This method may be called with any of the following signatures:

Float(value: float, bits: int=32)
Float(value: int, bits: int=32)
Float(value: Data)
Float(value: bytes)

The method works like Uint.__init__(), with the following exceptions:

  • If value is given as a number, it can be either a float or an int. int values are automatically converted into a corresponding float value.

  • bits must always be 32; it exists as an argument only to give all __init__ methods of Data subclasses a similar signature. Likewise, if value is a Data or a bytes object, it must contain exactly 32 bits of data.

Raises:

ValueError – If bits is not 32, or value can’t be expressed in 32 bits of data.

__bytes__()

Return bytes(self).

See Uint.__bytes__() for details.

__add__(other)

Return self + other.

This method works like Data.__add__(), except that the returned object is a Bin instead of a Float, because Float objects cannot have more than 32 bits.

__getitem__(key)

Return self[key].

This method works like Data.__getitem__(), except that the returned object is a Bin instead of a Float, because Float objects cannot have less than 32 bits.

class turboctl.telegram.datatypes.Bin(value, bits: Optional[int] = None)

Bases: Data

__init__(value, bits: Optional[int] = None)
__init__(value: str, bits: Optional[int] = None)
__init__(value: int, bits: int = 8)
__init__(value: Data)
__init__(value: bytes)

Initialize a new Bin.

This method may be called with any of the following signatures:

Bin(value: str, bits: Optional[int]=None)
Bin(value: int, bits: int=8)
Bin(value: Data)
Bin(value: bytes)

The method works like Uint.__init__(), with the following exceptions:

  • If value is specified directly, it can be either a str or an int. If it is a str, it must be composed solely of the characters '1' and '0', or be an empty string. If it is an int, it must be a valid bits bit unsigned integer, which will be automatically converted into its binary representation.

  • If value is a str and bits is None, bits will be set to the length of value. If value is a str and bits is not None, value is padded with zeroes to a length of bits. Giving bits a value that is smaller than the length of value will raise a ValueError.

__bytes__()

Return bytes(self).

See Uint.__bytes__() for details.