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,0is 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,0is 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,0is returned.- Parameters:
bits – A non-negative
int.
- class turboctl.telegram.datatypes.Data¶
Bases:
objectA 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 n_bytes: int¶
How many bytes are needed to store the data in the object; equal to
bitsdivided byBYTESIZEand 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,valueandbits, otherwiseFalse. Two objects with a value ofNaNare 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:
DataA 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,valueandbitswill 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 samebits.If value is a
bytesobject, the initialized object will represent the same binary data as value. Becausebytesobjects represent sequences of full bytes,bitswill beBYTESIZEmultiplied bylen(value).- Raises:
TypeError or ValueError – If value or bits have invalid types or values.
TypeError – If the bits argument is supplied when value is not an
int.
- class turboctl.telegram.datatypes.Sint(value, bits: int = 8)¶
Bases:
DataA 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 validintvalues it accepts is different.
- __bytes__()¶
Return
bytes(self).See
Uint.__bytes__()for details.
- class turboctl.telegram.datatypes.Float(value, bits=32)¶
Bases:
DataA 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
floator anint.intvalues are automatically converted into a correspondingfloatvalue.bits must always be
32; it exists as an argument only to give all__init__methods ofDatasubclasses a similar signature. Likewise, if value is aDataor abytesobject, 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 aBininstead of aFloat, becauseFloatobjects cannot have more than 32 bits.
- __getitem__(key)¶
Return
self[key].This method works like
Data.__getitem__(), except that the returned object is aBininstead of aFloat, becauseFloatobjects 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
stror anint. If it is astr, it must be composed solely of the characters'1'and'0', or be an empty string. If it is anint, it must be a valid bits bit unsigned integer, which will be automatically converted into its binary representation.If value is a
strand bits isNone, bits will be set to the length of value. If value is astrand bits is notNone, value is padded with zeroes to a length of bits. Giving bits a value that is smaller than the length of value will raise aValueError.
- __bytes__()¶
Return
bytes(self).See
Uint.__bytes__()for details.