Skip to content

Commit

Permalink
Added python2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
yuce committed May 23, 2018
1 parent 0a9e29c commit 2b0e4c0
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

See the [CHANGELOG](CHANGELOG.md).

**This library is being refactored. Expect API breakage and incompatibility with previous versions.**
**This library is being cleaned up and refactored. Expect API breakage and incompatibility with previous versions.**

Thanks to all [contributors](CONTRIBUTORS.txt). If you have contributed to PySWIP in the past and your name does not appear on that list, please [let me know](mailto:[email protected]) so I can add your name.

Expand All @@ -22,7 +22,7 @@ doesn't require compilation to be installed.

## Requirements:

* Python 3.4 and higher.
* Python 2.7 or 3.4 and higher.
* PyPy is currently not supported.
* SWI-Prolog 7.6.x and higher (most probably other versions will also work).
* libpl as a shared library.
Expand Down
5 changes: 5 additions & 0 deletions examples/coins/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@

# 100 coins must sum to $5.00

from __future__ import print_function
from pyswip.prolog import Prolog

try:
input = raw_input
except NameError:
pass

def main():
prolog = Prolog()
Expand Down
22 changes: 13 additions & 9 deletions examples/coins/coins_new.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# 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
Expand All @@ -22,29 +21,34 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


# 100 coins must sum to $5.00


from __future__ import print_function
from pyswip import Prolog, Functor, Variable, Query


try:
input = raw_input
except NameError:
pass


def main():
prolog = Prolog()
prolog.consult("coins.pl")
count = int(raw_input("How many coins (default: 100)? ") or 100)
total = int(raw_input("What should be the total (default: 500)? ") or 500)
count = int(input("How many coins (default: 100)? ") or 100)
total = int(input("What should be the total (default: 500)? ") or 500)
coins = Functor("coins", 3)
S = Variable()
q = Query(coins(S, count, total))
i = 0
while q.nextSolution():
## [1,5,10,50,100]
s = zip(S.value, [1, 5, 10, 50, 100])
print i,
print(i, end=" ")
for c, v in s:
print "%dx%d" % (c,v),
print
print("%dx%d" % (c,v), end=" ")
print()
i += 1
q.closeQuery()

Expand Down
7 changes: 7 additions & 0 deletions examples/draughts/puzzle1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@
# are four guards watching each wall. How can they be rearranged such
# that there are five watching each wall?"

from __future__ import print_function
from pyswip.prolog import Prolog


try:
input = raw_input
except NameError:
pass


def main():
prolog = Prolog()
prolog.consult("puzzle1.pl")
Expand Down
1 change: 1 addition & 0 deletions examples/father.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
from pyswip import *


Expand Down
6 changes: 6 additions & 0 deletions examples/hanoi/hanoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
from collections import deque

from pyswip.prolog import Prolog
from pyswip.easy import getList, registerForeign

try:
input = raw_input
except NameError:
pass


class Notifier:
def __init__(self, fun):
Expand Down
1 change: 1 addition & 0 deletions examples/hanoi/hanoi_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
from pyswip.prolog import Prolog
from pyswip.easy import getList, registerForeign

Expand Down
1 change: 1 addition & 0 deletions examples/register_foreign_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

# Demonstrates registering a Python function as a Prolog predicate through SWI-Prolog's FFI.

from __future__ import print_function
from pyswip.prolog import Prolog
from pyswip.easy import registerForeign, getAtomChars

Expand Down
2 changes: 1 addition & 1 deletion examples/sendmoremoney/money.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
# So, what should be the values of S, E, N, D, M, O, R, Y
# if they are all distinct digits.

from __future__ import print_function
from pyswip import Prolog


letters = "S E N D M O R Y".split()
prolog = Prolog()
prolog.consult("money.pl")
Expand Down
1 change: 1 addition & 0 deletions examples/sendmoremoney/money_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# So, what should be the values of S, E, N, D, M, O, R, Y
# if they are all distinct digits.

from __future__ import print_function
from pyswip import Prolog, Functor, Variable, call


Expand Down
1 change: 1 addition & 0 deletions examples/sudoku/sudoku.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import print_function
from pyswip.prolog import Prolog
from pyswip.easy import *

Expand Down
50 changes: 25 additions & 25 deletions examples/sudoku/sudoku_daily.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# -*- coding: utf-8 -*-


# pyswip -- Python SWI-Prolog bridge
# Copyright (c) 2007-2012 Yüce Tekol
# 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
Expand All @@ -26,15 +25,19 @@
# Sudoku auto-solver. Get today's sudoku at http://www.sudoku.org.uk/daily.asp
# and solve it


import urllib
from HTMLParser import HTMLParser, HTMLParseError

from __future__ import print_function
from pyswip.prolog import Prolog
from pyswip.easy import *

try:
from html.parser import HTMLParser
except:
from HTMLParser import HTMLParser

URL = "http://www.sudoku.org.uk/daily.asp"
try:
import urllib.request as urllib_request
except ImportError:
import urllib as urllib_request


class DailySudokuPuzzle(HTMLParser):
Expand All @@ -59,23 +62,20 @@ def handle_endtag(self, tag):

def handle_data(self, data):
if self.__in_td:
self.puzzle.append(int(data))
self.puzzle.append(int(data))


def pretty_print(table):
print "".join(["/---", "----"*8, "\\"])
print("".join(["/---", "----"*8, "\\"]))
for row in table:
print "".join(["|", "|".join(" %s " % (i or " ") for i in row), "|"])
print "".join(["\\---", "----"*8, "/"])
print("".join(["|", "|".join(" %s " % (i or " ") for i in row), "|"]))
print("".join(["\\---", "----"*8, "/"]))


def get_daily_sudoku(url):
puzzle = DailySudokuPuzzle()
f = urllib.urlopen(url)
try:
puzzle.feed(f.read())
except HTMLParseError:
pass
f = urllib_request.urlopen(url)
puzzle.feed(f.read().decode("latin-1"))
puzzle = puzzle.puzzle
return [puzzle[i*9:i*9+9] for i in range(9)]

Expand All @@ -90,20 +90,20 @@ def solve(problem):
else:
return False


if __name__ == "__main__":
URL = "http://www.sudoku.org.uk/daily.asp"

prolog = Prolog() # having this in `solve` bites! because of __del__

print "Getting puzzle from:", URL

print("Getting puzzle from:", URL)
puzzle = get_daily_sudoku(URL)
print "-- PUZZLE --"
pretty_print(puzzle)
print
print " -- SOLUTION --"
print("-- PUZZLE --")
pretty_print(puzzle)
print()
print(" -- SOLUTION --")
solution = solve(puzzle)
if solution:
pretty_print(solution)
else:
print "This puzzle has no solutions [is it valid?]"
print("This puzzle has no solutions [is it valid?]")

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

import sys
import os
import io
import os.path
from setuptools import setup

with open("README.md") as f:
with io.open("README.md", encoding="utf-8") as f:
long_description = f.read()


Expand Down

0 comments on commit 2b0e4c0

Please sign in to comment.