Skip to content

Commit

Permalink
Merge pull request #89 from reddit/refactor_cleanup
Browse files Browse the repository at this point in the history
Refactor common code
  • Loading branch information
mrlevitas authored Dec 15, 2022
2 parents fb56fe5 + 35f1509 commit 02a4572
Showing 1 changed file with 47 additions and 62 deletions.
109 changes: 47 additions & 62 deletions reddit_decider/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -816,6 +770,37 @@ 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,
Expand Down

0 comments on commit 02a4572

Please sign in to comment.