Instruction:
How would you explain what the following Python 3 function does
def check_fast(self, reproduce=False, **args):
        hashvals = {}

        fast_check = self.check_file(
            filepaths=self.data.keys(),
            hashvals=hashvals,
            hashfn=fast_hashes,
            shortcircuit=True,
            **args
        )

        if not fast_check:

            # Save all the fast hashes for failed files that we've already
            # calculated
            for filepath in hashvals:
                for hash, val in hashvals[filepath].items():
                    self.data[filepath]['hashes'][hash] = val

            if reproduce:
                for filepath in hashvals:
                    print('Check failed for {0} {1}'
                          ''.format(filepath, hashvals[filepath]))
                    tmphash = {}
                    full_check = self.check_file(
                        filepaths=filepath,
                        hashfn=full_hashes,
                        hashvals=tmphash,
                        shortcircuit=False,
                        **args
                    )

                    if full_check:
                        # File is still ok, so replace fast hashes
                        print('Full hashes ({0}) checked ok'
                              ''.format(full_hashes))
                        print('Updating fast hashes for {0} in {1}'
                              ''.format(filepath, self.path))
                        self.add_fast(filepath, force=True)
                        print('Saving updated manifest')
                        self.needsync = True
                    else:
                        sys.stderr.write(
                            'Run cannot reproduce: manifest {0} is not '
                            'correct\n'.format(self.path)
                        )
                        for path, hashdict in tmphash.items():
                            print('    {0}:'.format(path))
                            for hash, val in hashdict.items():
                                hash_table = self.data[path]['hashes']
                                hash_table_val = hash_table.get(hash, None)
                                print('        {0}: {1} != {2}'
                                      ''.format(hash, val, hash_table_val))
                        sys.exit(1)
            else:
                # Not relevant if full hashes are correct. Regenerate full
                # hashes for all filepaths that failed fast check.
                print('Updating full hashes for {0} files in {1}'
                      ''.format(len(hashvals), self.path))

                # Add all full hashes at once -- much faster. Definitely want
                # to force the full hash to be updated. In the specific case of
                # an empty hash the value will be None, without force it will
                # be written as null.
                self.add(
                    filepaths=list(hashvals.keys()),
                    hashfn=full_hashes,
                    force=True,
                    fullpaths=[self.fullpath(fpath) for fpath
                               in list(hashvals.keys())]
                )

                # Flag need to update version on disk
                self.needsync = True

Response: Check if all filepaths have the same hash value and replace the fast hashes with the new one.


Instruction:
Can you tell what is the following Python 3 function doing
def add_filepath(self, filepath, fullpath, copy=False):

        # Ignore directories
        if os.path.isdir(fullpath):
            return False

        # Ignore anything matching the ignore patterns
        for pattern in self.ignore:
            if fnmatch.fnmatch(os.path.basename(fullpath), pattern):
                return False

        if filepath not in self.data:
            self.data[filepath] = {}

        self.data[filepath]['fullpath'] = fullpath
        if 'hashes' not in self.data[filepath]:
            self.data[filepath]['hashes'] = {hash: None for hash in all_hashes}

        if copy:
            self.data[filepath]['copy'] = copy

        if hasattr(self, 'existing_filepaths'):
            if filepath in self.existing_filepaths:
                self.existing_filepaths.remove(filepath)

        return True

Response: Add filepath & fullpath to the manifest object without hashing.


Instruction:
Write a Python 3 script to
add a filepath to the cache.

Response:
def add_fast(self, filepath, hashfn=None, force=False):
        """
        Bespoke function to add filepaths but set shortcircuit to True, which
        means only the first calculable hash will be stored. In this way only
        one "fast" hashing function need be called for each filepath.
        """
        if hashfn is None:
            hashfn = fast_hashes
        self.add(filepath, hashfn, force, shortcircuit=True)


Instruction:
Can you tell what is the following Python 3 function doing
def copy_file(self, filepath):
        copy_file = False
        try:
            copy_file = self.data[filepath]['copy']
        except KeyError:
            return False
        return copy_file

Response: Returns flag which says to copy rather than link a file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_filepath(self, manifest, filepath, fullpath, copy=False):
        filepath = os.path.normpath(filepath)
        if self.manifests[manifest].add_filepath(filepath, fullpath, copy):
            # Only link if filepath was added
            self.manifests[manifest].make_link(filepath)

Response: Wrapper to add_filepath function in PayuManifest.


Instruction:
How would you explain what the following Python 3 function does
def commit_hash(dir='.'):

    cmd = ['git', 'rev-parse', 'HEAD']

    try:
        with open(os.devnull, 'w') as devnull:
            revision_hash = subprocess.check_output(
                cmd,
                cwd=dir,
                stderr=devnull
            )
        if sys.version_info.major > 2:
            revision_hash = revision_hash.decode('ascii')

        return revision_hash.strip()

    except subprocess.CalledProcessError:
        return None

Response: Return the commit hash for HEAD of the specified directory.


Instruction:
Implement a function in Python 3 to
construct the list of files to be tracked by the runlog.

Response:
def create_manifest(self):
        """Construct the list of files to be tracked by the runlog."""
        config_path = os.path.join(self.expt.control_path,
                                   DEFAULT_CONFIG_FNAME)

        self.manifest = []

        if os.path.isfile(config_path):
            self.manifest.append(config_path)

        for model in self.expt.models:
            config_files = model.config_files + model.optional_config_files

            self.manifest.extend(os.path.join(model.control_path, f)
                                 for f in config_files)

        # Add file manifests to runlog manifest
        for mf in self.expt.manifest:
            self.manifest.append(mf.path)


Instruction:
Here you have a function in Python 3, explain what it does
def push(self):
        expt_name = self.config.get('name', self.expt.name)

        default_ssh_key = 'id_rsa_payu_' + expt_name
        ssh_key = self.config.get('sshid', default_ssh_key)
        ssh_key_path = os.path.join(os.path.expanduser('~'), '.ssh', 'payu',
                                    ssh_key)

        if not os.path.isfile(ssh_key_path):
            print('payu: error: Github SSH key {key} not found.'
                  ''.format(key=ssh_key_path))
            print('payu: error: Run `payu ghsetup` to generate a new key.')
            sys.exit(-1)

        cmd = ('ssh-agent bash -c "ssh-add {key}; git push --all payu"'
               ''.format(key=ssh_key_path))
        subprocess.check_call(shlex.split(cmd), cwd=self.expt.control_path)

Response: Push local runlog changes to the remote runlog repository


Instruction:
Make a summary of the following Python 3 code
def github_setup(self):
        github_auth = self.authenticate()
        github_username = github_auth[0]

        expt_name = self.config.get('name', self.expt.name)
        expt_description = self.expt.config.get('description')
        if not expt_description:
            expt_description = input('Briefly describe the experiment: ')
            assert isinstance(expt_description, str)
        expt_private = self.config.get('private', False)

        # 1. Create the organisation if needed
        github_api_url = 'https://api.github.com'
        org_name = self.config.get('organization')
        if org_name:
            repo_target = org_name

            # Check if org exists
            org_query_url = os.path.join(github_api_url, 'orgs', org_name)
            org_req = requests.get(org_query_url)

            if org_req.status_code == 404:
                # NOTE: Orgs cannot be created via the API
                print('payu: github organization {org} does not exist.'
                      ''.format(org=org_name))
                print('      You must first create this on the website.')

            elif org_req.status_code == 200:
                # TODO: Confirm that the user can interact with the repo
                pass

            else:
                # TODO: Exit with grace
                print('payu: abort!')
                sys.exit(-1)

            repo_query_url = os.path.join(github_api_url, 'orgs', org_name,
                                          'repos')
            repo_api_url = os.path.join(github_api_url, 'repos', org_name,
                                        expt_name)
        else:
            repo_target = github_username

            # Create repo in user account
            repo_query_url = os.path.join(github_api_url, 'user', 'repos')
            repo_api_url = os.path.join(github_api_url, 'repos',
                                        github_username, expt_name)

        # 2. Create the remote repository
        user_repos = []
        page = 1
        while True:
            repo_params = {'page': page, 'per_page': 100}
            repo_query = requests.get(repo_query_url, auth=github_auth,
                                      params=repo_params)
            assert repo_query.status_code == 200
            if repo_query.json():
                user_repos.extend(list(r['name'] for r in repo_query.json()))
                page += 1
            else:
                break

        if expt_name not in user_repos:
            repo_config = {
                'name': expt_name,
                'description': expt_description,
                'private': expt_private,
                'has_issues': True,
                'has_downloads': True,
                'has_wiki': False
            }

            repo_gen = requests.post(repo_query_url, json.dumps(repo_config),
                                     auth=github_auth)

            assert repo_gen.status_code == 201

        # 3. Check if remote is set
        git_remote_out = subprocess.check_output(shlex.split('git remote -v'),
                                                 cwd=self.expt.control_path)

        git_remotes = dict([(r.split()[0], r.split()[1])
                            for r in git_remote_out.split('\n') if r])

        remote_name = self.config.get('remote', 'payu')
        remote_url = os.path.join('ssh://git@github.com', repo_target,
                                  self.expt.name + '.git')

        if remote_name not in git_remotes:
            cmd = ('git remote add {name} {url}'
                   ''.format(name=remote_name, url=remote_url))
            subprocess.check_call(shlex.split(cmd), cwd=self.expt.control_path)
        elif git_remotes[remote_name] != remote_url:
            print('payu: error: Existing remote URL does not match '
                  'the proposed URL.')
            print('payu: error: To delete the old remote, type '
                  '`git remote rm {name}`.'.format(name=remote_name))
            sys.exit(-1)

        # 4. Generate a payu-specific SSH key
        default_ssh_key = 'id_rsa_payu_' + expt_name
        ssh_key = self.config.get('sshid', default_ssh_key)
        ssh_dir = os.path.join(os.path.expanduser('~'), '.ssh', 'payu')
        mkdir_p(ssh_dir)

        ssh_keypath = os.path.join(ssh_dir, ssh_key)
        if not os.path.isfile(ssh_keypath):
            cmd = 'ssh-keygen -t rsa -f {key} -q -P ""'.format(key=ssh_key)
            subprocess.check_call(shlex.split(cmd), cwd=ssh_dir)

        # 5. Deploy key to repo
        with open(ssh_keypath + '.pub') as keyfile:
            pubkey = ' '.join(keyfile.read().split()[:-1])

        # TODO: Get this from github?
        repo_keys_url = os.path.join(repo_api_url, 'keys')
        keys_req = requests.get(repo_keys_url, auth=github_auth)
        assert keys_req.status_code == 200

        if not any(k['key'] == pubkey for k in keys_req.json()):
            add_key_param = {'title': 'payu', 'key': pubkey}
            add_key_req = requests.post(repo_keys_url, auth=github_auth,
                                        json=add_key_param)
            assert add_key_req.status_code == 201

Response: Setup the github API and the authentication keys and API tokens.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_expiration_postfix(expiration):
    if re.match(r'^[1-9][0-9]*$', expiration):
        return expiration + ".0a1"

    if re.match(r'^[1-9][0-9]*\.0$', expiration):
        return expiration + "a1"

    return expiration

Response: Formats the expiration version and adds a version postfix if needed.


Instruction:
Can you implement a function in Python 3 that
loads a YAML file from disk throw a ParserError on failure.

Response:
def load_yaml_file(filename):
    """ Load a YAML file from disk, throw a ParserError on failure."""
    try:
        with open(filename, 'r') as f:
            return yaml.safe_load(f)
    except IOError as e:
        raise ParserError('Error opening ' + filename + ': ' + e.message)
    except ValueError as e:
        raise ParserError('Error parsing processes in {}: {}'
                          .format(filename, e.message))


Instruction:
Explain what the following Python 3 code does
def stringIndex(self, string):
        if string in self.table:
            return self.table[string]
        else:
            result = self.current_index
            self.table[string] = result
            self.current_index += self.c_strlen(string)
            return result

Response: Returns the index in the table of the provided string. Adds the string to the table if it s not there.


Instruction:
Here you have a function in Python 3, explain what it does
def writeDefinition(self, f, name):
        entries = self.table.items()
        entries.sort(key=lambda x: x[1])

        # Avoid null-in-string warnings with GCC and potentially
        # overlong string constants; write everything out the long way.
        def explodeToCharArray(string):
            def toCChar(s):
                if s == "'":
                    return "'\\''"
                else:
                    return "'%s'" % s
            return ", ".join(map(toCChar, string))

        f.write("const char %s[] = {\n" % name)
        for (string, offset) in entries:
            if "*/" in string:
                raise ValueError("String in string table contains unexpected sequence '*/': %s" %
                                 string)

            e = explodeToCharArray(string)
            if e:
                f.write("  /* %5d - \"%s\" */ %s, '\\0',\n"
                        % (offset, string, explodeToCharArray(string)))
            else:
                f.write("  /* %5d - \"%s\" */ '\\0',\n" % (offset, string))
        f.write("};\n\n")

Response: Writes out the string table to a file as a C const char array.


Instruction:
Create a Python 3 function for
rendering comments for obj.

Response:
def comments(context, obj):
    """ Render comments for obj. """
    content_type = ContentType.objects.get_for_model(obj.__class__)
    comment_list = LogEntry.objects.filter(
        content_type=content_type,
        object_id=obj.pk,
        action_flag=COMMENT
    )
    return {
        'obj': obj,
        'comment_list': comment_list,
        'is_admin': context['is_admin'],
    }


Instruction:
Make a summary of the following Python 3 code
def get_disk_quota(username, machine_name=None):

    try:
        ua = Account.objects.get(
            username=username,
            date_deleted__isnull=True)
    except Account.DoesNotExist:
        return 'Account not found'

    result = ua.get_disk_quota()
    if result is None:
        return False

    return result * 1048576

Response: Returns disk quota for username in KB


Instruction:
Can you write a function in Python 3 where it
generates a bar graph for a project

Response:
def _gen_project_trend_graph(project, start, end, force_overwrite=False):
    """Generates a bar graph for a project

    Keyword arguments:
    project -- Project
    start -- start date
    end -- end date

    """
    filename = graphs.get_project_trend_graph_filename(project, start, end)
    csv_filename = os.path.join(GRAPH_ROOT, filename + '.csv')
    png_filename = os.path.join(GRAPH_ROOT, filename + '.png')

    _check_directory_exists(csv_filename)
    _check_directory_exists(png_filename)

    if not settings.GRAPH_DEBUG or force_overwrite:
        if os.path.exists(csv_filename):
            if os.path.exists(png_filename):
                return

    query = CPUJob.objects.filter(
        project=project,
        date__range=(start, end)
    )
    query = query.values('account', 'account__username', 'date')
    query = query.annotate(Sum('cpu_usage')).order_by('account', 'date')

    t_start = start
    t_end = end

    start_str = start.strftime('%Y-%m-%d')
    end_str = end.strftime('%Y-%m-%d')

    fig, ax = plt.subplots(figsize=(6, 4))
    ax.set_xlim(start, end + datetime.timedelta(days=1))
    ax.set_title('%s   %s - %s' % (project.pid, start_str, end_str))
    ax.set_ylabel("CPU Time (hours)")
    ax.set_xlabel("Date")

    locator = mdates.AutoDateLocator()
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
    ax.xaxis.set_minor_locator(mdates.DayLocator())

    data = {}
    x_data = {}
    y_data = {}

    with open(csv_filename, 'w') as csv_file:
        csv_writer = csv.writer(csv_file)
        for row in query.iterator():
            csv_writer.writerow([
                row['account__username'],
                row['date'], row['cpu_usage__sum'] / 3600.00
            ])

            account = row['account']
            date = row['date']

            if account not in data:
                data[account] = {}
                x_data[account] = []
                y_data[account] = []

            data[account][date] = row['cpu_usage__sum']

    for account, dates in six.iteritems(data):
        start = t_start
        end = t_end
        while start <= end:
            total = 0
            if start in dates:
                total = dates[start]
            x_data[account].append(start)
            y_data[account].append(total / 3600.00)
            start = start + datetime.timedelta(days=1)

    del data

    totals = []
    start = t_start
    end = t_end
    while start <= end:
        totals.append(0)
        start = start + datetime.timedelta(days=1)

    count = 0
    for account in x_data.keys():
        ax.bar(
            x_data[account], y_data[account],
            bottom=totals,
            color=graphs.get_colour(count),
            edgecolor=graphs.get_colour(count),
            align='edge')
        count = count + 1

        i = 0
        start = t_start
        end = t_end
        while start <= end:
            totals[i] += y_data[account][i]
            i = i + 1
            start = start + datetime.timedelta(days=1)

    del x_data
    del y_data
    del totals

    fig.autofmt_xdate()
    plt.tight_layout()
    plt.savefig(png_filename)
    plt.close()


Instruction:
Make a summary of the following Python 3 code
def _gen_institute_graph(start, end, force_overwrite=False):
    filename = graphs.get_institute_graph_filename(start, end)
    csv_filename = os.path.join(GRAPH_ROOT, filename + '.csv')
    png_filename = os.path.join(GRAPH_ROOT, filename + '.png')

    _check_directory_exists(csv_filename)
    _check_directory_exists(png_filename)

    if not settings.GRAPH_DEBUG or force_overwrite:
        if os.path.exists(csv_filename):
            if os.path.exists(png_filename):
                return

    institute_list = Institute.active.all()

    plt.subplots(figsize=(4, 4))

    data = []
    labels = []

    total = 0
    with open(csv_filename, 'w') as csv_file:
        csv_writer = csv.writer(csv_file)
        for institute in institute_list.iterator():
            hours, jobs = usage.get_institute_usage(institute, start, end)
            total = total + int(hours)
            if hours > 0:
                csv_writer.writerow([institute.name, hours, jobs])
                data.append(hours)
                labels.append(institute.name)

        mcu = usage.get_machine_category_usage(start, end)
        hours = int(mcu.available_time - total)
        csv_writer.writerow(["unused", hours])
        data.append(hours)
        labels.append('Unused')

    plt.pie(data, labels=labels, autopct='%1.1f%%', shadow=True)
    plt.tight_layout()
    plt.savefig(png_filename)
    plt.close()

Response: Generate a pie chart comparing institutes usage.


Instruction:
Here you have a function in Python 3, explain what it does
def _gen_machine_graph(start, end, force_overwrite=False):
    filename = graphs.get_machine_graph_filename(start, end)
    csv_filename = os.path.join(GRAPH_ROOT, filename + '.csv')
    png_filename = os.path.join(GRAPH_ROOT, filename + '.png')

    _check_directory_exists(csv_filename)
    _check_directory_exists(png_filename)

    if not settings.GRAPH_DEBUG or force_overwrite:
        if os.path.exists(csv_filename):
            if os.path.exists(png_filename):
                return

    machine_list = Machine.objects.all()

    plt.subplots(figsize=(4, 4))

    data = []
    labels = []

    with open(csv_filename, 'w') as csv_file:
        csv_writer = csv.writer(csv_file)
        for machine in machine_list.iterator():
            hours, jobs = usage.get_machine_usage(machine, start, end)
            if hours > 0:
                csv_writer.writerow([machine.name, hours, jobs])
                data.append(hours)
                labels.append(machine.name)

    plt.pie(data, labels=labels, autopct='%1.1f%%', shadow=True)

    del data
    del labels

    plt.tight_layout()
    plt.savefig(png_filename)
    plt.close()

Response: Generate a graph of the machines usage.


Instruction:
Here you have a function in Python 3, explain what it does
def _gen_trend_graph(start, end, force_overwrite=False):
    filename = graphs.get_trend_graph_filename(start, end)
    csv_filename = os.path.join(GRAPH_ROOT, filename + '.csv')
    png_filename = os.path.join(GRAPH_ROOT, filename + '.png')

    _check_directory_exists(csv_filename)
    _check_directory_exists(png_filename)

    if not settings.GRAPH_DEBUG or force_overwrite:
        if os.path.exists(csv_filename):
            if os.path.exists(png_filename):
                return

    query = CPUJob.objects.filter(
        date__range=(start, end)
    )
    query = query.values('date').annotate(Sum('cpu_usage'))
    query = query.order_by('date')

    t_start = start
    t_end = end

    start_str = start.strftime('%Y-%m-%d')
    end_str = end.strftime('%Y-%m-%d')

    fig, ax = plt.subplots(figsize=(6, 4))
    ax.set_xlim(start, end)
    ax.set_title('%s - %s' % (start_str, end_str))
    ax.set_ylabel("CPU Time (hours)")
    ax.set_xlabel("Date")

    locator = mdates.AutoDateLocator()
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(mdates.AutoDateFormatter(locator))
    ax.xaxis.set_minor_locator(mdates.DayLocator())

    data = {}
    x_data = []
    y_data = []

    with open(csv_filename, 'w') as csv_file:
        csv_writer = csv.writer(csv_file)
        for row in query.iterator():
            csv_writer.writerow([
                row['date'], row['cpu_usage__sum'] / 3600.00
            ])

            date = row['date']

            data[date] = row['cpu_usage__sum']

    start = t_start
    end = t_end
    while start <= end:
        total = 0
        if start in data:
            total = data[start]
        x_data.append(start)
        y_data.append(total / 3600.00)
        start = start + datetime.timedelta(days=1)

    del data

    ax.plot(x_data, y_data)

    del x_data
    del y_data

    fig.autofmt_xdate()
    plt.tight_layout()
    plt.savefig(png_filename)
    plt.close()

Response: Generate a total trend graph for a given start and end date.


Instruction:
Can you tell what is the following Python 3 function doing
def daynum_to_date(daynum, max_days=1000000):
    if daynum is None:
        return None
    try:
        daycount = int(daynum)
    except ValueError:
        return None

    if daycount > max_days:
        # Using default: some time in the 48th century, clearly bogus.
        daycount = max_days
    return date(1970, 1, 1) + timedelta(daycount)

Response: Convert a number of days to a date.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def mau(dataframe, target_day, past_days=28, future_days=10, date_format="%Y%m%d"):
    target_day_date = datetime.strptime(target_day, date_format)

    # Compute activity over `past_days` days leading up to target_day
    min_activity_date = target_day_date - timedelta(past_days)
    min_activity = unix_time_nanos(min_activity_date)
    max_activity = unix_time_nanos(target_day_date + timedelta(1))
    act_col = dataframe.activityTimestamp

    min_submission = datetime.strftime(min_activity_date, date_format)
    max_submission_date = target_day_date + timedelta(future_days)
    max_submission = datetime.strftime(max_submission_date, date_format)
    sub_col = dataframe.submission_date_s3

    filtered = filter_date_range(dataframe, act_col, min_activity, max_activity,
                                 sub_col, min_submission, max_submission)
    return count_distinct_clientids(filtered)

Response: Compute Monthly Active Users from the Executive Summary dataset.


Instruction:
Can you write a function in Python 3 where it
snaps a date to the beginning of the week.

Response:
def snap_to_beginning_of_week(day, weekday_start="Sunday"):
    """ Get the first day of the current week.

    :param day: The input date to snap.
    :param weekday_start: Either "Monday" or "Sunday", indicating the first day of the week.
    :returns: A date representing the first day of the current week.
    """
    delta_days = ((day.weekday() + 1) % 7) if weekday_start is "Sunday" else day.weekday()
    return day - timedelta(days=delta_days)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_last_week_range(weekday_start="Sunday"):
    today = date.today()
    # Get the first day of the past complete week.
    start_of_week = snap_to_beginning_of_week(today, weekday_start) - timedelta(weeks=1)
    end_of_week = start_of_week + timedelta(days=6)
    return (start_of_week, end_of_week)

Response: Gets the date for the first and last day of the previous complete week.


Instruction:
Can you tell what is the following Python 3 function doing
def get_last_month_range():
    today = date.today()
    # Get the last day for the previous month.
    end_of_last_month = snap_to_beginning_of_month(today) - timedelta(days=1)
    start_of_last_month = snap_to_beginning_of_month(end_of_last_month)
    return (start_of_last_month, end_of_last_month)

Response: Gets the date for the first and last day of the previous complete month.


Instruction:
Create a Python 3 function to
return a dataframe with a sample of clients given a given sample_id.

Response:
def sampler(dataframe, modulo, column="client_id", sample_id=42):
    """ Collect a sample of clients given an input column

    Filter dataframe based on the modulus of the CRC32 of a given string
    column matching a given sample_id. if dataframe has already been filtered
    by sample_id, then modulo should be a multiple of 100, column should be
    "client_id", and the given sample_id should match the value previously
    used, optionally plus multiples of 100.

    Args:
        dataframe: A Dataframe to be sampled
        modulo (int): selects a 1/modulo sampling of dataframe
        column (str): name of a string column to sample on
        sample_id (int): modulus result to select for sampling

    Returns:
        A DataFrame sampled on the given inputs.
    """
    return dataframe \
        .withColumn(
            "sampler",
            udf(lambda key: (crc32(key or "") & 0xffffffff) % modulo)(column),
        ).where("sampler = %s" % sample_id).drop("sampler")


Instruction:
Can you create a Python 3 function that
checks status of task.

Response:
def progress(request):
    """ Check status of task. """
    if 'delete' in request.GET:
        models.MachineCache.objects.all().delete()
        models.InstituteCache.objects.all().delete()
        models.PersonCache.objects.all().delete()
        models.ProjectCache.objects.all().delete()
        return render(
            template_name='main.html',
            context={'content': 'Deleted'},
            request=request)

    if request.method == 'POST':
        if 'task_id' in request.POST:
            result = Task.AsyncResult(request.POST['task_id'])
            if result.failed():
                value = {
                    'info': {},
                    'ready': result.ready(),
                }
            else:
                value = {
                    'info': result.info,
                    'ready': result.ready(),
                }
            return HttpResponse(
                json.dumps(value), content_type="application/json")
    return None


Instruction:
Here you have a function in Python 3, explain what it does
def synchronise(func):
    def inner(request, *args):
        lock_id = '%s-%s-built-%s' % (
            datetime.date.today(), func.__name__,
            ",".join([str(a) for a in args]))

        if cache.add(lock_id, 'true', LOCK_EXPIRE):
            result = func(request, *args)
            cache.set(lock_id, result.task_id)
        else:
            task_id = cache.get(lock_id)
            if not task_id:
                return None

            cache.set(lock_id, "")
            result = Task.AsyncResult(task_id)
            if result.ready():
                result.forget()
                return None
        return result
    return inner

Response: Decorator that synchronises a task into the cache.


Instruction:
Can you create a Python 3 function that
partitions a list of objects in even buckets

Response:
def _group_by_size_greedy(obj_list, tot_groups):
    """Partition a list of objects in even buckets

    The idea is to choose the bucket for an object in a round-robin fashion.
    The list of objects is sorted to also try to keep the total size in bytes
    as balanced as possible.
    :param obj_list: a list of dict-like objects with a 'size' property
    :param tot_groups: number of partitions to split the data into.
    :return: a list of lists, one for each partition.
    """
    sorted_list = sorted(obj_list, key=lambda x: x['size'], reverse=True)
    groups = [[] for _ in range(tot_groups)]
    for index, obj in enumerate(sorted_list):
        current_group = groups[index % len(groups)]
        current_group.append(obj)
    return groups


Instruction:
Write a Python 3 function for
partitioning a list of objects evenly and by file size.

Response:
def _group_by_equal_size(obj_list, tot_groups, threshold=pow(2, 32)):
    """Partition a list of objects evenly and by file size

    Files are placed according to largest file in the smallest bucket. If the
    file is larger than the given threshold, then it is placed in a new bucket
    by itself.
    :param obj_list: a list of dict-like objects with a 'size' property
    :param tot_groups: number of partitions to split the data
    :param threshold: the maximum size of each bucket
    :return: a list of lists, one for each partition
    """
    sorted_obj_list = sorted([(obj['size'], obj) for obj in obj_list], reverse=True)
    groups = [(random.random(), []) for _ in range(tot_groups)]

    if tot_groups <= 1:
        groups = _group_by_size_greedy(obj_list, tot_groups)
        return groups
    heapq.heapify(groups)
    for obj in sorted_obj_list:
        if obj[0] > threshold:
            heapq.heappush(groups, (obj[0], [obj[1]]))
        else:
            size, files = heapq.heappop(groups)
            size += obj[0]
            files.append(obj[1])
            heapq.heappush(groups, (size, files))
    groups = [group[1] for group in groups]
    return groups


Instruction:
How would you explain what the following Python 3 function does
def select(self, *properties, **aliased_properties):
        if not (properties or aliased_properties):
            return self
        merged_properties = dict(zip(properties, properties))
        merged_properties.update(aliased_properties)

        for prop_name in (merged_properties.keys()):
            if prop_name in self.selection:
                raise Exception('The property {} has already been selected'.format(prop_name))

        new_selection = self.selection.copy()
        new_selection.update(merged_properties)

        return self._copy(selection=new_selection)

Response: Select the properties of the dataset.


Instruction:
Write a Python 3 script for
returning a new Dataset refined using the given condition.

Response:
def where(self, **kwargs):
        """Return a new Dataset refined using the given condition

        :param kwargs: a map of `dimension` => `condition` to filter the elements
            of the dataset. `condition` can either be an exact value or a
            callable returning a boolean value. If `condition` is a value, it is
            converted to a string, then sanitized. If `condition` is a callable, note that it will
            be passed sanitized values -- i.e., characters outside [a-zA-Z0-9_.] are converted
            to `_`.
        """
        clauses = copy(self.clauses)
        for dimension, condition in kwargs.items():
            if dimension in self.clauses:
                raise Exception('There should be only one clause for {}'.format(dimension))
            if dimension not in self.schema:
                raise Exception('The dimension {} doesn\'t exist'.format(dimension))
            if isfunction(condition) or isinstance(condition, functools.partial):
                clauses[dimension] = condition
            else:
                clauses[dimension] = functools.partial((lambda x, y: x == y), self._sanitize_dimension(str(condition)))
        return self._copy(clauses=clauses)


Instruction:
How would you implement a function in Python 3 that
retrieves the summaries of the files contained in the current dataset.

Response:
def summaries(self, sc, limit=None):
        """Summary of the files contained in the current dataset

        Every item in the summary is a dict containing a key name and the corresponding size of
        the key item in bytes, e.g.::
        {'key': 'full/path/to/my/key', 'size': 200}

        :param limit: Max number of objects to retrieve
        :return: An iterable of summaries
        """
        clauses = copy(self.clauses)
        schema = self.schema

        if self.prefix:
            schema = ['prefix'] + schema
            # Add a clause for the prefix that always returns True, in case
            # the output is not filtered at all (so that we do a scan/filter
            # on the prefix directory)
            clauses['prefix'] = lambda x: True

        with futures.ThreadPoolExecutor(self.max_concurrency) as executor:
            scanned = self._scan(schema, [self.prefix], clauses, executor)
        keys = sc.parallelize(scanned).flatMap(self.store.list_keys)
        return keys.take(limit) if limit else keys.collect()


Instruction:
How would you explain what the following Python 3 function does
def records(self, sc, group_by='greedy', limit=None, sample=1, seed=42, decode=None, summaries=None):
        decode = decode or message_parser.parse_heka_message
        summaries = summaries or self.summaries(sc, limit)

        # Calculate the sample if summaries is not empty and limit is not set
        if summaries and limit is None and sample != 1:
            if sample < 0 or sample > 1:
                raise ValueError('sample must be between 0 and 1')
            print(
                "WARNING: THIS IS NOT A REPRESENTATIVE SAMPLE.\n"
                "This 'sampling' is based on s3 files and is highly\n"
                "susceptible to skew. Use only for quicker performance\n"
                "while prototyping."
            )
            # We want this sample to be reproducible.
            # See https://bugzilla.mozilla.org/show_bug.cgi?id=1318681
            seed_state = random.getstate()
            try:
                random.seed(seed)
                summaries = random.sample(summaries,
                                          int(len(summaries) * sample))
            finally:
                random.setstate(seed_state)

        # Obtain size in MB
        total_size = functools.reduce(lambda acc, item: acc + item['size'], summaries, 0)
        total_size_mb = total_size / float(1 << 20)
        print("fetching %.5fMB in %s files..." % (total_size_mb, len(summaries)))

        if group_by == 'equal_size':
            groups = _group_by_equal_size(summaries, 10*sc.defaultParallelism)
        elif group_by == 'greedy':
            groups = _group_by_size_greedy(summaries, 10*sc.defaultParallelism)
        else:
            raise Exception("group_by specification is invalid")

        self._compile_selection()

        keys = (
            sc.parallelize(groups, len(groups))
            .flatMap(lambda x: x)
            .map(lambda x: x['key'])
        )
        file_handles = keys.map(self.store.get_key)

        # decode(fp: file-object) -> list[dict]
        data = file_handles.flatMap(decode)

        return data.map(self._apply_selection)

Response: Retrieve the elements of a Dataset


Instruction:
Can you tell what is the following Python 3 function doing
def dataframe(self, spark, group_by='greedy', limit=None, sample=1, seed=42, decode=None, summaries=None, schema=None, table_name=None):
        rdd = self.records(spark.sparkContext, group_by, limit, sample, seed, decode, summaries)
        if not schema:
            df = rdd.map(lambda d: Row(**d)).toDF()
        else:
            df = spark.createDataFrame(rdd, schema=schema)
        if table_name:
            df.createOrReplaceTempView(table_name)
        return df

Response: Convert an RDD returned from records function to a Spark DataFrame


Instruction:
Write a Python 3 function for
creating a new Dataset configured for the given source_name

Response:
def from_source(source_name):
        """Create a Dataset configured for the given source_name

        This is particularly convenient when the user doesn't know
        the list of dimensions or the bucket name, but only the source name.

        Usage example::

            records = Dataset.from_source('telemetry').where(
                docType='main',
                submissionDate='20160701',
                appUpdateChannel='nightly'
            )
        """
        meta_bucket = 'net-mozaws-prod-us-west-2-pipeline-metadata'
        store = S3Store(meta_bucket)

        try:
            source = json.loads(store.get_key('sources.json').read().decode('utf-8'))[source_name]
        except KeyError:
            raise Exception('Unknown source {}'.format(source_name))

        schema = store.get_key('{}/schema.json'.format(source['metadata_prefix'])).read().decode('utf-8')
        dimensions = [f['field_name'] for f in json.loads(schema)['dimensions']]
        return Dataset(source['bucket'], dimensions, prefix=source['prefix'])


Instruction:
Can you generate the documentation for the following Python 3 function
def get_next_action(self, request, application, roles):
        application.reopen()
        link, is_secret = base.get_email_link(application)
        emails.send_invite_email(application, link, is_secret)
        messages.success(
            request,
            "Sent an invitation to %s."
            % application.applicant.email)
        return 'success'

Response: Send an invitation email to the application.


Instruction:
Implement a Python 3 function for
retrieving the next state of the application.

Response:
def get_next_action(self, request, application, roles):
        """ Retrieve the next state. """

        # Check for serious errors in submission.
        # Should only happen in rare circumstances.
        errors = application.check_valid()
        if len(errors) > 0:
            for error in errors:
                messages.error(request, error)
            return 'error'

        # mark as submitted
        application.submit()

        return 'success'


Instruction:
How would you explain what the following Python 3 function does
def get_next_action(self, request, application, roles):
        # Check for serious errors in submission.
        # Should only happen in rare circumstances.
        errors = application.check_valid()
        if len(errors) > 0:
            for error in errors:
                messages.error(request, error)
            return 'error'

        # approve application
        approved_by = request.user
        created_person, created_account = application.approve(approved_by)

        # send email
        application.extend()
        link, is_secret = base.get_email_link(application)
        emails.send_approved_email(
            application, created_person, created_account, link, is_secret)

        if created_person or created_account:
            return 'password_needed'
        else:
            return 'password_ok'

Response: Retrieve the next action.


Instruction:
Can you write a function in Python 3 where it
sends an email to each project leader for person

Response:
def send_bounced_warning(person, leader_list):
    """Sends an email to each project leader for person
    informing them that person's email has bounced"""
    context = CONTEXT.copy()
    context['person'] = person

    for lp in leader_list:
        leader = lp['leader']

        context['project'] = lp['project']
        context['receiver'] = leader

        to_email = leader.email
        subject = render_to_string(
            'karaage/people/emails/bounced_email_subject.txt', context)
        body = render_to_string(
            'karaage/people/emails/bounced_email_body.txt', context)
        send_mail(
            subject.replace('\n', ''), body,
            settings.ACCOUNTS_EMAIL, [to_email])
        log.change(
            leader,
            'Sent email about bounced emails from %s' % person)


Instruction:
How would you code a function in Python 3 to
send an email to user allowing them to set their password.

Response:
def send_reset_password_email(person):
    """Sends an email to user allowing them to set their password."""
    uid = urlsafe_base64_encode(force_bytes(person.pk)).decode("ascii")
    token = default_token_generator.make_token(person)
    url = '%s/persons/reset/%s/%s/' % (
        settings.REGISTRATION_BASE_URL, uid, token)

    context = CONTEXT.copy()
    context.update({
        'url': url,
        'receiver': person,
    })

    to_email = person.email
    subject, body = render_email('reset_password', context)

    send_mail(subject, body, settings.ACCOUNTS_EMAIL, [to_email])


Instruction:
Can you tell what is the following Python 3 function doing
def send_confirm_password_email(person):
    url = '%s/profile/login/%s/' % (
        settings.REGISTRATION_BASE_URL, person.username)

    context = CONTEXT.copy()
    context.update({
        'url': url,
        'receiver': person,
    })

    to_email = person.email
    subject, body = render_email('confirm_password', context)

    send_mail(subject, body, settings.ACCOUNTS_EMAIL, [to_email])

Response: Sends an email to user allowing them to confirm their password.


Instruction:
Can you generate the documentation for the following Python 3 function
def check_can_approve(self, request, application, roles):
        try:
            authorised_persons = self.get_authorised_persons(application)
            authorised_persons.get(pk=request.user.pk)
            return True
        except Person.DoesNotExist:
            return False

Response: Check the person s authorization.


Instruction:
Given the following Python 3 function, write the documentation
def get_next_action(self, request, application, label, roles):
        actions = self.get_actions(request, application, roles)
        if label is None and 'is_applicant' in roles:
            assert application.content_type.model == 'person'
            if application.applicant.has_usable_password():
                form = forms.PersonVerifyPassword(
                    data=request.POST or None, person=application.applicant)
                form_type = "verify"
            else:
                form = forms.PersonSetPassword(
                    data=request.POST or None, person=application.applicant)
                form_type = "set"
            if request.method == 'POST':
                if 'cancel' in request.POST:
                    return 'cancel'
                if form.is_valid():
                    form.save()
                    messages.success(
                        request, 'Password updated. New accounts activated.')
                    for action in actions:
                        if action in request.POST:
                            return action
                    return HttpResponseBadRequest("
Bad Request
")
            return render(
                template_name='kgapplications/common_password.html',
                context={
                    'application': application,
                    'form': form,
                    'actions': actions,
                    'roles': roles,
                    'type': form_type
                },
                request=request)
        return super(StatePassword, self).get_next_action(request, application, label, roles)

Response: This method is used to get the next action for the user.


Instruction:
How would you explain what the following Python 3 function does
def get_next_action(self, request, application, label, roles):
        actions = self.get_actions(request, application, roles)
        if label is None and \
                'is_applicant' in roles and 'is_admin' not in roles:
            # applicant, admin, leader can reopen an application
            for action in actions:
                if action in request.POST:
                    return action
            return render(
                template_name='kgapplications/common_declined.html',
                context={
                    'application': application,
                    'actions': actions,
                    'roles': roles
                },
                request=request)
        return super(StateDeclined, self).get_next_action(
            request, application, label, roles)

Response: This method returns the next action that is needed to be used in the state declined page.


Instruction:
Given the following Python 3 function, write the documentation
def add_step(self, step, step_id):
        assert step_id not in self._steps
        assert step_id not in self._order
        assert isinstance(step, Step)

        self._steps[step_id] = step
        self._order.append(step_id)

Response: Add a step to the list.


Instruction:
Given the following Python 3 function, write the documentation
def get_next_action(self, request, application, label, roles):
        actions = self.get_actions(request, application, roles)

        # if the user is not the applicant, the steps don't apply.
        if 'is_applicant' not in roles:
            return super(StateWithSteps, self).get_next_action(
                request, application, label, roles)

        # was label supplied?
        if label is None:
            # no label given, find first step and redirect to it.
            this_id = self._order[0]
            url = base.get_url(request, application, roles, this_id)
            return HttpResponseRedirect(url)
        else:
            # label was given, get the step position and id for it
            this_id = label
            if this_id not in self._steps:
                return HttpResponseBadRequest("
Bad Request
")
            position = self._order.index(this_id)

        # get the step given the label
        this_step = self._steps[this_id]

        # define list of allowed actions for step
        step_actions = {}
        if 'cancel' in actions:
            step_actions['cancel'] = "state:cancel"
        if 'submit' in actions and position == len(self._order) - 1:
            step_actions['submit'] = "state:submit"
        if position > 0:
            step_actions['prev'] = self._order[position - 1]
        if position < len(self._order) - 1:
            step_actions['next'] = self._order[position + 1]

        # process the request
        if request.method == "GET":
            # if GET request, state changes are not allowed
            response = this_step.view(
                request, application, this_id, roles, step_actions.keys())
            assert isinstance(response, HttpResponse)
            return response
        elif request.method == "POST":
            # if POST request, state changes are allowed
            response = this_step.view(
                request, application, this_id, roles, step_actions.keys())
            assert response is not None

            # If it was a HttpResponse, just return it
            if isinstance(response, HttpResponse):
                return response
            else:
                # try to lookup the response
                if response not in step_actions:
                    raise RuntimeError(
                        "Invalid response '%s' from step '%s'"
                        % (response, this_step))
                action = step_actions[response]

                # process the required action
                if action.startswith("state:"):
                    return action[6:]
                else:
                    url = base.get_url(request, application, roles, action)
                    return HttpResponseRedirect(url)

        # Something went wrong.
        return HttpResponseBadRequest("
Bad Request
")

Response: Process the get_next_action request at the current step.


Instruction:
How would you explain what the following Python 3 function does
def get_next_action(self, request, application, label, roles):
        actions = self.get_actions(request, application, roles)
        if label is None and \
                'is_applicant' in roles and 'is_admin' not in roles:
            for action in actions:
                if action in request.POST:
                    return action
            link, is_secret = base.get_email_link(application)
            return render(
                template_name='kgapplications/software_introduction.html',
                context={
                    'actions': actions,
                    'application': application,
                    'roles': roles,
                    'link': link,
                    'is_secret': is_secret
                },
                request=request)
        return super(StateIntroduction, self).get_next_action(
            request, application, label, roles)

Response: This method returns the next action for the given application and label.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_account_username(account, old_username, new_username):
    for datastore in _get_datastores():
        datastore.set_account_username(account, old_username, new_username)

Response: Sets the username of an account.


Instruction:
How would you explain what the following Python 3 function does
def get_account_details(account):
    result = []
    for datastore in _get_datastores():
        value = datastore.get_account_details(account)
        value['datastore'] = datastore.config['DESCRIPTION']
        result.append(value)
    return result

Response: Get the account details.


Instruction:
Implement a Python 3 function for
changing the group name.

Response:
def set_group_name(group, old_name, new_name):
    """ Group was renamed. """
    for datastore in _get_datastores():
        datastore.set_group_name(group, old_name, new_name)


Instruction:
How would you implement a function in Python 3 that
gets details for this user.

Response:
def get_project_details(project):
    """ Get details for this user. """
    result = []
    for datastore in _get_datastores():
        value = datastore.get_project_details(project)
        value['datastore'] = datastore.config['DESCRIPTION']
        result.append(value)
    return result


Instruction:
Can you tell what is the following Python 3 function doing
def set_project_pid(project, old_pid, new_pid):
    for datastore in _get_datastores():
        datastore.save_project(project)
        datastore.set_project_pid(project, old_pid, new_pid)

Response: Set the project s PID to new_pid.


Instruction:
Can you tell what is the following Python 3 function doing
def get_institute_details(institute):
    result = []
    for datastore in _get_datastores():
        value = datastore.get_institute_details(institute)
        value['datastore'] = datastore.config['DESCRIPTION']
        result.append(value)
    return result

Response: Get details for this user.


Instruction:
Write a Python 3 script to
add accounts to group.

Response:
def add_accounts_to_group(accounts_query, group):
    """ Add accounts to group. """

    query = accounts_query.filter(date_deleted__isnull=True)

    for account in query:
        add_account_to_group(account, group)


Instruction:
Here you have a function in Python 3, explain what it does
def remove_accounts_from_group(accounts_query, group):

    query = accounts_query.filter(date_deleted__isnull=True)

    for account in query:
        remove_account_from_group(account, group)

Response: Remove accounts from group.


Instruction:
Write a Python 3 function that can
add accounts to project.

Response:
def add_accounts_to_project(accounts_query, project):
    """ Add accounts to project. """

    query = accounts_query.filter(date_deleted__isnull=True)

    for account in query:
        add_account_to_project(account, project)


Instruction:
How would you implement a function in Python 3 that
removes accounts from project.

Response:
def remove_accounts_from_project(accounts_query, project):
    """ Remove accounts from project. """

    query = accounts_query.filter(date_deleted__isnull=True)

    for account in query:
        remove_account_from_project(account, project)


Instruction:
Write a Python 3 script to
add accounts to institute.

Response:
def add_accounts_to_institute(accounts_query, institute):
    """ Add accounts to institute. """

    query = accounts_query.filter(date_deleted__isnull=True)

    for account in query:
        add_account_to_institute(account, institute)


Instruction:
Given the following Python 3 function, write the documentation
def remove_accounts_from_institute(accounts_query, institute):

    query = accounts_query.filter(date_deleted__isnull=True)

    for account in query:
        remove_account_from_institute(account, institute)

Response: Remove accounts from institute.


Instruction:
How would you explain what the following Python 3 function does
def view(self, request, application, label, roles, actions):
        # Get the appropriate form
        status = None
        form = None

        if application.content_type.model != 'applicant':
            status = "You are already registered in the system."
        elif application.content_type.model == 'applicant':
            if application.applicant.saml_id is not None:
                form = forms.SAMLApplicantForm(
                    request.POST or None,
                    instance=application.applicant)
            else:
                form = forms.UserApplicantForm(
                    request.POST or None,
                    instance=application.applicant)

        # Process the form, if there is one
        if form is not None and request.method == 'POST':
            if form.is_valid():
                form.save(commit=True)
                for action in actions:
                    if action in request.POST:
                        return action
                return HttpResponseBadRequest("
Bad Request
")
            else:
                # if form didn't validate and we want to go back or cancel,
                # then just do it.
                if 'cancel' in request.POST:
                    return "cancel"
                if 'prev' in request.POST:
                    return 'prev'

        # If we don't have a form, we can just process the actions here
        if form is None:
            for action in actions:
                if action in request.POST:
                    return action

        # Render the response
        return render(
            template_name='kgapplications/project_aed_applicant.html',
            context={
                'form': form,
                'application': application,
                'status': status, 'actions': actions, 'roles': roles,
            },
            request=request)

Response: This method is called by the Django app to view the applicant state.


Instruction:
Write a Python 3 function for
processing the get_next_action request at the current step.

Response:
def get_next_action(self, request, application, label, roles):
        """ Process the get_next_action request at the current step. """

        # if user is logged and and not applicant, steal the
        # application
        if 'is_applicant' in roles:
            # if we got this far, then we either we are logged in as applicant,
            # or we know the secret for this application.

            new_person = None
            reason = None
            details = None

            attrs, _ = saml.parse_attributes(request)
            saml_id = attrs['persistent_id']
            if saml_id is not None:
                query = Person.objects.filter(saml_id=saml_id)
                if application.content_type.model == "person":
                    query = query.exclude(pk=application.applicant.pk)
                if query.count() > 0:
                    new_person = Person.objects.get(saml_id=saml_id)
                    reason = "SAML id is already in use by existing person."
                    details = (
                        "It is not possible to continue this application "
                        + "as is because the saml identity already exists "
                        + "as a registered user.")
                del query

            if request.user.is_authenticated:
                new_person = request.user
                reason = "%s was logged in " \
                    "and accessed the secret URL." % new_person
                details = (
                    "If you want to access this application "
                    + "as %s " % application.applicant
                    + "without %s stealing it, " % new_person
                    + "you will have to ensure %s is " % new_person
                    + "logged out first.")

            if new_person is not None:
                if application.applicant != new_person:
                    if 'steal' in request.POST:
                        old_applicant = application.applicant
                        application.applicant = new_person
                        application.save()
                        log.change(
                            application.application_ptr,
                            "Stolen application from %s" % old_applicant)
                        messages.success(
                            request,
                            "Stolen application from %s" % old_applicant)
                        url = base.get_url(request, application, roles, label)
                        return HttpResponseRedirect(url)
                    else:
                        return render(
                            template_name='kgapplications'
                            '/project_aed_steal.html',
                            context={
                                'application': application,
                                'person': new_person,
                                'reason': reason,
                                'details': details,
                            },
                            request=request)

        # if the user is the leader, show him the leader specific page.
        if ('is_leader' in roles or 'is_delegate' in roles) \
                and 'is_admin' not in roles \
                and 'is_applicant' not in roles:
            actions = ['reopen']
            if 'reopen' in request.POST:
                return 'reopen'
            return render(
                template_name='kgapplications/project_aed_for_leader.html',
                context={'application': application,
                         'actions': actions, 'roles': roles, },
                request=request)

        # otherwise do the default behaviour for StateWithSteps
        return super(StateApplicantEnteringDetails, self) \
            .get_next_action(request, application, label, roles)


Instruction:
Make a summary of the following Python 3 code
def _filter_string(value):
        if value is None:
            value = ""

        # replace whitespace with space
        value = value.replace("\n", " ")
        value = value.replace("\t", " ")

        # CSV seperator
        value = value.replace("|", " ")

        # remove leading/trailing whitespace
        value = value.strip()

        # hack because MAM doesn't quote sql correctly
        value = value.replace("\\", "")

        # Used for stripping non-ascii characters
        value = ''.join(c for c in value if 31 < ord(c) < 127)

        return value

Response: Filter the string so MAM doesn t have heart failure.


Instruction:
Can you create a Python 3 function that
truncates a string after a given number of chars

Response:
def _truncate(value, arg):
        """
        Truncates a string after a given number of chars
        Argument: Number of chars to _truncate after
        """
        length = int(arg)
        if value is None:
            value = ""
        if len(value) > length:
            return value[:length] + "..."
        else:
            return value


Instruction:
Write a Python 3 script for
calling a command and return the result.

Response:
def _call(self, command, ignore_errors=None):
        """ Call remote command with logging. """
        if ignore_errors is None:
            ignore_errors = []

        command = self._get_command(command)
        logger.debug("Cmd %s" % command)
        null = open('/dev/null', 'w')
        retcode = subprocess.call(command, stdout=null, stderr=null)
        null.close()

        if retcode in ignore_errors:
            logger.debug(
                "<-- Cmd %s returned %d (ignored)" % (command, retcode))
            return

        if retcode:
            logger.error(
                "<-- Cmd %s returned: %d (error)" % (command, retcode))
            raise subprocess.CalledProcessError(retcode, command)

        logger.debug("<-- Returned %d (good)" % retcode)
        return


Instruction:
Implement a Python 3 function for
getting the user details from MAM.

Response:
def get_user(self, username):
        """ Get the user details from MAM. """
        cmd = ["glsuser", "-u", username, "--raw"]
        results = self._read_output(cmd)

        if len(results) == 0:
            return None
        elif len(results) > 1:
            logger.error(
                "Command returned multiple results for '%s'." % username)
            raise RuntimeError(
                "Command returned multiple results for '%s'." % username)

        the_result = results[0]
        the_name = the_result["Name"]
        if username.lower() != the_name.lower():
            logger.error(
                "We expected username '%s' but got username '%s'."
                % (username, the_name))
            raise RuntimeError(
                "We expected username '%s' but got username '%s'."
                % (username, the_name))

        return the_result


Instruction:
Can you tell what is the following Python 3 function doing
def get_user_balance(self, username):
        cmd = ["gbalance", "-u", username, "--raw"]
        results = self._read_output(cmd)

        if len(results) == 0:
            return None

        return results

Response: Get the user balance details from MAM.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_users_in_project(self, projectname):
        ds_project = self.get_project(projectname)
        if ds_project is None:
            logger.error(
                "Project '%s' does not exist in MAM" % projectname)
            raise RuntimeError(
                "Project '%s' does not exist in MAM" % projectname)

        user_list = []
        if ds_project["Users"] != "":
            user_list = ds_project["Users"].lower().split(",")
        return user_list

Response: Get list of users in project from MAM.


Instruction:
Here you have a function in Python 3, explain what it does
def get_projects_in_user(self, username):
        ds_balance = self.get_user_balance(username)
        if ds_balance is None:
            return []

        project_list = []
        for bal in ds_balance:
            project_list.append(bal["Name"])
        return project_list

Response: Get list of projects in user from MAM.


Instruction:
Write a Python 3 script to
call when account is created or updated with username override.

Response:
def _save_account(self, account, username):
        """ Called when account is created/updated. With username override. """

        # retrieve default project, or use null project if none
        default_project_name = self._null_project
        if account.default_project is not None:
            default_project_name = account.default_project.pid

        # account created
        # account updated

        ds_user = self.get_user(username)
        if account.date_deleted is None:
            # date_deleted is not set, user should exist
            logger.debug("account is active")

            if ds_user is None:
                # create user if doesn't exist
                self._create_user(username, default_project_name)
            else:
                # or just set default project
                self._set_user_default_project(username, default_project_name)

            # update user meta information
            self._call([
                "gchuser",
                "-n", self._filter_string(account.person.get_full_name()),
                "-u", username])
            self._call([
                "gchuser",
                "-E", self._filter_string(account.person.email),
                "-u", username])

            # add rest of projects user belongs to
            mam_projects = self.get_projects_in_user(username)
            for project in account.person.projects.all():
                if project.pid not in mam_projects:
                    self.add_account_to_project(account, project)
        else:
            # date_deleted is not set, user should not exist
            logger.debug("account is not active")
            if ds_user is not None:
                # delete MAM user if account marked as deleted
                self._call(["grmuser", "-u", username], ignore_errors=[8])

        return


Instruction:
Given the following Python 3 function, write the documentation
def _delete_account(self, account, username):

        # account deleted

        ds_user = self.get_user(username)
        if ds_user is not None:
            self._call(["grmuser", "-u", username], ignore_errors=[8])

        return

Response: Called when account is deleted. With username override.


Instruction:
How would you code a function in Python 3 to
set the username of an account.

Response:
def set_account_username(self, account, old_username, new_username):
        """ Account's username was changed. """
        self._delete_account(account, old_username)
        self._save_account(account, new_username)


Instruction:
Create a Python 3 function for
getting the account details

Response:
def get_account_details(self, account):
        """ Get the account details """
        result = self.get_user(account.username)
        if result is None:
            result = {}
        return result


Instruction:
How would you explain what the following Python 3 function does
def save_project(self, project):
        pid = project.pid

        # project created
        # project updated

        if project.is_active:
            # project is not deleted
            logger.debug("project is active")
            ds_project = self.get_project(pid)
            if ds_project is None:
                self._create_project(pid)

            # update project meta information
            name = self._truncate(project.name, 40)
            self._set_project(pid, name, project.institute)
        else:
            # project is deleted
            logger.debug("project is not active")
            ds_project = self.get_project(pid)
            if ds_project is not None:
                self._delete_project(pid)

        return

Response: Called when project is saved or updated.


Instruction:
Given the following Python 3 function, write the documentation
def delete_project(self, project):
        pid = project.pid

        # project deleted

        ds_project = self.get_project(pid)
        if ds_project is not None:
            self._delete_project(pid)

        return

Response: Called when a project is deleted.


Instruction:
How would you implement a function in Python 3 that
gets the project details.

Response:
def get_project_details(self, project):
        """ Get the project details. """
        result = self.get_project(project.pid)
        if result is None:
            result = {}
        return result


Instruction:
Can you generate the documentation for the following Python 3 function
def save_institute(self, institute):
        name = institute.name
        logger.debug("save_institute '%s'" % name)

        # institute created
        # institute updated

        if institute.is_active:
            # date_deleted is not set, user should exist
            logger.debug("institute is active")

            self._call(
                ["goldsh", "Organization", "Create", "Name=%s" % name],
                ignore_errors=[185])
        else:
            # date_deleted is not set, user should not exist
            logger.debug("institute is not active")
            # delete MAM organisation if institute marked as deleted
            self._call(["goldsh", "Organization", "Delete", "Name==%s" % name])

        logger.debug("returning")
        return

Response: Called when institute is created or updated.


Instruction:
Implement a Python 3 function for
calling when institute is deleted.

Response:
def delete_institute(self, institute):
        """ Called when institute is deleted. """
        name = institute.name
        logger.debug("institute_deleted '%s'" % name)

        # institute deleted
        self._call(["goldsh", "Organization", "Delete", "Name==%s" % name])

        logger.debug("returning")
        return


Instruction:
How would you explain what the following Python 3 function does
def add_account_to_project(self, account, project):
        username = account.username
        projectname = project.pid
        self._call([
            "gchproject",
            "--add-user", username,
            "-p", projectname],
            ignore_errors=[74])

Response: Add account to project.


Instruction:
Can you implement a function in Python 3 that
renders a Matplotlib figure in Zeppelin.

Response:
def show(fig, width=600):
    """ Renders a Matplotlib figure in Zeppelin.

    :param fig: a Matplotlib figure
    :param width: the width in pixel of the rendered figure, defaults to 600

    Usage example::

        import matplotlib.pyplot as plt
        from moztelemetry.zeppelin import show

        fig = plt.figure()
        plt.plot([1, 2, 3])
        show(fig)
    """
    img = StringIO()
    fig.savefig(img, format='svg')
    img.seek(0)
    print("%html
{}
".format(width, img.buf))


Instruction:
Can you write a function in Python 3 where it
returns a fully - connected set of hub components using default filenames.

Response:
def default_hub(hub_name, genome, email, short_label=None, long_label=None):
    """
    Returns a fully-connected set of hub components using default filenames.

    Parameters
    ----------

    hub_name : str
        Name of the hub

    genome : str
        Assembly name (hg38, dm6, etc)

    email : str
        Email to include with hub.

    short_label : str
        Short label for the hub. If None, defaults to the value of `hub_name`

    long_label : str
        Long label for the hub. If None, defaults to the value of `short_label`.
    """
    if short_label is None:
        short_label = hub_name
    if long_label is None:
        long_label = short_label

    hub = Hub(
        hub=hub_name,
        short_label=short_label,
        long_label=long_label,
        email=email)

    genome = Genome(genome)
    genomes_file = GenomesFile()
    trackdb = TrackDb()
    hub.add_genomes_file(genomes_file)
    genomes_file.add_genome(genome)
    genome.add_trackdb(trackdb)
    return hub, genomes_file, genome, trackdb


Instruction:
How would you explain what the following Python 3 function does
def parse_usage(machine, usage, date, machine_name, log_type):
    assert machine.name == machine_name

    year, month, day = date.split('-')
    date = datetime.date(int(year), int(month), int(day))

    return parse_logs(usage, date, machine_name, log_type)

Response: Parses a usage object and returns a list of log objects.


Instruction:
Can you tell what is the following Python 3 function doing
def from_files(filenames, strict_type_checks=True):
    if strict_type_checks:
        load_whitelist()

    all_histograms = OrderedDict()
    for filename in filenames:
        parser = FILENAME_PARSERS[os.path.basename(filename)]
        histograms = parser(filename, strict_type_checks)

        # OrderedDicts are important, because then the iteration order over
        # the parsed histograms is stable, which makes the insertion into
        # all_histograms stable, which makes ordering in generated files
        # stable, which makes builds more deterministic.
        if not isinstance(histograms, OrderedDict):
            raise ParserError("Histogram parser did not provide an OrderedDict.")

        for name, definition in iteritems(histograms):
            if name in all_histograms:
                raise ParserError('Duplicate histogram name "%s".' % name)
            all_histograms[name] = definition

    # We require that all USE_COUNTER2_* histograms be defined in a contiguous
    # block.
    use_counter_indices = filter(lambda x: x[1].startswith("USE_COUNTER2_"),
                                 enumerate(iterkeys(all_histograms)))
    if use_counter_indices:
        lower_bound = use_counter_indices[0][0]
        upper_bound = use_counter_indices[-1][0]
        n_counters = upper_bound - lower_bound + 1
        if n_counters != len(use_counter_indices):
            raise ParserError("Use counter histograms must be defined in a contiguous block.")

    # Check that histograms that were removed from Histograms.json etc.
    # are also removed from the whitelists.
    if whitelists is not None:
        all_whitelist_entries = itertools.chain.from_iterable(whitelists.itervalues())
        orphaned = set(all_whitelist_entries) - set(iterkeys(all_histograms))
        if len(orphaned) > 0:
            msg = 'The following entries are orphaned and should be removed from ' \
                  'histogram-whitelists.json:\n%s'
            raise ParserError(msg % (', '.join(sorted(orphaned))))

    for name, definition in iteritems(all_histograms):
        yield Histogram(name, definition, strict_type_checks=strict_type_checks)

Response: Return an iterator that provides a sequence of Histograms for each file in filenames.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ranges(self):
        bucket_fns = {
            'boolean': linear_buckets,
            'flag': linear_buckets,
            'count': linear_buckets,
            'enumerated': linear_buckets,
            'categorical': linear_buckets,
            'linear': linear_buckets,
            'exponential': exponential_buckets,
        }

        if self._kind not in bucket_fns:
            raise ParserError('Unknown kind "%s" for histogram "%s".' % (self._kind, self._name))

        fn = bucket_fns[self._kind]
        return fn(self.low(), self.high(), self.n_buckets())

Response: Return an array of lower bounds for each bucket in the histogram.


Instruction:
Write a Python 3 script to
return a new Project ID based on the number of the current institute.

Response:
def get_new_pid(institute):
    """ Return a new Project ID
    Keyword arguments:
    institute_id -- Institute id
    """
    number = '0001'
    prefix = 'p%s' % institute.name.replace(' ', '')[:4]

    found = True
    while found:
        try:
            Project.objects.get(pid=prefix + number)
            number = str(int(number) + 1)
            if len(number) == 1:
                number = '000' + number
            elif len(number) == 2:
                number = '00' + number
            elif len(number) == 3:
                number = '0' + number
        except Project.DoesNotExist:
            found = False

    return prefix + number


Instruction:
Can you implement a function in Python 3 that
adds items from extra_context dict to context.

Response:
def apply_extra_context(extra_context, context):
    """
    Adds items from extra_context dict to context.  If a value in extra_context
    is callable, then it is called and the result is added to context.
    """
    for key, value in six.iteritems(extra_context):
        if callable(value):
            context[key] = value()
        else:
            context[key] = value


Instruction:
Explain what the following Python 3 code does
def get_model_and_form_class(model, form_class):
    if form_class:
        return form_class._meta.model, form_class
    if model:
        # The inner Meta class fails if model = model is used for some reason.
        tmp_model = model
        # TODO: we should be able to construct a ModelForm without creating
        # and passing in a temporary inner class.

        class Meta:
            model = tmp_model

        class_name = model.__name__ + 'Form'
        form_class = ModelFormMetaclass(
            class_name, (ModelForm,), {'Meta': Meta})
        return model, form_class
    raise GenericViewError("Generic view must be called with either a model or"
                           " form_class argument.")

Response: Returns a model and form class based on the model and form_class parameters.


Instruction:
Create a Python 3 function for
returning a HttpResponseRedirect to the post_save_redirect parameter.

Response:
def redirect(post_save_redirect, obj):
    """
    Returns a HttpResponseRedirect to ``post_save_redirect``.

    ``post_save_redirect`` should be a string, and can contain named string-
    substitution place holders of ``obj`` field names.

    If ``post_save_redirect`` is None, then redirect to ``obj``'s URL returned
    by ``get_absolute_url()``.  If ``obj`` has no ``get_absolute_url`` method,
    then raise ImproperlyConfigured.

    This function is meant to handle the post_save_redirect parameter to the
    ``create_object`` and ``update_object`` views.
    """
    if post_save_redirect:
        return HttpResponseRedirect(post_save_redirect % obj.__dict__)
    elif hasattr(obj, 'get_absolute_url'):
        return HttpResponseRedirect(obj.get_absolute_url())
    else:
        raise ImproperlyConfigured(
            "No URL to redirect to.  Either pass a post_save_redirect"
            " parameter to the generic view or define a get_absolute_url"
            " method on the Model.")


Instruction:
Can you generate the documentation for the following Python 3 function
def lookup_object(model, object_id, slug, slug_field):
    lookup_kwargs = {}
    if object_id:
        lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id
    elif slug and slug_field:
        lookup_kwargs['%s__exact' % slug_field] = slug
    else:
        raise GenericViewError(
            "Generic view must be called with either an object_id or a"
            " slug/slug_field.")
    try:
        return model.objects.get(**lookup_kwargs)
    except ObjectDoesNotExist:
        raise Http404("No %s found for %s"
                      % (model._meta.verbose_name, lookup_kwargs))

Response: Lookup the object with the passed object_id.


Instruction:
Explain what the following Python 3 code does
def create_object(
        request, model=None, template_name=None,
        template_loader=loader, extra_context=None, post_save_redirect=None,
        login_required=False, context_processors=None, form_class=None):
    """
    Generic object-creation function.

    Templates: ``<app_label>/<model_name>_form.html``
    Context:
        form
            the form for the object
    """
    if extra_context is None:
        extra_context = {}
    if login_required and not request.user.is_authenticated:
        return redirect_to_login(request.path)

    model, form_class = get_model_and_form_class(model, form_class)
    if request.method == 'POST':
        form = form_class(request.POST, request.FILES)
        if form.is_valid():
            new_object = form.save()

            msg = ugettext("The %(verbose_name)s was created successfully.") %\
                {"verbose_name": model._meta.verbose_name}
            messages.success(request, msg, fail_silently=True)
            return redirect(post_save_redirect, new_object)
    else:
        form = form_class()

    # Create the template, context, response
    if not template_name:
        template_name = "%s/%s_form.html" % (
            model._meta.app_label, model._meta.object_name.lower())
    t = template_loader.get_template(template_name)
    c = {
        'form': form,
    }
    apply_extra_context(extra_context, c)
    return HttpResponse(t.render(context=c, request=request))

Response: Create an object of the specified type.


Instruction:
How would you explain what the following Python 3 function does
def add_params(self, **kw):
        for k, v in kw.items():
            if k not in self.params:
                raise ValidationError(
                    '"%s" is not a valid parameter for %s'
                    % (k, self.__class__.__name__))
            self.params[k].validate(v)

        self._orig_kwargs.update(kw)
        self.kwargs = self._orig_kwargs.copy()

Response: Add parameters to the Assembly.


Instruction:
Explain what the following Python 3 code does
def remove_params(self, *args):
        for a in args:
            self._orig_kwargs.pop(a)
        self.kwargs = self._orig_kwargs.copy()

Response: Remove parameters from the Assembly.


Instruction:
How would you implement a function in Python 3 that
hooks that executes whenever the group members are changed.

Response:
def _members_changed(
        sender, instance, action, reverse, model, pk_set, **kwargs):
    """
    Hook that executes whenever the group members are changed.
    """
    if action == "post_remove":
        if not reverse:
            group = instance
            for person in model.objects.filter(pk__in=pk_set):
                _remove_group(group, person)
        else:
            person = instance
            for group in model.objects.filter(pk__in=pk_set):
                _remove_group(group, person)

    elif action == "pre_clear":
        # This has to occur in pre_clear, not post_clear, as otherwise
        # we won't see what groups need to be removed.
        if not reverse:
            group = instance
            for person in group.members.all():
                _remove_group(group, person)
        else:
            person = instance
            for group in person.groups.all():
                _remove_group(group, person)


Instruction:
Can you generate the documentation for the following Python 3 function
def validate_username(username):

    # Check username looks ok

    if not username.islower():
        raise UsernameInvalid(six.u('Username must be all lowercase'))
    if len(username) < 2:
        raise UsernameInvalid(six.u('Username must be at least 2 characters'))
    if not username_re.search(username):
        raise UsernameInvalid(settings.USERNAME_VALIDATION_ERROR_MSG)

    return username

Response:
Validate the new username. If the username is invalid, raises
    :py:exc:`UsernameInvalid`.

    :param username: Username to validate.


Instruction:
Can you implement a function in Python 3 that
validates the new username for a new person.

Response:
def validate_username_for_new_person(username):
    """ Validate the new username for a new person. If the username is invalid
    or in use, raises :py:exc:`UsernameInvalid` or :py:exc:`UsernameTaken`.

    :param username: Username to validate.
    """

    # is the username valid?

    validate_username(username)

    # Check for existing people

    count = Person.objects.filter(username__exact=username).count()
    if count >= 1:
        raise UsernameTaken(six.u(
            'The username is already taken. Please choose another. '
            'If this was the name of your old account please email %s')
            % settings.ACCOUNTS_EMAIL)

    # Check for existing accounts

    count = Account.objects.filter(username__exact=username).count()
    if count >= 1:
        raise UsernameTaken(six.u(
            'The username is already taken. Please choose another. '
            'If this was the name of your old account please email %s')
            % settings.ACCOUNTS_EMAIL)

    # Check account datastore, in case username created outside Karaage.

    if account_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external account datastore.'))

    return username


Instruction:
Explain what the following Python 3 code does
def validate_username_for_new_account(person, username):

    # This is much the same as validate_username_for_new_person, except
    # we don't care if the username is used by the person owning the account

    # is the username valid?

    validate_username(username)

    # Check for existing people

    query = Person.objects.filter(username__exact=username)
    count = query.exclude(pk=person.pk).count()
    if count >= 1:
        raise UsernameTaken(six.u(
            'The username is already taken. Please choose another. '
            'If this was the name of your old account please email %s')
            % settings.ACCOUNTS_EMAIL)

    # Check for existing accounts not belonging to this person

    query = Account.objects.filter(username__exact=username)
    count = query.exclude(person__pk=person.pk).count()
    if count >= 1:
        raise UsernameTaken(six.u(
            'The username is already taken. Please choose another. '
            'If this was the name of your old account please email %s')
            % settings.ACCOUNTS_EMAIL)

    # Check datastore, in case username created outside Karaage.
    # Make sure we don't count the entry for person.

    query = Person.objects.filter(username__exact=username)
    count = query.filter(pk=person.pk).count()
    if count == 0 and account_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external personal datastore.'))

Response: Validate the new username for a new account.


Instruction:
Here you have a function in Python 3, explain what it does
def check_username_for_new_account(person, username):

    query = Account.objects.filter(
        username__exact=username,
        date_deleted__isnull=True)

    if query.count() > 0:
        raise UsernameTaken(
            six.u('Username already in use.')
        )

    if account_exists(username):
        raise UsernameTaken(
            six.u('Username is already in datastore.')
        )

    return username

Response: Checks the new username for a new account. If the username is already in use raises a UsernameTaken.


Instruction:
Here you have a function in Python 3, explain what it does
def validate_username_for_rename_person(username, person):

    # is the username valid?

    validate_username(username)

    # Check for existing people

    count = Person.objects.filter(username__exact=username) \
        .exclude(pk=person.pk).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken by an existing person.'))

    # Check for existing accounts not owned by person.
    # If there is a conflicting account owned by person it doesn't matter.

    count = Account.objects.filter(username__exact=username) \
        .exclude(person=person).count()
    if count >= 1:
        raise UsernameTaken(
            six.u('The username is already taken by an existing account.'))

    # Check datastore, in case username created outside Karaage.

    count = Account.objects.filter(
        username__exact=username,
        person=person,
        date_deleted__isnull=True).count()
    if count == 0 and account_exists(username):
        raise UsernameTaken(
            six.u('Username is already in external account datastore.'))

Response: Validate the new username for rename person.


Instruction:
Write a Python 3 script to
retrieve a link that will work for the current user.

Response:
def get_url(request, application, roles, label=None):
    """ Retrieve a link that will work for the current user. """
    args = []
    if label is not None:
        args.append(label)

    # don't use secret_token unless we have to
    if 'is_admin' in roles:
        # Administrators can access anything without secrets
        require_secret = False
    elif 'is_applicant' not in roles:
        # we never give secrets to anybody but the applicant
        require_secret = False
    elif not request.user.is_authenticated:
        # If applicant is not logged in, we redirect them to secret URL
        require_secret = True
    elif request.user != application.applicant:
        # If logged in as different person, we redirect them to secret
        # URL. This could happen if the application was open with a different
        # email address, and the applicant is logged in when accessing it.
        require_secret = True
    else:
        # otherwise redirect them to URL that requires correct login.
        require_secret = False

    # return required url
    if not require_secret:
        url = reverse(
            'kg_application_detail',
            args=[application.pk, application.state] + args)
    else:
        url = reverse(
            'kg_application_unauthenticated',
            args=[application.secret_token, application.state] + args)
    return url


Instruction:
Can you generate the documentation for the following Python 3 function
def get_admin_email_link(application):
    url = '%s/applications/%d/' % (settings.ADMIN_BASE_URL, application.pk)
    is_secret = False
    return url, is_secret

Response: Retrieve a link that can be emailed to the administrator.


Instruction:
Can you tell what is the following Python 3 function doing
def get_registration_email_link(application):
    url = '%s/applications/%d/' % (
        settings.REGISTRATION_BASE_URL, application.pk)
    is_secret = False
    return url, is_secret

Response: Retrieve a link that can be emailed to the logged other users.


Instruction:
Here you have a function in Python 3, explain what it does
def get_email_link(application):
    # don't use secret_token unless we have to
    if (application.content_type.model == 'person'
            and application.applicant.has_usable_password()):
        url = '%s/applications/%d/' % (
            settings.REGISTRATION_BASE_URL, application.pk)
        is_secret = False
    else:
        url = '%s/applications/%s/' % (
            settings.REGISTRATION_BASE_URL, application.secret_token)
        is_secret = True
    return url, is_secret

Response: Retrieve a link that can be emailed to the applicant.


Instruction:
How would you explain what the following Python 3 function does
def start(self, request, application, extra_roles=None):
        # Get the authentication of the current user
        roles = self._get_roles_for_request(request, application)
        if extra_roles is not None:
            roles.update(extra_roles)

        # Ensure current user is authenticated. If user isn't applicant,
        # leader, delegate or admin, they probably shouldn't be here.
        if 'is_authorised' not in roles:
            return HttpResponseForbidden('
Access Denied
')

        # Go to first state.
        return self._next(request, application, roles, self._first_state)

Response: Start the state machine.


Instruction:
Explain what the following Python 3 code does
def process(
            self, request, application,
            expected_state, label, extra_roles=None):
        """ Process the view request at the current state. """

        # Get the authentication of the current user
        roles = self._get_roles_for_request(request, application)
        if extra_roles is not None:
            roles.update(extra_roles)

        # Ensure current user is authenticated. If user isn't applicant,
        # leader, delegate or admin, they probably shouldn't be here.
        if 'is_authorised' not in roles:
            return HttpResponseForbidden('
Access Denied
')

        # If user didn't supply state on URL, redirect to full URL.
        if expected_state is None:
            url = get_url(request, application, roles, label)
            return HttpResponseRedirect(url)

        # Check that the current state is valid.
        if application.state not in self._config:
            raise RuntimeError("Invalid current state '%s'" % application.state)

        # If state user expected is different to state we are in, warn user
        # and jump to expected state.
        if expected_state != application.state:
            # post data will be lost
            if request.method == "POST":
                messages.warning(
                    request,
                    "Discarding request and jumping to current state.")
            # note we discard the label, it probably isn't relevant for new
            # state
            url = get_url(request, application, roles)
            return HttpResponseRedirect(url)

        # Get the current state for this application
        state_config = self._config[application.state]

        # Finally do something
        instance = load_state_instance(state_config)
        if request.method == "GET":
            # if method is GET, state does not ever change.
            response = instance.get_next_config(request, application, label, roles)
            assert isinstance(response, HttpResponse)
            return response

        elif request.method == "POST":
            # if method is POST, it can return a HttpResponse or a string
            response = instance.get_next_config(request, application, label, roles)
            if isinstance(response, HttpResponse):
                # If it returned a HttpResponse, state not changed, just
                # display
                return response
            else:
                # If it returned a string, lookit up in the actions for this
                # state
                next_config = response
                # Go to the next state
                return self._next(request, application, roles, next_config)

        else:
            # Shouldn't happen, user did something weird
            return HttpResponseBadRequest("
Bad Request
")

Response: Process the view request at the current state of the application.


Instruction:
How would you explain what the following Python 3 function does
def _get_roles_for_request(request, application):
        roles = application.get_roles_for_person(request.user)

        if common.is_admin(request):
            roles.add("is_admin")
            roles.add('is_authorised')

        return roles

Response: Get the roles that the user is authorized to access.


Instruction:
Create a Python 3 function for
continuing the state machine at given state.

Response:
def _next(self, request, application, roles, next_config):
        """ Continue the state machine at given state. """
        # we only support state changes for POST requests
        if request.method == "POST":
            key = None

            # If next state is a transition, process it
            while True:
                # We do not expect to get a direct state transition here.
                assert next_config['type'] in ['goto', 'transition']

                while next_config['type'] == 'goto':
                    key = next_config['key']
                    next_config = self._config[key]

                instance = load_instance(next_config)

                if not isinstance(instance, Transition):
                    break

                next_config = instance.get_next_config(request, application, roles)

            # lookup next state
            assert key is not None
            state_key = key

            # enter that state
            instance.enter_state(request, application)
            application.state = state_key
            application.save()

            # log details
            log.change(application.application_ptr, "state: %s" % instance.name)

            # redirect to this new state
            url = get_url(request, application, roles)
            return HttpResponseRedirect(url)
        else:
            return HttpResponseBadRequest("
Bad Request
")


Instruction:
How would you implement a function in Python 3 that
checks that the user is authenticated and if not raise PermissionDenied

Response:
def check_auth(self, request):
        """
        to ensure that nobody can get your data via json simply by knowing the
        URL.  public facing forms should write a custom LookupChannel to
        implement as you wish.  also you could choose to return
        HttpResponseForbidden("who are you?") instead of raising
        PermissionDenied (401 response)
        """
        if not request.user.is_authenticated:
            raise PermissionDenied
        if not is_admin(request):
            raise PermissionDenied