diff --git a/src/pyswip/easy.py b/src/pyswip/easy.py index 6acbfe8..a1470ae 100644 --- a/src/pyswip/easy.py +++ b/src/pyswip/easy.py @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/src/pyswip/examples/coins.py b/src/pyswip/examples/coins.py index 995f73e..79b9346 100644 --- a/src/pyswip/examples/coins.py +++ b/src/pyswip/examples/coins.py @@ -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 diff --git a/src/pyswip/examples/hanoi.py b/src/pyswip/examples/hanoi.py index 1b03924..707476b 100644 --- a/src/pyswip/examples/hanoi.py +++ b/src/pyswip/examples/hanoi.py @@ -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: diff --git a/src/pyswip/prolog.py b/src/pyswip/prolog.py index fdaf6e1..43a001a 100644 --- a/src/pyswip/prolog.py +++ b/src/pyswip/prolog.py @@ -64,7 +64,7 @@ __all__ = "PrologError", "NestedQueryError", "Prolog" -RE_PLACEHOLDER = re.compile(r"%s") +RE_PLACEHOLDER = re.compile(r"%p") class PrologError(Exception): @@ -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 @@ -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: @@ -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) @@ -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 @@ -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) @@ -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 @@ -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}] """ @@ -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: diff --git a/tests/test_prolog.py b/tests/test_prolog.py index 80ebf1d..f32ea0f 100644 --- a/tests/test_prolog.py +++ b/tests/test_prolog.py @@ -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', ),