yuce / pyswip

PySwip is a Python-Prolog interface that enables querying SWI-Prolog in your Python programs.
https://pyswip.org
MIT License
482 stars 98 forks source link

Updates #191

Closed yuce closed 1 month ago

yuce commented 1 month ago

Enhancements

Python to Prolog Placeholders

Prolog.asserta, Prolog.assertz, Prolog.retract, Prolog.query methods take a format and optional arguments to convert Python values to strings. The following types are recognized:

Other types are converted to strings using the str function.

The placeholders are set using %p.

Example:

ids = [1, 2, 3]
joe = Atom("joe")
Prolog.assertz("user(%p, %p)", joe, ids)
list(Prolog.query("user(%p, IDs)", joe))

Added more examples

Moved The Towers of Hanoi example to pyswip.examples.hanoi package. For example, here is how to solve the puzzle with 5 disks:

from pyswip.examples.hanoi import solve
solve(5)

Or:

from pyswip.examples.hanoi import solve
solve(5, simple=True)

It is also available via the command line:

python3 -m pyswip.examples.hanoi 5 --simple

Similarly, Coins example was moved to pyswip.examples.coins package.