Instruction:
Implement a function in Python 3 to
build the XML by appending to the given XML builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(
            OID=self.oid, Name=self.name, Repeating=bool_to_yes_no(self.repeating)
        )

        if self.order_number is not None:
            params["mdsol:OrderNumber"] = str(self.order_number)

        if self.active is not None:
            params["mdsol:Active"] = bool_to_yes_no(self.active)

        params["mdsol:Template"] = bool_to_yes_no(self.template)
        params["mdsol:SignatureRequired"] = bool_to_yes_no(self.signature_required)
        params["mdsol:LogDirection"] = self.log_direction
        params["mdsol:DoubleDataEntry"] = self.double_data_entry
        params["mdsol:ConfirmationStyle"] = self.confirmation_style

        if self.link_study_event_oid:
            params["mdsol:LinkStudyEventOID"] = self.link_study_event_oid

        if self.link_form_oid:
            params["mdsol:LinkFormOID"] = self.link_form_oid

        builder.start("FormDef", params)
        for itemgroup_ref in self.itemgroup_refs:
            itemgroup_ref.build(builder)

        for helptext in self.helptexts:
            helptext.build(builder)

        for view_restriction in self.view_restrictions:
            view_restriction.build(builder)

        for entry_restriction in self.entry_restrictions:
            entry_restriction.build(builder)

        for alias in self.aliases:
            alias.build(builder)

        builder.end("FormDef")


Instruction:
Here you have a function in Python 3, explain what it does
def build(self, builder):

        params = dict(
            OID=self.oid,
            Name=self.name,
            Repeating=bool_to_yes_no(self.repeating),
            IsReferenceData=bool_to_yes_no(self.is_reference_data),
        )

        if self.sas_dataset_name is not None:
            params["SASDatasetName"] = self.sas_dataset_name

        if self.domain is not None:
            params["Domain"] = self.domain

        if self.origin is not None:
            params["Origin"] = self.origin

        if self.role is not None:
            params["Role"] = self.role

        if self.purpose is not None:
            params["Purpose"] = self.purpose

        if self.comment is not None:
            params["Comment"] = self.comment

        builder.start("ItemGroupDef", params)

        for itemref in self.item_refs:
            itemref.build(builder)

        # Extensions always listed AFTER core elements
        for labelref in self.label_refs:
            labelref.build(builder)

        for alias in self.aliases:
            alias.build(builder)

        builder.end("ItemGroupDef")

Response: Builds the XML by appending to the given XML builder


Instruction:
How would you explain what the following Python 3 function does
def build(self, builder):

        params = dict(ItemOID=self.oid, Mandatory=bool_to_yes_no(self.mandatory))

        if self.order_number is not None:
            params["OrderNumber"] = str(self.order_number)

        if self.key_sequence is not None:
            params["KeySequence"] = str(self.key_sequence)

        if self.imputation_method_oid is not None:
            params["ImputationMethodOID"] = self.imputation_method_oid

        if self.role is not None:
            params["Role"] = self.role

        if self.role_codelist_oid is not None:
            params["RoleCodeListOID"] = self.role_codelist_oid

        builder.start("ItemRef", params)

        for attribute in self.attributes:
            attribute.build(builder)
        builder.end("ItemRef")

Response: Builds the XML by appending to builder


Instruction:
Can you generate the documentation for the following Python 3 function
def build(self, builder):

        params = dict(
            OID=self.oid,
            Name=self.name,
            DataType=self.datatype.value,
            Length=str(self.length),
        )

        if self.date_time_format is not None:
            params["mdsol:DateTimeFormat"] = self.date_time_format

        params["mdsol:Active"] = bool_to_yes_no(self.active)

        if self.significant_digits is not None:
            params["SignificantDigits"] = str(self.significant_digits)

        if self.sas_field_name is not None:
            params["SASFieldName"] = self.sas_field_name

        if self.sds_var_name is not None:
            params["SDSVarName"] = self.sds_var_name

        if self.origin is not None:
            params["Origin"] = self.origin

        if self.comment is not None:
            params["Comment"] = self.comment

        if self.control_type is not None:
            params["mdsol:ControlType"] = self.control_type.value

        if self.acceptable_file_extensions is not None:
            params["mdsol:AcceptableFileExtensions"] = self.acceptable_file_extensions

        if self.default_value is not None:
            params["mdsol:DefaultValue"] = str(self.default_value)

        params["mdsol:SourceDocument"] = bool_to_yes_no(self.source_document_verify)
        params["mdsol:IndentLevel"] = str(self.indent_level)

        if self.sas_format is not None:
            params["mdsol:SASFormat"] = self.sas_format

        if self.sas_label is not None:
            params["mdsol:SASLabel"] = self.sas_label

        params["mdsol:QueryFutureDate"] = bool_to_yes_no(self.query_future_date)
        params["mdsol:Visible"] = bool_to_yes_no(self.visible)
        params["mdsol:TranslationRequired"] = bool_to_yes_no(self.translation_required)
        params["mdsol:QueryNonConformance"] = bool_to_yes_no(self.query_non_conformance)
        params["mdsol:OtherVisits"] = bool_to_yes_no(self.other_visits)
        params["mdsol:CanSetItemGroupDate"] = bool_to_yes_no(
            self.can_set_item_group_date
        )
        params["mdsol:CanSetFormDate"] = bool_to_yes_no(self.can_set_form_date)
        params["mdsol:CanSetStudyEventDate"] = bool_to_yes_no(
            self.can_set_study_event_date
        )
        params["mdsol:CanSetSubjectDate"] = bool_to_yes_no(self.can_set_subject_date)
        params["mdsol:VisualVerify"] = bool_to_yes_no(self.visual_verify)
        params["mdsol:DoesNotBreakSignature"] = bool_to_yes_no(
            self.does_not_break_signature
        )

        if self.field_number is not None:
            params["mdsol:FieldNumber"] = self.field_number

        if self.variable_oid is not None:
            params["mdsol:VariableOID"] = self.variable_oid

        builder.start("ItemDef", params)

        if self.question is not None:
            self.question.build(builder)

        if self.codelistref is not None:
            self.codelistref.build(builder)

        for mur in self.measurement_unit_refs:
            mur.build(builder)

        for range_check in self.range_checks:
            range_check.build(builder)

        if self.header_text is not None:
            self.header_text.build(builder)

        for view_restriction in self.view_restrictions:
            view_restriction.build(builder)

        for entry_restriction in self.entry_restrictions:
            entry_restriction.build(builder)

        for help_text in self.help_texts:
            help_text.build(builder)

        for review_group in self.review_groups:
            review_group.build(builder)

        for alias in self.aliases:
            alias.build(builder)

        builder.end("ItemDef")

Response: Builds the XML by appending to the given XML builder.


Instruction:
Write a Python 3 function for
building the XML representation of the object into the given builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder

        .. note:: Questions can contain translations
        """
        builder.start("Question", {})
        for translation in self.translations:
            translation.build(builder)
        builder.end("Question")


Instruction:
Write a Python 3 function that can
set the comparator (with validation of input)

Response:
def comparator(self, value):
        """sets the comparator (with validation of input)"""
        if not isinstance(value, RangeCheckComparatorType):
            raise AttributeError("%s comparator is invalid in RangeCheck." % (value,))
        self._comparator = value


Instruction:
How would you code a function in Python 3 to
set the Soft or Hard range setting

Response:
def soft_hard(self, value):
        """sets the Soft or Hard range setting (with validation of input)"""
        if not isinstance(value, RangeCheckType):
            raise AttributeError("%s soft_hard invalid in RangeCheck." % (value,))
        self._soft_hard = value


Instruction:
Implement a function in Python 3 to
build the ISO - 8601 range check XML by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(SoftHard=self.soft_hard.value, Comparator=self.comparator.value)
        builder.start("RangeCheck", params)
        if self.check_value is not None:
            self.check_value.build(builder)
        if self.measurement_unit_ref is not None:
            self.measurement_unit_ref.build(builder)
        builder.end("RangeCheck")


Instruction:
Write a Python 3 script to
build XML by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        builder.start("CheckValue", {})
        builder.data(str(self.value))
        builder.end("CheckValue")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build(self, builder):
        params = dict(OID=self.oid, Name=self.name, DataType=self.datatype.value)
        if self.sas_format_name is not None:
            params["SASFormatName"] = self.sas_format_name
        builder.start("CodeList", params)

        for item in self.codelist_items:
            item.build(builder)

        for alias in self.aliases:
            alias.build(builder)

        builder.end("CodeList")

Response: Builds the XML by appending to builder


Instruction:
Here you have a function in Python 3, explain what it does
def build(self, builder):
        params = dict(CodedValue=self.coded_value)
        if self.order_number is not None:
            params["mdsol:OrderNumber"] = str(self.order_number)

        if self.specify:
            params["mdsol:Specify"] = "Yes"

        builder.start("CodeListItem", params)

        if self.decode is not None:
            self.decode.build(builder)

        for alias in self.aliases:
            alias.build(builder)

        builder.end("CodeListItem")

Response: Builds the XML by appending to builder


Instruction:
Write a Python 3 script for
building this item : param builder : Induce the tree structure of the item

Response:
def build(self, builder):
        """
        Build this item
        :param builder:
        :return:
        """
        params = dict(Context=self.context, Name=self.name)
        builder.start("Alias", params)
        builder.end("Alias")


Instruction:
Explain what the following Python 3 code does
def build(self, builder):
        params = dict(LabelOID=self.oid, OrderNumber=str(self.order_number))

        builder.start("mdsol:LabelRef", params)
        builder.end("mdsol:LabelRef")

Response: Build XML by appending to builder


Instruction:
Write a Python 3 function for
appending this attribute to the XML builder.

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(
            Namespace=self.namespace,
            Name=self.name,
            Value=self.value,
            TransactionType=self.transaction_type,
        )

        builder.start("mdsol:Attribute", params)
        builder.end("mdsol:Attribute")


Instruction:
Write a Python 3 function that can
append this custom function definition to an xml. Builder object.

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(OID=self.oid, Language=self.language)
        builder.start("mdsol:CustomFunctionDef", params)
        builder.data(self.code)
        builder.end("mdsol:CustomFunctionDef")


Instruction:
Can you create a Python 3 function that
builds the XML by appending to the given XML builder.

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(
            OID=self.oid,
            Active=bool_to_true_false(self.active),
            BypassDuringMigration=bool_to_true_false(self.bypass_during_migration),
            NeedsRetesting=bool_to_true_false(self.needs_retesting),
        )

        if self.variable_oid is not None:
            params["VariableOID"] = self.variable_oid

        if self.field_oid is not None:
            params["FieldOID"] = self.field_oid

        if self.form_oid is not None:
            params["FormOID"] = self.form_oid

        if self.folder_oid is not None:
            params["FolderOID"] = self.folder_oid

        if self.record_position is not None:
            params["RecordPosition"] = str(self.record_position)

        if self.form_repeat_number is not None:
            params["FormRepeatNumber"] = str(self.form_repeat_number)

        if self.folder_repeat_number is not None:
            params["FolderRepeatNumber"] = str(self.folder_repeat_number)

        if self.all_variables_in_folders is not None:
            params["AllVariablesInFolders"] = bool_to_true_false(
                self.all_variables_in_folders
            )

        if self.all_variables_in_fields is not None:
            params["AllVariablesInFields"] = bool_to_true_false(
                self.all_variables_in_fields
            )

        if self.logical_record_position is not None:
            params["LogicalRecordPosition"] = self.logical_record_position.value

        builder.start("mdsol:DerivationDef", params)
        for step in self.derivation_steps:
            step.build(builder)
        builder.end("mdsol:DerivationDef")


Instruction:
Given the following Python 3 function, write the documentation
def build(self, builder):
        params = dict()

        if self.variable_oid is not None:
            params["VariableOID"] = self.variable_oid

        if self.data_format is not None:
            params["DataFormat"] = self.data_format

        if self.folder_oid is not None:
            params["FolderOID"] = self.folder_oid

        if self.field_oid is not None:
            params["FieldOID"] = self.field_oid

        if self.form_oid is not None:
            params["FormOID"] = self.form_oid

        if self.static_value is not None:
            params["StaticValue"] = self.static_value

        if self.function is not None:
            params["Function"] = self.function.value

        if self.custom_function is not None:
            params["CustomFunction"] = self.custom_function

        if self.record_position is not None:
            params["RecordPosition"] = str(self.record_position)

        if self.form_repeat_number is not None:
            params["FormRepeatNumber"] = str(self.form_repeat_number)

        if self.folder_repeat_number is not None:
            params["FolderRepeatNumber"] = str(self.folder_repeat_number)

        if self.logical_record_position is not None:
            params["LogicalRecordPosition"] = self.logical_record_position.value

        builder.start("mdsol:CheckStep", params)
        builder.end("mdsol:CheckStep")

Response: Builds the XML by appending to the given XML builder


Instruction:
Write a Python 3 script for
setting the value for the CheckActionType validating input

Response:
def check_action_type(self, value):
        """Set the value for the CheckActionType, validating input"""
        if value is not None:
            if not isinstance(value, ActionType):
                raise AttributeError("Invalid check action %s" % value)
        self._check_action_type = value


Instruction:
Can you create a Python 3 function that
builds the xml by appending it to the given XML builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(
            OID=self.oid,
            Active=bool_to_true_false(self.active),
            BypassDuringMigration=bool_to_true_false(self.bypass_during_migration),
            NeedsRetesting=bool_to_true_false(self.needs_retesting),
        )

        builder.start("mdsol:EditCheckDef", params)
        for step in self.check_steps:
            step.build(builder)

        for action in self.check_actions:
            action.build(builder)
        builder.end("mdsol:EditCheckDef")


Instruction:
How would you code a function in Python 3 to
build the XML by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""

        params = dict(OID=self.oid, Name=self.name)
        if self.field_number is not None:
            params["FieldNumber"] = str(self.field_number)

        builder.start("mdsol:LabelDef", params)

        for translation in self.translations:
            translation.build(builder)

        for view_restriction in self.view_restrictions:
            view_restriction.build(builder)

        builder.end("mdsol:LabelDef")


Instruction:
Here you have a function in Python 3, explain what it does
def build(self, builder):
        builder.start("mdsol:ReviewGroup", {})
        builder.data(self.name)
        builder.end("mdsol:ReviewGroup")

Response: Build the XML by appending to builder


Instruction:
Explain what the following Python 3 code does
def build(self, builder):
        params = dict(MetaDataVersionOID=str(self.metadata_version_oid),
                      StudyOID="%s (%s)" % (self.projectname, self.environment,),
                      )

        # mixins
        self.mixin_params(params)

        builder.start("ClinicalData", params)
        # Ask children
        if self.subject_data:
            for subject in self.subject_data:
                subject.build(builder)
        # Add the Annotations
        if self.annotations is not None:
            self.annotations.build(builder)
        builder.end("ClinicalData")

Response: Build XML by appending to builder


Instruction:
Can you create a Python 3 function that
builds the SubjectData XML by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(SubjectKey=self.subject_key)
        params['mdsol:SubjectKeyType'] = self.subject_key_type

        if self.transaction_type is not None:
            params["TransactionType"] = self.transaction_type

        # mixins
        self.mixin()
        self.mixin_params(params)

        builder.start("SubjectData", params)

        # Ask children
        if self.audit_record is not None:
            self.audit_record.build(builder)

        if self.siteref:
            self.siteref.build(builder)
        else:
            builder.start("SiteRef", {'LocationOID': self.sitelocationoid})
            builder.end("SiteRef")

        for event in self.study_events:
            event.build(builder)

        if self.signature is not None:
            self.signature.build(builder)

        for annotation in self.annotations:
            annotation.build(builder)

        builder.end("SubjectData")


Instruction:
Can you create a Python 3 function that
builds an XML string by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder
        :Example:
        <StudyEventData StudyEventOID="SCREENING" StudyEventRepeatKey="1" TransactionType="Update">

        """
        params = dict(StudyEventOID=self.study_event_oid)

        if self.transaction_type is not None:
            params["TransactionType"] = self.transaction_type

        if self.study_event_repeat_key is not None:
            params["StudyEventRepeatKey"] = str(self.study_event_repeat_key)

        # mixins
        self.mixin()
        self.mixin_params(params)

        builder.start("StudyEventData", params)

        # Ask children
        for form in self.forms:
            form.build(builder)

        if self.signature is not None:
            self.signature.build(builder)

        for annotation in self.annotations:
            annotation.build(builder)

        builder.end("StudyEventData")


Instruction:
Can you write a function in Python 3 where it
builds XML by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder
        :Example:
        <FormData FormOID="MH" TransactionType="Update">

        """
        params = dict(FormOID=self.formoid)

        if self.transaction_type is not None:
            params["TransactionType"] = self.transaction_type

        if self.form_repeat_key is not None:
            params["FormRepeatKey"] = str(self.form_repeat_key)

        # mixins
        self.mixin()
        self.mixin_params(params)

        builder.start("FormData", params)

        # Ask children
        for itemgroup in self.itemgroups:
            itemgroup.build(builder, self.formoid)

        if self.signature is not None:
            self.signature.build(builder)

        for annotation in self.annotations:
            annotation.build(builder)

        builder.end("FormData")


Instruction:
Make a summary of the following Python 3 code
def build(self, builder, formname=None):

        params = dict(ItemGroupOID=formname if formname else self.itemgroupoid)

        if self.transaction_type is not None:
            params["TransactionType"] = self.transaction_type

        if self.item_group_repeat_key is not None:
            params["ItemGroupRepeatKey"] = str(
                self.item_group_repeat_key)  # may be @context for transaction type upsert or context

        params["mdsol:Submission"] = "WholeItemGroup" if self.whole_item_group else "SpecifiedItemsOnly"

        # mixins
        self.mixin()
        self.mixin_params(params)

        builder.start("ItemGroupData", params)

        # Ask children
        for item in self.items.values():
            item.build(builder)

        # Add annotations
        for annotation in self.annotations:
            annotation.build(builder)

        # Add the signature if it exists
        if self.signature is not None:
            self.signature.build(builder)
        builder.end("ItemGroupData")

Response: Builds the XML by appending to builder


Instruction:
Can you tell what is the following Python 3 function doing
def build(self, builder):
        params = dict(ItemOID=self.itemoid)

        if self.transaction_type is not None:
            params["TransactionType"] = self.transaction_type

        if self.value in [None, '']:
            params['IsNull'] = 'Yes'
        else:
            params['Value'] = str(self.value)

        if self.specify_value is not None:
            params['mdsol:SpecifyValue'] = self.specify_value

        if self.lock is not None:
            params['mdsol:Lock'] = bool_to_yes_no(self.lock)

        if self.freeze is not None:
            params['mdsol:Freeze'] = bool_to_yes_no(self.freeze)

        if self.verify is not None:
            params['mdsol:Verify'] = bool_to_yes_no(self.verify)

        # mixins
        self.mixin()
        self.mixin_params(params)

        builder.start("ItemData", params)

        if self.audit_record is not None:
            self.audit_record.build(builder)

        # Measurement unit ref must be after audit record or RWS complains
        if self.measurement_unit_ref is not None:
            self.measurement_unit_ref.build(builder)

        for query in self.queries:  # type: MdsolQuery
            query.build(builder)

        for deviation in self.deviations:  # type: MdsolProtocolDeviation
            deviation.build(builder)

        for annotation in self.annotations:  # type: Annotation
            annotation.build(builder)

        builder.end("ItemData")

Response: Builds the XML by appending to the given XML builder.


Instruction:
How would you explain what the following Python 3 function does
def build(self, builder):

        params = {}
        if self.signature_id is not None:
            # If a Signature element is contained within a Signatures element, the ID attribute is required.
            params['ID'] = self.signature_id

        builder.start("Signature", params)

        if self.user_ref is None:
            raise ValueError("User Reference not set.")
        self.user_ref.build(builder)

        if self.location_ref is None:
            raise ValueError("Location Reference not set.")
        self.location_ref.build(builder)

        if self.signature_ref is None:
            raise ValueError("Signature Reference not set.")
        self.signature_ref.build(builder)

        if self.date_time_stamp is None:
            raise ValueError("DateTime not set.")
        self.date_time_stamp.build(builder)

        builder.end("Signature")

Response: Builds the contents of this object into the XML builder.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def annotation_id(self, value):
        if value in [None, ''] or str(value).strip() == '':
            raise AttributeError("Invalid ID value supplied")
        self._id = value

Response: Set the ID for this object.


Instruction:
Explain what the following Python 3 code does
def seqnum(self, value):
        if not re.match(r'\d+', str(value)) or value < 0:
            raise AttributeError("Invalid SeqNum value supplied")
        self._seqnum = value

Response: Set the SeqNum of the object for this object.


Instruction:
How would you implement a function in Python 3 that
builds the XML by appending to the given XML builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        params = {}

        # Add in the transaction type
        if self.transaction_type is not None:
            params["TransactionType"] = self.transaction_type

        if self.seqnum is None:
            # SeqNum is not optional (and defaulted)
            raise ValueError("SeqNum is not set.")  # pragma: no cover
        params["SeqNum"] = str(self.seqnum)

        if self.annotation_id is not None:
            # If an Annotation is contained with an Annotations element,
            # the ID attribute is required.
            params["ID"] = self.annotation_id

        builder.start("Annotation", params)

        if self.flags in (None, []):
            raise ValueError('Flag is not set.')

        # populate the flags
        for flag in self.flags:
            flag.build(builder)

        # add the Comment, if it exists
        if self.comment is not None:
            self.comment.build(builder)

        builder.end("Annotation")


Instruction:
Write a Python 3 script for
building the XML by appending it to the given XML builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        builder.start("Annotations")

        # populate the flags
        for annotation in self.annotations:
            annotation.build(builder)

        builder.end("Annotations")


Instruction:
Write a Python 3 function that can
set the text content for Comment

Response:
def text(self, value):
        """Set Text content for Comment (validation of input)"""
        if value in (None, '') or value.strip() == "":
            raise AttributeError("Empty text value is invalid.")
        self._text = value


Instruction:
Given the following Python 3 function, write the documentation
def sponsor_or_site(self, value):
        if value not in Comment.VALID_SPONSOR_OR_SITE_RESPONSES:
            raise AttributeError("%s sponsor_or_site value of %s is not valid" % (self.__class__.__name__,
                                                                                value))
        self._sponsor_or_site = value

Response: Set Originator with validation of input


Instruction:
Given the following Python 3 function, write the documentation
def build(self, builder):
        if self.text is None:
            raise ValueError("Text is not set.")
        params = {}
        if self.sponsor_or_site is not None:
            params['SponsorOrSite'] = self.sponsor_or_site

        builder.start("Comment", params)
        builder.data(self.text)
        builder.end("Comment")

Response: Builds the XML by appending to builder


Instruction:
Implement a Python 3 function for
building the object into an XML string.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        builder.start("Flag", {})

        if self.flag_type is not None:
            self.flag_type.build(builder)

        if self.flag_value is None:
            raise ValueError('FlagValue is not set.')
        self.flag_value.build(builder)

        builder.end("Flag")


Instruction:
Can you create a Python 3 function that
builds the object to XML by appending it to the given XML builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        if self.codelist_oid is None:
            raise ValueError("CodeListOID not set.")
        builder.start("FlagValue", dict(CodeListOID=self.codelist_oid))
        builder.data(self.flag_value)
        builder.end("FlagValue")


Instruction:
Create a Python 3 function to
build the ASN. 1 User Ref element into the given XML builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        builder.start("UserRef", dict(UserOID=self.oid))
        builder.end("UserRef")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build(self, builder):
        builder.start("LocationRef", dict(LocationOID=str(self.oid)))
        builder.end("LocationRef")

Response: Build an XML string representing this object into the given XML builder.


Instruction:
Write a Python 3 function for
building the XML by appending to builder

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        params = dict(LocationOID=self.oid)
        # mixins
        self.mixin()
        self.mixin_params(params)

        builder.start("SiteRef", params)
        builder.end("SiteRef")


Instruction:
Explain what the following Python 3 code does
def build(self, builder):
        builder.start("SignatureRef", dict(SignatureOID=self.oid))
        builder.end("SignatureRef")

Response: Build the XML for this object into the given builder.


Instruction:
Write a Python 3 function for
building the XML description of this object into the given XML builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        builder.start("ReasonForChange", {})
        builder.data(self.reason)
        builder.end("ReasonForChange")


Instruction:
Can you tell what is the following Python 3 function doing
def build(self, builder):
        builder.start("DateTimeStamp", {})
        if isinstance(self.date_time, datetime):
            builder.data(dt_to_iso8601(self.date_time))
        else:
            builder.data(self.date_time)
        builder.end("DateTimeStamp")

Response: Builds the ISO - 8601 date time entry into the XML builder.


Instruction:
Write a Python 3 script for
building the XML for this object into the given XML builder.

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        builder.start("SourceID", {})
        builder.data(self.source_id)
        builder.end("SourceID")


Instruction:
Write a Python 3 function that can
build the audit record into the XML string.

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = {}

        if self.edit_point is not None:
            params["EditPoint"] = self.edit_point

        if self.used_imputation_method is not None:
            params['UsedImputationMethod'] = bool_to_yes_no(self.used_imputation_method)

        if self.audit_id is not None:
            params['ID'] = str(self.audit_id)

        if self.include_file_oid is not None:
            params['mdsol:IncludeFileOID'] = bool_to_yes_no(self.include_file_oid)

        builder.start("AuditRecord", params)
        if self.user_ref is None:
            raise ValueError("User Reference not set.")
        self.user_ref.build(builder)

        if self.location_ref is None:
            raise ValueError("Location Reference not set.")
        self.location_ref.build(builder)

        if self.date_time_stamp is None:
            raise ValueError("DateTime not set.")

        self.date_time_stamp.build(builder)

        # Optional
        if self.source_id:
            self.source_id.build(builder)
        # Optional
        if self.reason_for_change is not None:
            self.reason_for_change.build(builder)

        builder.end("AuditRecord")


Instruction:
Can you create a Python 3 function that
appends this ProtocolDeviation to the XML builder.

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(Value=self.value,
                      Status=self.status.value,
                      ProtocolDeviationRepeatKey=self.repeat_key
                      )

        if self.code:
            params['Code'] = self.code
        if self.pdclass:
            params['Class'] = self.pdclass
        if self.transaction_type:
            params['TransactionType'] = self.transaction_type
        builder.start('mdsol:ProtocolDeviation', params)
        builder.end('mdsol:ProtocolDeviation')


Instruction:
Write a Python 3 script for
setting the status of the resource.

Response:
def status(self, value):
        """Set Query Status"""
        if value is not None:
            if not isinstance(value, QueryStatusType):
                raise AttributeError("%s action type is invalid in mdsol:Query." % (value,))
        self._status = value


Instruction:
Can you create a Python 3 function that
builds the XML by appending to builder

Response:
def build(self, builder):
        """
        Build XML by appending to builder
        """
        params = {}

        if self.value is not None:
            params['Value'] = str(self.value)

        if self.query_repeat_key is not None:
            params['QueryRepeatKey'] = str(self.query_repeat_key)

        if self.recipient is not None:
            params['Recipient'] = str(self.recipient)

        if self.status is not None:
            params['Status'] = self.status.value

        if self.requires_response is not None:
            params['RequiresResponse'] = bool_to_yes_no(self.requires_response)

        # When closing a query
        if self.response is not None:
            params['Response'] = str(self.response)

        builder.start("mdsol:Query", params)
        builder.end("mdsol:Query")


Instruction:
How would you explain what the following Python 3 function does
def getroot(self):
        builder = ET.TreeBuilder()
        self.build(builder)
        return builder.close()

Response: Build XML object return the root


Instruction:
How would you implement a function in Python 3 that
builds the XML object and return the root element

Response:
def build(self, builder):
        """Build XML object, return the root, this is a copy for consistency and testing"""
        params = dict(ODMVersion="1.3",
                      FileType=self.filetype,
                      CreationDateTime=self.creationdatetime,
                      Originator=self.originator,
                      FileOID=self.fileoid,
                      xmlns="http://www.cdisc.org/ns/odm/v1.3",
                      )
        if self.granularity_type:
            params['Granularity'] = self.granularity_type.value
        if self.source_system:
            params['SourceSystem'] = self.source_system

        if self.source_system_version:
            params['SourceSystemVersion'] = self.source_system_version
        params['xmlns:mdsol'] = "http://www.mdsol.com/ns/odm/metadata"

        if self.description:
            params['Description'] = self.description

        builder.start("ODM", params)

        # Ask the children
        if self.study is not None:
            self.study.build(builder)

        if self.clinical_data:
            for clinical_data in self.clinical_data:
                clinical_data.build(builder)

        if self.admindata is not None:
            self.admindata.build(builder)

        builder.end("ODM")
        return builder.close()


Instruction:
Write a Python 3 script for
returning a dictionary of keyword arguments for the URL query string.

Response:
def _querystring(self):
        """Additional keyword arguments"""

        kw = {"studyoid": self.studyoid}
        if self.location_oid is not None:
            kw["locationoid"] = self.location_oid
        return kw


Instruction:
Given the following Python 3 function, write the documentation
def value(self):
        if isinstance(self.raw_value, (datetime.datetime, datetime.date)):
            return self.raw_value.isoformat()
        elif isinstance(self.raw_value, (bool,)):
            return 'Yes' if self.raw_value else 'No'
        return self.raw_value

Response: Normalise the Attribute value and return the normalised value


Instruction:
Write a Python 3 function that can
add an attribute to the current instance of the modm extension

Response:
def add_attribute(self, attribute, value):
        """
        Add an attribute to the current instance

        :param str attribute: Attribute name
        :type value: Union[datetime,bool,str]
        :param value: Attribute value
        """
        class_name = self.__class__.__name__
        if class_name.startswith('ItemData'):
            # ItemData* Elements
            class_name = 'ItemData'
        if attribute not in MODMExtensionRegistry[class_name].value:
            raise ValueError("Can't add {} to {}".format(attribute, self.__class__.__name__))
        self.attributes.append(MODMAttribute(attribute, value))


Instruction:
Can you tell what is the following Python 3 function doing
def mixin_params(self, params):
        if not isinstance(params, (dict,)):
            raise AttributeError("Cannot mixin to object of type {}".format(type(params)))
        for attribute in self.attributes:
            params.update({attribute.tag: attribute.value})

Response: Merge in the MdsolAttribute for the passed parameter


Instruction:
Explain what the following Python 3 code does
def last_update_time(self, value):
        if isinstance(value, (datetime.datetime,)):
            self._last_update_time = value
        else:
            raise ValueError("Expect last_update_time to be a datetime")

Response: Setter for the last_update_time attribute.


Instruction:
How would you explain what the following Python 3 function does
def set_update_time(self, update_time=None):
        if update_time and isinstance(update_time, (datetime.datetime,)):
            self.last_update_time = update_time
        else:
            self.last_update_time = datetime.datetime.utcnow()

Response: Set the Update Time from the local clock in UTC.


Instruction:
Can you implement a function in Python 3 that
adds the mdsol : LastUpdateTime attribute to the params.

Response:
def mixin_params(self, params):
        """
        Add the mdsol:LastUpdateTime attribute
        :return:
        """
        super(LastUpdateMixin, self).mixin_params(params)
        if self.last_update_time is not None:
            params.update({"mdsol:LastUpdateTime": self.last_update_time.isoformat()})


Instruction:
Given the following Python 3 function, write the documentation
def add_milestone(self,
                      milestone,
                      codelistoid="MILESTONES"):
        """
        Add a milestone
        :param codelistoid: specify the CodeListOID (defaults to MILESTONES)
        :param str milestone: Milestone to add
        """
        if milestone not in self.milestones.get(codelistoid, []):
            self._milestones.setdefault(codelistoid, []).append(milestone)

Response: Adds a milestone to the cache.


Instruction:
Can you generate the documentation for the following Python 3 function
def mixin(self):
        if self.milestones:
            from rwslib.builders.clinicaldata import Annotation, Flag, FlagValue
            annotation = Annotation()
            for codelist, milestones in self.milestones.items():
                for milestone in milestones:
                    annotation << Flag() << FlagValue(milestone, codelist_oid=codelist)
            self.annotations.append(annotation)

Response: Add the annotations to the ODM Element


Instruction:
Can you generate the documentation for the following Python 3 function
def getEnvironmentFromNameAndProtocol(studyname, protocolname):
    # StudyName =    "TEST (1) (DEV)"
    # ProtocolName = "TEST (1)"
    # Raw Env      =           "(DEV)"

    raw_env = studyname[len(protocolname) :].strip()

    if "(" in raw_env:
        l_brace_pos = raw_env.rfind("(")
        r_brace_pos = raw_env.rfind(")")
        return raw_env[l_brace_pos + 1 : r_brace_pos]
    else:
        return raw_env

Response: Extract environment name using studyname and protocolname


Instruction:
Can you tell what is the following Python 3 function doing
def parseXMLString(xml):

    # Remove BOM if it exists (different requests seem to have different BOMs)
    unichr_captured = ""

    if not xml.strip():
        return u""

    while xml[0] != u"<":
        unichr_captured += xml[0]
        xml = xml[1:]
    parser = etree.XMLParser(ns_clean=True, collect_ids=False, huge_tree=True)
    try:
        return etree.fromstring(xml.encode("utf-8"), parser=parser)
    except etree.XMLSyntaxError:
        raise Exception(xml)

Response: Parse XML string and return root XML object


Instruction:
How would you explain what the following Python 3 function does
def fromElement(cls, elem):
        e_global_variables = elem.find(ODM_NS + "GlobalVariables")
        studyname = e_global_variables.find(ODM_NS + "StudyName").text
        protocolname = e_global_variables.find(ODM_NS + "ProtocolName").text
        self = cls(
            oid=elem.get("OID"),
            projecttype=elem.get(MEDI_NS + "ProjectType", "Project"),
            studyname=studyname,
            protocolname=protocolname,
            environment=getEnvironmentFromNameAndProtocol(studyname, protocolname),
        )

        return self

Response: Read properties from an XML Element to build a StudyList Item.


Instruction:
Write a Python 3 script for
reading properties from a MetaDataVersion element.

Response:
def fromElement(cls, elem):
        """
        Read properties from a MetaDataVersion element

        :param lxml.etree._Element elem: Source etree Element
        """

        self = cls()

        self.oid = elem.get("OID")
        self.name = elem.get("Name")
        return self


Instruction:
Given the following Python 3 function, write the documentation
def subject_name(self):
        if self.subjectkeytype and self.subjectkeytype == "SubjectUUID".lower():
            # if the SubjectKeyType is "SubjectUUID", then return the SubjectName
            return self.subjectname
        else:
            return self.subjectkey

Response: Get the subject name consistently


Instruction:
Given the following Python 3 function, write the documentation
def fromElement(cls, elem):
        self = cls()
        self.studyoid = elem.get("StudyOID")
        self.metadataversionoid = elem.get("MetaDataVersionOID")
        e_subjectdata = elem.findall(ODM_NS + "SubjectData")[0]
        self.subjectkey = e_subjectdata.get("SubjectKey")
        e_siteref = e_subjectdata.findall(ODM_NS + "SiteRef")[0]
        self.locationoid = e_siteref.get("LocationOID")

        e_links = e_subjectdata.findall(MEDI_NS + "Link")
        for e_link in e_links:
            self.links.append(e_link.get(XLINK_NS + "href"))

        decodes = {"yes": True, "no": False, "": None}
        for prop in RWSSubjectListItem.STATUS_PROPERTIES:
            val = e_subjectdata.get(MEDI_NS + prop, "").lower()
            setattr(self, prop.lower(), decodes.get(val, val))

        # By default we only get back active and non-deleted subjects
        self.active = decodes[
            e_subjectdata.get(MEDI_NS + "SubjectActive", "yes").lower()
        ]
        self.deleted = decodes[e_subjectdata.get(MEDI_NS + "Deleted", "no").lower()]

        return self

Response: Read properties from an XML Element object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def make_element(builder, tag, content):
    builder.start(tag, {})
    builder.data(content)  # Must be UTF-8 encoded
    builder.end(tag)

Response: Make an element with this tag and text content


Instruction:
Write a Python 3 function for
using to set guard the setting of an attribute which is singular and can t be set twice

Response:
def set_single_attribute(self, other, trigger_klass, property_name):
        """Used to set guard the setting of an attribute which is singular and can't be set twice"""

        if isinstance(other, trigger_klass):

            # Check property exists
            if not hasattr(self, property_name):
                raise AttributeError("%s has no property %s" % (self.__class__.__name__, property_name))

            if getattr(self, property_name) is None:
                setattr(self, property_name, other)
            else:
                raise ValueError(
                    '%s already has a %s element set.' % (self.__class__.__name__, other.__class__.__name__,))


Instruction:
Here you have a function in Python 3, explain what it does
def set_list_attribute(self, other, trigger_klass, property_name):
        # Check property exists
        if isinstance(other, trigger_klass):

            if not hasattr(self, property_name):
                raise AttributeError("%s has no property %s" % (self.__class__.__name__, property_name))

            val = getattr(self, property_name, [])
            if other in val:
                raise ValueError("%s already exists in %s" % (other.__class__.__name__, self.__class__.__name__))
            else:
                val.append(other)
                setattr(self, property_name, val)

Response: Used to set guard the setting of a list attribute.


Instruction:
Write a Python 3 script to
set the TransactionType ( with Input Validation

Response:
def transaction_type(self, value):
        """Set the TransactionType (with Input Validation)"""
        if value is not None:
            if value not in self.ALLOWED_TRANSACTION_TYPES:
                raise AttributeError('%s transaction_type element must be one of %s not %s' % (
                    self.__class__.__name__, ','.join(self.ALLOWED_TRANSACTION_TYPES), value,))
        self._transaction_type = value


Instruction:
Here you have a function in Python 3, explain what it does
def build(self, builder):
        params = {}
        if self.study_oid:
            params.update(dict(StudyOID=self.study_oid))
        builder.start("AdminData", params)
        for user in self.users:
            user.build(builder)
        for location in self.locations:
            location.build(builder)

        builder.end("AdminData")

Response: Build XML by appending to builder


Instruction:
Can you tell what is the following Python 3 function doing
def build(self, builder):
        params = dict(StudyOID=self.study_oid,
                      MetaDataVersionOID=self.metadata_version_oid,
                      EffectiveDate=dt_to_iso8601(self.effective_date))
        builder.start("MetaDataVersionRef", params)
        builder.end("MetaDataVersionRef")

Response: Build XML by appending to builder


Instruction:
Write a Python 3 script for
building XML by appending to builder

Response:
def build(self, builder):
        """Build XML by appending to builder"""
        params = dict(OID=self.oid,
                      Name=self.name)
        if self.location_type:
            params.update(dict(LocationType=self.location_type.value))
        # mixins
        self.mixin()
        self.mixin_params(params)
        builder.start("Location", params)
        for mdv in self.metadata_versions:
            mdv.build(builder)
        builder.end("Location")


Instruction:
Here you have a function in Python 3, explain what it does
def build(self, builder):
        params = dict()
        builder.start(self.__class__.__name__, params)
        for street in self.street_names:
            street.build(builder)
        # build the children
        for child in ('city', 'country', 'state_prov', 'postal_code', 'other_text'):
            if getattr(self, child) is not None:
                getattr(self, child).build(builder)
        builder.end(self.__class__.__name__)

Response: Build the XML by appending to the given XML builder.


Instruction:
Can you generate the documentation for the following Python 3 function
def build(self, builder):
        params = dict(OID=self.oid)
        if self.user_type:
            params.update(dict(UserType=self.user_type.value))
        builder.start(self.__class__.__name__, params)
        # build the children
        for child in ('login_name', 'display_name', 'full_name', 'first_name', 'last_name',
                      'organisation'):
            if getattr(self, child) is not None:
                getattr(self, child).build(builder)
        for address in self.addresses:
            address.build(builder)
        for email in self.emails:
            email.build(builder)
        for phone in self.phones:
            phone.build(builder)
        for location in self.locations:
            location.build(builder)
        builder.end(self.__class__.__name__)

Response: Builds the xml by appending it to the given XML builder object.


Instruction:
Can you tell what is the following Python 3 function doing
def build(self, builder):
        builder.start(self.__class__.__name__)
        builder.data(self.country_code)
        builder.end(self.__class__.__name__)

Response:
Builds this element
        : param builder : A builder object.


Instruction:
Write a Python 3 function that can
extract next start id from link for next result set and get the next start id

Response:
def get_next_start_id(self):
        """If link for next result set has been passed, extract it and get the next set start id"""
        link = self.rws_connection.last_result.links.get("next", None)
        if link:
            link = link['url']
            p = urlparse(link)
            start_id = int(parse_qs(p.query)['startid'][0])
            return start_id

        return None


Instruction:
How would you implement a function in Python 3 that
sends a request to the RWS.

Response:
def send_request(self, request_object, timeout=None, retries=1, **kwargs):
        """Send request to RWS endpoint. The request object passed provides the URL endpoint and the HTTP method.
           Takes the text response from RWS and allows the request object to modify it for return. This allows the request
           object to return text, an XML document object, a CSV file or anything else that can be generated from the text
           response from RWS.
           A timeout, in seconds, can be optionally passed into send_request.
        """
        if not isinstance(request_object, RWSRequest):
            raise ValueError("Request object must be a subclass of RWSRequest")

        # Construct a URL from the object and make a call
        full_url = make_url(self.base_url, request_object.url_path())
        if request_object.requires_authorization:
            kwargs["auth"] = self.auth
            # TODO: Look at different connect and read timeouts?
            kwargs["timeout"] = timeout
            kwargs.update(request_object.args())

        # Explicit use of requests library here. Could alter in future to inject library to use in case
        # requests not available.

        # Get a session that allows us to customize HTTP requests
        session = requests.Session()

        # Mount a custom adapter that retries failed connections for HTTP and HTTPS requests.
        for scheme in ["http://", "https://"]:
            session.mount(scheme, requests.adapters.HTTPAdapter(max_retries=retries))

        action = {"GET": session.get, "POST": session.post}[request_object.method]

        start_time = time.time()

        try:
            r = action(full_url, **kwargs)  # type: requests.models.Response
        except (
            requests.exceptions.ConnectTimeout,
            requests.exceptions.ReadTimeout,
        ) as exc:
            if isinstance(exc, (requests.exceptions.ConnectTimeout,)):
                raise RWSException(
                    "Server Connection Timeout",
                    "Connection timeout for {}".format(full_url),
                )
            elif isinstance(exc, (requests.exceptions.ReadTimeout,)):
                raise RWSException(
                    "Server Read Timeout", "Read timeout for {}".format(full_url)
                )

        self.request_time = time.time() - start_time
        self.last_result = r  # see also r.elapsed for timedelta object.

        if r.status_code in [400, 404]:
            # Is it a RWS response?
            if r.text.startswith("<Response"):
                error = RWSErrorResponse(r.text)
                raise RWSException(error.errordescription, error)
            elif "<html" in r.text:
                raise RWSException("IIS Error", r.text)
            else:
                error = RWSError(r.text)
            raise RWSException(error.errordescription, error)

        elif r.status_code == 500:
            raise RWSException("Server Error (500)", r.text)

        elif r.status_code == 401:
            # Either you didn't supply auth header and it was required OR your credentials were wrong
            # RWS handles each differently

            # You didn't supply auth (text response from RWS)
            if r.text == "Authorization Header not provided":
                raise AuthorizationException(r.text)

            if "
HTTP Error 401.0 - Unauthorized
" in r.text:
                raise RWSException("Unauthorized.", r.text)

            # Check if the content_type is text/xml.  Use startswith
            # in case the charset is also specified:
            #  content-type: text/xml; charset=utf-8
            if r.headers.get("content-type").startswith("text/xml"):
                # XML response
                if r.text.startswith("<Response"):
                    error = RWSErrorResponse(r.text)
                elif "ODM" in r.text:
                    error = RWSError(r.text)
            else:
                # There was some problem with your credentials (XML response from RWS)
                error = RWSErrorResponse(r.text)
            raise RWSException(error.errordescription, error)

        # Catch all.
        if r.status_code != 200:
            if "<" in r.text:
                # XML like
                if r.text.strip().startswith("<Response"):
                    error = RWSErrorResponse(r.text)
                elif "ODM" in r.text:
                    error = RWSError(r.text)
                else:
                    # IIS error page as an example
                    raise RWSException(
                        "Unexpected Status Code ({0.status_code})".format(r), r.text
                    )
            else:
                # not XML like, better to be safe than blow up
                # example response: 'HTTP 503 Service Temporarily Unavailable'
                raise RWSException(
                    "Unexpected Status Code ({0.status_code})".format(r), r.text
                )
            raise RWSException(error.errordescription, error)

        return request_object.result(r)


Instruction:
Write a Python 3 script for
testing demonstrating building clinical data

Response:
def example_clinical_data(study_name, environment):
    """Test demonstrating building clinical data"""

    odm = ODM("test system")(
       ClinicalData("Mediflex", "DEV")(
          SubjectData("MDSOL", "IJS TEST4", transaction_type="Insert")(
             StudyEventData("SUBJECT")(
                FormData("EN", transaction_type="Update")(
                   # Although Signature is ODM1.3.1 RWS does not support it inbound currently
                   # RWSBuilders do support outbound generation of Signature at FormData level
                   # Signature()(
                   #    UserRef("isparks"),
                   #    LocationRef("MDSOL"),
                   #    SignatureRef("APPROVED"),
                   #    DateTimeStamp(datetime(2015, 9, 11, 10, 15, 22, 80))
                   # ),
                   ItemGroupData()(
                      ItemData("SUBJINIT", "AAA")(
                            AuditRecord(edit_point=AuditRecord.EDIT_DATA_MANAGEMENT,
                                      used_imputation_method= False,
                                      identifier='X2011',
                                      include_file_oid=False)(
                                            UserRef("isparks"),
                                            LocationRef("MDSOL"),
                                            ReasonForChange("Data Entry Error"),
                                            DateTimeStamp(datetime(2015, 9, 11, 10, 15, 22, 80))
                            ),
                            MdsolQuery(value="Subject initials should be 2 chars only.", recipient="Site from System",
                                       status=QueryStatusType.Open)
                      ),
                      ItemData("SUBJID", '001')
                   )
                )
             )
          )
       )
    )
    return odm


Instruction:
Make a summary of the following Python 3 code
def getCSVReader(data, reader_type=csv.DictReader):
        f = StringIO(data[:-4])  # Remove \nEOF
        return reader_type(f)

Response: Take a Rave CSV output ending with just EOF on it and return a DictReader


Instruction:
Here you have a function in Python 3, explain what it does
def _setDatasets(self, metadata):
        # Pull the lot into a list
        cols = list(self.getCSVReader(metadata))

        # sort the cols by view name and ordinal (they come sorted, but lets be sure)
        cols.sort(key=lambda x: [x['viewname'], int(x['ordinal'])])

        # Now group them by their viewname
        for key, grp in groupby(cols, key=lambda x: x["viewname"]):
            self.datasets[key] = list(grp)

Response: Extract dataset definitions from CSV metadata


Instruction:
Implement a function in Python 3 to
take a string of form data as CSV and convert to insert statements return template and data values

Response:
def processFormData(self, data, dataset_name):
        """Take a string of form data as CSV and convert to insert statements, return template and data values"""

        # Get the cols for this dataset
        cols = self.datasets[dataset_name]
        reader = self.getCSVReader(data, reader_type=csv.reader)

        # Get fieldnames from first line of reader, what is left is set of rows
        fieldnames = next(reader)

        # Check columns
        for col in cols:
            varname = col["varname"]
            if varname not in fieldnames:
                raise ValueError("Column %s not found in data for dataset %s" % (varname, dataset_name,))

        # Now call overrideen methods of base classes to process this data
        self._processDML(dataset_name, cols, reader)


Instruction:
Create a Python 3 function for
generating and process SQLLite tables

Response:
def _processDDL(self):
        """Generate and process table SQL, SQLLite version"""
        sql_statements = self._generateDDL()

        logging.info('Generating sqllite tables')
        for stmt in sql_statements:
            c = self.conn.cursor()
            c.execute(stmt)
            self.conn.commit()


Instruction:
How would you explain what the following Python 3 function does
def _generateDDL(self):
        sql = []
        # Next convert each set of columns into a table structure
        for dataset_name in sorted(self.datasets.keys()):

            # SQL to drop the table if it already exists
            sql.append('''drop table if exists %s;''' % dataset_name)

            # Generate the SQL for the cols
            cols = self.datasets[dataset_name]
            col_defs = []
            for col in cols:
                sql_datatype = self.getSQLDataType(col["vartype"])
                col_defs.append("%s %s" % (col["varname"], sql_datatype,))

            stmt = 'CREATE TABLE %s (%s)' % (dataset_name, ','.join(col_defs))
            sql.append(stmt)

        return sql

Response: Generate DDL statements for SQLLite


Instruction:
Make a summary of the following Python 3 code
def _processDML(self, dataset_name, cols, reader):
        sql_template = self._generateInsertStatement(dataset_name, cols)

        # Now insert in batch, reader is a list of rows to insert at this point
        c = self.conn.cursor()
        c.executemany(sql_template, reader)
        self.conn.commit()

Response: Overridden version of create DML for SQLLite


Instruction:
Here you have a function in Python 3, explain what it does
def _generateInsertStatement(self, dataset_name, cols):
        col_names = [col["varname"] for col in cols]

        # Generate question mark placeholders
        qms = ','.join(['?' for x in col_names])

        return 'INSERT INTO %s (%s) values (%s)' % (dataset_name, ','.join(col_names), qms)

Response: Generates a sql INSERT template


Instruction:
Can you generate the documentation for the following Python 3 function
def name_type_from_viewname(viewname):
        matched = VIEW_REX.match(viewname)
        return matched.group('view'), matched.group('type') or 'REGULAR'

Response: Return the view and type from a view name


Instruction:
Implement a Python 3 function for
generating local DB and metadata from RWSConnection

Response:
def execute(self):
        """Generate local DB, pulling metadata and data from RWSConnection"""

        logging.info('Requesting view metadata for project %s' % self.project_name)
        project_csv_meta = self.rws_connection.send_request(ProjectMetaDataRequest(self.project_name))

        # Process it into a set of tables
        self.db_adapter.processMetaData(project_csv_meta)

        # Get the data for the study
        for dataset_name in self.db_adapter.datasets.keys():
            logging.info('Requesting data from dataset %s' % dataset_name)
            form_name, _type = self.name_type_from_viewname(dataset_name)
            form_data = self.rws_connection.send_request(
                FormDataRequest(self.project_name, self.environment, _type, form_name))

            # Now process the form_data into the db of choice
            logging.info('Populating dataset %s' % dataset_name)
            self.db_adapter.processFormData(form_data, dataset_name)

        logging.info('Process complete')


Instruction:
Can you create a Python 3 function that
functions to duck - type values for rave - data.

Response:
def typeof_rave_data(value):
    """Function to duck-type values, not relying on standard Python functions because, for example,
    a string of '1' should be typed as an integer and not as a string or float
    since we're trying to replace like with like when scrambling."""

    # Test if value is a date
    for format in ['%d %b %Y', '%b %Y', '%Y', '%d %m %Y', '%m %Y', '%d/%b/%Y', '%b/%Y', '%d/%m/%Y', '%m/%Y']:
        try:
            datetime.datetime.strptime(value, format)
            if len(value) == 4 and (int(value) < 1900 or int(value) > 2030):
                break
            return ('date', format)
        except ValueError:
            pass
        except TypeError:
            pass

    # Test if value is a time
    for format in ['%H:%M:%S', '%H:%M', '%I:%M:%S', '%I:%M', '%I:%M:%S %p', '%I:%M %p']:
        try:
            datetime.datetime.strptime(value, format)
            return ('time', format)
        except ValueError:
            pass
        except TypeError:
            pass

    # Test if value is a integer
    try:
        if ((isinstance(value, str) and isinstance(int(value), int)) \
                    or isinstance(value, int)):
            return ('int', None)
    except ValueError:
        pass
    except TypeError:
        pass

    # Test if value is a float
    try:
        float(value)
        return ('float', None)
    except ValueError:
        pass
    except TypeError:
        pass

    # If no match on anything else, assume its a string
    return ('string', None)


Instruction:
Given the following Python 3 function, write the documentation
def scramble_float(self, length, sd=0):
        if sd == 0:
            return str(fake.random_number(length))
        else:
            return str(fake.pyfloat(length - sd, sd, positive=True))

Response: Return random float in specified format


Instruction:
Create a Python 3 function for
scrambling a value and return it as a string

Response:
def scramble_value(self, value):
        """Duck-type value and scramble appropriately"""
        try:
            type, format = typeof_rave_data(value)
            if type == 'float':
                i, f = value.split('.')
                return self.scramble_float(len(value) - 1, len(f))
            elif type == 'int':
                return self.scramble_int(len(value))
            elif type == 'date':
                return self.scramble_date(value, format)
            elif type == 'time':
                return self.scramble_time(format)
            elif type == 'string':
                return self.scramble_string(len(value))
            else:
                return value
        except:
            return ""


Instruction:
Write a Python 3 function that can
scramble a code list into a random element

Response:
def scramble_codelist(self, codelist):
        """Return random element from code list"""
        # TODO:  External code lists
        path = ".//{0}[@{1}='{2}']".format(E_ODM.CODELIST.value, A_ODM.OID.value, codelist)
        elem = self.metadata.find(path)
        codes = []
        for c in elem.iter(E_ODM.CODELIST_ITEM.value):
            codes.append(c.get(A_ODM.CODED_VALUE.value))
        for c in elem.iter(E_ODM.ENUMERATED_ITEM.value):
            codes.append(c.get(A_ODM.CODED_VALUE.value))

        return fake.random_element(codes)


Instruction:
Write a Python 3 script to
scramble the value based on the data type of the item.

Response:
def scramble_itemdata(self, oid, value):
        """If metadata provided, use it to scramble the value based on data type"""
        if self.metadata is not None:
            path = ".//{0}[@{1}='{2}']".format(E_ODM.ITEM_DEF.value, A_ODM.OID.value, oid)
            elem = self.metadata.find(path)
            # for elem in self.metadata.iter(E_ODM.ITEM_DEF.value):
            datatype = elem.get(A_ODM.DATATYPE.value)

            codelist = None
            for el in elem.iter(E_ODM.CODELIST_REF.value):
                codelist = el.get(A_ODM.CODELIST_OID.value)

            length = 1 if not A_ODM.LENGTH in elem else int(elem.get(A_ODM.LENGTH.value))

            if A_ODM.SIGNIFICANT_DIGITS.value in elem.keys():
                sd = elem.get(A_ODM.SIGNIFICANT_DIGITS.value)
            else:
                sd = 0

            if A_ODM.DATETIME_FORMAT.value in elem.keys():
                dt_format = elem.get(A_ODM.DATETIME_FORMAT.value)
                for fmt in [('yyyy', '%Y'), ('MMM', '%b'), ('dd', '%d'), ('HH', '%H'), ('nn', '%M'), ('ss', '%S'),
                            ('-', '')]:
                    dt_format = dt_format.replace(fmt[0], fmt[1])

            if codelist is not None:
                return self.scramble_codelist(codelist)

            elif datatype == 'integer':
                return self.scramble_int(length)

            elif datatype == 'float':
                return self.scramble_float(length, sd)

            elif datatype in ['string', 'text']:
                return self.scramble_string(length)

            elif datatype in ['date', 'datetime']:
                return self.scramble_date(value, dt_format)

            elif datatype in ['time']:
                return self.scramble_time(dt_format)

            else:
                return self.scramble_value(value)

        else:
            return self.scramble_value(value)


Instruction:
Write a Python 3 script to
fill in random values for all empty - valued ItemData elements in an ODM document

Response:
def fill_empty(self, fixed_values, input):
        """Fill in random values for all empty-valued ItemData elements in an ODM document"""
        odm_elements = etree.fromstring(input)

        for v in odm_elements.iter(E_ODM.ITEM_DATA.value):
            if v.get(A_ODM.VALUE.value) == "":
                oid = v.get(A_ODM.ITEM_OID.value)

                if fixed_values is not None and oid in fixed_values:
                    d = fixed_values[oid]
                else:
                    d = self.scramble_itemdata(v.get(A_ODM.ITEM_OID.value), v.get(A_ODM.VALUE.value))

                v.set(A_ODM.VALUE.value, d)
            else:
                # Remove ItemData if it already has a value
                v.getparent().remove(v)

        # Remove empty ItemGroupData elements
        for v in odm_elements.iter(E_ODM.ITEM_GROUP_DATA.value):
            if len(v) == 0:
                v.getparent().remove(v)

        # Remove empty FormData elements
        for v in odm_elements.iter(E_ODM.FORM_DATA.value):
            if len(v) == 0:
                v.getparent().remove(v)

        # Remove empty StudyEventData elements
        for v in odm_elements.iter(E_ODM.STUDY_EVENT_DATA.value):
            if len(v) == 0:
                v.getparent().remove(v)

        return etree.tostring(odm_elements)


Instruction:
How would you implement a function in Python 3 that
converts string value to long

Response:
def make_int(value, missing=-1):
    """Convert string value to long, '' to missing"""
    if isinstance(value, six.string_types):
        if not value.strip():
            return missing
    elif value is None:
        return missing
    return int(value)


Instruction:
Implement a function in Python 3 to
parse the XML data firing events from the eventer

Response:
def parse(data, eventer):
    """Parse the XML data, firing events from the eventer"""
    parser = etree.XMLParser(target=ODMTargetParser(eventer))
    return etree.XML(data, parser)


Instruction:
Can you tell what is the following Python 3 function doing
def emit(self):

        self.count += 1

        # event_name = 'on_{0}'.format(self.context.subcategory.lower())
        event_name = self.context.subcategory
        if hasattr(self.handler, event_name):
            getattr(self.handler, event_name)(self.context)
        elif hasattr(self.handler, 'default'):
            self.handler.default(self.context)

Response: Emit the next element of the sequence.


Instruction:
Here you have a function in Python 3, explain what it does
def start(self, tag, attrib):
        if tag == E_CLINICAL_DATA:
            self.ref_state = AUDIT_REF_STATE
            self.context = Context(attrib[A_STUDY_OID],
                                   attrib[A_AUDIT_SUBCATEGORY_NAME],
                                   int(attrib[A_METADATA_VERSION_OID]))

        elif tag == E_SUBJECT_DATA:
            self.context.subject = Subject(
                attrib.get(A_SUBJECT_KEY),
                attrib.get(A_SUBJECT_NAME),
                attrib.get(A_SUBJECT_STATUS),
                attrib.get(A_TRANSACTION_TYPE, DEFAULT_TRANSACTION_TYPE),
            )
        elif tag == E_USER_REF:
            # Set the Signature or audit-record value depending on state
            self.get_parent_element().user_oid = attrib.get(A_USER_OID)

        elif tag == E_SOURCE_ID:
            self.state = STATE_SOURCE_ID

        elif tag == E_DATE_TIME_STAMP_:
            self.state = STATE_DATETIME

        elif tag == E_REASON_FOR_CHANGE:
            self.state = STATE_REASON_FOR_CHANGE

        elif tag == E_LOCATION_REF:
            # Set the Signature or audit-record value depending on state
            self.get_parent_element().location_oid = attrib.get(A_LOCATION_OID)

        elif tag == E_STUDY_EVENT_DATA:
            self.context.event = Event(
                attrib.get(A_STUDYEVENT_OID),
                attrib.get(A_STUDYEVENT_REPEAT_KEY),
                attrib.get(A_TRANSACTION_TYPE),
                attrib.get(A_INSTANCE_NAME),
                attrib.get(A_INSTANCE_OVERDUE),
                make_int(attrib.get(A_INSTANCE_ID), -1)
            )

        elif tag == E_FORM_DATA:
            self.context.form = Form(
                attrib.get(A_FORM_OID),
                int(attrib.get(A_FORM_REPEAT_KEY, 0)),
                attrib.get(A_TRANSACTION_TYPE),
                attrib.get(A_DATAPAGE_NAME),
                make_int(attrib.get(A_DATAPAGE_ID, -1)),
            )

        elif tag == E_ITEM_GROUP_DATA:
            self.context.itemgroup = ItemGroup(
                attrib.get(A_ITEMGROUP_OID),
                int(attrib.get(A_ITEMGROUP_REPEAT_KEY, 0)),
                attrib.get(A_TRANSACTION_TYPE),
                make_int(attrib.get(A_RECORD_ID, -1)),
            )

        elif tag == E_ITEM_DATA:
            self.context.item = Item(
                attrib.get(A_ITEM_OID),
                attrib.get(A_VALUE),
                yes_no_none(attrib.get(A_FREEZE)),
                yes_no_none(attrib.get(A_VERIFY)),
                yes_no_none(attrib.get(A_LOCK)),
                attrib.get(A_TRANSACTION_TYPE)
            )

        elif tag == E_QUERY:
            self.context.query = Query(
                make_int(attrib.get(A_QUERY_REPEAT_KEY, -1)),
                attrib.get(A_STATUS),
                attrib.get(A_RESPONSE),
                attrib.get(A_RECIPIENT),
                attrib.get(A_VALUE)  # Optional, depends on status
            )

        elif tag == E_PROTOCOL_DEVIATION:
            self.context.protocol_deviation = ProtocolDeviation(
                make_int(attrib.get(A_PROTCOL_DEVIATION_REPEAT_KEY, -1)),
                attrib.get(A_CODE),
                attrib.get(A_CLASS),
                attrib.get(A_STATUS),
                attrib.get(A_VALUE),
                attrib.get(A_TRANSACTION_TYPE)
            )

        elif tag == E_REVIEW:
            self.context.review = Review(
                attrib.get(A_GROUP_NAME),
                yes_no_none(attrib.get(A_REVIEWED)),
            )
        elif tag == E_COMMENT:
            self.context.comment = Comment(
                attrib.get(A_COMMENT_REPEAT_KEY),
                attrib.get(A_VALUE),
                attrib.get(A_TRANSACTION_TYPE)

            )
        elif tag == E_SIGNATURE:
            self.ref_state = SIGNATURE_REF_STATE

        elif tag == E_SIGNATURE_REF:
            self.context.signature.oid = attrib.get(A_SIGNATURE_OID)

Response: On start of element tag


Instruction:
How would you code a function in Python 3 to
get the parent element of the current context.

Response:
def get_parent_element(self):
        """Signatures and Audit elements share sub-elements, we need to know which to set attributes on"""
        return {AUDIT_REF_STATE: self.context.audit_record,
                SIGNATURE_REF_STATE: self.context.signature}[self.ref_state]


Instruction:
Explain what the following Python 3 code does
def data(self, data):
        if self.state == STATE_SOURCE_ID:
            self.context.audit_record.source_id = int(data)  # Audit ids can be 64 bits
        elif self.state == STATE_DATETIME:
            dt = datetime.datetime.strptime(data, "%Y-%m-%dT%H:%M:%S")
            self.get_parent_element().datetimestamp = dt
        elif self.state == STATE_REASON_FOR_CHANGE:
            self.context.audit_record.reason_for_change = data.strip() or None  # Convert a result of '' to None.
        self.state = STATE_NONE

Response: Called for text between tags


Instruction:
Explain what the following Python 3 code does
def validate_not_empty():
    def empty_validator(field, data):
        if isinstance(field.value, str) and not field.value.strip():
            raise ValidationError('empty')
    return empty_validator

Response: Validate that a field is not empty.


Instruction:
Can you tell what is the following Python 3 function doing
def validate_length(low=None, high=None, equal=None):
    def length_validator(field, data):
        if field.value is None:
            return
        if equal is not None and len(field.value) != equal:
            raise ValidationError('length_equal', equal=equal)
        if low is not None and len(field.value) < low:
            key = 'length_low' if high is None else 'length_between'
            raise ValidationError(key, low=low, high=high)
        if high is not None and len(field.value) > high:
            key = 'length_high' if low is None else 'length_between'
            raise ValidationError(key, low=low, high=high)
    return length_validator

Response: Validate the length of a node with either low high or equal.


Instruction:
Can you implement a function in Python 3 that
validates that a field is in one of the given values.

Response:
def validate_one_of(values):
    """
    Validate that a field is in one of the given values.

    :param values: Iterable of valid values.
    :raises: ``ValidationError('one_of')``
    """
    def one_of_validator(field, data):
        if field.value is None:
            return
        options = values
        if callable(options):
            options = options()
        if field.value not in options:
            raise ValidationError('one_of', choices=', '.join(map(str, options)))
    return one_of_validator


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate_none_of(values):
    def none_of_validator(field, data):
        options = values
        if callable(options):
            options = options()
        if field.value in options:
            raise ValidationError('none_of', choices=str.join(', ', options))
    return none_of_validator

Response: Validate that a field is not in one of the given values.


Instruction:
Given the following Python 3 function, write the documentation
def validate_range(low=None, high=None):
    def range_validator(field, data):
        if field.value is None:
            return
        if low is not None and field.value < low:
            key = 'range_low' if high is None else 'range_between'
            raise ValidationError(key, low=low, high=high)
        if high is not None and field.value > high:
            key = 'range_high' if high is None else 'range_between'
            raise ValidationError(key, low=low, high=high)
    return range_validator

Response: Validate that a field with either low or high or equal is present.


Instruction:
Write a Python 3 function for
validating the field value is equal to the given value.

Response:
def validate_equal(value):
    """
    Validate the field value is equal to the given value.
    Should work with anything that supports '==' operator.

    :param value: Value to compare.
    :raises: ``ValidationError('equal')``
    """
    def equal_validator(field, data):
        if field.value is None:
            return
        if not (field.value == value):
            raise ValidationError('equal', other=value)
    return equal_validator


Instruction:
How would you code a function in Python 3 to
validate that the value of a field in the data contains the value of another field in the data.

Response:
def validate_matches(other):
    """
    Validate the field value is equal to another field in the data.
    Should work with anything that supports '==' operator.

    :param value: Field key to compare.
    :raises: ``ValidationError('matches')``
    """
    def matches_validator(field, data):
        if field.value is None:
            return
        if not (field.value == data.get(other)):
            raise ValidationError('matches', other=other)
    return matches_validator


Instruction:
Given the following Python 3 function, write the documentation
def validate_regexp(pattern, flags=0):
    regex = re.compile(pattern, flags) if isinstance(pattern, str) else pattern

    def regexp_validator(field, data):
        if field.value is None:
            return
        if regex.match(str(field.value)) is None:
            raise ValidationError('regexp', pattern=pattern)
    return regexp_validator

Response: Validate that the field value matches the given regular expression.


Instruction:
Write a Python 3 script for
validating the field value matches the result of calling the given method.

Response:
def validate_function(method, **kwargs):
    """
    Validate the field matches the result of calling the given method. Example::

        def myfunc(value, name):
            return value == name

        validator = validate_function(myfunc, name='tim')

    Essentially creates a validator that only accepts the name 'tim'.

    :param method: Method to call.
    :param kwargs: Additional keyword arguments passed to the method.
    :raises: ``ValidationError('function')``
    """
    def function_validator(field, data):
        if field.value is None:
            return
        if not method(field.value, **kwargs):
            raise ValidationError('function', function=method.__name__)
    return function_validator


Instruction:
Can you write a function in Python 3 where it
validates the field is a valid email address.

Response:
def validate_email():
    """
    Validate the field is a valid email address.

    :raises: ``ValidationError('email')``
    """
    user_regex = re.compile(
        r"(^[-!#$%&'*+/=?^`{}|~\w]+(\.[-!#$%&'*+/=?^`{}|~\w]+)*$"
        r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]'
        r'|\\[\001-\011\013\014\016-\177])*"$)', re.IGNORECASE | re.UNICODE)

    domain_regex = re.compile(
        r'(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+'
        r'(?:[A-Z]{2,6}|[A-Z0-9-]{2,})$'
        r'|^\[(25[0-5]|2[0-4]\d|[0-1]?\d?\d)'
        r'(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\]$', re.IGNORECASE | re.UNICODE)

    domain_whitelist = ('localhost',)

    def email_validator(field, data):
        if field.value is None:
            return

        value = str(field.value)

        if '@' not in value:
            raise ValidationError('email')

        user_part, domain_part = value.rsplit('@', 1)

        if not user_regex.match(user_part):
            raise ValidationError('email')

        if domain_part in domain_whitelist:
            return

        if not domain_regex.match(domain_part):
            raise ValidationError('email')

    return email_validator


Instruction:
Write a Python 3 script for
validating the field is a unique given a queryset and lookup_field.

Response:
def validate_model_unique(lookup_field, queryset, pk_field=None, pk_value=None):
    """
    Validate the field is a unique, given a queryset and lookup_field. Example::

        validator = validate_model_unique(User.email, User.select())

    Creates a validator that can validate the uniqueness of an email address.

    :param lookup_field: Peewee model field that should be used for checking existing values.
    :param queryset: Queryset to use for lookup.
    :param pk_field: Field instance to use when excluding existing instance.
    :param pk_value: Field value to use when excluding existing instance.
    :raises: ``ValidationError('unique')``
    """
    def unique_validator(field, data):
        # If we have a PK, ignore it because it represents the current record.
        query = queryset.where(lookup_field == field.value)
        if pk_field and pk_value:
            query = query.where(~(pk_field == pk_value))
        if query.count():
            raise ValidationError('unique')
    return unique_validator


Instruction:
Create a Python 3 function to
coerce a value to a scalar value for lookup_field.

Response:
def coerce_single_instance(lookup_field, value):
    """
    Convert from whatever value is given to a scalar value for lookup_field.
    If value is a dict, then lookup_field.name is used to get the value from the dict. Example:
        lookup_field.name = 'id'
        value = {'id': 123, 'name': 'tim'}
        returns = 123
    If value is a model, then lookup_field.name is extracted from the model. Example:
        lookup_field.name = 'id'
        value = <User id=123 name='tim'>
        returns = 123
    Otherwise the value is returned as-is.

    :param lookup_field: Peewee model field used for getting name from value.
    :param value: Some kind of value (usually a dict, Model instance, or scalar).
    """
    if isinstance(value, dict):
        return value.get(lookup_field.name)
    if isinstance(value, peewee.Model):
        return getattr(value, lookup_field.name)
    return value


Instruction:
Implement a function in Python 3 to
return True if the value is iterable but not a string. Otherwise returns False.

Response:
def isiterable_notstring(value):
    """
    Returns True if the value is iterable but not a string. Otherwise returns False.

    :param value: Value to check.
    """
    if isinstance(value, str):
        return False
    return isinstance(value, Iterable) or isgeneratorfunction(value) or isgenerator(value)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_value(self, name, data):
        if name in data:
            return data.get(name)
        if self.default:
            if callable(self.default):
                return self.default()
            return self.default
        return None

Response: Get the value of this field from the data.


Instruction:
Can you generate the documentation for the following Python 3 function
def validate(self, name, data):
        self.value = self.get_value(name, data)
        if self.value is not None:
            self.value = self.coerce(self.value)
        for method in self.validators:
            method(self, data)

Response: Check to make sure the data for this field is valid.


Instruction:
Given the following Python 3 function, write the documentation
def validate(self, name, data):
        super().validate(name, data)
        if self.value is not None:
            try:
                self.value = self.query.get(self.lookup_field == self.value)
            except (AttributeError, ValueError, peewee.DoesNotExist):
                raise ValidationError('related', field=self.lookup_field.name, values=self.value)

Response: Validate that the related field is set.


Instruction:
Write a Python 3 function that can
convert from whatever is given to a list of scalars for the lookup_field.

Response:
def coerce(self, value):
        """Convert from whatever is given to a list of scalars for the lookup_field."""
        if isinstance(value, dict):
            value = [value]
        if not isiterable_notstring(value):
            value = [value]
        return [coerce_single_instance(self.lookup_field, v) for v in value]


Instruction:
Can you generate the documentation for the following Python 3 function
def initialize_fields(self):
        for field in dir(self):
            obj = getattr(self, field)
            if isinstance(obj, Field):
                self._meta.fields[field] = obj

Response: Initialize the base_fields dictionary.


Instruction:
Can you create a Python 3 function that
validates the data for all fields and return whether the validation was successful.

Response:
def validate(self, data=None, only=None, exclude=None):
        """
        Validate the data for all fields and return whether the validation was successful.
        This method also retains the validated data in ``self.data`` so that it can be accessed later.

        This is usually the method you want to call after creating the validator instance.

        :param data: Dictionary of data to validate.
        :param only: List or tuple of fields to validate.
        :param exclude: List or tuple of fields to exclude from validation.
        :return: True if validation was successful. Otherwise False.
        """
        only = only or []
        exclude = exclude or []
        data = data or {}
        self.errors = {}
        self.data = {}

        # Validate individual fields.
        for name, field in self._meta.fields.items():
            if name in exclude or (only and name not in only):
                continue
            try:
                field.validate(name, data)
            except ValidationError as err:
                self.add_error(name, err)
                continue
            self.data[name] = field.value

        # Clean individual fields.
        if not self.errors:
            self.clean_fields(self.data)

        # Then finally clean the whole data dict.
        if not self.errors:
            try:
                self.data = self.clean(self.data)
            except ValidationError as err:
                self.add_error('__base__', err)

        return (not self.errors)


Instruction:
How would you implement a function in Python 3 that
cleans the data of the record sets.

Response:
def clean_fields(self, data):
        """
        For each field, check to see if there is a clean_<name> method.
        If so, run that method and set the returned value on the self.data dict.
        This happens after all validations so that each field can act on the
        cleaned data of other fields if needed.

        :param data: Dictionary of data to clean.
        :return: None
        """
        for name, value in data.items():
            try:
                method = getattr(self, 'clean_{}'.format(name), None)
                if method:
                    self.data[name] = method(value)
            except ValidationError as err:
                self.add_error(name, err)
                continue


Instruction:
Create a Python 3 function for
converting all model fields to validator fields.

Response:
def initialize_fields(self):
        """
        Convert all model fields to validator fields.
        Then call the parent so that overwrites can happen if necessary for manually defined fields.

        :return: None
        """
        # # Pull all the "normal" fields off the model instance meta.
        for name, field in self.instance._meta.fields.items():
            if getattr(field, 'primary_key', False):
                continue
            self._meta.fields[name] = self.convert_field(name, field)

        # Many-to-many fields are not stored in the meta fields dict.
        # Pull them directly off the class.
        for name in dir(type(self.instance)):
            field = getattr(type(self.instance), name, None)
            if isinstance(field, ManyToManyField):
                self._meta.fields[name] = self.convert_field(name, field)

        super().initialize_fields()


Instruction:
How would you implement a function in Python 3 that
converts a single field from a Peewee model field to a validator field.

Response:
def convert_field(self, name, field):
        """
        Convert a single field from a Peewee model field to a validator field.

        :param name: Name of the field as defined on this validator.
        :param name: Peewee field instance.
        :return: Validator field.
        """
        if PEEWEE3:
            field_type = field.field_type.lower()
        else:
            field_type = field.db_field

        pwv_field = ModelValidator.FIELD_MAP.get(field_type, StringField)

        print('pwv_field', field_type, pwv_field)

        validators = []
        required = not bool(getattr(field, 'null', True))
        choices = getattr(field, 'choices', ())
        default = getattr(field, 'default', None)
        max_length = getattr(field, 'max_length', None)
        unique = getattr(field, 'unique', False)

        if required:
            validators.append(validate_required())

        if choices:
            print('CHOICES', choices)
            validators.append(validate_one_of([c[0] for c in choices]))

        if max_length:
            validators.append(validate_length(high=max_length))

        if unique:
            validators.append(validate_model_unique(field, self.instance.select(), self.pk_field, self.pk_value))

        if isinstance(field, peewee.ForeignKeyField):
            if PEEWEE3:
                rel_field = field.rel_field
            else:
                rel_field = field.to_field
            return ModelChoiceField(field.rel_model, rel_field, default=default, validators=validators)

        if isinstance(field, ManyToManyField):
            return ManyModelChoiceField(
                field.rel_model, field.rel_model._meta.primary_key,
                default=default, validators=validators)

        return pwv_field(default=default, validators=validators)


Instruction:
Given the following Python 3 function, write the documentation
def validate(self, data=None, only=None, exclude=None):
        data = data or {}
        only = only or self._meta.only
        exclude = exclude or self._meta.exclude

        for name, field in self.instance._meta.fields.items():
            if name in exclude or (only and name not in only):
                continue
            try:
                data.setdefault(name, getattr(self.instance, name, None))
            except (peewee.DoesNotExist):
                if PEEWEE3:
                    instance_data = self.instance.__data__
                else:
                    instance_data = self.instance._data
                data.setdefault(name, instance_data.get(name, None))

        # This will set self.data which we should use from now on.
        super().validate(data=data, only=only, exclude=exclude)

        if not self.errors:
            self.perform_index_validation(self.data)

        return (not self.errors)

Response: Validate the data for all fields and return whether the validation was successful.


Instruction:
Implement a function in Python 3 to
validate any unique indexes specified on the model.

Response:
def perform_index_validation(self, data):
        """
        Validate any unique indexes specified on the model.
        This should happen after all the normal fields have been validated.
        This can add error messages to multiple fields.

        :return: None
        """
        # Build a list of dict containing query values for each unique index.
        index_data = []
        for columns, unique in self.instance._meta.indexes:
            if not unique:
                continue
            index_data.append({col: data.get(col, None) for col in columns})

        # Then query for each unique index to see if the value is unique.
        for index in index_data:
            query = self.instance.filter(**index)
            # If we have a primary key, need to exclude the current record from the check.
            if self.pk_field and self.pk_value:
                query = query.where(~(self.pk_field == self.pk_value))
            if query.count():
                err = ValidationError('index', fields=str.join(', ', index.keys()))
                for col in index.keys():
                    self.add_error(col, err)


Instruction:
Make a summary of the following Python 3 code
def save(self, force_insert=False):
        delayed = {}
        for field, value in self.data.items():
            model_field = getattr(type(self.instance), field, None)

            # If this is a many-to-many field, we cannot save it to the instance until the instance
            # is saved to the database. Collect these fields and delay the setting until after
            # the model instance is saved.
            if isinstance(model_field, ManyToManyField):
                if value is not None:
                    delayed[field] = value
                continue

            setattr(self.instance, field, value)

        rv = self.instance.save(force_insert=force_insert)

        for field, value in delayed.items():
            setattr(self.instance, field, value)

        return rv

Response: Save the model and any related many - to - many fields.


Instruction:
Here you have a function in Python 3, explain what it does
def long_to_bytes(N, blocksize=1):

    bytestring = hex(N)
    bytestring = bytestring[2:] if bytestring.startswith('0x') else bytestring
    bytestring = bytestring[:-1] if bytestring.endswith('L') else bytestring
    bytestring = '0' + bytestring if (len(bytestring) % 2) != 0 else bytestring
    bytestring = binascii.unhexlify(bytestring)

    if blocksize > 0 and len(bytestring) % blocksize != 0:
        bytestring = '\x00' * \
            (blocksize - (len(bytestring) % blocksize)) + bytestring

    return bytestring

Response: Given an input integer N returns the representation of N in bytes.


Instruction:
Can you implement a function in Python 3 that
gives a string plaintext returns a ciphertext encrypted with an authenticated - encryption scheme using the keys specified in self. K1 and self. K2.

Response:
def encrypt(self, plaintext, iv_bytes=None):
        """Given ``plaintext``, returns a ``ciphertext`` encrypted with an authenticated-encryption scheme, using the keys specified in ``__init__``.
        Ciphertext expansion is deterministic, the output ciphertext is always 42 bytes longer than the input ``plaintext``.
        The input ``plaintext`` can be ``''``.

        Raises ``PlaintextTypeError`` if input plaintext is not a string.
        """

        if not isinstance(plaintext, str):
            raise PlaintextTypeError("Input plaintext is not of type string")

        if iv_bytes is None:
            iv_bytes = fte.bit_ops.random_bytes(Encrypter._IV_LENGTH)

        iv1_bytes = '\x01' + iv_bytes
        iv2_bytes = '\x02' + iv_bytes

        W1 = iv1_bytes
        W1 += fte.bit_ops.long_to_bytes(
            len(plaintext), Encrypter._MSG_COUNTER_LENGTH)
        W1 = self._ecb_enc_K1.encrypt(W1)

        counter_length_in_bits = AES.block_size * 8
        counter_val = fte.bit_ops.bytes_to_long(iv2_bytes)
        counter = Counter.new(
            counter_length_in_bits, initial_value=counter_val)
        ctr_enc = AES.new(key=self.K1,
                          mode=AES.MODE_CTR,
                          IV='\x00' * 8 + iv2_bytes,
                          counter=counter)
        W2 = ctr_enc.encrypt(plaintext)

        mac = HMAC.new(self.K2, W1 + W2, SHA512)
        T = mac.digest()
        T = T[:Encrypter._MAC_LENGTH]

        ciphertext = W1 + W2 + T

        return ciphertext