Skip to content

Commit

Permalink
Added x.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Nov 26, 2023
1 parent 960c1b3 commit 6a53724
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/mind_the_gaps/__init__.py
Original file line number Diff line number Diff line change
@@ -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"
44 changes: 44 additions & 0 deletions src/mind_the_gaps/var.py
Original file line number Diff line number Diff line change
@@ -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.
"""
15 changes: 15 additions & 0 deletions tests/test_x.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 6a53724

Please sign in to comment.