Instruction:
How would you implement a function in Python 3 that
canvases GET method on a full url. Return representation of the requested resource.

Response:
def _get_resource_url(self, url, auto_page, data_key):
        """
        Canvas GET method on a full url. Return representation of the
        requested resource, chasing pagination links to coalesce resources
        if indicated.
        """
        headers = {'Accept': 'application/json',
                   'Connection': 'keep-alive'}
        response = DAO.getURL(url, headers)

        if response.status != 200:
            raise DataFailureException(url, response.status, response.data)

        data = json.loads(response.data)

        self.next_page_url = self._next_page(response)
        if auto_page and self.next_page_url:
            if isinstance(data, list):
                data.extend(self._get_resource_url(self.next_page_url, True,
                                                   data_key))
            elif isinstance(data, dict) and data_key is not None:
                data[data_key].extend(self._get_resource_url(
                    self.next_page_url, True, data_key)[data_key])

        return data


Instruction:
Given the following Python 3 function, write the documentation
def _get_paged_resource(self, url, params=None, data_key=None):
        if not params:
            params = {}

        self._set_as_user(params)

        auto_page = not ('page' in params or 'per_page' in params)

        if 'per_page' not in params and self._per_page != DEFAULT_PAGINATION:
            params["per_page"] = self._per_page

        full_url = url + self._params(params)
        return self._get_resource_url(full_url, auto_page, data_key)

Response: Canvas GET method. Return representation of the requested resource either the requested page or the pagination links to the requested page.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _get_resource(self, url, params=None, data_key=None):
        if not params:
            params = {}

        self._set_as_user(params)

        full_url = url + self._params(params)

        return self._get_resource_url(full_url, True, data_key)

Response: Canvas GET method. Return representation of the requested resource.


Instruction:
Can you tell what is the following Python 3 function doing
def get_admins(self, account_id, params={}):
        url = ADMINS_API.format(account_id)

        admins = []
        for data in self._get_paged_resource(url, params=params):
            admins.append(CanvasAdmin(data=data))
        return admins

Response: Get a list of the admins in the account.


Instruction:
How would you explain what the following Python 3 function does
def create_admin(self, account_id, user_id, role):
        url = ADMINS_API.format(account_id)
        body = {"user_id": unquote(str(user_id)),
                "role": role,
                "send_confirmation": False}

        return CanvasAdmin(data=self._post_resource(url, body))

Response: Flag an existing user as an admin within the account.


Instruction:
Can you write a function in Python 3 where it
flags an existing user as an admin within the account sis id.

Response:
def create_admin_by_sis_id(self, sis_account_id, user_id, role):
        """
        Flag an existing user as an admin within the account sis id.
        """
        return self.create_admin(self._sis_id(sis_account_id), user_id, role)


Instruction:
Can you implement a function in Python 3 that
removes an account admin role from a user.

Response:
def delete_admin(self, account_id, user_id, role):
        """
        Remove an account admin role from a user.

        https://canvas.instructure.com/doc/api/admins.html#method.admins.destroy
        """
        url = ADMINS_API.format(account_id) + "/{}?role={}".format(
            user_id, quote(role))

        response = self._delete_resource(url)
        return True


Instruction:
Make a summary of the following Python 3 code
def delete_admin_by_sis_id(self, sis_account_id, user_id, role):
        return self.delete_admin(self._sis_id(sis_account_id), user_id, role)

Response: Delete an account admin role from a user


Instruction:
Explain what the following Python 3 code does
def get_grading_standards_for_course(self, course_id):
        url = COURSES_API.format(course_id) + "/grading_standards"

        standards = []
        for data in self._get_resource(url):
            standards.append(GradingStandard(data=data))
        return standards

Response: Get the grading standards available to a course


Instruction:
Write a Python 3 script to
create a new grading standard for the passed course.

Response:
def create_grading_standard_for_course(self, course_id, name,
                                           grading_scheme, creator):
        """
        Create a new grading standard for the passed course.

        https://canvas.instructure.com/doc/api/grading_standards.html#method.grading_standards_api.create
        """
        url = COURSES_API.format(course_id) + "/grading_standards"
        body = {
            "title": name,
            "grading_scheme_entry": grading_scheme,
            "as_user_id": creator
        }

        return GradingStandard(data=self._post_resource(url, body))


Instruction:
How would you implement a function in Python 3 that
gets a section resource for given canvas section id.

Response:
def get_section(self, section_id, params={}):
        """
        Return section resource for given canvas section id.

        https://canvas.instructure.com/doc/api/sections.html#method.sections.show
        """
        url = SECTIONS_API.format(section_id)
        return CanvasSection(data=self._get_resource(url, params=params))


Instruction:
Explain what the following Python 3 code does
def get_section_by_sis_id(self, sis_section_id, params={}):
        return self.get_section(
            self._sis_id(sis_section_id, sis_field="section"), params)

Response: Get a section resource by its sis id.


Instruction:
Here you have a function in Python 3, explain what it does
def get_sections_in_course(self, course_id, params={}):
        url = COURSES_API.format(course_id) + "/sections"

        sections = []
        for data in self._get_paged_resource(url, params=params):
            sections.append(CanvasSection(data=data))

        return sections

Response: Get list of sections in the passed course.


Instruction:
Implement a Python 3 function for
getting a list of sections in the course given the SIS ID.

Response:
def get_sections_in_course_by_sis_id(self, sis_course_id, params={}):
        """
        Return list of sections for the passed course SIS ID.
        """
        return self.get_sections_in_course(
            self._sis_id(sis_course_id, sis_field="course"), params)


Instruction:
Make a summary of the following Python 3 code
def get_sections_with_students_in_course(self, course_id, params={}):
        include = params.get("include", [])
        if "students" not in include:
            include.append("students")
        params["include"] = include

        return self.get_sections_in_course(course_id, params)

Response: Returns a list of all sections in the passed course.


Instruction:
How would you code a function in Python 3 to
return a list of sections including students in the course.

Response:
def get_sections_with_students_in_course_by_sis_id(self, sis_course_id,
                                                       params={}):
        """
        Return list of sections including students for the passed sis ID.
        """
        return self.get_sections_with_students_in_course(
            self._sis_id(sis_course_id, sis_field="course"), params)


Instruction:
Given the following Python 3 function, write the documentation
def create_section(self, course_id, name, sis_section_id):
        url = COURSES_API.format(course_id) + "/sections"
        body = {"course_section": {"name": name,
                                   "sis_section_id": sis_section_id}}

        return CanvasSection(data=self._post_resource(url, body))

Response: Create a canvas section in the given course id.


Instruction:
Can you implement a function in Python 3 that
updates a canvas section with the given section id.

Response:
def update_section(self, section_id, name, sis_section_id):
        """
        Update a canvas section with the given section id.

        https://canvas.instructure.com/doc/api/sections.html#method.sections.update
        """
        url = SECTIONS_API.format(section_id)
        body = {"course_section": {}}

        if name:
            body["course_section"]["name"] = name

        if sis_section_id:
            body["course_section"]["sis_section_id"] = sis_section_id

        return CanvasSection(data=self._put_resource(url, body))


Instruction:
Can you implement a function in Python 3 that
gets a list of quizzes for a given course.

Response:
def get_quizzes(self, course_id):
        """
        List quizzes for a given course

        https://canvas.instructure.com/doc/api/quizzes.html#method.quizzes_api.index
        """
        url = QUIZZES_API.format(course_id)
        data = self._get_resource(url)
        quizzes = []
        for datum in data:
            quizzes.append(Quiz(data=datum))
        return quizzes


Instruction:
How would you code a function in Python 3 to
return account resource for given canvas account id.

Response:
def get_account(self, account_id):
        """
        Return account resource for given canvas account id.

        https://canvas.instructure.com/doc/api/accounts.html#method.accounts.show
        """
        url = ACCOUNTS_API.format(account_id)
        return CanvasAccount(data=self._get_resource(url))


Instruction:
Create a Python 3 function for
getting list of subaccounts within the account with the passed account_id canvas id.

Response:
def get_sub_accounts(self, account_id, params={}):
        """
        Return list of subaccounts within the account with the passed
        canvas id.

        https://canvas.instructure.com/doc/api/accounts.html#method.accounts.sub_accounts
        """
        url = ACCOUNTS_API.format(account_id) + "/sub_accounts"

        accounts = []
        for datum in self._get_paged_resource(url, params=params):
            accounts.append(CanvasAccount(data=datum))

        return accounts


Instruction:
Given the following Python 3 function, write the documentation
def update_account(self, account):
        url = ACCOUNTS_API.format(account.account_id)
        body = {"account": {"name": account.name}}

        return CanvasAccount(data=self._put_resource(url, body))

Response: Updates the passed account. Returns the updated account.


Instruction:
Can you create a Python 3 function that
updates the SIS ID for the account identified by the passed account ID.

Response:
def update_sis_id(self, account_id, sis_account_id):
        """
        Updates the SIS ID for the account identified by the passed account ID.

        https://canvas.instructure.com/doc/api/accounts.html#method.accounts.update
        """
        if account_id == self._canvas_account_id:
            raise Exception("SIS ID cannot be updated for the root account")

        url = ACCOUNTS_API.format(account_id)
        body = {"account": {"sis_account_id": sis_account_id}}

        return CanvasAccount(data=self._put_resource(url, body))


Instruction:
Explain what the following Python 3 code does
def get_auth_settings(self, account_id):
        url = ACCOUNTS_API.format(account_id) + "/sso_settings"
        return CanvasSSOSettings(data=self._get_resource(url))

Response: Returns the authentication settings for the passed account_id.


Instruction:
Implement a function in Python 3 to
update the authentication settings for the passed account_id.

Response:
def update_auth_settings(self, account_id, auth_settings):
        """
        Update the authentication settings for the passed account_id.

        https://canvas.instructure.com/doc/api/authentication_providers.html#method.account_authorization_configs.update_sso_settings
        """
        url = ACCOUNTS_API.format(account_id) + "/sso_settings"
        data = self._put_resource(url, auth_settings.json_data())
        return CanvasSSOSettings(data=data)


Instruction:
How would you implement a function in Python 3 that
calculates the settlement of a shallow foundation.

Response:
def settlement_schmertmann(sp, fd, load, youngs_modulus_soil, **kwargs):
    """
    Calculates the settlement of a shallow foundation (Schmertmann, 19XX).

    :param sp: Soil Profile object
    :param fd: Foundation object
    :param load:
    :param youngs_modulus_soil: The Young's modulus of the soil.
    :param kwargs:
    :return: float, the settlement.
    """
    length = float(fd.length)
    breadth = float(fd.width)
    depth = float(fd.depth)
    load = float(load)

    sp.gwl = kwargs.get("gwl", sp.gwl)
    sp.unit_sat_weight = kwargs.get("unit_sat_weight", sp.unit_sat_weight)
    verbose = kwargs.get("verbose", 0)
    years = kwargs.get("years", 0)

    q = load / (length * breadth)
    sigma_v0_eff = (sp.unit_dry_weight * min(depth, sp.gwl) +
                    (sp.unit_sat_weight - 9.8) * max([0, depth - sp.gwl]))
    delta_q = q - sigma_v0_eff

    # EMBEDMENT FACTOR
    c_1 = max(1 - 0.5 * (sigma_v0_eff / delta_q), 0.5)
    # CREEP FACTOR
    if years == 0:
        c_2 = 1.0
    else:
        c_2 = 1.0 + 0.2 * np.log10(years / 0.1)
    # SHAPE FACTOR
    long = max(length, breadth)
    short = min(length, breadth)
    c_3 = max(1.03 - 0.03 * (long / short), 0.73)
    # Peak settlement index
    if long / short > 10:
        zp = short + depth
        z_top = 0.2
        z_bottom = 4 * short + depth
    else:
        z_top = 0.1
        zp = 0.5 * short + depth
        z_bottom = 2 * short + depth

    sigma_vp_eff = (sp.unit_dry_weight * min(zp, sp.gwl) +
                    (sp.unit_sat_weight - 9.8) * max([0, zp - sp.gwl]))
    i_zp = 0.5 + 0.1 * (delta_q / sigma_vp_eff) ** 0.5

    i_z_top = (i_zp + z_top) / 2
    i_z_bottom = i_zp / 2

    settlement = (c_1 * c_2 * c_3 * delta_q *
                  (i_z_top * (zp - depth) + i_z_bottom * (z_bottom - zp)) / youngs_modulus_soil)

    if verbose:
        log("delta_q:", delta_q)
        log("c_1:", c_1)
        log("c_2:", c_2)
        log("c_3:", c_3)
        log("zp:", zp)
        log("sigma_vp_eff:", sigma_vp_eff)
        log("i_zp:", i_zp)
        log("i_z_top:", i_z_top)
        log("i_z_bottom:", i_z_bottom)
        log("settlement:", settlement)
    return settlement


Instruction:
Given the following Python 3 function, write the documentation
def get_all_terms(self):
        if not self._canvas_account_id:
            raise MissingAccountID()

        params = {"workflow_state": 'all', 'per_page': 500}
        url = ACCOUNTS_API.format(self._canvas_account_id) + "/terms"
        data_key = 'enrollment_terms'

        terms = []
        response = self._get_paged_resource(url, params, data_key)
        for data in response[data_key]:
            terms.append(CanvasTerm(data=data))
        return terms

Response: Get all of the terms in the account.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_term_by_sis_id(self, sis_term_id):
        for term in self.get_all_terms():
            if term.sis_term_id == sis_term_id:
                return term

Response: Return a term resource for the passed SIS ID.


Instruction:
Write a Python 3 script for
updating an existing enrollment term with the passed overrides.

Response:
def update_term_overrides(self, sis_term_id, overrides={}):
        """
        Update an existing enrollment term for the passed SIS ID.
        https://canvas.instructure.com/doc/api/enrollment_terms.html#method.terms.update
        """
        if not self._canvas_account_id:
            raise MissingAccountID()

        url = ACCOUNTS_API.format(
            self._canvas_account_id) + "/terms/{}".format(
                self._sis_id(sis_term_id, sis_field='term'))

        body = {'enrollment_term': {'overrides': overrides}}
        return CanvasTerm(data=self._put_resource(url, body))


Instruction:
How would you implement a function in Python 3 that
produces console output. :param out_str: Output string :param o2: Additional output string :param o3: Additional output string :param o4: Additional output string :return: None

Response:
def log(out_str, o2="", o3="", o4=""):
    """
    Produces console output.
    :param out_str: Output string
    :param o2: Additional output string
    :param o3: Additional output string
    :param o4: Additional output string
    :return: None
    """
    print(out_str, o2, o3, o4)


Instruction:
Write a Python 3 script for
importing a CSV string.

Response:
def import_str(self, csv, params={}):
        """
        Imports a CSV string.

        https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.create
        """
        if not self._canvas_account_id:
            raise MissingAccountID()

        params["import_type"] = SISImportModel.CSV_IMPORT_TYPE
        url = SIS_IMPORTS_API.format(
            self._canvas_account_id) + ".json{}".format(self._params(params))
        headers = {"Content-Type": "text/csv"}

        return SISImportModel(data=self._post_resource(url, headers, csv))


Instruction:
Can you write a function in Python 3 where it
imports a directory of CSV files.

Response:
def import_dir(self, dir_path, params={}):
        """
        Imports a directory of CSV files.

        https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.create
        """
        if not self._canvas_account_id:
            raise MissingAccountID()

        body = self._build_archive(dir_path)
        params["import_type"] = SISImportModel.CSV_IMPORT_TYPE
        url = SIS_IMPORTS_API.format(
            self._canvas_account_id) + ".json{}".format(self._params(params))
        headers = {"Content-Type": "application/zip"}

        return SISImportModel(data=self._post_resource(url, headers, body))


Instruction:
Create a Python 3 function to
get the status of an already created SIS import.

Response:
def get_import_status(self, sis_import):
        """
        Get the status of an already created SIS import.

        https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.show
        """
        if not self._canvas_account_id:
            raise MissingAccountID()

        url = SIS_IMPORTS_API.format(
            self._canvas_account_id) + "/{}.json".format(sis_import.import_id)

        return SISImportModel(data=self._get_resource(url))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _build_archive(self, dir_path):
        zip_path = os.path.join(dir_path, "import.zip")
        archive = zipfile.ZipFile(zip_path, "w")

        for filename in CSV_FILES:
            filepath = os.path.join(dir_path, filename)

            if os.path.exists(filepath):
                archive.write(filepath, filename, zipfile.ZIP_DEFLATED)

        archive.close()

        with open(zip_path, "rb") as f:
            body = f.read()

        return body

Response: Builds a zip archive from the files in path.


Instruction:
Can you tell what is the following Python 3 function doing
def get_assignments(self, course_id):
        url = ASSIGNMENTS_API.format(course_id)
        data = self._get_resource(url)
        assignments = []
        for datum in data:
            assignments.append(Assignment(data=datum))
        return assignments

Response: Get a list of assignments for a given course.


Instruction:
Implement a Python 3 function for
modifying an existing assignment.

Response:
def update_assignment(self, assignment):
        """
        Modify an existing assignment.

        https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.update
        """
        url = ASSIGNMENTS_API.format(assignment.course_id) + "/{}".format(
            assignment.assignment_id)

        data = self._put_resource(url, assignment.json_data())
        return Assignment(data=data)


Instruction:
Implement a Python 3 function for
returning the list of available reports for the canvas account id.

Response:
def get_available_reports(self, account_id):
        """
        Returns the list of reports for the canvas account id.

        https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.available_reports
        """
        url = ACCOUNTS_API.format(account_id) + "/reports"

        report_types = []
        for datum in self._get_resource(url):
            report_types.append(ReportType(data=datum, account_id=account_id))
        return report_types


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_reports_by_type(self, account_id, report_type):
        url = ACCOUNTS_API.format(account_id) + "/reports/{}".format(
            report_type)

        reports = []
        for datum in self._get_resource(url):
            datum["account_id"] = account_id
            reports.append(Report(data=datum))

        return reports

Response: Returns a list of reports of the passed type for the passed account id.


Instruction:
Write a Python 3 script for
creating a new report instance for the given account id and term id.

Response:
def create_report(self, report_type, account_id, term_id=None, params={}):
        """
        Generates a report instance for the canvas account id.

        https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.create
        """
        if term_id is not None:
            params["enrollment_term_id"] = term_id

        url = ACCOUNTS_API.format(account_id) + "/reports/{}".format(
            report_type)
        body = {"parameters": params}

        data = self._post_resource(url, body)
        data["account_id"] = account_id
        return Report(data=data)


Instruction:
Can you tell what is the following Python 3 function doing
def create_course_provisioning_report(self, account_id, term_id=None,
                                          params={}):
        """
        Convenience method for create_report, for creating a course
        provisioning report.
        """
        params["courses"] = True
        return self.create_report(ReportType.PROVISIONING, account_id, term_id,
                                  params)

Response: Creates a course provisioning report.


Instruction:
Can you generate the documentation for the following Python 3 function
def create_course_sis_export_report(self, account_id, term_id=None,
                                        params={}):
        """
        Convenience method for create_report, for creating a course sis export
        report.
        """
        params["courses"] = True
        return self.create_report(ReportType.SIS_EXPORT, account_id, term_id,
                                  params)

Response: Convenience method for creating a course sis export report.


Instruction:
Can you implement a function in Python 3 that
creates an unused courses report.

Response:
def create_unused_courses_report(self, account_id, term_id=None):
        """
        Convenience method for create_report, for creating an unused courses
        report.
        """
        return self.create_report(ReportType.UNUSED_COURSES, account_id,
                                  term_id)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_report_data(self, report):
        if report.report_id is None or report.status is None:
            raise ReportFailureException(report)

        interval = getattr(settings, 'CANVAS_REPORT_POLLING_INTERVAL', 5)
        while report.status != "complete":
            if report.status == "error":
                raise ReportFailureException(report)
            sleep(interval)
            report = self.get_report_status(report)

        if report.attachment is None or report.attachment.url is None:
            return

        data = self._get_report_file(report.attachment.url)

        return data.split("\n")

Response: Returns a completed report as a list of csv strings.


Instruction:
Implement a Python 3 function for
returning the status of a report.

Response:
def get_report_status(self, report):
        """
        Returns the status of a report.

        https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.show
        """
        if (report.account_id is None or report.type is None or
                report.report_id is None):
            raise ReportFailureException(report)

        url = ACCOUNTS_API.format(report.account_id) + "/reports/{}/{}".format(
            report.type, report.report_id)

        data = self._get_resource(url)
        data["account_id"] = report.account_id
        return Report(data=data)


Instruction:
Make a summary of the following Python 3 code
def delete_report(self, report):
        url = ACCOUNTS_API.format(report.account_id) + "/reports/{}/{}".format(
            report.type, report.report_id)

        response = self._delete_resource(url)
        return True

Response: Deletes a generated report instance.


Instruction:
Can you write a function in Python 3 where it
crops an image given the top left corner.

Response:
def crop_image(img, start_y, start_x, h, w):
    """
    Crop an image given the top left corner.
    :param img: The image
    :param start_y: The top left corner y coord
    :param start_x: The top left corner x coord
    :param h: The result height
    :param w: The result width
    :return: The cropped image.
    """
    return img[start_y:start_y + h, start_x:start_x + w, :].copy()


Instruction:
Explain what the following Python 3 code does
def move_detections(label, dy, dx):
    for k in label.keys():
        if k.startswith("detection"):
            detections = label[k]
            for detection in detections:
                detection.move_image(-dx, -dy)

Response: Moves all detections in direction dx dy.


Instruction:
Can you tell what is the following Python 3 function doing
def augment_detections(hyper_params, feature, label):
    # Do not augment these ways:
    # 1) Rotation is not possible
    # 3) Scaling is not possible, because it ruins depth perception
    # However, random crops can improve performance. (Training speed and accuracy)
    if hyper_params.problem.get("augmentation", None) is None:
        return feature, label

    img_h, img_w, img_c = feature["image"].shape
    augmented_feature = {}
    augmented_label = {}
    augmented_feature["image"] = feature["image"].copy()
    if "depth" in feature:
        augmented_feature["depth"] = feature["depth"].copy()
    if "calibration" in feature:
        augmented_feature["calibration"] = feature["calibration"]
    augmented_feature["hflipped"] = np.array([0], dtype=np.uint8)
    augmented_feature["crop_offset"] = np.array([0, 0], dtype=np.int8)

    for k in label.keys():
        augmented_label[k] = [detection.copy() for detection in label[k]]

    if hyper_params.problem.augmentation.get("enable_horizontal_flip", False):
        if random.random() < 0.5:
            img_h, img_w, img_c = augmented_feature["image"].shape
            augmented_feature["image"] = np.fliplr(augmented_feature["image"])
            if "depth" in feature:
                augmented_feature["depth"] = np.fliplr(augmented_feature["depth"])
            augmented_feature["hflipped"][0] = 1
            hflip_detections(augmented_label, img_w)

    if hyper_params.problem.augmentation.get("enable_micro_translation", False):
        img_h, img_w, img_c = augmented_feature["image"].shape
        dx = int(random.random() * 3)
        dy = int(random.random() * 3)

        augmented_feature["image"] = crop_image(augmented_feature["image"], dy, dx, img_h - dy, img_w - dx)
        if "depth" in feature:
            augmented_feature["depth"] = crop_image(augmented_feature["depth"], dy, dx, img_h - dy, img_w - dx)
        augmented_feature["crop_offset"][0] += dy
        augmented_feature["crop_offset"][1] += dx

        move_detections(augmented_label, -dy, -dx)

    if hyper_params.problem.augmentation.get("random_crop", None) is not None:
        img_h, img_w, img_c = augmented_feature["image"].shape
        target_w = hyper_params.problem.augmentation.random_crop.shape.width
        target_h = hyper_params.problem.augmentation.random_crop.shape.height

        delta_x = max(int(math.ceil((target_w + 1 - img_w) / 2)), 0)
        delta_y = max(int(math.ceil((target_h + 1 - img_h) / 2)), 0)
        move_detections(augmented_label, delta_y, delta_x)
        augmented_feature["image"] = cv2.copyMakeBorder(augmented_feature["image"],
                                                        delta_y, delta_y, delta_x, delta_x,
                                                        cv2.BORDER_CONSTANT)
        img_h, img_w, img_c = augmented_feature["image"].shape

        start_x = 0
        start_y = 0
        if len(augmented_label["detections_2d"]) != 0:
            idx = random.randint(0, len(augmented_label["detections_2d"]) - 1)
            detection = augmented_label["detections_2d"][idx]
            start_x = int(detection.cx - random.random() * (target_w - 20) / 2.0 - 10)
            start_y = int(detection.cy - random.random() * (target_h - 20) / 2.0 - 10)
        else:
            start_x = int(img_w * random.random())
            start_y = int(img_h * random.random())

        # Compute start point so that crop fit's into image and random crop contains detection
        if start_x < 0:
            start_x = 0
        if start_y < 0:
            start_y = 0
        if start_x >= img_w - target_w:
            start_x = img_w - target_w - 1
        if start_y >= img_h - target_h:
            start_y = img_h - target_h - 1

        # Crop image
        augmented_feature["image"] = crop_image(augmented_feature["image"], start_y, start_x, target_h, target_w)
        if "depth" in feature:
            augmented_feature["depth"] = crop_image(augmented_feature["depth"], start_y, start_x, target_h, target_w)
        augmented_feature["crop_offset"][0] += start_y
        augmented_feature["crop_offset"][1] += start_x

        # Crop labels
        move_detections(augmented_label, -start_y, -start_x)

    if hyper_params.problem.augmentation.get("enable_texture_augmentation", False):
        if random.random() < 0.5:
            augmented_feature["image"] = full_texture_augmentation(augmented_feature["image"])

    return augmented_feature, augmented_label

Response: Augment the detection dataset.


Instruction:
Make a summary of the following Python 3 code
def get_dict_from_obj(obj):
    '''
    Edit to get the dict even when the object is a GenericRelatedObjectManager.
    Added the try except.
    '''
    obj_dict = obj.__dict__
    obj_dict_result = obj_dict.copy()
    for key, value in obj_dict.items():
        if key.endswith('_id'):
            key2 = key.replace('_id', '')
            try:
                field, model, direct, m2m = obj._meta.get_field_by_name(key2)
                if isinstance(field, ForeignKey):
                    obj_dict_result[key2] = obj_dict_result[key]
                    del obj_dict_result[key]
            except FieldDoesNotExist:
                pass
    manytomany_list = obj._meta.many_to_many
    for manytomany in manytomany_list:
        pks = [obj_rel.pk for obj_rel in manytomany.value_from_object(obj).select_related()]
        if pks:
            obj_dict_result[manytomany.name] = pks
    return obj_dict_result

Response: Get the dict representation of the object.


Instruction:
Write a Python 3 script to
get the config from the settings. py and the values given to the template tag element and complete these with the ones from the settings. py.

Response:
def get_config(self, request, **kwargs):
        """
        Get the arguments given to the template tag element and complete these
        with the ones from the settings.py if necessary.
        """
        config = kwargs

        config_from_settings = deepcopy(inplace_settings.DEFAULT_INPLACE_EDIT_OPTIONS)
        config_one_by_one = inplace_settings.DEFAULT_INPLACE_EDIT_OPTIONS_ONE_BY_ONE

        if not config_one_by_one:
            # Solution 1: Using default config only if none specified.
            if not config and config_from_settings:
                config = config_from_settings
        else:
            # Solution 2: Updating the configured config with the default one.
            config = dict(config_from_settings, **config)
        return config


Instruction:
Can you generate a brief explanation for the following Python 3 code
def empty_value(self):
        '''
        Get the text to display when the field is empty.
        '''
        edit_empty_value = self.config.get('edit_empty_value', False)
        if edit_empty_value:
            return edit_empty_value
        else:
            return unicode(inplace_settings.INPLACEEDIT_EDIT_EMPTY_VALUE)

Response: Get the text to display when the field is empty.


Instruction:
Can you tell what is the following Python 3 function doing
def do_eval(parser, token):
    "Usage: {% eval %}1 + 1{% endeval %}"

    nodelist = parser.parse(('endeval',))

    class EvalNode(template.Node):
        def render(self, context):
            return template.Template(nodelist.render(context)).render(template.Context(context))
    parser.delete_first_token()
    return EvalNode()

Response: Usage : 1 + 1 { % eval % } 1 + 1 { % endeval % } 1 + 1 { % eval % } 1 + 1 { % endeval % } 1 + 1 { % eval % } 1 + 1 {% endeval % } 1 + 1 {% eval % } 1 + 1 {% eval % } 1 + 1 {% endeval % } 1 + 1 {% eval % } 1 + 1 {% eval % } 1


Instruction:
Create a Python 3 function to
parse uniformly args and kwargs from a templatetag

Response:
def parse_args_kwargs(parser, token):
    """
    Parse uniformly args and kwargs from a templatetag

    Usage::

      For parsing a template like this:

      {% footag my_contents,height=10,zoom=20 as myvar %}

      You simply do this:

      @register.tag
      def footag(parser, token):
          args, kwargs = parse_args_kwargs(parser, token)
    """
    bits = token.contents.split(' ')

    if len(bits) <= 1:
        raise template.TemplateSyntaxError("'%s' takes at least one argument" % bits[0])

    if token.contents[13] == '"':
        end_quote = token.contents.index('"', 14) + 1
        args = [template.Variable(token.contents[13:end_quote])]
        kwargs_start = end_quote
    else:
        try:
            next_space = token.contents.index(' ', 14)
            kwargs_start = next_space + 1
        except ValueError:
            next_space = None
            kwargs_start = None
        args = [template.Variable(token.contents[13:next_space])]

    kwargs = {}
    kwargs_list = token.contents[kwargs_start:].split(',')
    for kwargs_item in kwargs_list:
        if '=' in kwargs_item:
            k, v = kwargs_item.split('=', 1)
            k = k.strip()
            kwargs[k] = template.Variable(v)
    return args, kwargs


Instruction:
Create a Python 3 function for
creating and register metrics from a list of MetricConfigs.

Response:
def create_metrics(
            self, metric_configs: Iterable[MetricConfig]) -> Dict[str, Metric]:
        """Create and register metrics from a list of MetricConfigs."""
        return self.registry.create_metrics(metric_configs)


Instruction:
How would you explain what the following Python 3 function does
def _setup_logging(self, log_level: str):
        level = getattr(logging, log_level)
        names = (
            'aiohttp.access', 'aiohttp.internal', 'aiohttp.server',
            'aiohttp.web', self.name)
        for name in names:
            setup_logger(name=name, stream=sys.stderr, level=level)

Response: Setup logging for the application and aiohttp.


Instruction:
Implement a function in Python 3 to
return a PrometheusExporter configured with args.

Response:
def _get_exporter(self, args: argparse.Namespace) -> PrometheusExporter:
        """Return a :class:`PrometheusExporter` configured with args."""
        exporter = PrometheusExporter(
            self.name, self.description, args.host, args.port, self.registry)
        exporter.app.on_startup.append(self.on_application_startup)
        exporter.app.on_shutdown.append(self.on_application_shutdown)
        return exporter


Instruction:
How would you explain what the following Python 3 function does
def create_metrics(self,
                       configs: Iterable[MetricConfig]) -> Dict[str, Metric]:
        """Create Prometheus metrics from a list of MetricConfigs."""
        metrics: Dict[str, Metric] = {
            config.name: self._register_metric(config)
            for config in configs
        }
        self._metrics.update(metrics)
        return metrics

Response: Create Prometheus metrics from a list of MetricConfigs.


Instruction:
Write a Python 3 function for
returning a metric optionally configured with labels.

Response:
def get_metric(
            self, name: str,
            labels: Union[Dict[str, str], None] = None) -> Metric:
        """Return a metric, optionally configured with labels."""
        metric = self._metrics[name]
        if labels:
            return metric.labels(**labels)

        return metric


Instruction:
Make a summary of the following Python 3 code
def run(self):
        run_app(
            self.app,
            host=self.host,
            port=self.port,
            print=lambda *args, **kargs: None,
            access_log_format='%a "%r" %s %b "%{Referrer}i" "%{User-Agent}i"')

Response: Run the application for the exporter.


Instruction:
How would you implement a function in Python 3 that
homes page request handler.

Response:
async def _handle_home(self, request: Request) -> Response:
        """Home page request handler."""
        if self.description:
            title = f'{self.name} - {self.description}'
        else:
            title = self.name

        text = dedent(
            f'''<!DOCTYPE html>
            <html>
              <head>
                <title>{title}</title>
              </head>
              <body>
                <h1>{title}</h1>
                <p>
                  Metric are exported at the
                  <a href="/metrics">/metrics</a> endpoint.
                </p>
              </body>
            </html>
            ''')
        return Response(content_type='text/html', text=text)


Instruction:
Make a summary of the following Python 3 code
def wa(client, event, channel, nick, rest):
	client = wolframalpha.Client(pmxbot.config['Wolfram|Alpha API key'])
	res = client.query(rest)
	return next(res.results).text

Response: Wolfram query resolver by Wolfram|Alpha API. Returns the first result.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fix_HTTPMessage():
	if six.PY3:
		return

	http_client.HTTPMessage.get_content_type = http_client.HTTPMessage.gettype
	http_client.HTTPMessage.get_param = http_client.HTTPMessage.getparam

Response: Fixes the HTTPMessage. get_content_type and get_param methods.


Instruction:
Can you implement a function in Python 3 that
queries Wolfram|Alpha using the v2. 0 API.

Response:
def query(self, input, params=(), **kwargs):
        """
        Query Wolfram|Alpha using the v2.0 API

        Allows for arbitrary parameters to be passed in
        the query. For example, to pass assumptions:

            client.query(input='pi', assumption='*C.pi-_*NamedConstant-')

        To pass multiple assumptions, pass multiple items
        as params:

            params = (
                ('assumption', '*C.pi-_*NamedConstant-'),
                ('assumption', 'DateOrder_**Day.Month.Year--'),
            )
            client.query(input='pi', params=params)

        For more details on Assumptions, see
        https://products.wolframalpha.com/api/documentation.html#6
        """
        data = dict(
            input=input,
            appid=self.app_id,
        )
        data = itertools.chain(params, data.items(), kwargs.items())

        query = urllib.parse.urlencode(tuple(data))
        url = 'https://api.wolframalpha.com/v2/query?' + query
        resp = urllib.request.urlopen(url)
        assert resp.headers.get_content_type() == 'text/xml'
        assert resp.headers.get_param('charset') == 'utf-8'
        return Result(resp)


Instruction:
Can you generate the documentation for the following Python 3 function
def info(self):
        return itertools.chain(self.pods, self.assumptions, self.warnings)

Response: Returns a list of all the pods assumptions and warnings of this result.


Instruction:
Can you implement a function in Python 3 that
returns a list of all the result pods that hold the response to a simple discrete query.

Response:
def results(self):
        """
        The pods that hold the response to a simple, discrete query.
        """
        return (
            pod
            for pod in self.pods
            if pod.primary
            or pod.title == 'Result'
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def encode(request, data):
        if data is None:
            return request

        request.add_header('Content-Type', 'application/json')
        request.data = json.dumps(data)

        return request

Response: Add request content data to request body set Content - type header.


Instruction:
Here you have a function in Python 3, explain what it does
def call_api(
            self,
            method,
            url,
            headers=None,
            params=None,
            data=None,
            files=None,
            timeout=None,
    ):
        """ Call API.

        This returns object containing data, with error details if applicable.

        Args:
            method (str): The HTTP method to use.
            url (str): Resource location relative to the base URL.
            headers (dict or None): Extra request headers to set.
            params (dict or None): Query-string parameters.
            data (dict or None): Request body contents for POST or PUT requests.
            files (dict or None: Files to be passed to the request.
            timeout (int): Maximum time before timing out.

        Returns:
            ResultParser or ErrorParser.
        """
        method = method.upper()
        headers = deepcopy(headers) or {}
        headers['Accept'] = self.accept_type
        params = deepcopy(params) or {}
        data = data or {}
        files = files or {}

        if self.username and self.api_key:
            params.update(self.get_credentials())

        url = urljoin(self.base_url, url)

        r = requests.request(
            method,
            url,
            headers=headers,
            params=params,
            files=files,
            data=data,
            timeout=timeout,
        )

        return r, r.status_code

Response:
Call API.

        This returns object containing data, with error details if applicable.

        Args:
            method (str): The HTTP method to use.
            url (str): Resource location relative to the base URL.
            headers (dict or None): Extra request headers to set.
            params (dict or None): Query-string parameters.
            data (dict or None): Request body contents for POST or PUT requests.
            files (dict or None: Files to be passed to the request.
            timeout (int): Maximum time before timing out.

        Returns:
            ResultParser or ErrorParser.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, url, params=None, **kwargs):
        return self.call_api(
            "GET",
            url,
            params=params,
            **kwargs
        )

Response: Call the API with a GET request.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete(self, url, params=None, **kwargs):
        return self.call_api(
            "DELETE",
            url,
            params=params,
            **kwargs
        )

Response: Call the API with a DELETE request.


Instruction:
Create a Python 3 function for
calling the API with a PUT request.

Response:
def put(self, url, params=None, data=None, files=None, **kwargs):
        """ Call the API with a PUT request.

        Args:
            url (str): Resource location relative to the base URL.
            params (dict or None): Query-string parameters.
            data (dict or None): Request body contents.
            files (dict or None: Files to be passed to the request.

        Returns:
            An instance of ResultParser or ErrorParser.
        """
        return self.call_api(
            "PUT",
            url,
            params=params,
            data=data,
            files=files,
            **kwargs
        )


Instruction:
Explain what the following Python 3 code does
def post(self, url, params=None, data=None, files=None, **kwargs):
        return self.call_api(
            "POST",
            url,
            params=params,
            data=data,
            files=files,
            **kwargs
        )

Response: Call the API with a POST request.


Instruction:
How would you code a function in Python 3 to
process a query and return the response.

Response:
def _process_query(self, query, prepared=False):
        """ Process query recursively, if the text is too long,
        it is split and processed bit a bit.

        Args:
            query (sdict): Text to be processed.
            prepared (bool): True when the query is ready to be submitted via
            POST request.
        Returns:
            str: Body ready to be submitted to the API.
        """

        # Exit condition and POST
        if prepared is True:
            files = {'query': str(query)}

            logger.debug('About to submit the following query {}'.format(query))

            res, status = self.post(
                self.disambiguate_service,
                files=files,
                headers={'Accept': 'application/json'},
            )

            if status == 200:
                return self.decode(res), status
            else:
                logger.debug('Disambiguation failed.')
                return None, status

        text = query['text']

        sentence_coordinates = [
            {
                "offsetStart": 0,
                "offsetEnd": len(text)
            }
        ]

        total_nb_sentences = len(sentence_coordinates)  # Sentences from text.
        sentences_groups = []

        if len(text) > self.max_text_length:
            res, status_code = self.segment(text)

            if status_code == 200:
                sentence_coordinates = res['sentences']
                total_nb_sentences = len(sentence_coordinates)
            else:
                logger.error('Error during the segmentation of the text.')

            logger.debug(
                'Text too long, split in {} sentences; building groups of {} '
                'sentences.'.format(
                    total_nb_sentences, self.sentences_per_group
                )
            )
            sentences_groups = self._group_sentences(
                total_nb_sentences,
                self.sentences_per_group
            )
        else:
            query['sentence'] = "true"

        if total_nb_sentences > 1:
            query['sentences'] = sentence_coordinates

        if len(sentences_groups) > 0:
            for group in sentences_groups:
                query['processSentence'] = group

                res, status_code = self._process_query(query, prepared=True)

                if status_code == 200:
                    if 'entities' in res:
                        query['entities'] = res[u'entities']
                    query['language'] = res[u'language']
                else:
                    logger.error(
                        "Error when processing the query {}".format(query)
                    )
                    return None, status_code

        else:
            res, status_code = self._process_query(query, prepared=True)

            if status_code == 200:
                query['language'] = res[u'language']
                if 'entities' in res:
                    query['entities'] = res[u'entities']
            else:
                logger.error("Error when processing the query {}".format(query))
                return None, status_code

        return query, status_code


Instruction:
Can you tell what is the following Python 3 function doing
def _group_sentences(total_nb_sentences, group_length):
        sentences_groups = []
        current_sentence_group = []

        for i in range(0, total_nb_sentences):
            if i % group_length == 0:
                if len(current_sentence_group) > 0:
                    sentences_groups.append(current_sentence_group)
                current_sentence_group = [i]
            else:
                current_sentence_group.append(i)

        if len(current_sentence_group) > 0:
            sentences_groups.append(current_sentence_group)

        return sentences_groups

Response: Splits sentences in groups given a specific length.


Instruction:
Can you implement a function in Python 3 that
disambiguates a PDF file.

Response:
def disambiguate_pdf(self, file, language=None, entities=None):
        """ Call the disambiguation service in order to process a pdf file .

        Args:
            pdf (file): PDF file to be disambiguated.
            language (str): language of text (if known)

        Returns:
            dict, int: API response and API status.
        """

        body = {
            "customisation": "generic"
        }

        if language:
            body['language'] = {"lang": language}

        if entities:
            body['entities'] = entities

        files = {
            'query': str(body),
            'file': (
                file,
                open(file, 'rb'),
                'application/pdf',
                {'Expires': '0'}
            )
        }

        res, status = self.post(
            self.disambiguate_service,
            files=files,
            headers={'Accept': 'application/json'},
        )

        if status != 200:
            logger.debug('Disambiguation failed with error ' + str(status))

        return self.decode(res), status


Instruction:
Here you have a function in Python 3, explain what it does
def disambiguate_terms(self, terms, language="en", entities=None):

        body = {
            "termVector": terms,
            "entities": [],
            "onlyNER": "false",
            "customisation": "generic"
        }

        body['language'] = {"lang": language}

        if entities:
            body['entities'] = entities

        files = {'query': str(body)}

        logger.debug('About to submit the following query {}'.format(body))

        res, status = self.post(
            self.disambiguate_service,
            files=files,
            headers={'Accept': 'application/json'},
        )

        if status == 200:
            return self.decode(res), status
        else:
            logger.debug('Disambiguation failed.')
            return None, status

Response: Disambiguate the terms in the list of objects of term weight and language.


Instruction:
Can you generate the documentation for the following Python 3 function
def disambiguate_text(self, text, language=None, entities=None):

        body = {
            "text": text,
            "entities": [],
            "onlyNER": "false",
            "customisation": "generic"
        }

        if language:
            body['language'] = {"lang": language}

        if entities:
            body['entities'] = entities

        result, status_code = self._process_query(body)

        if status_code != 200:
            logger.debug('Disambiguation failed.')

        return result, status_code

Response: Disambiguate a text in the specified language and entities.


Instruction:
Can you generate the documentation for the following Python 3 function
def disambiguate_query(self, query, language=None, entities=None):

        body = {
            "shortText": query,
            "entities": [],
            "onlyNER": "false",
            "customisation": "generic"
        }

        if language:
            body['language'] = {"lang": language}

        if entities:
            body['entities'] = entities

        files = {'query': str(body)}

        logger.debug('About to submit the following query {}'.format(body))

        res, status = self.post(
            self.disambiguate_service,
            files=files,
            headers={'Accept': 'application/json'},
        )

        if status == 200:
            return self.decode(res), status
        else:
            logger.debug('Disambiguation failed.')
            return None, status

Response: Disambiguate a search query.


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

        files = {'text': text}
        res, status_code = self.post(self.segmentation_service, files=files)

        if status_code != 200:
            logger.debug('Segmentation failed.')

        return self.decode(res), status_code

Response: Segment the text in a set of sentences.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_language(self, text):
        files = {'text': text}
        res, status_code = self.post(self.language_service, files=files)

        if status_code != 200:
            logger.debug('Language recognition failed.')

        return self.decode(res), status_code

Response:
Recognise the language of the text in input
 getSeasons and return the result.


Instruction:
Can you tell what is the following Python 3 function doing
def get_concept(self, conceptId, lang='en'):
        url = urljoin(self.concept_service + '/', conceptId)

        res, status_code = self.get(url, params={'lang': lang})

        if status_code != 200:
            logger.debug('Fetch concept failed.')

        return self.decode(res), status_code

Response: Fetch the concept from the Knowledge base and return the response code.


Instruction:
How would you implement a function in Python 3 that
fits the MDR ensemble from the provided training data and returns the feature map.

Response:
def fit(self, features, classes):
        """Constructs the MDR ensemble from the provided training data

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        classes: array-like {n_samples}
            List of class labels for prediction

        Returns
        -------
        None

        """
        self.ensemble.fit(features, classes)

        # Construct the feature map from the ensemble predictions
        unique_rows = list(set([tuple(row) for row in features]))
        for row in unique_rows:
            self.feature_map[row] = self.ensemble.predict([row])[0]


Instruction:
How would you implement a function in Python 3 that
estimates the accuracy of the predictions from the MDR ensemble and returns the score of the predictions based on the new feature.

Response:
def score(self, features, classes, scoring_function=None, **scoring_function_kwargs):
        """Estimates the accuracy of the predictions from the MDR ensemble

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to predict from
        classes: array-like {n_samples}
            List of true class labels

        Returns
        -------
        accuracy_score: float
            The estimated accuracy based on the constructed feature

        """
        new_feature = self.ensemble.predict(features)

        if scoring_function is None:
            return accuracy_score(classes, new_feature)
        else:
            return scoring_function(classes, new_feature, **scoring_function_kwargs)


Instruction:
Can you tell what is the following Python 3 function doing
def fit(self, features, class_labels):
        unique_labels = sorted(np.unique(class_labels))
        if len(unique_labels) != 2:
            raise ValueError('MDR only supports binary endpoints.')

        # Count the distribution of classes that fall into each MDR grid cell
        self.class_count_matrix = defaultdict(lambda: defaultdict(int))
        for row_i in range(features.shape[0]):
            feature_instance = tuple(features[row_i])
            self.class_count_matrix[feature_instance][class_labels[row_i]] += 1
        self.class_count_matrix = dict(self.class_count_matrix)

        # Only applies to binary classification
        overall_class_fraction = float(sum(class_labels == unique_labels[0])) / class_labels.size

        # If one class is more abundant in a MDR grid cell than it is overall, then assign the cell to that class
        self.feature_map = {}
        for feature_instance in self.class_count_matrix:
            counts = self.class_count_matrix[feature_instance]
            fraction = float(counts[unique_labels[0]]) / np.sum(list(counts.values()))
            if fraction > overall_class_fraction:
                self.feature_map[feature_instance] = unique_labels[0]
            elif fraction == overall_class_fraction:
                self.feature_map[feature_instance] = self.tie_break
            else:
                self.feature_map[feature_instance] = unique_labels[1]

        return self

Response: Constructs the MDR feature map from the provided training data.


Instruction:
Given the following Python 3 function, write the documentation
def fit_transform(self, features, class_labels):
        self.fit(features, class_labels)
        return self.transform(features)

Response: Convenience function that fits the provided data then constructs a new feature from the provided features.


Instruction:
Make a summary of the following Python 3 code
def fit_predict(self, features, class_labels):
        self.fit(features, class_labels)
        return self.predict(features)

Response: Convenience function that fits the provided data then constructs predictions from the provided features.


Instruction:
Write a Python 3 script for
estimating the accuracy of the predictions from the constructed feature.

Response:
def score(self, features, class_labels, scoring_function=None, **scoring_function_kwargs):
        """Estimates the accuracy of the predictions from the constructed feature.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to predict from
        class_labels: array-like {n_samples}
            List of true class labels

        Returns
        -------
        accuracy_score: float
            The estimated accuracy based on the constructed feature

        """
        if self.feature_map is None:
            raise ValueError('The MDR model must be fit before score can be called.')

        new_feature = self.predict(features)

        if scoring_function is None:
            return accuracy_score(class_labels, new_feature)
        else:
            return scoring_function(class_labels, new_feature, **scoring_function_kwargs)


Instruction:
Implement a function in Python 3 to
construct the Continuous MDR feature map from the provided training data.

Response:
def fit(self, features, targets):
        """Constructs the Continuous MDR feature map from the provided training data.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix
        targets: array-like {n_samples}
            List of target values for prediction

        Returns
        -------
        self: A copy of the fitted model

        """
        self.feature_map = defaultdict(lambda: self.default_label)
        self.overall_mean_trait_value = np.mean(targets)
        self.mdr_matrix_values = defaultdict(list)

        for row_i in range(features.shape[0]):
            feature_instance = tuple(features[row_i])
            self.mdr_matrix_values[feature_instance].append(targets[row_i])

        for feature_instance in self.mdr_matrix_values:
            grid_mean_trait_value = np.mean(self.mdr_matrix_values[feature_instance])
            if grid_mean_trait_value > self.overall_mean_trait_value:
                self.feature_map[feature_instance] = 1
            elif grid_mean_trait_value == self.overall_mean_trait_value:
                self.feature_map[feature_instance] = self.tie_break
            else:
                self.feature_map[feature_instance] = 0

        # Convert defaultdict to dict so CMDR objects can be easily pickled
        self.feature_map = dict(self.feature_map)
        self.mdr_matrix_values = dict(self.mdr_matrix_values)

        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def transform(self, features):
        new_feature = np.zeros(features.shape[0], dtype=np.int)

        for row_i in range(features.shape[0]):
            feature_instance = tuple(features[row_i])
            if feature_instance in self.feature_map:
                new_feature[row_i] = self.feature_map[feature_instance]
            else:
                new_feature[row_i] = self.default_label

        return new_feature.reshape(features.shape[0], 1)

Response: Uses the Continuous MDR feature map to construct a new feature from the provided features.


Instruction:
How would you code a function in Python 3 to
estimate the quality of the ContinuousMDR model using a t - statistic.

Response:
def score(self, features, targets):
        """Estimates the quality of the ContinuousMDR model using a t-statistic.

        Parameters
        ----------
        features: array-like {n_samples, n_features}
            Feature matrix to predict from
        targets: array-like {n_samples}
            List of true target values

        Returns
        -------
        quality_score: float
            The estimated quality of the Continuous MDR model

        """
        if self.feature_map is None:
            raise ValueError('The Continuous MDR model must be fit before score() can be called.')

        group_0_trait_values = []
        group_1_trait_values = []

        for feature_instance in self.feature_map:
            if self.feature_map[feature_instance] == 0:
                group_0_trait_values.extend(self.mdr_matrix_values[feature_instance])
            else:
                group_1_trait_values.extend(self.mdr_matrix_values[feature_instance])

        return abs(ttest_ind(group_0_trait_values, group_1_trait_values).statistic)


Instruction:
Can you create a Python 3 function that
calculates the entropy H ( X ) in the given base

Response:
def entropy(X, base=2):
    """Calculates the entropy, H(X), in the given base

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the entropy
    base: integer (default: 2)
        The base in which to calculate entropy

    Returns
    ----------
    entropy: float
        The entropy calculated according to the equation H(X) = -sum(p_x * log p_x) for all states of X

    """
    return scipy.stats.entropy(list(Counter(X).values()), base=base)


Instruction:
How would you explain what the following Python 3 function does
def joint_entropy(X, Y, base=2):
    X_Y = ['{}{}'.format(x, y) for x, y in zip(X, Y)]
    return entropy(X_Y, base=base)

Response: Calculates the joint entropy H ( X Y ) in the given base


Instruction:
Can you generate the documentation for the following Python 3 function
def conditional_entropy(X, Y, base=2):
    return joint_entropy(X, Y, base=base) - entropy(Y, base=base)

Response: Calculates the conditional entropy H ( X | Y ) in the given base


Instruction:
Can you create a Python 3 function that
calculates the mutual information between two variables I ( X ; Y ) in the given base

Response:
def mutual_information(X, Y, base=2):
    """Calculates the mutual information between two variables, I(X;Y), in the given base

    Parameters
    ----------
    X: array-like (# samples)
        An array of values for which to compute the mutual information
    Y: array-like (# samples)
        An array of values for which to compute the mutual information
    base: integer (default: 2)
        The base in which to calculate mutual information

    Returns
    ----------
    mutual_information: float
        The mutual information calculated according to the equation I(X;Y) = H(Y) - H(Y|X)

    """
    return entropy(Y, base=base) - conditional_entropy(Y, X, base=base)


Instruction:
Can you tell what is the following Python 3 function doing
def two_way_information_gain(X, Y, Z, base=2):
    X_Y = ['{}{}'.format(x, y) for x, y in zip(X, Y)]
    return (mutual_information(X_Y, Z, base=base) -
            mutual_information(X, Z, base=base) -
            mutual_information(Y, Z, base=base))

Response: Calculates the two - way information gain between two variables I X Y and Z.


Instruction:
How would you implement a function in Python 3 that
calculates the three - way information gain between two variables I ( W X Y Z ) and I ( W ; X Y Z ) in the given base

Response:
def three_way_information_gain(W, X, Y, Z, base=2):
    """Calculates the three-way information gain between three variables, I(W;X;Y;Z), in the given base

    IG(W;X;Y;Z) indicates the information gained about variable Z by the joint variable W_X_Y, after removing
    the information that W, X, and Y have about Z individually and jointly in pairs. Thus, 3-way information gain
    measures the synergistic predictive value of variables W, X, and Y about variable Z.

    Parameters
    ----------
    W: array-like (# samples)
        An array of values for which to compute the 3-way information gain
    X: array-like (# samples)
        An array of values for which to compute the 3-way information gain
    Y: array-like (# samples)
        An array of values for which to compute the 3-way information gain
    Z: array-like (# samples)
        An array of outcome values for which to compute the 3-way information gain
    base: integer (default: 2)
        The base in which to calculate 3-way information

    Returns
    ----------
    mutual_information: float
        The information gain calculated according to the equation:
            IG(W;X;Y;Z) = I(W,X,Y;Z) - IG(W;X;Z) - IG(W;Y;Z) - IG(X;Y;Z) - I(W;Z) - I(X;Z) - I(Y;Z)

    """
    W_X_Y = ['{}{}{}'.format(w, x, y) for w, x, y in zip(W, X, Y)]
    return (mutual_information(W_X_Y, Z, base=base) -
            two_way_information_gain(W, X, Z, base=base) -
            two_way_information_gain(W, Y, Z, base=base) -
            two_way_information_gain(X, Y, Z, base=base) -
            mutual_information(W, Z, base=base) -
            mutual_information(X, Z, base=base) -
            mutual_information(Y, Z, base=base))


Instruction:
Given the following Python 3 function, write the documentation
def _mdr_predict(X, Y, labels):
    return MDR().fit_predict(np.column_stack((X, Y)), labels)

Response: Fits a MDR model to variables X and Y with the given labels then returns the predictions of the MDR model to the variables X and Y with the given labels.


Instruction:
Here you have a function in Python 3, explain what it does
def mdr_entropy(X, Y, labels, base=2):
    return entropy(_mdr_predict(X, Y, labels), base=base)

Response: Calculates the MDR entropy H ( XY ) in the given base base


Instruction:
Write a Python 3 script to
calculate the MDR conditional entropy of the given variables X and Y and returns the MDR conditional entropy of the resulting model.

Response:
def mdr_conditional_entropy(X, Y, labels, base=2):
    """Calculates the MDR conditional entropy, H(XY|labels), in the given base

    MDR conditional entropy is calculated by combining variables X and Y into a single MDR model then calculating
    the entropy of the resulting model's predictions conditional on the provided labels.

    Parameters
    ----------
    X: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    Y: array-like (# samples)
        An array of values corresponding to one feature in the MDR model
    labels: array-like (# samples)
        The class labels corresponding to features X and Y
    base: integer (default: 2)
        The base in which to calculate MDR conditional entropy

    Returns
    ----------
    mdr_conditional_entropy: float
        The MDR conditional entropy calculated according to the equation H(XY|labels) = H(XY,labels) - H(labels)

    """
    return conditional_entropy(_mdr_predict(X, Y, labels), labels, base=base)


Instruction:
Here you have a function in Python 3, explain what it does
def mdr_mutual_information(X, Y, labels, base=2):
    return mutual_information(_mdr_predict(X, Y, labels), labels, base=base)

Response: Calculates the MDR mutual information between two variables X and Y.


Instruction:
Explain what the following Python 3 code does
def n_way_models(mdr_instance, X, y, n=[2], feature_names=None):
    if feature_names is None:
        feature_names = list(range(X.shape[1]))

    for cur_n in n:
        for features in itertools.combinations(range(X.shape[1]), cur_n):
            mdr_model = copy.deepcopy(mdr_instance)
            mdr_model.fit(X[:, features], y)
            mdr_model_score = mdr_model.score(X[:, features], y)
            model_features = [feature_names[feature] for feature in features]
            yield mdr_model, mdr_model_score, model_features

Response: Returns a generator that yields the n - way models for each feature in X.


Instruction:
Can you tell what is the following Python 3 function doing
def plot_mdr_grid(mdr_instance):
    var1_levels = list(set([variables[0] for variables in mdr_instance.feature_map]))
    var2_levels = list(set([variables[1] for variables in mdr_instance.feature_map]))
    max_count = np.array(list(mdr_instance.class_count_matrix.values())).flatten().max()

    """
    TODO:
        - Add common axis labels
        - Make sure this scales for smaller and larger record sizes
        - Extend to 3-way+ models, e.g., http://4.bp.blogspot.com/-vgKCjEkWFUc/UPwPuHo6XvI/AAAAAAAAAE0/fORHqDcoikE/s1600/model.jpg
    """

    fig, splots = plt.subplots(ncols=len(var1_levels), nrows=len(var2_levels), sharey=True, sharex=True)
    fig.set_figwidth(6)
    fig.set_figheight(6)

    for (var1, var2) in itertools.product(var1_levels, var2_levels):
        class_counts = mdr_instance.class_count_matrix[(var1, var2)]
        splot = splots[var2_levels.index(var2)][var1_levels.index(var1)]
        splot.set_yticks([])
        splot.set_xticks([])
        splot.set_ylim(0, max_count * 1.5)
        splot.set_xlim(-0.5, 1.5)

        if var2_levels.index(var2) == 0:
            splot.set_title('X1 = {}'.format(var1), fontsize=12)
        if var1_levels.index(var1) == 0:
            splot.set_ylabel('X2 = {}'.format(var2), fontsize=12)

        bars = splot.bar(left=range(class_counts.shape[0]),
                         height=class_counts, width=0.5,
                         color='black', align='center')

        bgcolor = 'lightgrey' if mdr_instance.feature_map[(var1, var2)] == 0 else 'darkgrey'
        splot.set_axis_bgcolor(bgcolor)
        for index, bar in enumerate(bars):
            splot.text(index, class_counts[index] + (max_count * 0.1), class_counts[index], ha='center')

    fig.tight_layout()
    return fig

Response: Visualizes the MDR grid of a given fitted MDR instance.


Instruction:
How would you explain what the following Python 3 function does
def migrate(migrations_root):
    from flask_migrate import Migrate, upgrade as migrate_upgrade
    from flask_sqlalchemy import SQLAlchemy
    from sqlalchemy.engine.url import make_url
    from sqlalchemy_utils import database_exists, create_database

    db = SQLAlchemy()
    dsn = make_url(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(dsn):
        create_database(dsn)
        pass

    migrations_root = migrations_root or os.path.join(
        os.environ.get('FANTASY_MIGRATION_PATH',
                       os.getcwd()),
        'migrations')
    migrations_root = os.path.expanduser(migrations_root)

    if os.path.exists(migrations_root):
        mig = Migrate(app, db, directory=migrations_root)
        mig.init_app(app, db)
        migrate_upgrade(migrations_root)
    else:
        click.echo(
            click.style('migration files not exist,skip migrate...', fg='red'))
        sys.exit(-1)
    pass

Response: 等价于 django migrate 操作


Instruction:
Create a Python 3 function to
build a new resource tree for the given hive. txt file.

Response:
def requirements(work_dir, hive_root, with_requirements,
                 with_dockerfile, active_module, active_module_file):
    """编译全新依赖文件"""
    import sys
    sys.path.insert(0, hive_root)

    hive_root = os.path.abspath(os.path.expanduser(hive_root))

    work_dir = work_dir or os.path.join(
        os.environ.get('FANTASY_APP_PATH',
                       os.getcwd()))

    work_dir = os.path.expanduser(work_dir)

    requirements_root = os.path.join(work_dir, 'requirements')
    migrate_root = os.path.join(work_dir, 'migrations')

    # active_modules 严格按照顺序
    active_module_paths = []
    active_module_list = []

    if active_module_file:
        with open(active_module_file, 'r') as fp:
            for l in fp:
                pkg = l.split('#')[0].strip()
                if pkg:
                    active_module_list.append(l.strip("\n"))
            pass

    active_module_list += active_module

    for m in active_module_list:
        try:
            mod = importlib.import_module(m)
            active_module_paths.append(os.path.dirname(mod.__file__))
        except ImportError:
            click.echo('module "%s" not found.' % m, color="yellow")
            pass
        pass

    def build_requirements():
        """构造requirements文件

        requirements文件共分为两份：

        - hive.txt  从hive项目中直接复制
        - hive-modules.txt 从指定的模块中装载依赖项

        .. note::
            requirements要求必须是顺序无关的
            因为我们会使用set来去重，并按照value排序

        """
        if not os.path.exists(requirements_root):
            os.makedirs(requirements_root)
            pass

        click.echo(click.style("Generate hive requirements...", fg="yellow"))

        shutil.copy(
            os.path.join(hive_root, 'requirements.txt'),
            os.path.join(requirements_root, 'hive.txt')
        )

        click.echo(click.style("Generate hive-module requirements...",
                               fg="yellow"))

        requirements_files = []

        for m in active_module_paths:
            t = os.path.join(m, 'requirements.txt')
            if os.path.exists(t):
                requirements_files.append(t)
            pass

        module_packages = set()
        with fileinput.input(requirements_files) as fp:
            for line in fp:
                pkg = line.split('#')[0].strip()
                if pkg:
                    module_packages.add(pkg)
            pass

        with click.open_file(
                os.path.join(requirements_root, 'hive-modules.txt'),
                'w') as fp:
            for p in module_packages:
                fp.write("%s\n" % p)
            pass
        pass

    def build_dockerfile():
        """构造Dockerfile"""

        modules_in_hive = map(
            lambda x: x.replace(hive_root, '').lstrip('/'),
            filter(lambda x: x.startswith(hive_root),
                   active_module_paths))

        modules_path = ' '.join(modules_in_hive)

        docker_file = os.path.join(
            os.path.dirname(requirements_root),
            'Dockerfile'
        )

        # update Dockerfile
        if os.path.exists(docker_file):
            click.echo(click.style("Found Dockerfile,try update...",
                                   fg="yellow"))

            with open(docker_file, 'r') as fp:
                buffer = fp.read()
                pass
            import re
            replaced = re.sub('ARG HIVE_PACKAGES=".*"',
                              'ARG HIVE_PACKAGES="%s"' % modules_path,
                              buffer)
            with open(docker_file, 'w') as fp:
                fp.write(replaced)
                pass

            pass
        pass

    def build_migrations():
        models_pairs = filter(
            lambda pair: os.path.exists(pair[0]),
            map(lambda x: (os.path.join(x[0], 'models.py'), x[1]),
                [(v, active_module_list[i]) for i, v in
                 enumerate(active_module_paths)]))

        try:
            _, models = zip(*models_pairs)
        except ValueError:
            click.echo(click.style("No models found,"
                                   "is it include in "
                                   "your PYTHONPATH?\n"
                                   "Modules: %s" %
                                   ','.join(active_module_list),
                                   fg="yellow"))
            return

        click.echo(click.style("Found models.txt,try update...",
                               fg="yellow"))

        with open(os.path.join(migrate_root, 'models.txt'), 'w') as fp:
            for p in models:
                fp.write("%s\n" % p)
            pass
        pass

    def build_tasks():
        tasks_pairs = filter(
            lambda pair: os.path.exists(pair[0]),
            map(lambda x: (os.path.join(x[0], 'tasks.py'), x[1]),
                [(v, active_module_list[i]) for i, v in
                 enumerate(active_module_paths)]))

        try:
            _, tasks = zip(*tasks_pairs)
        except ValueError:
            click.echo(click.style("No tasks found,"
                                   "is it include in "
                                   "your PYTHONPATH?\n"
                                   "Modules: %s" %
                                   ','.join(active_module_list),
                                   fg="yellow"))
            return

        click.echo(click.style("Found tasks.txt,try update...",
                               fg="yellow"))
        with open(os.path.join(migrate_root, 'tasks.txt'), 'w') as fp:
            for p in tasks:
                fp.write("%s\n" % p)
        pass

    if with_requirements:
        build_requirements()

    if with_dockerfile:
        build_dockerfile()

    if os.path.exists(migrate_root):
        build_migrations()

    if os.path.exists(migrate_root):
        build_tasks()

    click.echo(click.style("Generate done...", fg="yellow"))
    pass


Instruction:
Write a Python 3 script for
creating a smart database if it doesn t exist

Response:
def smart_database(app):
    """尝试对数据库做初始化操作"""

    from sqlalchemy.engine.url import make_url
    from sqlalchemy_utils import database_exists, create_database

    # 如果数据库不存在，则尝试创建数据
    dsn = make_url(app.config['SQLALCHEMY_DATABASE_URI'])
    if not database_exists(dsn):
        create_database(dsn)
        pass
    pass


Instruction:
Can you generate the documentation for the following Python 3 function
def smart_migrate(app, migrations_root):

    db = app.db
    if os.path.exists(migrations_root) and \
            os.environ['FANTASY_PRIMARY_NODE'] != 'no':
        from flask_migrate import (Migrate,
                                   upgrade as migrate_upgrade)

        migrate = Migrate(app, db, directory=migrations_root)
        migrate.init_app(app, db)
        migrate_upgrade(migrations_root)
        pass
    pass

Response: smart_migrate - 如果存在 migration且指定为 primary_node则执行 migrate操作


Instruction:
Can you tell what is the following Python 3 function doing
def load_tasks(app, entry_file=None):
    from celery import Task
    tasks_txt = os.path.join(os.path.dirname(entry_file), 'migrations',
                             'tasks.txt')

    if not os.path.exists(tasks_txt):
        import sys
        print('Tasks file not found:%s' % tasks_txt)
        sys.exit(-1)

    class ContextTask(Task):
        abstract = True

        def __call__(self, *args, **kwargs):
            with app.app_context():
                return super().__call__(*args, **kwargs)

    app.celery.config_from_object(app.config, namespace='CELERY')
    app.celery.Task = ContextTask

    with app.app_context():
        with open(tasks_txt, 'r') as f:
            for line in f:
                mod = line.strip('\n')
                if mod:
                    importlib.import_module(mod + '.tasks')
                pass
            pass
        pass
    pass

Response: Load tasks from a file.


Instruction:
Can you generate the documentation for the following Python 3 function
def create_app(app_name, config={}, db=None, celery=None):

    track_mode = os.environ['FANTASY_TRACK_MODE'] == 'yes'
    if track_mode:
        print('(00/14)fantasy track mode active...')

    active_db = os.environ['FANTASY_ACTIVE_DB'] == 'yes'

    if track_mode:
        print('(01/14)hacking webargs...')

    from webargs.flaskparser import parser
    from . import error_handler, hacker, cli

    hacker.hack_webargs()

    migrations_root = os.path.join(
        os.environ.get('FANTASY_MIGRATION_PATH',
                       os.getcwd()),
        'migrations')

    if track_mode:
        print('(02/14)initial app...')

    mod = importlib.import_module(app_name)
    app = FantasyFlask(__name__, root_path=os.path.dirname(mod.__file__))

    if track_mode:
        print('(03/14)update app.config...')

    if config:
        app.config.update(config)

    # 由外部做显式声明，否则不做调用
    config_module = os.environ.get('FANTASY_SETTINGS_MODULE', None)
    if track_mode:
        print("       found config module %s,try load it..." % config_module)

    if config_module:
        app.config.from_object(config_module)

    if track_mode:
        print('(04/14)confirm celery...')

    if celery:
        app.celery = celery
        pass

    if track_mode:
        print('(05/14)bind app context...')

    with app.app_context():

        if track_mode:
            print('(06/14)confirm db handle...')

        if db is None:
            global _db
            app.db = _db
        else:
            app.db = db

        if track_mode:
            print('(07/14)confirm cache...')

        if os.environ['FANTASY_ACTIVE_CACHE'] != 'no':
            from flask_caching import Cache
            app.cache = Cache(app, config=app.config)
            pass

        if track_mode:
            print('(08/14)confirm sentry...')

        if os.environ.get('FANTASY_ACTIVE_SENTRY') != 'no':
            from raven.contrib.flask import Sentry
            Sentry(app)
            pass

        if track_mode:
            print('(09/14)active app...')

        if hasattr(mod, 'run_app'):
            run_app = getattr(mod, 'run_app')

            try:
                run_app(app)
            except Exception as e:
                if hasattr(app, 'sentry'):
                    app.sentry.handle_exception(e)
                    pass

                import sys
                import traceback
                traceback.print_exc()
                sys.exit(-1)

            pass

        if active_db and app.db:
            if track_mode:
                print('(10/14)trigger auto migrate...')

            smart_database(app)
            smart_migrate(app, migrations_root)
            smart_account(app)
            app.db.init_app(app)

            @app.teardown_request
            def session_clear(exception=None):
                if exception and app.db.session.is_active:
                    app.db.session.rollback()
                    app.db.session.remove()

            pass

        if track_mode:
            print('(11/14)bind error handle...')

        # 添加错误控制
        @parser.error_handler
        def h_webargs(error):
            return error_handler.webargs_error(error)

        @app.errorhandler(422)
        def h_422(error):
            return error_handler.http422(error)

        @app.errorhandler(500)
        def h_500(error):
            return error_handler.http500(error)

        if hasattr(mod, 'error_handler'):
            error_handle = getattr(mod, 'error_handle')
            error_handle(app)
            pass

        if track_mode:
            print('(12/14)bind admin handle...')

        if hasattr(mod, 'run_admin'):
            import flask_admin
            admin = flask_admin.Admin(name=os.environ.get('FANTASY_ADMIN_NAME',
                                                          'Admin'),
                                      template_mode=os.environ.get(
                                          'FANTASY_ADMIN_TEMPLATE_MODE',
                                          'bootstrap3'))

            run_admin = getattr(mod, 'run_admin')
            run_admin(admin)
            admin.init_app(app)
            pass

        pass

    if track_mode:
        print('(13/14)bind ff command...')

    app.cli.add_command(cli.ff)

    if track_mode:
        print('(14/14)bind cli command...')

    if hasattr(mod, 'run_cli'):
        run_cli = getattr(mod, 'run_cli')
        run_cli(app)
        pass

    return app

Response: create an app with the given name


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_config(app, prefix='hive_'):
    items = app.config.items()
    prefix = prefix.upper()

    def strip_prefix(tup):
        return (tup[0].replace(prefix, ''), tup[1])

    return dict([strip_prefix(i) for i in items if i[0].startswith(prefix)])

Response: Conveniently get the security configuration for the specified application without the annoying SECURITY_ prefix.


Instruction:
Implement a Python 3 function for
getting a Flask - Security configuration value.

Response:
def config_value(key, app=None, default=None, prefix='hive_'):
    """Get a Flask-Security configuration value.

    :param key: The configuration key without the prefix `SECURITY_`
    :param app: An optional specific application to inspect. Defaults to
                Flask's `current_app`
    :param default: An optional default value if the value is not set
    """
    app = app or current_app
    return get_config(app, prefix=prefix).get(key.upper(), default)


Instruction:
Can you write a function in Python 3 where it
creates a new vector with the given members.

Response:
def vector(members: Iterable[T], meta: Optional[IPersistentMap] = None) -> Vector[T]:
    """Creates a new vector."""
    return Vector(pvector(members), meta=meta)


Instruction:
Make a summary of the following Python 3 code
def v(*members: T, meta: Optional[IPersistentMap] = None) -> Vector[T]:
    return Vector(pvector(members), meta=meta)

Response: Creates a new vector from members.


Instruction:
Can you generate the documentation for the following Python 3 function
def eval_file(filename: str, ctx: compiler.CompilerContext, module: types.ModuleType):
    last = None
    for form in reader.read_file(filename, resolver=runtime.resolve_alias):
        last = compiler.compile_and_exec_form(form, ctx, module)
    return last

Response: Evaluate a file with the given name into a Python module AST node.


Instruction:
How would you code a function in Python 3 to
evaluate the forms in stdin into a Python module AST node.

Response:
def eval_stream(stream, ctx: compiler.CompilerContext, module: types.ModuleType):
    """Evaluate the forms in stdin into a Python module AST node."""
    last = None
    for form in reader.read(stream, resolver=runtime.resolve_alias):
        last = compiler.compile_and_exec_form(form, ctx, module)
    return last


Instruction:
Can you generate a brief explanation for the following Python 3 code
def eval_str(s: str, ctx: compiler.CompilerContext, module: types.ModuleType, eof: Any):
    last = eof
    for form in reader.read_str(s, resolver=runtime.resolve_alias, eof=eof):
        last = compiler.compile_and_exec_form(form, ctx, module)
    return last

Response: Evaluate the forms in a string into a Python module AST node.


Instruction:
Given the following Python 3 function, write the documentation
def bootstrap_repl(which_ns: str) -> types.ModuleType:
    repl_ns = runtime.Namespace.get_or_create(sym.symbol("basilisp.repl"))
    ns = runtime.Namespace.get_or_create(sym.symbol(which_ns))
    repl_module = importlib.import_module("basilisp.repl")
    ns.add_alias(sym.symbol("basilisp.repl"), repl_ns)
    ns.refer_all(repl_ns)
    return repl_module

Response:
Bootstrap the REPL with a few useful vars and returned the REPL
    bootstrapped module so it s functions can be used by the REPL
    command.


Instruction:
Implement a function in Python 3 to
run a Basilisp script or a line of code.

Response:
def run(  # pylint: disable=too-many-arguments
    file_or_code,
    code,
    in_ns,
    use_var_indirection,
    warn_on_shadowed_name,
    warn_on_shadowed_var,
    warn_on_var_indirection,
):
    """Run a Basilisp script or a line of code, if it is provided."""
    basilisp.init()
    ctx = compiler.CompilerContext(
        filename=CLI_INPUT_FILE_PATH
        if code
        else (
            STDIN_INPUT_FILE_PATH if file_or_code == STDIN_FILE_NAME else file_or_code
        ),
        opts={
            compiler.WARN_ON_SHADOWED_NAME: warn_on_shadowed_name,
            compiler.WARN_ON_SHADOWED_VAR: warn_on_shadowed_var,
            compiler.USE_VAR_INDIRECTION: use_var_indirection,
            compiler.WARN_ON_VAR_INDIRECTION: warn_on_var_indirection,
        },
    )
    eof = object()

    with runtime.ns_bindings(in_ns) as ns:
        if code:
            print(runtime.lrepr(eval_str(file_or_code, ctx, ns.module, eof)))
        elif file_or_code == STDIN_FILE_NAME:
            print(
                runtime.lrepr(
                    eval_stream(click.get_text_stream("stdin"), ctx, ns.module)
                )
            )
        else:
            print(runtime.lrepr(eval_file(file_or_code, ctx, ns.module)))


Instruction:
How would you explain what the following Python 3 function does
def multifn(dispatch: DispatchFunction, default=None) -> MultiFunction[T]:
    name = sym.symbol(dispatch.__qualname__, ns=dispatch.__module__)
    return MultiFunction(name, dispatch, default)

Response: Decorator function which can be used to make Python multi functions.


Instruction:
Explain what the following Python 3 code does
def __add_method(m: lmap.Map, key: T, method: Method) -> lmap.Map:
        return m.assoc(key, method)

Response: Swap the methods atom to include method with key.


Instruction:
Create a Python 3 function to
add a new method to this function which will respond for key returned from the dispatch function.

Response:
def add_method(self, key: T, method: Method) -> None:
        """Add a new method to this function which will respond for
        key returned from the dispatch function."""
        self._methods.swap(MultiFunction.__add_method, key, method)


Instruction:
Explain what the following Python 3 code does
def get_method(self, key: T) -> Optional[Method]:
        method_cache = self.methods
        # The 'type: ignore' comment below silences a spurious MyPy error
        # about having a return statement in a method which does not return.
        return Maybe(method_cache.entry(key, None)).or_else(
            lambda: method_cache.entry(self._default, None)  # type: ignore
        )

Response:
Return the method which would handle this dispatch key or
            None if no method defined for this dispatch key and no default.


Instruction:
Write a Python 3 script to
swap the methods atom to remove method with key.

Response:
def __remove_method(m: lmap.Map, key: T) -> lmap.Map:
        """Swap the methods atom to remove method with key."""
        return m.dissoc(key)


Instruction:
Can you implement a function in Python 3 that
removes the method defined for this key and return it.

Response:
def remove_method(self, key: T) -> Optional[Method]:
        """Remove the method defined for this key and return it."""
        method = self.methods.entry(key, None)
        if method:
            self._methods.swap(MultiFunction.__remove_method, key)
        return method


Instruction:
Write a Python 3 function that can
return True if the meta contains async keyword.

Response:
def _is_async(o: IMeta) -> bool:
    """Return True if the meta contains :async keyword."""
    return (  # type: ignore
        Maybe(o.meta)
        .map(lambda m: m.entry(SYM_ASYNC_META_KEY, None))
        .or_else_get(False)
    )


Instruction:
Here you have a function in Python 3, explain what it does
def _is_macro(v: Var) -> bool:
    return (
        Maybe(v.meta)
        .map(lambda m: m.entry(SYM_MACRO_META_KEY, None))  # type: ignore
        .or_else_get(False)
    )

Response: Return True if the Var holds a macro function.


Instruction:
Can you tell what is the following Python 3 function doing
def _loc(form: Union[LispForm, ISeq]) -> Optional[Tuple[int, int]]:
    try:
        meta = form.meta  # type: ignore
        line = meta.get(reader.READER_LINE_KW)  # type: ignore
        col = meta.get(reader.READER_COL_KW)  # type: ignore
    except AttributeError:
        return None
    else:
        assert isinstance(line, int) and isinstance(col, int)
        return line, col

Response:
Fetch the location of the form in the original filename from the the
    input form if it has metadata.


Instruction:
Can you generate the documentation for the following Python 3 function
def _with_loc(f: ParseFunction):

    @wraps(f)
    def _parse_form(ctx: ParserContext, form: Union[LispForm, ISeq]) -> Node:
        form_loc = _loc(form)
        if form_loc is None:
            return f(ctx, form)
        else:
            return f(ctx, form).fix_missing_locations(form_loc)

    return _parse_form

Response:
Attach any available location information from the input form to
    the node environment returned from the parsing function returned from the parsing function.


Instruction:
Can you create a Python 3 function that
removes reader metadata from the form s meta map.

Response:
def _clean_meta(meta: Optional[lmap.Map]) -> Optional[lmap.Map]:
    """Remove reader metadata from the form's meta map."""
    if meta is None:
        return None
    else:
        new_meta = meta.dissoc(reader.READER_LINE_KW, reader.READER_COL_KW)
        return None if len(new_meta) == 0 else new_meta


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

    @wraps(gen_node)
    def with_meta(
        ctx: ParserContext,
        form: Union[llist.List, lmap.Map, ISeq, lset.Set, vec.Vector],
    ) -> Node:
        assert not ctx.is_quoted, "with-meta nodes are not used in quoted expressions"

        descriptor = gen_node(ctx, form)

        if isinstance(form, IMeta):
            assert isinstance(form.meta, (lmap.Map, type(None)))
            form_meta = _clean_meta(form.meta)
            if form_meta is not None:
                meta_ast = _parse_ast(ctx, form_meta)
                assert isinstance(meta_ast, MapNode) or (
                    isinstance(meta_ast, Const) and meta_ast.type == ConstType.MAP
                )
                return WithMeta(
                    form=form, meta=meta_ast, expr=descriptor, env=ctx.get_node_env()
                )

        return descriptor

    return with_meta

Response: Wraps the node generated by gen_node in a WithMeta AST node if the original form has meta.


Instruction:
Create a Python 3 function to
roll up deftype* declared bases and method implementations.

Response:
def __deftype_impls(  # pylint: disable=too-many-branches
    ctx: ParserContext, form: ISeq
) -> Tuple[List[DefTypeBase], List[Method]]:
    """Roll up deftype* declared bases and method implementations."""
    current_interface_sym: Optional[sym.Symbol] = None
    current_interface: Optional[DefTypeBase] = None
    interfaces = []
    methods: List[Method] = []
    interface_methods: MutableMapping[sym.Symbol, List[Method]] = {}
    for elem in form:
        if isinstance(elem, sym.Symbol):
            if current_interface is not None:
                if current_interface_sym in interface_methods:
                    raise ParserException(
                        f"deftype* forms may only implement an interface once",
                        form=elem,
                    )
                assert (
                    current_interface_sym is not None
                ), "Symbol must be defined with interface"
                interface_methods[current_interface_sym] = methods

            current_interface_sym = elem
            current_interface = _parse_ast(ctx, elem)
            methods = []

            if not isinstance(current_interface, (MaybeClass, MaybeHostForm, VarRef)):
                raise ParserException(
                    f"deftype* interface implementation must be an existing interface",
                    form=elem,
                )
            interfaces.append(current_interface)
        elif isinstance(elem, ISeq):
            if current_interface is None:
                raise ParserException(
                    f"deftype* method cannot be declared without interface", form=elem
                )
            methods.append(__deftype_method(ctx, elem, current_interface))
        else:
            raise ParserException(
                f"deftype* must consist of interface or protocol names and methods",
                form=elem,
            )

    if current_interface is not None:
        if len(methods) > 0:
            if current_interface_sym in interface_methods:
                raise ParserException(
                    f"deftype* forms may only implement an interface once",
                    form=current_interface_sym,
                )
            assert (
                current_interface_sym is not None
            ), "Symbol must be defined with interface"
            interface_methods[current_interface_sym] = methods
        else:
            raise ParserException(
                f"deftype* may not declare interface without at least one method",
                form=current_interface_sym,
            )

    return interfaces, list(chain.from_iterable(interface_methods.values()))


Instruction:
How would you implement a function in Python 3 that
asserts that the node is not a cur node.

Response:
def _assert_no_recur(node: Node) -> None:
    """Assert that `recur` forms do not appear in any position of this or
    child AST nodes."""
    if node.op == NodeOp.RECUR:
        raise ParserException(
            "recur must appear in tail position", form=node.form, lisp_ast=node
        )
    elif node.op in {NodeOp.FN, NodeOp.LOOP}:
        pass
    else:
        node.visit(_assert_no_recur)