Skip to content

Commit

Permalink
Use %p as the placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed Oct 26, 2024
1 parent ef65f6a commit 3cda897
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/pyswip/easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
class InvalidTypeError(TypeError):
def __init__(self, *args):
type_ = args and args[0] or "Unknown"
msg = "Term is expected to be of type: '%s'" % type_
msg = f"Term is expected to be of type: '{type_}'"
Exception.__init__(self, msg, *args)


Expand All @@ -116,7 +116,7 @@ class ArgumentTypeError(Exception):
"""

def __init__(self, expected, got):
msg = "Expected an argument of type '%s' but got '%s'" % (expected, got)
msg = f"Expected an argument of type '{expected}' but got '{got}'"
Exception.__init__(self, msg)


Expand Down Expand Up @@ -290,7 +290,7 @@ def __str__(self):
return self.__repr__()

def __repr__(self):
return "Variable(%s)" % self.handle
return f"Variable({self.handle})"

def put(self, term):
# PL_put_variable(term)
Expand Down Expand Up @@ -703,7 +703,7 @@ class Query(object):
def __init__(self, *terms, **kwargs):
for key in kwargs:
if key not in ["flags", "module"]:
raise Exception("Invalid kwarg: %s" % key, key)
raise Exception(f"Invalid kwarg: {key}", key)

flags = kwargs.get("flags", PL_Q_NODEBUG | PL_Q_CATCH_EXCEPTION)
module = kwargs.get("module", None)
Expand Down
4 changes: 2 additions & 2 deletions src/pyswip/examples/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ def solve(
"""
Solves the coins problem.
Finds and returns combinations of ``coin_count`` coins that makes ``total``cents.
Finds and returns combinations of ``coin_count`` coins that makes ``total`` cents.
:param coin_count: Number of coins
:param total_cents: Total cent value of coins
"""
cents = [1, 5, 10, 50, 100]
query = Prolog.query(
"coins(%s, %s, Solution)", coin_count, total_cents, maxresult=max_solutions
"coins(%p, %p, Solution)", coin_count, total_cents, maxresult=max_solutions
)
return [
{cent: count for cent, count in zip(cents, soln["Solution"])} for soln in query
Expand Down
2 changes: 1 addition & 1 deletion src/pyswip/examples/hanoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def solve(disk_count: int = 3, simple: bool = False, file: IO = None) -> None:
tower = Tower(disk_count, file=file)
notifier = Notifier(tower.move)
Prolog.register_foreign(notifier.notify)
list(Prolog.query("hanoi(%s)", disk_count))
list(Prolog.query("hanoi(%p)", disk_count))


def prolog_source() -> str:
Expand Down
20 changes: 10 additions & 10 deletions src/pyswip/prolog.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
__all__ = "PrologError", "NestedQueryError", "Prolog"


RE_PLACEHOLDER = re.compile(r"%s")
RE_PLACEHOLDER = re.compile(r"%p")


class PrologError(Exception):
Expand All @@ -86,7 +86,7 @@ def __initialize():
args.append("-q") # --quiet
args.append("--nosignals") # "Inhibit any signal handling by Prolog"
if SWI_HOME_DIR:
args.append("--home=%s" % SWI_HOME_DIR)
args.append(f"--home={SWI_HOME_DIR}")

result = PL_initialise(len(args), args)
# result is a boolean variable (i.e. 0 or 1) indicating whether the
Expand Down Expand Up @@ -207,7 +207,7 @@ def asserta(cls, format: str, *args, catcherrors: bool = False) -> None:
:param format:
The format to be used to generate the clause.
The placeholders (``%s``) are replaced by the ``args`` if one ore more arguments are given.
The placeholders (``%p``) are replaced by the ``args`` if one ore more arguments are given.
:param args:
Arguments to replace the placeholders in the ``format`` string
:param catcherrors:
Expand All @@ -224,7 +224,7 @@ def asserta(cls, format: str, *args, catcherrors: bool = False) -> None:
... big(A),
... small(B)''')
>>> nums = list(range(5))
>>> Prolog.asserta("numbers(%s)", nums)
>>> Prolog.asserta("numbers(%p)", nums)
"""
next(
cls.query(format.join(["asserta((", "))."]), *args, catcherrors=catcherrors)
Expand All @@ -241,7 +241,7 @@ def assertz(cls, format: str, *args, catcherrors: bool = False) -> None:
:param format:
The format to be used to generate the clause.
The placeholders (``%s``) are replaced by the ``args`` if one ore more arguments are given.
The placeholders (``%p``) are replaced by the ``args`` if one ore more arguments are given.
:param catcherrors:
Catches the exception raised during goal execution
Expand All @@ -256,7 +256,7 @@ def assertz(cls, format: str, *args, catcherrors: bool = False) -> None:
... big(A),
... small(B)''')
>>> nums = list(range(5))
>>> Prolog.assertz("numbers(%s)", nums)
>>> Prolog.assertz("numbers(%p)", nums)
"""
next(
cls.query(format.join(["assertz((", "))."]), *args, catcherrors=catcherrors)
Expand Down Expand Up @@ -295,7 +295,7 @@ def retract(cls, format: str, *args, catcherrors: bool = False) -> None:
:param format:
The format to be used to generate the term.
The placeholders (``%s``) are replaced by the ``args`` if one ore more arguments are given.
The placeholders (``%p``) are replaced by the ``args`` if one ore more arguments are given.
:param catcherrors:
Catches the exception raised during goal execution
Expand All @@ -315,10 +315,10 @@ def retract(cls, format: str, *args, catcherrors: bool = False) -> None:
>>> Prolog.dynamic("numbers/1")
>>> nums = list(range(5))
>>> Prolog.asserta("numbers(10)")
>>> Prolog.asserta("numbers(%s)", nums)
>>> Prolog.asserta("numbers(%p)", nums)
>>> list(Prolog.query("numbers(X)"))
[{'X': [0, 1, 2, 3, 4]}, {'X': 10}]
>>> Prolog.retract("numbers(%s)", nums)
>>> Prolog.retract("numbers(%p)", nums)
>>> list(Prolog.query("numbers(X)"))
[{'X': 10}]
"""
Expand Down Expand Up @@ -408,7 +408,7 @@ def query(
:param format:
The format to be used to generate the query.
The placeholders (``%s``) are replaced by the ``args`` if one ore more arguments are given.
The placeholders (``%p``) are replaced by the ``args`` if one ore more arguments are given.
:param args:
Arguments to replace the placeholders in the ``format`` string
:param maxresult:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_prolog.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ def test_retract(self):
format_prolog_fixture = [
("", (), ""),
("no-args", (), "no-args"),
("before%safter", ("text",), 'before"text"after'),
("before%safter", (123,), "before123after"),
("before%safter", (123.45,), "before123.45after"),
("before%safter", (Atom("foo"),), "before'foo'after"),
("before%safter", (Variable(name="Foo"),), "beforeFooafter"),
("before%pafter", ("text",), 'before"text"after'),
("before%pafter", (123,), "before123after"),
("before%pafter", (123.45,), "before123.45after"),
("before%pafter", (Atom("foo"),), "before'foo'after"),
("before%pafter", (Variable(name="Foo"),), "beforeFooafter"),
(
"before%safter",
"before%pafter",
(["foo", 38, 45.897, [1, 2, 3]],),
'before["foo",38,45.897,[1,2,3]]after',
),
Expand Down

0 comments on commit 3cda897

Please sign in to comment.