Instruction:
Can you generate the documentation for the following Python 3 function
def export(id, local=False, scrub_pii=False):

    print("Preparing to export the data...")

    if local:
        db_uri = db.db_url
    else:
        db_uri = HerokuApp(id).db_uri

    # Create the data package if it doesn't already exist.
    subdata_path = os.path.join("data", id, "data")
    try:
        os.makedirs(subdata_path)

    except OSError as e:
        if e.errno != errno.EEXIST or not os.path.isdir(subdata_path):
            raise

    # Copy in the data.
    copy_db_to_csv(db_uri, subdata_path, scrub_pii=scrub_pii)

    # Copy the experiment code into a code/ subdirectory.
    try:
        shutil.copyfile(
            os.path.join("snapshots", id + "-code.zip"),
            os.path.join("data", id, id + "-code.zip"),
        )

    except Exception:
        pass

    # Copy in the DATA readme.
    # open(os.path.join(id, "README.txt"), "a").close()

    # Save the experiment id.
    with open(os.path.join("data", id, "experiment_id.md"), "a+") as file:
        file.write(id)

    # Zip data
    src = os.path.join("data", id)
    dst = os.path.join("data", id + "-data.zip")
    archive_data(id, src, dst)

    cwd = os.getcwd()
    data_filename = "{}-data.zip".format(id)
    path_to_data = os.path.join(cwd, "data", data_filename)

    # Backup data on S3 unless run locally
    if not local:
        bucket = user_s3_bucket()
        bucket.upload_file(path_to_data, data_filename)
        url = _generate_s3_url(bucket, data_filename)

        # Register experiment UUID with dallinger
        register(id, url)

    return path_to_data

Response: Export data from an experiment.


Instruction:
Can you create a Python 3 function that
gives a path to a zip file created with export() recreate the database with the data stored in the included. csv files.

Response:
def ingest_zip(path, engine=None):
    """Given a path to a zip file created with `export()`, recreate the
    database with the data stored in the included .csv files.
    """
    import_order = [
        "network",
        "participant",
        "node",
        "info",
        "notification",
        "question",
        "transformation",
        "vector",
        "transmission",
    ]
    with ZipFile(path, "r") as archive:
        filenames = archive.namelist()
        for name in import_order:
            filename = [f for f in filenames if name in f][0]
            model_name = name.capitalize()
            model = getattr(models, model_name)
            file = archive.open(filename)
            if six.PY3:
                file = io.TextIOWrapper(file, encoding="utf8", newline="")
            ingest_to_model(file, model, engine)


Instruction:
Can you generate the documentation for the following Python 3 function
def ingest_to_model(file, model, engine=None):
    if engine is None:
        engine = db.engine
    reader = csv.reader(file)
    columns = tuple('"{}"'.format(n) for n in next(reader))
    postgres_copy.copy_from(
        file, model, engine, columns=columns, format="csv", HEADER=False
    )
    fix_autoincrement(model.__table__.name)

Response: Load data from a CSV file into a SQLAlchemy model class.


Instruction:
Can you tell what is the following Python 3 function doing
def _get_or_create_s3_bucket(s3, name):
    exists = True
    try:
        s3.meta.client.head_bucket(Bucket=name)
    except botocore.exceptions.ClientError as e:
        error_code = int(e.response["Error"]["Code"])
        if error_code == 404:
            exists = False
        else:
            raise

    if not exists:
        s3.create_bucket(Bucket=name)

    return s3.Bucket(name)

Response: Get an S3 bucket resource after making sure it exists


Instruction:
Write a Python 3 function for
getting the user s S3 bucket.

Response:
def user_s3_bucket(canonical_user_id=None):
    """Get the user's S3 bucket."""
    s3 = _s3_resource()
    if not canonical_user_id:
        canonical_user_id = _get_canonical_aws_user_id(s3)

    s3_bucket_name = "dallinger-{}".format(
        hashlib.sha256(canonical_user_id.encode("utf8")).hexdigest()[0:8]
    )

    return _get_or_create_s3_bucket(s3, s3_bucket_name)


Instruction:
Can you tell what is the following Python 3 function doing
def _contents(self):
        images = ["owl.png"]

        # We're selecting from a list of only one item here, but it's a useful
        # technique to demonstrate:
        image = random.choice(images)

        image_path = os.path.join("static", "stimuli", image)

        uri_encoded_image = "data:image/png;base64," + base64.b64encode(
            open(image_path, "rb").read()
        )

        return json.dumps({"image": uri_encoded_image, "sketch": ""})

Response: Define the contents of new Infos.


Instruction:
Make a summary of the following Python 3 code
def transmit_by_fitness(from_whom, to_whom=None, what=None):
    parents = from_whom
    parent_fs = [p.fitness for p in parents]
    parent_probs = [(f / (1.0 * sum(parent_fs))) for f in parent_fs]

    rnd = random.random()
    temp = 0.0
    for i, probability in enumerate(parent_probs):
        temp += probability
        if temp > rnd:
            parent = parents[i]
            break

    parent.transmit(what=what, to_whom=to_whom)

Response: Choose a parent with probability proportional to their fitness.


Instruction:
Make a summary of the following Python 3 code
def get_messenger(config):
    email_settings = EmailConfig(config)
    if config.get("mode") == "debug":
        return DebugMessenger(email_settings)
    problems = email_settings.validate()
    if problems:
        logger.info(problems + " Will log errors instead of emailing them.")
        return DebugMessenger(email_settings)
    return EmailingMessenger(email_settings)

Response: Return an appropriate Messenger.


Instruction:
How would you code a function in Python 3 to
validate that the config is valid.

Response:
def validate(self):
        """Could this config be used to send a real email?"""
        missing = []
        for k, v in self._map.items():
            attr = getattr(self, k, False)
            if not attr or attr == CONFIG_PLACEHOLDER:
                missing.append(v)
        if missing:
            return "Missing or invalid config values: {}".format(
                ", ".join(sorted(missing))
            )


Instruction:
Explain what the following Python 3 code does
def sessions_scope(local_session, commit=False):
    try:
        yield local_session
        if commit:
            local_session.commit()
            logger.debug("DB session auto-committed as requested")
    except Exception as e:
        # We log the exception before re-raising it, in case the rollback also
        # fails
        logger.exception("Exception during scoped worker transaction, " "rolling back.")
        # This rollback is potentially redundant with the remove call below,
        # depending on how the scoped session is configured, but we'll be
        # explicit here.
        local_session.rollback()
        raise e
    finally:
        local_session.remove()
        logger.debug("Session complete, db session closed")

Response: Provide a transactional scope around a series of operations.


Instruction:
Write a Python 3 script for
managing contexts and add debugging to db sessions.

Response:
def scoped_session_decorator(func):
    """Manage contexts and add debugging to db sessions."""

    @wraps(func)
    def wrapper(*args, **kwargs):
        with sessions_scope(session):
            # The session used in func comes from the funcs globals, but
            # it will be a proxied thread local var from the session
            # registry, and will therefore be identical to the one returned
            # by the context manager above.
            logger.debug("Running worker %s in scoped DB session", func.__name__)
            return func(*args, **kwargs)

    return wrapper


Instruction:
Explain what the following Python 3 code does
def init_db(drop_all=False, bind=engine):
    try:
        if drop_all:
            Base.metadata.drop_all(bind=bind)
        Base.metadata.create_all(bind=bind)
    except OperationalError as err:
        msg = 'password authentication failed for user "dallinger"'
        if msg in err.message:
            sys.stderr.write(db_user_warning)
        raise

    return session

Response: Initialize the database optionally dropping existing tables optionally creating new tables.


Instruction:
How would you explain what the following Python 3 function does
def serialized(func):

    @wraps(func)
    def wrapper(*args, **kw):
        attempts = 100
        session.remove()
        while attempts > 0:
            try:
                session.connection(
                    execution_options={"isolation_level": "SERIALIZABLE"}
                )
                result = func(*args, **kw)
                session.commit()
                return result
            except OperationalError as exc:
                session.rollback()
                if isinstance(exc.orig, TransactionRollbackError):
                    if attempts > 0:
                        attempts -= 1
                    else:
                        raise Exception(
                            "Could not commit serialized transaction "
                            "after 100 attempts."
                        )
                else:
                    raise
            finally:
                session.remove()
            time.sleep(random.expovariate(0.5))

    return wrapper

Response: A decorator that commits and retries the transaction if the function fails.


Instruction:
Here you have a function in Python 3, explain what it does
def chat(ws):
    lag_tolerance_secs = float(request.args.get("tolerance", 0.1))
    client = Client(ws, lag_tolerance_secs=lag_tolerance_secs)
    client.subscribe(request.args.get("channel"))
    gevent.spawn(client.heartbeat)
    client.publish()

Response: Relay chat messages to and from clients.


Instruction:
Make a summary of the following Python 3 code
def subscribe(self, client):
        self.clients.append(client)
        log("Subscribed client {} to channel {}".format(client, self.name))

Response: Subscribe a client to the channel.


Instruction:
Given the following Python 3 function, write the documentation
def listen(self):
        pubsub = redis_conn.pubsub()
        name = self.name
        if isinstance(name, six.text_type):
            name = name.encode("utf-8")
        try:
            pubsub.subscribe([name])
        except ConnectionError:
            app.logger.exception("Could not connect to redis.")
        log("Listening on channel {}".format(self.name))
        for message in pubsub.listen():
            data = message.get("data")
            if message["type"] == "message" and data != "None":
                channel = message["channel"]
                payload = "{}:{}".format(channel.decode("utf-8"), data.decode("utf-8"))
                for client in self.clients:
                    gevent.spawn(client.send, payload)
            gevent.sleep(0.001)

Response: Listen to all subscribed clients.


Instruction:
How would you explain what the following Python 3 function does
def subscribe(self, client, channel_name):
        if channel_name not in self.channels:
            self.channels[channel_name] = channel = Channel(channel_name)
            channel.start()

        self.channels[channel_name].subscribe(client)

Response: Register a new client to receive messages on a channel.


Instruction:
Can you generate the documentation for the following Python 3 function
def unsubscribe(self, client):
        for channel in self.channels.values():
            channel.unsubscribe(client)

Response: Unsubscribe a client from all channels.


Instruction:
How would you explain what the following Python 3 function does
def send(self, message):
        if isinstance(message, bytes):
            message = message.decode("utf8")

        with self.send_lock:
            try:
                self.ws.send(message)
            except socket.error:
                chat_backend.unsubscribe(self)

Response: Send a single message to the websocket.


Instruction:
Make a summary of the following Python 3 code
def heartbeat(self):
        while not self.ws.closed:
            gevent.sleep(HEARTBEAT_DELAY)
            gevent.spawn(self.send, "ping")

Response: Send a ping to the websocket periodically.


Instruction:
Explain what the following Python 3 code does
def publish(self):
        while not self.ws.closed:
            # Sleep to prevent *constant* context-switches.
            gevent.sleep(self.lag_tolerance_secs)
            message = self.ws.receive()
            if message is not None:
                channel_name, data = message.split(":", 1)
                redis_conn.publish(channel_name, data)

Response: Relay messages from client to redis.


Instruction:
Create a Python 3 function for
returning a Selenium WebDriver instance of the type requested in the configuration.

Response:
def driver(self):
        """Returns a Selenium WebDriver instance of the type requested in the
        configuration."""
        from dallinger.config import get_config

        config = get_config()
        if not config.ready:
            config.load()
        driver_url = config.get("webdriver_url", None)
        driver_type = config.get("webdriver_type")
        driver = None

        if driver_url:
            capabilities = CAPABILITY_MAP.get(driver_type.lower())
            if capabilities is None:
                raise ValueError(
                    "Unsupported remote webdriver_type: {}".format(driver_type)
                )
            driver = webdriver.Remote(
                desired_capabilities=capabilities, command_executor=driver_url
            )
        else:
            driver_class = DRIVER_MAP.get(driver_type.lower())
            if driver_class is not None:
                driver = driver_class()

        if driver is None:
            raise ValueError("Unsupported webdriver_type: {}".format(driver_type))

        driver.set_window_size(1024, 768)
        logger.info("Created {} webdriver.".format(driver_type))
        return driver


Instruction:
How would you explain what the following Python 3 function does
def sign_up(self):
        try:
            self.driver.get(self.URL)
            logger.info("Loaded ad page.")
            begin = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CLASS_NAME, "btn-primary"))
            )
            begin.click()
            logger.info("Clicked begin experiment button.")
            WebDriverWait(self.driver, 10).until(lambda d: len(d.window_handles) == 2)
            self.driver.switch_to_window(self.driver.window_handles[-1])
            self.driver.set_window_size(1024, 768)
            logger.info("Switched to experiment popup.")
            consent = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.ID, "consent"))
            )
            consent.click()
            logger.info("Clicked consent button.")
            participate = WebDriverWait(self.driver, 10).until(
                EC.element_to_be_clickable((By.CLASS_NAME, "btn-success"))
            )
            participate.click()
            logger.info("Clicked start button.")
            return True
        except TimeoutException:
            logger.error("Error during experiment sign up.")
            return False

Response: Accept HIT give consent and start experiment.


Instruction:
Here you have a function in Python 3, explain what it does
def complete_questionnaire(self):
        logger.info("Complete questionnaire.")
        difficulty = self.driver.find_element_by_id("difficulty")
        difficulty.value = "4"
        engagement = self.driver.find_element_by_id("engagement")
        engagement.value = "3"

Response: Complete the standard debriefing form.


Instruction:
Implement a Python 3 function for
signing off the current experiment.

Response:
def sign_off(self):
        """Submit questionnaire and finish.

        This uses Selenium to click the submit button on the questionnaire
        and return to the original window.
        """
        try:
            logger.info("Bot player signing off.")
            feedback = WebDriverWait(self.driver, 20).until(
                EC.presence_of_element_located((By.ID, "submit-questionnaire"))
            )
            self.complete_questionnaire()
            feedback.click()
            logger.info("Clicked submit questionnaire button.")
            self.driver.switch_to_window(self.driver.window_handles[0])
            self.driver.set_window_size(1024, 768)
            logger.info("Switched back to initial window.")
            return True
        except TimeoutException:
            logger.error("Error during experiment sign off.")
            return False


Instruction:
Write a Python 3 function for
sending worker status ('worker_complete' worker_failed worker_id

Response:
def complete_experiment(self, status):
        """Sends worker status ('worker_complete' or 'worker_failed')
        to the experiment server.
        """
        url = self.driver.current_url
        p = urllib.parse.urlparse(url)
        complete_url = "%s://%s/%s?participant_id=%s"
        complete_url = complete_url % (p.scheme, p.netloc, status, self.participant_id)
        self.driver.get(complete_url)
        logger.info("Forced call to %s: %s" % (status, complete_url))


Instruction:
Create a Python 3 function for
signing up run the participate method then sign off and close the driver.

Response:
def run_experiment(self):
        """Sign up, run the ``participate`` method, then sign off and close
        the driver."""
        try:
            self.sign_up()
            self.participate()
            if self.sign_off():
                self.complete_experiment("worker_complete")
            else:
                self.complete_experiment("worker_failed")
        finally:
            self.driver.quit()


Instruction:
Implement a Python 3 function for
running the phases of interacting with the experiment .

Response:
def run_experiment(self):
        """Runs the phases of interacting with the experiment
        including signup, participation, signoff, and recording completion.
        """
        self.sign_up()
        self.participate()
        if self.sign_off():
            self.complete_experiment("worker_complete")
        else:
            self.complete_experiment("worker_failed")


Instruction:
Can you write a function in Python 3 where it
signs up a participant for the experiment.

Response:
def sign_up(self):
        """Signs up a participant for the experiment.

        This is done using a POST request to the /participant/ endpoint.
        """
        self.log("Bot player signing up.")
        self.subscribe_to_quorum_channel()
        while True:
            url = (
                "{host}/participant/{self.worker_id}/"
                "{self.hit_id}/{self.assignment_id}/"
                "debug?fingerprint_hash={hash}&recruiter=bots:{bot_name}".format(
                    host=self.host,
                    self=self,
                    hash=uuid.uuid4().hex,
                    bot_name=self.__class__.__name__,
                )
            )
            try:
                result = requests.post(url)
                result.raise_for_status()
            except RequestException:
                self.stochastic_sleep()
                continue

            if result.json()["status"] == "error":
                self.stochastic_sleep()
                continue

            self.on_signup(result.json())
            return True


Instruction:
Can you generate the documentation for the following Python 3 function
def complete_experiment(self, status):
        self.log("Bot player completing experiment. Status: {}".format(status))
        while True:
            url = "{host}/{status}?participant_id={participant_id}".format(
                host=self.host, participant_id=self.participant_id, status=status
            )
            try:
                result = requests.get(url)
                result.raise_for_status()
            except RequestException:
                self.stochastic_sleep()
                continue
            return result

Response: Record worker completion status to the experiment server.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def subscribe_to_quorum_channel(self):
        from dallinger.experiment_server.sockets import chat_backend

        self.log("Bot subscribing to quorum channel.")
        chat_backend.subscribe(self, "quorum")

Response: Subscribe to the quorum channel.


Instruction:
Make a summary of the following Python 3 code
def complete_questionnaire(self):
        while True:
            data = {
                "question": "questionnaire",
                "number": 1,
                "response": json.dumps(self.question_responses),
            }
            url = "{host}/question/{self.participant_id}".format(
                host=self.host, self=self
            )
            try:
                result = requests.post(url, data=data)
                result.raise_for_status()
            except RequestException:
                self.stochastic_sleep()
                continue
            return True

Response: Complete the standard debriefing form.


Instruction:
Can you tell what is the following Python 3 function doing
def check_credentials(self):
        try:
            return bool(self.mturk.get_account_balance())
        except NoCredentialsError:
            raise MTurkServiceException("No AWS credentials set!")
        except ClientError:
            raise MTurkServiceException("Invalid AWS credentials!")
        except Exception as ex:
            raise MTurkServiceException(
                "Error checking credentials: {}".format(str(ex))
            )

Response: Verifies the credentials for the key and secret combination by making a balance inquiry


Instruction:
Write a Python 3 function for
setting a REST endpoint to recieve notifications about the HIT.

Response:
def set_rest_notification(self, url, hit_type_id):
        """Set a REST endpoint to recieve notifications about the HIT
        The newer AWS MTurk API does not support this feature, which means we
        cannot use boto3 here. Instead, we make the call manually after
        assembling a properly signed request.
        """
        ISO8601 = "%Y-%m-%dT%H:%M:%SZ"
        notification_version = "2006-05-05"
        API_version = "2014-08-15"
        data = {
            "AWSAccessKeyId": self.aws_key,
            "HITTypeId": hit_type_id,
            "Notification.1.Active": "True",
            "Notification.1.Destination": url,
            "Notification.1.EventType.1": "AssignmentAccepted",
            "Notification.1.EventType.2": "AssignmentAbandoned",
            "Notification.1.EventType.3": "AssignmentReturned",
            "Notification.1.EventType.4": "AssignmentSubmitted",
            "Notification.1.EventType.5": "HITReviewable",
            "Notification.1.EventType.6": "HITExpired",
            "Notification.1.Transport": "REST",
            "Notification.1.Version": notification_version,
            "Operation": "SetHITTypeNotification",
            "SignatureVersion": "1",
            "Timestamp": time.strftime(ISO8601, time.gmtime()),
            "Version": API_version,
        }
        query_string, signature = self._calc_old_api_signature(data)
        body = query_string + "&Signature=" + urllib.parse.quote_plus(signature)
        data["Signature"] = signature
        headers = {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Content-Length": str(len(body)),
            "Host": self.legacy_host,
        }
        resp = requests.post("https://" + self.legacy_host, headers=headers, data=body)
        return "<IsValid>True</IsValid>" in resp.text


Instruction:
Write a Python 3 function for
registering HIT Type for this HIT and return the HIT Type s ID.

Response:
def register_hit_type(
        self, title, description, reward, duration_hours, keywords, qualifications
    ):
        """Register HIT Type for this HIT and return the type's ID, which
        is required for creating a HIT.
        """
        reward = str(reward)
        duration_secs = int(datetime.timedelta(hours=duration_hours).total_seconds())
        hit_type = self.mturk.create_hit_type(
            Title=title,
            Description=description,
            Reward=reward,
            AssignmentDurationInSeconds=duration_secs,
            Keywords=",".join(keywords),
            AutoApprovalDelayInSeconds=0,
            QualificationRequirements=qualifications,
        )

        return hit_type["HITTypeId"]


Instruction:
Implement a Python 3 function for
translating restrictions and qualifications to boto Qualifications objects

Response:
def build_hit_qualifications(self, approve_requirement, restrict_to_usa, blacklist):
        """Translate restrictions/qualifications to boto Qualifications objects

        @blacklist is a list of names for Qualifications workers must
        not already hold in order to see and accept the HIT.
        """
        quals = [
            {
                "QualificationTypeId": PERCENTAGE_APPROVED_REQUIREMENT_ID,
                "Comparator": "GreaterThanOrEqualTo",
                "IntegerValues": [approve_requirement],
                "RequiredToPreview": True,
            }
        ]
        if restrict_to_usa:
            quals.append(
                {
                    "QualificationTypeId": LOCALE_REQUIREMENT_ID,
                    "Comparator": "EqualTo",
                    "LocaleValues": [{"Country": "US"}],
                    "RequiredToPreview": True,
                }
            )
        if blacklist is not None:
            for item in blacklist:
                qtype = self.get_qualification_type_by_name(item)
                if qtype:
                    quals.append(
                        {
                            "QualificationTypeId": qtype["id"],
                            "Comparator": "DoesNotExist",
                            "RequiredToPreview": True,
                        }
                    )
        return quals


Instruction:
Given the following Python 3 function, write the documentation
def create_qualification_type(self, name, description, status="Active"):
        try:
            response = self.mturk.create_qualification_type(
                Name=name, Description=description, QualificationTypeStatus=status
            )
        except Exception as ex:
            if "already created a QualificationType with this name" in str(ex):
                raise DuplicateQualificationNameError(str(ex))

        return self._translate_qtype(response["QualificationType"])

Response: Create a new qualification type.


Instruction:
Can you tell what is the following Python 3 function doing
def get_qualification_type_by_name(self, name):
        max_fuzzy_matches_to_check = 100
        query = name.upper()
        start = time.time()
        args = {
            "Query": query,
            "MustBeRequestable": False,
            "MustBeOwnedByCaller": True,
            "MaxResults": max_fuzzy_matches_to_check,
        }
        results = self.mturk.list_qualification_types(**args)["QualificationTypes"]
        # This loop is largely for tests, because there's some indexing that
        # needs to happen on MTurk for search to work:
        while not results and time.time() - start < self.max_wait_secs:
            time.sleep(1)
            results = self.mturk.list_qualification_types(**args)["QualificationTypes"]

        if not results:
            return None

        qualifications = [self._translate_qtype(r) for r in results]
        if len(qualifications) > 1:
            for qualification in qualifications:
                if qualification["name"].upper() == query:
                    return qualification

            raise MTurkServiceException("{} was not a unique name".format(query))

        return qualifications[0]

Response: Return a Qualification Type by name.


Instruction:
Explain what the following Python 3 code does
def assign_qualification(self, qualification_id, worker_id, score, notify=False):
        return self._is_ok(
            self.mturk.associate_qualification_with_worker(
                QualificationTypeId=qualification_id,
                WorkerId=worker_id,
                IntegerValue=score,
                SendNotification=notify,
            )
        )

Response: Assign a worker to a specific qualification


Instruction:
How would you explain what the following Python 3 function does
def increment_qualification_score(self, name, worker_id, notify=False):
        result = self.get_current_qualification_score(name, worker_id)
        current_score = result["score"] or 0
        new_score = current_score + 1
        qtype_id = result["qtype"]["id"]
        self.assign_qualification(qtype_id, worker_id, new_score, notify)

        return {"qtype": result["qtype"], "score": new_score}

Response:
Increment the current qualification score for a worker on a
        qualification with the provided name.


Instruction:
Can you create a Python 3 function that
gets a worker s qualification score as an iteger.

Response:
def get_qualification_score(self, qualification_id, worker_id):
        """Return a worker's qualification score as an iteger.
        """
        try:
            response = self.mturk.get_qualification_score(
                QualificationTypeId=qualification_id, WorkerId=worker_id
            )
        except ClientError as ex:
            error = str(ex)
            if "does not exist" in error:
                raise WorkerLacksQualification(
                    "Worker {} does not have qualification {}.".format(
                        worker_id, qualification_id
                    )
                )
            if "operation can be called with a status of: Granted" in error:
                raise RevokedQualification(
                    "Worker {} has had qualification {} revoked.".format(
                        worker_id, qualification_id
                    )
                )

            raise MTurkServiceException(error)
        return response["Qualification"]["IntegerValue"]


Instruction:
Create a Python 3 function to
return the current score for a worker on a qualification with the provided name.

Response:
def get_current_qualification_score(self, name, worker_id):
        """Return the current score for a worker, on a qualification with the
        provided name.
        """
        qtype = self.get_qualification_type_by_name(name)
        if qtype is None:
            raise QualificationNotFoundException(
                'No Qualification exists with name "{}"'.format(name)
            )
        try:
            score = self.get_qualification_score(qtype["id"], worker_id)
        except (WorkerLacksQualification, RevokedQualification):
            score = None

        return {"qtype": qtype, "score": score}


Instruction:
Can you create a Python 3 function that
removes a qualification type from the database

Response:
def dispose_qualification_type(self, qualification_id):
        """Remove a qualification type we created"""
        return self._is_ok(
            self.mturk.delete_qualification_type(QualificationTypeId=qualification_id)
        )


Instruction:
How would you explain what the following Python 3 function does
def get_workers_with_qualification(self, qualification_id):
        done = False
        next_token = None
        while not done:
            if next_token is not None:
                response = self.mturk.list_workers_with_qualification_type(
                    QualificationTypeId=qualification_id,
                    MaxResults=MAX_SUPPORTED_BATCH_SIZE,
                    Status="Granted",
                    NextToken=next_token,
                )
            else:
                response = self.mturk.list_workers_with_qualification_type(
                    QualificationTypeId=qualification_id,
                    MaxResults=MAX_SUPPORTED_BATCH_SIZE,
                    Status="Granted",
                )
            if response:
                for r in response["Qualifications"]:
                    yield {"id": r["WorkerId"], "score": r["IntegerValue"]}
            if "NextToken" in response:
                next_token = response["NextToken"]
            else:
                done = True

Response: Get workers with the given qualification.


Instruction:
Can you tell what is the following Python 3 function doing
def create_hit(
        self,
        title,
        description,
        keywords,
        reward,
        duration_hours,
        lifetime_days,
        ad_url,
        notification_url,
        approve_requirement,
        max_assignments,
        us_only,
        blacklist=None,
        annotation=None,
    ):
        """Create the actual HIT and return a dict with its useful properties."""
        frame_height = 600
        mturk_question = self._external_question(ad_url, frame_height)
        qualifications = self.build_hit_qualifications(
            approve_requirement, us_only, blacklist
        )
        # We need a HIT_Type in order to register for REST notifications
        hit_type_id = self.register_hit_type(
            title, description, reward, duration_hours, keywords, qualifications
        )
        self.set_rest_notification(notification_url, hit_type_id)

        params = {
            "HITTypeId": hit_type_id,
            "Question": mturk_question,
            "LifetimeInSeconds": int(
                datetime.timedelta(days=lifetime_days).total_seconds()
            ),
            "MaxAssignments": max_assignments,
            "UniqueRequestToken": self._request_token(),
        }
        if annotation:
            params["RequesterAnnotation"] = annotation

        response = self.mturk.create_hit_with_hit_type(**params)
        if "HIT" not in response:
            raise MTurkServiceException("HIT request was invalid for unknown reason.")
        return self._translate_hit(response["HIT"])

Response: Create the HIT and return a dict with its useful properties.


Instruction:
Explain what the following Python 3 code does
def extend_hit(self, hit_id, number, duration_hours=None):
        self.create_additional_assignments_for_hit(hit_id, number)

        if duration_hours is not None:
            self.update_expiration_for_hit(hit_id, duration_hours)

        return self.get_hit(hit_id)

Response: Extend an existing HIT and return an updated description


Instruction:
Can you generate a brief explanation for the following Python 3 code
def expire_hit(self, hit_id):
        try:
            self.mturk.update_expiration_for_hit(HITId=hit_id, ExpireAt=0)
        except Exception as ex:
            raise MTurkServiceException(
                "Failed to expire HIT {}: {}".format(hit_id, str(ex))
            )
        return True

Response:
Expire a HIT, which will change its status to "Reviewable",
        allowing it to be deleted.


Instruction:
How would you implement a function in Python 3 that
grants a bonus to the Worker.

Response:
def grant_bonus(self, assignment_id, amount, reason):
        """Grant a bonus to the MTurk Worker.
        Issues a payment of money from your account to a Worker.  To
        be eligible for a bonus, the Worker must have submitted
        results for one of your HITs, and have had those results
        approved or rejected. This payment happens separately from the
        reward you pay to the Worker when you approve the Worker's
        assignment.
        """
        assignment = self.get_assignment(assignment_id)
        worker_id = assignment["worker_id"]
        amount_str = "{:.2f}".format(amount)
        try:
            return self._is_ok(
                self.mturk.send_bonus(
                    WorkerId=worker_id,
                    BonusAmount=amount_str,
                    AssignmentId=assignment_id,
                    Reason=reason,
                    UniqueRequestToken=self._request_token(),
                )
            )
        except ClientError as ex:
            error = "Failed to pay assignment {} bonus of {}: {}".format(
                assignment_id, amount_str, str(ex)
            )
            raise MTurkServiceException(error)


Instruction:
Given the following Python 3 function, write the documentation
def get_assignment(self, assignment_id):
        try:
            response = self.mturk.get_assignment(AssignmentId=assignment_id)
        except ClientError as ex:
            if "does not exist" in str(ex):
                return None
            raise
        return self._translate_assignment(response["Assignment"])

Response: Get an assignment by ID and reformat the response.


Instruction:
Write a Python 3 script for
approving an assignment initiates two payments from the Requester's Amazon.com account: 1. The Worker who submitted the results is paid the reward specified in the HIT. 2. Amazon Mechanical Turk fees are debited.

Response:
def approve_assignment(self, assignment_id):
        """Approving an assignment initiates two payments from the
        Requester's Amazon.com account:
            1. The Worker who submitted the results is paid
               the reward specified in the HIT.
            2. Amazon Mechanical Turk fees are debited.
        """
        try:
            return self._is_ok(
                self.mturk.approve_assignment(AssignmentId=assignment_id)
            )
        except ClientError as ex:
            assignment = self.get_assignment(assignment_id)
            raise MTurkServiceException(
                "Failed to approve assignment {}, {}: {}".format(
                    assignment_id, str(assignment), str(ex)
                )
            )


Instruction:
Implement a function in Python 3 to
make the specified directory importable as the dallinger_experiment package.

Response:
def initialize_experiment_package(path):
    """Make the specified directory importable as the `dallinger_experiment` package."""
    # Create __init__.py if it doesn't exist (needed for Python 2)
    init_py = os.path.join(path, "__init__.py")
    if not os.path.exists(init_py):
        open(init_py, "a").close()

    # Retain already set experiment module
    if sys.modules.get("dallinger_experiment") is not None:
        return
    dirname = os.path.dirname(path)
    basename = os.path.basename(path)
    sys.path.insert(0, dirname)
    package = __import__(basename)
    if path not in package.__path__:
        raise Exception("Package was not imported from the requested path!")
    sys.modules["dallinger_experiment"] = package
    package.__package__ = "dallinger_experiment"
    sys.path.pop(0)


Instruction:
Write a Python 3 script for
returning json description of a participant.

Response:
def json_data(self):
        """Return json description of a participant."""
        return {
            "type": self.type,
            "recruiter": self.recruiter_id,
            "assignment_id": self.assignment_id,
            "hit_id": self.hit_id,
            "mode": self.mode,
            "end_time": self.end_time,
            "base_pay": self.base_pay,
            "bonus": self.bonus,
            "status": self.status,
        }


Instruction:
Create a Python 3 function for
getting a list of questions associated with this participant. If type is not specified the type of questions is returned.

Response:
def questions(self, type=None):
        """Get questions associated with this participant.

        Return a list of questions associated with the participant. If
        specified, ``type`` filters by class.

        """
        if type is None:
            type = Question

        if not issubclass(type, Question):
            raise TypeError("{} is not a valid question type.".format(type))

        return type.query.filter_by(participant_id=self.id).all()


Instruction:
Write a Python 3 function that can
get all infos created by the participants nodes.

Response:
def infos(self, type=None, failed=False):
        """Get all infos created by the participants nodes.

        Return a list of infos produced by nodes associated with the
        participant. If specified, ``type`` filters by class. By default, failed
        infos are excluded, to include only failed nodes use ``failed=True``,
        for all nodes use ``failed=all``. Note that failed filters the infos,
        not the nodes - infos from all nodes (whether failed or not) can be
        returned.

        """
        nodes = self.nodes(failed="all")
        infos = []
        for n in nodes:
            infos.extend(n.infos(type=type, failed=failed))
        return infos


Instruction:
Explain what the following Python 3 code does
def json_data(self):
        return {
            "number": self.number,
            "type": self.type,
            "participant_id": self.participant_id,
            "question": self.question,
            "response": self.response,
        }

Response: Return json description of a question.


Instruction:
Here you have a function in Python 3, explain what it does
def json_data(self):
        return {
            "type": self.type,
            "max_size": self.max_size,
            "full": self.full,
            "role": self.role,
        }

Response: Return json description of a participant.


Instruction:
Can you generate the documentation for the following Python 3 function
def nodes(self, type=None, failed=False, participant_id=None):
        if type is None:
            type = Node

        if not issubclass(type, Node):
            raise TypeError("{} is not a valid node type.".format(type))

        if failed not in ["all", False, True]:
            raise ValueError("{} is not a valid node failed".format(failed))

        if participant_id is not None:
            if failed == "all":
                return type.query.filter_by(
                    network_id=self.id, participant_id=participant_id
                ).all()
            else:
                return type.query.filter_by(
                    network_id=self.id, participant_id=participant_id, failed=failed
                ).all()
        else:
            if failed == "all":
                return type.query.filter_by(network_id=self.id).all()
            else:
                return type.query.filter_by(failed=failed, network_id=self.id).all()

Response: Get nodes in the network.


Instruction:
Can you generate the documentation for the following Python 3 function
def infos(self, type=None, failed=False):
        if type is None:
            type = Info
        if failed not in ["all", False, True]:
            raise ValueError("{} is not a valid failed".format(failed))

        if failed == "all":
            return type.query.filter_by(network_id=self.id).all()
        else:
            return type.query.filter_by(network_id=self.id, failed=failed).all()

Response: Get infos in the network.


Instruction:
How would you code a function in Python 3 to
get all transmissions in the network.

Response:
def transmissions(self, status="all", failed=False):
        """Get transmissions in the network.

        status { "all", "received", "pending" }
        failed { False, True, "all" }
        To get transmissions from a specific vector, see the
        transmissions() method in class Vector.
        """
        if status not in ["all", "pending", "received"]:
            raise ValueError(
                "You cannot get transmission of status {}.".format(status)
                + "Status can only be pending, received or all"
            )
        if failed not in ["all", False, True]:
            raise ValueError("{} is not a valid failed".format(failed))

        if status == "all":
            if failed == "all":
                return Transmission.query.filter_by(network_id=self.id).all()
            else:
                return Transmission.query.filter_by(
                    network_id=self.id, failed=failed
                ).all()
        else:
            if failed == "all":
                return Transmission.query.filter_by(
                    network_id=self.id, status=status
                ).all()
            else:
                return Transmission.query.filter_by(
                    network_id=self.id, status=status, failed=failed
                ).all()


Instruction:
Can you write a function in Python 3 where it
gets the vectors in the network.

Response:
def vectors(self, failed=False):
        """
        Get vectors in the network.

        failed = { False, True, "all" }
        To get the vectors to/from to a specific node, see Node.vectors().
        """
        if failed not in ["all", False, True]:
            raise ValueError("{} is not a valid vector failed".format(failed))

        if failed == "all":
            return Vector.query.filter_by(network_id=self.id).all()
        else:
            return Vector.query.filter_by(network_id=self.id, failed=failed).all()


Instruction:
Write a Python 3 function that can
get a node s neighbors - nodes that are directly connected to it.

Response:
def neighbors(self, type=None, direction="to", failed=None):
        """Get a node's neighbors - nodes that are directly connected to it.

        Type specifies the class of neighbour and must be a subclass of
        Node (default is Node).
        Connection is the direction of the connections and can be "to"
        (default), "from", "either", or "both".
        """
        # get type
        if type is None:
            type = Node
        if not issubclass(type, Node):
            raise ValueError(
                "{} is not a valid neighbor type,"
                "needs to be a subclass of Node.".format(type)
            )

        # get direction
        if direction not in ["both", "either", "from", "to"]:
            raise ValueError(
                "{} not a valid neighbor connection."
                "Should be both, either, to or from.".format(direction)
            )

        if failed is not None:
            raise ValueError(
                "You should not pass a failed argument to neighbors(). "
                "Neighbors is "
                "unusual in that a failed argument cannot be passed. This is "
                "because there is inherent uncertainty in what it means for a "
                "neighbor to be failed. The neighbors function will only ever "
                "return not-failed nodes connected to you via not-failed "
                "vectors. If you want to do more elaborate queries, for "
                "example, getting not-failed nodes connected to you via failed"
                " vectors, you should do so via sql queries."
            )

        neighbors = []
        # get the neighbours
        if direction == "to":
            outgoing_vectors = (
                Vector.query.with_entities(Vector.destination_id)
                .filter_by(origin_id=self.id, failed=False)
                .all()
            )

            neighbor_ids = [v.destination_id for v in outgoing_vectors]
            if neighbor_ids:
                neighbors = Node.query.filter(Node.id.in_(neighbor_ids)).all()
                neighbors = [n for n in neighbors if isinstance(n, type)]

        if direction == "from":
            incoming_vectors = (
                Vector.query.with_entities(Vector.origin_id)
                .filter_by(destination_id=self.id, failed=False)
                .all()
            )

            neighbor_ids = [v.origin_id for v in incoming_vectors]
            if neighbor_ids:
                neighbors = Node.query.filter(Node.id.in_(neighbor_ids)).all()
                neighbors = [n for n in neighbors if isinstance(n, type)]

        if direction == "either":
            neighbors = list(
                set(
                    self.neighbors(type=type, direction="to")
                    + self.neighbors(type=type, direction="from")
                )
            )

        if direction == "both":
            neighbors = list(
                set(self.neighbors(type=type, direction="to"))
                & set(self.neighbors(type=type, direction="from"))
            )

        return neighbors


Instruction:
Can you write a function in Python 3 where it
checks whether this node is connected [to/from] whom. whom can be a list of nodes or a single node. direction can be "to" (default), "from", "both" or "either". If whom is a single node this method returns a boolean, otherwise it returns a list of booleans

Response:
def is_connected(self, whom, direction="to", failed=None):
        """Check whether this node is connected [to/from] whom.

        whom can be a list of nodes or a single node.
        direction can be "to" (default), "from", "both" or "either".

        If whom is a single node this method returns a boolean,
        otherwise it returns a list of booleans
        """
        if failed is not None:
            raise ValueError(
                "You should not pass a failed argument to is_connected."
                "is_connected is "
                "unusual in that a failed argument cannot be passed. This is "
                "because there is inherent uncertainty in what it means for a "
                "connection to be failed. The is_connected function will only "
                "ever check along not-failed vectors. "
                "If you want to check along failed vectors "
                "you should do so via sql queries."
            )

        # make whom a list
        if isinstance(whom, list):
            is_list = True
        else:
            whom = [whom]
            is_list = False

        whom_ids = [n.id for n in whom]

        # check whom contains only Nodes
        for node in whom:
            if not isinstance(node, Node):
                raise TypeError(
                    "is_connected cannot parse objects of type {}.".format(type(node))
                )

        # check direction
        if direction not in ["to", "from", "either", "both"]:
            raise ValueError(
                "{} is not a valid direction for is_connected".format(direction)
            )

        # get is_connected
        connected = []
        if direction == "to":
            vectors = (
                Vector.query.with_entities(Vector.destination_id)
                .filter_by(origin_id=self.id, failed=False)
                .all()
            )
            destinations = set([v.destination_id for v in vectors])
            for w in whom_ids:
                connected.append(w in destinations)

        elif direction == "from":
            vectors = (
                Vector.query.with_entities(Vector.origin_id)
                .filter_by(destination_id=self.id, failed=False)
                .all()
            )
            origins = set([v.origin_id for v in vectors])
            for w in whom_ids:
                connected.append(w in origins)

        elif direction in ["either", "both"]:

            vectors = (
                Vector.query.with_entities(Vector.origin_id, Vector.destination_id)
                .filter(
                    and_(
                        Vector.failed == false(),
                        or_(
                            Vector.destination_id == self.id,
                            Vector.origin_id == self.id,
                        ),
                    )
                )
                .all()
            )

            destinations = set([v.destination_id for v in vectors])
            origins = set([v.origin_id for v in vectors])

            if direction == "either":
                origins_destinations = destinations.union(origins)

            elif direction == "both":
                origins_destinations = destinations.intersection(origins)

            for w in whom_ids:
                connected.append(w in origins_destinations)

        if is_list:
            return connected
        else:
            return connected[0]


Instruction:
How would you code a function in Python 3 to
get infos that originate from this node.

Response:
def infos(self, type=None, failed=False):
        """Get infos that originate from this node.

        Type must be a subclass of :class:`~dallinger.models.Info`, the default is
        ``Info``. Failed can be True, False or "all".

        """
        if type is None:
            type = Info

        if not issubclass(type, Info):
            raise TypeError(
                "Cannot get infos of type {} " "as it is not a valid type.".format(type)
            )

        if failed not in ["all", False, True]:
            raise ValueError("{} is not a valid vector failed".format(failed))

        if failed == "all":
            return type.query.filter_by(origin_id=self.id).all()
        else:
            return type.query.filter_by(origin_id=self.id, failed=failed).all()


Instruction:
Write a Python 3 function for
getting all infos that have been sent to this node.

Response:
def received_infos(self, type=None, failed=None):
        """Get infos that have been sent to this node.

        Type must be a subclass of info, the default is Info.
        """
        if failed is not None:
            raise ValueError(
                "You should not pass a failed argument to received_infos. "
                "received_infos is "
                "unusual in that a failed argument cannot be passed. This is "
                "because there is inherent uncertainty in what it means for a "
                "received info to be failed. The received_infos function will "
                "only ever check not-failed transmissions. "
                "If you want to check failed transmissions "
                "you should do so via sql queries."
            )

        if type is None:
            type = Info

        if not issubclass(type, Info):
            raise TypeError(
                "Cannot get infos of type {} " "as it is not a valid type.".format(type)
            )

        transmissions = (
            Transmission.query.with_entities(Transmission.info_id)
            .filter_by(destination_id=self.id, status="received", failed=False)
            .all()
        )

        info_ids = [t.info_id for t in transmissions]
        if info_ids:
            return type.query.filter(type.id.in_(info_ids)).all()
        else:
            return []


Instruction:
Create a Python 3 function for
getting all the transformations done by this Node.

Response:
def transformations(self, type=None, failed=False):
        """
        Get Transformations done by this Node.

        type must be a type of Transformation (defaults to Transformation)
        Failed can be True, False or "all"
        """
        if failed not in ["all", False, True]:
            raise ValueError("{} is not a valid transmission failed".format(failed))

        if type is None:
            type = Transformation

        if failed == "all":
            return type.query.filter_by(node_id=self.id).all()
        else:
            return type.query.filter_by(node_id=self.id, failed=failed).all()


Instruction:
Write a Python 3 function that can
fail a node, setting its status to "failed". Also fails all vectors that connect to or from the node. You cannot fail a node that has already failed, but you can fail a dead node. Set node.failed to True and :attr:`~dallinger.models.Node.time_of_death` to now. Instruct all not-failed vectors connected to this node, infos made by this node, transmissions to or from this node and transformations made by this node to fail.

Response:
def fail(self):
        """
        Fail a node, setting its status to "failed".

        Also fails all vectors that connect to or from the node.
        You cannot fail a node that has already failed, but you
        can fail a dead node.

        Set node.failed to True and :attr:`~dallinger.models.Node.time_of_death`
        to now. Instruct all not-failed vectors connected to this node, infos
        made by this node, transmissions to or from this node and
        transformations made by this node to fail.

        """
        if self.failed is True:
            raise AttributeError("Cannot fail {} - it has already failed.".format(self))
        else:
            self.failed = True
            self.time_of_death = timenow()
            self.network.calculate_full()

            for v in self.vectors():
                v.fail()
            for i in self.infos():
                i.fail()
            for t in self.transmissions(direction="all"):
                t.fail()
            for t in self.transformations():
                t.fail()


Instruction:
How would you code a function in Python 3 to
create a vector from self to whom and return a list of vectors between the node and whom.

Response:
def connect(self, whom, direction="to"):
        """Create a vector from self to/from whom.

        Return a list of newly created vector between the node and whom.
        ``whom`` can be a specific node or a (nested) list of nodes. Nodes can
        only connect with nodes in the same network. In addition nodes cannot
        connect with themselves or with Sources. ``direction`` specifies the
        direction of the connection it can be "to" (node -> whom), "from" (whom
        -> node) or both (node <-> whom). The default is "to".

        Whom may be a (nested) list of nodes.

        Will raise an error if:
            1. whom is not a node or list of nodes
            2. whom is/contains a source if direction is to or both
            3. whom is/contains self
            4. whom is/contains a node in a different network

        If self is already connected to/from whom a Warning
        is raised and nothing happens.

        This method returns a list of the vectors created
        (even if there is only one).

        """
        # check direction
        if direction not in ["to", "from", "both"]:
            raise ValueError(
                "{} is not a valid direction for connect()".format(direction)
            )

        # make whom a list
        whom = self.flatten([whom])

        # make the connections
        new_vectors = []
        if direction in ["to", "both"]:
            already_connected_to = self.flatten(
                [self.is_connected(direction="to", whom=whom)]
            )
            for node, connected in zip(whom, already_connected_to):
                if connected:
                    print(
                        "Warning! {} already connected to {}, "
                        "instruction to connect will be ignored.".format(self, node)
                    )
                else:
                    new_vectors.append(Vector(origin=self, destination=node))
        if direction in ["from", "both"]:
            already_connected_from = self.flatten(
                [self.is_connected(direction="from", whom=whom)]
            )
            for node, connected in zip(whom, already_connected_from):
                if connected:
                    print(
                        "Warning! {} already connected from {}, "
                        "instruction to connect will be ignored.".format(self, node)
                    )
                else:
                    new_vectors.append(Vector(origin=node, destination=self))
        return new_vectors


Instruction:
Implement a function in Python 3 to
turn a list of lists into a list.

Response:
def flatten(self, lst):
        """Turn a list of lists into a list."""
        if lst == []:
            return lst
        if isinstance(lst[0], list):
            return self.flatten(lst[0]) + self.flatten(lst[1:])
        return lst[:1] + self.flatten(lst[1:])


Instruction:
How would you implement a function in Python 3 that
transmits one or more infos from one node to another.

Response:
def transmit(self, what=None, to_whom=None):
        """Transmit one or more infos from one node to another.

        "what" dictates which infos are sent, it can be:
            (1) None (in which case the node's _what method is called).
            (2) an Info (in which case the node transmits the info)
            (3) a subclass of Info (in which case the node transmits all
                its infos of that type)
            (4) a list of any combination of the above
        "to_whom" dictates which node(s) the infos are sent to, it can be:
            (1) None (in which case the node's _to_whom method is called)
            (2) a Node (in which case the node transmits to that node)
            (3) a subclass of Node (in which case the node transmits to all
                nodes of that type it is connected to)
            (4) a list of any combination of the above
        Will additionally raise an error if:
            (1) _what() or _to_whom() returns None or a list containing None.
            (2) what is/contains an info that does not originate from the
                transmitting node
            (3) to_whom is/contains a node that the transmitting node does not
                have a not-failed connection with.
        """
        whats = set()
        for what in self.flatten([what]):
            if what is None:
                what = self._what()
            if inspect.isclass(what) and issubclass(what, Info):
                whats.update(self.infos(type=what))
            else:
                whats.add(what)

        to_whoms = set()
        for to_whom in self.flatten([to_whom]):
            if to_whom is None:
                to_whom = self._to_whom()
            if inspect.isclass(to_whom) and issubclass(to_whom, Node):
                to_whoms.update(self.neighbors(direction="to", type=to_whom))
            else:
                to_whoms.add(to_whom)

        transmissions = []
        vectors = self.vectors(direction="outgoing")
        for what in whats:
            for to_whom in to_whoms:
                try:
                    vector = [v for v in vectors if v.destination_id == to_whom.id][0]
                except IndexError:
                    raise ValueError(
                        "{} cannot transmit to {} as it does not have "
                        "a connection to them".format(self, to_whom)
                    )
                t = Transmission(info=what, vector=vector)
                transmissions.append(t)

        return transmissions


Instruction:
Explain what the following Python 3 code does
def fail(self):
        if self.failed is True:
            raise AttributeError("Cannot fail {} - it has already failed.".format(self))
        else:
            self.failed = True
            self.time_of_death = timenow()

            for t in self.transmissions():
                t.fail()

Response: Fail a single instance of the class.


Instruction:
Can you tell what is the following Python 3 function doing
def json_data(self):
        return {
            "type": self.type,
            "origin_id": self.origin_id,
            "network_id": self.network_id,
            "contents": self.contents,
        }

Response: The json representation of an info.


Instruction:
Can you generate the documentation for the following Python 3 function
def transformations(self, relationship="all"):
        if relationship not in ["all", "parent", "child"]:
            raise ValueError(
                "You cannot get transformations of relationship {}".format(relationship)
                + "Relationship can only be parent, child or all."
            )

        if relationship == "all":
            return Transformation.query.filter(
                and_(
                    Transformation.failed == false(),
                    or_(
                        Transformation.info_in == self, Transformation.info_out == self
                    ),
                )
            ).all()

        if relationship == "parent":
            return Transformation.query.filter_by(
                info_in_id=self.id, failed=False
            ).all()

        if relationship == "child":
            return Transformation.query.filter_by(
                info_out_id=self.id, failed=False
            ).all()

Response: Get all the transformations involving this info.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def json_data(self):
        return {
            "vector_id": self.vector_id,
            "origin_id": self.origin_id,
            "destination_id": self.destination_id,
            "info_id": self.info_id,
            "network_id": self.network_id,
            "receive_time": self.receive_time,
            "status": self.status,
        }

Response: The json representation of a transmissions.


Instruction:
Explain what the following Python 3 code does
def json_data(self):
        return {
            "info_in_id": self.info_in_id,
            "info_out_id": self.info_out_id,
            "node_id": self.node_id,
            "network_id": self.network_id,
        }

Response: The json representation of a transformation.


Instruction:
Create a Python 3 function for
returning the absolute path of the root directory of the installed Dallinger package

Response:
def dallinger_package_path():
    """Return the absolute path of the root directory of the installed
    Dallinger package:

    >>> utils.dallinger_package_location()
    '/Users/janedoe/projects/Dallinger3/dallinger'
    """
    dist = get_distribution("dallinger")
    src_base = os.path.join(dist.location, dist.project_name)

    return src_base


Instruction:
Implement a function in Python 3 to
generate random id numbers.

Response:
def generate_random_id(size=6, chars=string.ascii_uppercase + string.digits):
    """Generate random id numbers."""
    return "".join(random.choice(chars) for x in range(size))


Instruction:
Create a Python 3 function for
running a command and returns the return code.

Response:
def run_command(cmd, out, ignore_errors=False):
    """We want to both send subprocess output to stdout or another file
    descriptor as the subprocess runs, *and* capture the actual exception
    message on errors. CalledProcessErrors do not reliably contain the
    underlying exception in either the 'message' or 'out' attributes, so
    we tee the stderr to a temporary file and if a CalledProcessError is
    raised we read its contents to recover stderr
    """
    tempdir = tempfile.mkdtemp()
    output_file = os.path.join(tempdir, "stderr")
    original_cmd = " ".join(cmd)
    p = subprocess.Popen(cmd, stdout=out, stderr=subprocess.PIPE)
    t = subprocess.Popen(["tee", output_file], stdin=p.stderr, stdout=out)
    t.wait()
    p.communicate()
    p.stderr.close()
    if p.returncode != 0 and not ignore_errors:
        with open(output_file, "r") as output:
            error = output.read()
        message = 'Command: "{}": Error: "{}"'.format(
            original_cmd, error.replace("\n", "")
        )
        shutil.rmtree(tempdir, ignore_errors=True)
        raise CommandError(message)

    shutil.rmtree(tempdir, ignore_errors=True)
    return p.returncode


Instruction:
Write a Python 3 function that can
log a message to stdout.

Response:
def log(msg, delay=0.5, chevrons=True, verbose=True):
    """Log a message to stdout."""
    if verbose:
        if chevrons:
            click.echo("\n❯❯ " + msg)
        else:
            click.echo(msg)
        time.sleep(delay)


Instruction:
Implement a Python 3 function for
logging a message to stderr.

Response:
def error(msg, delay=0.5, chevrons=True, verbose=True):
    """Log a message to stdout."""
    if verbose:
        if chevrons:
            click.secho("\n❯❯ " + msg, err=True, fg="red")
        else:
            click.secho(msg, err=True, fg="red")
        time.sleep(delay)


Instruction:
Can you create a Python 3 function that
verifies the experiment id.

Response:
def verify_id(ctx, param, app):
    """Verify the experiment id."""
    if app is None:
        raise TypeError("Select an experiment using the --app parameter.")
    elif app[0:5] == "dlgr-":
        raise ValueError(
            "The --app parameter requires the full "
            "UUID beginning with {}-...".format(app[5:13])
        )
    return app


Instruction:
Given the following Python 3 function, write the documentation
def verify_directory(verbose=True, max_size_mb=50):
    # Check required files
    ok = True
    mb_to_bytes = 1000 * 1000
    expected_files = ["config.txt", "experiment.py"]

    for f in expected_files:
        if os.path.exists(f):
            log("✓ {} is PRESENT".format(f), chevrons=False, verbose=verbose)
        else:
            log("✗ {} is MISSING".format(f), chevrons=False, verbose=verbose)
            ok = False

    # Check size
    max_size = max_size_mb * mb_to_bytes
    size = size_on_copy()
    if size > max_size:
        size_in_mb = round(size / mb_to_bytes)
        log(
            "✗ {}MB is TOO BIG (greater than {}MB)".format(size_in_mb, max_size_mb),
            chevrons=False,
            verbose=verbose,
        )
        ok = False

    return ok

Response: Verify that the current directory looks like a Dallinger experiment and that it does not have unintended contents that will be copied on the current deployment.


Instruction:
Write a Python 3 function that can
perform basic sanity checks on experiment. py.

Response:
def verify_experiment_module(verbose):
    """Perform basic sanity checks on experiment.py.
    """
    ok = True
    if not os.path.exists("experiment.py"):
        return False

    # Bootstrap a package in a temp directory and make it importable:
    temp_package_name = "TEMP_VERIFICATION_PACKAGE"
    tmp = tempfile.mkdtemp()
    clone_dir = os.path.join(tmp, temp_package_name)
    to_ignore = shutil.ignore_patterns(
        os.path.join(".git", "*"), "*.db", "snapshots", "data", "server.log"
    )
    shutil.copytree(os.getcwd(), clone_dir, ignore=to_ignore)
    initialize_experiment_package(clone_dir)
    from dallinger_experiment import experiment

    if clone_dir not in experiment.__file__:
        raise ImportError("Checking the wrong experiment.py... aborting.")
    classes = inspect.getmembers(experiment, inspect.isclass)
    exps = [c for c in classes if (c[1].__bases__[0].__name__ in "Experiment")]

    # Clean up:
    for entry in [k for k in sys.modules if temp_package_name in k]:
        del sys.modules[entry]

    # Run checks:
    if len(exps) == 0:
        log(
            "✗ experiment.py does not define an experiment class.",
            delay=0,
            chevrons=False,
            verbose=verbose,
        )
        ok = False
    elif len(exps) == 1:
        log(
            "✓ experiment.py defines 1 experiment",
            delay=0,
            chevrons=False,
            verbose=verbose,
        )
    else:
        log(
            "✗ experiment.py defines more than one experiment class.",
            delay=0,
            chevrons=False,
            verbose=verbose,
        )
        ok = False

    return ok


Instruction:
Write a Python 3 function for
checking for common or costly errors in experiment configuration.

Response:
def verify_config(verbose=True):
    """Check for common or costly errors in experiment configuration.
    """
    ok = True
    config = get_config()
    if not config.ready:
        config.load()
    # Check base_payment is correct
    try:
        base_pay = config.get("base_payment")
    except KeyError:
        log("✗ No value for base_pay.", delay=0, chevrons=False, verbose=verbose)
    else:
        dollarFormat = "{:.2f}".format(base_pay)

        if base_pay <= 0:
            log(
                "✗ base_payment must be positive value in config.txt.",
                delay=0,
                chevrons=False,
                verbose=verbose,
            )
            ok = False

        if float(dollarFormat) != float(base_pay):
            log(
                "✗ base_payment must be in [dollars].[cents] format in config.txt. Try changing "
                "{0} to {1}.".format(base_pay, dollarFormat),
                delay=0,
                chevrons=False,
                verbose=verbose,
            )
            ok = False

    return ok


Instruction:
How would you code a function in Python 3 to
warn if there are files which conflict with these deployed by Dallinger but always returns True.

Response:
def verify_no_conflicts(verbose=True):
    """Warn if there are filenames which conflict with those deployed by
    Dallinger, but always returns True (meaning "OK").
    """
    conflicts = False

    reserved_files = [
        os.path.join("templates", "complete.html"),
        os.path.join("templates", "error.html"),
        os.path.join("templates", "error-complete.html"),
        os.path.join("templates", "launch.html"),
        os.path.join("templates", "thanks.html"),
        os.path.join("static", "css", "dallinger.css"),
        os.path.join("static", "scripts", "dallinger2.js"),
        os.path.join("static", "scripts", "reqwest.min.js"),
        os.path.join("static", "scripts", "store+json2.min.js"),
        os.path.join("static", "scripts", "tracker.js"),
        os.path.join("static", "robots.txt"),
    ]

    for f in reserved_files:
        if os.path.exists(f):
            log(
                "✗ {} OVERWRITES shared frontend files inserted at run-time".format(f),
                delay=0,
                chevrons=False,
                verbose=verbose,
            )
            conflicts = True

    if not conflicts:
        log("✓ no file conflicts", delay=0, chevrons=False, verbose=verbose)

    return True


Instruction:
Implement a Python 3 function for
performing a series of checks on the current directory to verify that that it s a valid Dallinger experiment.

Response:
def verify_package(verbose=True):
    """Perform a series of checks on the current directory to verify that
    it's a valid Dallinger experiment.
    """
    results = (
        verify_directory(verbose),
        verify_experiment_module(verbose),
        verify_config(verbose),
        verify_no_conflicts(verbose),
    )

    ok = all(results)

    return ok


Instruction:
How would you explain what the following Python 3 function does
def require_exp_directory(f):
    error = "The current directory is not a valid Dallinger experiment."

    @wraps(f)
    def wrapper(**kwargs):
        if not verify_directory(kwargs.get("verbose")):
            raise click.UsageError(error)
        return f(**kwargs)

    return wrapper

Response: Decorator to verify that a command is run inside a valid Dallinger experiment directory.


Instruction:
Implement a Python 3 function for
running the experiment locally.

Response:
def debug(verbose, bot, proxy, exp_config=None):
    """Run the experiment locally."""
    debugger = DebugDeployment(Output(), verbose, bot, proxy, exp_config)
    log(header, chevrons=False)
    debugger.run()


Instruction:
Can you create a Python 3 function that
deploys app using Heroku to the MTurk Sandbox.

Response:
def sandbox(verbose, app):
    """Deploy app using Heroku to the MTurk Sandbox."""
    if app:
        verify_id(None, None, app)
    log(header, chevrons=False)
    _deploy_in_mode("sandbox", app=app, verbose=verbose, log=log)


Instruction:
Can you tell what is the following Python 3 function doing
def qualify(workers, qualification, value, by_name, notify, sandbox):
    if not (workers and qualification and value):
        raise click.BadParameter(
            "Must specify a qualification ID, value/score, and at least one worker ID"
        )
    mturk = _mturk_service_from_config(sandbox)
    if by_name:
        result = mturk.get_qualification_type_by_name(qualification)
        if result is None:
            raise click.BadParameter(
                'No qualification with name "{}" exists.'.format(qualification)
            )

        qid = result["id"]
    else:
        qid = qualification

    click.echo(
        "Assigning qualification {} with value {} to {} worker{}...".format(
            qid, value, len(workers), "s" if len(workers) > 1 else ""
        )
    )
    for worker in workers:
        if mturk.set_qualification_score(qid, worker, int(value), notify=notify):
            click.echo("{} OK".format(worker))

    # print out the current set of workers with the qualification
    results = list(mturk.get_workers_with_qualification(qid))

    click.echo("{} workers with qualification {}:".format(len(results), qid))

    for score, count in Counter([r["score"] for r in results]).items():
        click.echo("{} with value {}".format(count, score))

Response: Assign a qualification to 1 or more workers


Instruction:
Can you generate a brief explanation for the following Python 3 code
def revoke(workers, qualification, by_name, reason, sandbox):
    if not (workers and qualification):
        raise click.BadParameter(
            "Must specify a qualification ID or name, and at least one worker ID"
        )

    mturk = _mturk_service_from_config(sandbox)
    if by_name:
        result = mturk.get_qualification_type_by_name(qualification)
        if result is None:
            raise click.BadParameter(
                'No qualification with name "{}" exists.'.format(qualification)
            )

        qid = result["id"]
    else:
        qid = qualification

    if not click.confirm(
        '\n\nYou are about to revoke qualification "{}" '
        "for these workers:\n\t{}\n\n"
        "This will send an email to each of them from Amazon MTurk. "
        "Continue?".format(qid, "\n\t".join(workers))
    ):
        click.echo("Aborting...")
        return

    for worker in workers:
        if mturk.revoke_qualification(qid, worker, reason):
            click.echo(
                'Revoked qualification "{}" from worker "{}"'.format(qid, worker)
            )

    # print out the current set of workers with the qualification
    results = list(mturk.get_workers_with_qualification(qid))
    click.echo(
        'There are now {} workers with qualification "{}"'.format(len(results), qid)
    )

Response: Revoke a qualification from 1 or more workers


Instruction:
Can you generate the documentation for the following Python 3 function
def hibernate(app):
    log("The database backup URL is...")
    backup_url = data.backup(app)
    log(backup_url)

    log("Scaling down the web servers...")
    heroku_app = HerokuApp(app)
    heroku_app.scale_down_dynos()

    log("Removing addons...")

    addons = [
        "heroku-postgresql",
        # "papertrail",
        "heroku-redis",
    ]
    for addon in addons:
        heroku_app.addon_destroy(addon)

Response: Pause an experiment and remove costly resources.


Instruction:
Implement a function in Python 3 to
list hits for an experiment id.

Response:
def hits(app, sandbox):
    """List hits for an experiment id."""
    hit_list = list(_current_hits(_mturk_service_from_config(sandbox), app))
    out = Output()
    out.log(
        "Found {} hits for this experiment id: {}".format(
            len(hit_list), ", ".join(h["id"] for h in hit_list)
        )
    )


Instruction:
Can you generate the documentation for the following Python 3 function
def expire(app, sandbox, exit=True):
    success = []
    failures = []
    service = _mturk_service_from_config(sandbox)
    hits = _current_hits(service, app)
    for hit in hits:
        hit_id = hit["id"]
        try:
            service.expire_hit(hit_id)
            success.append(hit_id)
        except MTurkServiceException:
            failures.append(hit_id)
    out = Output()
    if success:
        out.log("Expired {} hits: {}".format(len(success), ", ".join(success)))
    if failures:
        out.log(
            "Could not expire {} hits: {}".format(len(failures), ", ".join(failures))
        )
    if not success and not failures:
        out.log("No hits found for this application.")
        if not sandbox:
            out.log(
                "If this experiment was run in the MTurk sandbox, use: "
                "`dallinger expire --sandbox --app {}`".format(app)
            )
    if exit and not success:
        sys.exit(1)

Response: Expire all hits for an experiment id.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def destroy(ctx, app, expire_hit, sandbox):
    if expire_hit:
        ctx.invoke(expire, app=app, sandbox=sandbox, exit=False)
    HerokuApp(app).destroy()

Response: Tear down an experiment server.


Instruction:
Make a summary of the following Python 3 code
def awaken(app, databaseurl):
    id = app
    config = get_config()
    config.load()

    bucket = data.user_s3_bucket()
    key = bucket.lookup("{}.dump".format(id))
    url = key.generate_url(expires_in=300)

    heroku_app = HerokuApp(id, output=None, team=None)
    heroku_app.addon("heroku-postgresql:{}".format(config.get("database_size")))
    time.sleep(60)

    heroku_app.pg_wait()
    time.sleep(10)

    heroku_app.addon("heroku-redis:{}".format(config.get("redis_size")))
    heroku_app.restore(url)

    # Scale up the dynos.
    log("Scaling up the dynos...")
    size = config.get("dyno_type")
    for process in ["web", "worker"]:
        qty = config.get("num_dynos_" + process)
        heroku_app.scale_up_dyno(process, qty, size)
    if config.get("clock_on"):
        heroku_app.scale_up_dyno("clock", 1, size)

Response: Restores the database from a given url.


Instruction:
Create a Python 3 function to
load the database state from an exported zip file and leave the server running until stopping the process with <control > - c.

Response:
def load(app, verbose, replay, exp_config=None):
    """Import database state from an exported zip file and leave the server
    running until stopping the process with <control>-c.
    """
    if replay:
        exp_config = exp_config or {}
        exp_config["replay"] = True
    log(header, chevrons=False)
    loader = LoaderDeployment(app, Output(), verbose, exp_config)
    loader.run()


Instruction:
Here you have a function in Python 3, explain what it does
def monitor(app):
    heroku_app = HerokuApp(dallinger_uid=app)
    webbrowser.open(heroku_app.dashboard_url)
    webbrowser.open("https://requester.mturk.com/mturk/manageHITs")
    heroku_app.open_logs()
    check_call(["open", heroku_app.db_uri])
    while _keep_running():
        summary = get_summary(app)
        click.clear()
        click.echo(header)
        click.echo("\nExperiment {}\n".format(app))
        click.echo(summary)
        time.sleep(10)

Response: Set up application monitoring.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def bot(app, debug):
    if debug is None:
        verify_id(None, None, app)

    (id, tmp) = setup_experiment(log)

    if debug:
        url = debug
    else:
        heroku_app = HerokuApp(dallinger_uid=app)
        worker = generate_random_id()
        hit = generate_random_id()
        assignment = generate_random_id()
        ad_url = "{}/ad".format(heroku_app.url)
        ad_parameters = "assignmentId={}&hitId={}&workerId={}&mode=sandbox"
        ad_parameters = ad_parameters.format(assignment, hit, worker)
        url = "{}?{}".format(ad_url, ad_parameters)
    bot = bot_factory(url)
    bot.run_experiment()

Response: Run the experiment bot.


Instruction:
How would you code a function in Python 3 to
verify that the current directory is compatible with Dallinger.

Response:
def verify():
    """Verify that app is compatible with Dallinger."""
    verbose = True
    log(
        "Verifying current directory as a Dallinger experiment...",
        delay=0,
        verbose=verbose,
    )
    ok = verify_package(verbose=verbose)
    if ok:
        log("✓ Everything looks good!", delay=0, verbose=verbose)
    else:
        log("☹ Some problems were found.", delay=0, verbose=verbose)


Instruction:
How would you implement a function in Python 3 that
gets all the drawings.

Response:
def getdrawings():
    """Get all the drawings."""
    infos = Info.query.all()
    sketches = [json.loads(info.contents) for info in infos]
    return jsonify(drawings=sketches)


Instruction:
Explain what the following Python 3 code does
def index():
    config = _config()
    html = "
Dallinger Experiment in progress

"
    for item in sorted(config.as_dict().items()):
        html += '
{}

{}
'.format(
            *item
        )
    html += "
"
    return html

Response: Index route for the current Dallinger Experiment


Instruction:
Can you implement a function in Python 3 that
returns a generic success response.

Response:
def success_response(**data):
    """Return a generic success response."""
    data_out = {}
    data_out["status"] = "success"
    data_out.update(data)
    js = dumps(data_out, default=date_handler)
    return Response(js, status=200, mimetype="application/json")


Instruction:
Can you generate the documentation for the following Python 3 function
def error_response(
    error_type="Internal server error",
    error_text="",
    status=400,
    participant=None,
    simple=False,
    request_data="",
):
    """Return a generic server error response."""
    last_exception = sys.exc_info()
    if last_exception[0]:
        db.logger.error(
            "Failure for request: {!r}".format(dict(request.args)),
            exc_info=last_exception,
        )

    data = {"status": "error"}

    if simple:
        data["message"] = error_text
    else:
        data["html"] = (
            error_page(
                error_text=error_text,
                error_type=error_type,
                participant=participant,
                request_data=request_data,
            )
            .get_data()
            .decode("utf-8")
        )
    return Response(dumps(data), status=status, mimetype="application/json")

Response: Return a generic server error response.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def error_page(
    participant=None,
    error_text=None,
    compensate=True,
    error_type="default",
    request_data="",
):
    """Render HTML for error page."""
    config = _config()

    if error_text is None:
        error_text = """There has been an error and so you are unable to
        continue, sorry!"""

    if participant is not None:
        hit_id = (participant.hit_id,)
        assignment_id = (participant.assignment_id,)
        worker_id = participant.worker_id
        participant_id = participant.id
    else:
        hit_id = request.form.get("hit_id", "")
        assignment_id = request.form.get("assignment_id", "")
        worker_id = request.form.get("worker_id", "")
        participant_id = request.form.get("participant_id", None)

    if participant_id:
        try:
            participant_id = int(participant_id)
        except (ValueError, TypeError):
            participant_id = None

    return make_response(
        render_template(
            "error.html",
            error_text=error_text,
            compensate=compensate,
            contact_address=config.get("contact_email_on_error"),
            error_type=error_type,
            hit_id=hit_id,
            assignment_id=assignment_id,
            worker_id=worker_id,
            request_data=request_data,
            participant_id=participant_id,
        ),
        500,
    )

Response: Render HTML for error page.


Instruction:
Given the following Python 3 function, write the documentation
def inject_experiment():
    exp = Experiment(session)
    return dict(experiment=exp, env=os.environ)

Response: Inject experiment and enviroment variables into the template context.


Instruction:
Make a summary of the following Python 3 code
def should_show_thanks_page_to(participant):
    if participant is None:
        return False
    status = participant.status
    marked_done = participant.end_time is not None
    ready_for_external_submission = (
        status in ("overrecruited", "working") and marked_done
    )
    assignment_complete = status in ("submitted", "approved")

    return assignment_complete or ready_for_external_submission

Response: Returns True if the participant is shown in the ad. html page.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def advertisement():
    if not ("hitId" in request.args and "assignmentId" in request.args):
        raise ExperimentError("hit_assign_worker_id_not_set_in_mturk")
    config = _config()

    # Browser rule validation, if configured:
    browser = ValidatesBrowser(config)
    if not browser.is_supported(request.user_agent.string):
        raise ExperimentError("browser_type_not_allowed")

    hit_id = request.args["hitId"]
    assignment_id = request.args["assignmentId"]
    app_id = config.get("id", "unknown")
    mode = config.get("mode")
    debug_mode = mode == "debug"
    worker_id = request.args.get("workerId")
    participant = None

    if worker_id is not None:
        # First check if this workerId has completed the task before
        # under a different assignment (v1):
        already_participated = bool(
            models.Participant.query.filter(
                models.Participant.assignment_id != assignment_id
            )
            .filter(models.Participant.worker_id == worker_id)
            .count()
        )

        if already_participated and not debug_mode:
            raise ExperimentError("already_did_exp_hit")

        # Next, check for participants already associated with this very
        # assignment, and retain their status, if found:
        try:
            participant = (
                models.Participant.query.filter(models.Participant.hit_id == hit_id)
                .filter(models.Participant.assignment_id == assignment_id)
                .filter(models.Participant.worker_id == worker_id)
                .one()
            )
        except exc.SQLAlchemyError:
            pass

    recruiter_name = request.args.get("recruiter")
    if recruiter_name:
        recruiter = recruiters.by_name(recruiter_name)
    else:
        recruiter = recruiters.from_config(config)
        recruiter_name = recruiter.nickname

    if should_show_thanks_page_to(participant):
        # They've either done, or they're from a recruiter that requires
        # submission of an external form to complete their participation.
        return render_template(
            "thanks.html",
            hitid=hit_id,
            assignmentid=assignment_id,
            workerid=worker_id,
            external_submit_url=recruiter.external_submission_url,
            mode=config.get("mode"),
            app_id=app_id,
        )
    if participant and participant.status == "working":
        # Once participants have finished the instructions, we do not allow
        # them to start the task again.
        raise ExperimentError("already_started_exp_mturk")

    # Participant has not yet agreed to the consent. They might not
    # even have accepted the HIT.
    return render_template(
        "ad.html",
        recruiter=recruiter_name,
        hitid=hit_id,
        assignmentid=assignment_id,
        workerid=worker_id,
        mode=config.get("mode"),
        app_id=app_id,
    )

Response: This function returns the url that will be used to display the ad for the given hit.


Instruction:
Make a summary of the following Python 3 code
def summary():
    exp = Experiment(session)
    state = {
        "status": "success",
        "summary": exp.log_summary(),
        "completed": exp.is_complete(),
    }
    unfilled_nets = (
        models.Network.query.filter(models.Network.full != true())
        .with_entities(models.Network.id, models.Network.max_size)
        .all()
    )
    working = (
        models.Participant.query.filter_by(status="working")
        .with_entities(func.count(models.Participant.id))
        .scalar()
    )
    state["unfilled_networks"] = len(unfilled_nets)
    nodes_remaining = 0
    required_nodes = 0
    if state["unfilled_networks"] == 0:
        if working == 0 and state["completed"] is None:
            state["completed"] = True
    else:
        for net in unfilled_nets:
            node_count = (
                models.Node.query.filter_by(network_id=net.id, failed=False)
                .with_entities(func.count(models.Node.id))
                .scalar()
            )
            net_size = net.max_size
            required_nodes += net_size
            nodes_remaining += net_size - node_count
    state["nodes_remaining"] = nodes_remaining
    state["required_nodes"] = required_nodes

    if state["completed"] is None:
        state["completed"] = False

    # Regenerate a waiting room message when checking status
    # to counter missed messages at the end of the waiting room
    nonfailed_count = models.Participant.query.filter(
        (models.Participant.status == "working")
        | (models.Participant.status == "overrecruited")
        | (models.Participant.status == "submitted")
        | (models.Participant.status == "approved")
    ).count()
    exp = Experiment(session)
    overrecruited = exp.is_overrecruited(nonfailed_count)
    if exp.quorum:
        quorum = {"q": exp.quorum, "n": nonfailed_count, "overrecruited": overrecruited}
        db.queue_message(WAITING_ROOM_CHANNEL, dumps(quorum))

    return Response(dumps(state), status=200, mimetype="application/json")

Response: Summarize the participants status codes.


Instruction:
How would you explain what the following Python 3 function does
def experiment_property(prop):
    exp = Experiment(session)
    try:
        value = exp.public_properties[prop]
    except KeyError:
        abort(404)
    return success_response(**{prop: value})

Response: Get a property of the experiment by name.


Instruction:
Make a summary of the following Python 3 code
def consent():
    config = _config()
    return render_template(
        "consent.html",
        hit_id=request.args["hit_id"],
        assignment_id=request.args["assignment_id"],
        worker_id=request.args["worker_id"],
        mode=config.get("mode"),
    )

Response: Return the consent form. Here for backwards - compatibility with 2. x.


Instruction:
Implement a function in Python 3 to
get a parameter from a request.

Response:
def request_parameter(parameter, parameter_type=None, default=None, optional=False):
    """Get a parameter from a request.

    parameter is the name of the parameter you are looking for
    parameter_type is the type the parameter should have
    default is the value the parameter takes if it has not been passed

    If the parameter is not found and no default is specified,
    or if the parameter is found but is of the wrong type
    then a Response object is returned
    """
    exp = Experiment(session)

    # get the parameter
    try:
        value = request.values[parameter]
    except KeyError:
        # if it isnt found use the default, or return an error Response
        if default is not None:
            return default
        elif optional:
            return None
        else:
            msg = "{} {} request, {} not specified".format(
                request.url, request.method, parameter
            )
            return error_response(error_type=msg)

    # check the parameter type
    if parameter_type is None:
        # if no parameter_type is required, return the parameter as is
        return value
    elif parameter_type == "int":
        # if int is required, convert to an int
        try:
            value = int(value)
            return value
        except ValueError:
            msg = "{} {} request, non-numeric {}: {}".format(
                request.url, request.method, parameter, value
            )
            return error_response(error_type=msg)
    elif parameter_type == "known_class":
        # if its a known class check against the known classes
        try:
            value = exp.known_classes[value]
            return value
        except KeyError:
            msg = "{} {} request, unknown_class: {} for parameter {}".format(
                request.url, request.method, value, parameter
            )
            return error_response(error_type=msg)
    elif parameter_type == "bool":
        # if its a boolean, convert to a boolean
        if value in ["True", "False"]:
            return value == "True"
        else:
            msg = "{} {} request, non-boolean {}: {}".format(
                request.url, request.method, parameter, value
            )
            return error_response(error_type=msg)
    else:
        msg = "/{} {} request, unknown parameter type: {} for parameter {}".format(
            request.url, request.method, parameter_type, parameter
        )
        return error_response(error_type=msg)


Instruction:
Create a Python 3 function for
assigning properties to an object.

Response:
def assign_properties(thing):
    """Assign properties to an object.

    When creating something via a post request (e.g. a node), you can pass the
    properties of the object in the request. This function gets those values
    from the request and fills in the relevant columns of the table.
    """
    details = request_parameter(parameter="details", optional=True)
    if details:
        setattr(thing, "details", loads(details))

    for p in range(5):
        property_name = "property" + str(p + 1)
        property = request_parameter(parameter=property_name, optional=True)
        if property:
            setattr(thing, property_name, property)

    session.commit()