[Tutor] Help with building bytearray arrays

Cameron Simpson cs at cskk.id.au
Sat Sep 8 21:14:59 EDT 2018


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,[])

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')

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>


More information about the Tutor mailing list