From 6a53724432d00a0e3a440bb71f8ed7c0bbc9233c Mon Sep 17 00:00:00 2001 From: salt-die <53280662+salt-die@users.noreply.github.com> Date: Sun, 26 Nov 2023 14:00:24 -0600 Subject: [PATCH] Added `x`. --- src/mind_the_gaps/__init__.py | 3 ++- src/mind_the_gaps/var.py | 44 +++++++++++++++++++++++++++++++++++ tests/test_x.py | 15 ++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/mind_the_gaps/var.py create mode 100644 tests/test_x.py diff --git a/src/mind_the_gaps/__init__.py b/src/mind_the_gaps/__init__.py index 9c10ece..bd6cbaf 100644 --- a/src/mind_the_gaps/__init__.py +++ b/src/mind_the_gaps/__init__.py @@ -1,5 +1,6 @@ """A library for unions, intersections, subtractions, and xors of intervals (gaps).""" from .gaps import Endpoint, Gaps, NegativeInfinity, PositiveInfinity +from .var import x -__all__ = ["Gaps", "PositiveInfinity", "NegativeInfinity", "Endpoint"] +__all__ = ["Gaps", "PositiveInfinity", "NegativeInfinity", "Endpoint", "x"] __version__ = "0.2.0" diff --git a/src/mind_the_gaps/var.py b/src/mind_the_gaps/var.py new file mode 100644 index 0000000..7ca8199 --- /dev/null +++ b/src/mind_the_gaps/var.py @@ -0,0 +1,44 @@ +from dataclasses import dataclass + +from .gaps import Endpoint, SupportsLessThan + +__all__ = ["x"] + + +@dataclass +class _Var[T: SupportsLessThan]: + name: str + + def __str__(self): + return self.name + + def __gt__(self, value: T) -> Endpoint: + return Endpoint(value, "(") + + def __ge__(self, value: T) -> Endpoint: + return Endpoint(value, "[") + + def __lt__(self, value: T) -> Endpoint: + return Endpoint(value, ")") + + def __le__(self, value: T) -> Endpoint: + return Endpoint(value, "]") + + +x = _Var("x") +""" +A very convenient constructor for `Endpoints`. + +It's best to start with an example:: + >>> from mind_the_gaps import x + >>> 0 <= x + Endpoint(value=0, boundary='[') + >>> x < 1 + Endpoint(value=1, boundary=')') + >>> print(Gaps([0 <= x, x < 1])) + {[0, 1)} + +Values compared with `x` will return an endpoint. The type of comparison determines +the boundary of the endpoint. `>` is left-open, `>=` is left-closed, `<` is right-open, +and `<=` is right-closed. +""" diff --git a/tests/test_x.py b/tests/test_x.py new file mode 100644 index 0000000..26db71d --- /dev/null +++ b/tests/test_x.py @@ -0,0 +1,15 @@ +from mind_the_gaps import Endpoint, Gaps, x + + +def test_x_closed(): + assert Gaps([0 <= x, x <= 1, 2 <= x, x <= 3]) == Gaps([0, 1, 2, 3]) + + +def test_x_open(): + assert Gaps([0 < x, x < 1, 2 < x, x < 3]) == Gaps( + [Endpoint(0, "("), Endpoint(1, ")"), Endpoint(2, "("), Endpoint(3, ")")] + ) + + +def test_x_singleton(): + assert Gaps([0 <= x, x <= 0]) == Gaps([0, 0])