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.
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
-- 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)"
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
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
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