Skip to content

Commit 34bec23

Browse files
Targeting Update (#26)
* telemetry support * updating from feedback * Changes from design meeting * formatting, updating samples * fixing merge issue * typing issue * fixing test validations from changes * Update test_json_validations.py * Apply suggestions from code review Co-authored-by: Zhiyuan Liang <[email protected]> * Updating doc strings * Spelling * Updating async name. * Adding missing eval reason. Fixed formatting. * targeting changes * Updating Readme and Samples * Update feature_flag_with_azure_app_configuration_sample.py * Updating return * Comments + Async * Updating passing around evaluation event * formatting * Adding more code comments --------- Co-authored-by: Zhiyuan Liang <[email protected]>
1 parent e557d54 commit 34bec23

28 files changed

+651
-367
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,16 @@ The Targeting Filter provides the capability to enable a feature for a target au
220220
You can provide the current user info through `kwargs` when calling `isEnabled`.
221221

222222
```python
223+
from featuremanagement import FeatureManager, TargetingContext
224+
223225
# Returns true, because user1 is in the Users list
224-
feature_manager.is_enabled("Beta", user="user1", groups=["group1"])
226+
feature_manager.is_enabled("Beta", TargetingContext(user_id="user1", groups=["group1"]))
225227

226228
# Returns false, because group2 is in the Exclusion.Groups list
227-
feature_manager.is_enabled("Beta", user="user1", groups=["group2"])
229+
feature_manager.is_enabled("Beta", TargetingContext(user_id="user1", groups=["group2"]))
228230

229231
# Has a 50% chance of returning true, but will be conisistent for the same user
230-
feature_manager.is_enabled("Beta", user="user4")
232+
feature_manager.is_enabled("Beta", TargetingContext(user_id="user4"))
231233
```
232234

233235
#### Custom Filters

featuremanagement/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@
66
from ._featuremanager import FeatureManager
77
from ._featurefilters import FeatureFilter
88
from ._defaultfilters import TimeWindowFilter, TargetingFilter
9-
from ._models._variant import Variant
9+
from ._models import FeatureFlag, Variant, TargetingContext
1010

1111
from ._version import VERSION
1212

1313
__version__ = VERSION
14-
__all__ = ["FeatureManager", "TimeWindowFilter", "TargetingFilter", "FeatureFilter", "Variant"]
14+
__all__ = [
15+
"FeatureManager",
16+
"TimeWindowFilter",
17+
"TargetingFilter",
18+
"FeatureFilter",
19+
"FeatureFlag",
20+
"Variant",
21+
"TargetingContext",
22+
]

featuremanagement/_defaultfilters.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,22 @@
3535

3636
class TargetingException(Exception):
3737
"""
38-
Exception raised when the targeting filter is not configured correctly
38+
Exception raised when the targeting filter is not configured correctly.
3939
"""
4040

4141

4242
@FeatureFilter.alias("Microsoft.TimeWindow")
4343
class TimeWindowFilter(FeatureFilter):
4444
"""
45-
Feature Filter that determines if the current time is within the time window
45+
Feature Filter that determines if the current time is within the time window.
4646
"""
4747

4848
def evaluate(self, context, **kwargs):
4949
"""
50-
Determine if the feature flag is enabled for the given context
50+
Determine if the feature flag is enabled for the given context.
5151
52-
:keyword Mapping context: Mapping with the Start and End time for the feature flag
53-
:paramtype context: Mapping
54-
:return: True if the current time is within the time window
52+
:keyword Mapping context: Mapping with the Start and End time for the feature flag.
53+
:return: True if the current time is within the time window.
5554
:rtype: bool
5655
"""
5756
start = context.get(PARAMETERS_KEY, {}).get(START_KEY)
@@ -72,7 +71,7 @@ def evaluate(self, context, **kwargs):
7271
@FeatureFilter.alias("Microsoft.Targeting")
7372
class TargetingFilter(FeatureFilter):
7473
"""
75-
Feature Filter that determines if the user is targeted for the feature flag
74+
Feature Filter that determines if the user is targeted for the feature flag.
7675
"""
7776

7877
@staticmethod
@@ -98,11 +97,10 @@ def _target_group(self, target_user, target_group, group, feature_flag_name):
9897

9998
def evaluate(self, context, **kwargs):
10099
"""
101-
Determine if the feature flag is enabled for the given context
100+
Determine if the feature flag is enabled for the given context.
102101
103-
:keyword Mapping context: Context for evaluating the user/group
104-
:paramtype context: Mapping
105-
:return: True if the user is targeted for the feature flag
102+
:keyword Mapping context: Context for evaluating the user/group.
103+
:return: True if the user is targeted for the feature flag.
106104
:rtype: bool
107105
"""
108106
target_user = kwargs.get(TARGETED_USER_KEY, None)

featuremanagement/_featurefilters.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@
88

99
class FeatureFilter(ABC):
1010
"""
11-
Parent class for all feature filters
11+
Parent class for all feature filters.
1212
"""
1313

1414
@abstractmethod
1515
def evaluate(self, context, **kwargs):
1616
"""
17-
Determine if the feature flag is enabled for the given context
17+
Determine if the feature flag is enabled for the given context.
1818
19-
:param Mapping context: Context for the feature flag
20-
:paramtype context: Mapping
19+
:param Mapping context: Context for the feature flag.
2120
"""
2221

2322
@property
2423
def name(self):
2524
"""
26-
Get the name of the filter
25+
Get the name of the filter.
2726
28-
:return: Name of the filter, or alias if it exists
27+
:return: Name of the filter, or alias if it exists.
2928
:rtype: str
3029
"""
3130
if hasattr(self, "_alias"):
@@ -35,10 +34,10 @@ def name(self):
3534
@staticmethod
3635
def alias(alias):
3736
"""
38-
Decorator to set the alias for the filter
37+
Decorator to set the alias for the filter.
3938
40-
:param str alias: Alias for the filter
41-
:return: Decorator
39+
:param str alias: Alias for the filter.
40+
:return: Decorator.
4241
:rtype: callable
4342
"""
4443

0 commit comments

Comments
 (0)