Skip to content

Commit

Permalink
Project updates (#163)
Browse files Browse the repository at this point in the history
Project update

- Updated development dependencies
- Added a Github action to run tests and coverage
- Formatted the code with black
  • Loading branch information
yuce authored Jul 15, 2024
1 parent 8b002d4 commit afe972d
Show file tree
Hide file tree
Showing 25 changed files with 774 additions and 590 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: "Run tests"

on:
push:
branches:
- "master"

pull_request_target:
branches:
- "master"

jobs:

run-tests:

name: "Run tests with Python ${{ matrix.python-version }} on ${{ matrix.os }}"

strategy:
matrix:
python-version: [ '3.8', '3.12' ]
os: [ "ubuntu-24.04" ]
fail-fast: false

runs-on: "${{ matrix.os }}"

steps:

- uses: "actions/checkout@v2"

- name: "Set up Python ${{ matrix.python-version }}"
uses: "actions/setup-python@v2"
with:
python-version: "${{ matrix.python-version }}"

- name: "Install test dependencies"
run: |
pip install -r dev-requirements.txt
- name: "Check style"
run: |
make check
- name: "Install SWI-Prolog"
run: |
sudo apt-get install -y swi-prolog-nox
- name: "Run tests"
run: |
make cover
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ clean:
rm -rf dist build pyswip.egg-info

cover:
py.test --cov=pyswip tests
py.test tests --verbose --cov=pyswip

test:
py.test tests --verbose

upload:
twine upload dist/*

check:
black --check .
2 changes: 2 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black==24.4.2
pytest-cov==5.0.0
15 changes: 8 additions & 7 deletions examples/coins/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -31,19 +31,20 @@
except NameError:
pass


def main():
prolog = Prolog()
prolog.consult("coins.pl")
count = int(input("How many coins (default: 100)? ") or 100)
total = int(input("What should be the total (default: 500)? ") or 500)
for i, soln in enumerate(prolog.query("coins(S, %d, %d)." % (count,total))):
for i, soln in enumerate(prolog.query("coins(S, %d, %d)." % (count, total))):
S = zip(soln["S"], [1, 5, 10, 50, 100])
print(i, end=" ")
for c, v in S:
print("%dx%d" % (c,v), end=" ")
print("%dx%d" % (c, v), end=" ")
print()
list(prolog.query("coins(S, %d, %d)." % (count,total)))
list(prolog.query("coins(S, %d, %d)." % (count, total)))



if __name__ == "__main__":
main()
10 changes: 5 additions & 5 deletions examples/coins/coins_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -47,11 +47,11 @@ def main():
s = zip(S.value, [1, 5, 10, 50, 100])
print(i, end=" ")
for c, v in s:
print("%dx%d" % (c,v), end=" ")
print("%dx%d" % (c, v), end=" ")
print()
i += 1
q.closeQuery()


if __name__ == "__main__":
main()
13 changes: 7 additions & 6 deletions examples/create_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -24,9 +24,10 @@
from pyswip.core import *
from pyswip.prolog import Prolog


def main():
prolog = Prolog()

a1 = PL_new_term_refs(2)
a2 = a1 + 1
t = PL_new_term_ref()
Expand All @@ -40,9 +41,9 @@ def main():
PL_cons_functor_v(t, animal2, a1)
PL_cons_functor_v(ta, assertz, t)
PL_call(ta, None)

print(list(prolog.query("animal(X,Y)", catcherrors=True)))


if __name__ == "__main__":
main()
15 changes: 8 additions & 7 deletions examples/draughts/puzzle1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -54,12 +54,13 @@ def main():

# [NW,N,NE,W,E,SW,S,SE]
print("%d %d %d" % tuple(B[:3]))
print("%d %d" % tuple(B[3:5]))
print("%d %d %d" % tuple(B[5:]))
print("%d %d" % tuple(B[3:5]))
print("%d %d %d" % tuple(B[5:]))

cont = input("Press 'n' to finish: ")
if cont.lower() == "n": break
if cont.lower() == "n":
break



if __name__ == "__main__":
main()
17 changes: 9 additions & 8 deletions examples/father.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -36,21 +36,22 @@ def main():
p.assertz("father(john,gina)")
p.assertz("mother(jane,mich)")

X = Variable(); Y = Variable(); Z = Variable()
X = Variable()
Y = Variable()
Z = Variable()

listing = Functor("listing", 1)
call(listing(father))

q = Query(father("john",Y), mother(Z,Y))
q = Query(father("john", Y), mother(Z, Y))
while q.nextSolution():
print(Y.value, Z.value)
q.closeQuery() # Newer versions of SWI-Prolog do not allow nested queries
q.closeQuery() # Newer versions of SWI-Prolog do not allow nested queries

print("\nQuery with strings\n")
for s in p.query("father(john,Y),mother(Z,Y)"):
print(s["Y"], s["Z"])


if __name__ == "__main__":
main()


22 changes: 11 additions & 11 deletions examples/hanoi/hanoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -36,22 +36,22 @@
class Notifier:
def __init__(self, fun):
self.fun = fun

def notify(self, t):
return not self.fun(t)

notify.arity = 1


class Tower:
def __init__(self, N=3, interactive=False):
"""N is the number of disks
"""
"""N is the number of disks"""
self.N = N
self.disks = dict(left=deque(range(N, 0, -1)), center=deque(), right=deque())
self.started = False
self.interactive = interactive
self.step = 0

def move(self, r):
if not self.started:
self.step += 1
Expand All @@ -62,7 +62,7 @@ def move(self, r):
disks[str(r[1])].append(disks[str(r[0])].pop())
self.step += 1
return self.draw()

def draw(self):
disks = self.disks
print("\n Step", self.step)
Expand All @@ -85,14 +85,14 @@ def draw(self):
def main():
N = 3
INTERACTIVITY = True

prolog = Prolog()
tower = Tower(N, INTERACTIVITY)
notifier = Notifier(tower.move)
registerForeign(notifier.notify)
prolog.consult("hanoi.pl")
list(prolog.query("hanoi(%d)" % N))


if __name__ == "__main__":
main()
12 changes: 7 additions & 5 deletions examples/hanoi/hanoi_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2018 Yüce Tekol
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -27,16 +27,18 @@

N = 3 # Number of disks


def main():
def notify(t):
print("move disk from %s pole to %s pole." % tuple(t))

notify.arity = 1

prolog = Prolog()
registerForeign(notify)
prolog.consult("hanoi.pl")
list(prolog.query("hanoi(%d)" % N))


if __name__ == "__main__":
main()
Loading

0 comments on commit afe972d

Please sign in to comment.