Skip to content

Commit

Permalink
Some cleanup and adopt hatch and ruff (#167)
Browse files Browse the repository at this point in the history
Cleanup and hatch+ruff adoption

* Cleanup of the project
* Adopted hatch for test, coverage and other tasks
* Adopdated ruff as the linter
* Minimum supported Python is 3.8
  • Loading branch information
yuce authored Aug 30, 2024
1 parent 1b036c1 commit c6752ee
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 129 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@ on:
jobs:

run-tests:

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

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

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

steps:

- uses: "actions/checkout@v2"
- uses: "actions/checkout@v4"

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

Expand All @@ -48,5 +46,6 @@ jobs:
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
run: |
make cover
coveralls
make check
make coverage
make upload-coverage
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
.PHONY: build clean cover test upload
.PHONY: build clean coverage upload-coverage test upload

build:
python setup.py sdist
python setup.py bdist_wheel --universal
hatch build

clean:
clean:coveralls
rm -rf dist build pyswip.egg-info

cover:
py.test tests --verbose --cov=pyswip
hatch run test:coverage

upload-coverage:
hatch run upload-coverage:coveralls

test:
py.test tests --verbose
hatch run test:all

upload:
twine upload dist/*

check:
black --check .
ruff format --check
ruff check
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

## Installing the Latest Version

The latest SWI-Prolog supported by Ubuntu 18.04 and 20.04 are 7.6.4. We generally want to support LTS releases of
Ubuntu. The compatibility of PySwip with 7.6.4 on master is broken, so we are not able to release a new version until
this is fixed. In the meantime, you can use the following to install PySwip from the master branch:
The latest SWI-Prolog supported by Ubuntu 22.04 are 9.0.4.
We generally want to support LTS releases of Ubuntu.
You can use the following to install PySwip from the master branch:

```
pip install git+https://github.com/yuce/pyswip@master#egg=pyswip
Expand All @@ -19,39 +19,44 @@ pip install git+https://github.com/yuce/pyswip@master#egg=pyswip
## The End of Python 2 Support

Python 2 has reached end of life on January 1st, 2020 as documented [here](https://www.python.org/doc/sunset-python-2/).
So, PySwip 0.2.10 will be the last version which officially supports Python 2.

Do you still require Python 2 support? Let us know at: https://github.com/yuce/pyswip/issues/94
So, PySwip 0.2.10 is the last version which officially supports Python 2.

---

## What's New?

See the [CHANGELOG](CHANGELOG.md).

**WARNING! PySwip has no Windows installers! If you are a Windows user, see [INSTALL](https://github.com/yuce/pyswip/blob/master/INSTALL.md#windows). There are some "free download" sites that claim to be hosting PySwip installers. DO NOT TRUST THEM!**
---

**WARNING! PySwip has no Windows installers!
If you are a Windows user, see [INSTALL](https://github.com/yuce/pyswip/blob/master/INSTALL.md#windows).
There are some "free download" sites that claim to be hosting PySwip installers.
DO NOT TRUST THEM!**

---

Thanks to all [contributors](CONTRIBUTORS.txt).

## Introduction

PySwip is a Python - SWI-Prolog bridge enabling to query [SWI-Prolog](http://www.swi-prolog.org) in your Python programs.
It features an (incomplete) SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a
Pythonic interface.
It features an (incomplete) SWI-Prolog foreign language interface, a utility class that makes it easy querying with Prolog and also a Pythonic interface.

Since PySwip uses SWI-Prolog as a shared library and ctypes to access it, it doesn't require compilation to be installed.

## Requirements:

* Python 3.6 and higher.
* Python 3.8 and higher.
* PyPy is currently not supported.
* SWI-Prolog 8.2 and higher.
* SWI-Prolog 9.0.4 and higher.
* `libswipl` as a shared library. *This is the default on most platforms.*
* Works on Linux, Windows, MacOS and FreeBSD. Should work on other POSIX.

## Install

**IMPORTANT: Make sure the SWI-Prolog architecture is the same as the Python architecture. If you are using a 64bit build of Python, use a 64bit build of SWI-Prolog, etc.**
**IMPORTANT: Make sure the SWI-Prolog architecture is the same as the Python architecture.
If you are using a 64bit build of Python, use a 64bit build of SWI-Prolog, etc.**

See [INSTALL](INSTALL.md) for instructions.

Expand Down Expand Up @@ -117,7 +122,6 @@ q.closeQuery()
# Outputs:
# Hello, john
# Hello, gina

```

The core functionality of `Prolog.query` is based on Nathan Denny's public domain prolog.py.
Expand Down Expand Up @@ -217,7 +221,7 @@ If you would like to reference PySwip in a LaTeX document, you can use the provi
## License

```
Copyright (c) 2007-2020 Yüce Tekol and PySwip contributors
Copyright (c) 2007-2024 Yüce Tekol and PySwip contributors
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
Expand Down
4 changes: 2 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
black==24.4.2
pytest-cov==5.0.0
hatch==1.12.0
ruff==0.6.2
2 changes: 0 additions & 2 deletions examples/father.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ def main():

father = Functor("father", 2)
mother = Functor("mother", 2)
assertz = Functor("assertz", 1)

p.assertz("father(john,mich)")
p.assertz("father(john,gina)")
p.assertz("mother(jane,mich)")

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

Expand Down
7 changes: 1 addition & 6 deletions examples/hanoi/hanoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@
from collections import deque

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

try:
input = raw_input
except NameError:
pass
from pyswip.easy import registerForeign


class Notifier:
Expand Down
2 changes: 1 addition & 1 deletion examples/hanoi/hanoi_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

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

N = 3 # Number of disks

Expand Down
2 changes: 1 addition & 1 deletion examples/knowledgebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def main():
p = Prolog()
_ = Prolog()

assertz = Functor("assertz")
parent = Functor("parent", 2)
Expand Down
2 changes: 1 addition & 1 deletion examples/register_foreign_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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


def hello(t):
Expand Down
12 changes: 3 additions & 9 deletions examples/sudoku/sudoku_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@
from pyswip.prolog import Prolog
from pyswip.easy import *

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

try:
import urllib.request as urllib_request
except ImportError:
import urllib as urllib_request
from html.parser import HTMLParser

import urllib.request as urllib_request


class DailySudokuPuzzle(HTMLParser):
Expand Down
80 changes: 80 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "pyswip"
dynamic = ["version"]
description = "PySwip enables querying SWI-Prolog in your Python programs."
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
authors = [
{ name = "Yuce Tekol", email = "[email protected]" },
]
keywords = [
"ai",
"artificial intelligence",
"ctypes",
"ffi",
"prolog",
]
classifiers = [
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Software Development :: Libraries :: Python Modules",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
]

[project.urls]
Download = "https://github.com/yuce/pyswip/releases"
Homepage = "https://github.com/yuce/pyswip"

[tool.hatch.version]
path = "pyswip/__init__.py"

[tool.hatch.build.targets.sdist]
include = [
"/pyswip",
]

[tool.hatch.build.targets.wheel]
packages = ["src/foo"]

[tool.hatch.envs.test]
dependencies = [
"pytest",
"pytest-cov",
]

[tool.hatch.envs.test.scripts]
all = "py.test tests --verbose"
coverage = "py.test tests --verbose --cov=pyswip"

[tool.hatch.envs.upload-coverage]
dependencies = [
"coveralls",
]

[tool.hatch.envs.upload-coverage.scripts]
coveralls = "coveralls"

[tool.hatch.envs.types]
extra-dependencies = [
"mypy>=1.0.0",
]
[tool.hatch.envs.types.scripts]
check = "mypy --install-types --non-interactive {args:pyswip tests}"

[tool.ruff.lint]
ignore = ["F403", "F405", "E721"]
2 changes: 1 addition & 1 deletion pyswip/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
# PySwip version
__VERSION__ = "0.2.11"

from pyswip.prolog import Prolog
from pyswip.prolog import Prolog as Prolog
from pyswip.easy import *
7 changes: 1 addition & 6 deletions pyswip/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ def _findSwiplFromExec():
swiHome = None

try: # try to get library path from swipl executable.

# We may have pl or swipl as the executable
try:
cmd = Popen(["swipl", "--dump-runtime-variables"], stdout=PIPE)
except OSError:
cmd = Popen(["pl", "--dump-runtime-variables"], stdout=PIPE)
cmd = Popen(["swipl", "--dump-runtime-variables"], stdout=PIPE)
ret = cmd.communicate()

# Parse the output into a dictionary
Expand Down Expand Up @@ -1401,7 +1397,6 @@ def exit(self, code=0):
def cleanupProlog():
# only do something if prolog has been initialised
if PL_is_initialised(None, None):

# clean up the prolog system using the caught exit code
# if exit code is None, the program exits normally and we can use 0
# instead.
Expand Down
2 changes: 1 addition & 1 deletion pyswip/easy.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def putTerm(term, value):
raise Exception("Not implemented")


def putList(l, ls):
def putList(l, ls): # noqa: E741
PL_put_nil(l)
for item in reversed(ls):
a = PL_new_term_ref() # PL_new_term_refs(len(ls))
Expand Down
Loading

0 comments on commit c6752ee

Please sign in to comment.