Instruction:
Explain what the following Python 3 code does
def command_loop(self, run_script_event):
        while True:
            obj = remote_client.receive(self)
            command = obj["command"]
            # TODO: ensure we always have a command if receive returns
            args = obj.get('args', {})

            if command == 'getBreakpoints':
                breakpoints_list = IKBreakpoint.get_breakpoints_list()
                remote_client.reply(obj, breakpoints_list)
                _logger.b_debug("getBreakpoints(%s) => %s", args, breakpoints_list)

            elif command == "setBreakpoint":
                # Set a new breakpoint. If the lineno line doesn't exist for the
                # filename passed as argument, return an error message.
                # The filename should be in canonical form, as described in the
                # canonic() method.
                file_name = args['file_name']
                line_number = args['line_number']
                condition = args.get('condition', None)
                enabled = args.get('enabled', True)
                _logger.b_debug("setBreakpoint(file_name=%s, line_number=%s,"
                                " condition=%s, enabled=%s) with CWD=%s",
                                file_name,
                                line_number,
                                condition,
                                enabled,
                                os.getcwd())

                error_messages = []
                result = {}

                c_file_name = self.normalize_path_in(file_name)
                if not c_file_name:
                    err = "Failed to find file '%s'" % file_name
                    _logger.g_error("setBreakpoint error: %s", err)
                    msg = "IKP3db error: Failed to set a breakpoint at %s:%s "\
                          "(%s)." % (file_name, line_number, err)
                    error_messages = [msg]
                    command_exec_status = 'error'
                else:
                    err, bp_number = self.set_breakpoint(c_file_name,
                                                         line_number,
                                                         condition=condition,
                                                         enabled=enabled)
                    if err:
                        _logger.g_error("setBreakpoint error: %s", err)
                        msg = "IKP3db error: Failed to set a breakpoint at %s:%s "\
                              "(%s)." % (file_name, line_number, err,)
                        error_messages = [msg]
                        command_exec_status = 'error'
                    else:
                        result = {'breakpoint_number': bp_number}
                        command_exec_status = 'ok'
                remote_client.reply(obj, result,
                                    command_exec_status=command_exec_status,
                                    error_messages=error_messages)

            elif command == "changeBreakpointState":
                # Allows to:
                #  - activate or deactivate breakpoint
                #  - set or remove condition
                _logger.b_debug("changeBreakpointState(%s)", args)
                bp_number = args.get('breakpoint_number', None)
                if bp_number is None:
                    result = {}
                    msg = "changeBreakpointState() error: missing required " \
                          "breakpointNumber parameter."
                    _logger.g_error("    "+msg)
                    error_messages = [msg]
                    command_exec_status = 'error'
                else:
                    err = self.change_breakpoint_state(bp_number,
                                                       args.get('enabled', False),
                                                       condition=args.get('condition', ''))
                    result = {}
                    error_messages = []
                    if err:
                        msg = "changeBreakpointState() error: \"%s\"" % err
                        _logger.g_error("    "+msg)
                        error_messages = [msg]
                        command_exec_status = 'error'
                    else:
                        command_exec_status = 'ok'
                remote_client.reply(obj, result,
                                    command_exec_status=command_exec_status,
                                    error_messages=error_messages)
                _logger.b_debug("    command_exec_status => %s", command_exec_status)

            elif command == "clearBreakpoint":
                _logger.b_debug("clearBreakpoint(%s)", args)
                bp_number = args.get('breakpoint_number', None)
                if bp_number is None:
                    result = {}
                    msg = "IKP3db error: Failed to delete breakpoint (Missing "\
                          "required breakpointNumber parameter)."
                    error_messages = [msg]
                    command_exec_status = 'error'
                else:
                    err = self.clear_breakpoint(args['breakpoint_number'])
                    result = {}
                    error_messages = []
                    if err:
                        msg = "IKP3db error: Failed to delete breakpoint (%s)." % err
                        _logger.g_error(msg)
                        error_messages = [msg]
                        command_exec_status = 'error'
                    else:
                        command_exec_status = 'ok'
                remote_client.reply(obj, result,
                                    command_exec_status=command_exec_status,
                                    error_messages=error_messages)

            elif command == 'runScript':
                #TODO: handle a 'stopAtEntry' arg
                _logger.x_debug("runScript(%s)", args)
                remote_client.reply(obj, {'executionStatus': 'running'})
                run_script_event.set()

            elif command == 'suspend':
                _logger.x_debug("suspend(%s)", args)
                # We return a running status which is True at that point. Next
                # programBreak will change status to 'stopped'
                remote_client.reply(obj, {'executionStatus': 'running'})
                self.setup_suspend()

            elif command == 'resume':
                _logger.x_debug("resume(%s)", args)
                remote_client.reply(obj, {'executionStatus': 'running'})
                self._command_q.put({'cmd':'resume'})

            elif command == 'stepOver':  # <=> Pdb n(ext)
                _logger.x_debug("stepOver(%s)", args)
                remote_client.reply(obj, {'executionStatus': 'running'})
                self._command_q.put({'cmd':'stepOver'})

            elif command == 'stepInto':  # <=> Pdb s(tep)
                _logger.x_debug("stepInto(%s)", args)
                remote_client.reply(obj, {'executionStatus': 'running'})
                self._command_q.put({'cmd':'stepInto'})

            elif command == 'stepOut':  # <=> Pdb r(eturn)
                _logger.x_debug("stepOut(%s)", args)
                remote_client.reply(obj, {'executionStatus': 'running'})
                self._command_q.put({'cmd':'stepOut'})

            elif command == 'evaluate':
                _logger.e_debug("evaluate(%s)", args)
                if self.tracing_enabled and self.status == 'stopped':
                    self._command_q.put({
                        'cmd':'evaluate',
                        'obj': obj,
                        'frame': args['frame'],
                        'expression': args['expression'],
                        'global': args['global'],
                        'disableBreak': args['disableBreak']
                    })
                    # reply will be done in _tracer() where result is available
                else:
                    remote_client.reply(obj, {'value': None, 'type': None})

            elif command == 'getProperties':
                _logger.e_debug("getProperties(%s,%s)", args, obj)
                if self.tracing_enabled and self.status == 'stopped':
                    if args.get('id'):
                        self._command_q.put({
                            'cmd':'getProperties',
                            'obj': obj,
                            'id': args['id']
                        })
                        # reply will be done in _tracer() when result is available
                    else:
                        result={}
                        command_exec_status = 'error'
                        error_messages = ["IKP3db received getProperties command sent without target variable 'id'."]
                        remote_client.reply(obj, result,
                                            command_exec_status=command_exec_status,
                                            error_messages=error_messages)

                else:
                    remote_client.reply(obj, {'value': None, 'type': None})

            elif command == 'setVariable':
                _logger.e_debug("setVariable(%s)", args)
                if self.tracing_enabled and self.status == 'stopped':
                    self._command_q.put({
                        'cmd':'setVariable',
                        'obj': obj,
                        'frame': args['frame'],
                        'name': args['name'],  # TODO: Rework plugin to send var's id
                        'value': args['value']
                    })
                    # reply will be done in _tracer() when result is available
                else:
                    remote_client.reply(obj, {'value': None, 'type': None})

            elif command == 'reconnect':
                _logger.n_debug("reconnect(%s)", args)
                remote_client.reply(obj, {'executionStatus': self.status})

            elif command == 'getThreads':
                _logger.x_debug("getThreads(%s)", args)
                threads_list = self.get_threads()
                remote_client.reply(obj, threads_list)

            elif command == 'setDebuggedThread':
                _logger.x_debug("setDebuggedThread(%s)", args)
                ret_val = self.set_debugged_thread(args['ident'])
                if ret_val['error']:
                    remote_client.reply(obj,
                                        {},  # result
                                        command_exec_status='error',
                                        error_messages=[ret_val['error']])

                else:
                    remote_client.reply(obj, ret_val['result'])

            elif command == '_InternalQuit':
                # '_InternalQuit' is an IKP3db internal message, generated by
                # IKPdbConnectionHandler when a socket.error occured.
                # Usually this occurs when socket has been destroyed as
                # debugged program sys.exit()
                # So we leave the command loop to stop the debugger thread
                # in order to allow debugged program to shutdown correctly.
                # This message must NEVER be send by remote client.
                _logger.e_debug("_InternalQuit(%s)", args)
                self._command_q.put({'cmd':'_InternalQuit'})
                return

            else: # unrecognized command ; just log and ignored
                _logger.g_critical("Unsupported command '%s' ignored.", command)

            if IKPdbLogger.enabled:
                _logger.b_debug("Current breakpoints list [any_active_breakpoint=%s]:",
                                IKBreakpoint.any_active_breakpoint)
                _logger.b_debug("    IKBreakpoint.breakpoints_by_file_and_line:")
                if not IKBreakpoint.breakpoints_by_file_and_line:
                    _logger.b_debug("        <empty>")
                for file_line, bp in list(IKBreakpoint.breakpoints_by_file_and_line.items()):
                    _logger.b_debug("        %s => #%s, enabled=%s, condition=%s, %s",
                                    file_line,
                                    bp.number,
                                    bp.enabled,
                                    repr(bp.condition),
                                    bp)
                _logger.b_debug("    IKBreakpoint.breakpoints_files = %s",
                                IKBreakpoint.breakpoints_files)
                _logger.b_debug("    IKBreakpoint.breakpoints_by_number = %s",
                                IKBreakpoint.breakpoints_by_number)

Response: This is the debugger command loop that processes client - specific requests.


Instruction:
Write a Python 3 script for
getting elements matching id or name

Response:
def _get_elements(self, url, key, eclass, id=None, name=None):
        """Get elements matching `id` or `name`

        Args:
            url(str): url of children.
            key(str): key in the returned JSON.
            eclass(subclass type of :py:class:`_ResourceElement`): element class to create instances of.
            id(str, optional): only return resources whose `id` property matches the given `id`
            name(str, optional): only return resources whose `name` property matches the given `name`

        Returns:
            list(_ResourceElement): List of `eclass` instances

        Raises:
            ValueError: both `id` and `name` are specified together
        """
        if id is not None and name is not None:
            raise ValueError("id and name cannot specified together")

        json_elements = self.rest_client.make_request(url)[key]
        return [eclass(element, self.rest_client) for element in json_elements
                if _exact_resource(element, id) and _matching_resource(element, name)]


Instruction:
Make a summary of the following Python 3 code
def _get_element_by_id(self, url, key, eclass, id):
        elements = self._get_elements(url, key, eclass, id=id)
        if not elements:
            raise ValueError("No resource matching: {0}".format(id))
        if len(elements) == 1:
            return elements[0]
        raise ValueError("Multiple resources matching: {0}".format(id))

Response: Get a single element matching an id


Instruction:
Implement a function in Python 3 to
get the Streams domain for the instance that owns this view.

Response:
def get_domain(self):
        """Get the Streams domain for the instance that owns this view.

        Returns:
            Domain: Streams domain for the instance owning this view.
        """
        if hasattr(self, 'domain'):
            return Domain(self.rest_client.make_request(self.domain), self.rest_client)


Instruction:
Make a summary of the following Python 3 code
def get_instance(self):
        return Instance(self.rest_client.make_request(self.instance), self.rest_client)

Response: Get the instance that owns this view.


Instruction:
How would you code a function in Python 3 to
return the Streams Job that owns this view.

Response:
def get_job(self):
        """Get the Streams job that owns this view.

        Returns:
            Job: Streams Job owning this view.
        """
        return Job(self.rest_client.make_request(self.job), self.rest_client)


Instruction:
Write a Python 3 script to
stop the thread that fetches data from the Streams view server.

Response:
def stop_data_fetch(self):
        """Stops the thread that fetches data from the Streams view server.
        """
        if self._data_fetcher:
            self._data_fetcher.stop.set()
            self._data_fetcher = None


Instruction:
Explain what the following Python 3 code does
def start_data_fetch(self):
        self.stop_data_fetch()
        self._data_fetcher = _ViewDataFetcher(self, self._tuple_fn)
        t = threading.Thread(target=self._data_fetcher)
        t.start()
        return self._data_fetcher.items

Response: Starts a thread that fetches data from the Streams view server.


Instruction:
Can you generate the documentation for the following Python 3 function
def fetch_tuples(self, max_tuples=20, timeout=None):
        tuples = list()
        if timeout is None:
            while len(tuples) < max_tuples:
                fetcher = self._data_fetcher
                if not fetcher:
                    break
                tuples.append(fetcher.items.get())
            return tuples

        timeout = float(timeout)
        end = time.time() + timeout
        while len(tuples) < max_tuples:
            qto = end - time.time()
            if qto <= 0:
                break
            try:
                fetcher = self._data_fetcher
                if not fetcher:
                    break
                tuples.append(fetcher.items.get(timeout=qto))
            except queue.Empty:
                break
        return tuples

Response: Fetch a number of tuples from this view.


Instruction:
Write a Python 3 function for
displaying a view within a Jupyter or IPython notebook cell.

Response:
def display(self, duration=None, period=2):
        """Display a view within a Jupyter or IPython notebook.

        Provides an easy mechanism to visualize data on a stream
        using a view.

        Tuples are fetched from the view and displayed in a table
        within the notebook cell using a ``pandas.DataFrame``.
        The table is continually updated with the latest tuples from the view.

        This method calls :py:meth:`start_data_fetch` and will call
        :py:meth:`stop_data_fetch` when completed if `duration` is set.

        Args:
            duration(float): Number of seconds to fetch and display tuples. If ``None`` then the display will be updated until :py:meth:`stop_data_fetch` is called.
            period(float): Maximum update period.

        .. note::
            A view is a sampling of data on a stream so tuples that
            are on the stream may not appear in the view.

        .. note::
            Python modules `ipywidgets` and `pandas` must be installed
            in the notebook environment.

        .. warning::
            Behavior when called outside a notebook is undefined.

        .. versionadded:: 1.12
        """
        import ipywidgets as widgets
        vn = widgets.Text(value=self.description, description=self.name, disabled=True)
        active = widgets.Valid(value=True, description='Fetching', readout='Stopped')
        out = widgets.Output(layout={'border': '1px solid black'})
        hb = widgets.HBox([vn, active])
        vb = widgets.VBox([hb, out])
        display(vb)
        self._display_thread = threading.Thread(target=lambda: self._display(out, duration, period, active))
        self._display_thread.start()


Instruction:
Can you write a function in Python 3 where it
gets a list of ViewItem elements associated with this view.

Response:
def get_view_items(self):
        """Get a list of :py:class:`ViewItem` elements associated with this view.

        Returns:
            list(ViewItem): List of ViewItem(s) associated with this view.
        """
        view_items = [ViewItem(json_view_items, self.rest_client) for json_view_items
                      in self.rest_client.make_request(self.viewItems)['viewItems']]
        logger.debug("Retrieved " + str(len(view_items)) + " items from view " + self.name)
        return view_items


Instruction:
Implement a Python 3 function for
retrieving the application log and trace files of the job and saves them as a compressed tar file.

Response:
def retrieve_log_trace(self, filename=None, dir=None):
        """Retrieves the application log and trace files of the job
        and saves them as a compressed tar file.

        An existing file with the same name will be overwritten.

        Args:
            filename (str): name of the created tar file. Defaults to `job_<id>_<timestamp>.tar.gz` where `id` is the job identifier and `timestamp` is the number of seconds since the Unix epoch, for example ``job_355_1511995995.tar.gz``.
            dir (str): a valid directory in which to save the archive. Defaults to the current directory.

        Returns:
            str: the path to the created tar file, or ``None`` if retrieving a job's logs is not supported in the version of IBM Streams to which the job is submitted.

        .. versionadded:: 1.8
        """

        if hasattr(self, "applicationLogTrace") and self.applicationLogTrace is not None:
            logger.debug("Retrieving application logs from: " + self.applicationLogTrace)
            if not filename:
                filename = _file_name('job', self.id, '.tar.gz')

            return self.rest_client._retrieve_file(self.applicationLogTrace, filename, dir, 'application/x-compressed')
        else:
            return None


Instruction:
Explain what the following Python 3 code does
def get_views(self, name=None):
        return self._get_elements(self.views, 'views', View, name=name)

Response: Get the list of views associated with this job.


Instruction:
Implement a function in Python 3 to
get the list of Operator elements associated with this job.

Response:
def get_operators(self, name=None):
        """Get the list of :py:class:`Operator` elements associated with this job.

        Args:
            name(str): Only return operators matching `name`, where `name` can be a regular expression.  If
                `name` is not supplied, then all operators for this job are returned.

        Returns:
            list(Operator): List of Operator elements associated with this job.

        Retrieving a list of operators whose name contains the string "temperatureSensor" could be performed as followed
        Example:
            >>> from streamsx import rest
            >>> sc = rest.StreamingAnalyticsConnection()
            >>> instances = sc.get_instances()
            >>> job = instances[0].get_jobs()[0]
            >>> operators = job.get_operators(name="*temperatureSensor*")

        .. versionchanged:: 1.9 `name` parameter added.
        """
        return self._get_elements(self.operators, 'operators', Operator, name=name)


Instruction:
Create a Python 3 function for
canceling this job. Args: force (bool, optional): Forcefully cancel this job. Returns: bool: True if the job was cancelled, otherwise False if an error occurred.

Response:
def cancel(self, force=False):
        """Cancel this job.

        Args:
            force (bool, optional): Forcefully cancel this job.

        Returns:
            bool: True if the job was cancelled, otherwise False if an error occurred.
        """
        return self.rest_client._sc._delegator._cancel_job(self, force)


Instruction:
Write a Python 3 script for
getting metrics for this operator.

Response:
def get_metrics(self, name=None):
        """Get metrics for this operator.

        Args:
            name(str, optional): Only return metrics matching `name`, where `name` can be a regular expression.  If
                `name` is not supplied, then all metrics for this operator are returned.

        Returns:
             list(Metric): List of matching metrics.

        Retrieving a list of metrics whose name contains the string "temperatureSensor" could be performed as followed
        Example:
            >>> from streamsx import rest
            >>> sc = rest.StreamingAnalyticsConnection()
            >>> instances = sc.get_instances()
            >>> operator = instances[0].get_operators()[0]
            >>> metrics = op.get_metrics(name='*temperatureSensor*')
        """
        return self._get_elements(self.metrics, 'metrics', Metric, name=name)


Instruction:
Here you have a function in Python 3, explain what it does
def get_host(self):
        if hasattr(self, 'host') and self.host:
            return Host(self.rest_client.make_request(self.host), self.rest_client)

Response: Get the resource this operator is currently executing in.


Instruction:
Make a summary of the following Python 3 code
def get_pe(self):
        return PE(self.rest_client.make_request(self.pe), self.rest_client)

Response: Get the processing element this operator is executing in.


Instruction:
Write a Python 3 function that can
retrieve the application trace files for this PE and saves them as plain text files.

Response:
def retrieve_trace(self, filename=None, dir=None):
        """Retrieves the application trace files for this PE
        and saves them as a plain text file.

        An existing file with the same name will be overwritten.

        Args:
            filename (str): name of the created file. Defaults to `pe_<id>_<timestamp>.trace` where `id` is the PE identifier and `timestamp` is the number of seconds since the Unix epoch, for example ``pe_83_1511995995.trace``.
            dir (str): a valid directory in which to save the file. Defaults to the current directory.

        Returns:
            str: the path to the created file, or None if retrieving a job's logs is not supported in the version of streams to which the job is submitted.

        .. versionadded:: 1.9
        """
        if hasattr(self, "applicationTrace") and self.applicationTrace is not None:
            logger.debug("Retrieving PE trace: " + self.applicationTrace)
            if not filename:
                filename = _file_name('pe', self.id, '.trace')
            return self.rest_client._retrieve_file(self.applicationTrace, filename, dir, 'text/plain')

        else:
            return None


Instruction:
Make a summary of the following Python 3 code
def retrieve_console_log(self, filename=None, dir=None):
        if hasattr(self, "consoleLog") and self.consoleLog is not None:
            logger.debug("Retrieving PE console log: " + self.consoleLog)
            if not filename:
                filename = _file_name('pe', self.id, '.stdouterr')
            return self.rest_client._retrieve_file(self.consoleLog, filename, dir, 'text/plain')

        else:
            return None

Response: Retrieves the application console log for this PE and saves them as plain text files.


Instruction:
Can you tell what is the following Python 3 function doing
def get_resource_allocation(self):
        if hasattr(self, 'resourceAllocation'):
            return ResourceAllocation(self.rest_client.make_request(self.resourceAllocation), self.rest_client)

Response: Get the : py : class : ResourceAllocation element tance.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_resource(self):
        return Resource(self.rest_client.make_request(self.resource), self.rest_client)

Response: Get the resource of the resource allocation.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_jobs(self, name=None):
        if self.applicationResource:
            return self._get_elements(self.jobs, 'jobs', Job, None, name)
        else:
            return []

Response: Retrieves jobs running on this resource in its instance.


Instruction:
How would you explain what the following Python 3 function does
def get_operator_output_port(self):
        return OperatorOutputPort(self.rest_client.make_request(self.operatorOutputPort), self.rest_client)

Response: Returns the OperatorOutputPort object for this exported stream.


Instruction:
Can you create a Python 3 function that
connects to an IBM Streams service instance.

Response:
def of_service(config):
        """Connect to an IBM Streams service instance running in IBM Cloud Private for Data.

        The instance is specified in `config`. The configuration may be code injected from the list of services
        in a Jupyter notebook running in ICPD or manually created. The code that selects a service instance by name is::

            # Two lines are code injected in a Jupyter notebook by selecting the service instance
            from icpd_core import ipcd_util
            cfg = icpd_util.get_service_details(name='instanceName')

            instance = Instance.of_service(cfg)

        SSL host verification is disabled by setting :py:const:`~streamsx.topology.context.ConfigParams.SSL_VERIFY`
        to ``False`` within `config` before calling this method::

            cfg[ConfigParams.SSL_VERIFY] = False
            instance = Instance.of_service(cfg)

        Args:
            config(dict): Configuration of IBM Streams service instance.

        Returns:
            Instance: Instance representing for IBM Streams service instance.

        .. note:: Only supported when running within the ICPD cluster,
            for example in a Jupyter notebook within a ICPD project.

        .. versionadded:: 1.12
        """
        service = Instance._find_service_def(config)
        if not service:
            raise ValueError()
        endpoint = service['connection_info'].get('serviceRestEndpoint')
        resource_url, name = Instance._root_from_endpoint(endpoint)

        sc = streamsx.rest.StreamsConnection(resource_url=resource_url, auth=_ICPDAuthHandler(name, service['service_token']))
        if streamsx.topology.context.ConfigParams.SSL_VERIFY in config:
                sc.session.verify = config[streamsx.topology.context.ConfigParams.SSL_VERIFY]
        return sc.get_instance(name)


Instruction:
Write a Python 3 function for
retrieving a job matching the given id

Response:
def get_job(self, id):
        """Retrieves a job matching the given `id`

        Args:
            id (str): Job `id` to match.

        Returns:
            Job: Job matching the given `id`

        Raises:
            ValueError: No resource matches given `id` or multiple resources matching given `id`
        """
        return self._get_element_by_id(self.jobs, 'jobs', Job, str(id))


Instruction:
Write a Python 3 script to
get a list of currently published topics for this instance.

Response:
def get_published_topics(self):
        """Get a list of published topics for this instance.

        Streams applications publish streams to a a topic that can be subscribed to by other
        applications. This allows a microservice approach where publishers
        and subscribers are independent of each other.

        A published stream has a topic and a schema. It is recommended that a
        topic is only associated with a single schema.

        Streams may be published and subscribed by applications regardless of the
        implementation language. For example a Python application can publish
        a stream of JSON tuples that are subscribed to by SPL and Java applications.

        Returns:
             list(PublishedTopic): List of currently published topics.
        """
        published_topics = []
        # A topic can be published multiple times
        # (typically with the same schema) but the
        # returned list only wants to contain a topic,schema
        # pair once. I.e. the list of topics being published is
        # being returned, not the list of streams.
        seen_topics = {}
        for es in self.get_exported_streams():
            pt = es._as_published_topic()
            if pt is not None:
                if pt.topic in seen_topics:
                    if pt.schema is None:
                        continue
                    if pt.schema in seen_topics[pt.topic]:
                        continue
                    seen_topics[pt.topic].append(pt.schema)
                else:
                    seen_topics[pt.topic] = [pt.schema]
                published_topics.append(pt)

        return published_topics


Instruction:
How would you code a function in Python 3 to
retrieve application configurations for this instance.

Response:
def get_application_configurations(self, name=None):
        """Retrieves application configurations for this instance.

        Args:
            name (str, optional): Only return application configurations containing property **name** that matches `name`. `name` can be a
                regular expression. If `name` is not supplied, then all application configurations are returned.

        Returns:
            list(ApplicationConfiguration): A list of application configurations matching the given `name`.

        .. versionadded 1.12
        """
        if hasattr(self, 'applicationConfigurations'):
           return self._get_elements(self.applicationConfigurations, 'applicationConfigurations', ApplicationConfiguration, None, name)


Instruction:
Explain what the following Python 3 code does
def create_application_configuration(self, name, properties, description=None):
        if not hasattr(self, 'applicationConfigurations'):
            raise NotImplementedError()

        cv = ApplicationConfiguration._props(name, properties, description)

        res = self.rest_client.session.post(self.applicationConfigurations,
            headers = {'Accept' : 'application/json'},
            json=cv)
        _handle_http_errors(res)
        return ApplicationConfiguration(res.json(), self.rest_client)

Response: Create an application configuration.


Instruction:
Write a Python 3 function for
submitting a new application bundle to this Streaming Analytics service.

Response:
def submit_job(self, bundle, job_config=None):
        """Submit a Streams Application Bundle (sab file) to
        this Streaming Analytics service.

        Args:
            bundle(str): path to a Streams application bundle (sab file)
                containing the application to be submitted
            job_config(JobConfig): a job configuration overlay

        Returns:
            dict: JSON response from service containing 'name' field with unique
                job name assigned to submitted job, or, 'error_status' and
                'description' fields if submission was unsuccessful.
        """
        return self._delegator._submit_job(bundle=bundle, job_config=job_config)


Instruction:
How would you code a function in Python 3 to
cancel a running job.

Response:
def cancel_job(self, job_id=None, job_name=None):
        """Cancel a running job.

        Args:
            job_id (str, optional): Identifier of job to be canceled.
            job_name (str, optional): Name of job to be canceled.

        Returns:
            dict: JSON response for the job cancel operation.
        """
        return self._delegator.cancel_job(job_id=job_id, job_name = job_name)


Instruction:
Can you tell what is the following Python 3 function doing
def _get_jobs_url(self):
        if self._jobs_url is None:
            self.get_instance_status()
            if self._jobs_url is None:
                raise ValueError("Cannot obtain jobs URL")
        return self._jobs_url

Response: Get & save jobs URL from the status call.


Instruction:
How would you implement a function in Python 3 that
cancels a running job.

Response:
def cancel_job(self, job_id=None, job_name=None):
        """Cancel a running job.

        Args:
            job_id (str, optional): Identifier of job to be canceled.
            job_name (str, optional): Name of job to be canceled.

        Returns:
            dict: JSON response for the job cancel operation.
        """
        payload = {}
        if job_name is not None:
            payload['job_name'] = job_name
        if job_id is not None:
            payload['job_id'] = job_id

        jobs_url = self._get_url('jobs_path')
        res = self.rest_client.session.delete(jobs_url, params=payload)
        _handle_http_errors(res)
        return res.json()


Instruction:
Can you tell what is the following Python 3 function doing
def start_instance(self):
        start_url = self._get_url('start_path')
        res = self.rest_client.session.put(start_url, json={})
        _handle_http_errors(res)
        return res.json()

Response: Start the instance for this Streaming Analytics service.


Instruction:
How would you implement a function in Python 3 that
stops the instance for this Streaming Analytics service.

Response:
def stop_instance(self):
        """Stop the instance for this Streaming Analytics service.

        Returns:
            dict: JSON response for the instance stop operation.
        """
        stop_url = self._get_url('stop_path')
        res = self.rest_client.session.put(stop_url, json={})
        _handle_http_errors(res)
        return res.json()


Instruction:
Make a summary of the following Python 3 code
def get_instance_status(self):
        status_url = self._get_url('status_path')
        res = self.rest_client.session.get(status_url)
        _handle_http_errors(res)
        return res.json()

Response: Get the status of the instance for this Streaming Analytics service.


Instruction:
How would you implement a function in Python 3 that
submits this Streams Application Bundle to its associated instance.

Response:
def submit_job(self, job_config=None):
        """Submit this Streams Application Bundle (sab file) to
        its associated instance.

        Args:
            job_config(JobConfig): a job configuration overlay

        Returns:
            Job: Resulting job instance.
        """
        job_id = self._delegator._submit_bundle(self, job_config)
        return self._instance.get_job(job_id)


Instruction:
Implement a Python 3 function for
canceling the job using streamtool.

Response:
def _cancel_job(self, job, force):
        """Cancel job using streamtool."""
        import streamsx.st as st
        if st._has_local_install:
            return st._cancel_job(job.id, force,
                domain_id=job.get_instance().get_domain().id, instance_id=job.get_instance().id)
        return False


Instruction:
Implement a Python 3 function for
updating this application configuration.

Response:
def update(self, properties=None, description=None):
        """Update this application configuration.

        To create or update a property provide its key-value
        pair in `properties`.

        To delete a property provide its key with the value ``None``
        in properties.

        Args:
            properties (dict): Property values to be updated. If ``None`` the properties are unchanged.
            description (str): Description for the configuration. If ``None`` the description is unchanged.

        Returns:
            ApplicationConfiguration: self
        """
        cv = ApplicationConfiguration._props(properties=properties, description=description)
        res = self.rest_client.session.patch(self.rest_self,
            headers = {'Accept' : 'application/json',
                       'Content-Type' : 'application/json'},
            json=cv)
        _handle_http_errors(res)
        self.json_rep = res.json()
        return self


Instruction:
Make a summary of the following Python 3 code
def delete(self):
        res = self.rest_client.session.delete(self.rest_self)
        _handle_http_errors(res)

Response: Delete this application configuration.


Instruction:
Make a summary of the following Python 3 code
def is_common(schema):
    if isinstance(schema, StreamSchema):
        return schema.schema() in _SCHEMA_COMMON
    if isinstance(schema, CommonSchema):
        return True
    if isinstance(schema, basestring):
        return is_common(StreamSchema(schema))
    return False

Response: Is schema an common schema?


Instruction:
Write a Python 3 function that can
set a schema from another schema

Response:
def _set(self, schema):
        """Set a schema from another schema"""
        if isinstance(schema, CommonSchema):
            self._spl_type = False
            self._schema = schema.schema()
            self._style = self._default_style()
        else:
            self._spl_type = schema._spl_type
            self._schema = schema._schema
            self._style = schema._style


Instruction:
Can you tell what is the following Python 3 function doing
def as_tuple(self, named=None):
        if not named:
            return self._copy(tuple)

        if named == True or isinstance(named, basestring):
            return self._copy(self._make_named_tuple(name=named))

        return self._copy(tuple)

Response: Create a structured schema that will pass stream tuples into a tuple.


Instruction:
Write a Python 3 function that can
extend this schema by another schema.

Response:
def extend(self, schema):
        """
        Extend a structured schema by another.

        For example extending ``tuple<rstring id, timestamp ts, float64 value>``
        with ``tuple<float32 score>`` results in ``tuple<rstring id, timestamp ts, float64 value, float32 score>``.

        Args:
            schema(StreamSchema): Schema to extend this schema by.

        Returns:
            StreamSchema: New schema that is an extension of this schema.
        """
        if self._spl_type:
           raise TypeError("Not supported for declared SPL types")
        base = self.schema()
        extends = schema.schema()
        new_schema = base[:-1] + ',' + extends[6:]
        return StreamSchema(new_schema)


Instruction:
Write a Python 3 script to
set an operator s parameter representing the style of this schema.

Response:
def _fnop_style(schema, op, name):
        """Set an operator's parameter representing the style of this schema."""
        if is_common(schema):
            if name in op.params:
                del op.params[name]
            return
        if _is_pending(schema):
            ntp = 'pending'
        elif schema.style is tuple:
            ntp = 'tuple'
        elif schema.style is _spl_dict:
            ntp = 'dict'
        elif _is_namedtuple(schema.style) and hasattr(schema.style, '_splpy_namedtuple'):
            ntp = 'namedtuple:' + schema.style._splpy_namedtuple
        else:
            return
        op.params[name] = ntp


Instruction:
Implement a Python 3 function for
saving all changed values to the database.

Response:
def save(self) -> None:
        """
        Saves all changed values to the database.
        """
        for name, field in self.fields.items():
            value = self.cleaned_data[name]
            if isinstance(value, UploadedFile):
                # Delete old file
                fname = self._s.get(name, as_type=File)
                if fname:
                    try:
                        default_storage.delete(fname.name)
                    except OSError:  # pragma: no cover
                        logger.error('Deleting file %s failed.' % fname.name)

                # Create new file
                newname = default_storage.save(self.get_new_filename(value.name), value)
                value._name = newname
                self._s.set(name, value)
            elif isinstance(value, File):
                # file is unchanged
                continue
            elif isinstance(field, forms.FileField):
                # file is deleted
                fname = self._s.get(name, as_type=File)
                if fname:
                    try:
                        default_storage.delete(fname.name)
                    except OSError:  # pragma: no cover
                        logger.error('Deleting file %s failed.' % fname.name)
                del self._s[name]
            elif value is None:
                del self._s[name]
            elif self._s.get(name, as_type=type(value)) != value:
                self._s.set(name, value)


Instruction:
Can you create a Python 3 function that
returns the new filename based on the original filename of an uploaded file.

Response:
def get_new_filename(self, name: str) -> str:
        """
        Returns the file name to use based on the original filename of an uploaded file.
        By default, the file name is constructed as::

            <model_name>-<attribute_name>/<primary_key>/<original_basename>.<random_nonce>.<extension>
        """
        nonce = get_random_string(length=8)
        return '%s-%s/%s/%s.%s.%s' % (
            self.obj._meta.model_name, self.attribute_name,
            self.obj.pk, name, nonce, name.split('.')[-1]
        )


Instruction:
Given the following Python 3 function, write the documentation
def _stop(sas, cmd_args):
    if not cmd_args.force:
        status = sas.get_instance_status()
        jobs = int(status['job_count'])
        if jobs:
            return status
    return sas.stop_instance()

Response: Stop the service if no jobs are running unless force is set


Instruction:
Can you tell what is the following Python 3 function doing
def main(args=None):
    streamsx._streams._version._mismatch_check('streamsx.topology.context')
    try:
        sr = run_cmd(args)
        sr['return_code'] = 0
    except:
        sr = {'return_code':1, 'error': sys.exc_info()}
    return sr

Response: Runs an action against a Streaming Analytics service.


Instruction:
Write a Python 3 function that can
get information from the user s code.

Response:
def _source_info():
    """
    Get information from the user's code (two frames up)
    to leave breadcrumbs for file, line, class and function.
    """
    ofi = inspect.getouterframes(inspect.currentframe())[2]
    try:
        calling_class = ofi[0].f_locals['self'].__class__
    except KeyError:
        calling_class = None
    # Tuple of file,line,calling_class,function_name
    return ofi[1], ofi[2], calling_class, ofi[3]


Instruction:
Can you tell what is the following Python 3 function doing
def source(self, func, name=None):
        _name = name
        if inspect.isroutine(func):
            pass
        elif callable(func):
            pass
        else:
            if _name is None:
                _name = type(func).__name__
            func = streamsx.topology.runtime._IterableInstance(func)

        sl = _SourceLocation(_source_info(), "source")
        _name = self.graph._requested_name(_name, action='source', func=func)
        # source is always stateful
        op = self.graph.addOperator(self.opnamespace+"::Source", func, name=_name, sl=sl)
        op._layout(kind='Source', name=_name, orig_name=name)
        oport = op.addOutputPort(name=_name)
        return Stream(self, oport)._make_placeable()

Response: Declare a source stream that introduces tuples from an external source.


Instruction:
Implement a Python 3 function for
subscribing to a topic.

Response:
def subscribe(self, topic, schema=streamsx.topology.schema.CommonSchema.Python, name=None, connect=None, buffer_capacity=None, buffer_full_policy=None):
        """
        Subscribe to a topic published by other Streams applications.
        A Streams application may publish a stream to allow other
        Streams applications to subscribe to it. A subscriber matches a
        publisher if the topic and schema match.

        By default a stream is subscribed as :py:const:`~streamsx.topology.schema.CommonSchema.Python` objects
        which connects to streams published to topic by Python Streams applications.

        Structured schemas are subscribed to using an instance of
        :py:class:`StreamSchema`.  A Streams application publishing
        structured schema streams may have been implemented in any
        programming language supported by Streams.

        JSON streams are subscribed to using schema :py:const:`~streamsx.topology.schema.CommonSchema.Json`.
        Each tuple on the returned stream will be a Python dictionary
        object created by ``json.loads(tuple)``.
        A Streams application publishing JSON streams may have been implemented in any programming language
        supported by Streams.

        String streams are subscribed to using schema :py:const:`~streamsx.topology.schema.CommonSchema.String`.
        Each tuple on the returned stream will be a Python string object.
        A Streams application publishing string streams may have been implemented in any programming language
        supported by Streams.

        Subscribers can ensure they do not slow down matching publishers
        by using a buffered connection with a buffer full policy
        that drops tuples.

        Args:
            topic(str): Topic to subscribe to.
            schema(~streamsx.topology.schema.StreamSchema): schema to subscribe to.
            name(str): Name of the subscribed stream, defaults to a generated name.
            connect(SubscribeConnection): How subscriber will be connected to matching publishers. Defaults to :py:const:`~SubscribeConnection.Direct` connection.
            buffer_capacity(int): Buffer capacity in tuples when `connect` is set to :py:const:`~SubscribeConnection.Buffered`. Defaults to 1000 when `connect` is `Buffered`. Ignored when `connect` is `None` or `Direct`.
            buffer_full_policy(~streamsx.types.CongestionPolicy): Policy when a pulished tuple arrives and the subscriber's buffer is full. Defaults to `Wait` when `connect` is `Buffered`. Ignored when `connect` is `None` or `Direct`.

        Returns:
            Stream:  A stream whose tuples have been published to the topic by other Streams applications.

        .. versionchanged:: 1.9 `connect`, `buffer_capacity` and `buffer_full_policy` parameters added.

        .. seealso:`SubscribeConnection`
        """
        schema = streamsx.topology.schema._normalize(schema)
        _name = self.graph._requested_name(name, 'subscribe')
        sl = _SourceLocation(_source_info(), "subscribe")
        # subscribe is never stateful
        op = self.graph.addOperator(kind="com.ibm.streamsx.topology.topic::Subscribe", sl=sl, name=_name, stateful=False)
        oport = op.addOutputPort(schema=schema, name=_name)
        params = {'topic': topic, 'streamType': schema}
        if connect is not None and connect != SubscribeConnection.Direct:
            params['connect'] = connect
            if buffer_capacity:
                params['bufferCapacity'] = int(buffer_capacity)
            if buffer_full_policy:
                params['bufferFullPolicy'] = buffer_full_policy

        op.setParameters(params)
        op._layout_group('Subscribe', name if name else _name)
        return Stream(self, oport)._make_placeable()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_file_dependency(self, path, location):
        if location not in {'etc', 'opt'}:
            raise ValueError(location)

        if not os.path.isfile(path) and not os.path.isdir(path):
            raise ValueError(path)

        path = os.path.abspath(path)

        if location not in self._files:
             self._files[location] = [path]
        else:
             self._files[location].append(path)
        return location + '/' + os.path.basename(path)

Response: Adds a file or directory dependency into an Streams application bundle.


Instruction:
Create a Python 3 function for
adding a Python package dependency to the internal list of packages that are required by pip.

Response:
def add_pip_package(self, requirement):
        """
        Add a Python package dependency for this topology.

        If the package defined by the requirement specifier
        is not pre-installed on the build system then the
        package is installed using `pip` and becomes part
        of the Streams application bundle (`sab` file).
        The package is expected to be available from `pypi.org`.

        If the package is already installed on the build system
        then it is not added into the `sab` file.
        The assumption is that the runtime hosts for a Streams
        instance have the same Python packages installed as the
        build machines. This is always true for IBM Cloud
        Private for Data and the Streaming Analytics service on IBM Cloud.

        The project name extracted from the requirement
        specifier is added to :py:attr:`~exclude_packages`
        to avoid the package being added by the dependency
        resolver. Thus the package should be added before
        it is used in any stream transformation.

        When an application is run with trace level ``info``
        the available Python packages on the running system
        are listed to application trace. This includes
        any packages added by this method.

        Example::

            topo = Topology()
            # Add dependency on pint package
            # and astral at version 0.8.1
            topo.add_pip_package('pint')
            topo.add_pip_package('astral==0.8.1')

        Args:
            requirement(str): Package requirements specifier.

        .. warning::
            Only supported when using the build service with
            a Streams instance in IBM Cloud Private for Data
            or Streaming Analytics service on IBM Cloud.

        .. note::
            Installing packages through `pip` is preferred to
            the automatic dependency checking performed on local
            modules. This is because `pip` will perform a full
            install of the package including any dependent packages
            and additional files, such as shared libraries, that
            might be missed by dependency discovery.

        .. versionadded:: 1.9
        """
        self._pip_packages.append(str(requirement))
        pr = pkg_resources.Requirement.parse(requirement)
        self.exclude_packages.add(pr.project_name)


Instruction:
Make a summary of the following Python 3 code
def create_submission_parameter(self, name, default=None, type_=None):

        if name in self._submission_parameters:
            raise ValueError("Submission parameter {} already defined.".format(name))
        sp = streamsx.topology.runtime._SubmissionParam(name, default, type_)
        self._submission_parameters[name] = sp
        return sp

Response: Create a submission parameter.


Instruction:
Can you generate the documentation for the following Python 3 function
def _generate_requirements(self):
        if not self._pip_packages:
            return

        reqs = ''
        for req in self._pip_packages:
                reqs += "{}\n".format(req)
        reqs_include = {
            'contents': reqs,
            'target':'opt/python/streams',
            'name': 'requirements.txt'}

        if 'opt' not in self._files:
             self._files['opt'] = [reqs_include]
        else:
             self._files['opt'].append(reqs_include)

Response: Generate the info to create requirements. txt in the toookit.


Instruction:
Can you generate the documentation for the following Python 3 function
def _add_job_control_plane(self):
        if not self._has_jcp:
            jcp = self.graph.addOperator(kind="spl.control::JobControlPlane", name="JobControlPlane")
            jcp.viewable = False
            self._has_jcp = True

Response: Add a JobControlPlane operator to the topology if one has not already been added.


Instruction:
How would you code a function in Python 3 to
create an alias of this stream with name equal to name.

Response:
def aliased_as(self, name):
        """
        Create an alias of this stream.

        Returns an alias of this stream with name `name`.
        When invocation of an SPL operator requires an
        :py:class:`~streamsx.spl.op.Expression` against
        an input port this can be used to ensure expression
        matches the input port alias regardless of the name
        of the actual stream.

        Example use where the filter expression for a ``Filter`` SPL operator
        uses ``IN`` to access input tuple attribute ``seq``::

            s = ...
            s = s.aliased_as('IN')

            params =  {'filter': op.Expression.expression('IN.seq % 4ul == 0ul')}
            f = op.Map('spl.relational::Filter', stream, params = params)

        Args:
            name(str): Name for returned stream.

        Returns:
            Stream: Alias of this stream with ``name`` equal to `name`.

        .. versionadded:: 1.9
        """
        stream = copy.copy(self)
        stream._alias = name
        return stream


Instruction:
How would you code a function in Python 3 to
create a view on a distributed topology.

Response:
def view(self, buffer_time = 10.0, sample_size = 10000, name=None, description=None, start=False):
        """
        Defines a view on a stream.

        A view is a continually updated sampled buffer of a streams's tuples.
        Views allow visibility into a stream from external clients such
        as Jupyter Notebooks, the Streams console,
        `Microsoft Excel <https://www.ibm.com/support/knowledgecenter/SSCRJU_4.2.0/com.ibm.streams.excel.doc/doc/excel_overview.html>`_ or REST clients.

        The view created by this method can be used by external clients
        and through the returned :py:class:`~streamsx.topology.topology.View` object after the topology is submitted. For example a Jupyter Notebook can
        declare and submit an application with views, and then
        use the resultant `View` objects to visualize live data within the streams.

        When the stream contains Python objects then they are converted
        to JSON.

        Args:
            buffer_time: Specifies the buffer size to use measured in seconds.
            sample_size: Specifies the number of tuples to sample per second.
            name(str): Name of the view. Name must be unique within the topology. Defaults to a generated name.
            description: Description of the view.
            start(bool): Start buffering data when the job is submitted.
                If `False` then the view starts buffering data when the first
                remote client accesses it to retrieve data.

        Returns:
            streamsx.topology.topology.View: View object which can be used to access the data when the
            topology is submitted.

        .. note:: Views are only supported when submitting to distributed
            contexts including Streaming Analytics service.
        """
        if name is None:
            name = ''.join(random.choice('0123456789abcdef') for x in range(16))

        if self.oport.schema == streamsx.topology.schema.CommonSchema.Python:
            if self._json_stream:
                view_stream = self._json_stream
            else:
                self._json_stream = self.as_json(force_object=False)._layout(hidden=True)
                view_stream = self._json_stream
                # colocate map operator with stream that is being viewed.
                if self._placeable:
                    self._colocate(view_stream, 'view')
        else:
            view_stream = self

        port = view_stream.oport.name
        view_config = {
                'name': name,
                'port': port,
                'description': description,
                'bufferTime': buffer_time,
                'sampleSize': sample_size}
        if start:
            view_config['activateOption'] = 'automatic'
        view_stream.oport.operator.addViewConfig(view_config)
        _view = View(name)
        self.topology.graph._views.append(_view)
        return _view


Instruction:
Write a Python 3 function for
mapping each tuple from this stream into 0 or 1 stream tuples.

Response:
def map(self, func=None, name=None, schema=None):
        """
        Maps each tuple from this stream into 0 or 1 stream tuples.

        For each tuple on this stream ``result = func(tuple)`` is called.
        If `result` is not `None` then the result will be submitted
        as a tuple on the returned stream. If `result` is `None` then
        no tuple submission will occur.

        By default the submitted tuple is ``result`` without modification
        resulting in a stream of picklable Python objects. Setting the
        `schema` parameter changes the type of the stream and
        modifies each ``result`` before submission.

        * ``object`` or :py:const:`~streamsx.topology.schema.CommonSchema.Python` - The default:  `result` is submitted.
        * ``str`` type (``unicode`` 2.7) or :py:const:`~streamsx.topology.schema.CommonSchema.String` - A stream of strings: ``str(result)`` is submitted.
        * ``json`` or :py:const:`~streamsx.topology.schema.CommonSchema.Json` - A stream of JSON objects: ``result`` must be convertable to a JSON object using `json` package.
        * :py:const:`~streamsx.topology.schema.StreamSchema` - A structured stream. `result` must be a `dict` or (Python) `tuple`. When a `dict` is returned the outgoing stream tuple attributes are set by name, when a `tuple` is returned stream tuple attributes are set by position.
        * string value - Equivalent to passing ``StreamSchema(schema)``

        Args:
            func: A callable that takes a single parameter for the tuple.
                If not supplied then a function equivalent to ``lambda tuple_ : tuple_`` is used.
            name(str): Name of the mapped stream, defaults to a generated name.
            schema(StreamSchema|CommonSchema|str): Schema of the resulting stream.

        If invoking ``func`` for a tuple on the stream raises an exception
        then its processing element will terminate. By default the processing
        element will automatically restart though tuples may be lost.

        If ``func`` is a callable object then it may suppress exceptions
        by return a true value from its ``__exit__`` method. When an
        exception is suppressed no tuple is submitted to the mapped
        stream corresponding to the input tuple that caused the exception.

        Returns:
            Stream: A stream containing tuples mapped by `func`.

        .. versionadded:: 1.7 `schema` argument added to allow conversion to
            a structured stream.
        .. versionadded:: 1.8 Support for submitting `dict` objects as stream tuples to a structured stream (in addition to existing support for `tuple` objects).
        .. versionchanged:: 1.11 `func` is optional.
        """
        if schema is None:
            schema = streamsx.topology.schema.CommonSchema.Python
        if func is None:
            func = streamsx.topology.runtime._identity
            if name is None:
               name = 'identity'

        ms = self._map(func, schema=schema, name=name)._layout('Map')
        ms.oport.operator.sl = _SourceLocation(_source_info(), 'map')
        return ms


Instruction:
Write a Python 3 function for
returning a Stream containing the flattened and mapped tuples.

Response:
def flat_map(self, func=None, name=None):
        """
        Maps and flatterns each tuple from this stream into 0 or more tuples.

        For each tuple on this stream ``func(tuple)`` is called.
        If the result is not `None` then the the result is iterated over
        with each value from the iterator that is not `None` will be submitted
        to the return stream.

        If the result is `None` or an empty iterable then no tuples are submitted to
        the returned stream.

        Args:
            func: A callable that takes a single parameter for the tuple.
                If not supplied then a function equivalent to ``lambda tuple_ : tuple_`` is used.
                This is suitable when each tuple on this stream is an iterable to be flattened.

            name(str): Name of the flattened stream, defaults to a generated name.

        If invoking ``func`` for a tuple on the stream raises an exception
        then its processing element will terminate. By default the processing
        element will automatically restart though tuples may be lost.

        If ``func`` is a callable object then it may suppress exceptions
        by return a true value from its ``__exit__`` method. When an
        exception is suppressed no tuples are submitted to the flattened
        and mapped stream corresponding to the input tuple
        that caused the exception.

        Returns:
            Stream: A Stream containing flattened and mapped tuples.
        Raises:
            TypeError: if `func` does not return an iterator nor None

        .. versionchanged:: 1.11 `func` is optional.
        """
        if func is None:
            func = streamsx.topology.runtime._identity
            if name is None:
               name = 'flatten'

        sl = _SourceLocation(_source_info(), 'flat_map')
        _name = self.topology.graph._requested_name(name, action='flat_map', func=func)
        stateful = self._determine_statefulness(func)
        op = self.topology.graph.addOperator(self.topology.opnamespace+"::FlatMap", func, name=_name, sl=sl, stateful=stateful)
        op.addInputPort(outputPort=self.oport)
        streamsx.topology.schema.StreamSchema._fnop_style(self.oport.schema, op, 'pyStyle')
        oport = op.addOutputPort(name=_name)
        return Stream(self.topology, oport)._make_placeable()._layout('FlatMap', name=_name, orig_name=name)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parallel(self, width, routing=Routing.ROUND_ROBIN, func=None, name=None):
        _name = name
        if _name is None:
            _name = self.name + '_parallel'

        _name = self.topology.graph._requested_name(_name, action='parallel', func=func)

        if routing is None or routing == Routing.ROUND_ROBIN or routing == Routing.BROADCAST:
            op2 = self.topology.graph.addOperator("$Parallel$", name=_name)
            if name is not None:
                op2.config['regionName'] = _name
            op2.addInputPort(outputPort=self.oport)
            if routing == Routing.BROADCAST:
                oport = op2.addOutputPort(width, schema=self.oport.schema, routing="BROADCAST", name=_name)
            else:
                oport = op2.addOutputPort(width, schema=self.oport.schema, routing="ROUND_ROBIN", name=_name)

            return Stream(self.topology, oport)
        elif routing == Routing.HASH_PARTITIONED:

            if (func is None):
                if self.oport.schema == streamsx.topology.schema.CommonSchema.String:
                    keys = ['string']
                    parallel_input = self.oport
                elif self.oport.schema == streamsx.topology.schema.CommonSchema.Python:
                    func = hash
                else:
                    raise NotImplementedError("HASH_PARTITIONED for schema {0} requires a hash function.".format(self.oport.schema))

            if func is not None:
                keys = ['__spl_hash']
                stateful = self._determine_statefulness(func)
                hash_adder = self.topology.graph.addOperator(self.topology.opnamespace+"::HashAdder", func, stateful=stateful)
                hash_adder._op_def['hashAdder'] = True
                hash_adder._layout(hidden=True)
                hash_schema = self.oport.schema.extend(streamsx.topology.schema.StreamSchema("tuple<int64 __spl_hash>"))
                hash_adder.addInputPort(outputPort=self.oport)
                streamsx.topology.schema.StreamSchema._fnop_style(self.oport.schema, hash_adder, 'pyStyle')
                parallel_input = hash_adder.addOutputPort(schema=hash_schema)

            parallel_op = self.topology.graph.addOperator("$Parallel$", name=_name)
            if name is not None:
                parallel_op.config['regionName'] = _name
            parallel_op.addInputPort(outputPort=parallel_input)
            parallel_op_port = parallel_op.addOutputPort(oWidth=width, schema=parallel_input.schema, partitioned_keys=keys, routing="HASH_PARTITIONED")

            if func is not None:
                # use the Functor passthru operator to remove the hash attribute by removing it from output port schema
                hrop = self.topology.graph.addPassThruOperator()
                hrop._layout(hidden=True)
                hrop.addInputPort(outputPort=parallel_op_port)
                parallel_op_port = hrop.addOutputPort(schema=self.oport.schema)

            return Stream(self.topology, parallel_op_port)
        else :
            raise TypeError("Invalid routing type supplied to the parallel operator")

Response: Split the stream into channels and start a parallel region.


Instruction:
Create a Python 3 function for
ending a parallel region by merging the channels into a single stream.

Response:
def end_parallel(self):
        """
        Ends a parallel region by merging the channels into a single stream.

        Returns:
            Stream: Stream for which subsequent transformations are no longer parallelized.

        .. seealso:: :py:meth:`set_parallel`, :py:meth:`parallel`
        """
        outport = self.oport
        if isinstance(self.oport.operator, streamsx.topology.graph.Marker):
            if self.oport.operator.kind == "$Union$":
                pto = self.topology.graph.addPassThruOperator()
                pto.addInputPort(outputPort=self.oport)
                outport = pto.addOutputPort(schema=self.oport.schema)
        op = self.topology.graph.addOperator("$EndParallel$")
        op.addInputPort(outputPort=outport)
        oport = op.addOutputPort(schema=self.oport.schema)
        endP = Stream(self.topology, oport)
        return endP


Instruction:
How would you implement a function in Python 3 that
sets this source stream to be split into multiple channels and start a parallel region.

Response:
def set_parallel(self, width, name=None):
        """
        Set this source stream to be split into multiple channels
        as the start of a parallel region.

        Calling ``set_parallel`` on a stream created by
        :py:meth:`~Topology.source` results in the stream
        having `width` channels, each created by its own instance
        of the callable::

           s = topo.source(S())
           s.set_parallel(3)
           f = s.filter(F())
           e = f.end_parallel()

        Each channel has independent instances of ``S`` and ``F``. Tuples
        created by the instance of ``S`` in channel 0 are passed to the
        instance of ``F`` in channel 0, and so on for channels 1 and 2.

        Callable transforms instances within the channel can use
        the runtime functions
        :py:func:`~streamsx.ec.channel`,
        :py:func:`~streamsx.ec.local_channel`,
        :py:func:`~streamsx.ec.max_channels` &
        :py:func:`~streamsx.ec.local_max_channels`
        to adapt to being invoked in parallel. For example a
        source callable can use its channel number to determine
        which partition to read from in a partitioned external system.

        Calling ``set_parallel`` on a stream created by
        :py:meth:`~Topology.subscribe` results in the stream
        having `width` channels. Subscribe ensures that the
        stream will contain all published tuples matching the
        topic subscription and type. A published tuple will appear
        on one of the channels though the specific channel is not known
        in advance.

        A parallel region is terminated by :py:meth:`end_parallel`
        or :py:meth:`for_each`.

        The number of channels is set by `width` which may be an `int` greater
        than zero or a submission parameter created by
        :py:meth:`Topology.create_submission_parameter`.

        With IBM Streams 4.3 or later the number of channels can be
        dynamically changed at runtime.

        Parallel regions are started on non-source streams using
        :py:meth:`parallel`.

        Args:
            width: The degree of parallelism for the parallel region.
            name(str): Name of the parallel region. Defaults to the name of this stream.

        Returns:
            Stream: Returns this stream.

        .. seealso:: :py:meth:`parallel`, :py:meth:`end_parallel`

        .. versionadded:: 1.9
        .. versionchanged:: 1.11 `name` parameter added.
        """
        self.oport.operator.config['parallel'] = True
        self.oport.operator.config['width'] = streamsx.topology.graph._as_spl_json(width, int)
        if name:
            name = self.topology.graph._requested_name(str(name), action='set_parallel')
            self.oport.operator.config['regionName'] = name
        return self


Instruction:
Can you generate the documentation for the following Python 3 function
def set_consistent(self, consistent_config):

        # add job control plane if needed
        self.topology._add_job_control_plane()
        self.oport.operator.consistent(consistent_config)
        return self._make_placeable()

Response: Sets the consistent state of the current stream.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def last(self, size=1):
        win = Window(self, 'SLIDING')
        if isinstance(size, datetime.timedelta):
            win._evict_time(size)
        elif isinstance(size, int):
            win._evict_count(size)
        else:
            raise ValueError(size)
        return win

Response: Declares a slding window containing the most recent tuples on this stream.


Instruction:
How would you explain what the following Python 3 function does
def union(self, streamSet):
        if(not isinstance(streamSet,set)) :
            raise TypeError("The union operator parameter must be a set object")
        if(len(streamSet) == 0):
            return self
        op = self.topology.graph.addOperator("$Union$")
        op.addInputPort(outputPort=self.oport)
        for stream in streamSet:
            op.addInputPort(outputPort=stream.oport)
        oport = op.addOutputPort(schema=self.oport.schema)
        return Stream(self.topology, oport)

Response: Returns a new stream that is a union of this stream and other streams.


Instruction:
Can you tell what is the following Python 3 function doing
def print(self, tag=None, name=None):
        _name = name
        if _name is None:
            _name = 'print'
        fn = streamsx.topology.functions.print_flush
        if tag is not None:
            tag = str(tag) + ': '
            fn = lambda v : streamsx.topology.functions.print_flush(tag + str(v))
        sp = self.for_each(fn, name=_name)
        sp._op().sl = _SourceLocation(_source_info(), 'print')
        return sp

Response: Prints each tuple to stdout flushing after each tuple.


Instruction:
Can you create a Python 3 function that
publishes this stream to a topic.

Response:
def publish(self, topic, schema=None, name=None):
        """
        Publish this stream on a topic for other Streams applications to subscribe to.
        A Streams application may publish a stream to allow other
        Streams applications to subscribe to it. A subscriber
        matches a publisher if the topic and schema match.

        By default a stream is published using its schema.

        A stream of :py:const:`Python objects <streamsx.topology.schema.CommonSchema.Python>` can be subscribed to by other Streams Python applications.

        If a stream is published setting `schema` to
        :py:const:`~streamsx.topology.schema.CommonSchema.Json`
        then it is published as a stream of JSON objects.
        Other Streams applications may subscribe to it regardless
        of their implementation language.

        If a stream is published setting `schema` to
        :py:const:`~streamsx.topology.schema.CommonSchema.String`
        then it is published as strings
        Other Streams applications may subscribe to it regardless
        of their implementation language.

        Supported values of `schema` are only
        :py:const:`~streamsx.topology.schema.CommonSchema.Json`
        and
        :py:const:`~streamsx.topology.schema.CommonSchema.String`.

        Args:
            topic(str): Topic to publish this stream to.
            schema: Schema to publish. Defaults to the schema of this stream.
            name(str): Name of the publish operator, defaults to a generated name.
        Returns:
            streamsx.topology.topology.Sink: Stream termination.

        .. versionadded:: 1.6.1 `name` parameter.

        .. versionchanged:: 1.7
            Now returns a :py:class:`Sink` instance.
        """
        sl = _SourceLocation(_source_info(), 'publish')
        schema = streamsx.topology.schema._normalize(schema)
        if schema is not None and self.oport.schema.schema() != schema.schema():
            nc = None
            if schema == streamsx.topology.schema.CommonSchema.Json:
                schema_change = self.as_json()
            elif schema == streamsx.topology.schema.CommonSchema.String:
                schema_change = self.as_string()
            else:
                raise ValueError(schema)

            if self._placeable:
                self._colocate(schema_change, 'publish')
            sp = schema_change.publish(topic, schema=schema, name=name)
            sp._op().sl = sl
            return sp

        _name = self.topology.graph._requested_name(name, action="publish")
        # publish is never stateful
        op = self.topology.graph.addOperator("com.ibm.streamsx.topology.topic::Publish", params={'topic': topic}, sl=sl, name=_name, stateful=False)
        op.addInputPort(outputPort=self.oport)
        op._layout_group('Publish', name if name else _name)
        sink = Sink(op)

        if self._placeable:
            self._colocate(sink, 'publish')
        return sink


Instruction:
Here you have a function in Python 3, explain what it does
def autonomous(self):
        op = self.topology.graph.addOperator("$Autonomous$")
        op.addInputPort(outputPort=self.oport)
        oport = op.addOutputPort(schema=self.oport.schema)
        return Stream(self.topology, oport)

Response: Starts an autonomous region for downstream processing.


Instruction:
Write a Python 3 script to
return a new SAS stream containing the string representations of each tuple on this stream.

Response:
def as_string(self, name=None):
        """
        Declares a stream converting each tuple on this stream
        into a string using `str(tuple)`.

        The stream is typed as a :py:const:`string stream <streamsx.topology.schema.CommonSchema.String>`.

        If this stream is already typed as a string stream then it will
        be returned (with no additional processing against it and `name`
        is ignored).

        Args:
            name(str): Name of the resulting stream.
                When `None` defaults to a generated name.

        .. versionadded:: 1.6
        .. versionadded:: 1.6.1 `name` parameter added.

        Returns:
            Stream: Stream containing the string representations of tuples on this stream.
        """
        sas = self._change_schema(streamsx.topology.schema.CommonSchema.String, 'as_string', name)._layout('AsString')
        sas.oport.operator.sl = _SourceLocation(_source_info(), 'as_string')
        return sas


Instruction:
Make a summary of the following Python 3 code
def as_json(self, force_object=True, name=None):
        func = streamsx.topology.runtime._json_force_object if force_object else None
        saj = self._change_schema(streamsx.topology.schema.CommonSchema.Json, 'as_json', name, func)._layout('AsJson')
        saj.oport.operator.sl = _SourceLocation(_source_info(), 'as_json')
        return saj

Response: Returns a new stream that contains the JSON representation of the tuples on this stream.


Instruction:
Here you have a function in Python 3, explain what it does
def _initialize_rest(self):
        if self._submit_context is None:
            raise ValueError("View has not been created.")
        job = self._submit_context._job_access()
        self._view_object = job.get_views(name=self.name)[0]

Response: Used to initialize the View object on first use.


Instruction:
Given the following Python 3 function, write the documentation
def display(self, duration=None, period=2):
        self._initialize_rest()
        return self._view_object.display(duration, period)

Response: Display a view within a Jupyter or IPython notebook cell.


Instruction:
Can you create a Python 3 function that
completes the pending stream.

Response:
def complete(self, stream):
            """Complete the pending stream.

            Any connections made to :py:attr:`stream` are connected to `stream` once
            this method returns.

            Args:
                stream(Stream): Stream that completes the connection.
            """
            assert not self.is_complete()
            self._marker.addInputPort(outputPort=stream.oport)
            self.stream.oport.schema = stream.oport.schema
            # Update the pending schema to the actual schema
            # Any downstream filters that took the reference
            # will be automatically updated to the correct schema
            self._pending_schema._set(self.stream.oport.schema)

            # Mark the operator with the pending stream
            # a start point for graph travesal
            stream.oport.operator._start_op = True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def trigger(self, when=1):
        tw = Window(self.stream, self._config['type'])
        tw._config['evictPolicy'] = self._config['evictPolicy']
        tw._config['evictConfig'] = self._config['evictConfig']
        if self._config['evictPolicy'] == 'TIME':
            tw._config['evictTimeUnit'] = 'MILLISECONDS'

        if isinstance(when, datetime.timedelta):
            tw._config['triggerPolicy'] = 'TIME'
            tw._config['triggerConfig'] = int(when.total_seconds() * 1000.0)
            tw._config['triggerTimeUnit'] = 'MILLISECONDS'
        elif isinstance(when, int):
            tw._config['triggerPolicy'] = 'COUNT'
            tw._config['triggerConfig'] = when
        else:
            raise ValueError(when)
        return tw

Response: Declare a window with this window s size and a trigger policy.


Instruction:
How would you explain what the following Python 3 function does
def aggregate(self, function, name=None):
        schema = streamsx.topology.schema.CommonSchema.Python

        sl = _SourceLocation(_source_info(), "aggregate")
        _name = self.topology.graph._requested_name(name, action="aggregate", func=function)
        stateful = self.stream._determine_statefulness(function)
        op = self.topology.graph.addOperator(self.topology.opnamespace+"::Aggregate", function, name=_name, sl=sl, stateful=stateful)
        op.addInputPort(outputPort=self.stream.oport, window_config=self._config)
        streamsx.topology.schema.StreamSchema._fnop_style(self.stream.oport.schema, op, 'pyStyle')
        oport = op.addOutputPort(schema=schema, name=_name)
        op._layout(kind='Aggregate', name=_name, orig_name=name)
        return Stream(self.topology, oport)._make_placeable()

Response: Aggregates the contents of the window when the window is triggered.


Instruction:
Here you have a function in Python 3, explain what it does
def get_rest_api():
    assert _has_local_install

    url=[]
    ok = _run_st(['geturl', '--api'], lines=url)
    if not ok:
        raise ChildProcessError('streamtool geturl')
    return url[0]

Response: Get the root URL for IBM Streams REST API.


Instruction:
Given the following Python 3 function, write the documentation
def _get_package_name(module):
    try:
        # if __package__ is defined, use it
        package_name = module.__package__
    except AttributeError:
        package_name = None

    if package_name is None:
        # if __path__ is defined, the package name is the module name
        package_name = module.__name__
        if not hasattr(module, '__path__'):
            # if __path__ is not defined, the package name is the
            # string before the last "." of the fully-qualified module name
            package_name = package_name.rpartition('.')[0]

    return package_name

Response: Gets the package name given a module object


Instruction:
How would you implement a function in Python 3 that
gets the function s module name

Response:
def _get_module_name(function):
    """
    Gets the function's module name
    Resolves the __main__ module to an actual module name
    Returns:
        str: the function's module name
    """
    module_name = function.__module__
    if module_name == '__main__':
        # get the main module object of the function
        main_module = inspect.getmodule(function)
        # get the module name from __file__ by getting the base name and removing the .py extension
        # e.g. test1.py => test1
        if hasattr(main_module, '__file__'):
            module_name = os.path.splitext(os.path.basename(main_module.__file__))[0]
    return module_name


Instruction:
Write a Python 3 script to
am the given module a builtin module?

Response:
def _is_builtin_module(module):
    """Is builtin or part of standard library
    """
    if (not hasattr(module, '__file__')) or  module.__name__ in sys.builtin_module_names:
        return True
    if module.__name__ in _stdlib._STD_LIB_MODULES:
        return True
    amp = os.path.abspath(module.__file__)
    if 'site-packages' in amp:
        return False
    if amp.startswith(_STD_MODULE_DIR):
        return True
    if not '.' in module.__name__:
        return False
    mn_top = module.__name__.split('.')[0]
    return mn_top in _stdlib._STD_LIB_MODULES


Instruction:
Can you implement a function in Python 3 that
finds the set of dependent modules for used modules classes and routines.

Response:
def _find_dependent_modules(self, module):
        """
        Return the set of dependent modules for used modules,
        classes and routines.
        """
        dms = set()
        for um in inspect.getmembers(module, inspect.ismodule):
            dms.add(um[1])

        for uc in inspect.getmembers(module, inspect.isclass):
            self._add_obj_module(dms, uc[1])
        for ur in inspect.getmembers(module, inspect.isroutine):
            self._add_obj_module(dms, ur[1])

        return dms


Instruction:
Make a summary of the following Python 3 code
def add_dependencies(self, module):

        if module in self._processed_modules:
            return None

        if hasattr(module, "__name__"):
            mn = module.__name__
        else:
            mn = '<unknown>'

        _debug.debug("add_dependencies:module=%s", module)

        # If the module in which the class/function is defined is __main__, don't add it. Just add its dependencies.
        if mn == "__main__":
            self._processed_modules.add(module)

        # add the module as a dependency
        elif not self._add_dependency(module, mn):
            _debug.debug("add_dependencies:not added:module=%s", mn)
            return None

        _debug.debug("add_dependencies:ADDED:module=%s", mn)

        # recursively get the module's imports and add those as dependencies
        for dm in self._find_dependent_modules(module):
            _debug.debug("add_dependencies:adding dependent module %s for %s", dm, mn)
            self.add_dependencies(dm)

Response: Adds a module and its dependencies to the list of dependencies.


Instruction:
Write a Python 3 function for
returning True if a module should be included or excluded based upon included_packages and excluded_packages.

Response:
def _include_module(self, module, mn):
        """ See if a module should be included or excluded based upon
        included_packages and excluded_packages.

        As some packages have the following format:

        scipy.special.specfun
        scipy.linalg

        Where the top-level package name is just a prefix to a longer package name,
        we don't want to do a direct comparison. Instead, we want to exclude packages
        which are either exactly "<package_name>", or start with "<package_name>".
        """

        if mn in self.topology.include_packages:
            _debug.debug("_include_module:explicit using __include_packages: module=%s", mn)
            return True
        if '.' in mn:
            for include_package in self.topology.include_packages:
                if mn.startswith(include_package + '.'):
                    _debug.debug("_include_module:explicit pattern using __include_packages: module=%s pattern=%s", mn, \
                            include_package + '.')
                    return True

        if mn in self.topology.exclude_packages:
            _debug.debug("_include_module:explicit using __exclude_packages: module=%s", mn)
            return False
        if '.' in mn:
            for exclude_package in self.topology.exclude_packages:
                if mn.startswith(exclude_package + '.'):
                    _debug.debug("_include_module:explicit pattern using __exclude_packages: module=%s pattern=%s", mn, \
                                 exclude_package + '.')
                    return False

        _debug.debug("_include_module:including: module=%s", mn)
        return True


Instruction:
Implement a Python 3 function for
adding a module to the list of dependencies.

Response:
def _add_dependency(self, module, mn):
        """
        Adds a module to the list of dependencies
        without handling the modules dependences.

        Modules in site-packages are excluded from being added into
        the toolkit. This mimics dill.
        """
        _debug.debug("_add_dependency:module=%s", mn)

        if _is_streamsx_module(module):
            _debug.debug("_add_dependency:streamsx module=%s", mn)
            return False

        if _is_builtin_module(module):
            _debug.debug("_add_dependency:builtin module=%s", mn)
            return False

        if not self._include_module(module, mn):
          #print ("ignoring dependencies for {0} {1}".format(module.__name__, module))
          return False

        package_name = _get_package_name(module)
        top_package_name = module.__name__.split('.')[0]

        if package_name and top_package_name in sys.modules:
            # module is part of a package
            # get the top-level package
            top_package = sys.modules[top_package_name]

            if "__path__" in top_package.__dict__:
                # for regular packages, there is one top-level directory
                # for namespace packages, there can be more than one.
                # they will be merged in the bundle
                seen_non_site_package = False
                for top_package_path in reversed(list(top_package.__path__)):
                    top_package_path = os.path.abspath(top_package_path)
                    if 'site-packages' in top_package_path:
                        continue
                    seen_non_site_package = True
                    self._add_package(top_package_path)
                if not seen_non_site_package:
                    _debug.debug("_add_dependency:site-packages path module=%s", mn)
                    return False
            elif hasattr(top_package, '__file__'):
                # package that is an individual python file with empty __path__
                if 'site-packages' in top_package.__file__:
                    _debug.debug("_add_dependency:site-packages module=%s", mn)
                    return False
                self._add_package(os.path.abspath(top_package.__file__))
        elif hasattr(module, '__file__'):
            # individual Python module
            module_path = os.path.abspath(module.__file__)
            if 'site-packages' in module_path:
                _debug.debug("_add_dependency:site-packages module=%s", mn)
                return False
            self._modules.add(module_path)

        self._processed_modules.add(module)
        return True


Instruction:
Make a summary of the following Python 3 code
def freeze(self) -> dict:
        settings = {}
        for key, v in self._h.defaults.items():
            settings[key] = self._unserialize(v.value, v.type)
        if self._parent:
            settings.update(getattr(self._parent, self._h.attribute_name).freeze())
        for key in self._cache():
            settings[key] = self.get(key)
        return settings

Response: Returns a dictionary of all settings set for this object including any values of its parents or hardcoded defaults.


Instruction:
Can you write a function in Python 3 where it
gets a value from the object s settings object.

Response:
def get(self, key: str, default=None, as_type: type = None, binary_file=False):
        """
        Get a setting specified by key ``key``. Normally, settings are strings, but
        if you put non-strings into the settings object, you can request unserialization
        by specifying ``as_type``. If the key does not have a harcdoded default type,
        omitting ``as_type`` always will get you a string.

        If the setting with the specified name does not exist on this object, any parent object
        up to the global settings layer (if configured) will be queried. If still no value is
        found, a default value set in ths source code will be returned if one exists.
        If not, the value of the ``default`` argument of this method will be returned instead.

        If you receive a ``File`` object, it will already be opened. You can specify the ``binary_file``
        flag to indicate that it should be opened in binary mode.
        """
        if as_type is None and key in self._h.defaults:
            as_type = self._h.defaults[key].type

        if key in self._cache():
            value = self._cache()[key]
        else:
            value = None
            if self._parent:
                value = getattr(self._parent, self._h.attribute_name).get(key, as_type=str)
            if value is None and key in self._h.defaults:
                value = self._h.defaults[key].value
            if value is None and default is not None:
                value = default

        return self._unserialize(value, as_type, binary_file=binary_file)


Instruction:
Write a Python 3 function for
storing a setting to the database of the object.

Response:
def set(self, key: str, value: Any) -> None:
        """
        Stores a setting to the database of its object.

        The write to the database is performed immediately and the cache in the cache backend is flushed.
        The cache within this object will be updated correctly.
        """
        wc = self._write_cache()
        if key in wc:
            s = wc[key]
        else:
            s = self._type(object=self._obj, key=key)
        s.value = self._serialize(value)
        s.save()
        self._cache()[key] = s.value
        wc[key] = s
        self._flush_external_cache()


Instruction:
Write a Python 3 script to
delete a setting from this object s storage.

Response:
def delete(self, key: str) -> None:
        """
        Deletes a setting from this object's storage.

        The write to the database is performed immediately and the cache in the cache backend is flushed.
        The cache within this object will be updated correctly.
        """
        if key in self._write_cache():
            self._write_cache()[key].delete()
            del self._write_cache()[key]

        if key in self._cache():
            del self._cache()[key]

        self._flush_external_cache()


Instruction:
How would you explain what the following Python 3 function does
def _get_vcap_services(vcap_services=None):
    vcap_services = vcap_services or os.environ.get('VCAP_SERVICES')
    if not vcap_services:
        raise ValueError(
            "VCAP_SERVICES information must be supplied as a parameter or as environment variable 'VCAP_SERVICES'")
    # If it was passed to config as a dict, simply return it
    if isinstance(vcap_services, dict):
        return vcap_services
    try:
        # Otherwise, if it's a string, try to load it as json
        vcap_services = json.loads(vcap_services)
    except json.JSONDecodeError:
        # If that doesn't work, attempt to open it as a file path to the json config.
        try:
            with open(vcap_services) as vcap_json_data:
                vcap_services = json.load(vcap_json_data)
        except:
            raise ValueError("VCAP_SERVICES information is not JSON or a file containing JSON:", vcap_services)
    return vcap_services

Response: Retrieves the VCAP Services information from the VCAP_SERVICES environment variable or from the config object.


Instruction:
How would you implement a function in Python 3 that
retrieves the credentials of the specified service_name.

Response:
def _get_credentials(vcap_services, service_name=None):
    """Retrieves the credentials of the VCAP Service of the specified `service_name`.  If
    `service_name` is not specified, it takes the information from STREAMING_ANALYTICS_SERVICE_NAME environment
    variable.

    Args:
        vcap_services (dict): A dict representation of the VCAP Services information.
        service_name (str): One of the service name stored in `vcap_services`

    Returns:
        dict: A dict representation of the credentials.

    Raises:
        ValueError:  Cannot find `service_name` in `vcap_services`
    """
    service_name = service_name or os.environ.get('STREAMING_ANALYTICS_SERVICE_NAME', None)
    # Get the service corresponding to the SERVICE_NAME
    services = vcap_services['streaming-analytics']
    creds = None
    for service in services:
        if service['name'] == service_name:
            creds = service['credentials']
            break

    # If no corresponding service is found, error
    if creds is None:
        raise ValueError("Streaming Analytics service " + str(service_name) + " was not found in VCAP_SERVICES")
    return creds


Instruction:
Write a Python 3 function that can
retrieve the Streams REST API URL from the provided credentials.

Response:
def _get_rest_api_url_from_creds(session, credentials):
    """Retrieves the Streams REST API URL from the provided credentials.
    Args:
        session (:py:class:`requests.Session`): A Requests session object for making REST calls
        credentials (dict): A dict representation of the credentials.
    Returns:
        str: The remote Streams REST API URL.
    """
    resources_url = credentials['rest_url'] + credentials['resources_path']
    try:
        response_raw = session.get(resources_url, auth=(credentials['userid'], credentials['password']))
        response = response_raw.json()
    except:
        logger.error("Error while retrieving rest REST url from: " + resources_url)
        raise

    response_raw.raise_for_status()

    rest_api_url = response['streams_rest_url'] + '/resources'
    return rest_api_url


Instruction:
Here you have a function in Python 3, explain what it does
def _get_iam_rest_api_url_from_creds(rest_client, credentials):
    res = rest_client.make_request(credentials[_IAMConstants.V2_REST_URL])
    base = res['streams_self']
    end = base.find('/instances')
    return base[:end] + '/resources'

Response: Retrieves the remote Streams REST API URL from the provided credentials using IAM authentication.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def resource_url(self):
        self._resource_url = self._resource_url or st.get_rest_api()
        return self._resource_url

Response: str - Root URL for IBM Streams REST API


Instruction:
Create a Python 3 function to
get a single element matching an id

Response:
def _get_element_by_id(self, resource_name, eclass, id):
        """Get a single element matching an id"""
        elements = self._get_elements(resource_name, eclass, id=id)
        if not elements:
            raise ValueError("No resource matching: {0}".format(id))
        if len(elements) == 1:
            return elements[0]
        raise ValueError("Multiple resources matching: {0}".format(id))


Instruction:
Can you write a function in Python 3 where it
retrieves the available domains.

Response:
def get_domains(self):
        """Retrieves available domains.

        Returns:
            :py:obj:`list` of :py:class:`~.rest_primitives.Domain`: List of available domains
        """
        # Domains are fixed and actually only one per REST api.
        if self._domains is None:
            self._domains = self._get_elements('domains', Domain)
        return self._domains