Instruction:
How would you code a function in Python 3 to
download and parse upper air observations from an online archive and return a pandas. DataFrame containing the data.

Response:
def _get_data(self, time, site_id):
        r"""Download and parse upper air observations from an online archive.

        Parameters
        ----------
        time : datetime
            The date and time of the desired observation.

        site_id : str
            The three letter ICAO identifier of the station for which data should be
            downloaded.

        Returns
        -------
            :class:`pandas.DataFrame` containing the data

        """
        raw_data = self._get_data_raw(time, site_id)
        soup = BeautifulSoup(raw_data, 'html.parser')
        tabular_data = StringIO(soup.find_all('pre')[0].contents[0])
        col_names = ['pressure', 'height', 'temperature', 'dewpoint', 'direction', 'speed']
        df = pd.read_fwf(tabular_data, skiprows=5, usecols=[0, 1, 2, 3, 6, 7], names=col_names)
        df['u_wind'], df['v_wind'] = get_wind_components(df['speed'],
                                                         np.deg2rad(df['direction']))

        # Drop any rows with all NaN values for T, Td, winds
        df = df.dropna(subset=('temperature', 'dewpoint', 'direction', 'speed',
                               'u_wind', 'v_wind'), how='all').reset_index(drop=True)

        # Parse metadata
        meta_data = soup.find_all('pre')[1].contents[0]
        lines = meta_data.splitlines()

        # If the station doesn't have a name identified we need to insert a
        # record showing this for parsing to proceed.
        if 'Station number' in lines[1]:
            lines.insert(1, 'Station identifier: ')

        station = lines[1].split(':')[1].strip()
        station_number = int(lines[2].split(':')[1].strip())
        sounding_time = datetime.strptime(lines[3].split(':')[1].strip(), '%y%m%d/%H%M')
        latitude = float(lines[4].split(':')[1].strip())
        longitude = float(lines[5].split(':')[1].strip())
        elevation = float(lines[6].split(':')[1].strip())

        df['station'] = station
        df['station_number'] = station_number
        df['time'] = sounding_time
        df['latitude'] = latitude
        df['longitude'] = longitude
        df['elevation'] = elevation

        # Add unit dictionary
        df.units = {'pressure': 'hPa',
                    'height': 'meter',
                    'temperature': 'degC',
                    'dewpoint': 'degC',
                    'direction': 'degrees',
                    'speed': 'knot',
                    'u_wind': 'knot',
                    'v_wind': 'knot',
                    'station': None,
                    'station_number': None,
                    'time': None,
                    'latitude': 'degrees',
                    'longitude': 'degrees',
                    'elevation': 'meter'}
        return df


Instruction:
Write a Python 3 script to
download data from the University of Wyoming s upper air archive.

Response:
def _get_data_raw(self, time, site_id):
        """Download data from the University of Wyoming's upper air archive.

        Parameters
        ----------
        time : datetime
            Date and time for which data should be downloaded
        site_id : str
            Site id for which data should be downloaded

        Returns
        -------
        text of the server response

        """
        path = ('?region=naconf&TYPE=TEXT%3ALIST'
                '&YEAR={time:%Y}&MONTH={time:%m}&FROM={time:%d%H}&TO={time:%d%H}'
                '&STNM={stid}').format(time=time, stid=site_id)

        resp = self.get_path(path)
        # See if the return is valid, but has no data
        if resp.text.find('Can\'t') != -1:
            raise ValueError(
                'No data available for {time:%Y-%m-%d %HZ} '
                'for station {stid}.'.format(time=time, stid=site_id))

        return resp.text


Instruction:
Write a Python 3 function that can
handle reading an NcStream v1 data block from a file - like object.

Response:
def read_ncstream_data(fobj):
    """Handle reading an NcStream v1 data block from a file-like object."""
    data = read_proto_object(fobj, stream.Data)
    if data.dataType in (stream.STRING, stream.OPAQUE) or data.vdata:
        log.debug('Reading string/opaque/vlen')
        num_obj = read_var_int(fobj)
        log.debug('Num objects: %d', num_obj)
        blocks = [read_block(fobj) for _ in range(num_obj)]
        if data.dataType == stream.STRING:
            blocks = [b.decode('utf-8', errors='ignore') for b in blocks]

        # Again endian isn't coded properly
        dt = data_type_to_numpy(data.dataType).newbyteorder('>')
        if data.vdata:
            return np.array([np.frombuffer(b, dtype=dt) for b in blocks])
        else:
            return np.array(blocks, dtype=dt)
    elif data.dataType in _dtypeLookup:
        log.debug('Reading array data')
        bin_data = read_block(fobj)
        log.debug('Binary data: %s', bin_data)

        # Hard code to big endian for now since it's not encoded correctly
        dt = data_type_to_numpy(data.dataType).newbyteorder('>')

        # Handle decompressing the bytes
        if data.compress == stream.DEFLATE:
            bin_data = zlib.decompress(bin_data)
            assert len(bin_data) == data.uncompressedSize
        elif data.compress != stream.NONE:
            raise NotImplementedError('Compression type {0} not implemented!'.format(
                data.compress))

        # Turn bytes into an array
        return reshape_array(data, np.frombuffer(bin_data, dtype=dt))
    elif data.dataType == stream.STRUCTURE:
        sd = read_proto_object(fobj, stream.StructureData)

        # Make a datatype appropriate to the rows of struct
        endian = '>' if data.bigend else '<'
        dt = np.dtype([(endian, np.void, sd.rowLength)])

        # Turn bytes into an array
        return reshape_array(data, np.frombuffer(sd.data, dtype=dt))
    elif data.dataType == stream.SEQUENCE:
        log.debug('Reading sequence')
        blocks = []
        magic = read_magic(fobj)
        while magic != MAGIC_VEND:
            if magic == MAGIC_VDATA:
                log.error('Bad magic for struct/seq data!')
            blocks.append(read_proto_object(fobj, stream.StructureData))
            magic = read_magic(fobj)
        return data, blocks
    else:
        raise NotImplementedError("Don't know how to handle data type: {0}".format(
            data.dataType))


Instruction:
How would you implement a function in Python 3 that
handles reading an NcStream error from a file - like object and raise as error.

Response:
def read_ncstream_err(fobj):
    """Handle reading an NcStream error from a file-like object and raise as error."""
    err = read_proto_object(fobj, stream.Error)
    raise RuntimeError(err.message)


Instruction:
Explain what the following Python 3 code does
def read_messages(fobj, magic_table):
    messages = []

    while True:
        magic = read_magic(fobj)
        if not magic:
            break

        func = magic_table.get(magic)
        if func is not None:
            messages.append(func(fobj))
        else:
            log.error('Unknown magic: ' + str(' '.join('{0:02x}'.format(b)
                                                       for b in bytearray(magic))))

    return messages

Response: Read messages from a file - like object until stream is exhausted.


Instruction:
Can you create a Python 3 function that
reads a block of data and parse using the given protobuf object.

Response:
def read_proto_object(fobj, klass):
    """Read a block of data and parse using the given protobuf object."""
    log.debug('%s chunk', klass.__name__)
    obj = klass()
    obj.ParseFromString(read_block(fobj))
    log.debug('Header: %s', str(obj))
    return obj


Instruction:
Write a Python 3 function that can
read a block from a file - like object.

Response:
def read_block(fobj):
    """Read a block.

    Reads a block from a file object by first reading the number of bytes to read, which must
    be encoded as a variable-byte length integer.

    Parameters
    ----------
    fobj : file-like object
        The file to read from.

    Returns
    -------
    bytes
        block of bytes read

    """
    num = read_var_int(fobj)
    log.debug('Next block: %d bytes', num)
    return fobj.read(num)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def process_vlen(data_header, array):
    source = iter(array)
    return np.array([np.fromiter(itertools.islice(source, size), dtype=array.dtype)
                     for size in data_header.vlens])

Response: This function processes vlen coming back from NCStream v2.


Instruction:
Can you write a function in Python 3 where it
converts a DataCol from NCStream v2 into an array with appropriate type.

Response:
def datacol_to_array(datacol):
    """Convert DataCol from NCStream v2 into an array with appropriate type.

    Depending on the data type specified, this extracts data from the appropriate members
    and packs into a :class:`numpy.ndarray`, recursing as necessary for compound data types.

    Parameters
    ----------
    datacol : DataCol

    Returns
    -------
    ndarray
        array containing extracted data

    """
    if datacol.dataType == stream.STRING:
        arr = np.array(datacol.stringdata, dtype=np.object)
    elif datacol.dataType == stream.OPAQUE:
        arr = np.array(datacol.opaquedata, dtype=np.object)
    elif datacol.dataType == stream.STRUCTURE:
        members = OrderedDict((mem.name, datacol_to_array(mem))
                              for mem in datacol.structdata.memberData)
        log.debug('Struct members:\n%s', str(members))

        # str() around name necessary because protobuf gives unicode names, but dtype doesn't
        # support them on Python 2
        dt = np.dtype([(str(name), arr.dtype) for name, arr in members.items()])
        log.debug('Struct dtype: %s', str(dt))

        arr = np.empty((datacol.nelems,), dtype=dt)
        for name, arr_data in members.items():
            arr[name] = arr_data
    else:
        # Make an appropriate datatype
        endian = '>' if datacol.bigend else '<'
        dt = data_type_to_numpy(datacol.dataType).newbyteorder(endian)

        # Turn bytes into an array
        arr = np.frombuffer(datacol.primdata, dtype=dt)
        if arr.size != datacol.nelems:
            log.warning('Array size %d does not agree with nelems %d',
                        arr.size, datacol.nelems)
        if datacol.isVlen:
            arr = process_vlen(datacol, arr)
            if arr.dtype == np.object_:
                arr = reshape_array(datacol, arr)
            else:
                # In this case, the array collapsed, need different resize that
                # correctly sizes from elements
                shape = tuple(r.size for r in datacol.section.range) + (datacol.vlens[0],)
                arr = arr.reshape(*shape)
        else:
            arr = reshape_array(datacol, arr)
    return arr


Instruction:
How would you implement a function in Python 3 that
extracts the appropriate array shape from the header.

Response:
def reshape_array(data_header, array):
    """Extract the appropriate array shape from the header.

    Can handle taking a data header and either bytes containing data or a StructureData
    instance, which will have binary data as well as some additional information.

    Parameters
    ----------
    array : :class:`numpy.ndarray`
    data_header : Data

    """
    shape = tuple(r.size for r in data_header.section.range)
    if shape:
        return array.reshape(*shape)
    else:
        return array


Instruction:
Make a summary of the following Python 3 code
def data_type_to_numpy(datatype, unsigned=False):
    basic_type = _dtypeLookup[datatype]

    if datatype in (stream.STRING, stream.OPAQUE):
        return np.dtype(basic_type)

    if unsigned:
        basic_type = basic_type.replace('i', 'u')
    return np.dtype('=' + basic_type)

Response: Convert an ncstream datatype to a numpy one.


Instruction:
Can you tell what is the following Python 3 function doing
def struct_to_dtype(struct):
    # str() around name necessary because protobuf gives unicode names, but dtype doesn't
    # support them on Python 2
    fields = [(str(var.name), data_type_to_numpy(var.dataType, var.unsigned))
              for var in struct.vars]
    for s in struct.structs:
        fields.append((str(s.name), struct_to_dtype(s)))

    log.debug('Structure fields: %s', fields)
    dt = np.dtype(fields)
    return dt

Response: Convert a Structure specification to a numpy structured dtype.


Instruction:
Here you have a function in Python 3, explain what it does
def unpack_variable(var):
    # If we actually get a structure instance, handle turning that into a variable
    if var.dataType == stream.STRUCTURE:
        return None, struct_to_dtype(var), 'Structure'
    elif var.dataType == stream.SEQUENCE:
        log.warning('Sequence support not implemented!')

    dt = data_type_to_numpy(var.dataType, var.unsigned)
    if var.dataType == stream.OPAQUE:
        type_name = 'opaque'
    elif var.dataType == stream.STRING:
        type_name = 'string'
    else:
        type_name = dt.name

    if var.data:
        log.debug('Storing variable data: %s %s', dt, var.data)
        if var.dataType == stream.STRING:
            data = var.data
        else:
            # Always sent big endian
            data = np.frombuffer(var.data, dtype=dt.newbyteorder('>'))
    else:
        data = None

    return data, dt, type_name

Response: Unpack an NCStream Variable into a numpy array and a type name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def unpack_attribute(att):
    if att.unsigned:
        log.warning('Unsupported unsigned attribute!')

    # TDS 5.0 now has a dataType attribute that takes precedence
    if att.len == 0:  # Empty
        val = None
    elif att.dataType == stream.STRING:  # Then look for new datatype string
        val = att.sdata
    elif att.dataType:  # Then a non-zero new data type
        val = np.frombuffer(att.data,
                            dtype='>' + _dtypeLookup[att.dataType], count=att.len)
    elif att.type:  # Then non-zero old-data type0
        val = np.frombuffer(att.data,
                            dtype=_attrConverters[att.type], count=att.len)
    elif att.sdata:  # This leaves both 0, try old string
        val = att.sdata
    else:  # Assume new datatype is Char (0)
        val = np.array(att.data, dtype=_dtypeLookup[att.dataType])

    if att.len == 1:
        val = val[0]

    return att.name, val

Response: Unpack an embedded attribute into a python or numpy object.


Instruction:
Can you tell what is the following Python 3 function doing
def read_var_int(file_obj):
    # Read all bytes from here, stopping with the first one that does not have
    # the MSB set. Save the lower 7 bits, and keep stacking to the *left*.
    val = 0
    shift = 0
    while True:
        # Read next byte
        next_val = ord(file_obj.read(1))
        val |= ((next_val & 0x7F) << shift)
        shift += 7
        if not next_val & 0x80:
            break

    return val

Response: Reads a variable - length integer from the specified file - like object.


Instruction:
Here you have a function in Python 3, explain what it does
def fetch_data(self, **var):
        varstr = ','.join(name + self._convert_indices(ind)
                          for name, ind in var.items())
        query = self.query().add_query_parameter(req='data', var=varstr)
        return self._fetch(query)

Response: Retrieve data from CDMRemote for one or more variables.


Instruction:
Given the following Python 3 function, write the documentation
def query(self):
        q = super(CDMRemote, self).query()

        # Turn on compression if it's been set on the object
        if self.deflate:
            q.add_query_parameter(deflate=self.deflate)

        return q

Response: Generate a new query for the object containing the object s ID.


Instruction:
How would you explain what the following Python 3 function does
def check_token(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        response = func(*args, **kwargs)
        if response.status_code == 401:
            raise InvalidToken('Access token invalid or no longer valid')
        else:
            return response
    return wrapper

Response: Check if access token is valid.


Instruction:
Can you write a function in Python 3 where it
uploads a file to the remote server.

Response:
def upload(self, remote_path, file_content, ondup=None, **kwargs):
        """上传单个文件（<2G）.

        | 百度PCS服务目前支持最大2G的单个文件上传。
        | 如需支持超大文件（>2G）的断点续传，请参考下面的“分片文件上传”方法。

        :param remote_path: 网盘中文件的保存路径（包含文件名）。
                            必须以 /apps/ 开头。

                            .. warning::
                                * 路径长度限制为1000；
                                * 径中不能包含以下字符：``\\\\ ? | " > < : *``；
                                * 文件名或路径名开头结尾不能是 ``.``
                                  或空白字符，空白字符包括：
                                  ``\\r, \\n, \\t, 空格, \\0, \\x0B`` 。
        :param file_content: 上传文件的内容/文件对象 。
                             (e.g. ``open('foobar', 'rb')`` )
        :param ondup: （可选）

                      * 'overwrite'：表示覆盖同名文件；
                      * 'newcopy'：表示生成文件副本并进行重命名，命名规则为“
                        文件名_日期.后缀”。
        :return: Response 对象
        """

        params = {
            'path': remote_path,
            'ondup': ondup
        }
        files = {'file': ('file', file_content, '')}
        url = 'https://c.pcs.baidu.com/rest/2.0/pcs/file'
        return self._request('file', 'upload', url=url, extra_params=params,
                             files=files, **kwargs)


Instruction:
Here you have a function in Python 3, explain what it does
def upload_tmpfile(self, file_content, **kwargs):

        params = {
            'type': 'tmpfile'
        }
        files = {'file': ('file', file_content, '')}
        url = 'https://c.pcs.baidu.com/rest/2.0/pcs/file'
        return self._request('file', 'upload', url=url, extra_params=params,
                             files=files, **kwargs)

Response: Upload a file to a temporary file.


Instruction:
Here you have a function in Python 3, explain what it does
def upload_superfile(self, remote_path, block_list, ondup=None, **kwargs):

        params = {
            'path': remote_path,
            'ondup': ondup
        }
        data = {
            'param': json.dumps({'block_list': block_list}),
        }
        return self._request('file', 'createsuperfile', extra_params=params,
                             data=data, **kwargs)

Response: Upload a superfile to the remote path.


Instruction:
How would you explain what the following Python 3 function does
def mkdir(self, remote_path, **kwargs):

        data = {
            'path': remote_path
        }
        return self._request('file', 'mkdir', data=data, **kwargs)

Response: Create a new directory.


Instruction:
Implement a function in Python 3 to
get file meta data

Response:
def meta(self, remote_path, **kwargs):
        """获取单个文件或目录的元信息.

        :param remote_path: 网盘中文件/目录的路径，必须以 /apps/ 开头。

                            .. warning::
                                * 路径长度限制为1000；
                                * 径中不能包含以下字符：``\\\\ ? | " > < : *``；
                                * 文件名或路径名开头结尾不能是 ``.``
                                  或空白字符，空白字符包括：
                                  ``\\r, \\n, \\t, 空格, \\0, \\x0B`` 。
        :return: Response 对象
        """

        params = {
            'path': remote_path
        }
        return self._request('file', 'meta', extra_params=params, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def move(self, from_path, to_path, **kwargs):

        data = {
            'from': from_path,
            'to': to_path,
        }
        return self._request('file', 'move', data=data, **kwargs)

Response: move file from one location to another


Instruction:
Given the following Python 3 function, write the documentation
def copy(self, from_path, to_path, **kwargs):

        data = {
            'from': from_path,
            'to': to_path,
        }
        return self._request('file', 'copy', data=data, **kwargs)

Response: Copy a file from one location to another.


Instruction:
Explain what the following Python 3 code does
def delete(self, remote_path, **kwargs):

        data = {
            'path': remote_path
        }
        return self._request('file', 'delete', data=data, **kwargs)

Response: Delete a file from the server.


Instruction:
Explain what the following Python 3 code does
def multi_delete(self, path_list, **kwargs):

        data = {
            'param': json.dumps({
                'list': [{'path': path} for path in path_list]
            }),
        }
        return self._request('file', 'delete', data=data, **kwargs)

Response: Delete a list of files.


Instruction:
Make a summary of the following Python 3 code
def thumbnail(self, remote_path, height, width, quality=100, **kwargs):

        params = {
            'path': remote_path,
            'height': height,
            'width': width,
            'quality': quality,
        }
        return self._request('thumbnail', 'generate', extra_params=params,
                             **kwargs)

Response: Get a thumbnail of a file.


Instruction:
Can you generate the documentation for the following Python 3 function
def video_convert(self, remote_path, video_type, **kwargs):

        params = {
            'path': remote_path,
            'type': video_type,
        }
        return self._request('file', 'streaming', extra_params=params,
                             **kwargs)

Response: Convert a file to a video type.


Instruction:
Here you have a function in Python 3, explain what it does
def download_stream(self, remote_path, **kwargs):

        params = {
            'path': remote_path,
        }
        url = 'https://d.pcs.baidu.com/rest/2.0/pcs/file'
        return self._request('stream', 'download', url=url,
                             extra_params=params, **kwargs)

Response: Download a file from the Baidu server.


Instruction:
Can you generate the documentation for the following Python 3 function
def rapid_upload(self, remote_path, content_length, content_md5,
                     content_crc32, slice_md5, ondup=None, **kwargs):
        """秒传一个文件.

        .. warning::
           * 被秒传文件必须大于256KB（即 256*1024 B）。
           * 校验段为文件的前256KB，秒传接口需要提供校验段的MD5。
             (非强一致接口，上传后请等待1秒后再读取)

        :param remote_path: 上传文件的全路径名。

                            .. warning::
                                * 路径长度限制为1000；
                                * 径中不能包含以下字符：``\\\\ ? | " > < : *``；
                                * 文件名或路径名开头结尾不能是 ``.``
                                  或空白字符，空白字符包括：
                                  ``\\r, \\n, \\t, 空格, \\0, \\x0B`` 。
        :param content_length: 待秒传文件的长度。
        :param content_md5: 待秒传文件的MD5。
        :param content_crc32: 待秒传文件的CRC32。
        :param slice_md5: 待秒传文件校验段的MD5。
        :param ondup: （可选）

                      * 'overwrite'：表示覆盖同名文件；
                      * 'newcopy'：表示生成文件副本并进行重命名，命名规则为“
                        文件名_日期.后缀”。
        :return: Response 对象
        """
        data = {
            'path': remote_path,
            'content-length': content_length,
            'content-md5': content_md5,
            'content-crc32': content_crc32,
            'slice-md5': slice_md5,
            'ondup': ondup,
        }
        return self._request('file', 'rapidupload', data=data, **kwargs)

Response: Upload a file to a rapid server.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add_download_task(self, source_url, remote_path,
                          rate_limit=None, timeout=60 * 60,
                          expires=None, callback='', **kwargs):
        """添加离线下载任务，实现单个文件离线下载.

        :param source_url: 源文件的URL。
        :param remote_path: 下载后的文件保存路径。

                            .. warning::
                                * 路径长度限制为1000；
                                * 径中不能包含以下字符：``\\\\ ? | " > < : *``；
                                * 文件名或路径名开头结尾不能是 ``.``
                                  或空白字符，空白字符包括：
                                  ``\\r, \\n, \\t, 空格, \\0, \\x0B`` 。
        :param rate_limit: 下载限速，默认不限速。
        :type rate_limit: int or long
        :param timeout: 下载超时时间，默认3600秒。
        :param expires: 请求失效时间，如果有，则会校验。
        :type expires: int
        :param callback: 下载完毕后的回调，默认为空。
        :type callback: str
        :return: Response 对象
        """

        data = {
            'source_url': source_url,
            'save_path': remote_path,
            'expires': expires,
            'rate_limit': rate_limit,
            'timeout': timeout,
            'callback': callback,
        }
        return self._request('services/cloud_dl', 'add_task',
                             data=data, **kwargs)

Response: Add a download task to the cache.


Instruction:
Can you write a function in Python 3 where it
queries Cloud DL Task

Response:
def query_download_tasks(self, task_ids, operate_type=1,
                             expires=None, **kwargs):
        """根据任务ID号，查询离线下载任务信息及进度信息。

        :param task_ids: 要查询的任务ID列表
        :type task_ids: list or tuple
        :param operate_type:
                            * 0：查任务信息
                            * 1：查进度信息，默认为1
        :param expires: 请求失效时间，如果有，则会校验。
        :type expires: int
        :return: Response 对象
        """

        params = {
            'task_ids': ','.join(map(str, task_ids)),
            'op_type': operate_type,
            'expires': expires,
        }
        return self._request('services/cloud_dl', 'query_task',
                             extra_params=params, **kwargs)


Instruction:
Implement a function in Python 3 to
list all the tasks in the specified base URL.

Response:
def list_download_tasks(self, need_task_info=1, start=0, limit=10, asc=0,
                            create_time=None, status=None, source_url=None,
                            remote_path=None, expires=None, **kwargs):
        """查询离线下载任务ID列表及任务信息.

        :param need_task_info: 是否需要返回任务信息:

                               * 0：不需要
                               * 1：需要，默认为1
        :param start: 查询任务起始位置，默认为0。
        :param limit: 设定返回任务数量，默认为10。
        :param asc:

                   * 0：降序，默认值
                   * 1：升序
        :param create_time: 任务创建时间，默认为空。
        :type create_time: int
        :param status: 任务状态，默认为空。
                       0:下载成功，1:下载进行中 2:系统错误，3:资源不存在，
                       4:下载超时，5:资源存在但下载失败, 6:存储空间不足,
                       7:目标地址数据已存在, 8:任务取消.
        :type status: int
        :param source_url: 源地址URL，默认为空。
        :param remote_path: 文件保存路径，默认为空。

                            .. warning::
                                * 路径长度限制为1000；
                                * 径中不能包含以下字符：``\\\\ ? | " > < : *``；
                                * 文件名或路径名开头结尾不能是 ``.``
                                  或空白字符，空白字符包括：
                                  ``\\r, \\n, \\t, 空格, \\0, \\x0B`` 。
        :param expires: 请求失效时间，如果有，则会校验。
        :type expires: int
        :return: Response 对象
        """

        data = {
            'expires': expires,
            'start': start,
            'limit': limit,
            'asc': asc,
            'source_url': source_url,
            'save_path': remote_path,
            'create_time': create_time,
            'status': status,
            'need_task_info': need_task_info,
        }
        return self._request('services/cloud_dl', 'list_task',
                             data=data, **kwargs)


Instruction:
Explain what the following Python 3 code does
def cancel_download_task(self, task_id, expires=None, **kwargs):

        data = {
            'expires': expires,
            'task_id': task_id,
        }
        return self._request('services/cloud_dl', 'cancle_task',
                             data=data, **kwargs)

Response:
取消离线下载任务.

        :param task_id: 要取消的任务ID号。
        :type task_id: str
        :param expires: 请求失效时间，如果有，则会校验。
        :type expires: int
        :return: Response 对象


Instruction:
Can you tell what is the following Python 3 function doing
def list_recycle_bin(self, start=0, limit=1000, **kwargs):

        params = {
            'start': start,
            'limit': limit,
        }
        return self._request('file', 'listrecycle',
                             extra_params=params, **kwargs)

Response: List all the files in the current user s archive.


Instruction:
Make a summary of the following Python 3 code
def restore_recycle_bin(self, fs_id, **kwargs):

        data = {
            'fs_id': fs_id,
        }
        return self._request('file', 'restore', data=data, **kwargs)

Response:
还原单个文件或目录（非强一致接口，调用后请sleep 1秒读取）.

        :param fs_id: 所还原的文件或目录在PCS的临时唯一标识ID。
        :type fs_id: str
        :return: Response 对象


Instruction:
Create a Python 3 function to
restore all the files in a set of files

Response:
def multi_restore_recycle_bin(self, fs_ids, **kwargs):
        """批量还原文件或目录（非强一致接口，调用后请sleep1秒 ）.

        :param fs_ids: 所还原的文件或目录在 PCS 的临时唯一标识 ID 的列表。
        :type fs_ids: list or tuple
        :return: Response 对象
        """

        data = {
            'param': json.dumps({
                'list': [{'fs_id': fs_id} for fs_id in fs_ids]
            }),
        }
        return self._request('file', 'restore', data=data, **kwargs)


Instruction:
Can you implement a function in Python 3 that
gets a new Access Token.

Response:
def get_new_access_token(refresh_token, client_id, client_secret,
                         scope=None, **kwargs):
    """使用 Refresh Token 刷新以获得新的 Access Token.

    :param refresh_token: 用于刷新 Access Token 用的 Refresh Token；
    :param client_id: 应用的 API Key；
    :param client_secret: 应用的 Secret Key;
    :param scope: 以空格分隔的权限列表，若不传递此参数，代表请求的数据访问
                  操作权限与上次获取 Access Token 时一致。通过 Refresh Token
                  刷新 Access Token 时所要求的 scope 权限范围必须小于等于上次
                  获取 Access Token 时授予的权限范围。 关于权限的具体信息请参考
                  “ `权限列表`__ ”。
    :return: Response 对象

    关于 ``response.json()`` 字典的内容所代表的含义，
    请参考 `相关的百度帮助文档`__ 。

     __ http://developer.baidu.com/wiki/index.php?title=docs/oauth/baiduoauth/list
     __ http://developer.baidu.com/wiki/index.php?title=docs/oauth/refresh
    """
    data = {
        'grant_type': 'refresh_token',
        'refresh_token': refresh_token,
        'client_id': client_id,
        'client_secret': client_secret,
    }
    if scope:
        data['scope'] = scope
    url = 'https://openapi.baidu.com/oauth/2.0/token'
    return requests.post(url, data=data)


Instruction:
Can you implement a function in Python 3 that
logs-in to verisure app api api Login before calling any read or write commands getTaxID

Response:
def login(self):
        """ Login to verisure app api

        Login before calling any read or write commands

        """
        if os.path.exists(self._cookieFileName):
            with open(self._cookieFileName, 'r') as cookieFile:
                self._vid = cookieFile.read().strip()

            try:
                self._get_installations()
            except ResponseError:
                self._vid = None
                os.remove(self._cookieFileName)

        if self._vid is None:
            self._create_cookie()
            with open(self._cookieFileName, 'w') as cookieFile:
                cookieFile.write(self._vid)
            self._get_installations()

        self._giid = self.installations[0]['giid']


Instruction:
Create a Python 3 function for
getting information about installations

Response:
def _get_installations(self):
        """ Get information about installations """
        response = None
        for base_url in urls.BASE_URLS:
            urls.BASE_URL = base_url
            try:
                response = requests.get(
                    urls.get_installations(self._username),
                    headers={
                        'Cookie': 'vid={}'.format(self._vid),
                        'Accept': 'application/json,'
                                  'text/javascript, */*; q=0.01',
                    })
                if 2 == response.status_code // 100:
                    break
                elif 503 == response.status_code:
                    continue
                else:
                    raise ResponseError(response.status_code, response.text)
            except requests.exceptions.RequestException as ex:
                raise RequestError(ex)

        _validate_response(response)
        self.installations = json.loads(response.text)


Instruction:
How would you code a function in Python 3 to
get overview for installation

Response:
def get_overview(self):
        """ Get overview for installation """
        response = None
        try:
            response = requests.get(
                urls.overview(self._giid),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Accept-Encoding': 'gzip, deflate',
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)


Instruction:
Can you create a Python 3 function that
turns on or off smartplug on the specified smartplug device label.

Response:
def set_smartplug_state(self, device_label, state):
        """ Turn on or off smartplug

        Args:
            device_label (str): Smartplug device label
            state (boolean): new status, 'True' or 'False'
        """
        response = None
        try:
            response = requests.post(
                urls.smartplug(self._giid),
                headers={
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)},
                data=json.dumps([{
                    "deviceLabel": device_label,
                    "state": state}]))
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)


Instruction:
Can you implement a function in Python 3 that
gets recent events from the object.

Response:
def get_history(self, filters=(), pagesize=15, offset=0):
        """ Get recent events

        Args:
            filters (string set): 'ARM', 'DISARM', 'FIRE', 'INTRUSION',
                                  'TECHNICAL', 'SOS', 'WARNING', 'LOCK',
                                  'UNLOCK'
            pagesize (int): Number of events to display
            offset (int): Skip pagesize * offset first events
        """
        response = None
        try:
            response = requests.get(
                urls.history(self._giid),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)},
                params={
                    "offset": int(offset),
                    "pagesize": int(pagesize),
                    "notificationCategories": filters})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)


Instruction:
Make a summary of the following Python 3 code
def get_climate(self, device_label):
        response = None
        try:
            response = requests.get(
                urls.climate(self._giid),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)},
                params={
                    "deviceLabel": device_label})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Response: Get climate history from the device.


Instruction:
Explain what the following Python 3 code does
def set_lock_state(self, code, device_label, state):
        response = None
        try:
            response = requests.put(
                urls.set_lockstate(self._giid, device_label, state),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)},
                data=json.dumps({"code": str(code)}))
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Response: Set lock state of a lock in a specific device.


Instruction:
Can you implement a function in Python 3 that
gets lock state transaction status

Response:
def get_lock_state_transaction(self, transaction_id):
        """ Get lock state transaction status

        Args:
            transaction_id: Transaction ID received from set_lock_state
        """
        response = None
        try:
            response = requests.get(
                urls.get_lockstate_transaction(self._giid, transaction_id),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)


Instruction:
Can you tell what is the following Python 3 function doing
def get_lock_config(self, device_label):
        response = None
        try:
            response = requests.get(
                urls.lockconfig(self._giid, device_label),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Response: Get lock configuration of a specific lock in a specific device.


Instruction:
Explain what the following Python 3 code does
def set_lock_config(self, device_label, volume=None, voice_level=None,
                        auto_lock_enabled=None):
        """ Set lock configuration

        Args:
            device_label (str): device label of lock
            volume (str): 'SILENCE', 'LOW' or 'HIGH'
            voice_level (str): 'ESSENTIAL' or 'NORMAL'
            auto_lock_enabled (boolean): auto lock enabled
        """
        response = None
        data = {}
        if volume:
            data['volume'] = volume
        if voice_level:
            data['voiceLevel'] = voice_level
        if auto_lock_enabled is not None:
            data['autoLockEnabled'] = auto_lock_enabled
        try:
            response = requests.put(
                urls.lockconfig(self._giid, device_label),
                headers={
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)},
                data=json.dumps(data))
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)

Response: Set lock configuration for a specific lock in the nexus.


Instruction:
Explain what the following Python 3 code does
def capture_image(self, device_label):
        response = None
        try:
            response = requests.post(
                urls.imagecapture(self._giid, device_label),
                headers={
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)

Response: Capture smartcam image of a specific device label.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_camera_imageseries(self, number_of_imageseries=10, offset=0):
        response = None
        try:
            response = requests.get(
                urls.get_imageseries(self._giid),
                headers={
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Cookie': 'vid={}'.format(self._vid)},
                params={
                    "numberOfImageSeries": int(number_of_imageseries),
                    "offset": int(offset),
                    "fromDate": "",
                    "toDate": "",
                    "onlyNotViewed": "",
                    "_": self._giid})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)

Response: Get smartcam image series


Instruction:
Write a Python 3 function that can
download image taken by a smartcam

Response:
def download_image(self, device_label, image_id, file_name):
        """ Download image taken by a smartcam

        Args:
            device_label (str): device label of camera
            image_id (str): image id from image series
            file_name (str): path to file
        """
        response = None
        try:
            response = requests.get(
                urls.download_image(self._giid, device_label, image_id),
                headers={
                    'Cookie': 'vid={}'.format(self._vid)},
                stream=True)
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        with open(file_name, 'wb') as image_file:
            for chunk in response.iter_content(chunk_size=1024):
                if chunk:
                    image_file.write(chunk)


Instruction:
How would you explain what the following Python 3 function does
def logout(self):
        response = None
        try:
            response = requests.delete(
                urls.login(),
                headers={
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)

Response: Logout and remove vid


Instruction:
Implement a function in Python 3 to
set the heat pump mode for a specific user.

Response:
def set_heat_pump_mode(self, device_label, mode):
        """ Set heatpump mode
        Args:
            mode (str): 'HEAT', 'COOL', 'FAN' or 'AUTO'
        """
        response = None
        try:
            response = requests.put(
                urls.set_heatpump_state(self._giid, device_label),
                headers={
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)},
                data=json.dumps({'mode': mode}))
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)


Instruction:
Write a Python 3 function that can
set the heatpump mode for a specific feature.

Response:
def set_heat_pump_feature(self, device_label, feature):
        """ Set heatpump mode
        Args:
            feature: 'QUIET', 'ECONAVI', or 'POWERFUL'
        """
        response = None
        try:
            response = requests.put(
                urls.set_heatpump_feature(self._giid, device_label, feature),
                headers={
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'Cookie': 'vid={}'.format(self._vid)})
        except requests.exceptions.RequestException as ex:
            raise RequestError(ex)
        _validate_response(response)
        return json.loads(response.text)


Instruction:
Make a summary of the following Python 3 code
def print_result(overview, *names):
    if names:
        for name in names:
            toprint = overview
            for part in name.split('/'):
                toprint = toprint[part]
            print(json.dumps(toprint, indent=4, separators=(',', ': ')))
    else:
        print(json.dumps(overview, indent=4, separators=(',', ': ')))

Response: Print the result of a verisure request.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def main():
    parser = argparse.ArgumentParser(
        description='Read or change status of verisure devices')
    parser.add_argument(
        'username',
        help='MyPages username')
    parser.add_argument(
        'password',
        help='MyPages password')
    parser.add_argument(
        '-i', '--installation',
        help='Installation number',
        type=int,
        default=1)
    parser.add_argument(
        '-c', '--cookie',
        help='File to store cookie in',
        default='~/.verisure-cookie')

    commandsparser = parser.add_subparsers(
        help='commands',
        dest='command')

    # installations command
    commandsparser.add_parser(
        COMMAND_INSTALLATIONS,
        help='Get information about installations')

    # overview command
    overview_parser = commandsparser.add_parser(
        COMMAND_OVERVIEW,
        help='Read status of one or many device types')
    overview_parser.add_argument(
        'filter',
        nargs='*',
        help='Read status for device type')

    # armstate command
    commandsparser.add_parser(
        COMMAND_ARMSTATE,
        help='Get arm state')

    # Set command
    set_parser = commandsparser.add_parser(
        COMMAND_SET,
        help='Set status of a device')
    set_device = set_parser.add_subparsers(
        help='device',
        dest='device')

    # Set smartplug
    set_smartplug = set_device.add_parser(
        'smartplug',
        help='set smartplug value')
    set_smartplug.add_argument(
        'device_label',
        help='device label')
    set_smartplug.add_argument(
        'new_value',
        choices=[
            'on',
            'off'],
        help='new value')

    # Set alarm
    set_alarm = set_device.add_parser(
        'alarm',
        help='set alarm status')
    set_alarm.add_argument(
        'code',
        help='alarm code')
    set_alarm.add_argument(
        'new_status',
        choices=[
            'ARMED_HOME',
            'ARMED_AWAY',
            'DISARMED'],
        help='new status')

    # Set lock
    set_lock = set_device.add_parser(
        'lock',
        help='set lock status')
    set_lock.add_argument(
        'code',
        help='alarm code')
    set_lock.add_argument(
        'serial_number',
        help='serial number')
    set_lock.add_argument(
        'new_status',
        choices=[
            'lock',
            'unlock'],
        help='new status')

    # Get climate history
    history_climate = commandsparser.add_parser(
        COMMAND_CLIMATE,
        help='get climate history')
    history_climate.add_argument(
        'device_label',
        help='device label')

    # Event log command
    eventlog_parser = commandsparser.add_parser(
        COMMAND_EVENTLOG,
        help='Get event log')
    eventlog_parser.add_argument(
        '-p', '--pagesize',
        type=int,
        default=15,
        help='Number of elements on one page')
    eventlog_parser.add_argument(
        '-o', '--offset',
        type=int,
        default=0,
        help='Page offset')
    eventlog_parser.add_argument(
        '-f', '--filter',
        nargs='*',
        default=[],
        choices=[
            'ARM',
            'DISARM',
            'FIRE',
            'INTRUSION',
            'TECHNICAL',
            'SOS',
            'WARNING',
            'LOCK',
            'UNLOCK'],
        help='Filter event log')

    # Capture command
    capture_parser = commandsparser.add_parser(
        COMMAND_CAPTURE,
        help='Capture image')
    capture_parser.add_argument(
        'device_label',
        help='Device label')

    # Image series command
    commandsparser.add_parser(
        COMMAND_IMAGESERIES,
        help='Get image series')

    # Get image command
    getimage_parser = commandsparser.add_parser(
        COMMAND_GETIMAGE,
        help='Download image')
    getimage_parser.add_argument(
        'device_label',
        help='Device label')
    getimage_parser.add_argument(
        'image_id',
        help='image ID')
    getimage_parser.add_argument(
        'file_name',
        help='Output file name')

    # Vacation mode command
    commandsparser.add_parser(
        COMMAND_VACATIONMODE,
        help='Get vacation mode info')

    # Door window status command
    commandsparser.add_parser(
        COMMAND_DOOR_WINDOW,
        help='Get door/window status')

    # Test ethernet command
    commandsparser.add_parser(
        COMMAND_TEST_ETHERNET,
        help='Update ethernet status')

    args = parser.parse_args()
    session = verisure.Session(args.username, args.password, args.cookie)
    session.login()
    try:
        session.set_giid(session.installations[args.installation - 1]['giid'])
        if args.command == COMMAND_INSTALLATIONS:
            print_result(session.installations)
        if args.command == COMMAND_OVERVIEW:
            print_result(session.get_overview(), *args.filter)
        if args.command == COMMAND_ARMSTATE:
            print_result(session.get_arm_state())
        if args.command == COMMAND_SET:
            if args.device == 'smartplug':
                session.set_smartplug_state(
                    args.device_label,
                    args.new_value == 'on')
            if args.device == 'alarm':
                print_result(session.set_arm_state(
                    args.code,
                    args.new_status))
            if args.device == 'lock':
                print_result(session.set_lock_state(
                    args.code,
                    args.serial_number,
                    args.new_status))
        if args.command == COMMAND_CLIMATE:
            print_result(session.get_climate(args.device_label))
        if args.command == COMMAND_EVENTLOG:
            print_result(
                session.get_history(
                    args.filter,
                    pagesize=args.pagesize,
                    offset=args.offset))
        if args.command == COMMAND_CAPTURE:
            session.capture_image(args.device_label)
        if args.command == COMMAND_IMAGESERIES:
            print_result(session.get_camera_imageseries())
        if args.command == COMMAND_GETIMAGE:
            session.download_image(
                args.device_label,
                args.image_id,
                args.file_name)
        if args.command == COMMAND_VACATIONMODE:
            print_result(session.get_vacation_mode())
        if args.command == COMMAND_DOOR_WINDOW:
            print_result(session.get_door_window())
        if args.command == COMMAND_TEST_ETHERNET:
            session.test_ethernet()
    except verisure.session.ResponseError as ex:
        print(ex.text)

Response: Start verisure command line


Instruction:
Create a Python 3 function for
getting the ContentType id of the model.

Response:
def type_id(self):
        """
        Shortcut to retrieving the ContentType id of the model.
        """
        try:
            return ContentType.objects.get_for_model(self.model, for_concrete_model=False).id
        except DatabaseError as e:
            raise DatabaseError("Unable to fetch ContentType object, is a plugin being registered before the initial syncdb? (original error: {0})".format(str(e)))


Instruction:
Can you implement a function in Python 3 that
returns the cache key which is used to store a rendered item.

Response:
def get_output_cache_key(self, placeholder_name, instance):
        """
        .. versionadded:: 0.9
           Return the default cache key which is used to store a rendered item.
           By default, this function generates the cache key using :func:`get_output_cache_base_key`.
        """
        cachekey = self.get_output_cache_base_key(placeholder_name, instance)
        if self.cache_output_per_site:
            cachekey = "{0}-s{1}".format(cachekey, settings.SITE_ID)

        # Append language code
        if self.cache_output_per_language:
            # NOTE: Not using self.language_code, but using the current language instead.
            #       That is what the {% trans %} tags are rendered as after all.
            #       The render_placeholder() code can switch the language if needed.
            user_language = get_language()
            if user_language not in self.cache_supported_language_codes:
                user_language = 'unsupported'
            cachekey = "{0}.{1}".format(cachekey, user_language)

        return cachekey


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_output_cache_keys(self, placeholder_name, instance):
        base_key = self.get_output_cache_base_key(placeholder_name, instance)
        cachekeys = [
            base_key,
        ]

        if self.cache_output_per_site:
            site_ids = list(Site.objects.values_list('pk', flat=True))
            if settings.SITE_ID not in site_ids:
                site_ids.append(settings.SITE_ID)

            base_key = get_rendering_cache_key(placeholder_name, instance)
            cachekeys = ["{0}-s{1}".format(base_key, site_id) for site_id in site_ids]

        if self.cache_output_per_language or self.render_ignore_item_language:
            # Append language code to all keys,
            # have to invalidate a lot more items in memcache.
            # Also added "None" suffix, since get_parent_language_code() may return that.
            # TODO: ideally for render_ignore_item_language, only invalidate all when the fallback language changed.
            total_list = []
            cache_languages = list(self.cache_supported_language_codes) + ['unsupported', 'None']

            # All variants of the Placeholder (for full page caching)
            placeholder = instance.placeholder
            total_list.extend(get_placeholder_cache_key(placeholder, lc) for lc in cache_languages)

            # All variants of the ContentItem in different languages
            for user_language in cache_languages:
                total_list.extend("{0}.{1}".format(base, user_language) for base in cachekeys)
            cachekeys = total_list

        return cachekeys

Response: Return the possible cache keys for a rendered item.


Instruction:
Write a Python 3 script to
get the cached output for a rendered item.

Response:
def get_cached_output(self, placeholder_name, instance):
        """
        .. versionadded:: 0.9
           Return the cached output for a rendered item, or ``None`` if no output is cached.

           This method can be overwritten to implement custom caching mechanisms.
           By default, this function generates the cache key using :func:`get_output_cache_key`
           and retrieves the results from the configured Django cache backend (e.g. memcached).
        """
        cachekey = self.get_output_cache_key(placeholder_name, instance)
        return cache.get(cachekey)


Instruction:
Can you implement a function in Python 3 that
sets the cached output for a rendered item.

Response:
def set_cached_output(self, placeholder_name, instance, output):
        """
        .. versionadded:: 0.9
           Store the cached output for a rendered item.

           This method can be overwritten to implement custom caching mechanisms.
           By default, this function generates the cache key using :func:`~fluent_contents.cache.get_rendering_cache_key`
           and stores the results in the configured Django cache backend (e.g. memcached).

           When custom cache keys are used, also include those in :func:`get_output_cache_keys`
           so the cache will be cleared when needed.

        .. versionchanged:: 1.0
           The received data is no longer a HTML string, but :class:`~fluent_contents.models.ContentItemOutput` object.
        """
        cachekey = self.get_output_cache_key(placeholder_name, instance)
        if self.cache_timeout is not DEFAULT_TIMEOUT:
            cache.set(cachekey, output, self.cache_timeout)
        else:
            # Don't want to mix into the default 0/None issue.
            cache.set(cachekey, output)


Instruction:
Here you have a function in Python 3, explain what it does
def render(self, request, instance, **kwargs):
        render_template = self.get_render_template(request, instance, **kwargs)
        if not render_template:
            return str(_(u"{No rendering defined for class '%s'}" % self.__class__.__name__))

        context = self.get_context(request, instance, **kwargs)
        return self.render_to_string(request, render_template, context)

Response: Returns a string that can be used to render a single item.


Instruction:
Make a summary of the following Python 3 code
def render_to_string(self, request, template, context, content_instance=None):
        if not content_instance:
            content_instance = PluginContext(request)

        content_instance.update(context)
        return render_to_string(template, content_instance.flatten(), request=request)

Response: Render a custom template with the context as context instance.


Instruction:
Create a Python 3 function to
register a Media class to the frontend.

Response:
def register_frontend_media(request, media):
    """
    Add a :class:`~django.forms.Media` class to the current request.
    This will be rendered by the ``render_plugin_media`` template tag.
    """
    if not hasattr(request, '_fluent_contents_frontend_media'):
        request._fluent_contents_frontend_media = Media()

    add_media(request._fluent_contents_frontend_media, media)


Instruction:
How would you explain what the following Python 3 function does
def get_rendering_cache_key(placeholder_name, contentitem):
    if not contentitem.pk:
        return None
    return "contentitem.@{0}.{1}.{2}".format(
        placeholder_name,
        contentitem.plugin.type_name,  # always returns the upcasted name.
        contentitem.pk,                # already unique per language_code
    )

Response: Returns a cache key for the content item output.


Instruction:
Create a Python 3 function to
return a cache key for an existing placeholder object.

Response:
def get_placeholder_cache_key(placeholder, language_code):
    """
    Return a cache key for an existing placeholder object.

    This key is used to cache the entire output of a placeholder.
    """
    return _get_placeholder_cache_key_for_id(
        placeholder.parent_type_id,
        placeholder.parent_id,
        placeholder.slot,
        language_code
    )


Instruction:
Make a summary of the following Python 3 code
def get_placeholder_cache_key_for_parent(parent_object, placeholder_name, language_code):
    parent_type = ContentType.objects.get_for_model(parent_object)
    return _get_placeholder_cache_key_for_id(
        parent_type.id,
        parent_object.pk,
        placeholder_name,
        language_code
    )

Response: Return a cache key for a placeholder.


Instruction:
Explain what the following Python 3 code does
def remove_stale_items(self, stale_cts):
        stale_ct_ids = list(stale_cts.keys())
        items = (ContentItem.objects
                 .non_polymorphic()  # very important, or polymorphic skips them on fetching derived data
                 .filter(polymorphic_ctype__in=stale_ct_ids)
                 .order_by('polymorphic_ctype', 'pk')
                 )
        if not items:
            self.stdout.write("No stale items found.")
            return

        if self.dry_run:
            self.stdout.write("The following content items are stale:")
        else:
            self.stdout.write("The following content items were stale:")

        for item in items:
            ct = stale_cts[item.polymorphic_ctype_id]
            self.stdout.write("- #{id} points to removed {app_label}.{model}".format(
                id=item.pk, app_label=ct.app_label, model=ct.model
            ))

            if not self.dry_run:
                try:
                    item.delete()
                except PluginNotFound:
                    Model.delete(item)

Response: Remove stale items from the database.


Instruction:
Implement a Python 3 function for
removing items that no longer point to an existing parent.

Response:
def remove_unreferenced_items(self, stale_cts):
        """
        See if there are items that no longer point to an existing parent.
        """
        stale_ct_ids = list(stale_cts.keys())
        parent_types = (ContentItem.objects.order_by()
                        .exclude(polymorphic_ctype__in=stale_ct_ids)
                        .values_list('parent_type', flat=True).distinct())

        num_unreferenced = 0

        for ct_id in parent_types:
            parent_ct = ContentType.objects.get_for_id(ct_id)
            unreferenced_items = (ContentItem.objects
                                  .filter(parent_type=ct_id)
                                  .order_by('polymorphic_ctype', 'pk'))

            if parent_ct.model_class() is not None:
                # Only select the items that are part of removed pages,
                # unless the parent type was removed - then removing all is correct.
                unreferenced_items = unreferenced_items.exclude(
                    parent_id__in=parent_ct.get_all_objects_for_this_type()
                )

            if unreferenced_items:
                for item in unreferenced_items:
                    self.stdout.write(
                        "- {cls}#{id} points to nonexisting {app_label}.{model}".format(
                            cls=item.__class__.__name__, id=item.pk,
                            app_label=parent_ct.app_label, model=parent_ct.model
                        ))
                    num_unreferenced += 1
                    if not self.dry_run and self.remove_unreferenced:
                        item.delete()

        if not num_unreferenced:
            self.stdout.write("No unreferenced items found.")
        else:
            self.stdout.write("{0} unreferenced items found.".format(num_unreferenced))
            if not self.remove_unreferenced:
                self.stdout.write("Re-run this command with --remove-unreferenced to remove these items")


Instruction:
Create a Python 3 function for
returning the list of elements that are not already in the queryset

Response:
def __initial_minus_queryset(self):
        """
        Gives all elements from self._initial having a slot value that is not already
        in self.get_queryset()
        """
        queryset = self.get_queryset()

        def initial_not_in_queryset(initial):
            for x in queryset:
                if x.slot == initial['slot']:
                    return False

            return True

        return list(filter(initial_not_in_queryset, self._initial))


Instruction:
Create a Python 3 function for
validating and return the Placeholder object that points to the given placeholder object.

Response:
def _get_placeholder_arg(arg_name, placeholder):
    """
    Validate and return the Placeholder object that the template variable points to.
    """
    if placeholder is None:
        raise RuntimeWarning(u"placeholder object is None")
    elif isinstance(placeholder, Placeholder):
        return placeholder
    elif isinstance(placeholder, Manager):
        manager = placeholder
        try:
            parent_object = manager.instance  # read RelatedManager code
        except AttributeError:
            parent_object = None

        try:
            placeholder = manager.all()[0]
            if parent_object is not None:
                placeholder.parent = parent_object  # Fill GFK cache
            return placeholder
        except IndexError:
            raise RuntimeWarning(u"No placeholders found for query '{0}.all.0'".format(arg_name))
    else:
        raise ValueError(u"The field '{0}' does not refer to a placeholder object!".format(arg_name))


Instruction:
Make a summary of the following Python 3 code
def _split_js(media, domain):
    # Read internal property without creating new Media instance.
    if not media._js:
        return ImmutableMedia.empty_instance

    needs_local = domain == 'local'
    new_js = []
    for url in media._js:
        if needs_local == _is_local(url):
            new_js.append(url)

    if not new_js:
        return ImmutableMedia.empty_instance
    else:
        return Media(js=new_js)

Response: Split the local or external URLs from a Media object.


Instruction:
Can you tell what is the following Python 3 function doing
def _split_css(media, domain):
    # Read internal property without creating new Media instance.
    if not media._css:
        return ImmutableMedia.empty_instance

    needs_local = domain == 'local'
    new_css = {}
    for medium, url in six.iteritems(media._css):
        if needs_local == _is_local(url):
            new_css.setdefault(medium, []).append(url)

    if not new_css:
        return ImmutableMedia.empty_instance
    else:
        return Media(css=new_css)

Response: Extract the local or external URLs from a Media object.


Instruction:
Can you write a function in Python 3 where it
parses the node syntax for the national cache object.

Response:
def parse(cls, parser, token):
        """
        Parse the node syntax:

        .. code-block:: html+django

            {% page_placeholder parentobj slotname title="test" role="m" %}
        """
        bits, as_var = parse_as_var(parser, token)
        tag_name, args, kwargs = parse_token_kwargs(parser, bits, allowed_kwargs=cls.allowed_kwargs, compile_args=True, compile_kwargs=True)

        # Play with the arguments
        if len(args) == 2:
            parent_expr = args[0]
            slot_expr = args[1]
        elif len(args) == 1:
            # Allow 'page' by default. Works with most CMS'es, including django-fluent-pages.
            parent_expr = Variable('page')
            slot_expr = args[0]
        else:
            raise TemplateSyntaxError("""{0} tag allows two arguments: 'parent object' 'slot name' and optionally: title=".." role="..".""".format(tag_name))

        cls.validate_args(tag_name, *args, **kwargs)
        return cls(
            tag_name=tag_name,
            as_var=as_var,
            parent_expr=parent_expr,
            slot_expr=slot_expr,
            **kwargs
        )


Instruction:
Write a Python 3 function for
returning the string literal that is used in the template screens.

Response:
def get_title(self):
        """
        Return the string literal that is used in the template.
        The title is used in the admin screens.
        """
        try:
            return extract_literal(self.meta_kwargs['title'])
        except KeyError:
            slot = self.get_slot()
            if slot is not None:
                return slot.replace('_', ' ').title()

            return None


Instruction:
How would you explain what the following Python 3 function does
def extract_literal(templatevar):
    # FilterExpression contains another 'var' that either contains a Variable or SafeData object.
    if hasattr(templatevar, 'var'):
        templatevar = templatevar.var
        if isinstance(templatevar, SafeData):
            # Literal in FilterExpression, can return.
            return templatevar
        else:
            # Variable in FilterExpression, not going to work here.
            return None

    if templatevar[0] in ('"', "'") and templatevar[-1] in ('"', "'"):
        return templatevar[1:-1]
    else:
        return None

Response: Returns the value of a Literal in a FilterExpression.


Instruction:
Write a Python 3 script for
returning True if a template FilterExpression holds a literal boolean value.

Response:
def extract_literal_bool(templatevar):
    """
    See if a template FilterExpression holds a literal boolean value.

    :type templatevar: django.template.FilterExpression
    :rtype: bool|None
    """
    # FilterExpression contains another 'var' that either contains a Variable or SafeData object.
    if hasattr(templatevar, 'var'):
        templatevar = templatevar.var
        if isinstance(templatevar, SafeData):
            # Literal in FilterExpression, can return.
            return is_true(templatevar)
        else:
            # Variable in FilterExpression, not going to work here.
            return None

    return is_true(templatevar)


Instruction:
How would you explain what the following Python 3 function does
def _create_markup_plugin(language, model):
    form = type("{0}MarkupItemForm".format(language.capitalize()), (MarkupItemForm,), {
        'default_language': language,
    })

    classname = "{0}MarkupPlugin".format(language.capitalize())
    PluginClass = type(classname, (MarkupPluginBase,), {
        'model': model,
        'form': form,
    })

    return PluginClass

Response: Create a new MarkupPlugin class that represents the plugin type.


Instruction:
Can you implement a function in Python 3 that
returns a dictionary of keyword arguments for the generic parent_type / parent_id fields.

Response:
def get_parent_lookup_kwargs(parent_object):
    """
    Return lookup arguments for the generic ``parent_type`` / ``parent_id`` fields.

    :param parent_object: The parent object.
    :type parent_object: :class:`~django.db.models.Model`
    """
    if parent_object is None:
        return dict(
            parent_type__isnull=True,
            parent_id=0
        )
    elif isinstance(parent_object, models.Model):
        return dict(
            parent_type=ContentType.objects.get_for_model(parent_object),
            parent_id=parent_object.pk
        )
    else:
        raise ValueError("parent_object is not a model!")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_parent_active_language_choices(parent_object, exclude_current=False):
    assert parent_object is not None, "Missing parent_object!"

    from .db import ContentItem
    qs = ContentItem.objects \
        .parent(parent_object, limit_parent_language=False) \
        .values_list('language_code', flat=True).distinct()

    languages = set(qs)

    if exclude_current:
        parent_lang = get_parent_language_code(parent_object)
        languages.discard(parent_lang)

    if parler_appsettings.PARLER_LANGUAGES and not parler_appsettings.PARLER_SHOW_EXCLUDED_LANGUAGE_TABS:
        site_id = get_parent_site_id(parent_object)
        try:
            lang_dict = parler_appsettings.PARLER_LANGUAGES[site_id]
        except KeyError:
            lang_dict = ()

        allowed_languages = set(item['code'] for item in lang_dict)
        languages &= allowed_languages

    # No multithreading issue here, object is instantiated for this user only.
    choices = [(lang, str(get_language_title(lang))) for lang in languages if lang]
    choices.sort(key=lambda tup: tup[1])
    return choices

Response: Get the currently active languages of a parent object.


Instruction:
Implement a function in Python 3 to
return a placeholder by slot.

Response:
def get_by_slot(self, parent_object, slot):
        """
        Return a placeholder by key.
        """
        placeholder = self.parent(parent_object).get(slot=slot)
        placeholder.parent = parent_object  # fill the reverse cache
        return placeholder


Instruction:
How would you implement a function in Python 3 that
creates a new object with the given parameters and return it.

Response:
def create_for_object(self, parent_object, slot, role='m', title=None):
        """
        Create a placeholder with the given parameters
        """
        from .db import Placeholder
        parent_attrs = get_parent_lookup_kwargs(parent_object)
        obj = self.create(
            slot=slot,
            role=role or Placeholder.MAIN,
            title=title or slot.title().replace('_', ' '),
            **parent_attrs
        )
        obj.parent = parent_object  # fill the reverse cache
        return obj


Instruction:
Make a summary of the following Python 3 code
def translated(self, *language_codes):
        # this API has the same semantics as django-parler's .translated() for familiarity.
        # However, since this package doesn't filter in a related field, the ORM limitations don't apply.
        if not language_codes:
            language_codes = (get_language(),)
        else:
            # Since some code operates on a True/str switch, make sure that doesn't drip into this low level code.
            for language_code in language_codes:
                if not isinstance(language_code, six.string_types) or language_code.lower() in ('1', '0', 'true', 'false'):
                    raise ValueError("ContentItemQuerySet.translated() expected language_code to be an ISO code")

        if len(language_codes) == 1:
            return self.filter(language_code=language_codes[0])
        else:
            return self.filter(language_code__in=language_codes)

Response: Return a new QuerySet with translated objects.


Instruction:
Write a Python 3 script for
returning all content items which are associated with a given parent object.

Response:
def parent(self, parent_object, limit_parent_language=True):
        """
        Return all content items which are associated with a given parent object.
        """
        lookup = get_parent_lookup_kwargs(parent_object)

        # Filter the items by default, giving the expected "objects for this parent" items
        # when the parent already holds the language state.
        if limit_parent_language:
            language_code = get_parent_language_code(parent_object)
            if language_code:
                lookup['language_code'] = language_code

        return self.filter(**lookup)


Instruction:
Here you have a function in Python 3, explain what it does
def move_to_placeholder(self, placeholder, sort_order=None):
        qs = self.all()  # Get clone
        for item in qs:
            # Change the item directly in the resultset.
            item.move_to_placeholder(placeholder, sort_order=sort_order)
            if sort_order is not None:
                sort_order += 1

        return qs

Response: Move the entire queryset to a new object.


Instruction:
How would you implement a function in Python 3 that
copies the entire queryset to a new object.

Response:
def copy_to_placeholder(self, placeholder, sort_order=None):
        """
        .. versionadded: 1.0 Copy the entire queryset to a new object.

        Returns a queryset with the newly created objects.
        """
        qs = self.all()  # Get clone
        for item in qs:
            # Change the item directly in the resultset.
            item.copy_to_placeholder(placeholder, sort_order=sort_order, in_place=True)
            if sort_order is not None:
                sort_order += 1

        return qs


Instruction:
How would you explain what the following Python 3 function does
def parent(self, parent_object, limit_parent_language=True):
        return self.all().parent(parent_object, limit_parent_language)

Response: Return all content items which are associated with a given parent object.


Instruction:
Explain what the following Python 3 code does
def create_for_placeholder(self, placeholder, sort_order=1, language_code=None, **kwargs):
        if language_code is None:
            # Could also use get_language() or appsettings.FLUENT_CONTENTS_DEFAULT_LANGUAGE_CODE
            # thus avoid the risk of performing an extra query here to the parent.
            # However, this identical behavior to BaseContentItemFormSet,
            # and the parent can be set already via Placeholder.objects.create_for_object()
            language_code = get_parent_language_code(placeholder.parent)

        obj = self.create(
            placeholder=placeholder,
            parent_type_id=placeholder.parent_type_id,
            parent_id=placeholder.parent_id,
            sort_order=sort_order,
            language_code=language_code,
            **kwargs
        )

        # Fill the reverse caches
        obj.placeholder = placeholder
        parent = getattr(placeholder, '_parent_cache', None)  # by GenericForeignKey (_meta.virtual_fields[0].cache_attr)
        if parent is not None:
            obj.parent = parent

        return obj

Response: Create a new Content Item for a given Placeholder.


Instruction:
Write a Python 3 script to
return the data of the placeholder fields.

Response:
def get_placeholder_data(self, request, obj=None):
        """
        Return the data of the placeholder fields.
        """
        # Return all placeholder fields in the model.
        if not hasattr(self.model, '_meta_placeholder_fields'):
            return []

        data = []
        for name, field in self.model._meta_placeholder_fields.items():
            assert isinstance(field, PlaceholderField)
            data.append(PlaceholderData(
                slot=field.slot,
                title=field.verbose_name.capitalize(),
                fallback_language=None,  # Information cant' be known by "render_placeholder" in the template.
            ))

        return data


Instruction:
Can you tell what is the following Python 3 function doing
def get_all_allowed_plugins(self):
        # Get all allowed plugins of the various placeholders together.
        if not hasattr(self.model, '_meta_placeholder_fields'):
            # No placeholder fields in the model, no need for inlines.
            return []

        plugins = []
        for name, field in self.model._meta_placeholder_fields.items():
            assert isinstance(field, PlaceholderField)
            if field.plugins is None:
                # no limitations, so all is allowed
                return extensions.plugin_pool.get_plugins()
            else:
                plugins += field.plugins

        return list(set(plugins))

Response: Return all allowed plugins for the current model.


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_markup_model(fixed_language):
    title = backend.LANGUAGE_NAMES.get(fixed_language) or fixed_language

    objects = MarkupLanguageManager(fixed_language)

    def save(self, *args, **kwargs):
        self.language = fixed_language
        MarkupItem.save(self, *args, **kwargs)

    class Meta:
        verbose_name = title
        verbose_name_plural = _('%s items') % title
        proxy = True

    classname = "{0}MarkupItem".format(fixed_language.capitalize())

    new_class = type(str(classname), (MarkupItem,), {
        '__module__': MarkupItem.__module__,
        'objects': objects,
        'save': save,
        'Meta': Meta,
    })

    # Make easily browsable
    return new_class

Response: Create a new MarkupItem model that saves itself in a single language.


Instruction:
Create a Python 3 function to
return a PlaceholderFormField instance for this database Field.

Response:
def formfield(self, **kwargs):
        """
        Returns a :class:`PlaceholderFormField` instance for this database Field.
        """
        defaults = {
            'label': capfirst(self.verbose_name),
            'help_text': self.help_text,
            'required': not self.blank,
        }
        defaults.update(kwargs)
        return PlaceholderFormField(slot=self.slot, plugins=self._plugins, **defaults)


Instruction:
Create a Python 3 function to
get the set of plugins that this field may display.

Response:
def plugins(self):
        """
        Get the set of plugins that this field may display.
        """
        from fluent_contents import extensions
        if self._plugins is None:
            return extensions.plugin_pool.get_plugins()
        else:
            try:
                return extensions.plugin_pool.get_plugins_by_name(*self._plugins)
            except extensions.PluginNotFound as e:
                raise extensions.PluginNotFound(str(e) + " Update the plugin list of '{0}.{1}' field or FLUENT_CONTENTS_PLACEHOLDER_CONFIG['{2}'] setting.".format(self.model._meta.object_name, self.name, self.slot))


Instruction:
Can you tell what is the following Python 3 function doing
def value_from_object(self, obj):
        try:
            # not using self.attname, access the descriptor instead.
            placeholder = getattr(obj, self.name)
        except Placeholder.DoesNotExist:
            return None   # Still allow ModelForm / admin to open and create a new Placeholder if the table was truncated.

        return placeholder.id if placeholder else None

Response: Internal method used to return the value of the object instance.


Instruction:
Explain what the following Python 3 code does
def can_use_cached_output(self, contentitem):
        return contentitem.plugin.search_output and not contentitem.plugin.search_fields \
           and super(SearchRenderingPipe, self).can_use_cached_output(contentitem)

Response: Return True if the contentitem can be cached output.


Instruction:
Given the following Python 3 function, write the documentation
def render_item(self, contentitem):
        plugin = contentitem.plugin
        if not plugin.search_output and not plugin.search_fields:
            # Only render items when the item was output will be indexed.
            raise SkipItem

        if not plugin.search_output:
            output = ContentItemOutput('', cacheable=False)
        else:
            output = super(SearchRenderingPipe, self).render_item(contentitem)

        if plugin.search_fields:
            # Just add the results into the output, but avoid caching that somewhere.
            output.html += plugin.get_search_text(contentitem)
            output.cacheable = False

        return output

Response: Render the item - but render as search text instead.


Instruction:
Given the following Python 3 function, write the documentation
def get_content_item_inlines(plugins=None, base=BaseContentItemInline):
    COPY_FIELDS = (
        'form', 'raw_id_fields', 'filter_vertical', 'filter_horizontal',
        'radio_fields', 'prepopulated_fields', 'formfield_overrides', 'readonly_fields',
    )
    if plugins is None:
        plugins = extensions.plugin_pool.get_plugins()

    inlines = []
    for plugin in plugins:  # self.model._supported_...()
        # Avoid errors that are hard to trace
        if not isinstance(plugin, extensions.ContentPlugin):
            raise TypeError("get_content_item_inlines() expects to receive ContentPlugin instances, not {0}".format(plugin))

        ContentItemType = plugin.model

        # Create a new Type that inherits CmsPageItemInline
        # Read the static fields of the ItemType to override default appearance.
        # This code is based on FeinCMS, (c) Simon Meers, BSD licensed
        class_name = '%s_AutoInline' %  ContentItemType.__name__
        attrs = {
            '__module__': plugin.__class__.__module__,
            'model': ContentItemType,

            # Add metadata properties for template
            'name': plugin.verbose_name,
            'plugin': plugin,
            'type_name': plugin.type_name,
            'extra_fieldsets': plugin.fieldsets,
            'cp_admin_form_template': plugin.admin_form_template,
            'cp_admin_init_template': plugin.admin_init_template,
        }

        # Copy a restricted set of admin fields to the inline model too.
        for name in COPY_FIELDS:
            if getattr(plugin, name):
                attrs[name] = getattr(plugin, name)

        inlines.append(type(class_name, (base,), attrs))

    # For consistency, enforce ordering
    inlines.sort(key=lambda inline: inline.name.lower())

    return inlines

Response: Dynamically generate genuine django inlines for all registered content item types.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_oembed_providers():
    global _provider_list, _provider_lock
    if _provider_list is not None:
        return _provider_list

    # Allow only one thread to build the list, or make request to embed.ly.
    _provider_lock.acquire()
    try:
        # And check whether that already succeeded when the lock is granted.
        if _provider_list is None:
            _provider_list = _build_provider_list()
    finally:
        # Always release if there are errors
        _provider_lock.release()

    return _provider_list

Response: Get the list of OEmbed providers.


Instruction:
Implement a Python 3 function for
building the provider list from the app settings.

Response:
def _build_provider_list():
    """
    Construct the provider registry, using the app settings.
    """
    registry = None
    if appsettings.FLUENT_OEMBED_SOURCE == 'basic':
        registry = bootstrap_basic()
    elif appsettings.FLUENT_OEMBED_SOURCE == 'embedly':
        params = {}
        if appsettings.MICAWBER_EMBEDLY_KEY:
            params['key'] = appsettings.MICAWBER_EMBEDLY_KEY
        registry = bootstrap_embedly(**params)
    elif appsettings.FLUENT_OEMBED_SOURCE == 'noembed':
        registry = bootstrap_noembed(nowrap=1)
    elif appsettings.FLUENT_OEMBED_SOURCE == 'list':
        # Fill list manually in the settings, e.g. to have a fixed set of supported secure providers.
        registry = ProviderRegistry()
        for regex, provider in appsettings.FLUENT_OEMBED_PROVIDER_LIST:
            registry.register(regex, Provider(provider))
    else:
        raise ImproperlyConfigured("Invalid value of FLUENT_OEMBED_SOURCE, only 'basic', 'list', 'noembed' or 'embedly' is supported.")

    # Add any extra providers defined in the settings
    for regex, provider in appsettings.FLUENT_OEMBED_EXTRA_PROVIDERS:
        registry.register(regex, Provider(provider))

    return registry


Instruction:
Write a Python 3 function that can
fetch the OEmbed object from the registry and return the response as dictionary.

Response:
def get_oembed_data(url, max_width=None, max_height=None, **params):
    """
    Fetch the OEmbed object, return the response as dictionary.
    """
    if max_width:  params['maxwidth'] = max_width
    if max_height: params['maxheight'] = max_height

    registry = get_oembed_providers()
    return registry.request(url, **params)


Instruction:
Can you create a Python 3 function that
returns all plugins grouped by category.

Response:
def group_plugins_into_categories(plugins):
    """
    Return all plugins, grouped by category.
    The structure is a {"Categorynane": [list of plugin classes]}
    """
    if not plugins:
        return {}
    plugins = sorted(plugins, key=lambda p: p.verbose_name)
    categories = {}

    for plugin in plugins:
        title = str(plugin.category or u"")  # enforce resolving ugettext_lazy proxies.
        if title not in categories:
            categories[title] = []
        categories[title].append(plugin)

    return categories


Instruction:
Explain what the following Python 3 code does
def plugin_categories_to_choices(categories):
    choices = []
    for category, items in categories.items():
        if items:
            plugin_tuples = tuple((plugin.type_name, plugin.verbose_name) for plugin in items)
            if category:
                choices.append((category, plugin_tuples))
            else:
                choices += plugin_tuples

    choices.sort(key=lambda item: item[0])
    return choices

Response: Return a tuple of plugin model choices suitable for a select field.


Instruction:
Write a Python 3 function that can
add a new Media object to a new Media object.

Response:
def add_media(dest, media):
    """
    Optimized version of django.forms.Media.__add__() that doesn't create new objects.
    """
    if django.VERSION >= (2, 2):
        dest._css_lists += media._css_lists
        dest._js_lists += media._js_lists
    elif django.VERSION >= (2, 0):
        combined = dest + media
        dest._css = combined._css
        dest._js = combined._js
    else:
        dest.add_css(media._css)
        dest.add_js(media._js)


Instruction:
How would you explain what the following Python 3 function does
def get_dummy_request(language=None):

    if settings.ALLOWED_HOSTS and settings.ALLOWED_HOSTS != "*":
        host = settings.ALLOWED_HOSTS[0]
    else:
        host = Site.objects.get_current().domain

    request = RequestFactory().get("/", HTTP_HOST=host)
    request.session = {}
    request.LANGUAGE_CODE = language or settings.LANGUAGE_CODE
    # Needed for plugin rendering.
    request.current_page = None

    if 'django.contrib.auth' in settings.INSTALLED_APPS:
        from django.contrib.auth.models import AnonymousUser
        request.user = AnonymousUser()

    return request

Response: Returns a dummy Request instance populated with cms specific attributes.


Instruction:
Explain what the following Python 3 code does
def get_render_language(contentitem):
    plugin = contentitem.plugin

    if plugin.render_ignore_item_language \
    or (plugin.cache_output and plugin.cache_output_per_language):
        # Render the template in the current language.
        # The cache also stores the output under the current language code.
        #
        # It would make sense to apply this for fallback content too,
        # but that would be ambiguous however because the parent_object could also be a fallback,
        # and that case can't be detected here. Hence, better be explicit when desiring multi-lingual content.
        return get_language()  # Avoid switching the content,
    else:
        # Render the template in the ContentItem language.
        # This makes sure that {% trans %} tag output matches the language of the model field data.
        return contentitem.language_code

Response: Returns the language code that should be used to render the content item.


Instruction:
Given the following Python 3 function, write the documentation
def optimize_logger_level(logger, log_level):
    function_name = _log_functions[log_level]
    if getattr(logger, function_name) is _dummy_log:
        return False

    is_level_logged = logger.isEnabledFor(log_level)
    if not is_level_logged:
        setattr(logger, function_name, _dummy_log)

    return is_level_logged

Response: This function is used to optimize the logging level of a logger.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_search_field_values(contentitem):
    plugin = contentitem.plugin
    values = []
    for field_name in plugin.search_fields:
        value = getattr(contentitem, field_name)

        # Just assume all strings may contain HTML.
        # Not checking for just the PluginHtmlField here.
        if value and isinstance(value, six.string_types):
            value = get_cleaned_string(value)

        values.append(value)

    return values

Response: Extract the search fields from the model.


Instruction:
Make a summary of the following Python 3 code
def get_layout_view(self, request):
        template_name = request.GET['name']

        # Check if template is allowed, avoid parsing random templates
        templates = dict(appconfig.SIMPLECMS_TEMPLATE_CHOICES)
        if template_name not in templates:
            jsondata = {'success': False, 'error': 'Template not found'}
            status = 404
        else:
            # Extract placeholders from the template, and pass to the client.
            template = get_template(template_name)
            placeholders = get_template_placeholder_data(template)

            jsondata = {
                'placeholders': [p.as_dict() for p in placeholders],
            }
            status = 200

        jsonstr = json.dumps(jsondata)
        return HttpResponse(jsonstr, content_type='application/json', status=status)

Response: Return the metadata about a layout


Instruction:
How would you code a function in Python 3 to
apply soft hyphenation to the text.

Response:
def softhyphen_filter(textitem, html):
    """
    Apply soft hyphenation to the text, which inserts ```` markers.
    """
    language = textitem.language_code

    # Make sure the Django language code gets converted to what django-softhypen 1.0.2 needs.
    if language == 'en':
        language = 'en-us'
    elif '-' not in language:
        language = "{0}-{0}".format(language)

    return hyphenate(html, language=language)


Instruction:
Can you tell what is the following Python 3 function doing
def get_cached_placeholder_output(parent_object, placeholder_name):
    if not PlaceholderRenderingPipe.may_cache_placeholders():
        return None

    language_code = get_parent_language_code(parent_object)
    cache_key = get_placeholder_cache_key_for_parent(parent_object, placeholder_name, language_code)
    return cache.get(cache_key)

Response: Get the cached output for a placeholder.


Instruction:
Can you implement a function in Python 3 that
renders a placeholder and returns the rendered HTML output.

Response:
def render_placeholder(request, placeholder, parent_object=None, template_name=None, cachable=None, limit_parent_language=True, fallback_language=None):
    """
    Render a :class:`~fluent_contents.models.Placeholder` object.
    Returns a :class:`~fluent_contents.models.ContentItemOutput` object
    which contains the HTML output and :class:`~django.forms.Media` object.

    This function also caches the complete output of the placeholder
    when all individual items are cacheable.

    :param request: The current request object.
    :type request: :class:`~django.http.HttpRequest`
    :param placeholder: The placeholder object.
    :type placeholder: :class:`~fluent_contents.models.Placeholder`
    :param parent_object: Optional, the parent object of the placeholder (already implied by the placeholder)
    :param template_name: Optional template name used to concatenate the placeholder output.
    :type template_name: str | None
    :param cachable: Whether the output is cachable, otherwise the full output will not be cached.
                     Default: False when using a template, True otherwise.
    :type cachable: bool | None
    :param limit_parent_language: Whether the items should be limited to the parent language.
    :type limit_parent_language: bool
    :param fallback_language: The fallback language to use if there are no items in the current language. Passing ``True`` uses the default :ref:`FLUENT_CONTENTS_DEFAULT_LANGUAGE_CODE`.
    :type fallback_language: bool/str
    :rtype: :class:`~fluent_contents.models.ContentItemOutput`
    """
    output = PlaceholderRenderingPipe(request).render_placeholder(
        placeholder=placeholder,
        parent_object=parent_object,
        template_name=template_name,
        cachable=cachable,
        limit_parent_language=limit_parent_language,
        fallback_language=fallback_language
    )

    # Wrap the result after it's stored in the cache.
    if markers.is_edit_mode(request):
        output.html = markers.wrap_placeholder_output(output.html, placeholder)

    return output


Instruction:
How would you implement a function in Python 3 that
renders a list of items as HTML string.

Response:
def render_content_items(request, items, template_name=None, cachable=None):
    """
    Render a list of :class:`~fluent_contents.models.ContentItem` objects as HTML string.
    This is a variation of the :func:`render_placeholder` function.

    Note that the items are not filtered in any way by parent or language.
    The items are rendered as-is.

    :param request: The current request object.
    :type request: :class:`~django.http.HttpRequest`
    :param items: The list or queryset of objects to render. Passing a queryset is preferred.
    :type items: list or queryset of :class:`~fluent_contents.models.ContentItem`.
    :param template_name: Optional template name used to concatenate the placeholder output.
    :type template_name: Optional[str]
    :param cachable: Whether the output is cachable, otherwise the full output will not be cached.
                     Default: False when using a template, True otherwise.
    :type cachable: Optional[bool]

    :rtype: :class:`~fluent_contents.models.ContentItemOutput`
    """
    if not items:
        output = ContentItemOutput(mark_safe(u"<!-- no items to render -->"))
    else:
        output = RenderingPipe(request).render_items(
            placeholder=None,
            items=items,
            parent_object=None,
            template_name=template_name,
            cachable=cachable
        )

    # Wrap the result after it's stored in the cache.
    if markers.is_edit_mode(request):
        output.html = markers.wrap_anonymous_output(output.html)

    return output


Instruction:
Implement a function in Python 3 to
render a placeholder object to search text.

Response:
def render_placeholder_search_text(placeholder, fallback_language=None):
    """
    Render a :class:`~fluent_contents.models.Placeholder` object to search text.
    This text can be used by an indexer (e.g. haystack) to produce content search for a parent object.

    :param placeholder: The placeholder object.
    :type placeholder: :class:`~fluent_contents.models.Placeholder`
    :param fallback_language: The fallback language to use if there are no items in the current language.
                              Passing ``True`` uses the default :ref:`FLUENT_CONTENTS_DEFAULT_LANGUAGE_CODE`.
    :type fallback_language: bool|str
    :rtype: str
    """
    parent_object = placeholder.parent   # this is a cached lookup thanks to PlaceholderFieldDescriptor
    language = get_parent_language_code(parent_object)
    output = SearchRenderingPipe(language).render_placeholder(
        placeholder=placeholder,
        parent_object=parent_object,
        fallback_language=fallback_language
    )
    return output.html


Instruction:
Make a summary of the following Python 3 code
def render(self, name, value, attrs=None, renderer=None):
        other_instance_languages = None
        if value and value != "-DUMMY-":
            if get_parent_language_code(self.parent_object):
                # Parent is a multilingual object, provide information
                # for the copy dialog.
                other_instance_languages = get_parent_active_language_choices(
                    self.parent_object, exclude_current=True)

        context = {
            'cp_plugin_list': list(self.plugins),
            'placeholder_id': '',
            'placeholder_slot': self.slot,
            'other_instance_languages': other_instance_languages,
        }
        return mark_safe(render_to_string('admin/fluent_contents/placeholderfield/widget.html', context))

Response: Render the placeholder field.