Instruction:
Implement a Python 3 function for
updating the classification annotations and summaries of the given object

Response:
def update_classification_annotations_and_summaries(
            self,
            updatePeakMagnitudes=True):
        """*update classification annotations and summaries*

        **Key Arguments:**
            - ``updatePeakMagnitudes`` -- update the peak magnitudes in the annotations to give absolute magnitudes. Default *True*

            **Return:**
            - None

        **Usage:**
            ..  todo::

                - add usage info
                - create a sublime snippet for usage
                - write a command-line tool for this method
                - update package tutorial with command-line tool info if needed

            .. code-block:: python

                usage code

        .. todo ::

            - update key arguments values and definitions with defaults
            - update return values and definitions
            - update usage examples and text
            - update docstring text
            - check sublime snippet exists
            - clip any useful text to docs mindmap
            - regenerate the docs and check redendering of this docstring

        """
        self.log.debug(
            'starting the ``update_classification_annotations_and_summaries`` method')

        # import time
        # start_time = time.time()
        # print "COLLECTING TRANSIENTS WITH NO ANNOTATIONS"

        if updatePeakMagnitudes:
            sqlQuery = u"""
                SELECT * from sherlock_crossmatches cm, sherlock_classifications cl where rank =1 and cl.transient_object_id=cm.transient_object_id and (cl.annotation is null orcl.dateLastModified is null or cl.dateLastModified > DATE_SUB(NOW(), INTERVAL 30 DAY)) order by  cl.dateLastModified asc limit 100000
            """ % locals()
        else:
            sqlQuery = u"""
                SELECT * from sherlock_crossmatches cm, sherlock_classifications cl where rank =1 and cl.transient_object_id=cm.transient_object_id and cl.summary is null
            """ % locals()

        rows = readquery(
            log=self.log,
            sqlQuery=sqlQuery,
            dbConn=self.transientsDbConn,
            quiet=False
        )

        # print "FINISHED COLLECTING TRANSIENTS WITH NO ANNOTATIONS/GENERATING ANNOTATIONS: %d" % (time.time() - start_time,)
        # start_time = time.time()

        from astrocalc.coords import unit_conversion
        # ASTROCALC UNIT CONVERTER OBJECT
        converter = unit_conversion(
            log=self.log
        )

        updates = []

        for row in rows:

            catalogue = row["catalogue_table_name"]
            objectId = row["catalogue_object_id"]
            objectType = row["catalogue_object_type"]
            objectSubtype = row["catalogue_object_subtype"]
            catalogueString = catalogue
            if "catalogue" not in catalogueString.lower():
                catalogueString = catalogue + " catalogue"
            if "/" in catalogueString:
                catalogueString += "s"

            if "ned" in catalogue.lower() and "/" not in catalogue:
                objectId = '''%(objectId)s''' % locals()
            elif "sdss" in catalogue.lower() and "/" not in catalogue:
                objectId = "http://skyserver.sdss.org/dr12/en/tools/explore/Summary.aspx?id=%(objectId)s" % locals(
                )

                ra = converter.ra_decimal_to_sexegesimal(
                    ra=row["raDeg"],
                    delimiter=""
                )
                dec = converter.dec_decimal_to_sexegesimal(
                    dec=row["decDeg"],
                    delimiter=""
                )
                betterName = "SDSS J" + ra[0:9] + dec[0:9]
                objectId = '''%(betterName)s''' % locals()
            elif "milliquas" in catalogue.lower() and "/" not in catalogue:
                thisName = objectId
                objectId = objectId.replace(" ", "+")
                objectId = '''<a href="https://heasarc.gsfc.nasa.gov/db-perl/W3Browse/w3table.pl?popupFrom=Query+Results&tablehead=name%%3Dheasarc_milliquas%%26description%%3DMillion+Quasars+Catalog+%%28MILLIQUAS%%29%%2C+Version+4.8+%%2822+June+2016%%29%%26url%%3Dhttp%%3A%%2F%%2Fheasarc.gsfc.nasa.gov%%2FW3Browse%%2Fgalaxy-catalog%%2Fmilliquas.html%%26archive%%3DN%%26radius%%3D1%%26mission%%3DGALAXY+CATALOG%%26priority%%3D5%%26tabletype%%3DObject&dummy=Examples+of+query+constraints%%3A&varon=name&bparam_name=%%3D%%22%(objectId)s%%22&bparam_name%%3A%%3Aunit=+&bparam_name%%3A%%3Aformat=char25&varon=ra&bparam_ra=&bparam_ra%%3A%%3Aunit=degree&bparam_ra%%3A%%3Aformat=float8%%3A.5f&varon=dec&bparam_dec=&bparam_dec%%3A%%3Aunit=degree&bparam_dec%%3A%%3Aformat=float8%%3A.5f&varon=bmag&bparam_bmag=&bparam_bmag%%3A%%3Aunit=mag&bparam_bmag%%3A%%3Aformat=float8%%3A4.1f&varon=rmag&bparam_rmag=&bparam_rmag%%3A%%3Aunit=mag&bparam_rmag%%3A%%3Aformat=float8%%3A4.1f&varon=redshift&bparam_redshift=&bparam_redshift%%3A%%3Aunit=+&bparam_redshift%%3A%%3Aformat=float8%%3A6.3f&varon=radio_name&bparam_radio_name=&bparam_radio_name%%3A%%3Aunit=+&bparam_radio_name%%3A%%3Aformat=char22&varon=xray_name&bparam_xray_name=&bparam_xray_name%%3A%%3Aunit=+&bparam_xray_name%%3A%%3Aformat=char22&bparam_lii=&bparam_lii%%3A%%3Aunit=degree&bparam_lii%%3A%%3Aformat=float8%%3A.5f&bparam_bii=&bparam_bii%%3A%%3Aunit=degree&bparam_bii%%3A%%3Aformat=float8%%3A.5f&bparam_broad_type=&bparam_broad_type%%3A%%3Aunit=+&bparam_broad_type%%3A%%3Aformat=char4&bparam_optical_flag=&bparam_optical_flag%%3A%%3Aunit=+&bparam_optical_flag%%3A%%3Aformat=char3&bparam_red_psf_flag=&bparam_red_psf_flag%%3A%%3Aunit=+&bparam_red_psf_flag%%3A%%3Aformat=char1&bparam_blue_psf_flag=&bparam_blue_psf_flag%%3A%%3Aunit=+&bparam_blue_psf_flag%%3A%%3Aformat=char1&bparam_ref_name=&bparam_ref_name%%3A%%3Aunit=+&bparam_ref_name%%3A%%3Aformat=char6&bparam_ref_redshift=&bparam_ref_redshift%%3A%%3Aunit=+&bparam_ref_redshift%%3A%%3Aformat=char6&bparam_qso_prob=&bparam_qso_prob%%3A%%3Aunit=percent&bparam_qso_prob%%3A%%3Aformat=int2%%3A3d&bparam_alt_name_1=&bparam_alt_name_1%%3A%%3Aunit=+&bparam_alt_name_1%%3A%%3Aformat=char22&bparam_alt_name_2=&bparam_alt_name_2%%3A%%3Aunit=+&bparam_alt_name_2%%3A%%3Aformat=char22&Entry=&Coordinates=J2000&Radius=Default&Radius_unit=arcsec&NR=CheckCaches%%2FGRB%%2FSIMBAD%%2BSesame%%2FNED&Time=&ResultMax=1000&displaymode=Display&Action=Start+Search&table=heasarc_milliquas">%(thisName)s''' % locals()

            if objectSubtype and objectSubtype.lower() in ["uvs", "radios", "xray", "qso", "irs", 'uves', 'viss', 'hii', 'gclstr', 'ggroup', 'gpair', 'gtrpl']:
                objectType = objectSubtype

            if objectType == "star":
                objectType = "stellar source"
            elif objectType == "agn":
                objectType = "AGN"
            elif objectType == "cb":
                objectType = "CV"
            elif objectType == "unknown":
                objectType = "unclassified source"

            sep = row["separationArcsec"]
            if row["classificationReliability"] == 1:
                classificationReliability = "synonymous"
                psep = row["physical_separation_kpc"]
                if psep:
                    location = '%(sep)0.1f" (%(psep)0.1f Kpc) from the %(objectType)s core' % locals(
                    )
                else:
                    location = '%(sep)0.1f" from the %(objectType)s core' % locals(
                    )
            elif row["classificationReliability"] in (2, 3):
                classificationReliability = "possibly associated"
                n = row["northSeparationArcsec"]
                if n > 0:
                    nd = "S"
                else:
                    nd = "N"
                e = row["eastSeparationArcsec"]
                if e > 0:
                    ed = "W"
                else:
                    ed = "E"
                n = math.fabs(n)
                e = math.fabs(e)
                psep = row["physical_separation_kpc"]
                if psep:
                    location = '%(n)0.2f" %(nd)s, %(e)0.2f" %(ed)s (%(psep)0.1f Kpc) from the %(objectType)s centre' % locals(
                    )
                else:
                    location = '%(n)0.2f" %(nd)s, %(e)0.2f" %(ed)s from the %(objectType)s centre' % locals(
                    )
                location = location.replace("unclassified", "object's")

            best_mag = None
            best_mag_error = None
            best_mag_filter = None
            filters = ["R", "V", "B", "I", "J", "G", "H", "K", "U",
                       "_r", "_g", "_i", "_g", "_z", "_y", "_u", "unkMag"]
            for f in filters:
                if row[f] and not best_mag:
                    best_mag = row[f]
                    best_mag_error = row[f + "Err"]
                    subfilter = f.replace(
                        "_", "").replace("Mag", "")
                    best_mag_filter = f.replace(
                        "_", "").replace("Mag", "") + "="
                    if "unk" in best_mag_filter:
                        best_mag_filter = ""
                        subfilter = ''

            if not best_mag_filter:
                if str(best_mag).lower() in ("8", "11", "18"):
                    best_mag_filter = "an "
                else:
                    best_mag_filter = "a "
            else:
                if str(best_mag_filter)[0].lower() in ("r", "i", "h"):
                    best_mag_filter = "an " + best_mag_filter
                else:
                    best_mag_filter = "a " + best_mag_filter
            if not best_mag:
                best_mag = "an unknown-"
                best_mag_filter = ""
            else:
                best_mag = "%(best_mag)0.2f " % locals()

            distance = None
            if row["direct_distance"]:
                d = row["direct_distance"]
                distance = "distance of %(d)0.1f Mpc" % locals()

                if row["z"]:
                    z = row["z"]
                    distance += "(z=%(z)0.3f)" % locals()
            elif row["z"]:
                z = row["z"]
                distance = "z=%(z)0.3f" % locals()
            elif row["photoZ"]:
                z = row["photoZ"]
                zErr = row["photoZErr"]
                distance = "photoZ=%(z)0.3f (&plusmn%(zErr)0.3f)" % locals()

            if distance:
                distance = "%(distance)s" % locals()

            distance_modulus = None
            if row["direct_distance_modulus"]:
                distance_modulus = row["direct_distance_modulus"]
            elif row["distance_modulus"]:
                distance_modulus = row["distance_modulus"]

            if updatePeakMagnitudes:
                if distance:
                    absMag = row["transientAbsMag"]
                    absMag = """ A host %(distance)s implies a transient M = %(absMag)s.""" % locals(
                    )
                else:
                    absMag = ""
            else:
                if distance and distance_modulus:
                    absMag = "%(distance_modulus)0.2f" % locals()
                    absMag = """ A host %(distance)s implies a m - M = %(absMag)s.""" % locals(
                    )
                else:
                    absMag = ""

            annotation = "The transient is %(classificationReliability)s with %(objectId)s; %(best_mag_filter)s%(best_mag)smag %(objectType)s found in the %(catalogueString)s. It's located %(location)s.%(absMag)s" % locals()
            summary = '%(sep)0.1f" from %(objectType)s in %(catalogue)s' % locals(
            )

            update = {
                "transient_object_id": row["transient_object_id"],
                "annotation": annotation,
                "summary": summary,
                "separationArcsec": sep
            }
            updates.append(update)

        # print "FINISHED GENERATING ANNOTATIONS/ADDING ANNOTATIONS TO TRANSIENT DATABASE: %d" % (time.time() - start_time,)
        # start_time = time.time()

        insert_list_of_dictionaries_into_database_tables(
            dbConn=self.transientsDbConn,
            log=self.log,
            dictList=updates,
            dbTableName="sherlock_classifications",
            dateModified=True,
            batchSize=10000,
            replace=True,
            dbSettings=self.settings["database settings"]["transients"]
        )

        # print "FINISHED ADDING ANNOTATIONS TO TRANSIENT DATABASE/UPDATING ORPHAN ANNOTATIONS: %d" % (time.time() - start_time,)
        # start_time = time.time()

        sqlQuery = """update sherlock_classifications  set annotation = "The transient location is not matched against any known catalogued source", summary = "No catalogued match" where classification = 'ORPHAN'  and summary is null """ % locals()
        writequery(
            log=self.log,
            sqlQuery=sqlQuery,
            dbConn=self.transientsDbConn,
        )

        # print "FINISHED UPDATING ORPHAN ANNOTATIONS: %d" % (time.time() - start_time,)
        # start_time = time.time()

        self.log.debug(
            'completed the ``update_classification_annotations_and_summaries`` method')
        return None


Instruction:
Can you tell what is the following Python 3 function doing
def update_peak_magnitudes(
            self):
        """*update peak magnitudes*

        **Key Arguments:**
            # -

        **Return:**
            - None

        **Usage:**
            ..  todo::

                - add usage info
                - create a sublime snippet for usage
                - write a command-line tool for this method
                - update package tutorial with command-line tool info if needed

            .. code-block:: python

                usage code

        .. todo ::

            - update key arguments values and definitions with defaults
            - update return values and definitions
            - update usage examples and text
            - update docstring text
            - check sublime snippet exists
            - clip any useful text to docs mindmap
            - regenerate the docs and check redendering of this docstring

        """
        self.log.debug('starting the ``update_peak_magnitudes`` method')

        sqlQuery = self.settings["database settings"][
            "transients"]["transient peak magnitude query"]

        sqlQuery = """UPDATE sherlock_crossmatches s,
            (%(sqlQuery)s) t
        SET
            s.transientAbsMag = ROUND(t.mag - IFNULL(direct_distance_modulus,
                            distance_modulus),
                    2)
        WHERE
            IFNULL(direct_distance_modulus,
                    distance_modulus) IS NOT NULL
                AND t.id = s.transient_object_id;""" % locals()

        writequery(
            log=self.log,
            sqlQuery=sqlQuery,
            dbConn=self.transientsDbConn,
        )

        self.log.debug('completed the ``update_peak_magnitudes`` method')
        return None

Response:
*update peak magnitudes*

        **Key Arguments:**
            # -

        **Return:**
            - None

        **Usage:**
            ..  todo::

                - add usage info
                - create a sublime snippet for usage
                - write a command-line tool for this method
                - update package tutorial with command-line tool info if needed

            .. code-block:: python

                usage code

        .. todo ::

            - update key arguments values and definitions with defaults
            - update return values and definitions
            - update usage examples and text
            - update docstring text
            - check sublime snippet exists
            - clip any useful text to docs mindmap
            - regenerate the docs and check redendering of this docstring


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_tables_if_not_exist(
            self):
        """*create the sherlock helper tables if they don't yet exist*

        **Key Arguments:**
            # -

        **Return:**
            - None

        **Usage:**
            ..  todo::

                - add usage info
                - create a sublime snippet for usage
                - write a command-line tool for this method
                - update package tutorial with command-line tool info if needed

            .. code-block:: python

                usage code

        .. todo ::

            - update key arguments values and definitions with defaults
            - update return values and definitions
            - update usage examples and text
            - update docstring text
            - check sublime snippet exists
            - clip any useful text to docs mindmap
            - regenerate the docs and check redendering of this docstring

        """
        self.log.debug('starting the ``_create_tables_if_not_exist`` method')

        transientTable = self.settings["database settings"][
            "transients"]["transient table"]
        transientTableClassCol = self.settings["database settings"][
            "transients"]["transient classification column"]
        transientTableIdCol = self.settings["database settings"][
            "transients"]["transient primary id column"]

        crossmatchTable = "sherlock_crossmatches"
        createStatement = """
CREATE TABLE IF NOT EXISTS `%(crossmatchTable)s` (
  `transient_object_id` bigint(20) unsigned DEFAULT NULL,
  `catalogue_object_id` varchar(30) DEFAULT NULL,
  `catalogue_table_id` smallint(5) unsigned DEFAULT NULL,
  `separationArcsec` double DEFAULT NULL,
  `northSeparationArcsec` DOUBLE DEFAULT NULL,
  `eastSeparationArcsec` DOUBLE DEFAULT NULL,
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `z` double DEFAULT NULL,
  `scale` double DEFAULT NULL,
  `distance` double DEFAULT NULL,
  `distance_modulus` double DEFAULT NULL,
  `photoZ` double DEFAULT NULL,
  `photoZErr` double DEFAULT NULL,
  `association_type` varchar(45) DEFAULT NULL,
  `dateCreated` datetime DEFAULT CURRENT_TIMESTAMP,
  `physical_separation_kpc` double DEFAULT NULL,
  `catalogue_object_type` varchar(45) DEFAULT NULL,
  `catalogue_object_subtype` varchar(45) DEFAULT NULL,
  `association_rank` int(11) DEFAULT NULL,
  `catalogue_table_name` varchar(100) DEFAULT NULL,
  `catalogue_view_name` varchar(100) DEFAULT NULL,
  `rank` int(11) DEFAULT NULL,
  `rankScore` double DEFAULT NULL,
  `search_name` varchar(100) DEFAULT NULL,
  `major_axis_arcsec` double DEFAULT NULL,
  `direct_distance` double DEFAULT NULL,
  `direct_distance_scale` double DEFAULT NULL,
  `direct_distance_modulus` double DEFAULT NULL,
  `raDeg` double DEFAULT NULL,
  `decDeg` double DEFAULT NULL,
  `original_search_radius_arcsec` double DEFAULT NULL,
  `catalogue_view_id` int(11) DEFAULT NULL,
  `U` double DEFAULT NULL,
  `UErr` double DEFAULT NULL,
  `B` double DEFAULT NULL,
  `BErr` double DEFAULT NULL,
  `V` double DEFAULT NULL,
  `VErr` double DEFAULT NULL,
  `R` double DEFAULT NULL,
  `RErr` double DEFAULT NULL,
  `I` double DEFAULT NULL,
  `IErr` double DEFAULT NULL,
  `J` double DEFAULT NULL,
  `JErr` double DEFAULT NULL,
  `H` double DEFAULT NULL,
  `HErr` double DEFAULT NULL,
  `K` double DEFAULT NULL,
  `KErr` double DEFAULT NULL,
  `_u` double DEFAULT NULL,
  `_uErr` double DEFAULT NULL,
  `_g` double DEFAULT NULL,
  `_gErr` double DEFAULT NULL,
  `_r` double DEFAULT NULL,
  `_rErr` double DEFAULT NULL,
  `_i` double DEFAULT NULL,
  `_iErr` double DEFAULT NULL,
  `_z` double DEFAULT NULL,
  `_zErr` double DEFAULT NULL,
  `_y` double DEFAULT NULL,
  `_yErr` double DEFAULT NULL,
  `G` double DEFAULT NULL,
  `GErr` double DEFAULT NULL,
  `unkMag` double DEFAULT NULL,
  `unkMagErr` double DEFAULT NULL,
  `dateLastModified` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated` TINYINT NULL DEFAULT 0,
  `classificationReliability` TINYINT NULL DEFAULT NULL,
  `transientAbsMag` DOUBLE NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `key_transient_object_id` (`transient_object_id`),
  KEY `key_catalogue_object_id` (`catalogue_object_id`),
  KEY `idx_separationArcsec` (`separationArcsec`),
  KEY `idx_rank` (`rank`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=8;

CREATE TABLE IF NOT EXISTS `sherlock_classifications` (
  `transient_object_id` bigint(20) NOT NULL,
  `classification` varchar(45) DEFAULT NULL,
  `annotation` TEXT COLLATE utf8_unicode_ci DEFAULT NULL,
  `summary` VARCHAR(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `separationArcsec` DOUBLE DEFAULT NULL,
  `matchVerified` TINYINT NULL DEFAULT NULL,
  `developmentComment` VARCHAR(100) NULL,
  `dateLastModified` datetime DEFAULT CURRENT_TIMESTAMP,
  `dateCreated` datetime DEFAULT CURRENT_TIMESTAMP,
  `updated` varchar(45) DEFAULT '0',
  PRIMARY KEY (`transient_object_id`),
  KEY `key_transient_object_id` (`transient_object_id`),
  KEY `idx_summary` (`summary`),
  KEY `idx_classification` (`classification`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

""" % locals()

        # A FIX FOR MYSQL VERSIONS < 5.6
        triggers = []
        if float(self.dbVersions["transients"][:3]) < 5.6:
            createStatement = createStatement.replace(
                "`dateLastModified` datetime DEFAULT CURRENT_TIMESTAMP,", "`dateLastModified` datetime DEFAULT NULL,")
            createStatement = createStatement.replace(
                "`dateCreated` datetime DEFAULT CURRENT_TIMESTAMP,", "`dateCreated` datetime DEFAULT NULL,")

            triggers.append("""
CREATE TRIGGER dateCreated
BEFORE INSERT ON `%(crossmatchTable)s`
FOR EACH ROW
BEGIN
   IF NEW.dateCreated IS NULL THEN
      SET NEW.dateCreated = NOW();
      SET NEW.dateLastModified = NOW();
   END IF;
END""" % locals())

        try:
            writequery(
                log=self.log,
                sqlQuery=createStatement,
                dbConn=self.transientsDbConn,
                Force=True
            )
        except:
            self.log.info(
                "Could not create table (`%(crossmatchTable)s`). Probably already exist." % locals())

        sqlQuery = u"""
            SHOW TRIGGERS;
        """ % locals()
        rows = readquery(
            log=self.log,
            sqlQuery=sqlQuery,
            dbConn=self.transientsDbConn,
        )

        # DON'T ADD TRIGGERS IF THEY ALREADY EXIST
        for r in rows:
            if r["Trigger"] in ("sherlock_classifications_BEFORE_INSERT", "sherlock_classifications_AFTER_INSERT"):
                return None

        triggers.append("""CREATE TRIGGER `sherlock_classifications_BEFORE_INSERT` BEFORE INSERT ON `sherlock_classifications` FOR EACH ROW
BEGIN
    IF new.classification = "ORPHAN" THEN
        SET new.annotation = "The transient location is not matched against any known catalogued source", new.summary = "No catalogued match";
    END IF;
END""" % locals())

        triggers.append("""CREATE TRIGGER `sherlock_classifications_AFTER_INSERT` AFTER INSERT ON `sherlock_classifications` FOR EACH ROW
BEGIN
    update `%(transientTable)s` set `%(transientTableClassCol)s` = new.classification
                        where `%(transientTableIdCol)s`  = new.transient_object_id;
END""" % locals())

        for t in triggers:
            try:
                writequery(
                    log=self.log,
                    sqlQuery=t,
                    dbConn=self.transientsDbConn,
                    Force=True
                )
            except:
                self.log.info(
                    "Could not create trigger (`%(crossmatchTable)s`). Probably already exist." % locals())

        self.log.debug('completed the ``_create_tables_if_not_exist`` method')
        return None

Response: create the sherlock helper tables if they don t yet exist


Instruction:
Here you have a function in Python 3, explain what it does
def check(context: click.Context, family: str):
    analysis_obj = context.obj['store'].analyses(family=family).first()
    if analysis_obj is None:
        LOG.error('no analysis found')
        context.abort()

    config_path = Path(analysis_obj.config_path)
    if not config_path.exists():
        LOG.error(f"analysis config not found: {config_path}")
        context.abort()
    config_raw = ruamel.yaml.safe_load(config_path.open())
    config_data = files.parse_config(config_raw)

    sampleinfo_raw = ruamel.yaml.safe_load(Path(config_data['sampleinfo_path']).open())
    sampleinfo_data = files.parse_sampleinfo(sampleinfo_raw)

    qcmetrics_path = Path(sampleinfo_data['qcmetrics_path'])
    if not qcmetrics_path.exists():
        LOG.error(f"qc metrics not found: {str(qcmetrics_path)}")
        context.abort()
    qcmetrics_raw = ruamel.yaml.safe_load(qcmetrics_path.open())
    qcmetrics_data = files.parse_qcmetrics(qcmetrics_raw)

    samples = {
        'sample': [],
        'type': [],
        'ped': [],
        'chanjo': [],
        'peddy': [],
        'plink': [],
        'duplicates': [],
    }
    for sample_data in config_data['samples']:
        LOG.debug(f"{sample_data['id']}: parse analysis config")
        samples['sample'].append(sample_data['id'])
        samples['type'].append(sample_data['type'])

    for sample_data in sampleinfo_data['samples']:
        LOG.debug(f"{sample_data['id']}: parse sample info")
        samples['ped'].append(sample_data['sex'])

        with Path(sample_data['chanjo_sexcheck']).open() as chanjo_handle:
            sexcheck_data = files.parse_chanjo_sexcheck(chanjo_handle)

        predicted_sex = sexcheck_data['predicted_sex']
        xy_ratio = sexcheck_data['y_coverage'] / sexcheck_data['x_coverage']
        samples['chanjo'].append(f"{predicted_sex} ({xy_ratio:.3f})")

    for sample_data in qcmetrics_data['samples']:
        LOG.debug(f"{sample_data['id']}: parse qc metrics")
        samples['plink'].append(sample_data['plink_sex'])
        duplicates_percent = sample_data['duplicates'] * 100
        samples['duplicates'].append(f"{duplicates_percent:.3f}%")

    peddy_path = Path(sampleinfo_data['peddy']['sex_check'])
    if peddy_path.exists():
        with peddy_path.open() as sexcheck_handle:
            peddy_data = files.parse_peddy_sexcheck(sexcheck_handle)

        for sample_id in samples['sample']:
            LOG.debug(f"{sample_id}: parse peddy")
            predicted_sex = peddy_data[sample_id]['predicted_sex']
            het_ratio = peddy_data[sample_id]['het_ratio']
            samples['peddy'].append(f"{predicted_sex} ({het_ratio})")
    else:
        LOG.warning(f"missing peddy output: {peddy_path}")

    print(tabulate(samples, headers='keys', tablefmt='psql'))

Response: Delete an analysis log from the database.


Instruction:
Explain what the following Python 3 code does
def name_file(lane: int, flowcell: str, sample: str, read: int,
                  undetermined: bool=False, date: dt.datetime=None, index: str=None) -> str:
        """Name a FASTQ file following MIP conventions."""
        flowcell = f"{flowcell}-undetermined" if undetermined else flowcell
        date_str = date.strftime('%y%m%d') if date else '171015'
        index = index if index else 'XXXXXX'
        return f"{lane}_{date_str}_{flowcell}_{sample}_{index}_{read}.fastq.gz"

Response: Name a FASTQ file following MIP conventions.


Instruction:
How would you explain what the following Python 3 function does
def link(self, family: str, sample: str, analysis_type: str, files: List[str]):
        root_dir = Path(self.families_dir) / family / analysis_type / sample / 'fastq'
        root_dir.mkdir(parents=True, exist_ok=True)
        for fastq_data in files:
            fastq_path = Path(fastq_data['path'])
            fastq_name = self.name_file(
                lane=fastq_data['lane'],
                flowcell=fastq_data['flowcell'],
                sample=sample,
                read=fastq_data['read'],
                undetermined=fastq_data['undetermined'],
            )
            dest_path = root_dir / fastq_name
            if not dest_path.exists():
                log.info(f"linking: {fastq_path} -> {dest_path}")
                dest_path.symlink_to(fastq_path)
            else:
                log.debug(f"destination path already exists: {dest_path}")

Response: Link FASTQ files for a sample.


Instruction:
Implement a Python 3 function for
getting the path to the python package

Response:
def getpackagepath():
    """
     *Get the root path for this python package - used in unit testing code*
    """
    moduleDirectory = os.path.dirname(__file__)
    packagePath = os.path.dirname(__file__) + "/../"

    return packagePath


Instruction:
Can you tell what is the following Python 3 function doing
def mcast_receiver(port, mcgroup=MC_GROUP):

    if _is_broadcast_group(mcgroup):
        group = None
    else:
        group = mcgroup

    # Create a socket
    sock = socket(AF_INET, SOCK_DGRAM)

    # Allow multiple copies of this program on one machine
    # (not strictly needed)
    sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    if group:
        sock.setsockopt(SOL_IP, IP_MULTICAST_TTL, TTL_LOCALNET)  # default
        sock.setsockopt(SOL_IP, IP_MULTICAST_LOOP, 1)  # default

    # Bind it to the port
    sock.bind(('', port))

    # Look up multicast group address in name server
    # (doesn't hurt if it is already in ddd.ddd.ddd.ddd format)
    if group:
        group = gethostbyname(group)

        # Construct binary group address
        bytes_ = [int(b) for b in group.split(".")]
        grpaddr = 0
        for byte in bytes_:
            grpaddr = (grpaddr << 8) | byte

        # Construct struct mreq from grpaddr and ifaddr
        ifaddr = INADDR_ANY
        mreq = struct.pack('!LL', grpaddr, ifaddr)

        # Add group membership
        sock.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)

    return sock, group or '<broadcast>'

Response: Open a UDP socket bind it to a port and select a multicast group.


Instruction:
Write a Python 3 function for
deleting temporary logs for the current family.

Response:
def _delete_temp_logs(self, family_name: str):
        """Delete temporary logs for the current family."""
        for temp_log in self.store.analyses(family=family_name, temp=True):
            log.debug(f"delete temporary log: {temp_log.id} - {temp_log.status}")
            temp_log.delete()


Instruction:
Can you generate the documentation for the following Python 3 function
def parse(cls, config_data: dict, sampleinfo_data: dict, sacct_jobs: List[dict],
              jobs: int) -> dict:
        """Parse information about a run."""
        analysis_types = [sample['type'] for sample in config_data['samples']]
        run_data = {
            'user': config_data['email'],
            'family': config_data['family'],
            'priority': config_data['priority'],
            'started_at': sampleinfo_data['date'],
            'version': sampleinfo_data['version'],
            'out_dir': config_data['out_dir'],
            'config_path': config_data['config_path'],
            'type': cls._get_analysis_type(analysis_types),
        }

        sacct_data, last_job_end = cls._parse_sacct(sacct_jobs, jobs_count=jobs)
        run_data.update(sacct_data)

        run_data['status'] = cls.get_status(sampleinfo_data['is_finished'],
                                            len(run_data['failed_jobs']))
        if run_data['status'] == 'completed':
            run_data['completed_at'] = last_job_end

        return run_data

Response: Parse information about a run.


Instruction:
Write a Python 3 script for
parsing out info from Sacct log.

Response:
def _parse_sacct(sacct_jobs: List[dict], jobs_count: int=None):
        """Parse out info from Sacct log."""
        failed_jobs = sacct_api.filter_jobs(sacct_jobs, failed=True)
        completed_jobs = [job for job in sacct_jobs if job['is_completed']]
        last_job_end = completed_jobs[-1]['end'] if len(completed_jobs) > 0 else None
        data = {
            'jobs': jobs_count,
            'completed_jobs': len(completed_jobs),
            'progress': (len(completed_jobs) / jobs_count) if jobs_count else None,
            'failed_jobs': [{
                'slurm_id': job['id'],
                'started_at': job['start'],
                'elapsed': job['elapsed'],
                'status': job['state'].lower(),
                'name': job['step'],
                'context': job['context'],
            } for job in failed_jobs]
        }
        return data, last_job_end


Instruction:
Implement a Python 3 function for
determining the overall analysis type.

Response:
def _get_analysis_type(analysis_types: List[str]) -> str:
        """Determine the overall analysis type."""
        types_set = set(analysis_types)
        return types_set.pop() if len(types_set) == 1 else 'wgs'


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build(self, run_data: dict) -> models.Analysis:
        existing_run = self.store.find_analysis(family=run_data['family'],
                                                started_at=run_data['started_at'],
                                                status=run_data['status'])
        if existing_run:
            return None

        run_data['user'] = self.store.user(run_data['user'])
        new_failed_jobs = [self.store.Job(**job) for job in run_data['failed_jobs']]
        del run_data['failed_jobs']
        new_run = self.store.Analysis(**run_data)
        new_run.failed_jobs = new_failed_jobs
        return new_run

Response: Build a new Analysis object.


Instruction:
Here you have a function in Python 3, explain what it does
def get_default_value(self):
        default = self.default_value
        if isinstance(default, collections.Callable):
            default = default()
        return default

Response: get the default value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _init_compiled(cls, dialect, connection, dbapi_connection,
                       compiled, parameters):
        """Initialize execution context for a Compiled construct."""

        self = cls.__new__(cls)
        self.root_connection = connection
        self._dbapi_connection = dbapi_connection
        self.dialect = connection.dialect

        self.compiled = compiled

        # this should be caught in the engine before
        # we get here
        assert compiled.can_execute

        self.execution_options = compiled.execution_options.union(
            connection._execution_options)

        self.result_column_struct = (
            compiled._result_columns, compiled._ordered_columns,
            compiled._textual_ordered_columns)

        self.unicode_statement = util.text_type(compiled)
        if not dialect.supports_unicode_statements:
            self.statement = self.unicode_statement.encode(
                self.dialect.encoding)
        else:
            self.statement = self.unicode_statement

        self.isinsert = compiled.isinsert
        self.isupdate = compiled.isupdate
        self.isdelete = compiled.isdelete
        self.is_text = compiled.isplaintext

        if not parameters:
            self.compiled_parameters = [compiled.construct_params()]
        else:
            self.compiled_parameters = \
                [compiled.construct_params(m, _group_number=grp) for
                 grp, m in enumerate(parameters)]

            self.executemany = len(parameters) > 1

        self.cursor = self.create_cursor()

        if self.isinsert or self.isupdate or self.isdelete:
            self.is_crud = True
            self._is_explicit_returning = bool(compiled.statement._returning)
            self._is_implicit_returning = bool(
                compiled.returning and not compiled.statement._returning)

        if self.compiled.insert_prefetch or self.compiled.update_prefetch:
            if self.executemany:
                self._process_executemany_defaults()
            else:
                self._process_executesingle_defaults()

        processors = compiled._bind_processors

        # Convert the dictionary of bind parameter values
        # into a dict or list to be sent to the DBAPI's
        # execute() or executemany() method.
        parameters = []
        if dialect.positional:
            for compiled_params in self.compiled_parameters:
                param = []
                for key in self.compiled.positiontup:
                    if key in processors:
                        param.append(processors[key](compiled_params[key]))
                    else:
                        param.append(compiled_params[key])
                parameters.append(dialect.execute_sequence_format(param))
        else:
            encode = not dialect.supports_unicode_statements
            for compiled_params in self.compiled_parameters:

                if encode:
                    param = dict(
                        (
                            dialect._encoder(key)[0],
                            processors[key](compiled_params[key])
                            if key in processors
                            else compiled_params[key]
                        )
                        for key in compiled_params
                    )
                else:
                    param = dict(
                        (
                            key,
                            processors[key](compiled_params[key])
                            if key in processors
                            else compiled_params[key]
                        )
                        for key in compiled_params
                    )

                parameters.append(param)
        self.parameters = dialect.execute_sequence_format(parameters)

        self.statement = compiled
        return self

Response: Initialize the Execution Context for a Compiled construct.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def split_sentences(text):
    sentence_delimiters = re.compile(u'[.!?,;:\t\\\\"\\(\\)\\\'\u2019\u2013]|\\s\\-\\s')
    sentences = sentence_delimiters.split(text)
    return sentences

Response: Utility function to return a list of sentences.


Instruction:
Given the following Python 3 function, write the documentation
def _get_action_urls(self):
        actions = {}

        model_name = self.model._meta.model_name
        # e.g.: polls_poll
        base_url_name = '%s_%s' % (self.model._meta.app_label, model_name)
        # e.g.: polls_poll_actions
        model_actions_url_name = '%s_actions' % base_url_name

        self.tools_view_name = 'admin:' + model_actions_url_name

        # WISHLIST use get_change_actions and get_changelist_actions
        # TODO separate change and changelist actions
        for action in chain(self.change_actions, self.changelist_actions):
            actions[action] = getattr(self, action)
        return [
            # change, supports the same pks the admin does
            # https://github.com/django/django/blob/stable/1.10.x/django/contrib/admin/options.py#L555
            url(r'^(?P<pk>.+)/actions/(?P<tool>\w+)/$',
                self.admin_site.admin_view(  # checks permissions
                    ChangeActionView.as_view(
                        model=self.model,
                        actions=actions,
                        back='admin:%s_change' % base_url_name,
                        current_app=self.admin_site.name,
                    )
                ),
                name=model_actions_url_name),
            # changelist
            url(r'^actions/(?P<tool>\w+)/$',
                self.admin_site.admin_view(  # checks permissions
                    ChangeListActionView.as_view(
                        model=self.model,
                        actions=actions,
                        back='admin:%s_changelist' % base_url_name,
                        current_app=self.admin_site.name,
                    )
                ),
                # Dupe name is fine. https://code.djangoproject.com/ticket/14259
                name=model_actions_url_name),
        ]

Response: Get the url patterns that route each action to a view.


Instruction:
How would you explain what the following Python 3 function does
def _get_tool_dict(self, tool_name):
        tool = getattr(self, tool_name)
        standard_attrs, custom_attrs = self._get_button_attrs(tool)
        return dict(
            name=tool_name,
            label=getattr(tool, 'label', tool_name),
            standard_attrs=standard_attrs,
            custom_attrs=custom_attrs,
        )

Response: Represents the tool as a dict with extra meta.


Instruction:
Write a Python 3 function for
getting the HTML attributes associated with a tool.

Response:
def _get_button_attrs(self, tool):
        """
        Get the HTML attributes associated with a tool.

        There are some standard attributes (class and title) that the template
        will always want. Any number of additional attributes can be specified
        and passed on. This is kinda awkward and due for a refactor for
        readability.
        """
        attrs = getattr(tool, 'attrs', {})
        # href is not allowed to be set. should an exception be raised instead?
        if 'href' in attrs:
            attrs.pop('href')
        # title is not allowed to be set. should an exception be raised instead?
        # `short_description` should be set instead to parallel django admin
        # actions
        if 'title' in attrs:
            attrs.pop('title')
        default_attrs = {
            'class': attrs.get('class', ''),
            'title': getattr(tool, 'short_description', ''),
        }
        standard_attrs = {}
        custom_attrs = {}
        for k, v in dict(default_attrs, **attrs).items():
            if k in default_attrs:
                standard_attrs[k] = v
            else:
                custom_attrs[k] = v
        return standard_attrs, custom_attrs


Instruction:
How would you code a function in Python 3 to
add a question mark.

Response:
def question_mark(self, request, obj):
        """Add a question mark."""
        obj.question = obj.question + '?'
        obj.save()


Instruction:
Can you generate the documentation for the following Python 3 function
def build_link(href, text, cls=None, icon_class=None, **attrs):
    return build_html_element(tag='a',
                              text=text,
                              href=href,
                              cls=cls,
                              icon_class=icon_class,
                              **attrs)

Response: Builds an html link.


Instruction:
How would you implement a function in Python 3 that
builds an html element.

Response:
def build_html_element(tag, text=None, icon_class=None, cls=None, **kwargs):
    """Builds an html element.

    :param tag: the html tag to build ('a', 'div', 'img', etc)
    :param icon_class: the class to apply to an icon element.  This only
        applies to elements that allow a closing tag.
    :param cls: the css class to apply to the tag.  This can also be passed
        in as a kwarg as "class".

    >>> build_html_element(tag='a', href='someurl.com', text='hello')
    'hello'
    """
    if cls is not None:
        kwargs['class'] = cls

    tag_attrs = ' '.join(['{0}="{1}"'.format(k, v) for k, v in kwargs.items()])

    tag_content = '{tag} {tag_attrs}'.format(tag=tag, tag_attrs=tag_attrs)

    if tag in ('img', 'input', 'hr', 'br'):
        return mark_safe('<{tag_content} />'.format(tag_content=tag_content))

    icon = ' '.format(icon_class) if icon_class else ''

    if not text:
        text = ''
    elif not isinstance(text, SafeText):
        text = escape(text)

    return mark_safe('<{tag_content}>{icon}{text}</{tag}>'.format(
        tag_content=tag_content,
        icon=icon,
        tag=tag,
        text=text)
    )


Instruction:
Can you tell what is the following Python 3 function doing
def dump_migration_session_state(raw):
    class BlockStyle(str): pass
    class SessionDumper(yaml.SafeDumper): pass
    def str_block_formatter(dumper, data):
        return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
    SessionDumper.add_representer(BlockStyle, str_block_formatter)

    raw = deepcopy(raw)
    for step in raw:
        step['output'] = BlockStyle(step['output'])
        step['traceback'] = BlockStyle(step['traceback'])
    return yaml.dump(raw, Dumper=SessionDumper)

Response: Serialize a migration session state to yaml using nicer formatting


Instruction:
How would you explain what the following Python 3 function does
def add_migrations(self, migrations):
        if self.__closed:
            raise MigrationSessionError("Can't change applied session")
        self._to_apply.extend(migrations)

Response: Adds migrations to be applied.


Instruction:
Can you tell what is the following Python 3 function doing
def _get_unapplied_migrations(self, loader):
        unapplied = []
        graph = loader.graph
        plan = []
        seen = set()

        # Generate the plan, in the order that migrations have been/should be applied.
        for target in graph.leaf_nodes():
            for migration in graph.forwards_plan(target):
                if migration not in seen:
                    plan.append(graph.nodes[migration])
                    seen.add(migration)

        # Remove the migrations that have already been applied.
        for migration in plan:
            if not (migration.app_label, migration.name) in loader.applied_migrations:
                # NOTE: Unicode Django application names are unsupported.
                unapplied.append([migration.app_label, str(migration.name)])
        return unapplied

Response: Return a list of unapplied migrations in the form [ migration1 migration2... ]


Instruction:
Can you generate the documentation for the following Python 3 function
def _get_current_migration_state(self, loader, apps):
        # Only care about applied migrations for the passed-in apps.
        apps = set(apps)
        relevant_applied = [migration for migration in loader.applied_migrations if migration[0] in apps]
        # Sort them by the most recent migration and convert to a dictionary,
        # leaving apps as keys and most recent migration as values.
        # NB: this is a dirty trick
        most_recents = dict(sorted(relevant_applied, key=lambda m: m[1]))
        # Fill in the apps with no migrations with 'zero'.
        # NOTE: Unicode Django application names are unsupported.
        most_recents = [[app, 'zero' if app not in most_recents else str(most_recents[app])] for app in apps]
        return most_recents

Response: Extract the most recent migrations from the relevant apps and return a dictionary of app names to migration state.


Instruction:
How would you implement a function in Python 3 that
returns a tuple of unapplied current

Response:
def list_migrations(self):
        """
        Returns a tuple of unapplied, current

        "Unapplied" is a list of unapplied migrations. "Current" is a list of the current migration
        states for apps with unapplied migrations.

        Both are tuples of the form (app, migration_name).
        """
        connection = connections[self._database_name]
        loader = MigrationLoader(connection, ignore_no_migrations=True)
        unapplied = self._get_unapplied_migrations(loader)
        currents = self._get_current_migration_state(loader, [u[0] for u in unapplied])
        return unapplied, currents


Instruction:
Given the following Python 3 function, write the documentation
def _parse_migrate_output(self, output):
        failed = None
        succeeded = []

        # Mark migrations:
        # - before exception migration as success
        # - exception migration as failed
        for line in output.split('\n'):
            line = _remove_escape_characters(line).strip()
            line_match = self.migration_regex.match(line)
            if line_match:
                migration = (line_match.group('app_name'), line_match.group('migration_name'))
                if line_match.group('success') == 'OK':
                    # The migration succeeded
                    succeeded.append(migration)
                else:
                    # The migration failed
                    failed = migration
                    break
        return succeeded, failed

Response: Parse the output of manage. py migrate and return a list of tuples.


Instruction:
Given the following Python 3 function, write the documentation
def __apply(self, migration=None, run_all=False):
        out = StringIO()
        trace = None

        migrate_kwargs = {
            'interactive': False,
            'stdout': out,
            'database': self._database_name,
        }
        if migration is not None:
            migrate_kwargs.update({
                'app_label': migration[0],
                'migration_name': migration[1],
            })
        elif not run_all:
            raise ValueError('Either a migration must be provided or "run_all" must be True')

        start = self._timer()
        try:
            call_command("migrate", **migrate_kwargs)
        except Exception:
            trace = ''.join(traceback.format_exception(*sys.exc_info()))
        finally:
            end = self._timer()
            successes, failure = self._parse_migrate_output(out.getvalue())

            self._migration_state.append({
                'database': self._database_name,
                'migration': 'all' if run_all else (migration[0], migration[1]),
                'duration': end - start,
                'output': _remove_escape_characters(out.getvalue()),
                'succeeded_migrations': successes,        # [(app, migration), ...]
                'failed_migration': failure,              # (app, migration)
                'traceback': trace,
                'succeeded': failure is None and trace is None,
            })

        if failure is not None:
            raise CommandError("Migration failed for app '{}' - migration '{}'.\n".format(*failure))
        elif trace is not None:
            raise CommandError("Migrations failed unexpectedly. See self.state['traceback'] for details.")

Response: Applies a migration to the state.


Instruction:
Write a Python 3 function for
applying all migrations that have been added to the session.

Response:
def apply(self):
        """
        Applies all migrations that have been added.
        Note that some migrations depend on others, so you might end up
        running more than one.
        """
        if self.__closed:
            raise MigrationSessionError("Can't apply applied session")
        try:
            while self._to_apply:
                self.__apply(migration=self._to_apply.pop(0))
        except:
            raise
        finally:
            self.__closed = True


Instruction:
Make a summary of the following Python 3 code
def apply_all(self):
        if self.__closed:
            raise MigrationSessionError("Can't apply applied session")
        if self._to_apply:
            raise MigrationSessionError("Can't apply_all with migrations added to session")

        try:
            self.__apply(run_all=True)
        except:
            raise
        finally:
            self.__closed = True

Response: Applies all Django model migrations at once recording the result.


Instruction:
Write a Python 3 script for
sending an email from a template.

Response:
def send_email_from_template(to_email, from_email, subject,
                             markdown_template=None,
                             text_template=None, html_template=None,
                             fail_silently=False, context=None,
                             **kwargs):
    """Send an email from a template.

    :param to_email: the email address to send the email to
    :param from_email: the email address the email will be from
    :param subject: the subject of the email
    :param markdown_template: the markdown syntax template to use for the
        email. If provided, this will generate both the text and html versions
        of the email. You must have the "markdown" library installed in order
        to use this. pip install markdown.
    :param text_template: the template for the text version of the email. This
        can be omitted if the markdown_template is provided.
    :param html_template: the template for the html version of the email. This
        can be omitted if the markdown_template is provided.
    :param context: the context for the email templates
    """
    return send_emails_from_template(
        to_emails=[to_email],
        from_email=from_email,
        subject=subject,
        markdown_template=markdown_template,
        text_template=text_template,
        html_template=html_template,
        fail_silently=fail_silently,
        context=context,
        **kwargs
    )


Instruction:
Can you write a function in Python 3 where it
sends many emails from a single template.

Response:
def send_emails_from_template(to_emails, from_email, subject,
                              markdown_template=None, text_template=None,
                              html_template=None, fail_silently=False,
                              context=None, attachments=None, **kwargs):
    """Send many emails from single template.  Each email address listed in the
    ``to_emails`` will receive an separate email.

    :param to_emails: list of email address to send the email to
    :param from_email: the email address the email will be from
    :param subject: the subject of the email
    :param markdown_template: the markdown syntax template to use for the
        email.  If provided, this will generate both the text and html versions
        of the email. You must have the "markdown" library installed in order
        to use this. pip install markdown.
    :param text_template: the template for the text version of the email. This
        can be omitted if the markdown_template is provided.
    :param html_template: the template for the html version of the email. This
        can be omitted if the markdown_template is provided.
    :param context: the context for the email templates
    :param attachments: list of additional attachments to add to the email
        (example: email.mime.image.MIMEImage object).  The attachments will be
        added to each email sent.
    """
    if not to_emails:
        return

    if context is None:
        context = {}

    if markdown_template:
        try:
            from markdown import markdown
        except ImportError:
            raise ImportError(
                'The application is attempting to send an email by using the '
                '"markdown" library, but markdown is not installed.  Please '
                'install it. See: '
                'http://pythonhosted.org/Markdown/install.html'
            )

        base_html_template = getattr(settings,
                                     'CORE_BASE_HTML_EMAIL_TEMPLATE',
                                     'django_core/mail/base_email.html')

        text_content = render_to_string(markdown_template, context)
        context['email_content'] = markdown(text_content)
        html_content = render_to_string(base_html_template, context)
    else:
        text_content = render_to_string(text_template, context)
        html_content = render_to_string(html_template, context)

    emails = []

    for email_address in to_emails:
        email = EmailMultiAlternatives(
            subject=subject,
            body=text_content,
            from_email=from_email,
            to=[email_address],
            alternatives=[(html_content, 'text/html')]
        )

        if attachments:
            email.mixed_subtype = 'related'

            for attachment in attachments:
                email.attach(attachment)

        emails.append(email)

    connection = mail.get_connection()

    connection.open()
    connection.send_messages(emails)
    connection.close()


Instruction:
Write a Python 3 script for
finding all the possible interpretations given a set of symbols

Response:
def all_models(self, alphabet: _Alphabet) -> Set[PLInterpretation]:
        """Find all the possible interpretations given a set of symbols"""

        all_possible_interpretations = alphabet.powerset().symbols
        all_models = set()
        for i in all_possible_interpretations:
            # compute current Interpretation, considering False
            # all propositional symbols not present in current interpretation
            current_interpretation = PLInterpretation(i)
            if self.truth(current_interpretation):
                all_models.add(current_interpretation)

        self._all_models = all_models
        return all_models


Instruction:
Make a summary of the following Python 3 code
def minimal_models(self, alphabet: _Alphabet) -> Set[PLInterpretation]:
        models = self.all_models(alphabet)

        minimal_models = set()
        for m in models:
            min_m = m
            for m1 in models:
                if min_m.true_propositions.issuperset(m1.true_propositions):
                    min_m = m1
            minimal_models.add(min_m)

        return minimal_models

Response: Find models of minimal size.


Instruction:
Can you generate the documentation for the following Python 3 function
def save(self, *args, **kwargs):
        self.save_prep(instance_or_instances=self)
        return super(AbstractTokenModel, self).save(*args, **kwargs)

Response: Make sure token is added.


Instruction:
Given the following Python 3 function, write the documentation
def save_prep(cls, instance_or_instances):
        instances = make_obj_list(instance_or_instances)

        tokens = set(cls.objects.get_available_tokens(
            count=len(instances),
            token_length=cls.token_length
        ))

        for instance in instances:
            if not instance.token:
                instance.token = tokens.pop()

        super(AbstractTokenModel, cls).save_prep(
            instance_or_instances=instances
        )

Response: Preprocess the object before the object is saved.


Instruction:
Given the following Python 3 function, write the documentation
def _popup(self):
        res = ()
        for child in self.formulas:
            if type(child) == type(self):
                superchilds = child.formulas
                res += superchilds
            else:
                res += (child, )
        return tuple(res)

Response:
recursively find commutative binary operator
        among child formulas and pop up them at the same level


Instruction:
How would you code a function in Python 3 to
get the absolute url for the content object.

Response:
def get_content_object_url(self):
        """Gets the absolute url for the content object."""
        if (self.content_object and
            hasattr(self.content_object, 'get_absolute_url')):
            return self.content_object.get_absolute_url()

        return None


Instruction:
Can you generate a brief explanation for the following Python 3 code
def t_ATOM(self, t):
        r'[a-zA-Z_][a-zA-Z_0-9]*'
        t.type = LDLfLexer.reserved.get(t.value, 'ATOM')  # Check for reserved words
        return t

Response: t_ATOM is a bit of a hack to handle reserved words


Instruction:
Can you create a Python 3 function that
returns the url that should be used to redirect to the current url.

Response:
def get_redirect_url(current_url, config, https=None):
    """
    priorities are (primary domain and aliases are treated the same):
        exact redirect match > exact alias match > pattern redirect match > pattern alias match
    :param current_url: the url that is being called
    :param config: redirect configuration for this url
    :param want_https: whether redirects should go to https (None keeps the current scheme)
    :return: None for no redirect or an url to redirect to
    """
    primary_domain = config['domain']
    domains = set(config.get('aliases', [])) | set((primary_domain,))
    domain_patterns = compile_regexes(domains)
    redirect_domains = set(config.get('redirects', []))
    redirect_domain_patterns = compile_regexes(redirect_domains)
    url = yurl.URL(current_url)
    if https is None:
        target_scheme = url.scheme
    else:
        target_scheme = 'https' if https else 'http'
    redirect_url = None
    if url.is_host_ip() or url.is_host_ipv4():
        # don't redirect for ips
        return
    if url.host in domains and url.scheme == target_scheme:
        # exact host and scheme match: Nothing to do
        return
    if url.host in domains and url.scheme != target_scheme:
        # exact alias match, but scheme mismatch: redirect to changed scheme
        redirect_url = url.replace(scheme=target_scheme)
    elif url.host in redirect_domains:
        # exact redirect match: redirect
        redirect_url = url.replace(scheme=target_scheme, host=primary_domain)
    elif url.host in domains:
        # exact alias match: nothing to do
        return
    elif match_any(redirect_domain_patterns, url.host):
        # pattern redirect match: redirect
        redirect_url = url.replace(scheme=target_scheme, host=primary_domain)
    elif match_any(domain_patterns, url.host):
        # pattern alias match
        if url.scheme != target_scheme:
            # pattern alias match and scheme mismatch: redirect
            redirect_url = url.replace(scheme=target_scheme)
        else:
            return

    if redirect_url:
        return '{}'.format(redirect_url)


Instruction:
Explain what the following Python 3 code does
def invoke(self, ctx):
        if self.callback is not None:
            loop = asyncio.get_event_loop()
            return loop.run_until_complete(self.async_invoke(ctx))

Response: Invoke the callback on the given context.


Instruction:
How would you explain what the following Python 3 function does
def get_help_option(self, ctx):
        help_options = self.get_help_option_names(ctx)
        if not help_options or not self.add_help_option:
            return

        def show_help(ctx, param, value):
            import abot.bot
            if value and not ctx.resilient_parsing:
                event: abot.bot.MessageEvent = abot.bot.current_event.get()
                tbd_tasks.append(event.reply(ctx.get_help()))
                ctx.exit()

        return click.core.Option(help_options, is_flag=True,
                                 is_eager=True, expose_value=False,
                                 callback=show_help,
                                 help='Show this message and exit.')

Response: Returns the help option object.


Instruction:
Can you tell what is the following Python 3 function doing
def get_widget_css_class(self, attrs):
        size_class = 'size-{0}'.format(self.num_inputs)

        if 'class' in attrs:
            attrs['class'] += ' {0}'.format(size_class)
        else:
            attrs['class'] = size_class

Response: Gets the class for the widget.


Instruction:
Here you have a function in Python 3, explain what it does
def p_formula_atom(self, p):
        if p[1]==Symbols.TRUE.value:
            p[0] = PLTrue()
        elif p[1]==Symbols.FALSE.value:
            p[0] = PLFalse()
        else:
            p[0] = PLAtomic(Symbol(p[1]))

Response:
formula : ATOM
                   | TRUE
                   | FALSE


Instruction:
How would you explain what the following Python 3 function does
def get_or_none(self, prefetch_related=None, select_related=False,
                    **kwargs):
        """Gets a single object based on kwargs or None if one is not found.

        :param prefetch_related: list or tuple of fields to prefetch for an
            object.  This takes precedence over select_related.

            Example:

            >> get_or_none(prefetch_related=['some_field',
            ...                              'some_field__some_fields_field'])

            See:
            https://docs.djangoproject.com/en/dev/ref/models/querysets/#prefetch-related

        :param select_related: boolean when set to True will follow foreign-key
            relationships to prevent many db queries when looping over foreign
            keys. If this value is boolean True, the immediate foreign keys
            will be selected, but not foreign keys of foreign keys.  If this
            value is set to a list or tuple, then those will be the fields to
            to follow and select.

            Example:

            >> # Both of the following are valid
            >> get_or_none(select_related=True)
            >> get_or_none(select_related=['some_field',
            ...                            'some_field__some_fields_field'])

            See: https://docs.djangoproject.com/en/dev/ref/models/querysets/
        :param kwargs: list of fields and their values to retrieve.

        """
        try:
            if prefetch_related:
                query_set = self.prefetch_related(*prefetch_related)
            elif select_related == True:
                query_set = self.select_related()
            elif isinstance(select_related, (list, tuple)):
                query_set = self.select_related(*select_related)
            else:
                query_set = self

            return query_set.get(**kwargs)
        except self.model.DoesNotExist:
            return None

Response: Gets a single object based on kwargs or None if one is not found.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_by_id_or_404(self, id, **kwargs):
        obj = self.get_by_id(id=id, **kwargs)

        if obj:
            return obj

        raise Http404

Response: r Gets a single instance r raises a 404 is one isn t found.


Instruction:
Write a Python 3 script to
insert many object at once.

Response:
def bulk_create(self, objs, *args, **kwargs):
        """Insert many object at once."""
        if hasattr(self.model, 'save_prep'):
            # Method from AbstractBaseModel. If the model class doesn't
            # subclass AbstractBaseModel, then don't call this.
            self.model.save_prep(instance_or_instances=objs)

        return super(CommonManager, self).bulk_create(objs=objs,
                                                      *args,
                                                      **kwargs)


Instruction:
Write a Python 3 function that can
delete objects by ids.

Response:
def delete_by_ids(self, ids):
        """Delete objects by ids.

        :param ids: list of objects ids to delete.
        :return: True if objects were deleted.  Otherwise, return False if no
                objects were found or the delete was not successful.
        """
        try:
            self.filter(id__in=ids).delete()
            return True
        except self.model.DoesNotExist:
            return False


Instruction:
How would you code a function in Python 3 to
check to see if a slug is available.

Response:
def is_slug_available(self, slug, **kwargs):
        """Checks to see if a slug is available. If the slug is already being used
        this method returns False.  Otherwise, return True.
        """
        try:
            self.get(slug=slug, **kwargs)
            return False
        except self.model.DoesNotExist:
            return True


Instruction:
Here you have a function in Python 3, explain what it does
def get_next_slug(self, slug, **kwargs):
        original_slug = slug = slugify(slug)
        count = 0

        while not self.is_slug_available(slug=slug, **kwargs):
            count += 1
            slug = '{0}-{1}'.format(original_slug, count)

        return slug

Response: Gets the next available slug.


Instruction:
Write a Python 3 function for
getting the next available token.

Response:
def get_next_token(self, length=15, **kwargs):
        """Gets the next available token.

        :param length: length of the token
        :param kwargs: additional filter criteria to check for when looking for
            a unique token.

        """
        return self.get_available_tokens(count=1,
                                         token_length=length,
                                         **kwargs)[0]


Instruction:
Make a summary of the following Python 3 code
def get_available_tokens(self, count=10, token_length=15, **kwargs):
        # This is the number of extra tokens to try and retrieve so calls to
        # the db can be limited
        token_buffer = int(math.ceil(count * .05))

        if token_buffer < 5:
            token_buffer = 5

        available = set([])

        while True:
            tokens = [random_alphanum(length=token_length)
                      for t in range(count + token_buffer)]
            db_tokens = self.filter(token__in=tokens).values_list('token',
                                                                  flat=True)
            available.update(set(tokens).difference(db_tokens))

            if len(available) >= count:
                return list(available)[:count]

Response: Gets a list of available tokens.


Instruction:
Given the following Python 3 function, write the documentation
def create_generic(self, content_object=None, **kwargs):
        if content_object:
            kwargs['content_type'] = ContentType.objects.get_for_model(
                content_object
            )
            kwargs['object_id'] = content_object.id

        return self.create(**kwargs)

Response: Create a generic object.


Instruction:
Can you write a function in Python 3 where it
filters by a generic object.

Response:
def filter_generic(self, content_object=None, **kwargs):
        """Filter by a generic object.

        :param content_object: the content object to filter on.
        """
        if content_object:
            kwargs['content_type'] = ContentType.objects.get_for_model(
                content_object
            )
            kwargs['object_id'] = content_object.id

        return self.filter(**kwargs)


Instruction:
Write a Python 3 function for
getting or creates a generic object.

Response:
def get_or_create_generic(self, content_object=None, **kwargs):
        """Gets or creates a generic object.  This is a wrapper for
        get_or_create(...) when you need to get or create a generic object.

        :param obj: the object to get or create
        :param kwargs: any other kwargs that the model accepts.
        """
        if content_object:
            kwargs['content_type'] = ContentType.objects.get_for_model(
                content_object
            )
            kwargs['object_id'] = content_object.id

        return self.get_or_create(**kwargs)


Instruction:
Implement a Python 3 function for
getting all object by a specific model.

Response:
def get_by_model(self, model):
        """Gets all object by a specific model."""
        content_type = ContentType.objects.get_for_model(model)
        return self.filter(content_type=content_type)


Instruction:
Here you have a function in Python 3, explain what it does
def attr(obj, attr):
    if not obj or not hasattr(obj, attr):
        return ''

    return getattr(obj, attr, '')

Response: Returns the attribute of the object.


Instruction:
Can you create a Python 3 function that
makes an object iterable.

Response:
def make_iterable(obj):
    """Make an object iterable.

    >>> make_iterable(obj='hello')
    ('hello',)
    >>> make_iterable(obj=None)
    ()
    """
    if not obj:
        return tuple()

    if isinstance(obj, (list, tuple, set)):
        return obj

    return (obj,)


Instruction:
Here you have a function in Python 3, explain what it does
def get_form_kwargs(self):
        kwargs = super(ApiFormView, self).get_form_kwargs()
        kwargs['data'] = kwargs.get('initial')
        return kwargs

Response: Add the data to the form args so you can validate the form s data on a get request.


Instruction:
Can you generate the documentation for the following Python 3 function
def form_invalid(self, form, context=None, **kwargs):
        if not context:
            context = {}

        context['errors'] = form.errors
        return super(ApiFormView, self).render_to_response(context=context,
                                                           status=400)

Response: This will return the request with form errors as well as any additional context.


Instruction:
Make a summary of the following Python 3 code
def is_valid_rgb_color(value):
    if not value:
        return False

    regex = re.compile(RGB_COLOR_REGEX)
    return bool(regex.match(value))

Response: Checks whether the value is a valid rgb or rgba color string.


Instruction:
How would you implement a function in Python 3 that
validates that a password is at least 7 characters long and has at least 1 digit and 1 letter.

Response:
def validate_password_strength(value):
    """Validates that a password is as least 7 characters long and has at least
    1 digit and 1 letter.
    """
    min_length = 7

    if len(value) < min_length:
        raise ValidationError(_('Password must be at least {0} characters '
                                'long.').format(min_length))

    # check for digit
    if not any(char.isdigit() for char in value):
        raise ValidationError(_('Password must contain at least 1 digit.'))

    # check for letter
    if not any(char.isalpha() for char in value):
        raise ValidationError(_('Password must contain at least 1 letter.'))


Instruction:
How would you explain what the following Python 3 function does
def add_metaclass(metaclass):
    def wrapper(cls):
        orig_vars = cls.__dict__.copy()
        orig_vars.pop('__dict__', None)
        orig_vars.pop('__weakref__', None)
        return metaclass(cls.__name__, cls.__bases__, orig_vars)
    return wrapper

Response: Class decorator for creating a class with a metaclass.


Instruction:
How would you explain what the following Python 3 function does
def clean(self, value):
        if not value:
            return None

        # if any value exists, then add "0" as a placeholder to the remaining
        # values.
        if isinstance(value, list) and any(value):
            for i, item in enumerate(value):
                if not item:
                    value[i] = '0'

        return super(MultipleDecimalField, self).clean(value)

Response: Validates that the input can be converted to a list of decimals.


Instruction:
Given the following Python 3 function, write the documentation
def to_python(self, value):
        if not value:
            return None

        if isinstance(value, list):
            for index, position_val in enumerate(value):
                val = super(MultipleDecimalField, self).to_python(position_val)
                value[index] = val

        return value

Response: Validates that the input can be converted to a list of decimals.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def random_alphanum(length=10, lower_only=False):
    character_set = ALPHANUM_LOWER if lower_only else ALPHANUM
    sample_size = 5
    chars = random.sample(character_set, sample_size)

    while len(chars) < length:
        chars += random.sample(character_set, sample_size)

    random.shuffle(chars)
    return ''.join(chars[:length])

Response: Returns a random alphanumeric string of the given length.


Instruction:
Can you generate the documentation for the following Python 3 function
def generate_key(low=7, high=10, lower_only=False):
    return random_alphanum(length=randint(7, 10), lower_only=lower_only)

Response:
Generates a random alphanumeric key between low and high characters in
    length.


Instruction:
How would you code a function in Python 3 to
get the html link for the object.

Response:
def get_absolute_url_link(self, text=None, cls=None, icon_class=None,
                              **attrs):
        """Gets the html link for the object."""
        if text is None:
            text = self.get_link_text()

        return build_link(href=self.get_absolute_url(),
                          text=text,
                          cls=cls,
                          icon_class=icon_class,
                          **attrs)


Instruction:
How would you explain what the following Python 3 function does
def get_edit_url_link(self, text=None, cls=None, icon_class=None,
                          **attrs):
        """Gets the html edit link for the object."""
        if text is None:
            text = 'Edit'

        return build_link(href=self.get_edit_url(),
                          text=text,
                          cls=cls,
                          icon_class=icon_class,
                          **attrs)

Response: Gets the html edit link for the object.


Instruction:
Make a summary of the following Python 3 code
def get_delete_url_link(self, text=None, cls=None, icon_class=None,
                            **attrs):
        """Gets the html delete link for the object."""
        if text is None:
            text = 'Delete'

        return build_link(href=self.get_delete_url(),
                          text=text,
                          cls=cls,
                          icon_class=icon_class,
                          **attrs)

Response: Gets the html delete link for the object.


Instruction:
Implement a function in Python 3 to
expire all tokens for an email address or list of emails.

Response:
def expire_by_email(self, email_address, **kwargs):
        """Expires tokens for an email address or email addresses.

        :param email_address: the string email address or emails addresses to
            expire tokens for.
        :param reason: the codified reason for the tokens.  If explicitly set
            to None, this will expire all tokens for the email provided.
        """
        if not email_address:
            # no email(s) provided.  Nothing to do.
            return None

        if isinstance(email_address, (set, list, tuple)):
            email_address = [e.strip() for e in set(email_address)
                             if e and e.strip()]

            # make sure there's at least 1 valid email address
            if len(email_address) <= 0:
                # no valid emails
                return None

            kwargs['email_address__in'] = email_address
        else:
            kwargs['email_address'] = email_address

        # try setting the reason default if one exists (in the case of proxy
        # models)
        if 'reason' not in kwargs and self.model.reason_default:
            kwargs['reason'] = self.model.reason_default

        if 'reason' in kwargs and kwargs.get('reason') is None:
            # explicitly setting the reason to None will expire all tokens for
            # a user regardless of the reason.
            del kwargs['reason']

        self.filter(**kwargs).update(expires=datetime(1970, 1, 1))


Instruction:
Create a Python 3 function to
map the GET query string params to the mapped keys and updates the request s GET QueryDict with the mapped keys.

Response:
def map_query_string(self):
        """Maps the GET query string params the the query_key_mapper dict and
        updates the request's GET QueryDict with the mapped keys.
        """
        if (not self.query_key_mapper or
            self.request.method == 'POST'):
            # Nothing to map, don't do anything.
            # return self.request.POST
            return {}

        keys = list(self.query_key_mapper.keys())

        return {self.query_key_mapper.get(k) if k in keys else k: v.strip()
                for k, v in self.request.GET.items()}


Instruction:
Can you generate a brief explanation for the following Python 3 code
def bump_repos_version(module_name, new_version, local_only):
    # Make the cloning directory and change directories into it.
    tmp_dir = tempfile.mkdtemp(dir=os.getcwd())

    # Iterate through each repository.
    for owner, repo_name in REPOS_TO_CHANGE:
        repo_url = REPO_URL_FORMAT.format(owner, repo_name)

        gh = GitHubApiUtils(owner, repo_name)

        os.chdir(tmp_dir)

        # Clone the repo.
        ret_code = subprocess.call(['git', 'clone', '{}.git'.format(repo_url)])
        if ret_code:
            logging.error('Failed to clone repo {}'.format(repo_url))
            continue

        # Change into the cloned repo dir.
        os.chdir(repo_name)

        # Create a branch, using the version number.
        branch_name = '{}/{}'.format(module_name, new_version)
        ret_code = subprocess.call(['git', 'checkout', '-b', branch_name])
        if ret_code:
            logging.error('Failed to create branch in repo {}'.format(repo_url))
            continue

        # Search through all TXT files to find all lines with the module name, changing the pinned version.
        files_changed = False
        for root, _dirs, files in os.walk('.'):
            for file in files:
                if file.endswith('.txt') and (('requirements' in file) or ('requirements' in root)):
                    found = False
                    filepath = os.path.join(root, file)
                    with open(filepath) as f:
                        if '{}=='.format(module_name) in f.read():
                            found = True
                    if found:
                        files_changed = True
                        # Change the file in-place.
                        for line in fileinput.input(filepath, inplace=True):
                            if '{}=='.format(module_name) in line:
                                print '{}=={}'.format(module_name, new_version)
                            else:
                                print line,

        if not files_changed:
            # Module name wasn't found in the requirements files.
            logging.info("Module name '{}' not found in repo {} - skipping.".format(module_name, repo_url))
            continue

        # Add/commit the files.
        ret_code = subprocess.call(['git', 'commit', '-am', 'Updating {} requirement to version {}'.format(module_name, new_version)])
        if ret_code:
            logging.error("Failed to add and commit changed files to repo {}".format(repo_url))
            continue

        if local_only:
            # For local_only, don't push the branch to the remote and create the PR - leave all changes local for review.
            continue

        # Push the branch.
        ret_code = subprocess.call(['git', 'push', '--set-upstream', 'origin', branch_name])
        if ret_code:
            logging.error("Failed to push branch {} upstream for repo {}".format(branch_name, repo_url))
            continue

        # Create a PR with an automated message.
        rollback_branch_push = False
        try:
            # The GitHub "mention" below does not work via the API - unfortunately...
            response = gh.create_pull(
                title='Change {} version.'.format(module_name),
                body='Change the required version of {} to {}.\n\n@edx-ops/pipeline-team Please review and tag appropriate parties.'.format(module_name, new_version),
                head=branch_name,
                base='master'
            )
        except:
            logging.error('Failed to create PR for repo {} - did you set GITHUB_TOKEN?'.format(repo_url))
            rollback_branch_push = True
        else:
            logging.info('Created PR #{} for repo {}: {}'.format(response.number, repo_url, response.html_url))

        if rollback_branch_push:
            # Since the PR creation failed, delete the branch in the remote repo as well.
            ret_code = subprocess.call(['git', 'push', 'origin', '--delete', branch_name])
            if ret_code:
                logging.error("ROLLBACK: Failed to delete upstream branch {} for repo {}".format(branch_name, repo_url))

    if not local_only:
        # Remove the temp directory containing all the cloned repos.
        shutil.rmtree(tmp_dir)

Response: Bump the version number of a Python module in all repos.


Instruction:
Write a Python 3 function that can
call an Slack Web API method with parameters.

Response:
async def call(self, method, **params):
        """
        Call an Slack Web API method

        :param method: Slack Web API method to call
        :param params: {str: object} parameters to method
        :return: dict()
        """
        url = self.SLACK_RPC_PREFIX + method
        data = FormData()
        data.add_fields(MultiDict(token=self.bot_token, charset='utf-8', **params))
        response_body = await self.request(
            method='POST',
            url=url,
            data=data
        )
        if 'warning' in response_body:
            logger.warning(f'Warnings received from API call {method}: {response_body["warning"]}')
        if 'ok' not in response_body:
            logger.error(f'No ok marker in slack API call {method} {params} => {response_body}')
            raise SlackCallException('There is no ok marker, ... strange', method=method)
        if not response_body['ok']:
            logger.error(f'Slack API call failed {method} {params} => {response_body}')
            raise SlackCallException(f'No OK response returned', method=method)
        return response_body


Instruction:
Here you have a function in Python 3, explain what it does
def rtm_handler(self, ws_message):
        message = json.loads(ws_message.data)
        if 'reply_to' in message:
            reply_to = message['reply_to']
            future = self.response_futures.pop(reply_to, None)
            if future is None:
                logger.error(f'This should not happen, received reply to unknown message! {message}')
                return None
            future.set_result(message)
            return None
        if 'type' not in message:
            logger.error(f'No idea what this could be {message}')
            return
        message_type = message['type']

        if hasattr(self, f'handle_{message_type}'):
            function = getattr(self, f'handle_{message_type}')
            return function(message)

        if message_type in self.SLACK_RTM_EVENTS:
            logger.debug(f'Unhandled {message_type}. {message}')
        else:
            logger.warning(f'Unknown {message_type}. {message}')
        return message

Response: Handle a message from the Slack RTM.


Instruction:
How would you implement a function in Python 3 that
gets the default prefix for this form.

Response:
def get_default_prefix(self, instance=None):
        """Gets the prefix for this form.

        :param instance: the form model instance.  When calling this method
            directly this should almost always stay None so it looks for
            self.instance.
        """
        if instance is None and hasattr(self, 'instance'):
            instance = self.instance

        if instance and instance.id is not None:
            # it's an existing instance, use the instance prefix
            instance_prefix = self.default_instance_prefix
            if instance_prefix is None:
                instance_prefix = self.__class__.__name__.lower() + 'i-'

            return '{0}{1}'.format(instance_prefix,
                                   instance.id)

        if self.default_new_prefix is not None:
            return self.default_new_prefix

        return self.__class__.__name__.lower() + 'new-'


Instruction:
Can you generate a brief explanation for the following Python 3 code
def authenticate(self, username, password):
        # If username is an email address, then try to pull it up
        user = self.get_by_username_or_email(username)

        if not user:
            return None

        if user.check_password(password):
            return user

        return None

Response: Checks if a user is authenticated and returns the user s unique ID.


Instruction:
Create a Python 3 function to
return a set of all the elements in iterable that are the powerset of the elements of iterable.

Response:
def powerset(iterable) -> FrozenSet:
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    combs = _powerset(iterable)
    res = frozenset(frozenset(x) for x in combs)
    # res = map(frozenset, combs)
    return res


Instruction:
Explain what the following Python 3 code does
def edit_url_link(obj, **kwargs):
    if hasattr(obj, 'get_edit_url_link'):
        return obj.get_edit_url_link(**kwargs)

    edit_url = obj.get_edit_url()
    return build_link(href=edit_url, **kwargs)

Response: This method returns the edit url for the object


Instruction:
Explain what the following Python 3 code does
def delete_url_link(obj, **kwargs):
    if hasattr(obj, 'get_delete_url_link'):
        return obj.get_delete_url_link(**kwargs)

    delete_url = obj.get_delete_url()
    return build_link(href=delete_url, **kwargs)

Response: This method returns the delete url link for the object


Instruction:
Make a summary of the following Python 3 code
def formfield(self, form_class=None, choices_form_class=None, **kwargs):
        defaults = {
            'form_class': form_class or self.get_form_class()
        }
        defaults.update(kwargs)

        return super(ListField, self).formfield(**defaults)

Response: Make the default formfield a CommaSeparatedListField.


Instruction:
Given the following Python 3 function, write the documentation
def validate(self, value, model_instance, **kwargs):
        self.get_choices_form_class().validate(value, model_instance, **kwargs)

Response: This follows the validate rules for the choices_form_class field used.


Instruction:
Implement a Python 3 function for
converting the input JSON value into python structures. Raises a ValidationError if the input JSON value can t be converted.

Response:
def to_python(self, value):
        """
        Convert the input JSON value into python structures, raises
        django.core.exceptions.ValidationError if the data can't be converted.
        """
        if isinstance(value, dict):
            return value

        if self.blank and not value:
            return None

        if isinstance(value, string_types):
            try:
                return json.loads(value)
            except Exception as e:
                raise ValidationError(str(e))

        return value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_prep_value(self, value):
        try:
            return json.dumps(value, cls=DjangoJSONEncoder)
        except Exception as e:
            raise ValidationError(str(e))

Response: Convert value to JSON string before save


Instruction:
Here you have a function in Python 3, explain what it does
def _gorg(a):
    assert isinstance(a, GenericMeta)
    while a.__origin__ is not None:
        a = a.__origin__
    return a

Response: Return the farthest origin of a generic class.


Instruction:
How would you explain what the following Python 3 function does
def _next_in_mro(cls):
    next_in_mro = object
    # Look for the last occurrence of Generic or Generic[...].
    for i, c in enumerate(cls.__mro__[:-1]):
        if isinstance(c, GenericMeta) and _gorg(c) is Generic:
            next_in_mro = cls.__mro__[i + 1]
    return next_in_mro

Response:
Helper for Generic. __new__.
MimeType. _next_in_mro Returns the class after the last occurrence of Generic or Generic [... ] in cls.
MimeType.


Instruction:
How would you code a function in Python 3 to
construct a __subclasshook__ callable that incorporates the associated __extra__ class in subclass checks performed against cls.

Response:
def _make_subclasshook(cls):
    """Construct a __subclasshook__ callable that incorporates
    the associated __extra__ class in subclass checks performed
    against cls.
    """
    if isinstance(cls.__extra__, abc.ABCMeta):
        # The logic mirrors that of ABCMeta.__subclasscheck__.
        # Registered classes need not be checked here because
        # cls and its extra share the same _abc_registry.
        def __extrahook__(cls, subclass):
            res = cls.__extra__.__subclasshook__(subclass)
            if res is not NotImplemented:
                return res
            if cls.__extra__ in getattr(subclass, '__mro__', ()):
                return True
            for scls in cls.__extra__.__subclasses__():
                # If we have fake typing and typing detect by module name
                if scls.__class__.__module__ == "typing":
                    continue
                if isinstance(scls, GenericMeta):
                    continue
                if issubclass(subclass, scls):
                    return True
            return NotImplemented
    else:
        # For non-ABC extras we'll just call issubclass().
        def __extrahook__(cls, subclass):
            if cls.__extra__ and issubclass(subclass, cls.__extra__):
                return True
            return NotImplemented
    return classmethod(__extrahook__)


Instruction:
How would you implement a function in Python 3 that
gets the authorization object for the view.

Response:
def get_authorization(self, **kwargs):
        """Gets the authorization object for the view."""
        if self.authorization is not None:
            return self.authorization

        auth_class = self.get_authorization_class()
        auth_user = self.get_authorization_user()
        auth_kwargs = {
            'token': self.get_authorization_token(**kwargs)
        }

        if auth_user and auth_user.is_authenticated():
            auth_kwargs['created_user'] = self.get_authorization_user()

        self.authorization = auth_class.objects.get_by_token_or_404(
            **auth_kwargs
        )
        return self.authorization


Instruction:
Write a Python 3 function for
getting the user the authorization object is for.

Response:
def get_authorization_user(self, **kwargs):
        """Gets the user the authorization object is for."""
        if self.authorization_user is not None:
            return self.authorization_user

        self.authorization_user = self.request.user
        return self.request.user


Instruction:
Explain what the following Python 3 code does
def to_dict(self, dict_cls=FrozenOrderedDict):
        # type: (Type[dict]) -> Dict[str, Any]
        """Create a dictionary representation of object attributes

        Returns:
            OrderedDict serialised version of self
        """
        pairs = tuple((k, serialize_object(getattr(self, k), dict_cls))
                      for k in self.call_types)
        if self.typeid:
            d = dict_cls((("typeid", self.typeid),) + pairs)
        else:
            d = dict_cls(pairs)
        return d

Response: Create a dictionary representation of object attributes.


Instruction:
Implement a Python 3 function for
making sure it s a legit site to redirect to.

Response:
def safe_redirect(next_url, default=None):
    """Makes sure it's a legit site to redirect to.

    :param default: this is the default url or named url to redirect to in the
        event where next_url is not legit.

    """
    if is_legit_next_url(next_url):
        return redirect(next_url)

    if default:
        return redirect(default)

    return redirect('/')


Instruction:
Given the following Python 3 function, write the documentation
def replace_url_query_values(url, replace_vals):
    if '?' not in url:
        return url

    parsed_url = urlparse(url)
    query = dict(parse_qsl(parsed_url.query))
    query.update(replace_vals)
    return '{0}?{1}'.format(url.split('?')[0], urlencode(query))

Response: Replace querystring values in a url string.


Instruction:
Given the following Python 3 function, write the documentation
def get_query_values_from_url(url, keys=None):
    if not url or '?' not in url:
        # no query params
        return None

    parsed_url = urlparse(url)
    query = dict(parse_qsl(parsed_url.query))

    if keys is None:
        return query

    if isinstance(keys, string_types):
        return query.get(keys)

    return {k: query.get(k) for k in keys}

Response: Gets query string values from a url.


Instruction:
Make a summary of the following Python 3 code
def get_json_response(self, content, **kwargs):

        # Don't care to return a django form or view in the response here.
        # Remove those from the context.
        if isinstance(content, dict):
            response_content = {k: deepcopy(v) for k, v in content.items()
                                if k not in ('form', 'view') or k in ('form', 'view')
                                and not isinstance(v, (Form, View))}
        else:
            response_content = content

        return HttpResponse(content=json.dumps(response_content),
                            content_type='application/json; charset=utf-8',
                            **kwargs)

Response: Returns a json response object.


Instruction:
Can you create a Python 3 function that
deletes the object and then redirects to the success URL.

Response:
def delete(self, request, *args, **kwargs):
        """
        Calls the delete() method on the fetched object and then
        redirects to the success URL.
        """
        self.object = self.get_object()
        success_url = self.get_success_url()
        self.object.delete()

        if self.request.is_ajax():
            return JSONResponseMixin.render_to_response(self, context={})

        return HttpResponseRedirect(success_url)


Instruction:
Given the following Python 3 function, write the documentation
def find_atomics(formula: Formula) -> Set[PLAtomic]:
    f = formula
    res = set()
    if isinstance(formula, PLFormula):
        res = formula.find_atomics()
    # elif isinstance(f, PLNot):
    #     res = res.union(find_atomics(f.f))
    # elif isinstance(f, PLBinaryOperator):
    #     for subf in f.formulas:
    #         res = res.union(find_atomics(subf))
    else:
        res.add(f)
    return res

Response: Finds all the atomic formulas


Instruction:
Explain what the following Python 3 code does
def _transform_delta(f:Formula, formula2AtomicFormula):
    t = type(f)
    if t == PLNot:
        return PLNot(_transform_delta(f, formula2AtomicFormula))
    # elif isinstance(f, PLBinaryOperator): #PLAnd, PLOr, PLImplies, PLEquivalence
    elif t == PLAnd or t == PLOr or t == PLImplies or t == PLEquivalence:
        return t([_transform_delta(subf, formula2AtomicFormula) for subf in f.formulas])
    elif t == PLTrue or t == PLFalse:
        return f
    else:
        return formula2AtomicFormula[f]

Response:
From a Propositional Formula to a Propositional Formula
    with non - propositional subformulas replaced with a freezed atomic formula.