-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |