Skip to content

Commit 705e832

Browse files
committed
refactor common code
1 parent fb56fe5 commit 705e832

File tree

1 file changed

+49
-62
lines changed

1 file changed

+49
-62
lines changed

reddit_decider/__init__.py

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -318,19 +318,10 @@ def get_variant(
318318
319319
:return: Variant name if a variant is assigned, :code:`None` otherwise.
320320
"""
321-
if self._internal is None:
322-
logger.error("RustDecider is None--did not initialize.")
323-
return None
324-
325321
ctx = self._decider_context.to_dict()
322+
decision = self._get_decision(experiment_name, ctx)
326323

327-
try:
328-
decision = self._internal.choose(experiment_name, ctx)
329-
except FeatureNotFoundException as exc:
330-
warnings.warn(str(exc))
331-
return None
332-
except DeciderException as exc:
333-
logger.info(str(exc))
324+
if decision is None:
334325
return None
335326

336327
event_context_fields = self._decider_context.to_event_dict()
@@ -356,19 +347,10 @@ def get_variant_without_expose(self, experiment_name: str) -> Optional[str]:
356347
357348
:return: Variant name if a variant is assigned, None otherwise.
358349
"""
359-
if self._internal is None:
360-
logger.error("RustDecider is None--did not initialize.")
361-
return None
362-
363350
ctx = self._decider_context.to_dict()
351+
decision = self._get_decision(experiment_name, ctx)
364352

365-
try:
366-
decision = self._internal.choose(experiment_name, ctx)
367-
except FeatureNotFoundException as exc:
368-
warnings.warn(str(exc))
369-
return None
370-
except DeciderException as exc:
371-
logger.info(str(exc))
353+
if decision is None:
372354
return None
373355

374356
event_context_fields = self._decider_context.to_event_dict()
@@ -476,20 +458,12 @@ def get_variant_for_identifier(
476458
)
477459
return None
478460

479-
if self._internal is None:
480-
logger.error("RustDecider is None--did not initialize.")
481-
return None
482-
483461
ctx = self._decider_context.to_dict()
484462
ctx[identifier_type] = identifier
485463

486-
try:
487-
decision = self._internal.choose(experiment_name, ctx)
488-
except FeatureNotFoundException as exc:
489-
warnings.warn(str(exc))
490-
return None
491-
except DeciderException as exc:
492-
logger.info(str(exc))
464+
decision = self._get_decision(experiment_name, ctx)
465+
466+
if decision is None:
493467
return None
494468

495469
event_context_fields = self._decider_context.to_event_dict()
@@ -544,20 +518,12 @@ def get_variant_for_identifier_without_expose(
544518
)
545519
return None
546520

547-
if self._internal is None:
548-
logger.error("RustDecider is None--did not initialize.")
549-
return None
550-
551521
ctx = self._decider_context.to_dict()
552522
ctx[identifier_type] = identifier
553523

554-
try:
555-
decision = self._internal.choose(experiment_name, ctx)
556-
except FeatureNotFoundException as exc:
557-
warnings.warn(str(exc))
558-
return None
559-
except DeciderException as exc:
560-
logger.info(str(exc))
524+
decision = self._get_decision(experiment_name, ctx)
525+
526+
if decision is None:
561527
return None
562528

563529
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]]]:
598564
599565
:return: list of experiment dicts with non-:code:`None` variants.
600566
"""
601-
if self._internal is None:
602-
logger.error("rs_decider is None--did not initialize.")
603-
return []
604-
605567
ctx = self._decider_context.to_dict()
606568

607-
try:
608-
all_decisions = self._internal.choose_all(ctx)
609-
except DeciderException as exc:
610-
logger.info(str(exc))
569+
all_decisions = self._get_all_decisions(ctx)
570+
571+
if all_decisions is None:
611572
return []
612573

613574
parsed_choices = []
@@ -673,19 +634,12 @@ def get_all_variants_for_identifier_without_expose(
673634
)
674635
return []
675636

676-
if self._internal is None:
677-
logger.error("rs_decider is None--did not initialize.")
678-
return []
679-
680637
ctx = self._decider_context.to_dict()
681638
ctx[identifier_type] = identifier
682639

683-
try:
684-
all_decisions = self._internal.choose_all(
685-
context=ctx, bucketing_field_filter=identifier_type
686-
)
687-
except DeciderException as exc:
688-
logger.info(str(exc))
640+
all_decisions = self._get_all_decisions(ctx=ctx, bucketing_field_filter=identifier_type)
641+
642+
if all_decisions is None:
689643
return []
690644

691645
parsed_choices = []
@@ -816,6 +770,39 @@ def get_all_dynamic_configs(self) -> List[Dict[str, Any]]:
816770

817771
return parsed_configs
818772

773+
def _get_decision(
774+
self,
775+
experiment_name: str,
776+
ctx: Dict[str, Any],
777+
) -> Optional[Decision]:
778+
if self._internal is None:
779+
logger.error("RustDecider is None--did not initialize.")
780+
return None
781+
782+
try:
783+
return self._internal.choose(experiment_name, ctx)
784+
except FeatureNotFoundException as exc:
785+
warnings.warn(str(exc))
786+
return None
787+
except DeciderException as exc:
788+
logger.info(str(exc))
789+
return None
790+
791+
def _get_all_decisions(
792+
self,
793+
ctx: Dict[str, Any],
794+
bucketing_field_filter: Optional[str] = None
795+
) -> Optional[Dict[str, Decision]]:
796+
if self._internal is None:
797+
logger.error("RustDecider is None--did not initialize.")
798+
return None
799+
800+
try:
801+
return self._internal.choose_all(ctx, bucketing_field_filter)
802+
except DeciderException as exc:
803+
logger.info(str(exc))
804+
return None
805+
819806
def _get_dynamic_config_value(
820807
self,
821808
feature_name: str,

0 commit comments

Comments
 (0)