From 705e83270bae95d3db579855d0a6dcbd1c443df2 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 13 Dec 2022 20:13:10 -0800 Subject: [PATCH 1/2] refactor common code --- reddit_decider/__init__.py | 111 ++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 62 deletions(-) diff --git a/reddit_decider/__init__.py b/reddit_decider/__init__.py index f2ffc5c..3f96175 100644 --- a/reddit_decider/__init__.py +++ b/reddit_decider/__init__.py @@ -318,19 +318,10 @@ def get_variant( :return: Variant name if a variant is assigned, :code:`None` otherwise. """ - if self._internal is None: - logger.error("RustDecider is None--did not initialize.") - return None - ctx = self._decider_context.to_dict() + decision = self._get_decision(experiment_name, ctx) - try: - decision = self._internal.choose(experiment_name, ctx) - except FeatureNotFoundException as exc: - warnings.warn(str(exc)) - return None - except DeciderException as exc: - logger.info(str(exc)) + if decision is None: return None event_context_fields = self._decider_context.to_event_dict() @@ -356,19 +347,10 @@ def get_variant_without_expose(self, experiment_name: str) -> Optional[str]: :return: Variant name if a variant is assigned, None otherwise. """ - if self._internal is None: - logger.error("RustDecider is None--did not initialize.") - return None - ctx = self._decider_context.to_dict() + decision = self._get_decision(experiment_name, ctx) - try: - decision = self._internal.choose(experiment_name, ctx) - except FeatureNotFoundException as exc: - warnings.warn(str(exc)) - return None - except DeciderException as exc: - logger.info(str(exc)) + if decision is None: return None event_context_fields = self._decider_context.to_event_dict() @@ -476,20 +458,12 @@ def get_variant_for_identifier( ) return None - if self._internal is None: - logger.error("RustDecider is None--did not initialize.") - return None - ctx = self._decider_context.to_dict() ctx[identifier_type] = identifier - try: - decision = self._internal.choose(experiment_name, ctx) - except FeatureNotFoundException as exc: - warnings.warn(str(exc)) - return None - except DeciderException as exc: - logger.info(str(exc)) + decision = self._get_decision(experiment_name, ctx) + + if decision is None: return None event_context_fields = self._decider_context.to_event_dict() @@ -544,20 +518,12 @@ def get_variant_for_identifier_without_expose( ) return None - if self._internal is None: - logger.error("RustDecider is None--did not initialize.") - return None - ctx = self._decider_context.to_dict() ctx[identifier_type] = identifier - try: - decision = self._internal.choose(experiment_name, ctx) - except FeatureNotFoundException as exc: - warnings.warn(str(exc)) - return None - except DeciderException as exc: - logger.info(str(exc)) + decision = self._get_decision(experiment_name, ctx) + + if decision is None: return None event_context_fields = self._decider_context.to_event_dict() @@ -598,16 +564,11 @@ def get_all_variants_without_expose(self) -> List[Dict[str, Union[str, int]]]: :return: list of experiment dicts with non-:code:`None` variants. """ - if self._internal is None: - logger.error("rs_decider is None--did not initialize.") - return [] - ctx = self._decider_context.to_dict() - try: - all_decisions = self._internal.choose_all(ctx) - except DeciderException as exc: - logger.info(str(exc)) + all_decisions = self._get_all_decisions(ctx) + + if all_decisions is None: return [] parsed_choices = [] @@ -673,19 +634,12 @@ def get_all_variants_for_identifier_without_expose( ) return [] - if self._internal is None: - logger.error("rs_decider is None--did not initialize.") - return [] - ctx = self._decider_context.to_dict() ctx[identifier_type] = identifier - try: - all_decisions = self._internal.choose_all( - context=ctx, bucketing_field_filter=identifier_type - ) - except DeciderException as exc: - logger.info(str(exc)) + all_decisions = self._get_all_decisions(ctx=ctx, bucketing_field_filter=identifier_type) + + if all_decisions is None: return [] parsed_choices = [] @@ -816,6 +770,39 @@ def get_all_dynamic_configs(self) -> List[Dict[str, Any]]: return parsed_configs + def _get_decision( + self, + experiment_name: str, + ctx: Dict[str, Any], + ) -> Optional[Decision]: + if self._internal is None: + logger.error("RustDecider is None--did not initialize.") + return None + + try: + return self._internal.choose(experiment_name, ctx) + except FeatureNotFoundException as exc: + warnings.warn(str(exc)) + return None + except DeciderException as exc: + logger.info(str(exc)) + return None + + def _get_all_decisions( + self, + ctx: Dict[str, Any], + bucketing_field_filter: Optional[str] = None + ) -> Optional[Dict[str, Decision]]: + if self._internal is None: + logger.error("RustDecider is None--did not initialize.") + return None + + try: + return self._internal.choose_all(ctx, bucketing_field_filter) + except DeciderException as exc: + logger.info(str(exc)) + return None + def _get_dynamic_config_value( self, feature_name: str, From 35f15099aac99af92701e83888f95635226ab429 Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 15 Dec 2022 14:17:02 -0800 Subject: [PATCH 2/2] lint --- reddit_decider/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/reddit_decider/__init__.py b/reddit_decider/__init__.py index 3f96175..ce26694 100644 --- a/reddit_decider/__init__.py +++ b/reddit_decider/__init__.py @@ -789,9 +789,7 @@ def _get_decision( return None def _get_all_decisions( - self, - ctx: Dict[str, Any], - bucketing_field_filter: Optional[str] = None + self, ctx: Dict[str, Any], bucketing_field_filter: Optional[str] = None ) -> Optional[Dict[str, Decision]]: if self._internal is None: logger.error("RustDecider is None--did not initialize.")