0

I'm getting this 'TypeError: object of type 'int' has no len()' for a data conversion function that I am trying to integrating into a class. Outside of the class it works with no errors, put it into the class, or try to access it inside of the class, and I'm getting the same error.

The program I'm writing sends int/float type data between two rf/lora nodes. (This is just a portion of a larger telemetry program) I am converting the data to byte arrays using struct.pack() in order to send them. Code is written in CircuitPython using Adafruit hardware (Feather m4 & lora breakout board) and libraries.

Any suggestion on how to fix the issue or what's causing it would be greatly appreciated!

Code Below:

class TransmitState(State):

def __init__(self):
    super().__init__()
    self.entered = time.monotonic()
    self.rf = adafruit_rfm9x.RFM9x(spi, cs, reset, radio_freq)
    self.rf.tx_power = 23
    self.rf.enable_crc = True
    self.rf.node = 1
    self.rf.destination = 2
    led.value = True

# converts data to byte array - line error on ba = ...
def pack_data(self, data_to_pack):
    # First encode the number of data items, then the actual items
    ba = struct.pack("!I" + "d" * len(data_to_pack),     #    <--- Line that's causing the error
                len(data_to_pack), *data_to_pack)
    return ba

# sendData sends the converted data to our 2nd node
def sendData(self, data):
# checks to see if data is null
    if data:
        ts = self.pack_data(data)     #     <--- Call to convert fcn
        self.rf.send(ts)   
        

@property
def name(self):
    return 'transmit'

def enter(self, machine):
    State.enter(self, machine)
    self.entered = time.monotonic()
    led.value = True
    print("Entering Transmit")

def exit(self, machine):
    print("Exiting Transmit")
    State.exit(self, machine)

def update(self, machine):
   
    if State.update(self, machine):
        now = time.monotonic()
        if now - self.entered >= 1.0:
            led.value = False
            print("Transmitting...")
            self.sendData(3) #            <-- FCN Call, 3 is arbitrary 
            machine.go_to_state('idle')

Note: Import and pin assignments not shown

1 Answers1

0

That error is because you are trying to get the length of an int.

len() attribute is only for iterable types like dict, list, strings,sets and tuples.

Try :

ba = struct.pack("!I" + "d" * len(str(data_to_pack)), len(str(data_to_pack)), *data_to_pack):

What this will do is convert the data into string and then get the length.

  • Thank you Mukesh. I tried your suggestion, it's now showing another error, "TypeError: 'int' object is not iterable." While your answer makes sense, I don't believe that's the issue, because this function does work outside of the class. I had previously wrote the program just to send data without any classes, but now need to integrate it into a larger project and am running into errors. – Sum-one Apr 01 '21 at 18:39
  • please you provide traceback of the error. – Mukesh Tandale Apr 02 '21 at 04:14
  • Sorry for the late response, but totally! Here tis. Traceback (most recent call last): File "code.py", line 413, in File "code.py", line 112, in update File "code.py", line 348, in update File "code.py", line 323, in sendData File "code.py", line 315, in pack_data TypeError: object of type 'int' has no len() – Sum-one Apr 02 '21 at 23:17
  • You know what, I think you're right. I have always tested code before using an array, and not an individual float/int. Sorry to bother with such a simple issue / explanation. – Sum-one Apr 02 '21 at 23:43
  • it seems that you didn't use the whole snippet i gave. If `data_to_pack` is converted to str, then there should be no error like above. – Mukesh Tandale Apr 03 '21 at 04:36