当前位置: 首页>>代码示例>>Python>>正文


Python ExperimentCounter.participant_goal_frequencies方法代码示例

本文整理汇总了Python中experiments.experiment_counters.ExperimentCounter.participant_goal_frequencies方法的典型用法代码示例。如果您正苦于以下问题:Python ExperimentCounter.participant_goal_frequencies方法的具体用法?Python ExperimentCounter.participant_goal_frequencies怎么用?Python ExperimentCounter.participant_goal_frequencies使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在experiments.experiment_counters.ExperimentCounter的用法示例。


在下文中一共展示了ExperimentCounter.participant_goal_frequencies方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: IncorporateTestCase

# 需要导入模块: from experiments.experiment_counters import ExperimentCounter [as 别名]
# 或者: from experiments.experiment_counters.ExperimentCounter import participant_goal_frequencies [as 别名]
class IncorporateTestCase(TestCase):
    def setUp(self):
        self.experiment = Experiment.objects.create(name=EXPERIMENT_NAME, state=ENABLED_STATE)
        self.experiment_counter = ExperimentCounter()

        User = get_user_model()
        self.user = User.objects.create(username='incorporate_user')
        self.user.is_confirmed_human = True

        request_factory = RequestFactory()
        self.request = request_factory.get('/')
        self.request.session = DatabaseSession()
        participant(self.request).confirm_human()

    def tearDown(self):
        self.experiment_counter.delete(self.experiment)

    def _login(self):
        self.request.user = self.user
        transfer_enrollments_to_user(None, self.request, self.user)

    def test_visit_incorporate(self):
        alternative = participant(self.request).enroll(self.experiment.name, ['alternative'])

        ExperimentsRetentionMiddleware().process_response(self.request, HttpResponse())

        self.assertEqual(
            dict(self.experiment_counter.participant_goal_frequencies(self.experiment,
                                                                      alternative,
                                                                      participant(self.request)._participant_identifier()))[conf.VISIT_NOT_PRESENT_COUNT_GOAL],
            1
        )

        self.assertFalse(Enrollment.objects.all().exists())
        self._login()

        self.assertTrue(Enrollment.objects.all().exists())
        self.assertIsNotNone(Enrollment.objects.all()[0].last_seen)
        self.assertEqual(
            dict(self.experiment_counter.participant_goal_frequencies(self.experiment,
                                                                      alternative,
                                                                      participant(self.request)._participant_identifier()))[conf.VISIT_NOT_PRESENT_COUNT_GOAL],
            1
        )
        self.assertEqual(self.experiment_counter.goal_count(self.experiment, alternative, conf.VISIT_NOT_PRESENT_COUNT_GOAL), 1)
        self.assertEqual(self.experiment_counter.participant_count(self.experiment, alternative), 1)
开发者ID:DjangoBD,项目名称:django-experiments,代码行数:48,代码来源:test_webuser_incorporate.py

示例2: WebUser

# 需要导入模块: from experiments.experiment_counters import ExperimentCounter [as 别名]
# 或者: from experiments.experiment_counters.ExperimentCounter import participant_goal_frequencies [as 别名]
class WebUser(object):
    """Represents a user (either authenticated or session based) which can take part in experiments"""

    def __init__(self):
        self.experiment_counter = ExperimentCounter()

    def enroll(self, experiment_name, alternatives, force_alternative=None):
        """
        Enroll this user in the experiment if they are not already part of it. Returns the selected alternative

        force_alternative: Optionally force a user in an alternative at enrollment time
        """
        chosen_alternative = conf.CONTROL_GROUP

        experiment = experiment_manager.get(experiment_name, None)
        if experiment and experiment.is_displaying_alternatives():
            if isinstance(alternatives, collections.Mapping):
                if conf.CONTROL_GROUP not in alternatives:
                    experiment.ensure_alternative_exists(conf.CONTROL_GROUP, 1)
                for alternative, weight in alternatives.items():
                    experiment.ensure_alternative_exists(alternative, weight)
            else:
                alternatives_including_control = alternatives + [conf.CONTROL_GROUP]
                for alternative in alternatives_including_control:
                    experiment.ensure_alternative_exists(alternative)

            assigned_alternative = self._get_enrollment(experiment)
            if assigned_alternative:
                chosen_alternative = assigned_alternative
            elif experiment.is_accepting_new_users():
                if force_alternative:
                    chosen_alternative = force_alternative
                else:
                    chosen_alternative = experiment.random_alternative()
                self._set_enrollment(experiment, chosen_alternative)

        return chosen_alternative

    def get_alternative(self, experiment_name):
        """Get the alternative this user is enrolled in. If not enrolled in the experiment returns 'control'"""
        experiment = experiment_manager.get(experiment_name, None)
        if experiment and experiment.is_displaying_alternatives():
            alternative = self._get_enrollment(experiment)
            if alternative is not None:
                return alternative
        return "control"

    def set_alternative(self, experiment_name, alternative):
        """Explicitly set the alternative the user is enrolled in for the specified experiment.

        This allows you to change a user between alternatives. The user and goal counts for the new
        alternative will be increment, but those for the old one will not be decremented. The user will
        be enrolled in the experiment even if the experiment would not normally accept this user."""
        experiment = experiment_manager.get(experiment_name, None)
        if experiment:
            self._set_enrollment(experiment, alternative)

    def goal(self, goal_name, count=1):
        """Record that this user has performed a particular goal

        This will update the goal stats for all experiments the user is enrolled in."""
        for enrollment in self._get_all_enrollments():
            if enrollment.experiment.is_displaying_alternatives():
                self._experiment_goal(enrollment.experiment, enrollment.alternative, goal_name, count)

    def confirm_human(self):
        """Mark that this is a real human being (not a bot) and thus results should be counted"""
        pass

    def incorporate(self, other_user):
        """Incorporate all enrollments and goals performed by the other user

        If this user is not enrolled in a given experiment, the results for the
        other user are incorporated. For experiments this user is already
        enrolled in the results of the other user are discarded.

        This takes a relatively large amount of time for each experiment the other
        user is enrolled in."""
        for enrollment in other_user._get_all_enrollments():
            if not self._get_enrollment(enrollment.experiment):
                self._set_enrollment(
                    enrollment.experiment, enrollment.alternative, enrollment.enrollment_date, enrollment.last_seen
                )
                goals = self.experiment_counter.participant_goal_frequencies(
                    enrollment.experiment, enrollment.alternative, other_user._participant_identifier()
                )
                for goal_name, count in goals:
                    self.experiment_counter.increment_goal_count(
                        enrollment.experiment, enrollment.alternative, goal_name, self._participant_identifier(), count
                    )
            other_user._cancel_enrollment(enrollment.experiment)

    def visit(self):
        """Record that the user has visited the site for the purposes of retention tracking"""
        for enrollment in self._get_all_enrollments():
            if enrollment.experiment.is_displaying_alternatives():
                # We have two different goals, VISIT_NOT_PRESENT_COUNT_GOAL and VISIT_PRESENT_COUNT_GOAL.
                # VISIT_PRESENT_COUNT_GOAL will avoid firing on the first time we set last_seen as it is assumed that the user is
                # on the page and therefore it would automatically trigger and be valueless.
                # This should be used for experiments when we enroll the user as part of the pageview,
#.........这里部分代码省略.........
开发者ID:microlin,项目名称:django-experiments,代码行数:103,代码来源:utils.py


注:本文中的experiments.experiment_counters.ExperimentCounter.participant_goal_frequencies方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。