From 98cd2c100b78e7dc092eb8e832468597e045bded Mon Sep 17 00:00:00 2001 From: Tim Hutt Date: Fri, 7 Jun 2024 10:07:26 +0100 Subject: [PATCH 1/6] Add ability to pass custom Random to MinConflictsSolver This allows it to be used repeatably. --- CHANGELOG.md | 3 ++- constraint/solvers.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eac74da..f040eca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,12 +20,13 @@ in every released version. - Split the codebase into multiple files for convenience - Switched from `setup.py` to `pyproject.toml` - Achieved and requires test coverage of at least 65% - - Added `nox` for testing against all supported Python versions + - Added `nox` for testing against all supported Python versions - Added `ruff` for codestyle testing - Moved the documentation to GitHub Pages - Moved test & publishing automation to GitHub Actions - Switched dependency & build system to Poetry - Dropped Python 3.4, 3.5, 3.6, 3.7, 3.8 support + - Add ability to pass custom `Random` to `MinConflictsSolver`. ### Version 1.4.0 diff --git a/constraint/solvers.py b/constraint/solvers.py index e28b271..9923799 100644 --- a/constraint/solvers.py +++ b/constraint/solvers.py @@ -545,24 +545,30 @@ class MinConflictsSolver(Solver): NotImplementedError: MinConflictsSolver doesn't provide iteration """ - def __init__(self, steps=1000): + def __init__(self, steps=1000, rand=None): """Initialization method. Args: steps (int): Maximum number of steps to perform before giving up when looking for a solution (default is 1000) + rand (Random): Optional random.Random instance to use for + repeatability. """ self._steps = steps + self._rand = rand def getSolution(self, domains: dict, constraints: List[tuple], vconstraints: dict): # noqa: D102 + choice = self._rand.choice if self._rand is not None else random.choice + shuffle = self._rand.shuffle if self._rand is not None else random.shuffle + assignments = {} # Initial assignment for variable in domains: - assignments[variable] = random.choice(domains[variable]) + assignments[variable] = choice(domains[variable]) for _ in range(self._steps): conflicted = False lst = list(domains.keys()) - random.shuffle(lst) + shuffle(lst) for variable in lst: # Check if variable is not in conflict for constraint, variables in vconstraints[variable]: @@ -586,7 +592,7 @@ def getSolution(self, domains: dict, constraints: List[tuple], vconstraints: dic del minvalues[:] minvalues.append(value) # Pick a random one from these values. - assignments[variable] = random.choice(minvalues) + assignments[variable] = choice(minvalues) conflicted = True if not conflicted: return assignments From 97fdc5ad7262e8745a5112ecbc4e1805454443ea Mon Sep 17 00:00:00 2001 From: Floris-Jan Willemsen Date: Sat, 1 Feb 2025 14:15:53 +0100 Subject: [PATCH 2/6] Update CHANGELOG.md Removed suggested changes to CHANGELOG as they should not be made from a pull request --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d525fd..8a25760 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,7 +26,6 @@ All notable changes to this code base will be documented in this file, for every - Moved test & publishing automation to GitHub Actions - Switched dependency & build system to Poetry - Dropped Python 3.4, 3.5, 3.6, 3.7, 3.8 support - - Add ability to pass custom `Random` to `MinConflictsSolver`. ### Version 1.4.0 From c73fd0257cf0e52f8e5b7f1276bdc93f34e3e3fe Mon Sep 17 00:00:00 2001 From: Floris-Jan Willemsen Date: Sat, 1 Feb 2025 14:16:37 +0100 Subject: [PATCH 3/6] Update solvers.py Changed type hint from List to list --- constraint/solvers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/constraint/solvers.py b/constraint/solvers.py index b82352a..0a78f09 100644 --- a/constraint/solvers.py +++ b/constraint/solvers.py @@ -556,7 +556,7 @@ def __init__(self, steps=1000, rand=None): self._steps = steps self._rand = rand - def getSolution(self, domains: dict, constraints: List[tuple], vconstraints: dict): # noqa: D102 + def getSolution(self, domains: dict, constraints: list[tuple], vconstraints: dict): # noqa: D102 choice = self._rand.choice if self._rand is not None else random.choice shuffle = self._rand.shuffle if self._rand is not None else random.shuffle assignments = {} From 221b9f52188dc8a6dfa0e6b775448bfefb9b01ba Mon Sep 17 00:00:00 2001 From: Floris-Jan Willemsen Date: Sat, 1 Feb 2025 15:08:56 +0100 Subject: [PATCH 4/6] Fixed a typo in the changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a25760..dc4c544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log -## Python Contraint +## Python Constraint All notable changes to this code base will be documented in this file, for every released major and minor version. From a500ea3f3f83849c301b174c597eb48d758e7e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iy=C3=A1n=20M=C3=A9ndez=20Veiga?= Date: Sat, 1 Feb 2025 18:28:11 +0100 Subject: [PATCH 5/6] Include .so compiled files to wheel package Apparently, poetry does not include .so files by default when using packages. To solve this, we add an additional include line that ensures the compiled cpython shared libraries are included in the wheel packages. In addition, tests are also included now in the sdist tarball. --- pyproject.toml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 1facdaa..af85dc5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,11 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "python-constraint2" # when set back to "python-constraint", don't forget to remove '2' in other places (e.g. README) -packages = [{ include = "constraint", from = "." }] +packages = [ + { include = "constraint", from = "." }, + { include = "tests", format = "sdist" } +] +include = [{ path = "constraint/*.so", format = "wheel" }] description = "python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains." version = "2.0.1" # adhere to PEP440 versioning: https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#id55 authors = [ From 97f4cd60115872b8833066714710d8725ca99960 Mon Sep 17 00:00:00 2001 From: Floris-Jan Willemsen Date: Sun, 2 Feb 2025 00:45:03 +0100 Subject: [PATCH 6/6] Updated dependencies, bumped version --- poetry.lock | 8 ++++---- pyproject.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/poetry.lock b/poetry.lock index 01d53f9..ed3a5dd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -29,18 +29,18 @@ test = ["coverage", "mypy", "pexpect", "ruff", "wheel"] [[package]] name = "babel" -version = "2.16.0" +version = "2.17.0" description = "Internationalization utilities" optional = false python-versions = ">=3.8" groups = ["docs"] files = [ - {file = "babel-2.16.0-py3-none-any.whl", hash = "sha256:368b5b98b37c06b7daf6696391c3240c938b37767d4584413e8438c5c435fa8b"}, - {file = "babel-2.16.0.tar.gz", hash = "sha256:d1f3554ca26605fe173f3de0c65f750f5a42f924499bf134de6423582298e316"}, + {file = "babel-2.17.0-py3-none-any.whl", hash = "sha256:4d0b53093fdfb4b21c92b5213dba5a1b23885afa8383709427046b21c366e5f2"}, + {file = "babel-2.17.0.tar.gz", hash = "sha256:0c54cffb19f690cdcc52a3b50bcbf71e07a808d1c80d549f2459b9d2cf0afb9d"}, ] [package.extras] -dev = ["freezegun (>=1.0,<2.0)", "pytest (>=6.0)", "pytest-cov"] +dev = ["backports.zoneinfo", "freezegun (>=1.0,<2.0)", "jinja2 (>=3.0)", "pytest (>=6.0)", "pytest-cov", "pytz", "setuptools", "tzdata"] [[package]] name = "certifi" diff --git a/pyproject.toml b/pyproject.toml index af85dc5..353c3c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ packages = [ ] include = [{ path = "constraint/*.so", format = "wheel" }] description = "python-constraint is a module for efficiently solving CSPs (Constraint Solving Problems) over finite domains." -version = "2.0.1" # adhere to PEP440 versioning: https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#id55 +version = "2.0.2" # adhere to PEP440 versioning: https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#id55 authors = [ "Gustavo Niemeyer ", "Sébastien Celles ",