Instruction:
Here you have a function in Python 3, explain what it does
def retry_failure_internal(self, tenant_id, tenant_name, fw_data,
                               is_fw_virt, result):
        """Internal routine to retry the failed cases. """
        if not self.auto_nwk_create:
            LOG.info("Auto network creation disabled")
            return False
        try:
            # TODO(padkrish) More than 1 FW per tenant not supported
            if tenant_id not in self.service_attr:
                LOG.error("Tenant Obj not created")
                return False
            if result == fw_const.RESULT_FW_CREATE_INIT:
                # A check for is_fabric_create is not done here.
                ret = self.run_create_sm(tenant_id, fw_data, is_fw_virt)
            else:
                if result == fw_const.RESULT_FW_DELETE_INIT:
                    # A check for is_fabric_create is not done here.
                    # Pls check the comment given in function
                    # delete_fabric_fw_int
                    ret = self.run_delete_sm(tenant_id, fw_data, is_fw_virt)
                else:
                    LOG.error("Unknown state in retry")
                    return False
            self.service_attr[tenant_id].set_fabric_create(ret)
        except Exception as exc:
            LOG.error("Exception raised in create fabric int %s",
                      str(exc))
            return False
        return ret

Response: Internal routine to retry the failed cases.


Instruction:
Can you create a Python 3 function that
tops level retry failure routine.

Response:
def retry_failure(self, tenant_id, tenant_name, fw_data, is_fw_virt,
                      result):
        """Top level retry failure routine. """
        try:
            with self.mutex_lock:
                ret = self.retry_failure_internal(tenant_id, tenant_name,
                                                  fw_data, is_fw_virt, result)
        except Exception as exc:
            LOG.error("Exception raised in create fabric %s", str(exc))
            return False
        return ret


Instruction:
Explain what the following Python 3 code does
def get_specific_config(prefix):
    conf_dict = {}
    for uuid, val in cfg.CONF.get(prefix, {}).items():
        conf_dict[uuid] = dict(val)
    return conf_dict

Response: Retrieve config based on the format [ <prefix >:<value >.


Instruction:
Can you create a Python 3 function that
verifies that required attributes are in resource dictionary res_dict.

Response:
def verify_resource_dict(res_dict, is_create, attr_info):
    """Verifies required attributes are in resource dictionary, res_dict.

    Also checking that an attribute is only specified if it is allowed
    for the given operation (create/update).

    Attribute with default values are considered to be optional.

    This function contains code taken from function 'prepare_request_body' in
    attributes.py.
    """
    if ((bc.NEUTRON_VERSION >= bc.NEUTRON_NEWTON_VERSION) and 'tenant_id'
            in res_dict):
        res_dict['project_id'] = res_dict['tenant_id']
    if is_create:  # POST
        for attr, attr_vals in six.iteritems(attr_info):
            if attr_vals['allow_post']:
                if 'default' not in attr_vals and attr not in res_dict:
                    msg = _("Failed to parse request. Required attribute '%s' "
                            "not specified") % attr
                    raise webob.exc.HTTPBadRequest(msg)
                res_dict[attr] = res_dict.get(attr, attr_vals.get('default'))
            else:
                if attr in res_dict:
                    msg = _("Attribute '%s' not allowed in POST") % attr
                    raise webob.exc.HTTPBadRequest(msg)
    else:  # PUT
        for attr, attr_vals in six.iteritems(attr_info):
            if attr in res_dict and not attr_vals['allow_put']:
                msg = _("Cannot update read-only attribute %s") % attr
                raise webob.exc.HTTPBadRequest(msg)

    for attr, attr_vals in six.iteritems(attr_info):
        if (attr not in res_dict or
                res_dict[attr] is bc.constants.ATTR_NOT_SPECIFIED):
            continue
        # Convert values if necessary
        if 'convert_to' in attr_vals:
            res_dict[attr] = attr_vals['convert_to'](res_dict[attr])
        # Check that configured values are correct
        if 'validate' not in attr_vals:
            continue
        for rule in attr_vals['validate']:
            _ensure_format(rule, attr, res_dict)
            res = bc.validators[rule](res_dict[attr],
                                      attr_vals['validate'][rule])
            if res:
                msg_dict = dict(attr=attr, reason=res)
                msg = (_("Invalid input for %(attr)s. Reason: %(reason)s.") %
                       msg_dict)
                raise webob.exc.HTTPBadRequest(msg)
    return res_dict


Instruction:
How would you code a function in Python 3 to
take an integer and transforms it to a UUID format.

Response:
def uuidify(val):
    """Takes an integer and transforms it to a UUID format.

    returns: UUID formatted version of input.
    """
    if uuidutils.is_uuid_like(val):
        return val
    else:
        try:
            int_val = int(val, 16)
        except ValueError:
            with excutils.save_and_reraise_exception():
                LOG.error("Invalid UUID format %s. Please provide an "
                          "integer in decimal (0-9) or hex (0-9a-e) "
                          "format", val)
        res = str(int_val)
        num = 12 - len(res)
        return "00000000-0000-0000-0000-" + "0" * num + res


Instruction:
How would you explain what the following Python 3 function does
def _ensure_format(rule, attribute, res_dict):
    if rule == 'type:uuid' or (rule == 'type:uuid_or_none' and
                               res_dict[attribute]):
        res_dict[attribute] = uuidify(res_dict[attribute])
    elif rule == 'type:uuid_list':
        if not res_dict[attribute]:
            res_dict[attribute] = []
        else:
            temp_list = res_dict[attribute].split(':')
            res_dict[attribute] = []
            for item in temp_list:
                res_dict[attribute].append = uuidify(item)
    elif rule == 'type:string_or_none' and res_dict[attribute] == "":
        res_dict[attribute] = None

Response: Verifies that attribute in res_dict is properly formatted.


Instruction:
Here you have a function in Python 3, explain what it does
def obtain_hosting_device_credentials_from_config():
    cred_dict = get_specific_config('cisco_hosting_device_credential')
    attr_info = {
        'name': {'allow_post': True, 'allow_put': True,
                 'validate': {'type:string': None}, 'is_visible': True,
                 'default': ''},
        'description': {'allow_post': True, 'allow_put': True,
                        'validate': {'type:string': None},
                        'is_visible': True, 'default': ''},
        'user_name': {'allow_post': True, 'allow_put': True,
                      'validate': {'type:string': None},
                      'is_visible': True, 'default': ''},
        'password': {'allow_post': True, 'allow_put': True,
                     'validate': {'type:string': None},
                     'is_visible': True, 'default': ''},
        'type': {'allow_post': True, 'allow_put': True,
                 'validate': {'type:string': None}, 'is_visible': True,
                 'default': ''}}
    credentials = {}
    for cred_uuid, kv_dict in cred_dict.items():
        # ensure cred_uuid is properly formatted
        cred_uuid = uuidify(cred_uuid)
        verify_resource_dict(kv_dict, True, attr_info)
        credentials[cred_uuid] = kv_dict
    return credentials

Response: Gets credentials from config file and stores them in memory.


Instruction:
Create a Python 3 function to
list defined router types.

Response:
def get_routertypes(self, context, filters=None, fields=None,
                        sorts=None, limit=None, marker=None,
                        page_reverse=False):
        """Lists defined router types."""
        pass


Instruction:
Explain what the following Python 3 code does
def run_migrations_online():
    set_mysql_engine()
    engine = session.create_engine(neutron_config.database.connection)

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        include_object=include_object,
        version_table=alembic_migrations.VERSION_TABLE
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()
        engine.dispose()

Response: Run migrations in online mode.


Instruction:
Create a Python 3 function for
checking whether the Launch Paramters set the role.

Response:
def has_role(self, role):
        '''
        Check whether the Launch Paramters set the role.
        '''
        return self.roles and any([re.search(role, our_role, re.I)
                                   for our_role in self.roles])


Instruction:
Implement a Python 3 function for
returning the full given family name if set.

Response:
def username(self, default=None):
        '''
        Return the full, given, or family name if set.
        '''
        if self.lis_person_name_given:
            return self.lis_person_name_given
        elif self.lis_person_name_family:
            return self.lis_person_name_family
        elif self.lis_person_name_full:
            return self.lis_person_name_full
        else:
            return default


Instruction:
Can you tell what is the following Python 3 function doing
def post_replace_result(self, score, outcome_opts=defaultdict(lambda:None), result_data=None):
        '''
        POSTs the given score to the Tool Consumer with a replaceResult.

        Returns OutcomeResponse object and stores it in self.outcome_request

        OPTIONAL:
            result_data must be a dictionary
            Note: ONLY ONE of these values can be in the dict at a time,
            due to the Canvas specification.

            'text' : str text
            'url' : str url
        '''
        return self.new_request(outcome_opts).post_replace_result(score, result_data)

Response:
POSTs the given score to the Tool Consumer with a replaceResult. Returns an OutcomeResponse object and stores it in self. outcome_request
            OPTIONAL : The result_data is a dictionary with keys text url and result_data.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build_return_url(self):
        '''
        If the Tool Consumer sent a return URL, add any set messages to the
        URL.
        '''
        if not self.launch_presentation_return_url:
            return None

        lti_message_fields = ['lti_errormsg', 'lti_errorlog',
                              'lti_msg', 'lti_log']

        messages = dict([(key, getattr(self, key))
                         for key in lti_message_fields
                         if getattr(self, key, None)])

        # Disassemble original return URL and reassemble with our options added
        original = urlsplit(self.launch_presentation_return_url)

        combined = messages.copy()
        combined.update(dict(parse_qsl(original.query)))

        combined_query = urlencode(combined)

        return urlunsplit((
            original.scheme,
            original.netloc,
            original.path,
            combined_query,
            original.fragment
        ))

Response: Build the URL to return to the Tool Consumer.


Instruction:
Write a Python 3 script for
redirecting Django view to LTI Consumer with messages and log

Response:
def success_redirect(self, msg='', log=''):
        '''
        Shortcut for redirecting Django view to LTI Consumer with messages
        '''
        from django.shortcuts import redirect
        self.lti_msg = msg
        self.lti_log = log
        return redirect(self.build_return_url())


Instruction:
Write a Python 3 script to
redirect to LTI Consumer with error message

Response:
def error_redirect(self, errormsg='', errorlog=''):
        '''
        Shortcut for redirecting Django view to LTI Consumer with errors
        '''
        from django.shortcuts import redirect
        self.lti_errormsg = errormsg
        self.lti_errorlog = errorlog
        return redirect(self.build_return_url())


Instruction:
Can you tell what is the following Python 3 function doing
def _notify_thing_lid_change(self, from_lid, to_lid):
        try:
            with self.__private_things:
                self.__private_things[to_lid] = self.__private_things.pop(from_lid)
        except KeyError:
            logger.warning('Thing %s renamed (to %s), but not in private lookup table', from_lid, to_lid)
        else:
            # renaming could happen before get_thing is called on the original
            try:
                with self.__new_things:
                    self.__new_things[to_lid] = self.__new_things.pop(from_lid)
            except KeyError:
                pass

Response: Used by Thing instances to indicate that a rename operation has happened


Instruction:
Can you generate the documentation for the following Python 3 function
def register_catchall_feeddata(self, callback, callback_parsed=None):
        if callback_parsed:
            callback = self._get_parsed_feed_callback(callback_parsed, callback)
        return self.__client.register_callback_feeddata(callback)

Response: Registers a callback that is called for all feeddata your Thing receives.


Instruction:
Can you generate the documentation for the following Python 3 function
def register_catchall_controlreq(self, callback, callback_parsed=None):
        if callback_parsed:
            callback = self._get_parsed_control_callback(callback_parsed, callback)
        return self.__client.register_callback_controlreq(callback)

Response: Registers a callback that is called for all control requests received by your Thing.


Instruction:
Given the following Python 3 function, write the documentation
def register_callback_created(self, func, serialised=True):
        self.__client.register_callback_created(partial(self.__callback_payload_only, func), serialised=serialised)

Response: Register a callback for resource creation.


Instruction:
Can you tell what is the following Python 3 function doing
def register_callback_duplicate(self, func, serialised=True):
        self.__client.register_callback_duplicate(partial(self.__callback_payload_only, func), serialised=serialised)

Response: Register a callback for resource creation but where the resource already exists in Iotic Space.


Instruction:
Create a Python 3 function for
registering a callback for resource renaming.

Response:
def register_callback_renamed(self, func, serialised=True):
        """
        Register a callback for resource rename.  This will be called when any resource
        is renamed within your agent.
        If `serialised` is not set, the callbacks might arrive in a different order to they were requested.

        The payload passed to your callback is an OrderedDict with the following keys

            #!python
            r         : R_ENTITY, R_FEED, etc # the type of resource deleted
            lid       : <name>                # the new local name of the resource
            oldLid    : <name>                # the old local name of the resource
            id        : <GUID>                # the global Id of the resource

        `Note` resource types are defined [here](../Core/Const.m.html)

        `Example`

            #!python
            def renamed_callback(args):
                print(args)
            ...
            client.register_callback_renamed(renamed_callback)

        This would print out something like the following on renaming of an R_ENTITY

            #!python
            OrderedDict([(u'lid', u'new_name'),
                         (u'r', 1),
                         (u'oldLid', u'old_name'),
                         (u'id', u'4448993b44738411de5fe2a6cf32d957')])
        """

        self.__client.register_callback_renamed(partial(self.__callback_payload_only, func), serialised=serialised)


Instruction:
Can you tell what is the following Python 3 function doing
def register_callback_deleted(self, func, serialised=True):
        self.__client.register_callback_deleted(partial(self.__callback_payload_only, func), serialised=serialised)

Response: Register a callback for resource deletion.


Instruction:
Make a summary of the following Python 3 code
def register_callback_reassigned(self, func, serialised=True):
        self.__client.register_callback_reassigned(partial(self.__callback_payload_only, func), serialised)

Response: Register a callback for reassignment of a resource in the local system.


Instruction:
Can you tell what is the following Python 3 function doing
def register_callback_subscribed(self, callback):
        return self.__client.register_callback_created(partial(self.__callback_subscribed_filter, callback),
                                                       serialised=False)

Response: Register a callback for new subscription.


Instruction:
Write a Python 3 function that can
simulate the last recieved feeddata for given feedid

Response:
def simulate_feeddata(self, feedid, data, mime=None, time=None):
        """Simulate the last feeddata received for given feedid
        Calls the registered callback for the feed with the last recieved feed data. Allows you to test your code
        without having to wait for the remote thing to share again.

        `feedid` (required) (string) local id of your Feed

        `data` (optional) (as applicable) The data you want to use to simulate the arrival of remote feed data

        `mime` (optional) (string) The mime type of your data. See also:
        [share()](./Point.m.html#IoticAgent.IOT.Point.Feed.share)

        `time` (optional) (datetime) UTC timestamp for share. See also:
        [share()](./Point.m.html#IoticAgent.IOT.Point.Feed.share)
        """
        self.__client.simulate_feeddata(feedid, data, mime, time)


Instruction:
Make a summary of the following Python 3 code
def confirm_tell(self, data, success):
        logger.info("confirm_tell(success=%s) [lid=\"%s\",pid=\"%s\"]", success, data[P_ENTITY_LID], data[P_LID])
        evt = self._request_point_confirm_tell(R_CONTROL, data[P_ENTITY_LID], data[P_LID], success, data['requestId'])
        self._wait_and_except_if_failed(evt)

Response: Confirm that you ve done as you ve sent the data.


Instruction:
Create a Python 3 function for
saving the config update the seqnum & default language

Response:
def save_config(self):
        """Save the config, update the seqnum & default language
        """
        self.__config.set('agent', 'seqnum', self.__client.get_seqnum())
        self.__config.set('agent', 'lang', self.__client.default_lang)
        self.__config.save()


Instruction:
Can you write a function in Python 3 where it
uses by point instances and data callbacks

Response:
def _get_point_data_handler_for(self, point):
        """Used by point instances and data callbacks"""
        with self.__point_data_handlers:
            try:
                return self.__point_data_handlers[point]
            except KeyError:
                return self.__point_data_handlers.setdefault(point, PointDataObjectHandler(point, self))


Instruction:
Given the following Python 3 function, write the documentation
def _parsed_callback_wrapper(self, callback_parsed, callback_plain, foc, data):
        # used by PointDataObjectHandler as reference
        if foc == R_FEED:
            point_ref = data['pid']
        else:  # R_CONTROL
            point_ref = Control(self, data[P_ENTITY_LID], data[P_LID], '0' * 32)

        try:
            data['parsed'] = self._get_point_data_handler_for(point_ref).get_template(data=data[P_DATA])
        except RefreshException:
            # No metadata available, do not produce warning
            if callback_plain:
                callback_plain(data)
        except:
            logger.warning('Failed to parse %s data for %s%s', foc_to_str(foc), point_ref,
                           '' if callback_plain else ', ignoring',
                           exc_info=DEBUG_ENABLED)
            if callback_plain:
                callback_plain(data)
        else:
            callback_parsed(data)

Response: Wrapper for parsing data for a specific point.


Instruction:
Write a Python 3 function for
combining waiting for event and call _except_if_failed.

Response:
def _wait_and_except_if_failed(self, event, timeout=None):
        """Combines waiting for event and call to `_except_if_failed`. If timeout is not specified the configured
        sync_timeout is used.
        """
        event.wait(timeout or self.__sync_timeout)
        self._except_if_failed(event)


Instruction:
Create a Python 3 function to
raise an IOTException from the given event if it was not successful.

Response:
def _except_if_failed(cls, event):
        """Raises an IOTException from the given event if it was not successful. Assumes timeout success flag on event
        has not been set yet."""
        if event.success is None:
            raise IOTSyncTimeout('Requested timed out', event)
        if not event.success:
            msg = "Request failed, unknown error"
            if isinstance(event.payload, Mapping):
                if P_MESSAGE in event.payload:
                    msg = event.payload[P_MESSAGE]
                try:
                    exc_class = cls.__exception_mapping[event.payload[P_CODE]]
                except KeyError:
                    pass
                else:
                    raise exc_class(msg, event)

            raise IOTException(msg, event)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def list(self, all_my_agents=False, limit=500, offset=0):
        logger.info("list(all_my_agents=%s, limit=%s, offset=%s)", all_my_agents, limit, offset)
        if all_my_agents:
            evt = self._request_entity_list_all(limit=limit, offset=offset)
        else:
            evt = self._request_entity_list(limit=limit, offset=offset)

        self._wait_and_except_if_failed(evt)
        return evt.payload['entities']

Response: List all the things created by this client on this or all your agents.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_thing(self, lid):
        with self.__new_things:
            try:
                return self.__new_things.pop(lid)
            except KeyError as ex:
                raise_from(KeyError('Thing %s not know as new' % lid), ex)

Response: Get the details of a newly created Thing.


Instruction:
Explain what the following Python 3 code does
def create_thing(self, lid):
        evt = self.create_thing_async(lid)
        self._wait_and_except_if_failed(evt)
        try:
            with self.__new_things:
                return self.__new_things.pop(lid)
        except KeyError as ex:
            raise raise_from(IOTClientError('Thing %s not in cache (post-create)' % lid), ex)

Response: Create a new Thing with a local id.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete_thing(self, lid):
        logger.info("delete_thing(lid=\"%s\")", lid)
        evt = self.delete_thing_async(lid)
        self._wait_and_except_if_failed(evt)

Response: Delete a Thing from the Infrastructure


Instruction:
Given the following Python 3 function, write the documentation
def search(self, text=None, lang=None, location=None, unit=None, limit=50, offset=0, reduced=False, local=None,
               scope=SearchScope.PUBLIC):
        """Search the Iotic Space for public Things with metadata matching the search parameters:
        text, lang(uage), location, unit, limit, offset. Note that only things which have at least one point defined can
        be found.

        Returns dict of results as below (first with reduced=False, second with reduced=True)- OR -

            #!python
            # reduced=False returns dict similar to below
            {
                "2b2d8b068e404861b19f9e060877e002": {
                    "long": -1.74803,
                    "matches": 3.500,
                    "lat": 52.4539,
                    "label": "Weather Station #2",
                    "owner": "3bbf307b43b1460289fe707619dece3d",
                    "points": {
                        "a300cc90147f4e2990195639de0af201": {
                            "matches": 3.000,
                            "label": "Feed 201",
                            "type": "Feed",
                            "storesRecent": true
                        },
                        "a300cc90147f4e2990195639de0af202": {
                            "matches": 1.500,
                            "label": "Feed 202",
                            "type": "Feed",
                            "storesRecent": false
                        }
                    }
                },
                "76a3b24b02d34f20b675257624b0e001": {
                    "long": 0.716356,
                    "matches": 2.000,
                    "lat": 52.244384,
                    "label": "Weather Station #1",
                    "owner": "3bbf307b43b1460289fe707619dece3d",
                    "points": {
                        "fb1a4a4dbb2642ab9f836892da93f101": {
                            "matches": 1.000,
                            "label": "My weather feed",
                            "type": "Feed",
                            "storesRecent": false
                        },
                        "fb1a4a4dbb2642ab9f836892da93c102": {
                            "matches": 1.000,
                            "label": None,
                            "type": "Control",
                            "storesRecent": false
                        }
                    }
                }
            }

            # reduced=True returns dict similar to below
            {
                "2b2d8b068e404861b19f9e060877e002": {
                    "a300cc90147f4e2990195639de0af201": "Feed",
                    "a300cc90147f4e2990195639de0af202": "Feed"
                },
                "76a3b24b02d34f20b675257624b0e001": {
                    "fb1a4a4dbb2642ab9f836892da93f101": "Feed",
                    "fb1a4a4dbb2642ab9f836892da93f102": "Control"
                }
            }

        Raises [IOTException](./Exceptions.m.html#IoticAgent.IOT.Exceptions.IOTException)
        containing the error if the infrastructure detects a problem

        Raises [LinkException](../Core/AmqpLink.m.html#IoticAgent.Core.AmqpLink.LinkException)
        if there is a communications problem between you and the infrastructure

        `text` (optional) (string) The text to search for. Label and description will be searched
        for both Thing and Point and each word will be used as a tag search too. Text search is case-insensitive. Tag
        search is language neutral.

        `lang` (optional) (string) The two-character ISO 639-1 language code to search in, e.g. "en" "fr"
        Language is used to limit search to only labels and descriptions in that language. You will only get labels `in
        that language` back from search and then only if there are any in that language.

        `location` (optional) (dictionary) Latitude, longitude and radius to search within.
        All values are float, Radius is in kilometers (km). E.g. `{"lat"=1.2345, "long"=54.321, "radius"=6.789}`. Note:
        If `text` has not been specified, radius can at most be 25km.

        `unit` (optional) (string) Valid URL of a unit in an ontology.  Or use a constant from the
        [units](../Units.m.html#IoticAgent.Units) class - such as [METRE](../Units.m.html#IoticAgent.Units.METRE).

        `limit` (optional) (integer) Return this many search results.

        `offset` (optional) (integer) Return results starting at this offset - good for paging.

        `reduced` (optional) (boolean) If `true`, return the reduced results just containing points and
        their type.

        `local` (optional) (boolean) **Deprecated**, use `scope` instead. If `true`, perform search at container level.
        Check the local_meta flag to determine whether local metadata functionality is available. (Takes precedence over
        `scope`.)

        `scope` (optional) ([SearchScope](../Core/Const.m.html#IoticAgent.Core.Const.SearchScope)) Whether to perform
        PUBLIC, LOCAL (container level) or LOCAL_OWN (container level restricted to own things) search. Check the
        [local_meta](#IoticAgent.IOT.Client.Client.local_meta) flag to determine whether local metadata functionality is
        available. (Note that PUBLIC and LOCAL_OWN scopes are always available.)
        """
        logger.info("search(text=\"%s\", lang=\"%s\", location=\"%s\", unit=\"%s\", limit=%s, offset=%s, reduced=%s)",
                    text, lang, location, unit, limit, offset, reduced)
        evt = self._request_search(text, lang, location, unit, limit, offset,
                                   SearchType.REDUCED if reduced else SearchType.FULL, local, scope)

        self._wait_and_except_if_failed(evt)
        return evt.payload['result']

Response: Search the Iotic Space for public Things with metadata matching the search parameters.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def search_reduced(self, text=None, lang=None, location=None, unit=None, limit=100, offset=0, local=None,
                       scope=SearchScope.PUBLIC):
        """Shorthand for [search()](#IoticAgent.IOT.Client.Client.search) with `reduced=True`"""
        return self.search(text, lang, location, unit, limit, offset, reduced=True, local=local, scope=scope)

Response: Search reduced Iotic Agent


Instruction:
How would you implement a function in Python 3 that
searches for a specific language in a specific location.

Response:
def search_located(self, text=None, lang=None, location=None, unit=None, limit=100, offset=0, local=None,
                       scope=SearchScope.PUBLIC):
        """See [search()](#IoticAgent.IOT.Client.Client.search) for general documentation. Provides a thing-only
        result set comprised only of things which have a location set, e.g.:

            #!python
            {
                # Keyed by thing id
                '2b2d8b068e404861b19f9e060877e002':
                    # location (g, lat & long), label (l, optional)
                    {'g': (52.4539, -1.74803), 'l': 'Weather Station #2'},
                '76a3b24b02d34f20b675257624b0e001':
                    {'g': (52.244384, 0.716356), 'l': None},
                '76a3b24b02d34f20b675257624b0e004':
                    {'g': (52.245384, 0.717356), 'l': 'Gasometer'},
                '76a3b24b02d34f20b675257624b0e005':
                    {'g': (52.245384, 0.717356), 'l': 'Zepellin'}
            }

        """
        logger.info("search_located(text=\"%s\", lang=\"%s\", location=\"%s\", unit=\"%s\", limit=%s, offset=%s)",
                    text, lang, location, unit, limit, offset)
        evt = self._request_search(text, lang, location, unit, limit, offset, SearchType.LOCATED, local, scope)

        self._wait_and_except_if_failed(evt)
        return evt.payload['result']


Instruction:
Can you implement a function in Python 3 that
describes returns the public description of a Thing or Point.

Response:
def describe(self, guid_or_resource, lang=None, local=None, scope=DescribeScope.AUTO):
        """Describe returns the public (or local) description of a Thing or Point

        Returns the description dict (see below for Thing example) if available, otherwise `None`

            #!python
            {
                "type": "Entity",
                "meta": {
                    "long": 0.716356,
                    "lat": 52.244384,
                    "label": "Weather Station #1",
                    "parent": "3bbf307b43b1460289fe707619dece3d",
                    "points": [
                        {
                            "type": "Control",
                            "label": "Control 101",
                            "guid": "fb1a4a4dbb2642ab9f836892da93c101",
                            "storesRecent": false
                        },
                        {
                            "type": "Feed",
                            "label": "My weather feed",
                            "guid": "fb1a4a4dbb2642ab9f836892da93f101",
                            "storesRecent": true
                        }
                    ],
                    "comment": "A lovely weather station...",
                    "tags": [
                        "blue",
                        "garden"
                    ]
                }
            }

        Raises [IOTException](./Exceptions.m.html#IoticAgent.IOT.Exceptions.IOTException)
        containing the error if the infrastructure detects a problem

        Raises [LinkException](../Core/AmqpLink.m.html#IoticAgent.Core.AmqpLink.LinkException)
        if there is a communications problem between you and the infrastructure

        `guid_or_resource` (mandatory) (string or object).
        If a `string`, it should contain the globally unique id of the resource you want to describe in 8-4-4-4-12
        (or undashed) format.
        If an `object`, it should be an instance of Thing, Point, RemoteFeed or RemoteControl.  The system will return
        you the description of that object.

        `lang` (optional) (string) The two-character ISO 639-1 language code for which labels and comments will be
        returned. This does not affect Values (i.e. when describing a Point, apart from value comments) and tags as
        these are language neutral).

        `local` (optional) (boolean) **Deprecated**, use `scope` instead. If `true`, lookup metadata at container level.
        Check the local_meta flag to determine whether local metadata functionality is available. (Takes precedence over
        `scope`.)

        `scope` (optional) ([DescribeScope](../Core/Const.m.html#IoticAgent.Core.Const.DescribeScope)) Whether to
        perform PUBLIC, LOCAL (container level) or LOCAL_OWN (container level restricted to own things) metadata lookup.
        Check the [local_meta](#IoticAgent.IOT.Client.Client.local_meta) flag to determine whether local metadata
        functionality is available. (Note that AUTO, PUBLIC and LOCAL_OWN scopes are always available.). AUTO mode
        first attempts to look up private metadata, then public.
        """
        if isinstance(guid_or_resource, self.__guid_resources):
            guid = guid_or_resource.guid
        elif isinstance(guid_or_resource, string_types):
            guid = uuid_to_hex(guid_or_resource)
        else:
            raise ValueError("describe requires guid string or Thing, Point, RemoteFeed or RemoteControl instance")
        logger.info('describe() [guid="%s"]', guid)

        evt = self._request_describe(guid, lang, local, scope)

        self._wait_and_except_if_failed(evt)
        return evt.payload['result']


Instruction:
Explain what the following Python 3 code does
def from_post_request(post_request):
        '''
        Convenience method for creating a new OutcomeRequest from a request
        object.

        post_request is assumed to be a Django HttpRequest object
        '''
        request = OutcomeRequest()
        request.post_request = post_request
        request.process_xml(post_request.data)
        return request

Response: Convenience method for creating a new OutcomeRequest from a Django HttpRequest object.


Instruction:
How would you implement a function in Python 3 that
posts the given score to the Tool Consumer with a replaceResult.

Response:
def post_replace_result(self, score, result_data=None):
        '''
        POSTs the given score to the Tool Consumer with a replaceResult.

        OPTIONAL:
            result_data must be a dictionary
            Note: ONLY ONE of these values can be in the dict at a time,
            due to the Canvas specification.

            'text' : str text
            'url' : str url
        '''
        self.operation = REPLACE_REQUEST
        self.score = score
        self.result_data = result_data
        if result_data is not None:
            if len(result_data) > 1:
                error_msg = ('Dictionary result_data can only have one entry. '
                             '{0} entries were found.'.format(len(result_data)))
                raise InvalidLTIConfigError(error_msg)
            elif 'text' not in result_data and 'url' not in result_data:
                error_msg = ('Dictionary result_data can only have the key '
                             '"text" or the key "url".')
                raise InvalidLTIConfigError(error_msg)
            else:
                return self.post_outcome_request()
        else:
            return self.post_outcome_request()


Instruction:
Can you implement a function in Python 3 that
posts an OAuth signed request to the LTI - State Service.

Response:
def post_outcome_request(self):
        '''
        POST an OAuth signed request to the Tool Consumer.
        '''
        if not self.has_required_attributes():
            raise InvalidLTIConfigError(
                'OutcomeRequest does not have all required attributes')

        consumer = oauth2.Consumer(key=self.consumer_key,
                                   secret=self.consumer_secret)

        client = oauth2.Client(consumer)
        # monkey_patch_headers ensures that Authorization
        # header is NOT lower cased
        monkey_patch_headers = True
        monkey_patch_function = None
        if monkey_patch_headers:
            import httplib2
            http = httplib2.Http

            normalize = http._normalize_headers

            def my_normalize(self, headers):
                print("My Normalize", headers)
                ret = normalize(self, headers)
                if 'authorization' in ret:
                    ret['Authorization'] = ret.pop('authorization')
                print("My Normalize", ret)
                return ret
            http._normalize_headers = my_normalize
            monkey_patch_function = normalize

        response, content = client.request(
            self.lis_outcome_service_url,
            'POST',
            body=self.generate_request_xml(),
            headers={'Content-Type': 'application/xml'})

        if monkey_patch_headers and monkey_patch_function:
            import httplib2
            http = httplib2.Http
            http._normalize_headers = monkey_patch_function

        self.outcome_response = OutcomeResponse.from_post_response(response,
                                                                   content)
        return self.outcome_response


Instruction:
Implement a function in Python 3 to
parse Outcome Request XML.

Response:
def process_xml(self, xml):
        '''
        Parse Outcome Request data from XML.
        '''
        root = objectify.fromstring(xml)
        self.message_identifier = str(
            root.imsx_POXHeader.imsx_POXRequestHeaderInfo.
            imsx_messageIdentifier)
        try:
            result = root.imsx_POXBody.replaceResultRequest
            self.operation = REPLACE_REQUEST
            # Get result sourced id from resultRecord
            self.lis_result_sourcedid = result.resultRecord.\
                sourcedGUID.sourcedId
            self.score = str(result.resultRecord.result.
                             resultScore.textString)
        except:
            pass

        try:
            result = root.imsx_POXBody.deleteResultRequest
            self.operation = DELETE_REQUEST
            # Get result sourced id from resultRecord
            self.lis_result_sourcedid = result.resultRecord.\
                sourcedGUID.sourcedId
        except:
            pass

        try:
            result = root.imsx_POXBody.readResultRequest
            self.operation = READ_REQUEST
            # Get result sourced id from resultRecord
            self.lis_result_sourcedid = result.resultRecord.\
                sourcedGUID.sourcedId
        except:
            pass


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _setup_transport(self):
        if hasattr(self, 'sslopts'):
            self.sock = ssl.wrap_socket(self.sock, **self.sslopts)
        elif hasattr(self, 'sslctx'):
            self.sock = self.sslctx.wrap_socket(self.sock,
                                                server_hostname=self.hostname)
        else:
            self.sock = ssl.wrap_socket(self.sock)
        self.sock.do_handshake()
        self._quick_recv = self.sock.read

Response: Wrap the socket in an SSL object.


Instruction:
Can you create a Python 3 function that
unwraps a Python 2. 6 SSL socket so we can call shutdown

Response:
def _shutdown_transport(self):
        """Unwrap a Python 2.6 SSL socket, so we can call shutdown()"""
        if self.sock is not None:
            try:
                unwrap = self.sock.unwrap
            except AttributeError:
                return
            try:
                self.sock = unwrap()
            except ValueError:
                # Failure within SSL might mean unwrap exists but socket is not
                # deemed wrapped
                pass


Instruction:
Implement a Python 3 function for
writing config to file.

Response:
def save(self, filename=None):
        """Write config to file."""
        if self.__fname is None and filename is None:
            raise ValueError('Config loaded from string, no filename specified')
        conf = self.__config
        cpa = dict_to_cp(conf)
        with open(self.__fname if filename is None else filename, 'w') as f:
            cpa.write(f)


Instruction:
Make a summary of the following Python 3 code
def get(self, section, val):
        val = val.lower()
        if section in self.__config:
            if val in self.__config[section]:
                # logger.debug('get config %s %s = %s', section, val, self.__config[section][val])
                return self.__config[section][val]
        if section in self.__defaults:
            if val in self.__defaults[section]:
                # logger.debug('get defaults %s %s = %s', section, val, self.__defaults[section][val])
                return self.__defaults[section][val]
        return None

Response: Get a setting or the default value


Instruction:
Create a Python 3 function to
add a setting to the config file.

Response:
def set(self, section, val, data):
        """Add a setting to the config

        `section` (mandatory) (string) the section name in the config E.g. `"agent"`

        `val` (mandatory) (string) the section name in the config E.g. `"host"`

        `data` (mandatory) (as appropriate) the new value for the `val`
        """
        val = val.lower()
        if section in self.__config:
            # logger.debug('set %s %s = %s', section, val, data)
            self.__config[section][val] = data


Instruction:
Can you implement a function in Python 3 that
updates the value of the key in the config

Response:
def update(self, section, val, data):
        """Add a setting to the config, but if same as default or None then no action.
        This saves the .save writing the defaults

        `section` (mandatory) (string) the section name in the config E.g. `"agent"`

        `val` (mandatory) (string) the section name in the config E.g. `"host"`

        `data` (mandatory) (as appropriate) the new value for the `val`
        """
        k = self.get(section, val)
        # logger.debug('update %s %s from: %s to: %s', section, val, k, data)
        if data is not None and k != data:
            self.set(section, val, data)


Instruction:
Make a summary of the following Python 3 code
def get_location(self):
        lat = None
        lon = None
        # note: always picks from first triple
        for _, _, o in self._graph.triples((None, GEO_NS.lat, None)):
            lat = float(o)
            break
        for _, _, o in self._graph.triples((None, GEO_NS.long, None)):
            lon = float(o)
            break

        return lat, lon

Response: Gets the current geo location of your Thing


Instruction:
Here you have a function in Python 3, explain what it does
def delete_location(self):
        # normally this should only remove one triple each
        for s, p, o in self._graph.triples((None, GEO_NS.lat, None)):
            self._graph.remove((s, p, o))
        for s, p, o in self._graph.triples((None, GEO_NS.long, None)):
            self._graph.remove((s, p, o))

Response: Deletes all the geo : lat and geo : long metadata properties on your Thing


Instruction:
Create a Python 3 function to
start connection threads blocks until started is set

Response:
def start(self):
        """start connection threads, blocks until started
        """
        if not (self.__recv_thread or self.__send_thread):
            self.__end.clear()
            self.__send_ready.clear()
            self.__recv_ready.clear()
            timeout = self.__socket_timeout + 1
            ignore_exc = self.__startup_ignore_exc
            self.__send_exc_clear()
            self.__recv_exc_clear()

            # start & await send thread success (unless timeout reached or an exception has occured)
            self.__send_thread = Thread(target=self.__send_run, name='amqplink_send')
            self.__send_thread.start()
            start_time = monotonic()
            success = False
            while not (success or (not ignore_exc and self.__send_exc) or monotonic() - start_time > timeout):
                success = self.__send_ready.wait(.25)

            if success:
                # start & await receiver thread success
                self.__recv_thread = Thread(target=self.__recv_run, name='amqplink_recv')
                self.__recv_thread.start()
                start_time = monotonic()
                success = False
                while not (success or (not ignore_exc and self.__recv_exc) or monotonic() - start_time >= timeout):
                    success = self.__recv_ready.wait(.25)

            # handler either thread's failure
            if not success:
                logger.warning("AmqpLink Failed to start.  Giving up.")
                self.stop()
                if self.__recv_exc:
                    # prioritise receive thread since this can get access-denied whereas send does not (until sending)
                    raise_from(LinkException('Receive thread failure'), self.__recv_exc)
                elif self.__send_exc:
                    raise_from(LinkException('Send thread failure'), self.__send_exc)
                else:
                    raise LinkException('Unknown link failure (timeout reached)')

        else:
            raise LinkException('amqplink already started')


Instruction:
Given the following Python 3 function, write the documentation
def stop(self):
        self.__end.set()
        if self.__recv_thread:
            self.__recv_thread.join()
            self.__recv_thread = None
        if self.__send_thread:
            self.__send_thread.join()
            self.__send_thread = None

Response: disconnect blocks until stopped


Instruction:
Implement a Python 3 function for
sending a message to the broker.

Response:
def send(self, body, content_type='application/ubjson', timeout=5):
        """timeout indicates amount of time to wait for sending thread to be ready. set to larger than zero to wait
        (in seconds, fractional) or None to block.
        """
        if self.__send_ready.wait(timeout):
            try:
                with self.__send_lock:
                    # access denied response might be received inside send thread rather than here how to best handle?
                    self.__send_channel.basic_publish(msg=Message(body, delivery_mode=2, content_type=content_type),
                                                      exchange=self.__epid)
            except exceptions.AccessRefused as exc:
                raise_from(LinkException('Access denied'), exc)
            except (exceptions.AMQPError, SocketError) as exc:
                raise_from(LinkException('amqp/transport failure'), exc)
            except Exception as exc:  # pylint: disable=broad-except
                raise_from(LinkException('unexpected failure'), exc)
        else:
            exc = self.__send_exc
            if exc:
                raise_from(LinkException('Sender unavailable'), exc)
            else:
                raise LinkException('Sender unavailable (unknown error)')


Instruction:
Can you generate the documentation for the following Python 3 function
def __get_ssl_context(cls, sslca=None):
        if ((version_info[0] == 2 and (version_info[1] >= 7 and version_info[2] >= 5)) or
                (version_info[0] == 3 and version_info[1] >= 4)):
            logger.debug('SSL method for 2.7.5+ / 3.4+')
            # pylint: disable=no-name-in-module
            from ssl import SSLContext, PROTOCOL_TLSv1_2, CERT_REQUIRED, OP_NO_COMPRESSION
            ctx = SSLContext(PROTOCOL_TLSv1_2)
            ctx.set_ciphers('HIGH:!SSLv3:!TLSv1:!aNULL:@STRENGTH')
            # see CRIME security exploit
            ctx.options |= OP_NO_COMPRESSION
            # the following options are used to verify the identity of the broker
            if sslca:
                ctx.load_verify_locations(sslca)
                ctx.verify_mode = CERT_REQUIRED
                ctx.check_hostname = False
            else:
                # Verify public certifcates if sslca is None (default)
                from ssl import Purpose  # pylint: disable=no-name-in-module
                ctx.load_default_certs(purpose=Purpose.SERVER_AUTH)
                ctx.verify_mode = CERT_REQUIRED
                ctx.check_hostname = True

        elif version_info[0] == 3 and version_info[1] < 4:
            logger.debug('Using SSL method for 3.2+, < 3.4')
            # pylint: disable=no-name-in-module
            from ssl import SSLContext, CERT_REQUIRED, PROTOCOL_SSLv23, OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_TLSv1
            ctx = SSLContext(PROTOCOL_SSLv23)
            ctx.options |= (OP_NO_SSLv2 | OP_NO_SSLv3 | OP_NO_TLSv1)
            ctx.set_ciphers('HIGH:!SSLv3:!TLSv1:!aNULL:@STRENGTH')
            # the following options are used to verify the identity of the broker
            if sslca:
                ctx.load_verify_locations(sslca)
                ctx.verify_mode = CERT_REQUIRED
            else:
                # Verify public certifcates if sslca is None (default)
                ctx.set_default_verify_paths()
                ctx.verify_mode = CERT_REQUIRED

        else:
            raise Exception("Unsupported Python version %s" % '.'.join(str(item) for item in version_info[:3]))

        return ctx

Response: Make an SSLConext for this Python version using public or sslca.


Instruction:
How would you implement a function in Python 3 that
calls user - provided callback and marks message for Ack regardless of success

Response:
def __recv_cb(self, msg):
        """Calls user-provided callback and marks message for Ack regardless of success
        """
        try:
            self.__msg_callback(msg)
        except:
            logger.exception("AmqpLink.__recv_cb exception calling msg_callback")
        finally:
            # only works if all messages handled in series
            self.__last_id = msg.delivery_tag
            self.__unacked += 1


Instruction:
Given the following Python 3 function, write the documentation
def __recv_log_set_exc_and_wait(self, msg, level=None, wait_seconds=CONN_RETRY_DELAY_SECONDS):
        logger.log(
            ((logging.DEBUG if self.__recv_exc else logging.ERROR) if level is None else level),
            msg,
            exc_info=DEBUG_ENABLED
        )
        self.__recv_exc = exc_info()[1]
        self.__end.wait(wait_seconds)

Response: Set the exception of the receiver thread and wait for it to finish.


Instruction:
Can you generate the documentation for the following Python 3 function
def __send_log_set_exc_and_wait(self, msg, level=None, wait_seconds=CONN_RETRY_DELAY_SECONDS):
        logger.log(
            ((logging.DEBUG if self.__send_exc else logging.ERROR) if level is None else level),
            msg,
            exc_info=DEBUG_ENABLED
        )
        self.__send_exc_time = monotonic()
        self.__send_exc = exc_info()[1]
        self.__end.wait(wait_seconds)

Response: To be called in exception context only.


Instruction:
Explain what the following Python 3 code does
def __send_exc_clear(self, log_if_exc_set=None):
        if not (log_if_exc_set is None or self.__send_exc is None):
            logger.info(log_if_exc_set)
        self.__send_exc_time = None
        self.__send_exc = None

Response: Clear send exception and time.


Instruction:
Can you tell what is the following Python 3 function doing
def valid_mimetype(type_, allow_none=True):
    if isinstance(type_, unicode_type):
        match = __IDX_PATTERN.match(type_)
        if match:
            return match.group(1) in __IDX_MAPPING
        else:
            return __is_ascii(type_, 1, __MAX_LEN)
    else:
        return type_ is None and allow_none

Response: Checks for validity of given mimetype.


Instruction:
Can you implement a function in Python 3 that
returns long equivalent of type_ if available otherwise type_ itself. Does not raise exceptions

Response:
def expand_idx_mimetype(type_):
    """Returns long equivalent of type_, if available, otherwise type_ itself. Does not raise exceptions"""
    if isinstance(type_, unicode_type):
        match = __IDX_PATTERN.match(type_)
        return __IDX_MAPPING.get(match.group(1), type_) if match else type_
    else:
        return type_


Instruction:
Here you have a function in Python 3, explain what it does
def throttle(self):
        iterations = self.__iterations
        timestamp = monotonic()
        outdated_threshold = timestamp - self.__interval

        with self.__lock:
            # remove any iterations older than interval
            try:
                while iterations[0] < outdated_threshold:
                    iterations.popleft()
            except IndexError:
                pass
            # apply throttling if rate would be exceeded
            if len(iterations) <= self.__max_iterations:
                iterations.append(timestamp)
                retval = None
            else:
                # wait until oldest sample is too old
                delay = max(0, iterations[0] + self.__interval - timestamp)
                # only notify user about longer delays
                if delay > 1:
                    logger.warning('Send throttling delay (interval=%d, max_iterations=%d): %.2fs', self.__interval,
                                   self.__max_iterations, delay)
                retval = self.__wait_cmd(delay)
                # log actual addition time
                iterations.append(monotonic())

            return retval

Response: Returns None if the rate limit is exceeded.


Instruction:
Make a summary of the following Python 3 code
def decode_sent_msg(pref, message, pretty=False):
    newline = "\n" if pretty else " "
    indent = "    " if pretty else ""
    start = newline + indent

    out = []
    out.append("%s%s{%sSEQNUM: %d," % (pref, newline, start, message[Const.W_SEQ]))
    out.append("%sCOMPRESSION: %d," % (start, message[Const.W_COMPRESSION]))
    out.append("%sHASH: %s...," % (start, str(binascii.b2a_hex(message[Const.W_HASH]).decode('ascii'))[:10]))
    out.append("%sMESSAGE:%s{%sCLIENTREF: %s," % (start, start, start + indent,
                                                  message[Const.W_MESSAGE][Const.M_CLIENTREF]))
    out.append("%sRESOURCE: %s," % (start + indent, R_TYPES[message[Const.W_MESSAGE][Const.M_RESOURCE]]))
    out.append("%sTYPE: %s," % (start + indent, C_TYPES[message[Const.W_MESSAGE][Const.M_TYPE]]))
    out.append("%sACTION: %s," % (start + indent, message[Const.W_MESSAGE][Const.M_ACTION]))
    if Const.M_RANGE in message[Const.W_MESSAGE]:
        out.append("%sRANGE: %s," % (start + indent, message[Const.W_MESSAGE][Const.M_RANGE]))
    out.append("%sPAYLOAD: %s%s}%s}" % (start + indent, message[Const.W_MESSAGE][Const.M_PAYLOAD], start, newline))

    return ''.join(out)

Response: Decode a message into a string of the decoded message.


Instruction:
Can you implement a function in Python 3 that
decodes a received message into a string expanding short codes optionally with newlines and indent

Response:
def decode_rcvd_msg(pref, message, seqnum, pretty=False):
    """decode_rcvd_msg: Return string of received message expanding short codes, optionally with newlines and indent
    """
    newline = "\n" if pretty else " "
    indent = "    " if pretty else ""
    start = newline + indent

    out = []
    out.append("%s%s{%sSEQNUM: %d," % (pref, newline, start, seqnum))
    out.append("%sCLIENTREF: %s," % (start, message[Const.M_CLIENTREF]))
    out.append("%sTYPE: %s," % (start, M_TYPES[message[Const.M_TYPE]]))
    if message[Const.M_TYPE] in M_SUB_TYPES:
        out.append("%sPAYLOAD: {CODE: %s, MESSAGE: %s}" %
                   (start, M_SUB_TYPES[message[Const.M_TYPE]][message[Const.M_PAYLOAD][Const.P_CODE]],
                    message[Const.M_PAYLOAD][Const.P_MESSAGE]))
    else:
        payload = None
        if message[Const.M_PAYLOAD] is not None:
            payload = {}
            for item in message[Const.M_PAYLOAD]:
                if item == Const.P_RESOURCE:
                    payload['RESOURCE'] = R_TYPES[message[Const.M_PAYLOAD][Const.P_RESOURCE]]
                else:
                    payload[item] = message[Const.M_PAYLOAD][item]
        out.append("%sPAYLOAD: %s" % (start, payload))
    out.append("%s}" % newline)

    return ''.join(out)


Instruction:
Implement a function in Python 3 to
convert a long value into its byte string equivalent.

Response:
def _long_to_bytes(self, long_value):
        """
        Turns a long value into its byte string equivalent.
        :param long_value: the long value to be returned as a byte string
        :return: a byte string equivalent of a long value
        """
        _byte_string = b''
        pack = struct.pack
        while long_value > 0:
            _byte_string = pack(b'>I', long_value & 0xffffffff) + _byte_string
            long_value = long_value >> 32
        for i in range(len(_byte_string)):
            if _byte_string[i] != b'\000'[0]:
                break
        else:
            _byte_string = b'\000'
            i = 0
        _byte_string = _byte_string[i:]

        return _byte_string


Instruction:
How would you explain what the following Python 3 function does
def restore_event(self, requestId):
        with self.__requests:
            if requestId not in self.__requests:
                self.__requests[requestId] = RequestEvent(requestId)
                return True
        return False

Response: restore an event based on the requestId


Instruction:
Can you create a Python 3 function that
adds a callback to the list of callbacks for a specific type.

Response:
def __add_callback(self, type_, func, serialised_if_crud=True):
        """sync_if_crud indicates whether to serialise this callback (applies only to CRUD)"""
        Validation.callable_check(func)
        with self.__callbacks:
            self.__callbacks[type_].append((func, serialised_if_crud))


Instruction:
Given the following Python 3 function, write the documentation
def register_callback_created(self, func, serialised=True):
        self.__add_callback(_CB_CREATED, func, serialised_if_crud=serialised)

Response: Register a callback function to receive a QAPI Unsolicited ( resource ) CREATED.


Instruction:
Here you have a function in Python 3, explain what it does
def register_callback_duplicate(self, func, serialised=True):
        self.__add_callback(_CB_DUPLICATE, func, serialised_if_crud=serialised)

Response: Register a callback function to receive a duplicate message.


Instruction:
Implement a function in Python 3 to
register a callback function to receive a QAPI Unsolicited ( resource ) RENAMED.

Response:
def register_callback_renamed(self, func, serialised=True):
        """Register a callback function to receive QAPI Unsolicited (resource) RENAMED. The
        callback receives a single argument - the inner message. If `serialised` is not set,
        the callbacks might arrive out-of-order.
        """
        self.__add_callback(_CB_RENAMED, func, serialised_if_crud=serialised)


Instruction:
Can you create a Python 3 function that
registers a callback function to receive a Unsolicited ( entity ) REASSIGNED.

Response:
def register_callback_reassigned(self, func, serialised=True):
        """Register a callback function to receive QAPI Unsolicited (entity) REASSIGNED. The
        callback receives a single argument - the inner message. If `serialised` is not set,
        the callbacks might arrive out-of-order.
        """
        self.__add_callback(_CB_REASSIGNED, func, serialised_if_crud=serialised)


Instruction:
Write a Python 3 function that can
start the send & receive threads.

Response:
def start(self):  # noqa (complexity)
        """Start the send & recv Threads.
        Start can be delayed to EG restore requestIds before attaching to the QAPI
        Note: This function waits for/blocks until amqplink connect(s) and the current
              sequence number has been obtained from the container (within 5 seconds)
        """
        if not self.__end.is_set():
            return

        self.__end.clear()
        try:
            self.__network_retry_queue = Queue(self.__network_retry_queue_size)
            self.__network_retry_thread = Thread(target=self.__network_retry, name='network')
            self.__network_retry_thread.start()
            try:
                self.__amqplink.start()
            except Exception as exc:  # pylint: disable=broad-except
                if not self.__amqplink.is_alive():
                    raise_from(LinkException("Core.AmqpLink: Failed to connect"), exc)
                logger.exception("Unhandled startup error")
                raise

            req = self.request_ping()
            if not req.wait(5):
                raise LinkException("No container response to ping within 5s")
            # (for req.payload) pylint: disable=unsubscriptable-object
            if not req.success:
                try:
                    info = ': %s' % req.payload[P_MESSAGE]
                except (KeyError, TypeError):
                    info = ''
                raise Exception("Unexpected ping failure: %s" % info)

            payload = req.payload
            self.__qapi_version_check(payload)
            if self.__default_lang is None:
                self.__default_lang = payload['lang']
            self.__container_params = payload
            try:
                self.set_compression(payload['compression'])
            except ValueError as ex:
                raise_from(Exception('Container compression method (%d) unsupported' % payload['compression']), ex)
            self.__local_meta = payload['local_meta']

            self.__threadpool.start()
            self.__crud_threadpool.start()
        except:
            self.stop()
            raise


Instruction:
Can you implement a function in Python 3 that
stops the Client disconnect from queue

Response:
def stop(self):
        """Stop the Client, disconnect from queue
        """
        if self.__end.is_set():
            return
        self.__end.set()
        self.__send_retry_requests_timer.cancel()
        self.__threadpool.stop()
        self.__crud_threadpool.stop()
        self.__amqplink.stop()
        self.__network_retry_thread.join()
        # Clear out remaining pending requests
        with self.__requests:
            shutdown = LinkShutdownException('Client stopped')
            for req in self.__requests.values():
                req.exception = shutdown
                req._set()
                self.__clear_references(req, remove_request=False)
            if self.__requests:
                logger.warning('%d unfinished request(s) discarded', len(self.__requests))
            self.__requests.clear()
        #
        self.__network_retry_thread = None
        self.__network_retry_queue = None
        self.__container_params = None


Instruction:
Can you generate a brief explanation for the following Python 3 code
def set_compression(self, comp=COMP_DEFAULT, size=COMP_SIZE):
        if comp not in COMPRESSORS:
            if comp == COMP_LZ4F:
                raise ValueError('lz4f compression not available, required lz4framed')
            else:
                raise ValueError('Invalid compression method')
        if not isinstance(size, int_types) or size < 1:
            raise ValueError('size must be non-negative integer')
        self.__comp_default = comp
        self.__comp_size = size
        return self.__comp_default, self.__comp_size

Response: Override default compression method and threshold


Instruction:
Write a Python 3 function that can
call by the transport implementation to send all requests which have not been sent due to a transport failure. This function is called by the transport implementation to send all the requests that have not been sent due to a transport failure.

Response:
def __send_retry_requests(self, last_send_failure_time):
        """Called via Timer from __send_ready to resend requests which might not have been sent due to transport
           failure. This can happen since the current transport implementation does not received acknowledgements
           for sent messages."""
        # make sure multiple failures having set multiple times do not run concurrently
        with self.__send_retry_requests_lock:
            with self.__requests:
                # produce list instead of generator as requests mapping can change during subsequent loop
                retry_reqs = [req for req in self.__requests.values()
                              if req._sent_without_response(last_send_failure_time)]

            retry_req_count = 0
            # don't continue if another network failure has occured (which will trigger this function again)
            while retry_reqs and self.__amqplink.last_send_exc_time <= last_send_failure_time:
                req = retry_reqs.pop()
                # lock individuallly so incoming request handling does not 'pause' for too long
                with self.__requests:
                    # might have received a response (or finished since)
                    if not (req.id_ in self.__requests and req._sent_without_response(last_send_failure_time)):
                        logger.debug('Not resending request %s (finished or has received response)', req.id_)
                        continue
                logger.debug('Resending request %s', req.id_)
                if not self.__retry_enqueue(PreparedMessage(req._inner_msg_out, req.id_)):
                    # client shutdown
                    break
                retry_req_count += 1

        if retry_req_count:
            logger.debug('Resending of %d request(s) complete (before %s)', retry_req_count, last_send_failure_time)


Instruction:
How would you implement a function in Python 3 that
requests entity create: lid = local name to user If epId=None (default), the current agent/EP is chosen If epId=False, no agent is assigned If epId=guid, said agent is chosen

Response:
def request_entity_create(self, lid, epId=None):
        """request entity create: lid = local name to user
        If epId=None (default), the current agent/EP is chosen
        If epId=False, no agent is assigned
        If epId=guid, said agent is chosen
        """
        lid = Validation.lid_check_convert(lid)
        if epId is None:
            epId = self.__epId
        elif epId is False:
            epId = None
        else:
            epId = Validation.guid_check_convert(epId)
        logger.debug("request_entity_create lid='%s'", lid)
        return self._request(R_ENTITY, C_CREATE, None, {'epId': epId, 'lid': lid}, is_crud=True)


Instruction:
How would you code a function in Python 3 to
request entity to be reassigned to given ep

Response:
def request_entity_reassign(self, lid, nepId=None):
        """request entity to be reassigned to given ep/agent
        If nepId=None (default), the current agent/EP is chosen
        If nepId=False, no agent is assigned
        If nepId=guid, said agent is chosen
        """
        lid = Validation.lid_check_convert(lid)
        if nepId is None:
            nepId = self.__epId
        elif nepId is False:
            nepId = None
        else:
            nepId = Validation.guid_check_convert(nepId)
        logger.debug("request_entity_reassign lid='%s' -> nepId='%s'", lid, nepId)
        return self._request(R_ENTITY, C_UPDATE, (lid, 'reassign'), {'epId': nepId}, is_crud=True)


Instruction:
Create a Python 3 function to
request point create lid and pid point lid

Response:
def request_point_create(self, foc, lid, pid, control_cb=None, save_recent=0):
        """request point create: feed or control, lid and pid point lid
        """
        Validation.foc_check(foc)
        lid = Validation.lid_check_convert(lid)
        pid = Validation.pid_check_convert(pid)
        save_recent = validate_int(save_recent, 'save_recent')
        logger.debug("request_point_create foc=%i lid='%s' pid='%s' save_recent=%d", foc, lid, pid, save_recent)

        if foc == R_CONTROL:
            Validation.callable_check(control_cb)
            if save_recent:
                logger.warning('ignoring non-zero save_recent value for control')
            evt = self._request(foc, C_CREATE, (lid,), {'lid': pid}, is_crud=True)
            with self.__pending_controls:
                self.__pending_controls[evt.id_] = control_cb
            return evt
        elif control_cb:
            raise ValueError('callback specified for Feed')
        else:
            return self._request(foc, C_CREATE, (lid,), {'lid': pid, 'saveRecent': save_recent}, is_crud=True)


Instruction:
Implement a Python 3 function for
returning tuple of mime type & data. Auto encodes unicode strings to utf8 and unicode dictionaries depending on client setting.

Response:
def __point_data_to_bytes(self, data, mime=None):  # pylint: disable=too-many-branches
        """Returns tuple of mime type & data. Auto encodes unicode strings (to utf8) and
           dictionaries (to ubjson) depending on client setting."""
        if mime is None:
            if self.__auto_encode_decode:
                if isinstance(data, bytes):
                    return None, data
                elif isinstance(data, dict):
                    # check top level dictionary keys
                    if all(isinstance(key, unicode_type) for key in data):
                        return 'idx/1', ubjdumpb(data)  # application/ubjson
                    else:
                        raise ValueError('At least one key in dict not real (unicode) string')
                elif isinstance(data, unicode_type):
                    return 'idx/2', data.encode('utf8')  # text/plain; charset=utf8
                else:
                    raise ValueError('cannot auto-encode data of type %s' % type(data))
            elif isinstance(data, bytes):
                return None, data
            else:
                raise ValueError('No mime type specified and not bytes object (auto-encode disabled)')
        elif valid_mimetype(mime):
            if isinstance(data, bytes):
                return mime, data
            else:
                raise ValueError('mime specified but data not bytes object')
        else:
            raise ValueError('invalid mime type %s' % mime)


Instruction:
Can you create a Python 3 function that
attempts to auto - decode data

Response:
def __bytes_to_share_data(self, payload):
        """Attempt to auto-decode data"""
        rbytes = payload[P_DATA]
        mime = payload[P_MIME]

        if mime is None or not self.__auto_encode_decode:
            return rbytes, mime
        mime = expand_idx_mimetype(mime).lower()
        try:
            if mime == 'application/ubjson':
                return ubjloadb(rbytes), None
            elif mime == 'text/plain; charset=utf8':
                return rbytes.decode('utf-8'), None
            else:
                return rbytes, mime
        except:
            logger.warning('auto-decode failed, returning bytes', exc_info=DEBUG_ENABLED)
            return rbytes, mime


Instruction:
Can you generate a brief explanation for the following Python 3 code
def __new_request_id(self):
        while True:
            # Since seqnum wraps on 2^64 at most, this should always fit into 32 chars (QAPI request id limit)
            with self.__seqnum_lock:
                requestId = "%s%d" % (self.__reqpre, self.__reqnum)
            self.__reqnum += 1
            if requestId not in self.__requests:
                break
            # in the unlikely event of a collision update prefix
            self.__reqpre = self.__rnd_string(6)
        return requestId

Response: Returns a new request id for the current request.


Instruction:
Can you implement a function in Python 3 that
returns the hash for this innermsg token seqnum return digest bytes

Response:
def __make_hash(cls, innermsg, token, seqnum):
        """return the hash for this innermsg, token, seqnum
        return digest bytes
        """
        hobj = hmacNew(token, digestmod=hashfunc)
        hobj.update(innermsg)
        hobj.update(cls.__byte_packer(seqnum))
        return hobj.digest()


Instruction:
Write a Python 3 script for
checking if hash is good

Response:
def __check_hash(self, message):
        """return true/false if hash is good
        message = dict
        """
        return message[W_HASH] == self.__make_hash(message[W_MESSAGE], self.__token, message[W_SEQ])


Instruction:
Can you generate the documentation for the following Python 3 function
def __make_innermsg(resource, rtype, ref, action=None, payload=None, limit=None):
        if action is not None and not isinstance(action, (tuple, list)):
            raise TypeError('action must be None/tuple/list')
        p = {M_RESOURCE: resource,
             M_TYPE: int(rtype),
             M_CLIENTREF: ref,
             # Ensure action path consists only of strings
             M_ACTION: tuple(u(element) for element in action) if action else None,
             M_PAYLOAD: payload}
        if limit is not None:  # Note: fmtted like "0/15" where 0 = offset, 15 = limit
            p[M_RANGE] = limit
        return p

Response: return innermsg chunk (dict)


Instruction:
Create a Python 3 function to
set exception for the given request and remove from internal cache and set its event

Response:
def __request_except(self, requestId, exc, set_and_forget=True):
        """Set exception (if not None) for the given request and (optionally) remove from internal cache & setting its
           event"""
        try:
            with self.__requests:
                if set_and_forget:
                    req = self.__requests.pop(requestId)
                else:
                    req = self.__requests[requestId]
        except KeyError:
            logger.error('Unknown request %s - cannot set exception', requestId)
        else:
            if exc is not None:
                req.exception = exc
            if set_and_forget:
                req._set()


Instruction:
Can you generate the documentation for the following Python 3 function
def __request_mark_sent(self, requestId):
        with self.__requests:
            try:
                req = self.__requests[requestId]
            except KeyError:
                # request might have had a response already have been removed by receiving thread
                pass
            else:
                req.exception = None
                req._send_time = monotonic()

Response: Set send time & clear exception from request if set


Instruction:
Can you generate a brief explanation for the following Python 3 code
def __publish(self, qmsg):
        with self.__seqnum_lock:
            seqnum = self.__seqnum
            self.__seqnum = (self.__seqnum + 1) % _SEQ_WRAP_SIZE
        #
        innermsg = ubjdumpb(qmsg.inner_msg)
        clevel = COMP_NONE
        if len(innermsg) >= self.__comp_size:
            logger.debug('Compressing payload')
            try:
                innermsg = COMPRESSORS[self.__comp_default].compress(innermsg)
            except KeyError:
                logger.warning('Unknown compression method %s, not compressing', self.__comp_default)
            else:
                clevel = self.__comp_default

        p = {W_SEQ: seqnum,
             W_MESSAGE: innermsg,
             W_HASH: self.__make_hash(innermsg, self.__token, seqnum),
             W_COMPRESSION: clevel}
        msg = ubjdumpb(p)

        # do not send messages exceeding size limit
        if len(msg) > self.__max_encoded_length:
            self.__request_except(qmsg.requestId, ValueError("Message Payload too large %d > %d"
                                                             % (len(msg), self.__max_encoded_length)))
            return False

        self.__amqplink.send(msg, content_type='application/ubjson')
        if DEBUG_ENABLED:
            p[W_MESSAGE] = qmsg.inner_msg
            logger.debug(decode_sent_msg('decode_sent_msg', p))
        # Callback any debuggers
        self.__fire_callback(_CB_DEBUG_SEND, msg)
        #
        return True

Response: Sends a message to the AMPL server. Returns True if the message was successfully sent False otherwise.


Instruction:
Write a Python 3 function for
returning True if at least one callback was called

Response:
def __fire_callback(self, type_, *args, **kwargs):
        """Returns True if at least one callback was called"""
        called = False
        plain_submit = self.__threadpool.submit
        with self.__callbacks:
            submit = self.__crud_threadpool.submit if type_ in _CB_CRUD_TYPES else plain_submit
            for func, serialised_if_crud in self.__callbacks[type_]:
                called = True
                # allow CRUD callbacks to not be serialised if requested
                (submit if serialised_if_crud else plain_submit)(func, *args, **kwargs)
        return called


Instruction:
Write a Python 3 script to
validate the message and return the content of the message.

Response:
def __validate_decode_msg(self, message):  # noqa (complexity) pylint: disable=too-many-return-statements,too-many-branches
        """Decodes wrapper, check hash & seq, decodes body. Returns body or None, if validation / unpack failed"""
        try:
            if not _CONTENT_TYPE_PATTERN.match(message.content_type):
                logger.debug('Message with unexpected content type %s from container, ignoring', message.content_type)
                return None
        except AttributeError:
            logger.debug('Message without content type from container, ignoring')
            return None

        # Decode & check message wrapper
        try:
            body = ubjloadb(message.body)
        except:
            logger.warning('Failed to decode message wrapper, ignoring', exc_info=DEBUG_ENABLED)
            return None
        if not self.__valid_msg_wrapper(body):
            logger.warning('Invalid message wrapper, ignoring')
            return None

        # currently only warn although maybe this should be an error
        if self.__cnt_seqnum != -1 and not self.__valid_seqnum(body[W_SEQ], self.__cnt_seqnum):
            logger.warning('Unexpected seqnum from container: %d (last seen: %d)', body[W_SEQ],
                           self.__cnt_seqnum)
        self.__cnt_seqnum = body[W_SEQ]

        # Check message hash
        if not self.__check_hash(body):
            logger.warning('Message has invalid hash, ignoring')
            return None

        # Decompress inner message
        try:
            msg = COMPRESSORS[body[W_COMPRESSION]].decompress(body[W_MESSAGE])
        except KeyError:
            logger.warning('Received message with unknown compression: %s', body[W_COMPRESSION])
            return None
        except OversizeException as ex:
            logger.warning('Uncompressed message exceeds %d bytes, ignoring', ex.size, exc_info=DEBUG_ENABLED)
            return None
        except:
            logger.warning('Decompression failed, ignoring message', exc_info=DEBUG_ENABLED)
            return None

        # Decode inner message
        try:
            msg = ubjloadb(msg, object_pairs_hook=OrderedDict)
        except:
            logger.warning('Failed to decode message, ignoring', exc_info=DEBUG_ENABLED)
            return None

        if self.__valid_msg_body(msg):
            return (msg, body[W_SEQ])
        else:
            logger.warning('Message with invalid body, ignoring: %s', msg)
            return None


Instruction:
Write a Python 3 function that can
dispatch a message to the appropriate handler.

Response:
def __dispatch_msg(self, message):
        """Verify the signature and update RequestEvents / perform callbacks

        Note messages with an invalid wrapper, invalid hash, invalid sequence number or unexpected clientRef
        will be sent to debug_bad callback.
        """
        msg = self.__validate_decode_msg(message)
        if msg:
            msg, seqnum = msg
        else:
            self.__fire_callback(_CB_DEBUG_BAD, message.body, message.content_type)
            return

        if DEBUG_ENABLED:
            logger.debug(decode_rcvd_msg('decode_rcvd_msg', msg, seqnum))
        self.__fire_callback(_CB_DEBUG_RCVD, msg)

        # no reference, or set by client (not container)
        if msg[M_TYPE] not in _RSP_CONTAINER_REF:
            # solicitied
            if msg[M_CLIENTREF]:
                if not self.__handle_known_solicited(msg):
                    logger.debug('Ignoring response for unknown request %s of type %s', msg[M_CLIENTREF], msg[M_TYPE])
            # unsolicitied
            else:
                self.__perform_unsolicited_callbacks(msg)

        # unsolicited but can have reference set by container
        elif msg[M_TYPE] == E_CONTROLREQ:
            self.__handle_controlreq(msg[M_PAYLOAD], msg[M_CLIENTREF])

        else:
            logger.error('Unhandled unsolicited message of type %s', msg[M_TYPE])


Instruction:
How would you explain what the following Python 3 function does
def __handle_known_solicited(self, msg):
        with self.__requests:
            try:
                req = self.__requests[msg[M_CLIENTREF]]
            except KeyError:
                return False

            if self.__handle_low_seq_resend(msg, req):
                return True

            perform_cb = finish = False
            if msg[M_TYPE] not in _RSP_NO_REF:
                self.__update_existing(msg, req)
                # Finalise request if applicable (not marked as finished here so can perform callback first below)
                if msg[M_TYPE] in _RSP_TYPE_FINISH:
                    finish = True
                    # Exception - DUPLICATED also should produce callback
                    perform_cb = (msg[M_TYPE] == E_DUPLICATED)
                elif msg[M_TYPE] not in _RSP_TYPE_ONGOING:
                    perform_cb = True
            else:
                logger.warning('Reference unexpected for request %s of type %s', msg[M_CLIENTREF],
                               msg[M_TYPE])

        # outside lock to avoid deadlock if callbacks try to perform request-related functions
        if perform_cb:
            self.__perform_unsolicited_callbacks(msg)

        # mark request as finished
        if finish:
            req.success = msg[M_TYPE] in _RSP_TYPE_SUCCESS
            req.payload = msg[M_PAYLOAD]
            self.__clear_references(req)
            # Serialise completion of CRUD requests (together with CREATED, DELETED, etc. messages)
            if req.is_crud:
                self.__crud_threadpool.submit(req._set)
            else:
                req._set()

        return True

Response: returns True if message has been handled as a solicited response False otherwise


Instruction:
How would you code a function in Python 3 to
remove any internal references to the given request.

Response:
def __clear_references(self, request, remove_request=True):
        """Remove any internal references to the given request"""
        # remove request itself
        if remove_request:
            with self.__requests:
                self.__requests.pop(request.id_)
        # remove request type specific references
        if not request.success:
            with self.__pending_subs:
                self.__pending_subs.pop(request.id_, None)
            with self.__pending_controls:
                self.__pending_controls.pop(request.id_, None)


Instruction:
Implement a function in Python 3 to
propagate changes based on type of message. Performs additional information actions when solicited messages arrive.

Response:
def __update_existing(self, msg, req):
        """Propagate changes based on type of message. MUST be called within self.__requests lock. Performs additional
           actions when solicited messages arrive."""
        req._messages.append(msg)

        payload = msg[M_PAYLOAD]
        if msg[M_TYPE] in _RSP_TYPE_CREATION:
            if payload[P_RESOURCE] == R_SUB:
                # Add callback for feeddata
                with self.__pending_subs:
                    if msg[M_CLIENTREF] in self.__pending_subs:
                        callback = self.__pending_subs.pop(msg[M_CLIENTREF])
                        if payload[P_POINT_TYPE] == R_FEED:
                            self.__callbacks[_CB_FEED][payload[P_POINT_ID]] = callback
                        else:
                            logger.warning('Subscription intended to feed is actually control: %s', payload[P_POINT_ID])

            elif payload[P_RESOURCE] == R_CONTROL:
                with self.__pending_controls:
                    if msg[M_CLIENTREF] in self.__pending_controls:
                        # callbacks by thing
                        entity_point_callbacks = self.__callbacks[_CB_CONTROL].setdefault(payload[P_ENTITY_LID], {})
                        # callback by thing and point
                        entity_point_callbacks[payload[P_LID]] = self.__pending_controls.pop(msg[M_CLIENTREF])

        elif msg[M_TYPE] == E_RECENTDATA:
            samples = []
            for sample in payload[P_SAMPLES]:
                data, mime, time = self.__decode_data_time(sample)
                samples.append({'data': data, 'mime': mime, 'time': time})
            self.__fire_callback(_CB_RECENT_DATA, {'c': msg[M_CLIENTREF],
                                                   'samples': samples})


Instruction:
How would you explain what the following Python 3 function does
def __handle_low_seq_resend(self, msg, req):
        if msg[M_TYPE] == E_FAILED and msg[M_PAYLOAD][P_CODE] == E_FAILED_CODE_LOWSEQNUM:
            with self.__seqnum_lock:
                self.__seqnum = int(msg[M_PAYLOAD][P_MESSAGE])
            # return value indicating shutdown not useful here since this is run in receiver thread
            self.__retry_enqueue(PreparedMessage(req._inner_msg_out, req.id_))
            return True
        return False

Response: special error case - low sequence number resend


Instruction:
Explain what the following Python 3 code does
def __decode_data_time(self, payload):
        data, mime = self.__bytes_to_share_data(payload)
        try:
            time = datetime.strptime(payload.get(P_TIME), self.__share_time_fmt)
        except (ValueError, TypeError):
            logger.warning('Share payload from container has invalid timestamp (%s), will use naive local time',
                           payload.get(P_TIME))
            time = datetime.utcnow()
        return data, mime, time

Response: Extract time and decode payload based on mime type. Applies to E_FEEDDATA and E_RECENTDATA.


Instruction:
Can you implement a function in Python 3 that
performs unsolicited callbacks for the given message.

Response:
def __perform_unsolicited_callbacks(self, msg):
        """Callbacks for which a client reference is either optional or does not apply at all"""
        type_ = msg[M_TYPE]
        payload = msg[M_PAYLOAD]

        # callbacks for responses which might be unsolicited (e.g. created or deleted)
        if type_ in _RSP_PAYLOAD_CB_MAPPING:
            self.__fire_callback(_RSP_PAYLOAD_CB_MAPPING[type_], msg)

        # Perform callbacks for feed data
        elif type_ == E_FEEDDATA:
            self.__simulate_feeddata(payload[P_FEED_ID], *self.__decode_data_time(payload))

        # Perform callbacks for unsolicited subscriber message
        elif type_ == E_SUBSCRIBED:
            self.__fire_callback(_CB_SUBSCRIPTION, payload)

        else:
            logger.error('Unexpected message type for unsolicited callback %s', type_)


Instruction:
Implement a function in Python 3 to
set the roles for the current launch.

Response:
def roles(self, roles_list):
        '''
        Set the roles for the current launch.

        Full list of roles can be found here:
        http://www.imsglobal.org/LTI/v1p1/ltiIMGv1p1.html#_Toc319560479

        LIS roles include:
        * Student
        * Faculty
        * Member
        * Learner
        * Instructor
        * Mentor
        * Staff
        * Alumni
        * ProspectiveStudent
        * Guest
        * Other
        * Administrator
        * Observer
        * None
        '''
        if roles_list and isinstance(roles_list, list):
            self.roles = [].extend(roles_list)
        elif roles_list and isinstance(roles_list, basestring):
            self.roles = [role.lower() for role in roles_list.split(',')]


Instruction:
Can you create a Python 3 function that
populates the launch data from a dictionary. Only cares about keys custom_ or ext_.

Response:
def process_params(self, params):
        '''
        Populates the launch data from a dictionary. Only cares about keys in
        the LAUNCH_DATA_PARAMETERS list, or that start with 'custom_' or
        'ext_'.
        '''
        for key, val in params.items():
            if key in LAUNCH_DATA_PARAMETERS and val != 'None':
                if key == 'roles':
                    if isinstance(val, list):
                        # If it's already a list, no need to parse
                        self.roles = list(val)
                    else:
                        # If it's a ',' delimited string, split
                        self.roles = val.split(',')
                else:
                    setattr(self, key, touni(val))
            elif 'custom_' in key:
                self.custom_params[key] = touni(val)
            elif 'ext_' in key:
                self.ext_params[key] = touni(val)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_params(self):
        '''
        Createa a new dictionary with all launch data. Custom / Extension keys
        will be included. Roles are set as a ',' separated string.
        '''
        params = {}
        custom_params = {}
        for key in self.custom_params:
            custom_params[key] = self.custom_params[key]
        ext_params = {}
        for key in self.ext_params:
            ext_params[key] = self.ext_params[key]
        for key in LAUNCH_DATA_PARAMETERS:
            if hasattr(self, key):
                params[key] = getattr(self, key)
        params.update(custom_params)
        params.update(ext_params)
        return params

Response:
Createa a new dictionary with all launch data. Custom and Extension keys
        will be included. Roles are set as a comma separated string.


Instruction:
Can you create a Python 3 function that
writes a string up to 2 ** 32 bytes long after encoding.

Response:
def write_longstr(self, s):
        """Write a string up to 2**32 bytes long after encoding.

        If passed a unicode string, encode as UTF-8.

        """
        self._flushbits()
        if isinstance(s, text_t):
            s = s.encode('utf-8')
        self.write_long(len(s))
        self.out.write(s)


Instruction:
Can you generate the documentation for the following Python 3 function
def run(self, background=False):
        if self.__bgthread:
            raise Exception('run has already been called (since last stop)')
        self.__shutdown.clear()
        if background:
            self.__bgthread = Thread(target=self.__run, name=('bg_' + self.__client.agent_id))
            self.__bgthread.daemon = True
            self.__bgthread.start()
        else:
            self.__run()

Response: Runs on_startup main and on_shutdown blocking until finished.


Instruction:
Write a Python 3 script to
request device to stop running waiting at most the given timeout in seconds. Returns True if successfully stopped False otherwise.

Response:
def stop(self, timeout=None):
        """Requests device to stop running, waiting at most the given timout in seconds (fractional). Has no effect if
        `run()` was not called with background=True set. Returns True if successfully stopped (or already not running).
        """
        stopped = True
        self.__shutdown.set()
        if self.__bgthread:
            logger.debug('Stopping bgthread')
            self.__bgthread.join(timeout)
            if self.__bgthread.is_alive():
                logger.warning('bgthread did not finish within timeout')
                stopped = False
            self.__bgthread = None
        return stopped


Instruction:
Here you have a function in Python 3, explain what it does
def add_message_to_queue(self, payload, **attributes):

        return self.queue.send_message(MessageBody=json.dumps(payload),
                                       MessageAttributes={k: {"StringValue": v} for k, v in attributes.items()})

Response: Given a payload and any optional message attributes add it to the queue.


Instruction:
Implement a Python 3 function for
returning the first num_messages messages from the queue.

Response:
def receive_messages_from_queue(self, wait_time=15, num_messages=1):
        """ Returns the first (according to FIFO) element in the queue; if none, then returns None."""

        return self.queue.receive_messages(MaxNumberOfMessages=num_messages,
                                           WaitTimeSeconds=wait_time)


Instruction:
Given the following Python 3 function, write the documentation
def save_json(self, jsonfn=None, pretty=True):
        if jsonfn is None:
            jsonfn = os.path.join(os.getcwd(), 'units.json')
        #
        jsondump = None
        sort_keys = False
        indent = 0
        if pretty:
            sort_keys = True
            indent = 4
        jsondump = json.dumps(self.units, sort_keys=sort_keys, indent=indent)
        #
        with open(jsonfn, 'w') as f:
            f.write(jsondump)
            return True
        return False

Response: Write a. json file with the units tree containing the units


Instruction:
Explain what the following Python 3 code does
def accumulate_metric(cls, name, value):
        metrics_cache = cls._get_metrics_cache()
        metrics_cache.setdefault(name, 0)
        metrics_cache.set(name, value)

Response: Accumulate a custom metric name and value in the metrics cache.


Instruction:
Explain what the following Python 3 code does
def _batch_report(cls, request):
        if not newrelic:
            return
        metrics_cache = cls._get_metrics_cache()
        try:
            newrelic.agent.add_custom_parameter('user_id', request.user.id)
        except AttributeError:
            pass
        for key, value in metrics_cache.data.items():
            newrelic.agent.add_custom_parameter(key, value)

Response: Report the collected custom metrics to New Relic.


Instruction:
Implement a function in Python 3 to
store memory data to log later.

Response:
def process_request(self, request):
        """
        Store memory data to log later.
        """
        if self._is_enabled():
            self._cache.set(self.guid_key, six.text_type(uuid4()))
            log_prefix = self._log_prefix(u"Before", request)
            self._cache.set(self.memory_data_key, self._memory_data(log_prefix))


Instruction:
Write a Python 3 script for
logging memory data after processing response.

Response:
def process_response(self, request, response):
        """
        Logs memory data after processing response.
        """
        if self._is_enabled():
            log_prefix = self._log_prefix(u"After", request)
            new_memory_data = self._memory_data(log_prefix)

            log_prefix = self._log_prefix(u"Diff", request)
            cached_memory_data_response = self._cache.get_cached_response(self.memory_data_key)
            old_memory_data = cached_memory_data_response.get_value_or_default(None)
            self._log_diff_memory_data(log_prefix, new_memory_data, old_memory_data)
        return response


Instruction:
Can you create a Python 3 function that
returns a formatted prefix for logging for the given request.

Response:
def _log_prefix(self, prefix, request):
        """
        Returns a formatted prefix for logging for the given request.
        """
        # After a celery task runs, the request cache is cleared. So if celery
        # tasks are running synchronously (CELERY_ALWAYS _EAGER), "guid_key"
        # will no longer be in the request cache when process_response executes.
        cached_guid_response = self._cache.get_cached_response(self.guid_key)
        cached_guid = cached_guid_response.get_value_or_default(u"without_guid")
        return u"{} request '{} {} {}'".format(prefix, request.method, request.path, cached_guid)


Instruction:
Write a Python 3 function for
returning a dict with information for current memory utilization.

Response:
def _memory_data(self, log_prefix):
        """
        Returns a dict with information for current memory utilization.
        Uses log_prefix in log statements.
        """
        machine_data = psutil.virtual_memory()

        process = psutil.Process()
        process_data = {
            'memory_info': process.get_memory_info(),
            'ext_memory_info': process.get_ext_memory_info(),
            'memory_percent': process.get_memory_percent(),
            'cpu_percent': process.get_cpu_percent(),
        }

        log.info(u"%s Machine memory usage: %s; Process memory usage: %s", log_prefix, machine_data, process_data)
        return {
            'machine_data': machine_data,
            'process_data': process_data,
        }