From fd9a1ccf9e5aa323f733234cb6180630c71e2a74 Mon Sep 17 00:00:00 2001 From: Erel Segal-Halevi Date: Wed, 18 Oct 2023 17:29:16 +0300 Subject: [PATCH] remove dicttools dependency --- fairpyx/algorithms/almost_egalitarian.py | 2 +- .../algorithms/iterated_maximum_matching.py | 2 +- fairpyx/algorithms/utilitarian_matching.py | 2 +- fairpyx/utils/graph_utils.py | 2 +- fairpyx/utils/test_utils.py | 24 +++++++++++++++++++ fairpyx/zalternatives/yekta_day.py | 2 +- requirements.txt | 2 -- setup.py | 2 +- 8 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 fairpyx/utils/test_utils.py diff --git a/fairpyx/algorithms/almost_egalitarian.py b/fairpyx/algorithms/almost_egalitarian.py index c9d6474..96a4508 100644 --- a/fairpyx/algorithms/almost_egalitarian.py +++ b/fairpyx/algorithms/almost_egalitarian.py @@ -27,7 +27,7 @@ def almost_egalitarian_allocation(alloc: AllocationBuilder, surplus_donation:boo >>> from fairpyx.adaptors import divide - >>> from dicttools import stringify + >>> from fairpyx.utils.test_utils import stringify >>> instance = Instance(valuations={"avi": {"x":5, "y":4, "z":3, "w":2}, "beni": {"x":2, "y":3, "z":4, "w":5}}, agent_capacities=1, item_capacities=1) >>> stringify(divide(almost_egalitarian_allocation, instance=instance)) diff --git a/fairpyx/algorithms/iterated_maximum_matching.py b/fairpyx/algorithms/iterated_maximum_matching.py index f5d2f25..1b1c5ab 100644 --- a/fairpyx/algorithms/iterated_maximum_matching.py +++ b/fairpyx/algorithms/iterated_maximum_matching.py @@ -21,7 +21,7 @@ def iterated_maximum_matching(alloc:AllocationBuilder, adjust_utilities:bool=Fal :param alloc: an allocation builder, which tracks the allocation and the remaining capacity for items and agents. of the fair course allocation problem. :param adjust_utilities: if True, the utilities of agents, who did not get their max-value item in the current iteration, will be adjusted to give them a higher chance in the next iteration. - >>> from dicttools import stringify + >>> from fairpyx.utils.test_utils import stringify >>> from fairpyx.adaptors import divide >>> instance = Instance(valuations={"avi": {"x":5, "y":4, "z":3, "w":2}, "beni": {"x":2, "y":3, "z":4, "w":5}}, agent_capacities=1, item_capacities=1) diff --git a/fairpyx/algorithms/utilitarian_matching.py b/fairpyx/algorithms/utilitarian_matching.py index 195f1ee..6f43860 100644 --- a/fairpyx/algorithms/utilitarian_matching.py +++ b/fairpyx/algorithms/utilitarian_matching.py @@ -18,7 +18,7 @@ def utilitarian_matching(alloc: AllocationBuilder): """ Finds an allocation maximizing the sum of utilities for the given instance, using max-weight many-to-many matching. - >>> from dicttools import stringify + >>> from fairpyx.utils.test_utils import stringify >>> from fairpyx.adaptors import divide >>> instance = Instance(valuations={"avi": {"x":5, "y":4, "z":3, "w":2}, "beni": {"x":2, "y":3, "z":4, "w":5}}, agent_capacities=1, item_capacities=1) diff --git a/fairpyx/utils/graph_utils.py b/fairpyx/utils/graph_utils.py index 614a3b9..df17300 100644 --- a/fairpyx/utils/graph_utils.py +++ b/fairpyx/utils/graph_utils.py @@ -15,7 +15,7 @@ def many_to_many_matching(item_capacities: dict[any,int], agent_capacities:dict[ """ Computes a many-to-many matching of items to agents. - >>> from dicttools import stringify + >>> from fairpyx.utils.test_utils import stringify >>> valuations = {"a":{"x":11, "y":22}, "b":{"x":33,"y":55}} >>> stringify(many_to_many_matching(item_capacities={"x":2, "y":2}, agent_capacities={"a":1, "b":1}, valuations=valuations)) diff --git a/fairpyx/utils/test_utils.py b/fairpyx/utils/test_utils.py new file mode 100644 index 0000000..9dbd829 --- /dev/null +++ b/fairpyx/utils/test_utils.py @@ -0,0 +1,24 @@ +""" +Copied from dicttools library: https://github.com/trzemecki/dicttools/blob/39c19d9a5ecc965a58eed3eab18ac3e5e342fca2/dicttools/functions.py#L379C18-L379C18 +""" + +def stringify(d): + """ + Returns a canonical string representation of the given dict, + by sorting its items recursively. + This is especially useful in doctests:: + + >>> stringify({"a":1,"b":2,"c":{"d":3,"e":4}}) + '{a:1, b:2, c:{d:3, e:4}}' + """ + d2 = {} + + for k, v in d.items(): + d2[k] = stringify(v) if isinstance(v, dict) else v + + return "{" + ", ".join(["{}:{}".format(k, v) for k, v in sorted(d2.items())]) + "}" + + +if __name__ == "__main__": + import doctest, sys + print("\n",doctest.testmod(), "\n") diff --git a/fairpyx/zalternatives/yekta_day.py b/fairpyx/zalternatives/yekta_day.py index 46853ee..c370d9f 100644 --- a/fairpyx/zalternatives/yekta_day.py +++ b/fairpyx/zalternatives/yekta_day.py @@ -24,7 +24,7 @@ def yekta_day(alloc: AllocationBuilder): """ :param alloc: an allocation builder, which tracks the allocation and the remaining capacity for items and agents. of the fair course allocation problem. - >>> from dicttools import stringify + >>> from fairpyx.utils.test_utils import stringify >>> from fairpy.courses.adaptors import divide >>> instance = Instance(valuations={"avi": {"x":5, "y":4, "z":3, "w":2}, "beni": {"x":2, "y":3, "z":4, "w":5}}, agent_capacities=1, item_capacities=1) diff --git a/requirements.txt b/requirements.txt index dc51112..b7354e3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,5 +10,3 @@ cvxpy_base>=1.1.17 # prtpy>=0.7.0 # more_itertools # scs -dicttools @ git+https://github.com/trzemecki/dicttools.git - diff --git a/setup.py b/setup.py index 2e7a1de..9edb64b 100644 --- a/setup.py +++ b/setup.py @@ -52,5 +52,5 @@ # Publish to test PyPI: # twine upload --repository testpypi dist/* -# Publish to real PyPI: +# Publish to real PyPI (make sure you set the environment variables TWINE_USERNAME and TWINE_PASSWORD): # twine upload --repository pypi dist/*