<commit_before>"""
Defines records for assessment parts
"""
import json

from bson import ObjectId
from collections import OrderedDict
from random import shuffle

try:
    # python 2
    from urllib import quote, unquote
except ImportError:
    # python 3
    from urllib.parse import quote, unquote

from collections import OrderedDict

from dlkit.json_.assessment.assessment_utilities import get_assessment_part_lookup_session
from dlkit.json_.assessment_authoring.objects import AssessmentPartList
from dlkit.json_.assessment_authoring.sessions import AssessmentPartLookupSession
from dlkit.json_.id.objects import IdList
from dlkit.json_.osid import record_templates as osid_records
from dlkit.json_.osid.metadata import Metadata
from dlkit.json_.utilities import JSONClientValidated

from dlkit.abstract_osid.assessment_authoring import record_templates as abc_assessment_authoring_records
from dlkit.abstract_osid.osid.errors import IllegalState, InvalidArgument, NoAccess, NotFound, OperationFailed
from dlkit.primordium.id.primitives import Id

from ...osid.base_records import ObjectInitRecord

MAGIC_PART_AUTHORITY = 'magic-part-authority'
ENDLESS = 10000  # For seemingly endless waypoints


def get_part_from_magic_part_lookup_session(section, part_id, *args, **kwargs):
    mpls = MagicAssessmentPartLookupSession(section, *args, **kwargs)
    mpls.use_unsequestered_assessment_part_view()
    mpls.use_federated_bank_view()
    return mpls.get_assessment_part(part_id)


class ScaffoldDownAssessmentPartRecord(ObjectInitRecord):
    """magic assessment part record for scaffold down adaptive questions"""
    _implemented_record_type_identifiers = [
        'scaffold-down'
    ]

    def __init__(self, *args, **kwargs):
        super(ScaffoldDownAssessmentPartRecord, self).__init__(*args, **kwargs)
        self._magic_identifier = None
        self._assessment_section = None
        self._magic_parent_id = None
        self._level = 0
        self._part_map = dict()
        self._child_parts = None
        if self.my_osid_object._my_map['maxWaypointItems'] is None:
            self._max_waypoints = ENDLESS
        else:
            self._max_waypoints = self.my_osid_object._my_map['maxWaypointItems']

    def get_id(self):
        """override get_id to generate our "magic" id that encodes scaffolding information"""
        waypoint_index = 0
        if 'waypointIndex' in self.my_osid_object._my_map:
            waypoint_index = self.my_osid_object._my_map['waypointIndex']
        # NOTE that the order of the dict **must** match the order in generate_children()
        #   when creating the child_part_id
        #   1) level
        #   2) objective_ids
        #   3) parent_id
        #   4) waypoint_index
        magic_identifier = OrderedDict({
            'level': self._level,
            'objective_ids': self.my_osid_object._my_map['learningObjectiveIds'],
        })
        if self._magic_parent_id is not None:
            magic_identifier['parent_id'] = str(self._magic_parent_id)
        magic_identifier['waypoint_index'] = waypoint_index


        identifier = quote('{0}?{1}'.format(str(self.my_osid_object._my_map['_id']),
                                            json.dumps(magic_identifier)))
        return Id(namespace='assessment_authoring.AssessmentPart',
                  identifier=identifier,
                  authority=MAGIC_PART_AUTHORITY)

    ident = property(fget=get_id)
    id_ = property(fget=get_id)

    def initialize(self, magic_identifier, assessment_section):
        """This method is to be called by a magic AssessmentPart lookup session.

        magic_identifier_part includes:
            parent_id = id string of the parent part that created this part
            level = how many levels deep
            objective_id = the Objective Id to for which to select an item
            waypoint_index = the index of this item in its parent part

        """
        arg_map = json.loads(unquote(magic_identifier).split('?')[-1],
                             object_pairs_hook=OrderedDict)
        self._magic_identifier = magic_identifier
        self._assessment_section = assessment_section
        if 'level' in arg_map:
            self._level = arg_map['level']<commit_msg>fix pep8<commit_after>"""
Defines records for assessment parts
"""
import json

from bson import ObjectId
from collections import OrderedDict
from random import shuffle

try:
    # python 2
    from urllib import quote, unquote
except ImportError:
    # python 3
    from urllib.parse import quote, unquote

from collections import OrderedDict

from dlkit.json_.assessment.assessment_utilities import get_assessment_part_lookup_session
from dlkit.json_.assessment_authoring.objects import AssessmentPartList
from dlkit.json_.assessment_authoring.sessions import AssessmentPartLookupSession
from dlkit.json_.id.objects import IdList
from dlkit.json_.osid import record_templates as osid_records
from dlkit.json_.osid.metadata import Metadata
from dlkit.json_.utilities import JSONClientValidated

from dlkit.abstract_osid.assessment_authoring import record_templates as abc_assessment_authoring_records
from dlkit.abstract_osid.osid.errors import IllegalState, InvalidArgument, NoAccess, NotFound, OperationFailed
from dlkit.primordium.id.primitives import Id

from ...osid.base_records import ObjectInitRecord

MAGIC_PART_AUTHORITY = 'magic-part-authority'
ENDLESS = 10000  # For seemingly endless waypoints


def get_part_from_magic_part_lookup_session(section, part_id, *args, **kwargs):
    mpls = MagicAssessmentPartLookupSession(section, *args, **kwargs)
    mpls.use_unsequestered_assessment_part_view()
    mpls.use_federated_bank_view()
    return mpls.get_assessment_part(part_id)


class ScaffoldDownAssessmentPartRecord(ObjectInitRecord):
    """magic assessment part record for scaffold down adaptive questions"""
    _implemented_record_type_identifiers = [
        'scaffold-down'
    ]

    def __init__(self, *args, **kwargs):
        super(ScaffoldDownAssessmentPartRecord, self).__init__(*args, **kwargs)
        self._magic_identifier = None
        self._assessment_section = None
        self._magic_parent_id = None
        self._level = 0
        self._part_map = dict()
        self._child_parts = None
        if self.my_osid_object._my_map['maxWaypointItems'] is None:
            self._max_waypoints = ENDLESS
        else:
            self._max_waypoints = self.my_osid_object._my_map['maxWaypointItems']

    def get_id(self):
        """override get_id to generate our "magic" id that encodes scaffolding information"""
        waypoint_index = 0
        if 'waypointIndex' in self.my_osid_object._my_map:
            waypoint_index = self.my_osid_object._my_map['waypointIndex']
        # NOTE that the order of the dict **must** match the order in generate_children()
        #   when creating the child_part_id
        #   1) level
        #   2) objective_ids
        #   3) parent_id
        #   4) waypoint_index
        magic_identifier = OrderedDict({
            'level': self._level,
            'objective_ids': self.my_osid_object._my_map['learningObjectiveIds'],
        })
        if self._magic_parent_id is not None:
            magic_identifier['parent_id'] = str(self._magic_parent_id)
        magic_identifier['waypoint_index'] = waypoint_index

        identifier = quote('{0}?{1}'.format(str(self.my_osid_object._my_map['_id']),
                                            json.dumps(magic_identifier)))
        return Id(namespace='assessment_authoring.AssessmentPart',
                  identifier=identifier,
                  authority=MAGIC_PART_AUTHORITY)

    ident = property(fget=get_id)
    id_ = property(fget=get_id)

    def initialize(self, magic_identifier, assessment_section):
        """This method is to be called by a magic AssessmentPart lookup session.

        magic_identifier_part includes:
            parent_id = id string of the parent part that created this part
            level = how many levels deep
            objective_id = the Objective Id to for which to select an item
            waypoint_index = the index of this item in its parent part

        """
        arg_map = json.loads(unquote(magic_identifier).split('?')[-1],
                             object_pairs_hook=OrderedDict)
        self._magic_identifier = magic_identifier
        self._assessment_section = assessment_section
        if 'level' in arg_map:
            self._level = arg_map['level']
{'fraction_non_alphanumeric': 0.07381288199341796, 'fraction_numerical': 0.0038787023977433005, 'mean_word_length': 3.2566283141570787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870579', 'n_tokens_mistral': 2489, 'n_tokens_neox': 2397, 'n_words': 590}