[Tutor] Help with building bytearray arrays
Alan Gauld
alan.gauld at yahoo.co.uk
Sat Sep 8 06:40:55 EDT 2018
On 08/09/18 03:15, Chip Wachob wrote:
> my function's main pieces are:
It would probably be better to post the entire function,
a partial code sample like this just gives us an inkling
of what you are trying to do but not enough to be sure
of where the errors lie.
> def transfer_byte_array():
> MAX_LOOP_COUNT = 64
> slice_size = 16
> read_ary = bytearray(MAX_LOOP_COUNT)
> scratch_ary = bytearray()
>
> for step in range (0, MAX_LOOP_COUNT, slice_size):
> scratch_ary = transfer(data_to_send, slice_size)
You pass in two arguments here but in your description
of transfer below it is a method of an object that
only takes one (self being the object). Something is
broken here.
I would expect, given the code below, to see something
like
scratch_ary = someObject.transfer(data_to_send)
> for bytes in range (0, slice_size):
> read_ary = scratch_ary[bytes]
This is replacing rad_ary each time with a single value from scratch_ary.
At the end of theloop read_ary wil;l cpontain the last valuye in
scratch_ary. You can achieve the same result without a loop:
raed_ary = scratch_ary[-1]
But I suspect that's not what you want.
I think what you really want is the first
slice_size elements of scratch_ary:
read_ary = scratch_ary[:slice_size] # use slicing
> 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,[])
> def transfer(self, data):
The self parameter indicates that this is a method
definition from a class. That implies that to use
it you must create an instance of the class and
call the method on that instance.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list