Exemple #1
0
 def __init__(self):
     super(TopLevelVisitor, self).__init__()
     self.attrnames = oset()
     self.removed = oset()  # keep track of which variables were deleted
Exemple #2
0
def get_all_fields(Model, for_export=False):

    if Model is PageCompletion:
        return [
            'session_pk',
            'participant_pk',
            'page_index',
            'app_name',
            'page_name',
            'time_stamp',
            'seconds_on_page',
            'player_pk',
            'subsession_pk',
        ]

    if Model is Session:
        return [
            'code',
            'label',
            'experimenter_name',
            'real_world_currency_per_point',
            'time_scheduled',
            'time_started',
            'mturk_HITId',
            'mturk_HITGroupId',
            'participation_fee',
            'comment',
            'special_category',
        ]

    if Model is Participant:
        if for_export:
            return [
                '_id_in_session_display',
                'code',
                'label',
                '_current_page',
                '_current_app_name',
                '_round_number',
                '_current_page_name',
                'status',
                'last_request_succeeded',
                'ip_address',
                'time_started',
                'exclude_from_data_analysis',
                'name',
                'session',
                'visited',
                'mturk_worker_id',
                'mturk_assignment_id',
            ]
        else:
            return [
                '_id_in_session_display',
                'code',
                'label',
                '_current_page',
                '_current_app_name',
                '_round_number',
                '_current_page_name',
                'status',
                'last_request_succeeded',
                '_last_page_timestamp',
            ]

    first_fields = {
        'Player':
            [
                'id_in_group',
                'role',
            ],
        'Group':
            [
                'id',
            ],
        'Subsession':
            [],
    }[Model.__name__]
    first_fields = oset(first_fields)

    last_fields = {
        'Player': [],
        'Group': [],
        'Subsession': [],
    }[Model.__name__]
    last_fields = oset(last_fields)

    fields_for_export_but_not_view = {
        'Player': {'id', 'label', 'subsession', 'session'},
        'Group': {'id'},
        'Subsession': {'id', 'round_number'},
    }[Model.__name__]

    fields_for_view_but_not_export = {
        'Player': set(),
        'Group': {'subsession', 'session'},
        'Subsession': {'session'},
    }[Model.__name__]

    fields_to_exclude_from_export_and_view = {
        'Player': {
            '_index_in_game_pages',
            'participant',
            'group',
            'subsession',
            'session',
            'round_number',
        },
        'Group': {
            'subsession',
            'id_in_subsession',
            'session',
            '_is_missing_players',
        },
        'Subsession': {
            'code',
            'label',
            'session',
            'session_access_code',
            '_index_in_subsessions',
        },
    }[Model.__name__]

    if for_export:
        fields_to_exclude = fields_to_exclude_from_export_and_view.union(
            fields_for_view_but_not_export
        )
    else:
        fields_to_exclude = fields_to_exclude_from_export_and_view.union(
            fields_for_export_but_not_view
        )

    all_fields_in_model = oset([field.name for field in Model._meta.fields])

    middle_fields = (
        all_fields_in_model - first_fields - last_fields - fields_to_exclude
    )

    return list(first_fields | middle_fields | last_fields)
Exemple #3
0
    def visit_If(self, node):
        """
        Notes:
            elif clauses don't have a special representation in the AST, but
            rather appear as extra If nodes within the orelse section of the
            previous one.
        """
        if isinstance(node.test, ast.Compare):  # pragma: nobranch
            try:
                if all([
                        isinstance(node.test.ops[0], ast.Eq),
                        node.test.left.id == '__name__',
                        node.test.comparators[0].s == '__main__',
                ]):
                    # Ignore main block
                    return
            except Exception:  # nocover
                pass

        # TODO: handled deleted attributes?
        # Find definitions from conditionals that always accept or
        # that are defined in all possible non-rejecting branches (note this
        # requires an else statment). A rejecting branch is one that is
        # unconditionally false or unconditionally raises an exception
        if_node, elif_nodes, else_body = unpack_if_nodes(node)
        test_nodes = [if_node] + elif_nodes

        has_unconditional = False
        required = []

        for item in test_nodes:
            truth = static_truthiness(item.test)
            # if any(isinstance(n, ast.Raise) for n in item.body):
            #     # Ignore branches that simply raise an error
            #     continue
            if truth is _UNHANDLED:
                names = get_conditional_attrnames(item.body)
                required.append(names)
            elif truth is True:
                # Branch is unconditionally true, no need to check others
                names = get_conditional_attrnames(item.body)
                required.append(names)
                has_unconditional = True
                break
            elif truth is False:
                # Ignore branches that are unconditionally false
                continue
            else:
                raise AssertionError('cannot happen')

        if not has_unconditional and else_body:
            # If we havent found an unconditional branch we need an else
            if not any(isinstance(n, ast.Raise) for n in else_body):
                # Ignore else branches that simply raise an error
                names = get_conditional_attrnames(else_body)
                required.append(names)
            has_unconditional = True

        if has_unconditional:
            # We can only gaurentee that something will exist if there is at
            # least one path that must be taken
            if len(required) == 0:
                common = oset()
            elif len(required) == 1:
                common = required[0]
            else:
                common = oset.intersection(*required)
                # common = set.intersection(*map(set, required))
            self._register(sorted(common))
Exemple #4
0
def get_all_fields(Model, for_export=False):

    if Model is PageCompletion:
        return [
            'session_pk',
            'participant_pk',
            'page_index',
            'app_name',
            'page_name',
            'time_stamp',
            'seconds_on_page',
            'player_pk',
            'subsession_pk',
        ]

    if Model is Session:
        return [
            'code',
            'label',
            'experimenter_name',
            'real_world_currency_per_point',
            'time_scheduled',
            'time_started',
            'mturk_HITId',
            'mturk_HITGroupId',
            'participation_fee',
            'comment',
            'special_category',
        ]

    if Model is Participant:
        if for_export:
            return [
                '_id_in_session_display',
                'code',
                'label',
                '_current_page',
                '_current_app_name',
                '_round_number',
                '_current_page_name',
                'status',
                'last_request_succeeded',
                'ip_address',
                'time_started',
                'exclude_from_data_analysis',
                'name',
                'session',
                'visited',
                'mturk_worker_id',
                'mturk_assignment_id',
            ]
        else:
            return [
                '_id_in_session_display',
                'code',
                'label',
                '_current_page',
                '_current_app_name',
                '_round_number',
                '_current_page_name',
                'status',
                'last_request_succeeded',
                '_last_page_timestamp',
            ]

    first_fields = {
        'Player': [
            'id_in_group',
            'role',
        ],
        'Group': [
            'id',
        ],
        'Subsession': [],
    }[Model.__name__]
    first_fields = oset(first_fields)

    last_fields = {
        'Player': [],
        'Group': [],
        'Subsession': [],
    }[Model.__name__]
    last_fields = oset(last_fields)

    fields_for_export_but_not_view = {
        'Player': {'id', 'label', 'subsession', 'session'},
        'Group': {'id'},
        'Subsession': {'id', 'round_number'},
    }[Model.__name__]

    fields_for_view_but_not_export = {
        'Player': set(),
        'Group': {'subsession', 'session'},
        'Subsession': {'session'},
    }[Model.__name__]

    fields_to_exclude_from_export_and_view = {
        'Player': {
            '_index_in_game_pages',
            'participant',
            'group',
            'subsession',
            'session',
            'round_number',
        },
        'Group': {
            'subsession',
            'id_in_subsession',
            'session',
            '_is_missing_players',
        },
        'Subsession': {
            'code',
            'label',
            'session',
            'session_access_code',
            '_index_in_subsessions',
        },
    }[Model.__name__]

    if for_export:
        fields_to_exclude = fields_to_exclude_from_export_and_view.union(
            fields_for_view_but_not_export)
    else:
        fields_to_exclude = fields_to_exclude_from_export_and_view.union(
            fields_for_export_but_not_view)

    all_fields_in_model = oset([field.name for field in Model._meta.fields])

    middle_fields = (all_fields_in_model - first_fields - last_fields -
                     fields_to_exclude)

    return list(first_fields | middle_fields | last_fields)