Instruction:
Can you tell what is the following Python 3 function doing
async def download(self, files: Sequence[Union[str, Path]],
                       dest: Union[str, Path] = '.',
                       show_progress: bool = False):
        '''
        Downloads the given list of files from the compute session.

        :param files: The list of file paths in the compute session.
            If they are relative paths, the path is calculated from
            ``/home/work`` in the compute session container.
        :param dest: The destination directory in the client-side.
        :param show_progress: Displays a progress bar during downloads.
        '''
        params = {}
        if self.owner_access_key:
            params['owner_access_key'] = self.owner_access_key
        rqst = Request(self.session,
                       'GET', '/kernel/{}/download'.format(self.kernel_id),
                       params=params)
        rqst.set_json({
            'files': [*map(str, files)],
        })
        async with rqst.fetch() as resp:
            chunk_size = 1 * 1024
            file_names = None
            tqdm_obj = tqdm(desc='Downloading files',
                            unit='bytes', unit_scale=True,
                            total=resp.content.total_bytes,
                            disable=not show_progress)
            with tqdm_obj as pbar:
                fp = None
                while True:
                    chunk = await resp.aread(chunk_size)
                    if not chunk:
                        break
                    pbar.update(len(chunk))
                    # TODO: more elegant parsing of multipart response?
                    for part in chunk.split(b'\r\n'):
                        if part.startswith(b'--'):
                            if fp:
                                fp.close()
                                with tarfile.open(fp.name) as tarf:
                                    tarf.extractall(path=dest)
                                    file_names = tarf.getnames()
                                os.unlink(fp.name)
                            fp = tempfile.NamedTemporaryFile(suffix='.tar',
                                                             delete=False)
                        elif part.startswith(b'Content-') or part == b'':
                            continue
                        else:
                            fp.write(part)
                if fp:
                    fp.close()
                    os.unlink(fp.name)
            result = {'file_names': file_names}
            return result

Response: Downloads the given list of files from the compute session container.


Instruction:
Make a summary of the following Python 3 code
async def list_files(self, path: Union[str, Path] = '.'):
        '''
        Gets the list of files in the given path inside the compute session
        container.

        :param path: The directory path in the compute session.
        '''
        params = {}
        if self.owner_access_key:
            params['owner_access_key'] = self.owner_access_key
        rqst = Request(self.session,
                       'GET', '/kernel/{}/files'.format(self.kernel_id),
                       params=params)
        rqst.set_json({
            'path': path,
        })
        async with rqst.fetch() as resp:
            return await resp.json()

Response:
Gets the list of files in the given path inside the compute session
        container.


Instruction:
Write a Python 3 function that can
open a pseudo - terminal of the kernel.

Response:
def stream_pty(self) -> 'StreamPty':
        '''
        Opens a pseudo-terminal of the kernel (if supported) streamed via
        websockets.

        :returns: a :class:`StreamPty` object.
        '''
        params = {}
        if self.owner_access_key:
            params['owner_access_key'] = self.owner_access_key
        request = Request(self.session,
                          'GET', '/stream/kernel/{}/pty'.format(self.kernel_id),
                          params=params)
        return request.connect_websocket(response_cls=StreamPty)


Instruction:
Write a Python 3 function for
executing a code snippet in the streaming mode.

Response:
def stream_execute(self, code: str = '', *,
                       mode: str = 'query',
                       opts: dict = None) -> WebSocketResponse:
        '''
        Executes a code snippet in the streaming mode.
        Since the returned websocket represents a run loop, there is no need to
        specify *run_id* explicitly.
        '''
        params = {}
        if self.owner_access_key:
            params['owner_access_key'] = self.owner_access_key
        opts = {} if opts is None else opts
        if mode == 'query':
            opts = {}
        elif mode == 'batch':
            opts = {
                'clean': opts.get('clean', None),
                'build': opts.get('build', None),
                'buildLog': bool(opts.get('buildLog', False)),
                'exec': opts.get('exec', None),
            }
        else:
            msg = 'Invalid stream-execution mode: {0}'.format(mode)
            raise BackendClientError(msg)
        request = Request(self.session,
                          'GET', '/stream/kernel/{}/execute'.format(self.kernel_id),
                          params=params)

        async def send_code(ws):
            await ws.send_json({
                'code': code,
                'mode': mode,
                'options': opts,
            })

        return request.connect_websocket(on_enter=send_code)


Instruction:
Can you tell what is the following Python 3 function doing
def _connect(self, mqttc, userdata, flags, rc):
        if rc == 0:
            self.connected = True
            results = []
            LOGGER.info("MQTT Connected with result code " + str(rc) + " (Success)")
            # result, mid = self._mqttc.subscribe(self.topicInput)
            results.append((self.topicInput, tuple(self._mqttc.subscribe(self.topicInput))))
            results.append((self.topicPolyglotConnection, tuple(self._mqttc.subscribe(self.topicPolyglotConnection))))
            for (topic, (result, mid)) in results:
                if result == 0:
                    LOGGER.info("MQTT Subscribing to topic: " + topic + " - " + " MID: " + str(mid) + " Result: " + str(result))
                else:
                    LOGGER.info("MQTT Subscription to " + topic + " failed. This is unusual. MID: " + str(mid) + " Result: " + str(result))
                    # If subscription fails, try to reconnect.
                    self._mqttc.reconnect()
            self._mqttc.publish(self.topicSelfConnection, json.dumps(
                {
                    'connected': True,
                    'node': self.profileNum
                }), retain=True)
            LOGGER.info('Sent Connected message to Polyglot')
        else:
            LOGGER.error("MQTT Failed to connect. Result code: " + str(rc))

Response: Callback for when the client receives a CONNACK response from the server.


Instruction:
How would you explain what the following Python 3 function does
def _message(self, mqttc, userdata, msg):
        try:
            inputCmds = ['query', 'command', 'result', 'status', 'shortPoll', 'longPoll', 'delete']
            parsed_msg = json.loads(msg.payload.decode('utf-8'))
            if 'node' in parsed_msg:
                if parsed_msg['node'] != 'polyglot':
                    return
                del parsed_msg['node']
                for key in parsed_msg:
                    # LOGGER.debug('MQTT Received Message: {}: {}'.format(msg.topic, parsed_msg))
                    if key == 'config':
                        self.inConfig(parsed_msg[key])
                    elif key == 'connected':
                        self.polyglotConnected = parsed_msg[key]
                    elif key == 'stop':
                        LOGGER.debug('Received stop from Polyglot... Shutting Down.')
                        self.stop()
                    elif key in inputCmds:
                        self.input(parsed_msg)
                    else:
                        LOGGER.error('Invalid command received in message from Polyglot: {}'.format(key))

        except (ValueError) as err:
            LOGGER.error('MQTT Received Payload Error: {}'.format(err), exc_info=True)

Response: This callback is called when a PUBLISH message is received from the server.


Instruction:
How would you explain what the following Python 3 function does
def _disconnect(self, mqttc, userdata, rc):
        self.connected = False
        if rc != 0:
            LOGGER.info("MQTT Unexpected disconnection. Trying reconnect.")
            try:
                self._mqttc.reconnect()
            except Exception as ex:
                template = "An exception of type {0} occured. Arguments:\n{1!r}"
                message = template.format(type(ex).__name__, ex.args)
                LOGGER.error("MQTT Connection error: " + message)
        else:
            LOGGER.info("MQTT Graceful disconnection.")

Response: Callback for when a DISCONNECT occurs.


Instruction:
Explain what the following Python 3 code does
def _startMqtt(self):
        LOGGER.info('Connecting to MQTT... {}:{}'.format(self._server, self._port))
        try:
            # self._mqttc.connect_async(str(self._server), int(self._port), 10)
            self._mqttc.connect_async('{}'.format(self._server), int(self._port), 10)
            self._mqttc.loop_forever()
        except Exception as ex:
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            LOGGER.error("MQTT Connection error: {}".format(message), exc_info=True)

Response: Starts the MQTT client thread.


Instruction:
Here you have a function in Python 3, explain what it does
def stop(self):
        # self.loop.call_soon_threadsafe(self.loop.stop)
        # self.loop.stop()
        # self._longPoll.cancel()
        # self._shortPoll.cancel()
        if self.connected:
            LOGGER.info('Disconnecting from MQTT... {}:{}'.format(self._server, self._port))
            self._mqttc.publish(self.topicSelfConnection, json.dumps({'node': self.profileNum, 'connected': False}), retain=True)
            self._mqttc.loop_stop()
            self._mqttc.disconnect()
        try:
            for watcher in self.__stopObservers:
                watcher()
        except KeyError as e:
            LOGGER.exception('KeyError in gotConfig: {}'.format(e), exc_info=True)

Response: Stop the client and disconnect the broker if clean shutdown.


Instruction:
How would you implement a function in Python 3 that
sends a message to Polyglot.

Response:
def send(self, message):
        """
        Formatted Message to send to Polyglot. Connection messages are sent automatically from this module
        so this method is used to send commands to/from Polyglot and formats it for consumption
        """
        if not isinstance(message, dict) and self.connected:
            warnings.warn('payload not a dictionary')
            return False
        try:
            message['node'] = self.profileNum
            self._mqttc.publish(self.topicInput, json.dumps(message), retain=False)
        except TypeError as err:
            LOGGER.error('MQTT Send Error: {}'.format(err), exc_info=True)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def addNode(self, node):
        LOGGER.info('Adding node {}({})'.format(node.name, node.address))
        message = {
            'addnode': {
                'nodes': [{
                    'address': node.address,
                    'name': node.name,
                    'node_def_id': node.id,
                    'primary': node.primary,
                    'drivers': node.drivers,
                    'hint': node.hint
                }]
            }
        }
        self.send(message)

Response: Adds a node to the NodeServer


Instruction:
Write a Python 3 function that can
send custom dictionary to Polyglot to save and retrieve on startup.

Response:
def saveCustomData(self, data):
        """
        Send custom dictionary to Polyglot to save and be retrieved on startup.

        :param data: Dictionary of key value pairs to store in Polyglot database.
        """
        LOGGER.info('Sending customData to Polyglot.')
        message = { 'customdata': data }
        self.send(message)


Instruction:
Create a Python 3 function to
send custom dictionary to Polyglot to save and retrieve on startup.

Response:
def saveCustomParams(self, data):
        """
        Send custom dictionary to Polyglot to save and be retrieved on startup.

        :param data: Dictionary of key value pairs to store in Polyglot database.
        """
        LOGGER.info('Sending customParams to Polyglot.')
        message = { 'customparams': data }
        self.send(message)


Instruction:
Create a Python 3 function for
adding custom notice to Polyglot

Response:
def addNotice(self, data):
        """
        Add custom notice to front-end for this NodeServers

        :param data: String of characters to add as a notification in the front-end.
        """
        LOGGER.info('Sending addnotice to Polyglot: {}'.format(data))
        message = { 'addnotice': data }
        self.send(message)


Instruction:
Given the following Python 3 function, write the documentation
def removeNotice(self, data):
        LOGGER.info('Sending removenotice to Polyglot for index {}'.format(data))
        message = { 'removenotice': data }
        self.send(message)

Response: Add custom notice to front - end for this node


Instruction:
Make a summary of the following Python 3 code
def delNode(self, address):
        LOGGER.info('Removing node {}'.format(address))
        message = {
            'removenode': {
                'address': address
            }
        }
        self.send(message)

Response: Delete a node from the NodeServer


Instruction:
How would you explain what the following Python 3 function does
def getNode(self, address):
        try:
            for node in self.config['nodes']:
                if node['address'] == address:
                    return node
            return False
        except KeyError:
            LOGGER.error('Usually means we have not received the config yet.', exc_info=True)
            return False

Response: Get Node by Address of existing nodes.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def inConfig(self, config):
        self.config = config
        self.isyVersion = config['isyVersion']
        try:
            for watcher in self.__configObservers:
                watcher(config)

            self.send_custom_config_docs()

        except KeyError as e:
            LOGGER.error('KeyError in gotConfig: {}'.format(e), exc_info=True)

Response: Called by Polyglot when a new config is received from Polyglot.


Instruction:
Explain what the following Python 3 code does
def save_typed_params(self, data):
        LOGGER.info('Sending typed parameters to Polyglot.')
        if type(data) is not list:
            data = [ data ]
        message = { 'typedparams': data }
        self.send(message)

Response: Send typed parameters to Polyglot


Instruction:
Here you have a function in Python 3, explain what it does
def delNode(self, address):
        if address in self.nodes:
            del self.nodes[address]
        self.poly.delNode(address)

Response: Delete a node from the node list.


Instruction:
How would you implement a function in Python 3 that
queries the database for the specified key - value dictionary.

Response:
async def query(cls, query: str,
                    variables: Optional[Mapping[str, Any]] = None,
                    ) -> Any:
        '''
        Sends the GraphQL query and returns the response.

        :param query: The GraphQL query string.
        :param variables: An optional key-value dictionary
            to fill the interpolated template variables
            in the query.

        :returns: The object parsed from the response JSON string.
        '''
        gql_query = {
            'query': query,
            'variables': variables if variables else {},
        }
        rqst = Request(cls.session, 'POST', '/admin/graphql')
        rqst.set_json(gql_query)
        async with rqst.fetch() as resp:
            return await resp.json()


Instruction:
Write a Python 3 script for
returning human readable string from number of seconds

Response:
def get_readable_time_string(seconds):
    """Returns human readable string from number of seconds"""
    seconds = int(seconds)
    minutes = seconds // 60
    seconds = seconds % 60
    hours = minutes // 60
    minutes = minutes % 60
    days = hours // 24
    hours = hours % 24

    result = ""
    if days > 0:
        result += "%d %s " % (days, "Day" if (days == 1) else "Days")
    if hours > 0:
        result += "%d %s " % (hours, "Hour" if (hours == 1) else "Hours")
    if minutes > 0:
        result += "%d %s " % (minutes, "Minute" if (minutes == 1) else "Minutes")
    if seconds > 0:
        result += "%d %s " % (seconds, "Second" if (seconds == 1) else "Seconds")

    return result.strip()


Instruction:
Implement a function in Python 3 to
return a list of rate limit information from a given response s headers.

Response:
def get_rate_limits(response):
    """Returns a list of rate limit information from a given response's headers."""
    periods = response.headers['X-RateLimit-Period']
    if not periods:
        return []

    rate_limits = []

    periods = periods.split(',')
    limits = response.headers['X-RateLimit-Limit'].split(',')
    remaining = response.headers['X-RateLimit-Remaining'].split(',')
    reset = response.headers['X-RateLimit-Reset'].split(',')

    for idx, period in enumerate(periods):
        rate_limit = {}
        limit_period = get_readable_time_string(period)
        rate_limit["period"] = limit_period
        rate_limit["period_seconds"] = period
        rate_limit["request_limit"] = limits[idx]
        rate_limit["requests_remaining"] = remaining[idx]

        reset_datetime = get_datetime_from_timestamp(reset[idx])
        rate_limit["reset"] = reset_datetime

        right_now = datetime.now()
        if (reset_datetime is not None) and (right_now < reset_datetime):
            # add 1 second because of rounding
            seconds_remaining = (reset_datetime - right_now).seconds + 1
        else:
            seconds_remaining = 0

        rate_limit["reset_in_seconds"] = seconds_remaining

        rate_limit["time_to_reset"] = get_readable_time_string(seconds_remaining)
        rate_limits.append(rate_limit)

    return rate_limits


Instruction:
Can you tell what is the following Python 3 function doing
def isubset(self, *keys):
        # type: (*Hashable) -> ww.g
        """Return key, self[key] as generator for key in keys.

        Raise KeyError if a key does not exist

        Args:
            keys: Iterable containing keys

        Example:

            >>> from ww import d
            >>> list(d({1: 1, 2: 2, 3: 3}).isubset(1, 3))
            [(1, 1), (3, 3)]
        """
        return ww.g((key, self[key]) for key in keys)

Response: Return key as generator for key in keys Raise KeyError if a key does not exist


Instruction:
Can you generate the documentation for the following Python 3 function
def swap(self):
        # type: () -> DictWrapper
        """Swap key and value

        /!\ Be carreful, if there are duplicate values, only one will
        survive /!\

        Example:

            >>> from ww import d
            >>> d({1: 2, 2: 2, 3: 3}).swap()
            {2: 2, 3: 3}
        """
        return self.__class__((v, k) for k, v in self.items())

Response: Swap key and value of a dictionary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fromkeys(cls, iterable, value=None):
        # TODO : type: (Iterable, Union[Any, Callable]) -> DictWrapper
        # https://github.com/python/mypy/issues/2254
        """Create a new d from

        Args:
            iterable: Iterable containing keys
            value: value to associate with each key.
            If callable, will be value[key]

        Returns: new DictWrapper

        Example:

            >>> from ww import d
            >>> sorted(d.fromkeys('123', value=4).items())
            [('1', 4), ('2', 4), ('3', 4)]
            >>> sorted(d.fromkeys(range(3), value=lambda e:e**2).items())
            [(0, 0), (1, 1), (2, 4)]
        """
        if not callable(value):
            return cls(dict.fromkeys(iterable, value))

        return cls((key, value(key)) for key in iterable)

Response: Create a new DictWrapper from a list of keys and a value.


Instruction:
Given the following Python 3 function, write the documentation
def main():
    try:
        # Get the `src` directory's absolute path
        src_path = os.path.dirname(
            # `aoiklivereload` directory's absolute path
            os.path.dirname(
                # `demo` directory's absolute path
                os.path.dirname(
                    # This file's absolute path
                    os.path.abspath(__file__)
                )
            )
        )

        # If the `src` directory path is not in `sys.path`
        if src_path not in sys.path:
            # Add to `sys.path`.
            #
            # This aims to save user setting PYTHONPATH when running this demo.
            #
            sys.path.append(src_path)

        # Import reloader class
        from aoiklivereload import LiveReloader

        # Create reloader
        reloader = LiveReloader()

        # Start watcher thread
        reloader.start_watcher_thread()

        # Server host
        server_host = '0.0.0.0'

        # Server port
        server_port = 8000

        # Get message
        msg = '# ----- Run server -----\nHost: {}\nPort: {}'.format(
            server_host, server_port
        )

        # Print message
        print(msg)

        # Create Flask app
        flask_app = flask.Flask(__name__)

        # Create request handler
        @flask_app.route('/')
        def hello_handler():  # pylint: disable=unused-variable
            """
            Request handler.

            :return:
                Response body.
            """
            # Return response body
            return 'hello'

        # Run server
        flask_app.run(
            host=server_host,
            port=server_port,
            # Disable Flask's reloader
            debug=False,
        )

    # If have `KeyboardInterrupt`
    except KeyboardInterrupt:
        # Not treat as error
        pass

Response:
Main function.

    :return:
        None.


Instruction:
Implement a Python 3 function for
generating an Excel workbook object given api_data

Response:
def get_excel_workbook(api_data, result_info_key, identifier_keys):
    """Generates an Excel workbook object given api_data returned by the Analytics API

    Args:
        api_data: Analytics API data as a list of dicts (one per identifier)
        result_info_key: the key in api_data dicts that contains the data results
        identifier_keys: the list of keys used as requested identifiers
                         (address, zipcode, block_id, etc)

    Returns:
        raw excel file data
    """

    cleaned_data = []

    for item_data in api_data:
        result_info = item_data.pop(result_info_key, {})

        cleaned_item_data = {}

        if 'meta' in item_data:
            meta = item_data.pop('meta')
            cleaned_item_data['meta'] = meta

        for key in item_data:
            cleaned_item_data[key] = item_data[key]['result']

        cleaned_item_data[result_info_key] = result_info

        cleaned_data.append(cleaned_item_data)

    data_list = copy.deepcopy(cleaned_data)

    workbook = openpyxl.Workbook()

    write_worksheets(workbook, data_list, result_info_key, identifier_keys)

    return workbook


Instruction:
Create a Python 3 function to
write rest of the worksheets into the current workbook.

Response:
def write_worksheets(workbook, data_list, result_info_key, identifier_keys):
    """Writes rest of the worksheets to workbook.

    Args:
        workbook: workbook to write into
        data_list: Analytics API data as a list of dicts
        result_info_key: the key in api_data dicts that contains the data results
        identifier_keys: the list of keys used as requested identifiers
                         (address, zipcode, block_id, etc)
    """

    # we can use the first item to figure out the worksheet keys
    worksheet_keys = get_worksheet_keys(data_list[0], result_info_key)

    for key in worksheet_keys:

        title = key.split('/')[1]

        title = utilities.convert_snake_to_title_case(title)

        title = KEY_TO_WORKSHEET_MAP.get(title, title)

        if key == 'property/nod':
            # the property/nod endpoint needs to be split into two worksheets
            create_property_nod_worksheets(workbook, data_list, result_info_key, identifier_keys)
        else:
            # all other endpoints are written to a single worksheet

            # Maximum 31 characters allowed in sheet title
            worksheet = workbook.create_sheet(title=title[:31])

            processed_data = process_data(key, data_list, result_info_key, identifier_keys)

            write_data(worksheet, processed_data)

    # remove the first, unused empty sheet
    workbook.remove_sheet(workbook.active)


Instruction:
Given the following Python 3 function, write the documentation
def create_property_nod_worksheets(workbook, data_list, result_info_key, identifier_keys):
    nod_details_list = []
    nod_default_history_list = []

    for prop_data in data_list:
        nod_data = prop_data['property/nod']

        if nod_data is None:
            nod_data = {}

        default_history_data = nod_data.pop('default_history', [])

        _set_identifier_fields(nod_data, prop_data, result_info_key, identifier_keys)

        nod_details_list.append(nod_data)

        for item in default_history_data:
            _set_identifier_fields(item, prop_data, result_info_key, identifier_keys)
            nod_default_history_list.append(item)

    worksheet = workbook.create_sheet(title='NOD Details')
    write_data(worksheet, nod_details_list)

    worksheet = workbook.create_sheet(title='NOD Default History')
    write_data(worksheet, nod_default_history_list)

Response: Creates two worksheets out of the property and nod data.


Instruction:
Implement a Python 3 function for
getting sorted keys from the dict ignoring result_info_key and meta keys

Response:
def get_worksheet_keys(data_dict, result_info_key):
    """Gets sorted keys from the dict, ignoring result_info_key and 'meta' key
    Args:
        data_dict: dict to pull keys from

    Returns:
        list of keys in the dict other than the result_info_key
    """
    keys = set(data_dict.keys())
    keys.remove(result_info_key)
    if 'meta' in keys:
        keys.remove('meta')
    return sorted(keys)


Instruction:
Create a Python 3 function for
getting all possible keys from a list of dicts sorting by leading_columns first

Response:
def get_keys(data_list, leading_columns=LEADING_COLUMNS):
    """Gets all possible keys from a list of dicts, sorting by leading_columns first

    Args:
        data_list: list of dicts to pull keys from
        leading_columns: list of keys to put first in the result

    Returns:
        list of keys to be included as columns in excel worksheet
    """
    all_keys = set().union(*(list(d.keys()) for d in data_list))

    leading_keys = []

    for key in leading_columns:
        if key not in all_keys:
            continue
        leading_keys.append(key)
        all_keys.remove(key)

    return leading_keys + sorted(all_keys)


Instruction:
How would you implement a function in Python 3 that
gives a key in api_data dicts pulls the data for that endpoint out of the data_list for each address and processes the data into a more specific format and returns the data.

Response:
def process_data(key, data_list, result_info_key, identifier_keys):
    """ Given a key as the endpoint name, pulls the data for that endpoint out
        of the data_list for each address, processes the data into a more
        excel-friendly format and returns that data.

        Args:
            key: the endpoint name of the data to process
            data_list: the main data list to take the data from
            result_info_key: the key in api_data dicts that contains the data results
            identifier_keys: the list of keys used as requested identifiers
                             (address, zipcode, block_id, etc)

        Returns:
            A list of dicts (rows) to be written to a worksheet
    """
    master_data = []

    for item_data in data_list:
        data = item_data[key]

        if data is None:
            current_item_data = {}
        else:
            if key == 'property/value':
                current_item_data = data['value']

            elif key == 'property/details':
                top_level_keys = ['property', 'assessment']
                current_item_data = flatten_top_level_keys(data, top_level_keys)

            elif key == 'property/school':
                current_item_data = data['school']

                school_list = []
                for school_type_key in current_item_data:
                    schools = current_item_data[school_type_key]
                    for school in schools:
                        school['school_type'] = school_type_key
                        school['school_address'] = school['address']
                        school['school_zipcode'] = school['zipcode']
                        school_list.append(school)

                current_item_data = school_list

            elif key == 'property/value_forecast':
                current_item_data = {}
                for month_key in data:
                    current_item_data[month_key] = data[month_key]['value']

            elif key in ['property/value_within_block', 'property/rental_value_within_block']:
                current_item_data = flatten_top_level_keys(data, [
                    'housecanary_value_percentile_range',
                    'housecanary_value_sqft_percentile_range',
                    'client_value_percentile_range',
                    'client_value_sqft_percentile_range'
                ])

            elif key in ['property/zip_details', 'zip/details']:
                top_level_keys = ['multi_family', 'single_family']
                current_item_data = flatten_top_level_keys(data, top_level_keys)

            else:
                current_item_data = data

        if isinstance(current_item_data, dict):
            _set_identifier_fields(current_item_data, item_data, result_info_key, identifier_keys)

            master_data.append(current_item_data)
        else:
            # it's a list
            for item in current_item_data:
                _set_identifier_fields(item, item_data, result_info_key, identifier_keys)

            master_data.extend(current_item_data)

    return master_data


Instruction:
Can you generate a brief explanation for the following Python 3 code
def check_database_connected(app_configs, **kwargs):
    errors = []

    try:
        connection.ensure_connection()
    except OperationalError as e:
        msg = 'Could not connect to database: {!s}'.format(e)
        errors.append(checks.Error(msg,
                                   id=health.ERROR_CANNOT_CONNECT_DATABASE))
    except ImproperlyConfigured as e:
        msg = 'Datbase misconfigured: "{!s}"'.format(e)
        errors.append(checks.Error(msg,
                                   id=health.ERROR_MISCONFIGURED_DATABASE))
    else:
        if not connection.is_usable():
            errors.append(checks.Error('Database connection is not usable',
                                       id=health.ERROR_UNUSABLE_DATABASE))

    return errors

Response:
A Django check to see if connecting to the configured default
    database backend succeeds.


Instruction:
Can you tell what is the following Python 3 function doing
def check_migrations_applied(app_configs, **kwargs):
    from django.db.migrations.loader import MigrationLoader
    errors = []

    # Load migrations from disk/DB
    try:
        loader = MigrationLoader(connection, ignore_no_migrations=True)
    except (ImproperlyConfigured, ProgrammingError, OperationalError):
        msg = "Can't connect to database to check migrations"
        return [checks.Info(msg, id=health.INFO_CANT_CHECK_MIGRATIONS)]

    if app_configs:
        app_labels = [app.label for app in app_configs]
    else:
        app_labels = loader.migrated_apps

    for node, migration in loader.graph.nodes.items():
        if migration.app_label not in app_labels:
            continue
        if node not in loader.applied_migrations:
            msg = 'Unapplied migration {}'.format(migration)
            # NB: This *must* be a Warning, not an Error, because Errors
            # prevent migrations from being run.
            errors.append(checks.Warning(msg,
                                         id=health.WARNING_UNAPPLIED_MIGRATION))

    return errors

Response: A Django check to see if all migrations have been applied correctly.


Instruction:
How would you implement a function in Python 3 that
checks Redis connection and see if Redis ping is successful.

Response:
def check_redis_connected(app_configs, **kwargs):
    """
    A Django check to connect to the default redis connection
    using ``django_redis.get_redis_connection`` and see if Redis
    responds to a ``PING`` command.
    """
    import redis
    from django_redis import get_redis_connection
    errors = []

    try:
        connection = get_redis_connection('default')
    except redis.ConnectionError as e:
        msg = 'Could not connect to redis: {!s}'.format(e)
        errors.append(checks.Error(msg, id=health.ERROR_CANNOT_CONNECT_REDIS))
    except NotImplementedError as e:
        msg = 'Redis client not available: {!s}'.format(e)
        errors.append(checks.Error(msg, id=health.ERROR_MISSING_REDIS_CLIENT))
    except ImproperlyConfigured as e:
        msg = 'Redis misconfigured: "{!s}"'.format(e)
        errors.append(checks.Error(msg, id=health.ERROR_MISCONFIGURED_REDIS))
    else:
        result = connection.ping()
        if not result:
            msg = 'Redis ping failed'
            errors.append(checks.Error(msg, id=health.ERROR_REDIS_PING_FAILED))
    return errors


Instruction:
Can you implement a function in Python 3 that
functions takes in no param as input to fetch Real - Time Alarms from HPE IMC RESTFUL API

Response:
def get_realtime_alarm(username, auth, url):
    """Takes in no param as input to fetch RealTime Alarms from HP IMC RESTFUL API

    :param username OpeatorName, String type. Required. Default Value "admin". Checks the operator
     has the privileges to view the Real-Time Alarms.

    :param devId: int or str value of the target device

    :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class

    :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass

    :return:list of dictionaries where each element of the list represents a single alarm as pulled from the the current
     list of realtime alarms in the HPE IMC Platform

    :rtype: list

    >>> from pyhpeimc.auth import *

    >>> from pyhpeimc.plat.alarms import *

    >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")

    >>> real_time_alarm = get_realtime_alarm('admin', auth.creds, auth.url)

    >>> type(real_time_alarm)
    <class 'list'>
    >>> assert 'faultDesc' in real_time_alarm[0]

    """
    get_realtime_alarm_url = "/imcrs/fault/faultRealTime?operatorName=" + username
    f_url = url + get_realtime_alarm_url
    r = requests.get(f_url, auth=auth, headers=headers)
    try:
        realtime_alarm_list = (json.loads(r.text))
        return realtime_alarm_list['faultRealTime']['faultRealTimeList']
    except requests.exceptions.RequestException as e:
        return "Error:\n" + str(e) + ' get_realtime_alarm: An Error has occured'


Instruction:
Write a Python 3 function for
returning a list of dicts where each element of the list represents a single alarm.

Response:
def get_alarms(username, auth, url):
    """Takes in no param as input to fetch RealTime Alarms from HP IMC RESTFUL API

    :param username OpeatorName, String type. Required. Default Value "admin". Checks the operator
     has the privileges to view the Real-Time Alarms.

    :param devId: int or str value of the target device

    :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class

    :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass

    :return:list of dictionaries where each element of the list represents a single alarm as pulled from the the current
     list of browse alarms in the HPE IMC Platform

    :rtype: list

    >>> from pyhpeimc.auth import *

    >>> from pyhpeimc.plat.alarms import *

    >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")

    >>> all_alarms = get_alarms('admin', auth.creds, auth.url)

    >>> type(all_alarms)
    <class 'list'>
    >>> assert 'ackStatus' in all_alarms[0]

    [{'OID': '1.3.6.1.4.1.11.2.14.11.15.2.75.5.3.1.6.7',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '1001',
  'alarmCategoryDesc': 'Wireless Device Alarm',
  'alarmDesc': 'The device(MAC Address:   F4 B7 E2 95 A0 CD) is detected to be flooding the network. Attack Frame Type: Probe Request.',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187616',
  'alarmLevel': '3',
  'alarmLevelDesc': 'Minor',
  'deviceId': '31',
  'deviceIp': '10.10.10.5',
  'deviceName': 'HP830_WSC',
  'faultTime': '1469471298',
  'faultTimeDesc': '2016-07-25 14:28:18',
  'holdInfo': '',
  'id': '187616',
  'originalType': '1',
  'originalTypeDesc': 'Trap',
  'paras': '*Attack MAC=  F4 B7 E2 95 A0 CD;Attack Frame Type=Probe Request',
  'parentId': '0',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '15',
  'alarmCategoryDesc': 'Other Alarms from NMS',
  'alarmDesc': 'iMC alarm system has received 100 alarms about the event (hh3cPeriodicalTrap) of device ADP_Initial_Config(10.20.10.10) from 2016-07-25 12:56:01 to 2016-07-25 14:36:01.',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187625',
  'alarmLevel': '4',
  'alarmLevelDesc': 'Warning',
  'deviceId': '0',
  'deviceIp': '127.0.0.1',
  'deviceName': 'NMS',
  'faultTime': '1469471761',
  'faultTimeDesc': '2016-07-25 14:36:01',
  'id': '187625',
  'originalType': '3',
  'originalTypeDesc': 'iMC',
  'paras': 'Start Time=2016-07-25 12:56:01;Stop Time=2016-07-25 14:36:01;Times=100;*Repeat Event Name=hh3cPeriodicalTrap;*Device IP=10.20.10.10;Device Name=ADP_Initial_Config',
  'parentId': '173953',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '15',
  'alarmCategoryDesc': 'Other Alarms from NMS',
  'alarmDesc': 'iMC alarm system has received 100 alarms about the event (Some ambient device interferes) of device HP830_WSC(10.10.10.5) from 2016-07-25 14:07:06 to 2016-07-25 14:40:21.',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187626',
  'alarmLevel': '4',
  'alarmLevelDesc': 'Warning',
  'deviceId': '0',
  'deviceIp': '127.0.0.1',
  'deviceName': 'NMS',
  'faultTime': '1469472021',
  'faultTimeDesc': '2016-07-25 14:40:21',
  'id': '187626',
  'originalType': '3',
  'originalTypeDesc': 'iMC',
  'paras': 'Start Time=2016-07-25 14:07:06;Stop Time=2016-07-25 14:40:21;Times=100;*Repeat Event Name=Some ambient device interferes;*Device IP=10.10.10.5;Device Name=HP830_WSC',
  'parentId': '173953',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.4.1.25506.4.2.3.2.6.11',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '9',
  'alarmCategoryDesc': 'Traffic Analysis and Audit Alarm',
  'alarmDesc': 'The in direction of interface GigabitEthernet1/0/23 on device 192.168.1.221 at 2016-07-25 14:32:00 is greater than the baseline(2.87 Mbps).',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187631',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '67',
  'deviceIp': '10.101.0.203',
  'deviceName': 'HPEIMC72WIN2K12R2',
  'faultTime': '1469472140',
  'faultTimeDesc': '2016-07-25 14:42:20',
  'holdInfo': '',
  'id': '187631',
  'originalType': '3',
  'originalTypeDesc': 'iMC',
  'paras': '*UNBA Server Name=NMS;*UNBA Server IP=127.0.0.1;*Alarm Object Name=The in direction of interface GigabitEthernet1/0/23 on device 192.168.1.221;Alarm Occur Time=2016-07-25 14:32:00;Current Flow Speed=48.38 Mbps;Alarm Severity=2;Threshold Speed=2.87 Mbps;Device IP=192.168.1.221;Interface Index=23;Interface Name=GigabitEthernet1/0/23;Flow Direction=1',
  'parentId': '0',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.4.1.25506.4.2.3.2.6.11',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '9',
  'alarmCategoryDesc': 'Traffic Analysis and Audit Alarm',
  'alarmDesc': 'The out direction of interface GigabitEthernet1/0/2 on device 192.168.1.221 at 2016-07-25 14:32:00 is greater than the baseline(1.78 Mbps).',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187632',
  'alarmLevel': '2',
  'alarmLevelDesc': 'Major',
  'deviceId': '67',
  'deviceIp': '10.101.0.203',
  'deviceName': 'HPEIMC72WIN2K12R2',
  'faultTime': '1469472140',
  'faultTimeDesc': '2016-07-25 14:42:20',
  'holdInfo': '',
  'id': '187632',
  'originalType': '3',
  'originalTypeDesc': 'iMC',
  'paras': '*UNBA Server Name=NMS;*UNBA Server IP=127.0.0.1;*Alarm Object Name=The out direction of interface GigabitEthernet1/0/2 on device 192.168.1.221;Alarm Occur Time=2016-07-25 14:32:00;Current Flow Speed=48.79 Mbps;Alarm Severity=2;Threshold Speed=1.78 Mbps;Device IP=192.168.1.221;Interface Index=2;Interface Name=GigabitEthernet1/0/2;Flow Direction=0',
  'parentId': '0',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '15',
  'alarmCategoryDesc': 'Other Alarms from NMS',
  'alarmDesc': 'iMC alarm system has received 100 alarms about the event (Some ambient AP interferes) of device HP830_WSC(10.10.10.5) from 2016-07-25 14:14:00 to 2016-07-25 14:47:55.',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187637',
  'alarmLevel': '4',
  'alarmLevelDesc': 'Warning',
  'deviceId': '0',
  'deviceIp': '127.0.0.1',
  'deviceName': 'NMS',
  'faultTime': '1469472475',
  'faultTimeDesc': '2016-07-25 14:47:55',
  'id': '187637',
  'originalType': '3',
  'originalTypeDesc': 'iMC',
  'paras': 'Start Time=2016-07-25 14:14:00;Stop Time=2016-07-25 14:47:55;Times=100;*Repeat Event Name=Some ambient AP interferes;*Device IP=10.10.10.5;Device Name=HP830_WSC',
  'parentId': '173953',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'},
 {'OID': '1.3.6.1.4.1.25506.4.2.2.2.6.1',
  'ackStatus': '0',
  'ackStatusDesc': 'Unacknowledged',
  'ackTime': '0',
  'ackTimeDesc': '',
  'ackUserName': '',
  'alarmCategory': '15',
  'alarmCategoryDesc': 'Other Alarms from NMS',
  'alarmDesc': 'iMC alarm system has received 100 alarms about the event (Some ambient device interferes) of device HP830_WSC(10.10.10.5) from 2016-07-25 14:40:21 to 2016-07-25 15:15:50.',
  'alarmDetail': 'http://10.101.0.203:8080/imcrs/fault/alarm/187654',
  'alarmLevel': '4',
  'alarmLevelDesc': 'Warning',
  'deviceId': '0',
  'deviceIp': '127.0.0.1',
  'deviceName': 'NMS',
  'faultTime': '1469474150',
  'faultTimeDesc': '2016-07-25 15:15:50',
  'id': '187654',
  'originalType': '3',
  'originalTypeDesc': 'iMC',
  'paras': 'Start Time=2016-07-25 14:40:21;Stop Time=2016-07-25 15:15:50;Times=100;*Repeat Event Name=Some ambient device interferes;*Device IP=10.10.10.5;Device Name=HP830_WSC',
  'parentId': '173953',
  'recStatus': '0',
  'recStatusDesc': 'Unrecovered',
  'recTime': '0',
  'recTimeDesc': '',
  'recUserName': '',
  'remark': '',
  'somState': '0'}]
    """
    get_alarms_url = "/imcrs/fault/alarm?operatorName=" + username + "&recStatus=0&ackStatus=0&timeRange=0&size=50&desc=true"
    f_url = url + get_alarms_url
    r = requests.get(f_url, auth=auth, headers=headers)
    # r.status_code
    try:
        if r.status_code == 200:
            alarm_list = (json.loads(r.text))
            return alarm_list['alarm']
    except requests.exceptions.RequestException as e:
            return "Error:\n" + str(e) + ' get_alarms: An Error has occured'


Instruction:
Given the following Python 3 function, write the documentation
def get_dev_alarms(auth, url, devid=None, devip=None):
    # checks to see if the imc credentials are already available
    if devip is not None:
        devid = get_dev_details(devip, auth, url)['id']
    f_url = url + "/imcrs/fault/alarm?operatorName=admin&deviceId=" + \
                        str(devid) + "&desc=false"
    response = requests.get(f_url, auth=auth, headers=HEADERS)
    try:
        if response.status_code == 200:
            dev_alarm = (json.loads(response.text))
            if 'alarm' in dev_alarm:
                return dev_alarm['alarm']
            else:
                return "Device has no alarms"
    except requests.exceptions.RequestException as error:
        return "Error:\n" + str(error) + ' get_dev_alarms: An Error has occured'

Response: function takes the devId of a specific device and issues a RESTFUL call to get the current alarms for that device.


Instruction:
How would you explain what the following Python 3 function does
def get_realtime_alarm(username, auth, url):
    f_url = url + "/imcrs/fault/faultRealTime?operatorName=" + username
    response = requests.get(f_url, auth=auth, headers=HEADERS)
    try:
        realtime_alarm_list = (json.loads(response.text))
        return realtime_alarm_list['faultRealTime']['faultRealTimeList']
    except requests.exceptions.RequestException as error:
        return "Error:\n" + str(error) + ' get_realtime_alarm: An Error has occured'

Response: Function takes in no param as input to fetch Real - Time Alarms from HP IMC RESTFUL API


Instruction:
Can you generate the documentation for the following Python 3 function
def get_alarms(username, auth, url):
    f_url = url + "/imcrs/fault/alarm?operatorName=" + username + \
                     "&recStatus=0&ackStatus=0&timeRange=0&size=50&desc=true"
    response = requests.get(f_url, auth=auth, headers=HEADERS)
    try:
        if response.status_code == 200:
            alarm_list = (json.loads(response.text))
            return alarm_list['alarm']
    except requests.exceptions.RequestException as error:
        return "Error:\n" + str(error) + ' get_alarms: An Error has occured'

Response: Function takes in no param as input to fetch Real - Time Alarms from HP IMC RESTFUL API


Instruction:
Implement a Python 3 function for
functioning to take str input of alarm_id issues a REST call to the IMC REST interface and returns the details of a specific alarm

Response:
def get_alarm_details(alarm_id, auth, url):
    """
    function to take str input of alarm_id, issues a REST call to the IMC REST interface and
    returns a dictionary object which contains the  details of a specific alarm.
    :param alarm_id: str number which represents the internal ID of a specific alarm
    :param auth:
    :param url:
    :return:
    """
    f_url = url + "/imcrs/fault/alarm/" + str(alarm_id)
    response = requests.get(f_url, auth=auth, headers=HEADERS)
    try:
        alarm_details = json.loads(response.text)
        return alarm_details
    except requests.exceptions.RequestException as error:
        return "Error:\n" + str(error) + ' get_alarm_details: An Error has occured'


Instruction:
Implement a function in Python 3 to
function sends to REST API to acknowledge the specified alarm in the IMC alarm database.

Response:
def acknowledge_alarm(alarm_id, auth, url):
    """
    Function tasks input of str of alarm ID and sends to REST API. Function will acknowledge
    designated alarm in the IMC alarm database.
    :param alarm_id: str of alarm ID
    param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class

    :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass
    :return: integer HTTP response code

    :rtype int
    """
    f_url = url + "/imcrs/fault/alarm/acknowledge/"+str(alarm_id)
    response = requests.put(f_url, auth=auth, headers=HEADERS)
    try:
        return response.status_code
    except requests.exceptions.RequestException as error:
        return "Error:\n" + str(error) + ' get_alarms: An Error has occured'


Instruction:
Can you generate the documentation for the following Python 3 function
def create_operator(operator, auth, url,headers=HEADERS):
    create_operator_url = '/imcrs/plat/operator'
    f_url = url + create_operator_url
    payload = json.dumps(operator, indent=4)
    # creates the URL using the payload variable as the contents
    r = requests.post(f_url, data=payload, auth=auth, headers=headers)
    try:
        if r.status_code == 409:
            #print("Operator Already Exists")
            return r.status_code
        elif r.status_code == 201:
            return r.status_code
    except requests.exceptions.RequestException as e:
            return "Error:\n" + str(e) + ' create_operator: An Error has occured'

Response: Function creates a new operator in HPE IMC Restful API


Instruction:
How would you implement a function in Python 3 that
lists virtual folders that belongs to the current user.

Response:
def list():
    '''List virtual folders that belongs to the current user.'''
    fields = [
        ('Name', 'name'),
        ('ID', 'id'),
        ('Owner', 'is_owner'),
        ('Permission', 'permission'),
    ]
    with Session() as session:
        try:
            resp = session.VFolder.list()
            if not resp:
                print('There is no virtual folders created yet.')
                return
            rows = (tuple(vf[key] for _, key in fields) for vf in resp)
            hdrs = (display_name for display_name, _ in fields)
            print(tabulate(rows, hdrs))
        except Exception as e:
            print_error(e)
            sys.exit(1)


Instruction:
How would you explain what the following Python 3 function does
def list_hosts():
    '''List the hosts of virtual folders that is accessible to the current user.'''
    with Session() as session:
        try:
            resp = session.VFolder.list_hosts()
            print(f"Default vfolder host: {resp['default']}")
            print(f"Usable hosts: {', '.join(resp['allowed'])}")
        except Exception as e:
            print_error(e)
            sys.exit(1)

Response: List the hosts of virtual folders that is accessible to the current user.


Instruction:
Given the following Python 3 function, write the documentation
def create(name, host):
    '''Create a new virtual folder.

    \b
    NAME: Name of a virtual folder.
    HOST: Name of a virtual folder host in which the virtual folder will be created.
    '''
    with Session() as session:
        try:
            result = session.VFolder.create(name, host)
            print('Virtual folder "{0}" is created.'.format(result['name']))
        except Exception as e:
            print_error(e)
            sys.exit(1)

Response: Create a new virtual folder.


Instruction:
Can you create a Python 3 function that
deletes the given virtual folder. This operation is irreversible!

Response:
def delete(name):
    '''Delete the given virtual folder. This operation is irreversible!

    NAME: Name of a virtual folder.
    '''
    with Session() as session:
        try:
            session.VFolder(name).delete()
            print_done('Deleted.')
        except Exception as e:
            print_error(e)
            sys.exit(1)


Instruction:
Can you write a function in Python 3 where it
renames the given virtual folder.

Response:
def rename(old_name, new_name):
    '''Rename the given virtual folder. This operation is irreversible!
    You cannot change the vfolders that are shared by other users,
    and the new name must be unique among all your accessible vfolders
    including the shared ones.

    OLD_NAME: The current name of a virtual folder.
    NEW_NAME: The new name of a virtual folder.
    '''
    with Session() as session:
        try:
            session.VFolder(old_name).rename(new_name)
            print_done('Renamed.')
        except Exception as e:
            print_error(e)
            sys.exit(1)


Instruction:
Can you generate the documentation for the following Python 3 function
def info(name):
    '''Show the information of the given virtual folder.

    NAME: Name of a virtual folder.
    '''
    with Session() as session:
        try:
            result = session.VFolder(name).info()
            print('Virtual folder "{0}" (ID: {1})'
                  .format(result['name'], result['id']))
            print('- Owner:', result['is_owner'])
            print('- Permission:', result['permission'])
            print('- Number of files: {0}'.format(result['numFiles']))
        except Exception as e:
            print_error(e)
            sys.exit(1)

Response: Show the information of the given virtual folder.


Instruction:
Can you tell what is the following Python 3 function doing
def download(name, filenames):
    '''
    Download a file from the virtual folder to the current working directory.
    The files with the same names will be overwirtten.

    \b
    NAME: Name of a virtual folder.
    FILENAMES: Paths of the files to be uploaded.
    '''
    with Session() as session:
        try:
            session.VFolder(name).download(filenames, show_progress=True)
            print_done('Done.')
        except Exception as e:
            print_error(e)
            sys.exit(1)

Response: Downloads a file from the virtual folder to the current working directory.


Instruction:
Can you create a Python 3 function that
creates an empty directory in the virtual folder.

Response:
def mkdir(name, path):
    '''Create an empty directory in the virtual folder.

    \b
    NAME: Name of a virtual folder.
    PATH: The name or path of directory. Parent directories are created automatically
          if they do not exist.
    '''
    with Session() as session:
        try:
            session.VFolder(name).mkdir(path)
            print_done('Done.')
        except Exception as e:
            print_error(e)
            sys.exit(1)


Instruction:
How would you implement a function in Python 3 that
deletes files in a virtual folder.

Response:
def rm(name, filenames, recursive):
    '''
    Delete files in a virtual folder.
    If one of the given paths is a directory and the recursive option is enabled,
    all its content and the directory itself are recursively deleted.

    This operation is irreversible!

    \b
    NAME: Name of a virtual folder.
    FILENAMES: Paths of the files to delete.
    '''
    with Session() as session:
        try:
            if input("> Are you sure? (y/n): ").lower().strip()[:1] == 'y':
                session.VFolder(name).delete_files(
                    filenames,
                    recursive=recursive)
                print_done('Done.')
        except Exception as e:
            print_error(e)
            sys.exit(1)


Instruction:
How would you code a function in Python 3 to
list files in a virtual folder.

Response:
def ls(name, path):
    """
    List files in a path of a virtual folder.

    \b
    NAME: Name of a virtual folder.
    PATH: Path inside vfolder.
    """
    with Session() as session:
        try:
            print_wait('Retrieving list of files in "{}"...'.format(path))
            result = session.VFolder(name).list_files(path)
            if 'error_msg' in result and result['error_msg']:
                print_fail(result['error_msg'])
                return
            files = json.loads(result['files'])
            table = []
            headers = ['file name', 'size', 'modified', 'mode']
            for file in files:
                mdt = datetime.fromtimestamp(file['mtime'])
                mtime = mdt.strftime('%b %d %Y %H:%M:%S')
                row = [file['filename'], file['size'], mtime, file['mode']]
                table.append(row)
            print_done('Retrived.')
            print(tabulate(table, headers=headers))
        except Exception as e:
            print_error(e)


Instruction:
Given the following Python 3 function, write the documentation
def invite(name, emails, perm):
    with Session() as session:
        try:
            assert perm in ['rw', 'ro'], \
                   'Invalid permission: {}'.format(perm)
            result = session.VFolder(name).invite(perm, emails)
            invited_ids = result.get('invited_ids', [])
            if len(invited_ids) > 0:
                print('Invitation sent to:')
                for invitee in invited_ids:
                    print('\t- ' + invitee)
            else:
                print('No users found. Invitation was not sent.')
        except Exception as e:
            print_error(e)
            sys.exit(1)

Response: Invite other users to access the virtual folder.


Instruction:
Can you implement a function in Python 3 that
lists and manage received invitations.

Response:
def invitations():
    """List and manage received invitations.
    """
    with Session() as session:
        try:
            result = session.VFolder.invitations()
            invitations = result.get('invitations', [])
            if len(invitations) < 1:
                print('No invitations.')
                return
            print('List of invitations (inviter, vfolder id, permission):')
            for cnt, inv in enumerate(invitations):
                if inv['perm'] == 'rw':
                    perm = 'read-write'
                elif inv['perm'] == 'ro':
                    perm = 'read-only'
                else:
                    perm = inv['perm']
                print('[{}] {}, {}, {}'.format(cnt + 1, inv['inviter'],
                                               inv['vfolder_id'], perm))

            selection = input('Choose invitation number to manage: ')
            if selection.isdigit():
                selection = int(selection) - 1
            else:
                return
            if 0 <= selection < len(invitations):
                while True:
                    action = input('Choose action. (a)ccept, (r)eject, (c)ancel: ')
                    if action.lower() == 'a':
                        # TODO: Let user can select access_key among many.
                        #       Currently, the config objects holds only one key.
                        config = get_config()
                        result = session.VFolder.accept_invitation(
                            invitations[selection]['id'], config.access_key)
                        print(result['msg'])
                        break
                    elif action.lower() == 'r':
                        result = session.VFolder.delete_invitation(
                            invitations[selection]['id'])
                        print(result['msg'])
                        break
                    elif action.lower() == 'c':
                        break
        except Exception as e:
            print_error(e)
            sys.exit(1)


Instruction:
Create a Python 3 function for
adding a given check callback with the provided object to the list of checks. Useful for built - ins advanced custom checks.

Response:
def init_check(self, check, obj):
        """
        Adds a given check callback with the provided object to the list
        of checks. Useful for built-ins but also advanced custom checks.
        """
        self.logger.info('Adding extension check %s' % check.__name__)
        check = functools.wraps(check)(functools.partial(check, obj))
        self.check(func=check)


Instruction:
Create a Python 3 function for
initializing the extension with the given app.

Response:
def init_app(self, app):
        """
        Initializes the extension with the given app, registers the
        built-in views with an own blueprint and hooks up our signal
        callbacks.
        """
        # If no version path was provided in the init of the Dockerflow
        # class we'll use the parent directory of the app root path.
        if self.version_path is None:
            self.version_path = os.path.dirname(app.root_path)

        for view in (
            ('/__version__', 'version', self._version_view),
            ('/__heartbeat__', 'heartbeat', self._heartbeat_view),
            ('/__lbheartbeat__', 'lbheartbeat', self._lbheartbeat_view),
        ):
            self._blueprint.add_url_rule(*view)
        self._blueprint.before_app_request(self._before_request)
        self._blueprint.after_app_request(self._after_request)
        self._blueprint.app_errorhandler(HeartbeatFailure)(self._heartbeat_exception_handler)
        app.register_blueprint(self._blueprint)
        got_request_exception.connect(self._got_request_exception, sender=app)

        if not hasattr(app, 'extensions'):  # pragma: nocover
            app.extensions = {}
        app.extensions['dockerflow'] = self


Instruction:
Make a summary of the following Python 3 code
def _after_request(self, response):
        if not getattr(g, '_has_exception', False):
            extra = self.summary_extra()
            self.summary_logger.info('', extra=extra)
        return response

Response: Called by the HTTP server when the request has finished.


Instruction:
How would you code a function in Python 3 to
call when a request exception occurs.

Response:
def _got_request_exception(self, sender, exception, **extra):
        """
        The signal handler for the got_request_exception signal.
        """
        extra = self.summary_extra()
        extra['errno'] = 500
        self.summary_logger.error(str(exception), extra=extra)
        g._has_exception = True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def user_id(self):
        # This needs flask-login to be installed
        if not has_flask_login:
            return

        # and the actual login manager installed
        if not hasattr(current_app, 'login_manager'):
            return

        # fail if no current_user was attached to the request context
        try:
            is_authenticated = current_user.is_authenticated
        except AttributeError:
            return

        # because is_authenticated could be a callable, call it
        if callable(is_authenticated):
            is_authenticated = is_authenticated()

        # and fail if the user isn't authenticated
        if not is_authenticated:
            return

        # finally return the user id
        return current_user.get_id()

Response: Return the ID of the current user


Instruction:
Given the following Python 3 function, write the documentation
def summary_extra(self):
        out = {
            'errno': 0,
            'agent': request.headers.get('User-Agent', ''),
            'lang': request.headers.get('Accept-Language', ''),
            'method': request.method,
            'path': request.path,
        }

        # set the uid value to the current user ID
        user_id = self.user_id()
        if user_id is None:
            user_id = ''
        out['uid'] = user_id

        # the rid value to the current request ID
        request_id = g.get('_request_id', None)
        if request_id is not None:
            out['rid'] = request_id

        # and the t value to the time it took to render
        start_timestamp = g.get('_start_timestamp', None)
        if start_timestamp is not None:
            # Duration of request, in milliseconds.
            out['t'] = int(1000 * (time.time() - start_timestamp))

        return out

Response: Build the extra data for the summary logger.


Instruction:
Write a Python 3 function that can
view that returns the contents of version. json or a 404.

Response:
def _version_view(self):
        """
        View that returns the contents of version.json or a 404.
        """
        version_json = self._version_callback(self.version_path)
        if version_json is None:
            return 'version.json not found', 404
        else:
            return jsonify(version_json)


Instruction:
Can you generate the documentation for the following Python 3 function
def _heartbeat_view(self):
        details = {}
        statuses = {}
        level = 0

        for name, check in self.checks.items():
            detail = self._heartbeat_check_detail(check)
            statuses[name] = detail['status']
            level = max(level, detail['level'])
            if detail['level'] > 0:
                details[name] = detail

        payload = {
            'status': checks.level_to_text(level),
            'checks': statuses,
            'details': details,
        }

        def render(status_code):
            return make_response(jsonify(payload), status_code)

        if level < checks.WARNING:
            status_code = 200
            heartbeat_passed.send(self, level=level)
            return render(status_code)
        else:
            status_code = 500
            heartbeat_failed.send(self, level=level)
            raise HeartbeatFailure(response=render(status_code))

Response: Runs all the registered checks and returns a JSON response with either a status code of 200 or 500 depending on the results of the checks.


Instruction:
Can you generate the documentation for the following Python 3 function
def drange(start: Decimal, stop: Decimal, num: int):
    '''
    A simplified version of numpy.linspace with default options
    '''
    delta = stop - start
    step = delta / (num - 1)
    yield from (start + step * Decimal(tick) for tick in range(0, num))

Response: A simplified version of numpy. linspace with default options


Instruction:
Can you generate a brief explanation for the following Python 3 code
def range_expr(arg):
    '''
    Accepts a range expression which generates a range of values for a variable.

    Linear space range: "linspace:1,2,10" (start, stop, num) as in numpy.linspace
    Pythonic range: "range:1,10,2" (start, stop[, step]) as in Python's range
    Case range: "case:a,b,c" (comma-separated strings)
    '''
    key, value = arg.split('=', maxsplit=1)
    assert _rx_range_key.match(key), 'The key must be a valid slug string.'
    try:
        if value.startswith('case:'):
            return key, value[5:].split(',')
        elif value.startswith('linspace:'):
            start, stop, num = value[9:].split(',')
            return key, tuple(drange(Decimal(start), Decimal(stop), int(num)))
        elif value.startswith('range:'):
            range_args = map(int, value[6:].split(','))
            return key, tuple(range(*range_args))
        else:
            raise ArgumentTypeError('Unrecognized range expression type')
    except ValueError as e:
        raise ArgumentTypeError(str(e))

Response: Generates a range expression which generates a range of values for a variable.


Instruction:
How would you explain what the following Python 3 function does
async def exec_loop(stdout, stderr, kernel, mode, code, *, opts=None,
                    vprint_done=print_done, is_multi=False):
    '''
    Fully streamed asynchronous version of the execute loop.
    '''
    async with kernel.stream_execute(code, mode=mode, opts=opts) as stream:
        async for result in stream:
            if result.type == aiohttp.WSMsgType.TEXT:
                result = json.loads(result.data)
            else:
                # future extension
                continue
            for rec in result.get('console', []):
                if rec[0] == 'stdout':
                    print(rec[1], end='', file=stdout)
                elif rec[0] == 'stderr':
                    print(rec[1], end='', file=stderr)
                else:
                    print('----- output record (type: {0}) -----'.format(rec[0]),
                          file=stdout)
                    print(rec[1], file=stdout)
                    print('----- end of record -----', file=stdout)
            stdout.flush()
            files = result.get('files', [])
            if files:
                print('--- generated files ---', file=stdout)
                for item in files:
                    print('{0}: {1}'.format(item['name'], item['url']), file=stdout)
                print('--- end of generated files ---', file=stdout)
            if result['status'] == 'clean-finished':
                exitCode = result.get('exitCode')
                msg = 'Clean finished. (exit code = {0})'.format(exitCode)
                if is_multi:
                    print(msg, file=stderr)
                vprint_done(msg)
            elif result['status'] == 'build-finished':
                exitCode = result.get('exitCode')
                msg = 'Build finished. (exit code = {0})'.format(exitCode)
                if is_multi:
                    print(msg, file=stderr)
                vprint_done(msg)
            elif result['status'] == 'finished':
                exitCode = result.get('exitCode')
                msg = 'Execution finished. (exit code = {0})'.format(exitCode)
                if is_multi:
                    print(msg, file=stderr)
                vprint_done(msg)
                break
            elif result['status'] == 'waiting-input':
                if result['options'].get('is_password', False):
                    code = getpass.getpass()
                else:
                    code = input()
                await stream.send_str(code)
            elif result['status'] == 'continued':
                pass

Response: Execute the code in a single asynchronous thread.


Instruction:
Can you tell what is the following Python 3 function doing
def exec_loop_sync(stdout, stderr, kernel, mode, code, *, opts=None,
                   vprint_done=print_done):
    '''
    Old synchronous polling version of the execute loop.
    '''
    opts = opts if opts else {}
    run_id = None  # use server-assigned run ID
    while True:
        result = kernel.execute(run_id, code, mode=mode, opts=opts)
        run_id = result['runId']
        opts.clear()  # used only once
        for rec in result['console']:
            if rec[0] == 'stdout':
                print(rec[1], end='', file=stdout)
            elif rec[0] == 'stderr':
                print(rec[1], end='', file=stderr)
            else:
                print('----- output record (type: {0}) -----'.format(rec[0]),
                      file=stdout)
                print(rec[1], file=stdout)
                print('----- end of record -----', file=stdout)
        stdout.flush()
        files = result.get('files', [])
        if files:
            print('--- generated files ---', file=stdout)
            for item in files:
                print('{0}: {1}'.format(item['name'], item['url']), file=stdout)
            print('--- end of generated files ---', file=stdout)
        if result['status'] == 'clean-finished':
            exitCode = result.get('exitCode')
            vprint_done('Clean finished. (exit code = {0}'.format(exitCode),
                        file=stdout)
            mode = 'continue'
            code = ''
        elif result['status'] == 'build-finished':
            exitCode = result.get('exitCode')
            vprint_done('Build finished. (exit code = {0})'.format(exitCode),
                        file=stdout)
            mode = 'continue'
            code = ''
        elif result['status'] == 'finished':
            exitCode = result.get('exitCode')
            vprint_done('Execution finished. (exit code = {0})'.format(exitCode),
                        file=stdout)
            break
        elif result['status'] == 'waiting-input':
            mode = 'input'
            if result['options'].get('is_password', False):
                code = getpass.getpass()
            else:
                code = input()
        elif result['status'] == 'continued':
            mode = 'continue'
            code = ''

Response: Execute a single kernel loop.


Instruction:
Explain what the following Python 3 code does
def run(lang, files, session_id, cluster_size, code, clean, build, exec, terminal,
        basedir, rm, env, env_range, build_range, exec_range, max_parallel, mount,
        stats, tag, resources, quiet):
    '''
    Run the given code snippet or files in a session.
    Depending on the session ID you give (default is random),
    it may reuse an existing session or create a new one.

    \b
    LANG: The name (and version/platform tags appended after a colon) of session
          runtime or programming language.')
    FILES: The code file(s). Can be added multiple times.
    '''
    if quiet:
        vprint_info = vprint_wait = vprint_done = _noop
    else:
        vprint_info = print_info
        vprint_wait = print_wait
        vprint_done = print_done
    if files and code:
        print('You can run only either source files or command-line '
              'code snippet.', file=sys.stderr)
        sys.exit(1)
    if not files and not code:
        print('You should provide the command-line code snippet using '
              '"-c" option if run without files.', file=sys.stderr)
        sys.exit(1)

    envs = _prepare_env_arg(env)
    resources = _prepare_resource_arg(resources)
    mount = _prepare_mount_arg(mount)

    if not (1 <= cluster_size < 4):
        print('Invalid cluster size.', file=sys.stderr)
        sys.exit(1)

    if env_range is None: env_range = []      # noqa
    if build_range is None: build_range = []  # noqa
    if exec_range is None: exec_range = []    # noqa

    env_ranges = {v: r for v, r in env_range}
    build_ranges = {v: r for v, r in build_range}
    exec_ranges = {v: r for v, r in exec_range}

    env_var_maps = [dict(zip(env_ranges.keys(), values))
                    for values in itertools.product(*env_ranges.values())]
    build_var_maps = [dict(zip(build_ranges.keys(), values))
                      for values in itertools.product(*build_ranges.values())]
    exec_var_maps = [dict(zip(exec_ranges.keys(), values))
                     for values in itertools.product(*exec_ranges.values())]
    case_set = collections.OrderedDict()
    vmaps_product = itertools.product(env_var_maps, build_var_maps, exec_var_maps)
    build_template = string.Template(build)
    exec_template = string.Template(exec)
    env_templates = {k: string.Template(v) for k, v in envs.items()}
    for env_vmap, build_vmap, exec_vmap in vmaps_product:
        interpolated_envs = tuple((k, vt.substitute(env_vmap))
                                  for k, vt in env_templates.items())
        if build:
            interpolated_build = build_template.substitute(build_vmap)
        else:
            interpolated_build = '*'
        if exec:
            interpolated_exec = exec_template.substitute(exec_vmap)
        else:
            interpolated_exec = '*'
        case_set[(interpolated_envs, interpolated_build, interpolated_exec)] = 1

    is_multi = (len(case_set) > 1)
    if is_multi:
        if max_parallel <= 0:
            print('The number maximum parallel sessions must be '
                  'a positive integer.', file=sys.stderr)
            sys.exit(1)
        if terminal:
            print('You cannot run multiple cases with terminal.', file=sys.stderr)
            sys.exit(1)
        if not quiet:
            vprint_info('Running multiple sessions for the following combinations:')
            for case in case_set.keys():
                pretty_env = ' '.join('{}={}'.format(item[0], item[1])
                                      for item in case[0])
                print('env = {!r}, build = {!r}, exec = {!r}'
                      .format(pretty_env, case[1], case[2]))

    def _run_legacy(session, idx, session_id, envs,
                    clean_cmd, build_cmd, exec_cmd):
        try:
            kernel = session.Kernel.get_or_create(
                lang,
                client_token=session_id,
                cluster_size=cluster_size,
                mounts=mount,
                envs=envs,
                resources=resources,
                tag=tag)
        except Exception as e:
            print_error(e)
            sys.exit(1)
        if kernel.created:
            vprint_done('[{0}] Session {0} is ready.'.format(idx, kernel.kernel_id))
        else:
            vprint_done('[{0}] Reusing session {0}...'.format(idx, kernel.kernel_id))
        if kernel.service_ports:
            print_info('This session provides the following app services: '
                       ', '.join(sport['name'] for sport in kernel.service_ports))

        try:
            if files:
                vprint_wait('[{0}] Uploading source files...'.format(idx))
                ret = kernel.upload(files, basedir=basedir,
                                    show_progress=True)
                if ret.status // 100 != 2:
                    print_fail('[{0}] Uploading source files failed!'.format(idx))
                    print('{0}: {1}\n{2}'.format(
                        ret.status, ret.reason, ret.text()))
                    return
                vprint_done('[{0}] Uploading done.'.format(idx))
                opts = {
                    'clean': clean_cmd,
                    'build': build_cmd,
                    'exec': exec_cmd,
                }
                if not terminal:
                    exec_loop_sync(sys.stdout, sys.stderr, kernel, 'batch', '',
                                   opts=opts,
                                   vprint_done=vprint_done)
            if terminal:
                raise NotImplementedError('Terminal access is not supported in '
                                          'the legacy synchronous mode.')
            if code:
                exec_loop_sync(sys.stdout, sys.stderr, kernel, 'query', code,
                               vprint_done=vprint_done)
            vprint_done('[{0}] Execution finished.'.format(idx))
        except Exception as e:
            print_error(e)
            sys.exit(1)
        finally:
            if rm:
                vprint_wait('[{0}] Cleaning up the session...'.format(idx))
                ret = kernel.destroy()
                vprint_done('[{0}] Cleaned up the session.'.format(idx))
                if stats:
                    _stats = ret.get('stats', None) if ret else None
                    if _stats:
                        _stats.pop('precpu_used', None)
                        _stats.pop('precpu_system_used', None)
                        _stats.pop('cpu_system_used', None)
                        print('[{0}] Statistics:\n{1}'
                              .format(idx, _format_stats(_stats)))
                    else:
                        print('[{0}] Statistics is not available.'.format(idx))

    async def _run(session, idx, session_id, envs,
                   clean_cmd, build_cmd, exec_cmd,
                   is_multi=False):
        try:
            kernel = await session.Kernel.get_or_create(
                lang,
                client_token=session_id,
                cluster_size=cluster_size,
                mounts=mount,
                envs=envs,
                resources=resources,
                tag=tag)
        except BackendError as e:
            print_fail('[{0}] {1}'.format(idx, e))
            return
        if kernel.created:
            vprint_done('[{0}] Session {1} is ready.'.format(idx, kernel.kernel_id))
        else:
            vprint_done('[{0}] Reusing session {1}...'.format(idx, kernel.kernel_id))

        if not is_multi:
            stdout = sys.stdout
            stderr = sys.stderr
        else:
            log_dir = Path.home() / '.cache' / 'backend.ai' / 'client-logs'
            log_dir.mkdir(parents=True, exist_ok=True)
            stdout = open(log_dir / '{0}.stdout.log'.format(session_id),
                          'w', encoding='utf-8')
            stderr = open(log_dir / '{0}.stderr.log'.format(session_id),
                          'w', encoding='utf-8')

        try:
            def indexed_vprint_done(msg):
                vprint_done('[{0}] '.format(idx) + msg)
            if files:
                if not is_multi:
                    vprint_wait('[{0}] Uploading source files...'.format(idx))
                ret = await kernel.upload(files, basedir=basedir,
                                          show_progress=not is_multi)
                if ret.status // 100 != 2:
                    print_fail('[{0}] Uploading source files failed!'.format(idx))
                    print('{0}: {1}\n{2}'.format(
                        ret.status, ret.reason, ret.text()), file=stderr)
                    raise RuntimeError('Uploading source files has failed!')
                if not is_multi:
                    vprint_done('[{0}] Uploading done.'.format(idx))
                opts = {
                    'clean': clean_cmd,
                    'build': build_cmd,
                    'exec': exec_cmd,
                }
                if not terminal:
                    await exec_loop(stdout, stderr, kernel, 'batch', '',
                                    opts=opts,
                                    vprint_done=indexed_vprint_done,
                                    is_multi=is_multi)
            if terminal:
                await exec_terminal(kernel)
                return
            if code:
                await exec_loop(stdout, stderr, kernel, 'query', code,
                                vprint_done=indexed_vprint_done,
                                is_multi=is_multi)
        except BackendError as e:
            print_fail('[{0}] {1}'.format(idx, e))
            raise RuntimeError(e)
        except Exception as e:
            print_fail('[{0}] Execution failed!'.format(idx))
            traceback.print_exc()
            raise RuntimeError(e)
        finally:
            try:
                if rm:
                    if not is_multi:
                        vprint_wait('[{0}] Cleaning up the session...'.format(idx))
                    ret = await kernel.destroy()
                    vprint_done('[{0}] Cleaned up the session.'.format(idx))
                    if stats:
                        _stats = ret.get('stats', None) if ret else None
                        if _stats:
                            _stats.pop('precpu_used', None)
                            _stats.pop('precpu_system_used', None)
                            _stats.pop('cpu_system_used', None)
                            stats_str = _format_stats(_stats)
                            print(format_info('[{0}] Statistics:'.format(idx)) +
                                  '\n{0}'.format(stats_str))
                            if is_multi:
                                print('Statistics:\n{0}'.format(stats_str),
                                      file=stderr)
                        else:
                            print_warn('[{0}] Statistics: unavailable.'.format(idx))
                            if is_multi:
                                print('Statistics: unavailable.', file=stderr)
            finally:
                if is_multi:
                    stdout.close()
                    stderr.close()

    def _run_cases_legacy():
        if session_id is None:
            session_id_prefix = token_hex(5)
        else:
            session_id_prefix = session_id
        vprint_info('Session token prefix: {0}'.format(session_id_prefix))
        vprint_info('In the legacy mode, all cases will run serially!')
        with Session() as session:
            for idx, case in enumerate(case_set.keys()):
                if is_multi:
                    _session_id = '{0}-{1}'.format(session_id_prefix, idx)
                else:
                    _session_id = session_id_prefix
                envs = dict(case[0])
                clean_cmd = clean if clean else '*'
                build_cmd = case[1]
                exec_cmd = case[2]
                _run_legacy(session, idx, _session_id, envs,
                            clean_cmd, build_cmd, exec_cmd)

    async def _run_cases():
        loop = current_loop()
        if session_id is None:
            session_id_prefix = token_hex(5)
        else:
            session_id_prefix = session_id
        vprint_info('Session token prefix: {0}'.format(session_id_prefix))
        if is_multi:
            print_info('Check out the stdout/stderr logs stored in '
                       '~/.cache/backend.ai/client-logs directory.')
        async with AsyncSession() as session:
            tasks = []
            # TODO: limit max-parallelism using aiojobs
            for idx, case in enumerate(case_set.keys()):
                if is_multi:
                    _session_id = '{0}-{1}'.format(session_id_prefix, idx)
                else:
                    _session_id = session_id_prefix
                envs = dict(case[0])
                clean_cmd = clean if clean else '*'
                build_cmd = case[1]
                exec_cmd = case[2]
                t = loop.create_task(
                    _run(session, idx, _session_id, envs,
                         clean_cmd, build_cmd, exec_cmd,
                         is_multi=is_multi))
                tasks.append(t)
            results = await asyncio.gather(*tasks, return_exceptions=True)
            if any(map(lambda r: isinstance(r, Exception), results)):
                if is_multi:
                    print_fail('There were failed cases!')
                sys.exit(1)

    if is_legacy_server():
        _run_cases_legacy()
    else:
        loop = asyncio.get_event_loop()
        try:
            loop.run_until_complete(_run_cases())
        finally:
            loop.close()

Response: Runs the given code snippet or files in a session.


Instruction:
Implement a function in Python 3 to
start a single compute session.

Response:
def start(lang, session_id, owner, env, mount, tag, resources, cluster_size):
    '''
    Prepare and start a single compute session without executing codes.
    You may use the created session to execute codes using the "run" command
    or connect to an application service provided by the session using the "app"
    command.

    \b
    LANG: The name (and version/platform tags appended after a colon) of session
          runtime or programming language.
    '''
    if session_id is None:
        session_id = token_hex(5)
    else:
        session_id = session_id

    ######
    envs = _prepare_env_arg(env)
    resources = _prepare_resource_arg(resources)
    mount = _prepare_mount_arg(mount)
    with Session() as session:
        try:
            kernel = session.Kernel.get_or_create(
                lang,
                client_token=session_id,
                cluster_size=cluster_size,
                mounts=mount,
                envs=envs,
                resources=resources,
                owner_access_key=owner,
                tag=tag)
        except Exception as e:
            print_error(e)
            sys.exit(1)
        else:
            if kernel.created:
                print_info('Session ID {0} is created and ready.'
                           .format(session_id))
            else:
                print_info('Session ID {0} is already running and ready.'
                           .format(session_id))
            if kernel.service_ports:
                print_info('This session provides the following app services: ' +
                           ', '.join(sport['name']
                                     for sport in kernel.service_ports))


Instruction:
Given the following Python 3 function, write the documentation
def terminate(sess_id_or_alias, owner, stats):
    '''
    Terminate the given session.

    SESSID: session ID or its alias given when creating the session.
    '''
    print_wait('Terminating the session(s)...')
    with Session() as session:
        has_failure = False
        for sess in sess_id_or_alias:
            try:
                kernel = session.Kernel(sess, owner)
                ret = kernel.destroy()
            except BackendAPIError as e:
                print_error(e)
                if e.status == 404:
                    print_info(
                        'If you are an admin, use "-o" / "--owner" option '
                        'to terminate other user\'s session.')
                has_failure = True
            except Exception as e:
                print_error(e)
                has_failure = True
            if has_failure:
                sys.exit(1)
        else:
            print_done('Done.')
            if stats:
                stats = ret.get('stats', None) if ret else None
                if stats:
                    print(_format_stats(stats))
                else:
                    print('Statistics is not available.')

Response: Terminate the given session.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_custom_view_details(name, auth, url):
    view_id = get_custom_views(auth, url, name=name)[0]['symbolId']
    get_custom_view_details_url = '/imcrs/plat/res/view/custom/' + str(view_id)
    f_url = url + get_custom_view_details_url
    r = requests.get(f_url, auth=auth,
                     headers=HEADERS)  # creates the URL using the payload variable as the contents
    try:
        if r.status_code == 200:
            current_devices = (json.loads(r.text))
            if 'device' in current_devices:
                return current_devices['device']
            else:
                return []
    except requests.exceptions.RequestException as e:
        return "Error:\n" + str(e) + ' get_custom_views: An Error has occured'

Response: function takes name auth object and url and returns a list of dictionaries containing the details of the custom views


Instruction:
Write a Python 3 function for
functioning to add a list of devices to a specific custom views from HPE IMC

Response:
def add_devs_custom_views(custom_view_name, dev_list, auth, url):
    """
    function takes a list of devIDs from devices discovered in the HPE IMC platform and and issues a RESTFUL call to
     add the list of devices to a specific custom views from HPE IMC.

    :param dev_list: list containing the devID of all devices to be contained in this custom view.

    :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class

    :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass

    :return: str of creation results ( "view " + name + "created successfully"

    :rtype: str

    >>> from pyhpeimc.auth import *

    >>> from pyhpeimc.plat.groups import *

    >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")

    """
    view_id = get_custom_views(auth, url, name=custom_view_name)[0]['symbolId']
    add_devs_custom_views_url = '/imcrs/plat/res/view/custom/'+str(view_id)
    payload = '''{"device" : '''+ json.dumps(dev_list) + '''}'''
    f_url = url + add_devs_custom_views_url
    r = requests.put(f_url, data = payload, auth=auth, headers=HEADERS)  # creates the URL using the payload variable as the contents
    try:
        if r.status_code == 204:
            print ('View ' + custom_view_name +' : Devices Successfully Added')
            return r.status_code
    except requests.exceptions.RequestException as e:
            return "Error:\n" + str(e) + ' get_custom_views: An Error has occured'


Instruction:
Create a Python 3 function for
functioning takes input of auth, url, and name and issues a RESTFUL call to delete a specific of custom views from HPE IMC. :param name: string containg the name of the desired custom view :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass :return: str of creation results ( "view " + name + "created successfully" :rtype: str >>> from pyhpeimc.auth import * >>> from pyhpeimc.plat.groups import * >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin") >>> delete_custom_view(auth.creds, auth.url, name = "L1 View") 'View L1 View deleted successfully' >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View') >>> assert view_1 == None >>> delete_custom_view(auth.creds, auth.url, name = "L2 View") 'View L2 View deleted successfully' >>> view_2 =get_custom_views( auth.creds, auth.url, name = 'L2 View') >>> assert view_2 == None

Response:
def delete_custom_view(auth, url, name):
    """
    function takes input of auth, url, and name and issues a RESTFUL call to delete a specific of custom views from HPE
    IMC.
    :param name: string containg the name of the desired custom view

    :param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class

    :param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass

    :return: str of creation results ( "view " + name + "created successfully"

    :rtype: str

    >>> from pyhpeimc.auth import *

    >>> from pyhpeimc.plat.groups import *

    >>> auth = IMCAuth("http://", "10.101.0.203", "8080", "admin", "admin")

    >>> delete_custom_view(auth.creds, auth.url, name = "L1 View")
    'View L1 View deleted successfully'

    >>> view_1 =get_custom_views( auth.creds, auth.url, name = 'L1 View')

    >>> assert view_1 == None

    >>> delete_custom_view(auth.creds, auth.url, name = "L2 View")
    'View L2 View deleted successfully'

    >>> view_2 =get_custom_views( auth.creds, auth.url, name = 'L2 View')

    >>> assert view_2 == None

    """
    view_id  = get_custom_views(auth, url,name )[0]['symbolId']
    delete_custom_view_url = '/imcrs/plat/res/view/custom/'+str(view_id)
    f_url = url + delete_custom_view_url
    r = requests.delete(f_url, auth=auth, headers=HEADERS)  # creates the URL using the payload variable as the contents
    try:
        if r.status_code == 204:
            return 'View ' + name +' deleted successfully'
    except requests.exceptions.RequestException as e:
            return "Error:\n" + str(e) + ' delete_custom_view: An Error has occured'


Instruction:
Implement a Python 3 function for
liking unicode. split but accept several separators and regexes.

Response:
def multisplit(string,  # type: unicode
               *separators,  # type: unicode
               **kwargs  # type: Union[unicode, C[..., I[unicode]]]
               ):  # type: (...) -> I
    """ Like unicode.split, but accept several separators and regexes

        Args:
            string: the string to split.
            separators: strings you can split on. Each string can be a
                        regex.
            maxsplit: max number of time you wish to split. default is 0,
                      which means no limit.
            flags: flags you wish to pass if you use regexes. You should
                   pass them as a string containing a combination of:

                    - 'm' for re.MULTILINE
                    - 'x' for re.VERBOSE
                    - 'v' for re.VERBOSE
                    - 's' for re.DOTALL
                    - '.' for re.DOTALL
                    - 'd' for re.DEBUG
                    - 'i' for re.IGNORECASE
                    - 'u' for re.UNICODE
                    - 'l' for re.LOCALE
            cast: what to cast the result to

        Returns:
            An iterable of substrings.

        Raises:
            ValueError: if you pass a flag without separators.
            TypeError: if you pass something else than unicode strings.

        Example:

            >>> for word in multisplit(u'fat     black cat, big'): print(word)
            fat
            black
            cat,
            big
            >>> string = u'a,b;c/d=a,b;c/d'
            >>> chunks = multisplit(string, u',', u';', u'[/=]', maxsplit=4)
            >>> for chunk in chunks: print(chunk)
            a
            b
            c
            d
            a,b;c/d

    """

    cast = kwargs.pop('cast', list)
    flags = parse_re_flags(kwargs.get('flags', 0))
    # 0 means "no limit" for re.split
    maxsplit = require_positive_number(kwargs.get('maxsplit', 0),
                                       'maxsplit')

    # no separator means we use the default unicode.split behavior
    if not separators:
        if flags:
            raise ValueError(ww.s >> """
                             You can't pass flags without passing
                             a separator. Flags only have sense if
                             you split using a regex.
                            """)

        maxsplit = maxsplit or -1  # -1 means "no limit" for unicode.split
        return unicode.split(string, None, maxsplit)

    # Check that all separators are strings
    for i, sep in enumerate(separators):
        if not isinstance(sep, unicode):
            raise TypeError(ww.s >> """
                '{!r}', the separator at index '{}', is of type '{}'.
                multisplit() only accepts unicode strings.
            """.format(sep, i, type(sep)))

    # TODO: split let many empty strings in the result. Fix it.

    seps = list(separators)  # cast to list so we can slice it

    # simple code for when you need to split the whole string
    if maxsplit == 0:
        return cast(_split(string, seps, flags))

    # slow implementation with checks for recursive maxsplit
    return cast(_split_with_max(string, seps, maxsplit, flags))