[Tutor] Help with building bytearray arrays
Chip Wachob
wachobc at gmail.com
Mon Sep 10 14:15:43 EDT 2018
Peter,
I see that clue "[[".
The thread history pretty much sums up what is going on up to this point.
I'll cover it once more:
I'm using Adafruit FT232H Breakout board and Adafruit's library.
https://github.com/adafruit/Adafruit_Python_GPIO
Per Adafruit's example code, I create an SPI interface:
https://learn.adafruit.com/adafruit-ft232h-breakout?view=all
I can access this interface with the spi.transfer() spi.write() and
spi.read() functions.
The transfer() function (see link above) accepts and returns bytearray
objects data.
My application requires me to send a large payload down the SPI port
to my circuit, and read back the same in full duplex.
Due to a limitation of the part, I can't send my whole bytearray at
one time. It crashes out.
So, my solution was to send as large of a chunk of data at one time as
I can inside a loop until I get to the end of the data to be sent.
Meanwhile, the transfer() function is returning 'chunks' of bytearrays.
Earlier discussions here indicated that the best way was to :
results = []
for i in range (0, slice_size):
results.append(transfer(data_out))
Then, concatenate the results into a long bytearray so I can use it
elsewhere in my code.
all_together = bytearray().join(results)
I _thought_ that this was going to create a concatenated list of all
the returned results:
all_together = [results[0] + results[1] + results [2] + results[3]]
-- for example
but, as you point out, I got:
all_together = [[results[0]], [results[1]], [results[2]]. [results[3]]
-- I'm sure that the brackets and braces are not syntactically
correct, but I think you get the idea.
So I see why my .join() isn't working. I'm not sure how to fix it though.
Related to this, but I'm not yet at that point until I get this
resolved, I need to walk through the all_together data and find where
the data changes from one value to another..
My background is in C and other 'historical' languages, so I'm trying
to get a hold of the way Python handles arrays, which is different
that the way I've thought for 20+ years.. :)
I hope this helps.
I'm beginning to wonder if Python was the right choice for this
project.. but it's too late for me to switch now.
Thanks to everyone for your comments and patience.
On Mon, Sep 10, 2018 at 1:42 PM, Peter Otten <__peter__ at web.de> wrote:
> Chip Wachob wrote:
>
>> Cameron,
>>
>> Thank you again for the insight.
>>
>> Yes, data_out is an equivalently-sized 'chunk' of a larger array.
>>
>> I'm 'getting' this now..
>>
>> So, without all the fluff associated with wiggling lines, my function
>> now looks like this:
>>
>> def RSI_size_the_loop():
>> results = []
>> all_together = [] # not certain if I need this, put it in in an
>> attempt to fix the incompatibility if it existed
>>
>> for x in range (0, MAX_LOOP_COUNT, slice_size):
>> results.append(my_transfer(disp, data_out, slice_size)
>>
>> print " results ", x, " = ", results # show how results grows
>> on each iteration
>>
>> all_together = bytearray().join(results)
>>
>> print " all together ", all_together
>>
>>
>> I can observe results increasing in size and the last time through the
>> loop:
>>
>> results 48 =
>>
> [[bytearray(b'\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')],
>
> Note that there are two '[' at the start of the list. This means that the
> first list item is another list. In fact you seem to have a list of single
> item lists like
>
> [["foo"], ["bar"], ...]
>
> when you need
>
> ["foo", "bar", ...]
>
> Of course join will fail with that:
>
>>>> "".join(["foo", "bar"])
> 'foobar'
>>>> "".join([["foo"], ["bar"]])
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: sequence item 0: expected string, list found
>
> I think the error message is pretty clear ;)
>
> Have a look into your my_transfer() function to find out why it returns a
> list (or show us the code).
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list