Skip to content

Recurring time window filter #51

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

Merged
merged 34 commits into from
Apr 23, 2025
Merged

Recurring time window filter #51

merged 34 commits into from
Apr 23, 2025

Conversation

mrm9084
Copy link
Contributor

@mrm9084 mrm9084 commented Dec 11, 2024

Description

Add the Recurring time window filter to python Feature Management.

@mrm9084 mrm9084 requested a review from Copilot December 11, 2024 23:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 suggestions.

Comments skipped due to low confidence (1)

featuremanagement/_time_window_filter/_recurrence_validator.py:113

  • The error message should specify the exact parameters for clarity, e.g., 'The Recurrence.Range.EndDate should be after the Start date'.
raise ValueError("The Recurrence.Range.EndDate should be after the Start")

@zhiyuanliang-ms
Copy link
Contributor

zhiyuanliang-ms commented Dec 12, 2024

@mrm9084 Will you add testcases in this PR? The testsuite in .NET FM repo or in this PR are very comprehensive.


def _sort_days_of_week(days_of_week: List[int], first_day_of_week: int) -> List[int]:
sorted_days = sorted(days_of_week)
return sorted_days[sorted_days.index(first_day_of_week) :] + sorted_days[: sorted_days.index(first_day_of_week)]
Copy link
Contributor

Choose a reason for hiding this comment

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

What will happen if first_day_of_week is not in the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is guaranteed to be. In the RecurrencePattern init we validate that it is, and if it isn't we default to the index value of 0, which is Sunday.

Copy link
Contributor

Choose a reason for hiding this comment

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

In python Sunday index is 6, isn't it?
image
The first_day_of_week is Sunday (6) by default.
Let's say days_of_week is [0, 1] (Mon and Tue)
image
Won't _sort_days_of_week break as Sunday (6) is not in the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For my original reply, when I say 0 is the default, i.e. Sunday, That was prior to a commit, which I updated the list to start with Monday, which it does in .Net.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, if you look at the new sorted function, it fixes this by finding the closest day of the week to the first day of the week if the first day of the week isn't in the list.

for day in days_of_week:
self.days_of_week.append(self.days.index(day))
first_day_of_week = pattern_data.get("FirstDayOfWeek", "Sunday")
self.first_day_of_week = self.days.index(first_day_of_week) if first_day_of_week in self.days else 0
Copy link
Contributor

Choose a reason for hiding this comment

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

from email.utils import parsedate_to_datetime

s = "Sun, 05 Jan 2025 20:00:00 GMT"
t = parsedate_to_datetime(s)
print(t.weekday())

This produced 6 on my laptop, I am using python 3.11.9.

first day of week should be Sunday by default. Sunday index is 6 and Monday is 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.

I've thought about this and I don't want to mess with the days of the week in python. So, what I think should be fine is that I update. RecurrencePattern to start on Monday which is correct in python, but still have the default first day of the week to be "Sunday" and as the input and default are both strings and we get the index from the list we should be fine. Thoughts?

@mrm9084 mrm9084 requested a review from Copilot April 2, 2025 23:29
@zhiyuanliang-ms
Copy link
Contributor

Can we add more testcases like this PR did?

@mrm9084
Copy link
Contributor Author

mrm9084 commented Apr 18, 2025

Why does it PR fail to build for python 3.8?

I looked into it a bit, seems like a pylon bug in 3.8. I've added an empty init to the test folder which seems to have fixed it. Not sure why it wasn't an issue till now.

@mrm9084 mrm9084 requested a review from Copilot April 21, 2025 17:00
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a recurring time window filter to the Python Feature Management library. Key changes include implementing recurrence models, validators, and evaluators, as well as updating default filter logic and adding comprehensive tests.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/time_window_filter/test_time_window_filter_models.py Added tests for validating recurrence pattern and range models.
tests/time_window_filter/test_recurrence_validator.py Introduced tests for recurrence settings validation.
tests/time_window_filter/test_recurrence_evaluator.py Provided tests for recurrence evaluator behavior.
featuremanagement/_time_window_filter/_recurrence_validator.py Implemented validation logic for recurrence settings.
featuremanagement/_time_window_filter/_recurrence_evaluator.py Implemented evaluator logic for checking matching time window occurrences.
featuremanagement/_time_window_filter/_models.py Defined recurrence model classes with pattern and range support.
featuremanagement/_time_window_filter/init.py Exposed the recurrence filter functionality.
featuremanagement/_defaultfilters.py Updated default filter to incorporate recurrence data and evaluation.

except TypeError as e:
# Python 3.9 and earlier throw TypeError if the string is not in RFC 2822 format.
raise ValueError(f"Invalid value for EndDate: {end_date_str}") from e
self.num_of_occurrences = range_data.get("NumberOfOccurrences", math.pow(2, 63) - 1)
Copy link
Preview

Copilot AI Apr 21, 2025

Choose a reason for hiding this comment

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

Using math.pow(2, 63) returns a float, which may lead to type inconsistencies. Consider using the integer expression (2 ** 63 - 1) to ensure the default occurrence count is an integer.

Copilot uses AI. Check for mistakes.

if start > day_with_min_offset:
num_of_occurrences = 0
day_with_min_offset = start
if now < day_with_min_offset:
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't guard that start <= now in this function. But I think it is ok

@mrm9084 mrm9084 merged commit b960b3d into main Apr 23, 2025
6 checks passed
@mrm9084 mrm9084 deleted the RecurringTimeWindowFilter branch April 23, 2025 18:03
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.

3 participants