ValueError: dictionary update sequence element #0 has length 1; 2 is required

7,363 views
Skip to first unread message

zahi

unread,
Jul 22, 2007, 2:34:51 AM7/22/07
to pyswip
I'm running on Python 2.4. I hit the error message:

ValueError: dictionary update sequence element #0 has length 1; 2 is
required

I fixed it by modifying line 81 of pyswip 0.2.1:

if normalize:
try:
v = t.value
except AttributeError:
v = {}
for r in [x.value for x in t]:
v.update({r:1}) <<-- here
yield v
else:
yield t

Apparently 2.4 behavior changed. You can't call "update" on a dict
with just a string, you need to give it a value. After I made that
change, it works for me.

yuce

unread,
Jul 22, 2007, 10:33:20 AM7/22/07
to pyswip
Hi,

r is supposed to be a dictionary in line 81 of prolog.py, if it isn't
then there's in an issue with your query OR pyswip's way of converting
the result to a Python datatype. Could you please post a piece of code
that raises that exception?

Thanks,

Yuce

zahi

unread,
Jul 22, 2007, 11:17:48 PM7/22/07
to pyswip
I guess it didn't fix it after all. I was just using the examples in
the announcement. The internal calls are returning strings like:
'Functor12345' instead of dicts for "r".

-- test.py --
import sys
import ctypes
import pyswip

print "Python: %s" % sys.version
print "pyswip: %s" % pyswip.__VERSION__
print "ctypes: %s" % ctypes.__version__
prolog = pyswip.Prolog()
prolog.assertz("father(michael,john)")
prolog.assertz("father(michael,gina)")
list(prolog.query("father(michael,X)"))
for soln in prolog.query("father(X,Y)"):
print repr(soln)
#print soln["X"], "is the father of", soln["Y"]

-- output --
Python: 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]
pyswip: 0.2.1
ctypes: 1.0.2
{'Functor250124': 1}
{'Functor250124': 1}

My SWI-Prolog version is: "SWI-Prolog version 5.6.6 by Jan Wielemaker
(j...@swi-prolog.org)"

zahi

unread,
Jul 28, 2007, 10:26:21 PM7/28/07
to pyswip
I installed "2.6.35" of SWI-Prolog, (I was on 2.6.6), and the problem
went away. Who knows what was happening.

yuce

unread,
Jul 29, 2007, 4:26:41 AM7/29/07
to pyswip
Hi Zahi,

The builtin functors of SWI-Prolog [as far as I understand] have fixed
handles [which are integers]; for example =/2 's handle is 274700;
pyswip uses that to extract variable names from queries [If you check
lines 250-259 of easy.py, you can see it for yourself]. It seems this
handle has changed between the stable and the development version,
I'll update the requirements accordingly.

Thanks :)

Yuce

P.S: If you still have a 5.6.6 installation, could you please try to
change 274700 to 250124 at line 259 of easy.py? The line with
Functor.func[274700] = _unifier

Jan Wielemaker

unread,
Jul 29, 2007, 4:57:20 AM7/29/07
to pyswip
On Sunday 29 July 2007 10:26, yuce wrote:
> Hi Zahi,
>
> The builtin functors of SWI-Prolog [as far as I understand] have fixed
> handles [which are integers]; for example =/2 's handle is 274700;
> pyswip uses that to extract variable names from queries [If you check
> lines 250-259 of easy.py, you can see it for yourself]. It seems this
> handle has changed between the stable and the development version,
> I'll update the requirements accordingly.

Please do not rely on these. The set of handles that is considered
built-in varies regulary and their numbering changes regulary as well.
If you need access to functors, call PL_new_functor() at startup time
and then you can store the handle in a variable and use it for the
remainder of the process. I really don't want to fix the set of
built-ins and their numbering or even make the commitment they will be
stable for a single version (they might become pointers for example).

The only commitment is they are represented by functor_t, which is a
scalar variable with the same size as a pointer and their value doesn't
change during the lifetime of the process.

Cheers --- Jan

yuce

unread,
Jul 30, 2007, 8:57:43 AM7/30/07
to pyswip
Hi,

I've fixed that behavior, now pyswip won't rely on the functor handle
of =/2; the changes are in the SVN. Zahi, if you still have SWI-Prolog
5.6.6 installed and have access to pyswip's SVN, could you please
confirm it works? Your help is appreciated.

Yuce

Reply all
Reply to author
Forward
0 new messages