-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the initial value exchange doc
- Loading branch information
Showing
5 changed files
with
77 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Value Exchange Between Python and Prolog | ||
======================================== | ||
|
||
String Interpolation from Python to Prolog | ||
------------------------------------------ | ||
|
||
Currently there's limited support for converting Python values automatically to Prolog via a string interpolation mechanism. | ||
This mechanism is available to be used with the following ``Prolog`` class methods: | ||
|
||
* ``assertz`` | ||
* ``asserta`` | ||
* ``retract`` | ||
* ``query`` | ||
|
||
These methods take one string format argument, and zero or more arguments to replace placeholders with in the format to produce the final string. | ||
Placeholder is ``%p`` for all types. | ||
|
||
The following types are recognized: | ||
|
||
* String | ||
* Integer | ||
* Float | ||
* Boolean | ||
* ``pyswip.Atom`` | ||
* ``pyswip.Variable`` | ||
* Lists of the types above | ||
|
||
Other types are converted to strings using the ``str`` function. | ||
|
||
.. list-table:: String Interpolation to Prolog | ||
:widths: 50 50 | ||
:header-rows: 1 | ||
|
||
* - Python Value | ||
- String | ||
* - str ``"Some value"`` | ||
- ``"Some value"`` | ||
* - int ``38`` | ||
- ``38`` | ||
* - float ``38.42`` | ||
- ``38.42`` | ||
* - bool ``True`` | ||
- ``1`` | ||
* - bool ``False`` | ||
- ``0`` | ||
* - ``pyswip.Atom("carrot")`` | ||
- ``'carrot'`` | ||
* - ``pyswip.Variable("Width")`` | ||
- ``Width`` | ||
* - list ``["string", 12, 12.34, Atom("jill")]`` | ||
- ``["string", 12, 12.34, 'jill']`` | ||
* - Other ``value`` | ||
- ``str(value)`` | ||
|
||
|
||
The placeholders are set using ``%p``. | ||
|
||
Example: | ||
|
||
.. code-block:: python | ||
ids = [1, 2, 3] | ||
joe = Atom("joe") | ||
Prolog.assertz("user(%p,%p)", joe, ids) | ||
list(Prolog.query("user(%p,IDs)", joe)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters