Instruction:
Here you have a function in Python 3, explain what it does
def get_json_results(self, response):
        '''
        Parses the request result and returns the JSON object. Handles all errors.
        '''
        try:
            # return the proper JSON object, or error code if request didn't go through.
            self.most_recent_json = response.json()
            json_results = response.json()
            if response.status_code in [401, 403]: #401 is invalid key, 403 is out of monthly quota.
                raise PyMsCognitiveWebSearchException("CODE {code}: {message}".format(code=response.status_code,message=json_results["message"]) )
            elif response.status_code in [429]: #429 means try again in x seconds.
                message = json_results['message']
                try:
                    # extract time out seconds from response
                    timeout = int(re.search('in (.+?) seconds', message).group(1)) + 1
                    print ("CODE 429, sleeping for {timeout} seconds").format(timeout=str(timeout))
                    time.sleep(timeout)
                except (AttributeError, ValueError) as e:
                    if not self.silent_fail:
                        raise PyMsCognitiveWebSearchException("CODE 429. Failed to auto-sleep: {message}".format(code=response.status_code,message=json_results["message"]) )
                    else:
                        print ("CODE 429. Failed to auto-sleep: {message}. Trying again in 5 seconds.".format(code=response.status_code,message=json_results["message"]))
                        time.sleep(5)
        except ValueError as vE:
            if not self.silent_fail:
                raise PyMsCognitiveWebSearchException("Request returned with code %s, error msg: %s" % (r.status_code, r.text))
            else:
                print ("[ERROR] Request returned with code %s, error msg: %s. \nContinuing in 5 seconds." % (r.status_code, r.text))
                time.sleep(5)
        return json_results

Response: Parses the response and returns the JSON object. Handles all errors.


Instruction:
Write a Python 3 function that can
search all the available resource types for all available resources.

Response:
def search_all(self, quota=50, format='json'):
        '''
        Returns a single list containing up to 'limit' Result objects
        Will keep requesting until quota is met
        Will also truncate extra results to return exactly the given quota
        '''
        quota_left = quota
        results = []
        while quota_left > 0:
            more_results = self._search(quota_left, format)
            if not more_results:
                break
            results += more_results
            quota_left = quota_left - len(more_results)
            time.sleep(1)
        results = results[0:quota]
        return results


Instruction:
Make a summary of the following Python 3 code
def format_parameters(params):
    '''Reformat parameters into dict of format expected by the API.'''

    if not params:
        return {}

    # expect multiple invocations of --parameters but fall back
    # to ; delimited if only one --parameters is specified
    if len(params) == 1:
        if params[0].find(';') != -1:  # found
            params = params[0].split(';')
        else:
            params = params[0].split(',')

    parameters = {}
    for p in params:
        try:
            (n, v) = p.split('=', 1)
        except ValueError:
            msg = '%s(%s). %s.' % ('Malformed parameter', p,
                                   'Use the key=value format')
            raise exc.CommandError(msg)

        if n not in parameters:
            parameters[n] = v
        else:
            if not isinstance(parameters[n], list):
                parameters[n] = [parameters[n]]
            parameters[n].append(v)

    return parameters

Response: Reformat parameters into dict of format expected by the API.


Instruction:
How would you implement a function in Python 3 that
deletes a specific alarm.

Response:
def delete(self, **kwargs):
        """Delete a specific alarm."""
        url_str = self.base_url + '/%s' % kwargs['alarm_id']
        resp = self.client.delete(url_str)
        return resp


Instruction:
Write a Python 3 function for
returning version details of the running server api

Response:
def do_version():
    """Return version details of the running server api"""
    v = ApiPool.ping.model.Version(
        name=ApiPool().current_server_name,
        version=ApiPool().current_server_api.get_version(),
        container=get_container_version(),
    )
    log.info("/version: " + pprint.pformat(v))
    return v


Instruction:
Given the following Python 3 function, write the documentation
def do_metric_create_raw(mc, args):
    '''Create metric from raw json body.'''
    try:
        mc.metrics.create(**args.jsonbody)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print('Successfully created metric')

Response: Create metric from raw json body.


Instruction:
How would you code a function in Python 3 to
list names of metrics.

Response:
def do_metric_name_list(mc, args):
    '''List names of metrics.'''
    fields = {}
    if args.dimensions:
        fields['dimensions'] = utils.format_dimensions_query(args.dimensions)
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        metric_names = mc.metrics.list_names(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(metric_names))
            return
        if isinstance(metric_names, list):
            utils.print_list(metric_names, ['Name'], formatters={'Name': lambda x: x['name']})


Instruction:
How would you code a function in Python 3 to
list metrics for this tenant.

Response:
def do_metric_list(mc, args):
    '''List metrics for this tenant.'''
    fields = {}
    if args.name:
        fields['name'] = args.name
    if args.dimensions:
        fields['dimensions'] = utils.format_dimensions_query(args.dimensions)
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.starttime:
        _translate_starttime(args)
        fields['start_time'] = args.starttime
    if args.endtime:
        fields['end_time'] = args.endtime
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        metric = mc.metrics.list(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(metric))
            return
        cols = ['name', 'dimensions']
        formatters = {
            'name': lambda x: x['name'],
            'dimensions': lambda x: utils.format_dict(x['dimensions']),
        }
        if isinstance(metric, list):
            # print the list
            utils.print_list(metric, cols, formatters=formatters)
        else:
            # add the dictionary to a list, so print_list works
            metric_list = list()
            metric_list.append(metric)
            utils.print_list(
                metric_list,
                cols,
                formatters=formatters)


Instruction:
Implement a function in Python 3 to
list names of metric dimensions.

Response:
def do_dimension_name_list(mc, args):
    '''List names of metric dimensions.'''
    fields = {}
    if args.metric_name:
        fields['metric_name'] = args.metric_name
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        dimension_names = mc.metrics.list_dimension_names(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))

    if args.json:
        print(utils.json_formatter(dimension_names))
        return

    if isinstance(dimension_names, list):
        utils.print_list(dimension_names, ['Dimension Names'], formatters={
            'Dimension Names': lambda x: x['dimension_name']})


Instruction:
How would you implement a function in Python 3 that
lists names of metric dimensions.

Response:
def do_dimension_value_list(mc, args):
    '''List names of metric dimensions.'''
    fields = {}
    fields['dimension_name'] = args.dimension_name
    if args.metric_name:
        fields['metric_name'] = args.metric_name
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        dimension_values = mc.metrics.list_dimension_values(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))

    if args.json:
        print(utils.json_formatter(dimension_values))
        return

    if isinstance(dimension_values, list):
        utils.print_list(dimension_values, ['Dimension Values'], formatters={
            'Dimension Values': lambda x: x['dimension_value']})


Instruction:
Can you generate a brief explanation for the following Python 3 code
def do_metric_statistics(mc, args):
    '''List measurement statistics for the specified metric.'''
    statistic_types = ['AVG', 'MIN', 'MAX', 'COUNT', 'SUM']
    statlist = args.statistics.split(',')
    for stat in statlist:
        if stat.upper() not in statistic_types:
            errmsg = ('Invalid type, not one of [' +
                      ', '.join(statistic_types) + ']')
            raise osc_exc.CommandError(errmsg)

    fields = {}
    fields['name'] = args.name
    if args.dimensions:
        fields['dimensions'] = utils.format_dimensions_query(args.dimensions)
    _translate_starttime(args)
    fields['start_time'] = args.starttime
    if args.endtime:
        fields['end_time'] = args.endtime
    if args.period:
        fields['period'] = args.period
    fields['statistics'] = args.statistics
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.merge_metrics:
        fields['merge_metrics'] = args.merge_metrics
    if args.group_by:
        fields['group_by'] = args.group_by
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        metric = mc.metrics.list_statistics(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(metric))
            return
        cols = ['name', 'dimensions']
        # add dynamic column names
        if metric:
            column_names = metric[0]['columns']
            for name in column_names:
                cols.append(name)
        else:
            # when empty set, print_list needs a col
            cols.append('timestamp')

        formatters = {
            'name': lambda x: x['name'],
            'dimensions': lambda x: utils.format_dict(x['dimensions']),
            'timestamp': lambda x:
            format_statistic_timestamp(x['statistics'], x['columns'],
                                       'timestamp'),
            'avg': lambda x:
            format_statistic_value(x['statistics'], x['columns'], 'avg'),
            'min': lambda x:
            format_statistic_value(x['statistics'], x['columns'], 'min'),
            'max': lambda x:
            format_statistic_value(x['statistics'], x['columns'], 'max'),
            'count': lambda x:
            format_statistic_value(x['statistics'], x['columns'], 'count'),
            'sum': lambda x:
            format_statistic_value(x['statistics'], x['columns'], 'sum'),
        }
        if isinstance(metric, list):
            # print the list
            utils.print_list(metric, cols, formatters=formatters)
        else:
            # add the dictionary to a list, so print_list works
            metric_list = list()
            metric_list.append(metric)
            utils.print_list(
                metric_list,
                cols,
                formatters=formatters)

Response: List measurement statistics for the specified metric.


Instruction:
Here you have a function in Python 3, explain what it does
def do_notification_list(mc, args):
    '''List notifications for this tenant.'''
    fields = {}
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.sort_by:
        sort_by = args.sort_by.split(',')
        for field in sort_by:
            field_values = field.lower().split()
            if len(field_values) > 2:
                print("Invalid sort_by value {}".format(field))
            if field_values[0] not in allowed_notification_sort_by:
                print("Sort-by field name {} is not in [{}]".format(field_values[0],
                                                                    allowed_notification_sort_by))
                return
            if len(field_values) > 1 and field_values[1] not in ['asc', 'desc']:
                print("Invalid value {}, must be asc or desc".format(field_values[1]))
        fields['sort_by'] = args.sort_by

    try:
        notification = mc.notifications.list(**fields)
    except osc_exc.ClientException as he:
        raise osc_exc.CommandError(
            'ClientException code=%s message=%s' %
            (he.code, he.message))
    else:
        if args.json:
            print(utils.json_formatter(notification))
            return
        cols = ['name', 'id', 'type', 'address', 'period']
        formatters = {
            'name': lambda x: x['name'],
            'id': lambda x: x['id'],
            'type': lambda x: x['type'],
            'address': lambda x: x['address'],
            'period': lambda x: x['period'],
        }
        if isinstance(notification, list):

            utils.print_list(
                notification,
                cols,
                formatters=formatters)
        else:
            notif_list = list()
            notif_list.append(notification)
            utils.print_list(notif_list, cols, formatters=formatters)

Response: List notifications for this tenant.


Instruction:
Write a Python 3 function for
updating a single notification.

Response:
def do_notification_update(mc, args):
    '''Update notification.'''
    fields = {}
    fields['notification_id'] = args.id
    fields['name'] = args.name

    fields['type'] = args.type
    fields['address'] = args.address
    if not _validate_notification_period(args.period, args.type.upper()):
        return
    fields['period'] = args.period
    try:
        notification = mc.notifications.update(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print(jsonutils.dumps(notification, indent=2))


Instruction:
Here you have a function in Python 3, explain what it does
def do_alarm_definition_create(mc, args):
    '''Create an alarm definition.'''
    fields = {}
    fields['name'] = args.name
    if args.description:
        fields['description'] = args.description
    fields['expression'] = args.expression
    if args.alarm_actions:
        fields['alarm_actions'] = args.alarm_actions
    if args.ok_actions:
        fields['ok_actions'] = args.ok_actions
    if args.undetermined_actions:
        fields['undetermined_actions'] = args.undetermined_actions
    if args.severity:
        if not _validate_severity(args.severity):
            return
        fields['severity'] = args.severity
    if args.match_by:
        fields['match_by'] = args.match_by.split(',')
    try:
        alarm = mc.alarm_definitions.create(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print(jsonutils.dumps(alarm, indent=2))

Response: Create an alarm definition.


Instruction:
How would you implement a function in Python 3 that
lists alarm definitions for this tenant.

Response:
def do_alarm_definition_list(mc, args):
    '''List alarm definitions for this tenant.'''
    fields = {}
    if args.name:
        fields['name'] = args.name
    if args.dimensions:
        fields['dimensions'] = utils.format_dimensions_query(args.dimensions)
    if args.severity:
        if not _validate_severity(args.severity):
            return
        fields['severity'] = args.severity
    if args.sort_by:
        sort_by = args.sort_by.split(',')
        for field in sort_by:
            field_values = field.split()
            if len(field_values) > 2:
                print("Invalid sort_by value {}".format(field))
            if field_values[0] not in allowed_definition_sort_by:
                print("Sort-by field name {} is not in [{}]".format(field_values[0],
                                                                    allowed_definition_sort_by))
                return
            if len(field_values) > 1 and field_values[1] not in ['asc', 'desc']:
                print("Invalid value {}, must be asc or desc".format(field_values[1]))
        fields['sort_by'] = args.sort_by
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    try:
        alarm = mc.alarm_definitions.list(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(alarm))
            return
        cols = ['name', 'id', 'expression', 'match_by', 'actions_enabled']
        formatters = {
            'name': lambda x: x['name'],
            'id': lambda x: x['id'],
            'expression': lambda x: x['expression'],
            'match_by': lambda x: utils.format_list(x['match_by']),
            'actions_enabled': lambda x: x['actions_enabled'],
        }
        if isinstance(alarm, list):
            # print the list
            utils.print_list(alarm, cols, formatters=formatters)
        else:
            # add the dictionary to a list, so print_list works
            alarm_list = list()
            alarm_list.append(alarm)
            utils.print_list(alarm_list, cols, formatters=formatters)


Instruction:
Write a Python 3 script for
deleting the alarm definition.

Response:
def do_alarm_definition_delete(mc, args):
    '''Delete the alarm definition.'''
    fields = {}
    fields['alarm_id'] = args.id
    try:
        mc.alarm_definitions.delete(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print('Successfully deleted alarm definition')


Instruction:
Can you implement a function in Python 3 that
updates the alarm definition.

Response:
def do_alarm_definition_update(mc, args):
    '''Update the alarm definition.'''
    fields = {}
    fields['alarm_id'] = args.id
    fields['name'] = args.name
    fields['description'] = args.description
    fields['expression'] = args.expression
    fields['alarm_actions'] = _arg_split_patch_update(args.alarm_actions)
    fields['ok_actions'] = _arg_split_patch_update(args.ok_actions)
    fields['undetermined_actions'] = _arg_split_patch_update(args.undetermined_actions)
    if args.actions_enabled not in enabled_types:
        errmsg = ('Invalid value, not one of [' +
                  ', '.join(enabled_types) + ']')
        print(errmsg)
        return
    fields['actions_enabled'] = args.actions_enabled in ['true', 'True']
    fields['match_by'] = _arg_split_patch_update(args.match_by)
    if not _validate_severity(args.severity):
        return
    fields['severity'] = args.severity
    try:
        alarm = mc.alarm_definitions.update(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print(jsonutils.dumps(alarm, indent=2))


Instruction:
How would you implement a function in Python 3 that
patches the alarm definition.

Response:
def do_alarm_definition_patch(mc, args):
    '''Patch the alarm definition.'''
    fields = {}
    fields['alarm_id'] = args.id
    if args.name:
        fields['name'] = args.name
    if args.description:
        fields['description'] = args.description
    if args.expression:
        fields['expression'] = args.expression
    if args.alarm_actions:
        fields['alarm_actions'] = _arg_split_patch_update(args.alarm_actions, patch=True)
    if args.ok_actions:
        fields['ok_actions'] = _arg_split_patch_update(args.ok_actions, patch=True)
    if args.undetermined_actions:
        fields['undetermined_actions'] = _arg_split_patch_update(args.undetermined_actions,
                                                                 patch=True)
    if args.actions_enabled:
        if args.actions_enabled not in enabled_types:
            errmsg = ('Invalid value, not one of [' +
                      ', '.join(enabled_types) + ']')
            print(errmsg)
            return
        fields['actions_enabled'] = args.actions_enabled in ['true', 'True']
    if args.severity:
        if not _validate_severity(args.severity):
            return
        fields['severity'] = args.severity
    try:
        alarm = mc.alarm_definitions.patch(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print(jsonutils.dumps(alarm, indent=2))


Instruction:
Explain what the following Python 3 code does
def do_alarm_list(mc, args):
    '''List alarms for this tenant.'''
    fields = {}
    if args.alarm_definition_id:
        fields['alarm_definition_id'] = args.alarm_definition_id
    if args.metric_name:
        fields['metric_name'] = args.metric_name
    if args.metric_dimensions:
        fields['metric_dimensions'] = utils.format_dimensions_query(args.metric_dimensions)
    if args.state:
        if args.state.upper() not in state_types:
            errmsg = ('Invalid state, not one of [' +
                      ', '.join(state_types) + ']')
            print(errmsg)
            return
        fields['state'] = args.state
    if args.severity:
        if not _validate_severity(args.severity):
            return
        fields['severity'] = args.severity
    if args.state_updated_start_time:
        fields['state_updated_start_time'] = args.state_updated_start_time
    if args.lifecycle_state:
        fields['lifecycle_state'] = args.lifecycle_state
    if args.link:
        fields['link'] = args.link
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.sort_by:
        sort_by = args.sort_by.split(',')
        for field in sort_by:
            field_values = field.lower().split()
            if len(field_values) > 2:
                print("Invalid sort_by value {}".format(field))
            if field_values[0] not in allowed_alarm_sort_by:
                print("Sort-by field name {} is not in [{}]".format(field_values[0],
                                                                    allowed_alarm_sort_by))
                return
            if len(field_values) > 1 and field_values[1] not in ['asc', 'desc']:
                print("Invalid value {}, must be asc or desc".format(field_values[1]))
        fields['sort_by'] = args.sort_by
    try:
        alarm = mc.alarms.list(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(alarm))
            return
        cols = ['id', 'alarm_definition_id', 'alarm_definition_name', 'metric_name',
                'metric_dimensions', 'severity', 'state', 'lifecycle_state', 'link',
                'state_updated_timestamp', 'updated_timestamp', "created_timestamp"]
        formatters = {
            'id': lambda x: x['id'],
            'alarm_definition_id': lambda x: x['alarm_definition']['id'],
            'alarm_definition_name': lambda x: x['alarm_definition']['name'],
            'metric_name': lambda x: format_metric_name(x['metrics']),
            'metric_dimensions': lambda x: format_metric_dimensions(x['metrics']),
            'severity': lambda x: x['alarm_definition']['severity'],
            'state': lambda x: x['state'],
            'lifecycle_state': lambda x: x['lifecycle_state'],
            'link': lambda x: x['link'],
            'state_updated_timestamp': lambda x: x['state_updated_timestamp'],
            'updated_timestamp': lambda x: x['updated_timestamp'],
            'created_timestamp': lambda x: x['created_timestamp'],
        }
        if isinstance(alarm, list):
            # print the list
            utils.print_list(alarm, cols, formatters=formatters)
        else:
            # add the dictionary to a list, so print_list works
            alarm_list = list()
            alarm_list.append(alarm)
            utils.print_list(alarm_list, cols, formatters=formatters)

Response: List alarms for this tenant.


Instruction:
Here you have a function in Python 3, explain what it does
def do_alarm_update(mc, args):
    '''Update the alarm state.'''
    fields = {}
    fields['alarm_id'] = args.id
    if args.state.upper() not in state_types:
            errmsg = ('Invalid state, not one of [' +
                      ', '.join(state_types) + ']')
            print(errmsg)
            return
    fields['state'] = args.state
    fields['lifecycle_state'] = args.lifecycle_state
    fields['link'] = args.link
    try:
        alarm = mc.alarms.update(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        print(jsonutils.dumps(alarm, indent=2))

Response: Update the alarm state.


Instruction:
Implement a Python 3 function for
alarming state transition history.

Response:
def do_alarm_history(mc, args):
    '''Alarm state transition history.'''
    fields = {}
    fields['alarm_id'] = args.id
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    try:
        alarm = mc.alarms.history(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        output_alarm_history(args, alarm)


Instruction:
Implement a Python 3 function for
listing alarms state history.

Response:
def do_alarm_history_list(mc, args):
    '''List alarms state history.'''
    fields = {}
    if args.dimensions:
        fields['dimensions'] = utils.format_parameters(args.dimensions)
    if args.starttime:
        _translate_starttime(args)
        fields['start_time'] = args.starttime
    if args.endtime:
        fields['end_time'] = args.endtime
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    try:
        alarm = mc.alarms.history_list(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        output_alarm_history(args, alarm)


Instruction:
Can you write a function in Python 3 where it
lists notification types supported by monasca.

Response:
def do_notification_type_list(mc, args):
    '''List notification types supported by monasca.'''

    try:
        notification_types = mc.notificationtypes.list()
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(notification_types))
            return
        else:
            formatters = {'types': lambda x: x["type"]}
            # utils.print_list(notification_types['types'], ["types"], formatters=formatters)
            utils.print_list(notification_types, ["types"], formatters=formatters)


Instruction:
Create a Python 3 function to
validate an auth0 token and return the payload or an exception if the token is invalid.

Response:
def load_auth_token(token, load=True):
    """Validate an auth0 token. Returns the token's payload, or an exception
    of the type:"""

    assert get_config().jwt_secret, "No JWT secret configured for pymacaron"
    assert get_config().jwt_issuer, "No JWT issuer configured for pymacaron"
    assert get_config().jwt_audience, "No JWT audience configured for pymacaron"

    log.info("Validating token, using issuer:%s, audience:%s, secret:%s***" % (
        get_config().jwt_issuer,
        get_config().jwt_audience,
        get_config().jwt_secret[1:8],
    ))

    # First extract the issuer
    issuer = get_config().jwt_issuer
    try:
        headers = jwt.get_unverified_header(token)
    except jwt.DecodeError:
        raise AuthInvalidTokenError('token signature is invalid')

    log.debug("Token has headers %s" % headers)

    if 'iss' in headers:
        issuer = headers['iss']

    # Then validate the token against this issuer
    log.info("Validating token in issuer %s" % issuer)
    try:
        payload = jwt.decode(
            token,
            get_config().jwt_secret,
            audience=get_config().jwt_audience,
            # Allow for a time difference of up to 5min (300sec)
            leeway=300
        )
    except jwt.ExpiredSignature:
        raise AuthTokenExpiredError('Auth token is expired')
    except jwt.InvalidAudienceError:
        raise AuthInvalidTokenError('incorrect audience')
    except jwt.DecodeError:
        raise AuthInvalidTokenError('token signature is invalid')
    except jwt.InvalidIssuedAtError:
        raise AuthInvalidTokenError('Token was issued in the future')

    # Save payload to stack
    payload['token'] = token
    payload['iss'] = issuer

    if load:
        stack.top.current_user = payload

    return payload


Instruction:
Make a summary of the following Python 3 code
def authenticate_http_request(token=None):

    if token:
        auth = token
    else:
        auth = request.headers.get('Authorization', None)

    if not auth:
        auth = request.cookies.get('token', None)
        if auth:
            auth = unquote_plus(auth)

    log.debug("Validating Auth header [%s]" % auth)

    if not auth:
        raise AuthMissingHeaderError('There is no Authorization header in the HTTP request')

    parts = auth.split()

    if parts[0].lower() != 'bearer':
        raise AuthInvalidTokenError('Authorization header must start with Bearer')
    elif len(parts) == 1:
        raise AuthInvalidTokenError('Token not found in Authorization header')
    elif len(parts) > 2:
        raise AuthInvalidTokenError('Authorization header must be Bearer + \s + token')

    token = parts[1]

    return load_auth_token(token)

Response: Validate auth0 tokens passed in the HTTP request.


Instruction:
How would you explain what the following Python 3 function does
def generate_token(user_id, expire_in=None, data={}, issuer=None, iat=None):
    assert user_id, "No user_id passed to generate_token()"
    assert isinstance(data, dict), "generate_token(data=) should be a dictionary"
    assert get_config().jwt_secret, "No JWT secret configured in pymacaron"

    if not issuer:
        issuer = get_config().jwt_issuer

    assert issuer, "No JWT issuer configured for pymacaron"

    if expire_in is None:
        expire_in = get_config().jwt_token_timeout

    if iat:
        epoch_now = iat
    else:
        epoch_now = to_epoch(timenow())
    epoch_end = epoch_now + expire_in

    data['iss'] = issuer
    data['sub'] = user_id
    data['aud'] = get_config().jwt_audience
    data['exp'] = epoch_end
    data['iat'] = epoch_now

    headers = {
        "typ": "JWT",
        "alg": "HS256",
        "iss": issuer,
    }

    log.debug("Encoding token with data %s and headers %s (secret:%s****)" % (data, headers, get_config().jwt_secret[0:8]))

    t = jwt.encode(
        data,
        get_config().jwt_secret,
        headers=headers,
    )

    if type(t) is bytes:
        t = t.decode("utf-8")

    return t

Response: Generate a new JWT token for the given user_id.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_user_token():
    if not hasattr(stack.top, 'current_user'):
        return ''
    current_user = stack.top.current_user
    return current_user.get('token', '')

Response: Return the authenticated user s auth token


Instruction:
Implement a Python 3 function for
returning the issuer in which this user s token was created

Response:
def get_token_issuer():
    """Return the issuer in which this user's token was created"""
    try:
        current_user = stack.top.current_user
        return current_user.get('iss', get_config().jwt_issuer)
    except Exception:
        pass
    return get_config().jwt_issuer


Instruction:
Explain what the following Python 3 code does
def _search(self, limit, format):
        '''
        Returns a list of result objects, with the url for the next page MsCognitive search url.
        '''
        limit = min(limit, self.MAX_SEARCH_PER_QUERY)
        payload = {
          'q' : self.query,
          'count' : limit, #currently 50 is max per search.
          'offset': self.current_offset,
        }
        payload.update(self.CUSTOM_PARAMS)

        headers = { 'Ocp-Apim-Subscription-Key' : self.api_key }
        if not self.silent_fail:
            QueryChecker.check_web_params(payload, headers)
        response = requests.get(self.QUERY_URL, params=payload, headers=headers)
        json_results = self.get_json_results(response)
        packaged_results = [NewsResult(single_result_json) for single_result_json in json_results["value"]]
        self.current_offset += min(50, limit, len(packaged_results))
        return packaged_results

Response: Returns a list of result objects with the url for the next page MsCognitive search url.


Instruction:
Write a Python 3 function that can
get the details for a specific notification.

Response:
def get(self, **kwargs):
        """Get the details for a specific notification."""

        # NOTE(trebskit) should actually be find_one, but
        # monasca does not support expected response format

        url = '%s/%s' % (self.base_url, kwargs['notification_id'])
        resp = self.client.list(path=url)
        return resp


Instruction:
Write a Python 3 function for
creating an alarm definition.

Response:
def create(self, **kwargs):
        """Create an alarm definition."""
        resp = self.client.create(url=self.base_url,
                                  json=kwargs)
        return resp


Instruction:
Can you tell what is the following Python 3 function doing
def update(self, **kwargs):
        url_str = self.base_url + '/%s' % kwargs['alarm_id']
        del kwargs['alarm_id']

        resp = self.client.create(url=url_str,
                                  method='PUT',
                                  json=kwargs)

        return resp

Response: Update a specific alarm definition.


Instruction:
Here you have a function in Python 3, explain what it does
def compute_y(self, coefficients, num_x):
        y_vals = []

        for x in range(1, num_x + 1):
            y = sum([c * x ** i for i, c in enumerate(coefficients[::-1])])
            y_vals.append(y)

        return y_vals

Response: Computes the y - values for the domain of x - values in [ 1 num_x ).


Instruction:
Can you implement a function in Python 3 that
computes the error value of the current set of values produced by the current set of values.

Response:
def compute_err(self, solution_y, coefficients):
        """
        Return an error value by finding the absolute difference for each
        element in a list of solution-generated y-values versus expected values.

        Compounds error by 50% for each negative coefficient in the solution.

        solution_y:  list of y-values produced by a solution
        coefficients:  list of polynomial coefficients represented by the solution

        return:  error value
        """
        error = 0
        for modeled, expected in zip(solution_y, self.expected_values):
            error += abs(modeled - expected)

        if any([c < 0 for c in coefficients]):
            error *= 1.5

        return error


Instruction:
How would you explain what the following Python 3 function does
def eval_fitness(self, chromosome):
        coefficients = self.translator.translate_chromosome(chromosome)
        solution_y = self.compute_y(coefficients, self.num_x)
        fitness = -1 * self.compute_err(solution_y, coefficients)

        return fitness

Response:
Evaluate the polynomial equation using coefficients represented by a
        solution and chromosome. Returns the error as the solution s fitness.


Instruction:
Can you generate the documentation for the following Python 3 function
def read_config(ip, mac):
    click.echo("Read configuration from %s" % ip)
    request = requests.get(
        'http://{}/{}/{}/'.format(ip, URI, mac), timeout=TIMEOUT)
    print(request.json())

Response: Read the current configuration of a myStrom device.


Instruction:
How would you explain what the following Python 3 function does
def write_config(ip, mac, single, double, long, touch):
    click.echo("Write configuration to device %s" % ip)
    data = {
        'single': single,
        'double': double,
        'long': long,
        'touch': touch,
    }
    request = requests.post(
        'http://{}/{}/{}/'.format(ip, URI, mac), data=data, timeout=TIMEOUT)

    if request.status_code == 200:
        click.echo("Configuration of %s set" % mac)

Response: Write the current configuration of a myStrom button.


Instruction:
How would you code a function in Python 3 to
write the configuration for Home Assistant to a myStrom button.

Response:
def write_ha_config(ip, mac, hass, port, id):
    """Write the configuration for Home Assistant to a myStrom button."""
    click.echo("Write configuration for Home Assistant to device %s..." % ip)

    action = "get://{1}:{2}/api/mystrom?{0}={3}"
    data = {
        'single': action.format('single', hass, port, id),
        'double':  action.format('double', hass, port, id),
        'long':  action.format('long', hass, port, id),
        'touch':  action.format('touch', hass, port, id),
    }
    request = requests.post(
        'http://{}/{}/{}/'.format(ip, URI, mac), data=data, timeout=TIMEOUT)

    if request.status_code == 200:
        click.echo("Configuration for %s set" % ip)
        click.echo("After using the push pattern the first time then "
                   "the myStrom WiFi Button will show up as %s" % id)


Instruction:
Given the following Python 3 function, write the documentation
def reset_config(ip, mac):
    click.echo("Reset configuration of button %s..." % ip)
    data = {
        'single': "",
        'double': "",
        'long': "",
        'touch': "",
    }
    request = requests.post(
        'http://{}/{}/{}/'.format(ip, URI, mac), data=data, timeout=TIMEOUT)

    if request.status_code == 200:
        click.echo("Reset configuration of %s" % mac)

Response: Reset the current configuration of a myStrom WiFi Button.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def color(ip, mac, hue, saturation, value):
    bulb = MyStromBulb(ip, mac)
    bulb.set_color_hsv(hue, saturation, value)

Response: Switch the bulb on with the given color.


Instruction:
How would you implement a function in Python 3 that
checks if host is on an ec2 instance

Response:
def is_ec2_instance():
    """Try fetching instance metadata at 'curl http://169.254.169.254/latest/meta-data/'
    to see if host is on an ec2 instance"""

    # Note: this code assumes that docker containers running on ec2 instances
    # inherit instances metadata, which they do as of 2016-08-25

    global IS_EC2_INSTANCE

    if IS_EC2_INSTANCE != -1:
        # Returned the cached value
        return IS_EC2_INSTANCE

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(0.2)
    try:
        s.connect(("169.254.169.254", 80))
        IS_EC2_INSTANCE = 1
        return True
    except socket.timeout:
        IS_EC2_INSTANCE = 0
        return False
    except socket.error:
        IS_EC2_INSTANCE = 0
        return False


Instruction:
Create a Python 3 function to
take a datetime either as a string or a datetime. datetime object and return the corresponding epoch

Response:
def to_epoch(t):
    """Take a datetime, either as a string or a datetime.datetime object,
    and return the corresponding epoch"""
    if isinstance(t, str):
        if '+' not in t:
            t = t + '+00:00'
        t = parser.parse(t)
    elif t.tzinfo is None or t.tzinfo.utcoffset(t) is None:
        t = t.replace(tzinfo=pytz.timezone('utc'))

    t0 = datetime.datetime(1970, 1, 1, 0, 0, 0, 0, pytz.timezone('utc'))
    delta = t - t0
    return int(delta.total_seconds())


Instruction:
How would you code a function in Python 3 to
return the version of the docker container running the present server or an empty string if not in a container

Response:
def get_container_version():
    """Return the version of the docker container running the present server,
    or '' if not in a container"""
    root_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
    version_file = os.path.join(root_dir, 'VERSION')
    if os.path.exists(version_file):
        with open(version_file) as f:
            return f.read()
    return ''


Instruction:
How would you explain what the following Python 3 function does
def get_app_name():
    conf = get_config()
    if is_ec2_instance():
        return conf.app_name_live if hasattr(conf, 'app_name_live') else 'PYMACARON_LIVE'
    else:
        return conf.app_name_dev if hasattr(conf, 'app_name_dev') else 'PYMACARON_DEV'

Response: Return a generic name for this app usefull when reporting to monitoring frameworks


Instruction:
Can you write a function in Python 3 where it
creates a new instance of this gene class with random DNA and random characters chosen from the GeneMaterIAL_OPTIONS list.

Response:
def create_random(cls, length, **kwargs):
        """
        Return a new instance of this gene class with random DNA,
        with characters chosen from ``GENETIC_MATERIAL_OPTIONS``.

        length:  the number of characters in the randomized DNA
        **kwargs:  forwarded to the ``cls`` constructor
        """
        dna = ''.join([random.choice(cls.GENETIC_MATERIAL_OPTIONS) for _ in range(length)])
        return cls(dna, **kwargs)


Instruction:
How would you code a function in Python 3 to
simulate mutation against a probability.

Response:
def mutate(self, p_mutate):
        """
        Simulate mutation against a probability.

        p_mutate:  probability for mutation to occur
        """
        new_dna = []

        for bit in self.dna:
            if random.random() < p_mutate:
                new_bit = bit

                while new_bit == bit:
                    new_bit = random.choice(self.GENETIC_MATERIAL_OPTIONS)
                bit = new_bit

            new_dna.append(bit)

        self.dna = ''.join(new_dna)


Instruction:
How would you explain what the following Python 3 function does
def copy(self):
        return type(self)(self.dna, suppressed=self.suppressed, name=self.name)

Response: Return a copy of this gene with the same DNA.


Instruction:
Can you write a function in Python 3 where it
checks that a DNA string only contains characters in self. GENETIC_MATERIAL_OPTIONS.

Response:
def _check_dna(self, dna):
        """ Check that a DNA string only contains characters in ``GENETIC_MATERIAL_OPTIONS``. """
        valid_chars = set(self.GENETIC_MATERIAL_OPTIONS)
        assert all(char in valid_chars for char in dna)


Instruction:
Explain what the following Python 3 code does
def mutate(self, p_mutate):
        new_dna = []

        for bit in self.dna:
            if random.random() < p_mutate:
                bit = '1' if bit == '0' else '0'

            new_dna.append(bit)

        self.dna = ''.join(new_dna)

Response: Mutate the DNA string.


Instruction:
Here you have a function in Python 3, explain what it does
def make_client(api_version, session=None,
                endpoint=None, service_type='monitoring'):
    """Returns an monitoring API client."""

    client_cls = utils.get_client_class('monitoring', api_version, VERSION_MAP)
    c = client_cls(
        session=session,
        service_type=service_type,
        endpoint=endpoint,
        app_name='monascaclient',
        app_version=version.version_string,
    )

    return c

Response: Returns an monitoring API client.


Instruction:
Write a Python 3 script for
returning or reuses session.

Response:
def _session(kwargs):
    """Returns or reuses session.

    Method takes care of providing instance of
    session object for the client.

    :param kwargs: all params (without api_version) client was initialized with
    :type kwargs: dict

    :returns: session object
    :rtype keystoneauth1.session.Session

    """
    if 'session' in kwargs:
        LOG.debug('Reusing session')
        sess = kwargs.get('session')
        if not isinstance(sess, k_session.Session):
            msg = ('session should be an instance of %s' % k_session.Session)
            LOG.error(msg)
            raise RuntimeError(msg)
    else:
        LOG.debug('Initializing new session')
        auth = _get_auth_handler(kwargs)
        sess = _get_session(auth, kwargs)
    return sess


Instruction:
Implement a function in Python 3 to
load an instance of the class from a file.

Response:
def load(cls, path):
        """
        Loads an instance of the class from a file.

        Parameters
        ----------
        path : str
            Path to an HDF5 file.

        Examples
        --------
        This is an abstract data type, but let us say that ``Foo`` inherits
        from ``Saveable``. To construct an object of this class from a file, we
        do:

        >>> foo = Foo.load('foo.h5') #doctest: +SKIP
        """
        if path is None:
            return cls.load_from_dict({})
        else:
            d = io.load(path)
            return cls.load_from_dict(d)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def register(cls, name):
        def register_decorator(reg_cls):
            def name_func(self):
                return name
            reg_cls.name = property(name_func)
            assert issubclass(reg_cls, cls), \
                "Must be subclass matching your NamedRegistry class"
            cls.REGISTRY[name] = reg_cls
            return reg_cls
        return register_decorator

Response: Decorator to register a class.


Instruction:
How would you implement a function in Python 3 that
constructs an instance of the named object given its name.

Response:
def construct(cls, name, *args, **kwargs):
        """
        Constructs an instance of an object given its name.
        """
        return cls.REGISTRY[name](*args, **kwargs)


Instruction:
Given the following Python 3 function, write the documentation
def config():
    conf = ConfigParser()
    # Set up defaults
    conf.add_section('io')
    conf.set('io', 'compression', 'zlib')

    conf.read(os.path.expanduser('~/.deepdish.conf'))
    return conf

Response: Loads and returns a ConfigParser from ~. deepdish. conf.


Instruction:
Explain what the following Python 3 code does
def asgray(im):
    if im.ndim == 2:
        return im
    elif im.ndim == 3 and im.shape[2] in (3, 4):
        return im[..., :3].mean(axis=-1)
    else:
        raise ValueError('Invalid image format')

Response:
Takes an image and returns its grayscale version by averaging the color
    channels.


Instruction:
Implement a Python 3 function for
cropping an image in the center.

Response:
def crop(im, size):
    """
    Crops an image in the center.

    Parameters
    ----------
    size : tuple, (height, width)
        Finally size after cropping.
    """
    diff = [im.shape[index] - size[index] for index in (0, 1)]
    im2 = im[diff[0]//2:diff[0]//2 + size[0], diff[1]//2:diff[1]//2 + size[1]]
    return im2


Instruction:
Here you have a function in Python 3, explain what it does
def crop_or_pad(im, size, value=0):
    diff = [im.shape[index] - size[index] for index in (0, 1)]
    im2 = im[diff[0]//2:diff[0]//2 + size[0], diff[1]//2:diff[1]//2 + size[1]]
    return im2

Response: Crop an image or pad it.


Instruction:
Write a Python 3 script for
loading an image from file.

Response:
def load(path, dtype=np.float64):
    """
    Loads an image from file.

    Parameters
    ----------
    path : str
        Path to image file.
    dtype : np.dtype
        Defaults to ``np.float64``, which means the image will be returned as a
        float with values between 0 and 1. If ``np.uint8`` is specified, the
        values will be between 0 and 255 and no conversion cost will be
        incurred.
    """
    _import_skimage()
    import skimage.io
    im = skimage.io.imread(path)
    if dtype == np.uint8:
        return im
    elif dtype in {np.float16, np.float32, np.float64}:
        return im.astype(dtype) / 255
    else:
        raise ValueError('Unsupported dtype')


Instruction:
Write a Python 3 script to
load image using PIL. Image. open with PIL. Pillow without any processing.

Response:
def load_raw(path):
    """
    Load image using PIL/Pillow without any processing. This is particularly
    useful for palette images, which will be loaded using their palette index
    values as opposed to `load` which will convert them to RGB.

    Parameters
    ----------
    path : str
        Path to image file.
    """
    _import_pil()
    from PIL import Image
    return np.array(Image.open(path))


Instruction:
Can you write a function in Python 3 where it
saves an image to file.

Response:
def save(path, im):
    """
    Saves an image to file.

    If the image is type float, it will assume to have values in [0, 1].

    Parameters
    ----------
    path : str
        Path to which the image will be saved.
    im : ndarray (image)
        Image.
    """
    from PIL import Image
    if im.dtype == np.uint8:
        pil_im = Image.fromarray(im)
    else:
        pil_im = Image.fromarray((im*255).astype(np.uint8))
    pil_im.save(path)


Instruction:
Can you tell what is the following Python 3 function doing
def integrate(ii, r0, c0, r1, c1):
    # This line is modified
    S = np.zeros(ii.shape[-1])

    S += ii[r1, c1]

    if (r0 - 1 >= 0) and (c0 - 1 >= 0):
        S += ii[r0 - 1, c0 - 1]

    if (r0 - 1 >= 0):
        S -= ii[r0 - 1, c1]

    if (c0 - 1 >= 0):
        S -= ii[r1, c0 - 1]

    return S

Response: Integrate the image of a given window over a given top left corner of the block and the bottom right corner of the block.


Instruction:
Create a Python 3 function to
move the contents of an image with the specified offset along the two axes.

Response:
def offset(img, offset, fill_value=0):
    """
    Moves the contents of image without changing the image size. The missing
    values are given a specified fill value.

    Parameters
    ----------
    img : array
        Image.
    offset : (vertical_offset, horizontal_offset)
        Tuple of length 2, specifying the offset along the two axes.
    fill_value : dtype of img
        Fill value. Defaults to 0.
    """

    sh = img.shape
    if sh == (0, 0):
        return img
    else:
        x = np.empty(sh)
        x[:] = fill_value
        x[max(offset[0], 0):min(sh[0]+offset[0], sh[0]),
          max(offset[1], 0):min(sh[1]+offset[1], sh[1])] = \
            img[max(-offset[0], 0):min(sh[0]-offset[0], sh[0]),
                max(-offset[1], 0):min(sh[1]-offset[1], sh[1])]
        return x


Instruction:
Create a Python 3 function to
return a bounding box describing the smallest rectangle containing the foreground object.

Response:
def bounding_box(alpha, threshold=0.1):
    """
    Returns a bounding box of the support.

    Parameters
    ----------
    alpha : ndarray, ndim=2
        Any one-channel image where the background has zero or low intensity.
    threshold : float
        The threshold that divides background from foreground.

    Returns
    -------
    bounding_box : (top, left, bottom, right)
        The bounding box describing the smallest rectangle containing the
        foreground object, as defined by the threshold.
    """
    assert alpha.ndim == 2

    # Take the bounding box of the support, with a certain threshold.
    supp_axs = [alpha.max(axis=1-i) for i in range(2)]

    # Check first and last value of that threshold
    bb = [np.where(supp_axs[i] > threshold)[0][[0, -1]] for i in range(2)]

    return (bb[0][0], bb[1][0], bb[0][1], bb[1][1])


Instruction:
Here you have a function in Python 3, explain what it does
def bounding_box_as_binary_map(alpha, threshold=0.1):

    bb = bounding_box(alpha)
    x = np.zeros(alpha.shape, dtype=np.bool_)
    x[bb[0]:bb[2], bb[1]:bb[3]] = 1
    return x

Response: Returns the bounding box as a binary map.


Instruction:
Can you tell what is the following Python 3 function doing
def extract_patches(images, patch_shape, samples_per_image=40, seed=0,
                    cycle=True):
    """
    Takes a set of images and yields randomly chosen patches of specified size.

    Parameters
    ----------
    images : iterable
        The images have to be iterable, and each element must be a Numpy array
        with at least two spatial 2 dimensions as the first and second axis.
    patch_shape : tuple, length 2
        The spatial shape of the patches that should be extracted. If the
        images have further dimensions beyond the spatial, the patches will
        copy these too.
    samples_per_image : int
        Samples to extract before moving on to the next image.
    seed : int
        Seed with which to select the patches.
    cycle : bool
        If True, then the function will produce patches indefinitely, by going
        back to the first image when all are done. If False, the iteration will
        stop when there are no more images.

    Returns
    -------
    patch_generator
        This function returns a generator that will produce patches.

    Examples
    --------
    >>> import deepdish as dd
    >>> import matplotlib.pylab as plt
    >>> import itertools
    >>> images = ag.io.load_example('mnist')

    Now, let us say we want to exact patches from the these, where each patch
    has at least some activity.

    >>> gen = dd.image.extract_patches(images, (5, 5))
    >>> gen = (x for x in gen if x.mean() > 0.1)
    >>> patches = np.array(list(itertools.islice(gen, 25)))
    >>> patches.shape
    (25, 5, 5)
    >>> dd.plot.images(patches)
    >>> plt.show()

    """
    rs = np.random.RandomState(seed)
    for Xi in itr.cycle(images):
        # How many patches could we extract?
        w, h = [Xi.shape[i]-patch_shape[i] for i in range(2)]

        assert w > 0 and h > 0

        # Maybe shuffle an iterator of the indices?
        indices = np.asarray(list(itr.product(range(w), range(h))))
        rs.shuffle(indices)
        for x, y in indices[:samples_per_image]:
            yield Xi[x:x+patch_shape[0], y:y+patch_shape[1]]

Response: Takes a set of images and yields randomly chosen patches of specified size.


Instruction:
Write a Python 3 function for
returning the memory byte size of a Numpy array as an integer.

Response:
def bytesize(arr):
    """
    Returns the memory byte size of a Numpy array as an integer.
    """
    byte_size = np.prod(arr.shape) * np.dtype(arr.dtype).itemsize
    return byte_size


Instruction:
Given the following Python 3 function, write the documentation
def tupled_argmax(a):
    return np.unravel_index(np.argmax(a), np.shape(a))

Response:
Argmax that returns an index tuple. Note that numpy. argmax will return a
    scalar index as if you had flattened the array.


Instruction:
Can you implement a function in Python 3 that
pads an array with a specific value.

Response:
def pad(data, padwidth, value=0.0):
    """
    Pad an array with a specific value.

    Parameters
    ----------
    data : ndarray
        Numpy array of any dimension and type.
    padwidth : int or tuple
        If int, it will pad using this amount at the beginning and end of all
        dimensions. If it is a tuple (of same length as `ndim`), then the
        padding amount will be specified per axis.
    value : data.dtype
        The value with which to pad. Default is ``0.0``.

    See also
    --------
    pad_to_size, pad_repeat_border, pad_repeat_border_corner

    Examples
    --------
    >>> import deepdish as dd
    >>> import numpy as np

    Pad an array with zeros.

    >>> x = np.ones((3, 3))
    >>> dd.util.pad(x, (1, 2), value=0.0)
    array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  1.,  1.,  0.,  0.],
           [ 0.,  0.,  1.,  1.,  1.,  0.,  0.],
           [ 0.,  0.,  1.,  1.,  1.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.,  0.]])

    """
    data = np.asarray(data)
    shape = data.shape
    if isinstance(padwidth, int):
        padwidth = (padwidth,)*len(shape)

    padded_shape = tuple(map(lambda ix: ix[1]+padwidth[ix[0]]*2,
                             enumerate(shape)))
    new_data = np.empty(padded_shape, dtype=data.dtype)
    new_data[..., :] = value
    new_data[[slice(w, -w) if w > 0 else slice(None) for w in padwidth]] = data
    return new_data


Instruction:
Can you tell what is the following Python 3 function doing
def pad_repeat_border_corner(data, shape):
    new_data = np.empty(shape)
    new_data[[slice(upper) for upper in data.shape]] = data
    for i in range(len(shape)):
        selection = [slice(None)]*i + [slice(data.shape[i], None)]
        selection2 = [slice(None)]*i + [slice(data.shape[i]-1, data.shape[i])]
        new_data[selection] = new_data[selection2]
    return new_data

Response: Returns a new array with padded data by repeating the borders of each axis.


Instruction:
Can you implement a function in Python 3 that
checks if a dictionary can be saved natively as HDF5 groups.

Response:
def _dict_native_ok(d):
    """
    This checks if a dictionary can be saved natively as HDF5 groups.

    If it can't, it will be pickled.
    """
    if len(d) >= 256:
        return False

    # All keys must be strings
    for k in d:
        if not isinstance(k, six.string_types):
            return False

    return True


Instruction:
How would you explain what the following Python 3 function does
def _load_nonlink_level(handler, level, pathtable, pathname):
    if isinstance(level, tables.Group):
        if _sns and (level._v_title.startswith('SimpleNamespace:') or
                     DEEPDISH_IO_ROOT_IS_SNS in level._v_attrs):
            val = SimpleNamespace()
            dct = val.__dict__
        elif level._v_title.startswith('list:'):
            dct = {}
            val = []
        else:
            dct = {}
            val = dct
        # in case of recursion, object needs to be put in pathtable
        # before trying to fully load it
        pathtable[pathname] = val

        # Load sub-groups
        for grp in level:
            lev = _load_level(handler, grp, pathtable)
            n = grp._v_name
            # Check if it's a complicated pair or a string-value pair
            if n.startswith('__pair'):
                dct[lev['key']] = lev['value']
            else:
                dct[n] = lev

        # Load attributes
        for name in level._v_attrs._f_list():
            if name.startswith(DEEPDISH_IO_PREFIX):
                continue
            v = level._v_attrs[name]
            dct[name] = v

        if level._v_title.startswith('list:'):
            N = int(level._v_title[len('list:'):])
            for i in range(N):
                val.append(dct['i{}'.format(i)])
            return val
        elif level._v_title.startswith('tuple:'):
            N = int(level._v_title[len('tuple:'):])
            lst = []
            for i in range(N):
                lst.append(dct['i{}'.format(i)])
            return tuple(lst)
        elif level._v_title.startswith('nonetype:'):
            return None
        elif is_pandas_dataframe(level):
            assert _pandas, "pandas is required to read this file"
            store = _HDFStoreWithHandle(handler)
            return store.get(level._v_pathname)
        elif level._v_title.startswith('sparse:'):
            frm = level._v_attrs.format
            if frm in ('csr', 'csc', 'bsr'):
                shape = tuple(level.shape[:])
                cls = {'csr': sparse.csr_matrix,
                       'csc': sparse.csc_matrix,
                       'bsr': sparse.bsr_matrix}
                matrix = cls[frm](shape)
                matrix.data = level.data[:]
                matrix.indices = level.indices[:]
                matrix.indptr = level.indptr[:]
                matrix.maxprint = level._v_attrs.maxprint
                return matrix
            elif frm == 'dia':
                shape = tuple(level.shape[:])
                matrix = sparse.dia_matrix(shape)
                matrix.data = level.data[:]
                matrix.offsets = level.offsets[:]
                matrix.maxprint = level._v_attrs.maxprint
                return matrix
            elif frm == 'coo':
                shape = tuple(level.shape[:])
                matrix = sparse.coo_matrix(shape)
                matrix.data = level.data[:]
                matrix.col = level.col[:]
                matrix.row = level.row[:]
                matrix.maxprint = level._v_attrs.maxprint
                return matrix
            else:
                raise ValueError('Unknown sparse matrix type: {}'.format(frm))
        else:
            return val

    elif isinstance(level, tables.VLArray):
        if level.shape == (1,):
            return _load_pickled(level)
        else:
            return level[:]

    elif isinstance(level, tables.Array):
        if 'zeroarray_dtype' in level._v_attrs:
            # Unpack zero-size arrays (shape is stored in an HDF5 array and
            # type is stored in the attibute 'zeroarray_dtype')
            dtype = level._v_attrs.zeroarray_dtype
            sh = level[:]
            return np.zeros(tuple(sh), dtype=dtype)

        if 'strtype' in level._v_attrs:
            strtype = level._v_attrs.strtype
            itemsize = level._v_attrs.itemsize
            if strtype == b'unicode':
                return level[:].view(dtype=(np.unicode_, itemsize))
            elif strtype == b'ascii':
                return level[:].view(dtype=(np.string_, itemsize))
        # This serves two purposes:
        # (1) unpack big integers: the only time we save arrays like this
        # (2) unpack non-deepdish "scalars"
        if level.shape == ():
            return level[()]

        return level[:]

Response: Loads the non - link level and builds appropriate type without handling softlinks


Instruction:
Can you tell what is the following Python 3 function doing
def _load_level(handler, level, pathtable):
    if isinstance(level, tables.link.SoftLink):
        # this is a link, so see if target is already loaded, return it
        pathname = level.target
        node = level()
    else:
        # not a link, but it might be a target that's already been
        # loaded ... if so, return it
        pathname = level._v_pathname
        node = level
    try:
        return pathtable[pathname]
    except KeyError:
        pathtable[pathname] = _load_nonlink_level(handler, node, pathtable,
                                                  pathname)
        return pathtable[pathname]

Response: Loads the level and builds appropriate type if necessary


Instruction:
Can you create a Python 3 function that
saves a Python object to HDF5.

Response:
def save(path, data, compression='default'):
    """
    Save any Python structure to an HDF5 file. It is particularly suited for
    Numpy arrays. This function works similar to ``numpy.save``, except if you
    save a Python object at the top level, you do not need to issue
    ``data.flat[0]`` to retrieve it from inside a Numpy array of type
    ``object``.

    Some types of objects get saved natively in HDF5. The rest get serialized
    automatically.  For most needs, you should be able to stick to the natively
    supported types, which are:

    * Dictionaries
    * Short lists and tuples (<256 in length)
    * Basic data types (including strings and None)
    * Numpy arrays
    * Scipy sparse matrices
    * Pandas ``DataFrame``, ``Series``, and ``Panel``
    * SimpleNamespaces (for Python >= 3.3, but see note below)

    A recommendation is to always convert your data to using only these types
    That way your data will be portable and can be opened through any HDF5
    reader. A class that helps you with this is
    :class:`deepdish.util.Saveable`.

    Lists and tuples are supported and can contain heterogeneous types. This is
    mostly useful and plays well with HDF5 for short lists and tuples. If you
    have a long list (>256) it will be serialized automatically. However,
    in such cases it is common for the elements to have the same type, in which
    case we strongly recommend converting to a Numpy array first.

    Note that the SimpleNamespace type will be read in as dictionaries for
    earlier versions of Python.

    This function requires the `PyTables <http://www.pytables.org/>`_ module to
    be installed.

    You can change the default compression method to ``blosc`` (much faster,
    but less portable) by creating a ``~/.deepdish.conf`` with::

        [io]
            compression: blosc

    This is the recommended compression method if you plan to use your HDF5
    files exclusively through deepdish (or PyTables).

    Parameters
    ----------
    path : string
        Filename to which the data is saved.
    data : anything
        Data to be saved. This can be anything from a Numpy array, a string, an
        object, or a dictionary containing all of them including more
        dictionaries.
    compression : string or tuple
        Set compression method, choosing from `blosc`, `zlib`, `lzo`, `bzip2`
        and more (see PyTables documentation). It can also be specified as a
        tuple (e.g. ``('blosc', 5)``), with the latter value specifying the
        level of compression, choosing from 0 (no compression) to 9 (maximum
        compression).  Set to `None` to turn off compression. The default is
        `zlib`, since it is highly portable; for much greater speed, try for
        instance `blosc`.

    See also
    --------
    load
    """
    filters = _get_compression_filters(compression)

    with tables.open_file(path, mode='w') as h5file:
        # If the data is a dictionary, put it flatly in the root
        group = h5file.root
        group._v_attrs[DEEPDISH_IO_VERSION_STR] = IO_VERSION
        idtable = {}  # dict to keep track of objects already saved
        # Sparse matrices match isinstance(data, dict), so we'll have to be
        # more strict with the type checking
        if type(data) == type({}) and _dict_native_ok(data):
            idtable[id(data)] = '/'
            for key, value in data.items():
                _save_level(h5file, group, value, name=key,
                            filters=filters, idtable=idtable)

        elif (_sns and isinstance(data, SimpleNamespace) and
              _dict_native_ok(data.__dict__)):
            idtable[id(data)] = '/'
            group._v_attrs[DEEPDISH_IO_ROOT_IS_SNS] = True
            for key, value in data.__dict__.items():
                _save_level(h5file, group, value, name=key,
                            filters=filters, idtable=idtable)

        else:
            _save_level(h5file, group, data, name='data',
                        filters=filters, idtable=idtable)
            # Mark this to automatically unpack when loaded
            group._v_attrs[DEEPDISH_IO_UNPACK] = True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load(path, group=None, sel=None, unpack=False):
    with tables.open_file(path, mode='r') as h5file:
        pathtable = {}  # dict to keep track of objects already loaded
        if group is not None:
            if isinstance(group, str):
                data = _load_specific_level(h5file, h5file, group, sel=sel,
                                            pathtable=pathtable)
            else:  # Assume group is a list or tuple
                data = []
                for g in group:
                    data_i = _load_specific_level(h5file, h5file, g, sel=sel,
                                                  pathtable=pathtable)
                    data.append(data_i)
                data = tuple(data)
        else:
            grp = h5file.root
            auto_unpack = (DEEPDISH_IO_UNPACK in grp._v_attrs and
                           grp._v_attrs[DEEPDISH_IO_UNPACK])
            do_unpack = unpack or auto_unpack
            if do_unpack and len(grp._v_children) == 1:
                name = next(iter(grp._v_children))
                data = _load_specific_level(h5file, grp, name, sel=sel,
                                            pathtable=pathtable)
                do_unpack = False
            elif sel is not None:
                raise ValueError("Must specify group with `sel` unless it "
                                 "automatically unpacks")
            else:
                data = _load_level(h5file, grp, pathtable)

            if DEEPDISH_IO_VERSION_STR in grp._v_attrs:
                v = grp._v_attrs[DEEPDISH_IO_VERSION_STR]
            else:
                v = 0

            if v > IO_VERSION:
                warnings.warn('This file was saved with a newer version of '
                              'deepdish. Please upgrade to make sure it loads '
                              'correctly.')

            # Attributes can't be unpacked with the method above, so fall back
            # to this
            if do_unpack and isinstance(data, dict) and len(data) == 1:
                data = next(iter(data.values()))

    return data

Response: Loads an HDF5 file and returns a dict of the data that was saved with save.


Instruction:
Write a Python 3 script to
sort x with numeric semantics.

Response:
def sorted_maybe_numeric(x):
    """
    Sorts x with numeric semantics if all keys are nonnegative integers.
    Otherwise uses standard string sorting.
    """
    all_numeric = all(map(str.isdigit, x))
    if all_numeric:
        return sorted(x, key=int)
    else:
        return sorted(x)


Instruction:
How would you code a function in Python 3 to
color - aware abbreviator

Response:
def abbreviate(s, maxlength=25):
    """Color-aware abbreviator"""
    assert maxlength >= 4
    skip = False
    abbrv = None
    i = 0
    for j, c in enumerate(s):
        if c == '\033':
            skip = True
        elif skip:
            if c == 'm':
                skip = False
        else:
            i += 1

        if i == maxlength - 1:
            abbrv = s[:j] + '\033[0m...'
        elif i > maxlength:
            break

    if i <= maxlength:
        return s
    else:
        return abbrv


Instruction:
Given the following Python 3 function, write the documentation
def extend_settings(self, data_id, files, secrets):
        data = Data.objects.select_related('process').get(pk=data_id)

        files[ExecutorFiles.DJANGO_SETTINGS].update({
            'USE_TZ': settings.USE_TZ,
            'FLOW_EXECUTOR_TOOLS_PATHS': self.get_tools_paths(),
        })
        files[ExecutorFiles.DATA] = model_to_dict(data)
        files[ExecutorFiles.DATA_LOCATION] = model_to_dict(data.location)
        files[ExecutorFiles.PROCESS] = model_to_dict(data.process)
        files[ExecutorFiles.PROCESS]['resource_limits'] = data.process.get_resource_limits()

        # Add secrets if the process has permission to read them.
        secrets.update(data.resolve_secrets())

Response: Extend the settings dictionary with the contents of the object.


Instruction:
How would you code a function in Python 3 to
resolve the data path for use with the executor.

Response:
def resolve_data_path(self, data=None, filename=None):
        """Resolve data path for use with the executor.

        :param data: Data object instance
        :param filename: Filename to resolve
        :return: Resolved filename, which can be used to access the
            given data file in programs executed using this executor
        """
        if data is None:
            return settings.FLOW_EXECUTOR['DATA_DIR']

        return data.location.get_path(filename=filename)


Instruction:
Can you write a function in Python 3 where it
resolves the upload path for use with the executor.

Response:
def resolve_upload_path(self, filename=None):
        """Resolve upload path for use with the executor.

        :param filename: Filename to resolve
        :return: Resolved filename, which can be used to access the
            given uploaded file in programs executed using this
            executor
        """
        if filename is None:
            return settings.FLOW_EXECUTOR['UPLOAD_DIR']

        return os.path.join(settings.FLOW_EXECUTOR['UPLOAD_DIR'], filename)


Instruction:
Explain what the following Python 3 code does
def wrap(access_pyxb, read_only=False):
    w = AccessPolicyWrapper(access_pyxb)
    yield w
    if not read_only:
        w.get_normalized_pyxb()

Response: A context manager that wraps an AccessPolicy PyXB object.


Instruction:
Create a Python 3 function for
working with the AccessPolicy in a SystemMetadata PyXB object. Args: sysmeta_pyxb : SystemMetadata PyXB object SystemMetadata containing the AccessPolicy to modify. read_only: bool Do not update the wrapped AccessPolicy. When only a single AccessPolicy operation is needed, there's no need to use this context manager. Instead, use the generated context manager wrappers. There is no clean way in Python to make a context manager that allows client code to replace the object that is passed out of the manager. The AccessPolicy schema does not allow the AccessPolicy element to be empty. However, the SystemMetadata schema specifies the AccessPolicy as optional. By wrapping the SystemMetadata instead of the AccessPolicy when working with AccessPolicy that is within SystemMetadata, the wrapper can handle the situation of empty AccessPolicy by instead dropping the AccessPolicy from the SystemMetadata.

Response:
def wrap_sysmeta_pyxb(sysmeta_pyxb, read_only=False):
    """Work with the AccessPolicy in a SystemMetadata PyXB object.

    Args:
      sysmeta_pyxb : SystemMetadata PyXB object
        SystemMetadata containing the AccessPolicy to modify.

      read_only: bool
        Do not update the wrapped AccessPolicy.

    When only a single AccessPolicy operation is needed, there's no need to use
    this context manager. Instead, use the generated context manager wrappers.

    There is no clean way in Python to make a context manager that allows client code to
    replace the object that is passed out of the manager. The AccessPolicy schema does not
    allow the AccessPolicy element to be empty. However, the SystemMetadata schema
    specifies the AccessPolicy as optional. By wrapping the SystemMetadata instead of the
    AccessPolicy when working with AccessPolicy that is within SystemMetadata, the wrapper
    can handle the situation of empty AccessPolicy by instead dropping the AccessPolicy
    from the SystemMetadata.

    """
    w = AccessPolicyWrapper(sysmeta_pyxb.accessPolicy)
    yield w
    if not read_only:
        sysmeta_pyxb.accessPolicy = w.get_normalized_pyxb()


Instruction:
Write a Python 3 script to
return the highest permission for a subject.

Response:
def get_highest_perm_str(self, subj_str):
        """
    Args:
      subj_str : str
        Subject for which to retrieve the highest permission.

    Return:
       The highest permission for subject or None if subject does not have any permissions.
    """
        pres_perm_set = self._present_perm_set_for_subj(self._perm_dict, subj_str)
        return (
            None if not pres_perm_set else self._highest_perm_from_iter(pres_perm_set)
        )


Instruction:
Can you write a function in Python 3 where it
returns the list of effective permissions for the subject.

Response:
def get_effective_perm_list(self, subj_str):
        """
    Args:
      subj_str : str
        Subject for which to retrieve the effective permissions.

    Returns:
      list of str: List of permissions up to and including the highest permission for
      subject, ordered lower to higher, or empty list if subject does not have any
      permissions.

      E.g.: If 'write' is highest permission for subject, return ['read', 'write'].
    """
        highest_perm_str = self.get_highest_perm_str(subj_str)
        if highest_perm_str is None:
            return []
        return self._equal_or_lower_perm_list(highest_perm_str)


Instruction:
Implement a Python 3 function for
returning a set of subjects with equal or higher permission.

Response:
def get_subjects_with_equal_or_higher_perm(self, perm_str):
        """
    Args:
      perm_str : str
        Permission, ``read``, ``write`` or ``changePermission``.

    Returns:
      set of str : Subj that have perm equal or higher than ``perm_str``.

      Since the lowest permission a subject can have is ``read``, passing ``read``
      will return all subjects.
    """
        self._assert_valid_permission(perm_str)
        return {
            s
            for p in self._equal_or_higher_perm(perm_str)
            for s in self._perm_dict.get(p, set())
        }


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dump(self):
        logging.debug('AccessPolicy:')
        map(
            logging.debug,
            [
                '  {}'.format(s)
                for s in pprint.pformat(self.get_normalized_perm_list()).splitlines()
            ],
        )

Response: Dump the current state to debug level log.


Instruction:
Create a Python 3 function to
return True if subj_str has perm equal to or higher than perm_str.

Response:
def subj_has_perm(self, subj_str, perm_str):
        """Returns:

        bool: ``True`` if ``subj_str`` has perm equal to or higher than ``perm_str``.

        """
        self._assert_valid_permission(perm_str)
        return perm_str in self.get_effective_perm_list(subj_str)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_authenticated_read(self):
        self.remove_perm(d1_common.const.SUBJECT_PUBLIC, 'read')
        self.add_perm(d1_common.const.SUBJECT_AUTHENTICATED, 'read')

Response: Add read perm for all authenticated subj.


Instruction:
Write a Python 3 script for
adding verified read perm for all public subj.

Response:
def add_verified_read(self):
        """Add ``read`` perm for all verified subj.

        Public ``read`` is removed if present.

        """
        self.remove_perm(d1_common.const.SUBJECT_PUBLIC, 'read')
        self.add_perm(d1_common.const.SUBJECT_VERIFIED, 'read')


Instruction:
Write a Python 3 function for
adding a permission for a subject.

Response:
def add_perm(self, subj_str, perm_str):
        """Add a permission for a subject.

        Args:
          subj_str : str
            Subject for which to add permission(s)

          perm_str : str
            Permission to add. Implicitly adds all lower permissions. E.g., ``write``
            will also add ``read``.

        """
        self._assert_valid_permission(perm_str)
        self._perm_dict.setdefault(perm_str, set()).add(subj_str)


Instruction:
Can you implement a function in Python 3 that
removes a permission from a subject.

Response:
def remove_perm(self, subj_str, perm_str):
        """Remove permission from a subject.

        Args:
          subj_str : str
            Subject for which to remove permission(s)

          perm_str : str
            Permission to remove. Implicitly removes all higher permissions. E.g., ``write``
            will also remove ``changePermission`` if previously granted.

        """
        self._assert_valid_permission(perm_str)
        for perm_str in self._equal_or_higher_perm(perm_str):
            self._perm_dict.setdefault(perm_str, set()).discard(subj_str)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_subj(self, subj_str):
        for subj_set in list(self._perm_dict.values()):
            subj_set -= {subj_str}

Response: Removes all permissions for a subject.


Instruction:
Given the following Python 3 function, write the documentation
def _perm_dict_from_pyxb(self, access_pyxb):
        subj_dict = self._subj_dict_from_pyxb(access_pyxb)
        return self._perm_dict_from_subj_dict(subj_dict)

Response: Return dict representation of AccessPolicy PyXB obj.


Instruction:
Given the following Python 3 function, write the documentation
def _perm_dict_from_subj_dict(self, subj_dict):
        perm_dict = {}
        for subj_str, perm_set in list(subj_dict.items()):
            for perm_str in perm_set:
                perm_dict.setdefault(perm_str, set()).add(subj_str)
        return perm_dict

Response:
Return dict where keys and values of subj_dict have been flipped
        around.


Instruction:
Write a Python 3 function that can
return an AccessPolicy PyXB representation of perm_dict.

Response:
def _pyxb_from_perm_dict(self, perm_dict):
        """Return an AccessPolicy PyXB representation of ``perm_dict``

        - If ``norm_perm_list`` is empty, None is returned. The schema does not allow
        AccessPolicy to be empty, but in SystemMetadata, it can be left out
        altogether. So returning None instead of an empty AccessPolicy allows the
        result to be inserted directly into a SystemMetadata PyXB object.

        """
        norm_perm_list = self._norm_perm_list_from_perm_dict(perm_dict)
        return self._pyxb_from_norm_perm_list(norm_perm_list)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _pyxb_from_norm_perm_list(self, norm_perm_list):
        # Using accessPolicy() instead of AccessPolicy() and accessRule() instead of
        # AccessRule() gives PyXB the type information required for using this as a
        # root element.
        access_pyxb = d1_common.types.dataoneTypes.accessPolicy()
        for perm_str, subj_list in norm_perm_list:
            rule_pyxb = d1_common.types.dataoneTypes.accessRule()
            rule_pyxb.permission.append(perm_str)
            for subj_str in subj_list:
                rule_pyxb.subject.append(subj_str)
            access_pyxb.allow.append(rule_pyxb)
        if len(access_pyxb.allow):
            return access_pyxb

Response: Return an AccessPolicy PyXB representation of the norm_perm_list.


Instruction:
Implement a function in Python 3 to
return a dict representation of an AccessPolicyPyXB object.

Response:
def _subj_dict_from_pyxb(self, access_pyxb):
        """Return a dict representation of ``access_pyxb``, which is an AccessPolicy
        PyXB object.

        This also remove any duplicate subjects and permissions in the PyXB object.

        """
        subj_dict = {}
        for allow_pyxb in access_pyxb.allow:
            perm_set = set()
            for perm_pyxb in allow_pyxb.permission:
                perm_set.add(perm_pyxb)
            for subj_pyxb in allow_pyxb.subject:
                subj_dict.setdefault(subj_pyxb.value(), set()).update(perm_set)
        return subj_dict


Instruction:
Can you tell what is the following Python 3 function doing
def _highest_perm_dict_from_perm_dict(self, perm_dict):
        highest_perm_dict = copy.copy(perm_dict)
        for ordered_str in reversed(ORDERED_PERM_LIST):
            for lower_perm in self._lower_perm_list(ordered_str):
                highest_perm_dict.setdefault(lower_perm, set())
                highest_perm_dict[lower_perm] -= perm_dict.get(ordered_str, set())
        return highest_perm_dict

Response:
Return a perm_dict where only the highest permission for each subject is
        included.


Instruction:
Make a summary of the following Python 3 code
def _norm_perm_list_from_perm_dict(self, perm_dict):
        high_perm_dict = self._highest_perm_dict_from_perm_dict(perm_dict)
        return [
            [k, list(sorted(high_perm_dict[k]))]
            for k in ORDERED_PERM_LIST
            if high_perm_dict.get(k, False)
        ]

Response: Return a minimal ordered hashable list of subjects and permissions.


Instruction:
How would you explain what the following Python 3 function does
def _effective_perm_list_from_iter(self, perm_iter):
        highest_perm_str = self._highest_perm_from_iter(perm_iter)
        return (
            self._equal_or_lower_perm_list(highest_perm_str)
            if highest_perm_str is not None
            else None
        )

Response:
Return list of effective permissions for highest permission in
 perm_iter ordered lower to higher or None.


Instruction:
Can you generate the documentation for the following Python 3 function
def _present_perm_set_for_subj(self, perm_dict, subj_str):
        return {p for p, s in list(perm_dict.items()) if subj_str in s}

Response:
Return a set containing only the permissions that are present in the the
        perm_dict for subj_str


Instruction:
Write a Python 3 script for
returning the highest perm present in perm_iter or None.

Response:
def _highest_perm_from_iter(self, perm_iter):
        """Return the highest perm present in ``perm_iter`` or None if ``perm_iter`` is
        empty."""
        perm_set = set(perm_iter)
        for perm_str in reversed(ORDERED_PERM_LIST):
            if perm_str in perm_set:
                return perm_str


Instruction:
Write a Python 3 function for
returning the ordered index of perm_str.

Response:
def _ordered_idx_from_perm(self, perm_str):
        """Return the ordered index of ``perm_str`` or None if ``perm_str`` is not a
        valid permission."""
        for i, ordered_str in enumerate(ORDERED_PERM_LIST):
            if perm_str == ordered_str:
                return i


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _assert_valid_permission(self, perm_str):
        if perm_str not in ORDERED_PERM_LIST:
            raise d1_common.types.exceptions.InvalidRequest(
                0,
                'Permission must be one of {}. perm_str="{}"'.format(
                    ', '.join(ORDERED_PERM_LIST), perm_str
                ),
            )

Response: Raise D1 exception if perm_str is not a valid permission.


Instruction:
Create a Python 3 function for
handling an unexpected exception.

Response:
def handle_unexpected_exception(max_traceback_levels=100):
    """Suppress stack traces for common errors and provide hints for how to resolve
    them."""
    exc_type, exc_msgs = sys.exc_info()[:2]
    if exc_type.__name__ == "SSLError":
        d1_cli.impl.util.print_error(
            """HTTPS / TLS / SSL / X.509v3 Certificate Error:
  An HTTPS connection could not be established. Verify that a DataONE node
  responds at the URL provided in the cn-url or mn-url session variable. If the
  URL is valid and if you intended to connect without authentication, make sure
  that the session variable, "anonymous", is set to True. If you intended to
  connect with authentication, make sure that the parameter, "cert-file", points
  to a valid certificate from CILogon. If the certificate has the private
  key in a separate file, also set "key-file" to the private key file.
  Otherwise, set "key-file" to None. Note that CILogon certificates must be
  renewed after 18 hours.
            """
        )
    elif exc_type.__name__ == "timeout":
        d1_cli.impl.util.print_error(
            """Timeout error:
  A connection to a DataONE node timed out. Verify that a DataONE node responds
  at the URL provided in the cn-url or mn-url session variable.
            """
        )
    else:
        _print_unexpected_exception(max_traceback_levels)


Instruction:
Explain what the following Python 3 code does
def create_sciobj(request, sysmeta_pyxb):
    pid = d1_common.xml.get_req_val(sysmeta_pyxb.identifier)

    set_mn_controlled_values(request, sysmeta_pyxb, is_modification=False)
    d1_gmn.app.views.assert_db.is_valid_pid_for_create(pid)
    d1_gmn.app.views.assert_sysmeta.sanity(request, sysmeta_pyxb)

    if _is_proxy_sciobj(request):
        sciobj_url = _get_sciobj_proxy_url(request)
        _sanity_check_proxy_url(sciobj_url)
    else:
        sciobj_url = d1_gmn.app.sciobj_store.get_rel_sciobj_file_url_by_pid(pid)

    if not _is_proxy_sciobj(request):
        if d1_gmn.app.resource_map.is_resource_map_sysmeta_pyxb(sysmeta_pyxb):
            _create_resource_map(pid, request, sysmeta_pyxb, sciobj_url)
        else:
            _save_sciobj_bytes_from_request(request, pid)
            d1_gmn.app.scimeta.assert_valid(sysmeta_pyxb, pid)

    d1_gmn.app.sysmeta.create_or_update(sysmeta_pyxb, sciobj_url)

    d1_gmn.app.event_log.create(
        d1_common.xml.get_req_val(sysmeta_pyxb.identifier),
        request,
        timestamp=d1_common.date_time.normalize_datetime_to_utc(
            sysmeta_pyxb.dateUploaded
        ),
    )

Response: Create a new native locally stored science object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _save_sciobj_bytes_from_request(request, pid):
    sciobj_path = d1_gmn.app.sciobj_store.get_abs_sciobj_file_path_by_pid(pid)
    if hasattr(request.FILES['object'], 'temporary_file_path'):
        d1_common.utils.filesystem.create_missing_directories_for_file(sciobj_path)
        django.core.files.move.file_move_safe(
            request.FILES['object'].temporary_file_path(), sciobj_path
        )
    else:
        with d1_gmn.app.sciobj_store.open_sciobj_file_by_path_ctx(
            sciobj_path, write=True
        ) as sciobj_stream:
            for chunk in request.FILES['object'].chunks():
                sciobj_stream.write(chunk)

Response: Save bytes from request. FILES.


Instruction:
Make a summary of the following Python 3 code
def set_mn_controlled_values(request, sysmeta_pyxb, is_modification):
    now_datetime = d1_common.date_time.utc_now()

    default_value_list = [
        ('originMemberNode', django.conf.settings.NODE_IDENTIFIER, True),
        ('authoritativeMemberNode', django.conf.settings.NODE_IDENTIFIER, True),
        ('serialVersion', 1, False),
        ('dateUploaded', now_datetime, False),
    ]

    if not is_modification:
        # submitter cannot be updated as the CN does not allow it.
        default_value_list.append(('submitter', request.primary_subject_str, True))
        # dateSysMetadataModified cannot be updated as it is used for optimistic
        # locking. If changed, it is assumed that optimistic locking failed, and the
        # update is rejected in order to prevent a concurrent update from being lost.
        default_value_list.append(('dateSysMetadataModified', now_datetime, False))
    else:
        sysmeta_pyxb.submitter = None
        sysmeta_pyxb.dateSysMetadataModified = now_datetime

    for attr_str, default_value, is_simple_content in default_value_list:
        is_trusted_from_client = getattr(
            django.conf.settings, 'TRUST_CLIENT_{}'.format(attr_str.upper()), False
        )
        override_value = None
        if is_trusted_from_client:
            override_value = (
                d1_common.xml.get_opt_val(sysmeta_pyxb, attr_str)
                if is_simple_content
                else getattr(sysmeta_pyxb, attr_str, None)
            )
        setattr(sysmeta_pyxb, attr_str, override_value or default_value)

Response: Set the sysmeta_pyxb attributes to the appropriate values for the MN_CONTROLLED_ATTRIBUTE_SET_ALL attributes.


Instruction:
Here you have a function in Python 3, explain what it does
def duplicate(self, contributor=None, inherit_collections=False):
        return [
            entity.duplicate(contributor, inherit_collections)
            for entity in self
        ]

Response: Duplicate ( make a copy ) Entity objects.


Instruction:
How would you explain what the following Python 3 function does
def move_to_collection(self, source_collection, destination_collection):
        for entity in self:
            entity.move_to_collection(source_collection, destination_collection)

Response: Move entities from source to destination collection.


Instruction:
How would you code a function in Python 3 to
duplicate (make a copy).

Response:
def duplicate(self, contributor=None, inherit_collections=False):
        """Duplicate (make a copy)."""
        duplicate = Entity.objects.get(id=self.id)
        duplicate.pk = None
        duplicate.slug = None
        duplicate.name = 'Copy of {}'.format(self.name)
        duplicate.duplicated = now()
        if contributor:
            duplicate.contributor = contributor

        duplicate.save(force_insert=True)

        assign_contributor_permissions(duplicate)

        # Override fields that are automatically set on create.
        duplicate.created = self.created
        duplicate.save()

        # Duplicate entity's data objects.
        data = get_objects_for_user(contributor, 'view_data', self.data.all())  # pylint: disable=no-member
        duplicated_data = data.duplicate(contributor)
        duplicate.data.add(*duplicated_data)

        if inherit_collections:
            collections = get_objects_for_user(
                contributor,
                'add_collection',
                self.collections.all()  # pylint: disable=no-member
            )
            for collection in collections:
                collection.entity_set.add(duplicate)
                copy_permissions(collection, duplicate)
                collection.data.add(*duplicated_data)
                for datum in duplicated_data:
                    copy_permissions(collection, datum)

        return duplicate


Instruction:
Given the following Python 3 function, write the documentation
def move_to_collection(self, source_collection, destination_collection):
        # Remove from collection.
        self.collections.remove(source_collection)  # pylint: disable=no-member
        source_collection.data.remove(*self.data.all())  # pylint: disable=no-member

        # Add to collection.
        self.collections.add(destination_collection)  # pylint: disable=no-member
        destination_collection.data.add(*self.data.all())

Response: Move entity from source to destination collection.


Instruction:
How would you explain what the following Python 3 function does
def quote(s, unsafe='/'):
    res = s.replace('%', '%25')
    for c in unsafe:
        res = res.replace(c, '%' + (hex(ord(c)).upper())[2:])
    return res

Response:
Pass in a dictionary that has unsafe characters as the keys and percent
    encoded value as the value.


Instruction:
Write a Python 3 script for
returning dependencies which should trigger updates of this model.

Response:
def get_dependencies(self):
        """Return dependencies, which should trigger updates of this model."""
        # pylint: disable=no-member
        return super().get_dependencies() + [
            Data.collection_set,
            Data.entity_set,
            Data.parents,
        ]


Instruction:
Here you have a function in Python 3, explain what it does
def alpha_s(scale, f, alphasMZ=0.1185, loop=3):
    if scale == MZ and f == 5:
        return alphasMZ  # nothing to do
    _sane(scale, f)
    crd = rundec.CRunDec()
    if f == 5:
        return_value = crd.AlphasExact(alphasMZ, MZ, scale, f, loop)
    elif f == 6:
        crd.nfMmu.Mth = 170
        crd.nfMmu.muth = 170
        crd.nfMmu.nf = 6
        return_value = crd.AlL2AlH(alphasMZ, MZ, crd.nfMmu, scale, loop)
    elif f == 4:
        crd.nfMmu.Mth = 4.8
        crd.nfMmu.muth = 4.8
        crd.nfMmu.nf = 5
        return_value = crd.AlH2AlL(alphasMZ, MZ, crd.nfMmu, scale, loop)
    elif f == 3:
        crd.nfMmu.Mth = 4.8
        crd.nfMmu.muth = 4.8
        crd.nfMmu.nf = 5
        mc = 1.3
        asmc =  crd.AlH2AlL(alphasMZ, MZ, crd.nfMmu, mc, loop)
        crd.nfMmu.Mth = mc
        crd.nfMmu.muth = mc
        crd.nfMmu.nf = 4
        return_value = crd.AlH2AlL(asmc, mc, crd.nfMmu, scale, loop)
    else:
        raise ValueError("Invalid input: f={}, scale={}".format(f, scale))
    if return_value == 0:
        raise ValueError("Return value is 0, probably `scale={}` is too small.".format(scale))
    else:
        return return_value

Response: 3 - loop computation of alpha_s for f flavours


Instruction:
Can you generate the documentation for the following Python 3 function
def m_b(mbmb, scale, f, alphasMZ=0.1185, loop=3):
    if scale == mbmb and f == 5:
        return mbmb  # nothing to do
    _sane(scale, f)
    alphas_mb = alpha_s(mbmb, 5, alphasMZ=alphasMZ, loop=loop)
    crd = rundec.CRunDec()
    if f == 5:
        alphas_scale = alpha_s(scale, f, alphasMZ=alphasMZ, loop=loop)
        return crd.mMS2mMS(mbmb, alphas_mb, alphas_scale, f, loop)
    elif f == 4:
        crd.nfMmu.Mth = 4.8
        crd.nfMmu.muth = 4.8
        crd.nfMmu.nf = 5
        return crd.mH2mL(mbmb, alphas_mb, mbmb, crd.nfMmu, scale, loop)
    elif f == 3:
        mc = 1.3
        crd.nfMmu.Mth = 4.8
        crd.nfMmu.muth = 4.8
        crd.nfMmu.nf = 5
        mbmc = crd.mH2mL(mbmb, alphas_mb, mbmb, crd.nfMmu, mc, loop)
        crd.nfMmu.Mth = mc
        crd.nfMmu.muth = mc
        crd.nfMmu.nf = 4
        alphas_mc = alpha_s(mc, 4, alphasMZ=alphasMZ, loop=loop)
        return crd.mH2mL(mbmc, alphas_mc, mc, crd.nfMmu, scale, loop)
    elif f == 6:
        crd.nfMmu.Mth = 170
        crd.nfMmu.muth = 170
        crd.nfMmu.nf = 6
        return crd.mL2mH(mbmb, alphas_mb, mbmb, crd.nfMmu, scale, loop)
    else:
        raise ValueError("Invalid input: f={}, scale={}".format(f, scale))

Response: r Return the running b quark mass in the MSbar scheme at the scale scale.