Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subtract: Remove the intersection of two ranges #36

Merged
merged 1 commit into from
Jun 9, 2021
Merged

Subtract: Remove the intersection of two ranges #36

merged 1 commit into from
Jun 9, 2021

Conversation

bramski
Copy link
Contributor

@bramski bramski commented Jun 4, 2021

Subtracting two ranges will result in an array of ranges:
empty set -- the second range is equal or greater than the first
single set -- the second set overlaps the beginning or end of the first or does not intersect at all
two ranges -- the second set is within the first and slices it into two new pieces

:Output:
.. parsed-literal::

[2015-03-22T10:00:00+0900 - 2015-03-22T05:00+0900]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be [2015-03-22T10:00:00+0900 - 2015-03-22T10:05:00+0900]

class TestDateTimeRange_subtract:
@pytest.mark.parametrize(
["lhs", "rhs", "expected"],
[

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test case for when the lhs and rhs do not overlap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. See third test case.

:Output:
.. parsed-literal::

[2015-03-22T10:00:00+0900 - 2015-03-22T10:05:00+0900]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattasaminew addressed

Copy link

@mattasaminew mattasaminew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

time_range.subtract(x)

:Output:
::
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A blank line is required after here to properly rendered as a reStructuredText.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

@@ -0,0 +1,15 @@
Make an intersected time range
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this titled as you intended?

test/test_dtr.py Outdated
)
def test_exception(self, lhs, rhs, expected):
with pytest.raises(expected):
lhs.is_intersection(rhs)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, subtract method should be called here.

start_time_format=self.start_time_format, end_time_format=self.end_time_format),
DateTimeRange(start_datetime=overlap.end_datetime, end_datetime=self.end_datetime,
start_time_format=self.start_time_format, end_time_format=self.end_time_format)
]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current implementation, a DateTimeRange split in two if subtract a DateTimeRange with a range that start-time equals end-time.
For example:

>>> from datetimerange import DateTimeRange
>>> time_range = DateTimeRange("2015-01-22T09:50:00+0900", "2015-01-22T10:00:00+0900")
>>> x = DateTimeRange("2015-01-22T09:55:00+0900", "2015-01-22T09:55:00+0900")
>>> time_range.subtract(x)
[2015-01-22T09:50:00+0900 - 2015-01-22T09:55:00+0900, 2015-01-22T09:55:00+0900 - 2015-01-22T10:00:00+0900]

In this case, returning [self.copy()] might be more preferable.
What do you think about this behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. It's a zero length range. I would say yeah it's not really a valid case for breaking up the range and it should not cut it into two.

from datetimerange import DateTimeRange
dtr0 = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
dtr1 = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
dtr0.subtract(dtr1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above four lines need an extra indentation to be properly displayed as reStructuredText.

The result will be [self.copy()] if the second range does not overlap the first
The result will be [] if the second range wholly encompasses the first range
The result will be [new_range] if the second range overlaps one end of the range
The result will be [new_range1, new_range2] if the second range is an internal sub range of the first
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above lines are a little bit hard to read in the HTML after the docs build.
How about modifying like the followings:

        Remove a time range from this one and return the result.

        - The result will be ``[self.copy()]`` if the second range does not overlap the first
        - The result will be ``[]`` if the second range wholly encompasses the first range
        - The result will be ``[new_range]`` if the second range overlaps one end of the range
        - The result will be ``[new_range1, new_range2]`` if the second range is an internal sub range of the first

@thombashi
Copy link
Owner

Please apply code formatter. You can do that as follows:

make setup
make fmt

Subtracting two ranges will result in an array of ranges:
empty set -- the second range is equal or greater than the first
single set -- the second set overlaps the beginning or end of the first or does not intersect at all
two ranges -- the second set is within the first and slices it into two new pieces
[
DateTimeRange("2015-01-22T09:50:00+0900", "2015-01-22T10:30:00+0900"),
DateTimeRange("2015-01-22T09:55:00+0900", "2015-01-22T09:55:00+0900"),
[DateTimeRange("2015-01-22T09:50:00+0900", "2015-01-22T10:30:00+0900")],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thombashi test case to cover that case.

"""
overlap = self.intersection(x)
# No intersection, return a copy of the original
if not overlap.is_set() or overlap.get_timedelta_second() <= 0:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thombashi covers the case where you attempt to subtract a zero length range.

@bramski bramski requested a review from thombashi June 8, 2021 18:19
@bramski
Copy link
Contributor Author

bramski commented Jun 8, 2021

@thombashi looks like the test failures are due to python 3.5. This is all passing locally.

Copy link
Owner

@thombashi thombashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
Thank you for your pull request.

@thombashi
Copy link
Owner

@thombashi looks like the test failures are due to python 3.5. This is all passing locally.

I'll fix CI.

@thombashi thombashi merged commit e90c936 into thombashi:master Jun 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants