[Tutor] Help with building bytearray arrays
Chip Wachob
wachobc at gmail.com
Sun Sep 9 23:00:45 EDT 2018
On Sat, Sep 8, 2018 at 9:14 PM, Cameron Simpson <cs at cskk.id.au> wrote:
> On 08Sep2018 11:40, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>>
>> On 08/09/18 03:15, Chip Wachob wrote:
>>>
>>> Ideally, I'd like to take the slice_size chunks that have been read
>>> and concatenate them back togetjer into a long MAX_LOOP_COUNT size
>>> array to pass back to the rest of my code. Eg:
>>
>>
>> You need to create a list of read_ary
>>
>> results = []
>>
>> then after creating each read_ary value append it
>> to results.
>>
>> results.append(read_ary)
>>
>> Then, at the very end, return the summation of all
>> the lists in results.
>>
>> return sum(results,[])
>
Cameron is correct (Gold Star for you Cameron)
>
> Actually he's getting back bytearray instances from transfer and wants to
> join them up (his function does a few small transfers to work around an
> issue with one big transfer). His earlier code is just confused. So he
> wants:
>
> bytearray().join(results)
>
> Hacked example:
>
> >>> bytearray().join( (bytearray("foo"),bytearray("bah")) )
> bytearray(b'foobah')
I understand this example and I can replicate it in the interpreter..
But, I'm still missing something here.
I presume that I need to instantiate an array of slice_size-sized bytearrays.
So, when I'm looping through, I can do:
for i in range (0, slice_count):
results[i] = spi.transfer(data_out)
Then I can :
all_together = butearray().join(results)
But I can't seem to be able to find the proper syntax to create the
initial array.
And any attempt at filling the arrays to test some stand-alone code
only give me errors.
Here's my code (all of it)
#
#
#
import sys
slice_size = 16 # in bytes
MAX_LOOP_COUNT = 64 # in bytes
slice_count = MAX_LOOP_COUNT / slice_size
print " slice size = ", slice_size
print " MLC = ", MAX_LOOP_COUNT
print " slice count = ", slice_count
#results = bytearray( (bytearray(slice_size)*slice_count) )
results = bytearray(slice_size) # why isn't this 16 bytes long?
res_list = (results)*slice_count
print " results ", [results]
print " results size ", sys.getsizeof(results)
print " res_list ", [res_list]
print " res_list size ", sys.getsizeof(res_list)
print " type = ", type(results)
results[0] = ([1,3,5,7,9])
all_together = bytearray().join(results)
But I'm getting:
$ python merge_1.py
slice size = 16
MLC = 64
slice count = 4
results [bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')]
results size 65
res_list [bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')]
res_list size 113
type = <type 'bytearray'>
Traceback (most recent call last):
File "merge_1.py", line 27, in <module>
results[0] = ([1,3,5,7,9])
TypeError: an integer or string of size 1 is required
Again, I feel like I'm circling the target, but not able to divine the
proper syntax
I hope this makes sense.
>
> And he's working with bytearrays because the target library is Python 2,
> where there's no bytes type.
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
>
> _______________________________________________
> 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