[Tutor] Help with building bytearray arrays

Alan Gauld alan.gauld at yahoo.co.uk
Mon Sep 10 06:44:05 EDT 2018


On 10/09/18 04:00, Chip Wachob wrote:

> I presume that I need to instantiate an array of slice_size-sized bytearrays.

Cameron has already addressed this and explained
that you don't need to and if you did how to do it.

I'd only add that you need to readjust your thinking
when it comes to Python data structures versus C.
In C data structures are very primitive (little more
than an allocated chunk of memory).

In Python data structures are extremely rich and indeed
much of Python's power comes from exploiting the
richness of the standard data structures. It is well
worth while just playing around with these in the >>>
prompt using dir() and help() to explore all of the
functionality embodied in strings, lists, tuples,
sets and dictionaries.


> And any attempt at filling the arrays to test some stand-alone code
> only give me errors.

There are several ways of doing this and Cameron
showed you a couple. There is another option which
is very powerful, especially for generating more
complex data sets. Its called a generator expression
and the most commonly used version is a list
comprehension:

new_list = [expression for item in collection if condition]

The expression can be any valid Python expression.
The condition can be any valid Python expression that
evaluates to a boolean result (and is also optional).

It is equivalent to:

new_list = []
for item in collection:
   if condition:
      new_list.append(expression)


Some examples to clarify:

num_list = [num for num in range(5)]

which is the same result as

numlist = list(range(5))

odds = [num for num in range(5) if num % 2] ->[1,3]

evens = [num for num in range(5) if num %2 == 0] -> [0,2,4]

squares = [num*num for num in range(5)]

odd_squares = [n*n for n in odds]

You can make them as complex as you like by
creating helper functions too:

fancy_list = [func1(n) for n in my_data if func2(n)]

The syntax can look a little confusing at first
(its somewhat like set notation) but once you
get used to it its OK.


> results = bytearray(slice_size)    # why isn't this 16 bytes long?

It is for me.

> res_list = (results)*slice_count

And this is 64 bytes long

> print " results ", [results]

And this prints a list with a single, 16 byte, bytearray inside it.

> print " results size ", sys.getsizeof(results)

This prints(on my system) 65 which may be confusing you.
But remember what I said about Python data structures
being rich, the bytearray has more than just the raw data
in it. The size reflects allthe other stuff that Python
uses to create a bytearray object. Try

>>> sys.getsizeof("")

and

>>> sys.getsizeof(bytearray())
>>> sys.getsizeof(bytearray("1"))
>>> sys.getsizeof(bytearray("12"))

Do the results surprize you?

Remember, Python data structures are not just chunks of memory.

> print " res_list ", [res_list]

Note that you are putting res_list into a list here,
but it is already a bytearray... There is no adavantage
in enclosing it in a list.

> Again, I feel like I'm circling the target, but not able to divine the
> proper syntax

Its not so much the syntax but your mental model
of what's going on under the covers. It's not C
(at least not at the first level down).

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