Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Too Many Values To Unpack

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


victorsubervi at gmail

Nov 20, 2009, 7:45 AM

Post #1 of 9 (1992 views)
Permalink
Too Many Values To Unpack

Hi;
At one point Dennis Lee Bieber helped me with the following slightly
modified code:

#!/usr/bin/python

import sys,os
sys.path.append(os.getcwd())
import MySQLdb
from login import login
import re, string

def printTree(aTree, level=0):
tree = []
for name in sorted(aTree.keys()):
tree.append("%s%s") % ("\t" * level, name)
printTree(aTree[name], level + 1)

def expand(fetched):
aDict = {}
for (name, ) in fetched:
aDict[name] = {}
return aDict

def getChildren(levelDict, level = 0):
MAXLEVEL = 7
if level > MAXLEVEL:
return #possibly the data has a cycle/loop
for (nm, dt) in levelDict:
cursor.execute('''select c.name from categories as c
inner join relationship as r
on c.ID = r.Child
inner join categories as p
on r.Parent = p.ID
where p.category = %s
order by c.name''', (nm,))
levelDict[nm] = expand(cursor.fetchall())
# recursive call to do next level
getChildren(levelDict[nm], level + 1)
# no data return as we are mutating dictionaries in place

def catTree():
user, passwd, db, host = login()
database = MySQLdb.connect(host, user, passwd, db)
cursor = database.cursor()
cursor.execute('''create table if not exists categories
(ID int(3) unsigned primary key,
Category varchar(40),
Parent varchar(40))''')
cursor.execute('select Category, Parent from categories;')
data = cursor.fetchall()
cursor.execute('select Category from categories order by Parent, ID')
print data
Categories = [itm[0] for itm in cursor] #untuple single column
if len(Categories) > 0:
cursor.execute('select Parent from categories order by Parent, ID')
Parents = [itm[0] for itm in cursor]
MAXLEVEL = 15
cursor.execute('''create table if not exists categories
(ID integer auto_increment primary key,
Name varchar(40) not null,
unique (Name)
)''')
cursor.execute('''create table if not exists Relationship
(ID integer auto_increment primary key,
Parent integer not null,
foreign key (Parent) references categories (ID),
Child integer not null,
foreign key (Child) references categories (ID),
check (Parent <> Child) );''')
# get top level
print 'ok'
cursor.execute('select category from categories order by category')
theTree = expand(cursor.fetchall())
getChildren(theTree)
connection.commit()
return printTree(theTree)
else:
return ['There are no categories yet.']

catTree()

This throws the error:

[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] Traceback (most
recent call last):
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File
"/var/www/html/angrynates.com/cart/createCats.py", line 8, in ?
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] from catTree
import catTree
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File
"/var/www/html/angrynates.com/cart/catTree.py", line 77, in ?
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] catTree()
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File
"/var/www/html/angrynates.com/cart/catTree.py", line 71, in catTree
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58]
getChildren(theTree)
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] File
"/var/www/html/angrynates.com/cart/catTree.py", line 25, in getChildren
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] for (nm, dt)
in levelDict:
[Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] ValueError: too
many values to unpack

There is only one category in "categories". Please advise.
Victor


shashank.sunny.singh at gmail

Nov 20, 2009, 8:14 AM

Post #2 of 9 (1952 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

On Fri, Nov 20, 2009 at 9:15 PM, Victor Subervi <victorsubervi [at] gmail>wrote:

> Hi;
> At one point Dennis Lee Bieber helped me with the following slightly
> modified code:
>
> #!/usr/bin/python
>
> import sys,os
> sys.path.append(os.getcwd())
> import MySQLdb
> from login import login
> import re, string
>
> def printTree(aTree, level=0):
> tree = []
> for name in sorted(aTree.keys()):
> tree.append("%s%s") % ("\t" * level, name)
> printTree(aTree[name], level + 1)
>
> def expand(fetched):
> aDict = {}
> for (name, ) in fetched:
> aDict[name] = {}
> return aDict
>

def getChildren(levelDict, level = 0):
> MAXLEVEL = 7
> if level > MAXLEVEL:
> return #possibly the data has a cycle/loop
> for (nm, dt) in levelDict:
>

Are you sure your key values are 2-tuples in levelDict?
For-each on dicts enumerates the keys AFAIK

--
Regards
Shashank Singh
Senior Undergraduate, Department of Computer Science and Engineering
Indian Institute of Technology Bombay
shashank.sunny.singh [at] gmail
http://www.cse.iitb.ac.in/~shashanksingh


carsten.haese at gmail

Nov 20, 2009, 8:32 AM

Post #3 of 9 (1949 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

Victor Subervi wrote:
> Hi;
> At one point Dennis Lee Bieber helped me with the following slightly
> modified code:
>
> [snippage...]
>
> def getChildren(levelDict, level = 0):
> MAXLEVEL = 7
> if level > MAXLEVEL:
> return #possibly the data has a cycle/loop
> for (nm, dt) in levelDict:
> cursor.execute('''select c.name <http://c.name> from categories as c
> inner join relationship as r
> on c.ID = r.Child
> inner join categories as p
> on r.Parent = p.ID
> where p.category = %s
> order by c.name <http://c.name>''', (nm,))
> levelDict[nm] = expand(cursor.fetchall())
> # recursive call to do next level
> getChildren(levelDict[nm], level + 1)
> # no data return as we are mutating dictionaries in place
>
> [snippage...]
>
> [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] for (nm,
> dt) in levelDict:
> [Fri Nov 20 07:41:11 2009] [error] [client 208.84.198.58] ValueError:
> too many values to unpack
Iterating over a dictionary, such as what you're doing in the line <<for
(nm, dt) in levelDict>>, only produces the keys that are in the
dictionary. This means that this line only works if the keys in
levelDict are pairs of "nm"s and "dt"s, whatever "nm" and "dt"
represent. (In case the foregoing is too subtle a clue: Choose better
variable names.)

However, the line <<levelDict[nm] = expand(...)>> indicates that the
keys in levelDict are only "nm"s, which are presumably single objects,
not pairs. Also, the "dt" that you're trying to unpack from levelDict's
keys is not used anywhere in your function.

So, this would indicate that changing the offending line to <<for nm in
levelDict>> should fix this particular error.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


victorsubervi at gmail

Nov 20, 2009, 9:13 AM

Post #4 of 9 (1946 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

On Fri, Nov 20, 2009 at 12:14 PM, Shashank Singh <
shashank.sunny.singh [at] gmail> wrote:

> Are you sure your key values are 2-tuples in levelDict?
>

No. Here's some tweaked code:

cursor.execute('select category from categories order by category')
theTree = expand(cursor.fetchall())
print theTree

Now, categories only has one category called 'cat1'. Here's what it prints:

{'cat1': {}}

Now what I don't understand about the above code, provided by Dennis Lee
Bieber, is the "expand" statement. Nor could I find any documentation
googling either "expand python" or "expand fetchall python". Could someone
please explain what this code does?
TIA,
V


carsten.haese at gmail

Nov 20, 2009, 10:07 AM

Post #5 of 9 (1947 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

Victor Subervi wrote:
> Now what I don't understand about the above code, provided by Dennis Lee
> Bieber, is the "expand" statement.

It's not a statement. It's a function. The function is defined in your
code. Or rather, it's defined in the code that you copied from Dennis
Lee Bieber, apparently. Look at the function definition. It'll tell you
what the function does.

> Could
> someone please explain what this code does?

Maybe you should ask the person that wrote the code.

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


victorsubervi at gmail

Nov 21, 2009, 6:27 AM

Post #6 of 9 (1922 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

On Sat, Nov 21, 2009 at 2:10 AM, Dennis Lee Bieber <wlfraed [at] ix>wrote:

> And my follow-up to that original thread stated that it /was/
> pseudo-code, not something tested. My goal was to illustrate the
> general concepts of processing a recursive/nested data set stored in a
> flat/relational table.
>

Of course and appreciated.

>
> "expand()" was responsible for taking each "key" from a select query
> and creating an empty dictionary with that "key" as, well, key. Then the
> recursive operation would perform a select query used to populate the
> empty dictionary, ad infinitum.
>

Here is the output with just one test category:
{'cat1': {}}

Here is the code to which that output is fed:

def getChildren(levelDict, level = 0):
MAXLEVEL = 7
if level > MAXLEVEL:
return #possibly the data has a cycle/loop
for (nm, dt) in levelDict:
cursor.execute('''select c.name from categories as c
inner join relationship as r
on c.ID = r.Child
inner join categories as p
on r.Parent = p.ID
where p.category = %s
order by c.name''', (nm,))
levelDict[nm] = expand(cursor.fetchall())
# recursive call to do next level
getChildren(levelDict[nm], level + 1)
# no data return as we are mutating dictionaries in place

When this script is called from another script, the python interpreter
throws the following error:

Traceback (most recent call last):
File "createCats.py", line 8, in ?
from catTree import catTree
File "/var/www/html/angrynates.com/cart/catTree.py", line 85, in ?
catTree()
File "/var/www/html/angrynates.com/cart/catTree.py", line 76, in catTree
getChildren(theTree)
File "/var/www/html/angrynates.com/cart/catTree.py", line 25, in
getChildren
for (nm, dt) in levelDict:
ValueError: too many values to unpack

Please advise.
TIA,
V


carsten.haese at gmail

Nov 21, 2009, 1:41 PM

Post #7 of 9 (1904 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

Victor Subervi wrote:
> File "/var/www/html/angrynates.com/cart/catTree.py
> <http://angrynates.com/cart/catTree.py>", line 25, in getChildren
> for (nm, dt) in levelDict:
> ValueError: too many values to unpack
>
> Please advise.

I already explained what's causing this error. Read my first response on
this thread.
(http://mail.python.org/pipermail/python-list/2009-November/1227008.html)

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


greg.ewing at canterbury

Nov 21, 2009, 9:19 PM

Post #8 of 9 (1901 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

Dennis Lee Bieber wrote:

> I apparently thought "for ... in dictionary" would return (key,
> value) pairs, but it appears that it only returns the key itself -- and
> a single key can't be unpacked.
>
> Misleading error... too many /targets/ to unpack...

My guess is that the keys are strings, which means it's
unpacking them into characters, in which case a key of
length 3 or more will produce that message.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 21, 2009, 9:37 PM

Post #9 of 9 (1901 views)
Permalink
Re: Too Many Values To Unpack [In reply to]

On Sat, 21 Nov 2009 21:06:08 -0800, Dennis Lee Bieber wrote:

> I apparently thought "for ... in dictionary" would return (key,
> value) pairs, but it appears that it only returns the key itself -- and
> a single key can't be unpacked.
>
> Misleading error... too many /targets/ to unpack...

No, the error is fine.


>>> for x, y in {1:'a'}:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unpack non-sequence
>>>
>>> for x, y in {'a':1}:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
>>>
>>> for x, y in {'parrot':1}:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack


Strings are iterable, and so unpack into individual characters.


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.