Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Error message <exceptions.TypeError unpack non-sequence>

Reply
Thread Tools

Error message <exceptions.TypeError unpack non-sequence>

 
 
ahsan Imam
Guest
Posts: n/a
 
      02-05-2004
Hello All,

I am trying to move an application from python 1.5.2 to 2.3. The code
works fine in 1.5.2 but gives the exception (exceptions.TypeError
unpack non-sequence) in python 2.3. I did not write this code so I am
not sure what is happening here.

Here is the code snippet:

for (item, agent) in self.lItems:
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))

Note:
self.lItems contains two elements.

Questions:
1) What is the for statement doing?
2) Is this called tuple unpacking or list unpacking?
3) Is there newer syntax?
4) Why does he use the "for" loop like that?

Any help is appreciated.

Thanks
Ahsan
 
Reply With Quote
 
 
 
 
Josiah Carlson
Guest
Posts: n/a
 
      02-06-2004

> I am trying to move an application from python 1.5.2 to 2.3. The code
> works fine in 1.5.2 but gives the exception (exceptions.TypeError
> unpack non-sequence) in python 2.3. I did not write this code so I am
> not sure what is happening here.
>
> Here is the code snippet:
>
> for (item, agent) in self.lItems:
> lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
>
> Note:
> self.lItems contains two elements.


Always exactly 2 items?

> Questions:
> 1) What is the for statement doing?


Attempting to assign the names item and agent a pair of values in
self.lItems

> 2) Is this called tuple unpacking or list unpacking?


list unpacking:
[item, agent] = [1,2]

tuple unpacking:
item, agent = 1,2
(item, agent) = 1,2
item, agent = (1,2)
(item, agent) = (1,2)

I would be willing to bet that the list below is cast into a tuple:
(item, agent) = [1,2]

> 3) Is there newer syntax?


I wouldn't so much call it newer as more intuitive.

>>> for i,j in [(1,2), (3,4)]:

.... print i, j
....
1 2
3 4
>>>


> 4) Why does he use the "for" loop like that?


Because he doesn't realize he could do the below.
item, agent = self.lItems
lItems.append(interpolate(self._ITEM_FMT, id=str(item)))



- Josiah
 
Reply With Quote
 
 
 
 
ahsan Imam
Guest
Posts: n/a
 
      02-06-2004
Josiah Carlson <(E-Mail Removed)> wrote in message news:<bvv2dr$7oa$(E-Mail Removed)>...
> > I am trying to move an application from python 1.5.2 to 2.3. The code
> > works fine in 1.5.2 but gives the exception (exceptions.TypeError
> > unpack non-sequence) in python 2.3. I did not write this code so I am
> > not sure what is happening here.
> >
> > Here is the code snippet:
> >
> > for (item, agent) in self.lItems:
> > lItems.append(interpolate(self._ITEM_FMT, id=str(item)))
> >
> > Note:
> > self.lItems contains two elements.

>
> Always exactly 2 items?

What if there are more than 2 items? How can I do something where
element 0 and 1 are assigned to (item, agent) and so on? If this is a
silly question please let me know what I can read.

Thanks
Ahsan
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-07-2004

"ahsan Imam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Josiah Carlson <(E-Mail Removed)> wrote in message

news:<bvv2dr$7oa$(E-Mail Removed)>...
> > > I am trying to move an application from python 1.5.2 to 2.3. The code
> > > works fine in 1.5.2 but gives the exception (exceptions.TypeError
> > > unpack non-sequence) in python 2.3. I did not write this code so I am
> > > not sure what is happening here.


I do not know of any change in Python that would make code like the below
invalid. Are you possibly running the program with different input data?
I suggest you insert 'print self.lItems' before the loop to see it *that*
changed (somewhere else in the program).

> > >
> > > Here is the code snippet:
> > >
> > > for (item, agent) in self.lItems:
> > > lItems.append(interpolate(self._ITEM_FMT, id=str(item)))


self.lItems must contain zero or more (item,agent) *pairs*

> What if there are more than 2 items? How can I do something where
> element 0 and 1 are assigned to (item, agent) and so on?


Yes and no. You would have to group elements 0 and 1 into a pair, elements
2 and 3 into another (the second), and so on. There is probably something
in itertools that will do this. Otherwise, writing your own generator to
do so should be easy enough. Then write 'for (i,a) in grouper(s.l):' where
grouper is the pair generator.

Terry J. Reedy




 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      02-07-2004
> What if there are more than 2 items? How can I do something where
> element 0 and 1 are assigned to (item, agent) and so on? If this is a
> silly question please let me know what I can read.


If there ever was more than two items, that is, if it was a flat
sequence like this: [1,2,3,4,5,6], then the original for loop couldn't
have worked.

- Josiah
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: error ...1 value to unpack Chris Mellon Python 1 11-02-2007 10:09 PM
Re: error: Error -5 while decompressing data from struct.unpack Fredrik Lundh Python 0 12-14-2006 11:58 AM
Yaml error running `ri String#unpack` Frank Mitchell Ruby 0 10-20-2004 03:42 AM
How to run email-unpack.py from the Email Message Module. Chuck Amadi Python 2 06-06-2004 10:24 AM
How can i unpack a EXE file that contains a CRC error Jackal Computer Support 1 10-08-2003 06:59 PM



Advertisments