[Tutor] Write array to Status text

Stanfield, Vicki {D167~Indianapolis} VICKI.STANFIELD at roche.com
Tue Nov 18 15:13:42 EST 2003


I ended up using a list, but I still think my info was better suited to
an array. I had trouble getting the array right in my app, so I switched
back to a list and finally got the StatusText thing to work. I did this:

--------------------
errorarray=[0] *5
<SNIP>
StatusText=""
<SNIP part where count gets set>
while returnedval != EOT:
	output=port.read()
	returnedval= hex(ord(output))
	if count == 1:
         errorarray[byte]=returnedval
         byte = byte +1

StatusText=StatusText.join(str(errorarray[0:4]))
self.frame.SetStatusText(StatusText)
---------------------

I hope I didn't cut out anything critical to the explanation.

--vicki



-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Danny Yoo
Sent: Tuesday, November 18, 2003 3:05 PM
To: Vicki Stanfield
Cc: Tutor
Subject: RE: [Tutor] Write array to Status text



> > A Python 'List' is a collection that can dynamically grow in size, 
> > and that's probably what you want to use for general use.  We can 
> > construct lists by using '[]'.
> >
> > An 'array' is meant to be a statically-sized homogenous collection 
> > of primitive objects --- like numbers --- and it has a very very 
> > specialized purpose that most folks won't need to touch.  I guess 
> > I'm trying to say politely: don't use them now!  *grin*
>
> Funny thing though, my application is just that. There are exactly 4 
> entries and all are hexadecimal values.

Hi Vicki,


Hmmm!  Out of curiosity, what are some examples of StatusText that the
code wants to display to the user?  I just want to understand the
context of this problem better.  You've mentioned that you're getting
hexadecimal values back, so can I assume you're getting some character
string from a hardware device?



> The 10 in my array initialization was just to make sure I didn't 
> exceed my array boundaries. I changed it to 4 later. It is a far more 
> comfortable thing for me since I used them in C, and it allows me the 
> convenience of the tostring functionality.

Sure.  We can also pre-size a Python list:

###
>>> l = [0]*4
>>> l
[0, 0, 0, 0]
>>> l[2] = 0x34
>>> l
[0, 0, 52, 0]
###

and if we never do an append() on a list, then it behaves almost exactly
like a standard C array.  Indicing and index assignment should work
exactly how you'd expect in C.



(If you've used C++ before, you can think of a Python list as a
"vector", where we can push back (pushback()) elements at the end of the
container with relative ease.  Doing the same thing with an array alone
won't work, because arrays are fixed-sized containers.)


Lists don't have tostring().  On the other hand, they do have quite a
few things that arrays don't have:

###
>>> dir([])
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__repr__', '__rmul__',
'__setattr__', '__setitem__', '__setslice__', '__str__', 'append',
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',
'sort'] ###


As Python's generic container class, a list can do a lot of stuff,
including sort()s and arbitrary insert()ion.  And a key advantage of a
list is that we don't have to hardcode its size:

###
>>> l = []
>>> while 1:
...     text = raw_input('next? ')
...     if not text: break
...     l.append(int(text, 16))
...
next? 0x32
next? 0x16
next? 0x42
next? 0x7
next? 0xdeadbeef
next?
>>> l
[50, 22, 66, 7, 3735928559L]
>>>
>>>
>>> l2 = [None] * 2
>>> l2[0] = 'hello'
>>> l2[1] = 'world'
>>> l2
['hello', 'world']
>>> ':'.join(l2)
'hello:world'
###

The last example shows that if our list contains only strings, then it's
fairly easy to join them all together into a single string.



> But having said that, I will use either if I can make the StatusText 
> thing work. It is such a minimal part of the project that I really 
> don't care which I use. It seems really klunky to have to do a join 
> each time I add a value to my array (list), but if that is what it 
> takes, I'll do it.

Without knowing more about the context of the program, I can't say for
certain that the 'array' module is not what you need.  You mentioned
that you needed the array's tostring() method, so perhaps arrays will be
a win here.  I won't push too hard on forcing you to use lists.  *grin*


But when you have the time, I'd recommend looking more into Python
lists, because the majority of the Python code you'll see will use lists
as a fundamental type.  If you'd like more information about them, you
can take a look here:

http://www.python.org/doc/current/tut/node5.html#SECTION0051400000000000
00000


Good luck to you!


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list