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

		x_changed = self._libinput.libinput_event_tablet_tool_x_has_changed(
			self._handle)
		y_changed = self._libinput.libinput_event_tablet_tool_y_has_changed(
			self._handle)
		x = self._libinput.libinput_event_tablet_tool_get_x(self._handle)
		y = self._libinput.libinput_event_tablet_tool_get_y(self._handle)
		return (x, y), x_changed or y_changed

Response:
Returns the x y coordinates of the tablet tool in mm from the top left corner of the tablet in mm from
	the top left corner of the tablet in its current logical orientation and whether they have changed in this event.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delta(self):

		delta_x = self._libinput.libinput_event_tablet_tool_get_dx(self._handle)
		delta_y = self._libinput.libinput_event_tablet_tool_get_dy(self._handle)
		return delta_x, delta_y

Response: The relative delta between the last event and the current event.


Instruction:
Explain what the following Python 3 code does
def pressure(self):

		pressure = self._libinput.libinput_event_tablet_tool_get_pressure(
			self._handle)
		changed = self._libinput. \
			libinput_event_tablet_tool_pressure_has_changed(self._handle)
		return pressure, changed

Response: Returns the current pressure of the current tool in use normalized to the range [ 0 1 ) and whether it has changed in this event.


Instruction:
Given the following Python 3 function, write the documentation
def distance(self):

		distance = self._libinput.libinput_event_tablet_tool_get_distance(
			self._handle)
		changed = self._libinput. \
			libinput_event_tablet_tool_distance_has_changed(self._handle)
		return distance, changed

Response: Returns the current distance from the tablet s sensor to the range [ 0 1 ) and whether it has changed in this event.


Instruction:
Make a summary of the following Python 3 code
def tilt_axes(self):

		tilt_x = self._libinput.libinput_event_tablet_tool_get_tilt_x(
			self._handle)
		tilt_y = self._libinput.libinput_event_tablet_tool_get_tilt_y(
			self._handle)
		x_changed = self._libinput. \
			libinput_event_tablet_tool_tilt_x_has_changed(self._handle)
		y_changed = self._libinput. \
			libinput_event_tablet_tool_tilt_y_has_changed(self._handle)
		return (tilt_x, tilt_y), x_changed or y_changed

Response: Returns the current tilt along the X Y axes of the tablet s logical orientation in degrees off the tablet s Z axis.


Instruction:
Implement a Python 3 function for
returning the current Z rotation of the tool in degrees clockwise from the tool s logical neutral position and whether it has changed in this event.

Response:
def rotation(self):
		"""The current Z rotation of the tool in degrees, clockwise
		from the tool's logical neutral position and whether it has changed
		in this event.

		For tools of type :attr:`~libinput.constant.TabletToolType.MOUSE`
		and :attr:`~libinput.constant.TabletToolType.LENS` the logical
		neutral position is pointing to the current logical north
		of the tablet. For tools of type
		:attr:`~libinput.constant.TabletToolType.BRUSH`, the logical
		neutral position is with the buttons pointing up.

		If this axis does not exist on the current tool, this property is
		(0, :obj:`False`).

		Returns:
			(float, bool): The current value of the the axis and whether it has
			changed.
		"""

		rotation = self._libinput.libinput_event_tablet_tool_get_rotation(
			self._handle)
		changed = self._libinput. \
			libinput_event_tablet_tool_rotation_has_changed(self._handle)
		return rotation, changed


Instruction:
Make a summary of the following Python 3 code
def slider_position(self):

		position = self._libinput. \
			libinput_event_tablet_tool_get_slider_position(self._handle)
		changed = self._libinput.libinput_event_tablet_tool_slider_has_changed(
			self._handle)
		return position, changed

Response: Returns the current position of the slider on the tool and whether it has changed in this event.


Instruction:
Write a Python 3 function for
returning the delta for the wheel in degrees and whether it has changed in this event.

Response:
def wheel_delta(self):
		"""The delta for the wheel in degrees and whether it has changed in
		this event.

		Returns:
			(float, bool): The delta of the wheel, in degrees, compared to
			the last event and whether it has changed.
		"""

		delta = self._libinput.libinput_event_tablet_tool_get_wheel_delta(
			self._handle)
		changed = self._libinput.libinput_event_tablet_tool_wheel_has_changed(
			self._handle)
		return delta, changed


Instruction:
Write a Python 3 function for
returning the delta for the wheel in discrete steps compared to the last event and whether it has changed in this event.

Response:
def wheel_delta_discrete(self):
		"""The delta for the wheel in discrete steps (e.g. wheel clicks) and
		whether it has changed in this event.

		Returns:
			(int, bool): The delta of the wheel, in discrete steps, compared to
			the last event and whether it has changed.
		"""

		delta =  self._libinput. \
			libinput_event_tablet_tool_get_wheel_delta_discrete(self._handle)
		changed = self._libinput.libinput_event_tablet_tool_wheel_has_changed(
			self._handle)
		return delta, changed


Instruction:
Create a Python 3 function for
returning the current absolute x y coordinates of the tablet tool event transformed to screen coordinates and whether they have changed in this event.

Response:
def transform_coords(self, width, height):
		"""Return the current absolute (x, y) coordinates of
		the tablet tool event, transformed to screen coordinates and
		whether they have changed in this event.

		Note:
			On some devices, returned value may be negative or larger than
			the width of the device. See `Out-of-bounds motion events`_
			for more details.
		Args:
			width (int): The current output screen width.
			height (int): The current output screen height.
		Returns:
			((float, float), bool): The current absolute (x, y) coordinates
			transformed to screen coordinates and whether they have changed.
		"""

		x = self._libinput.libinput_event_tablet_tool_get_x_transformed(
			self._handle, width)
		y = self._libinput.libinput_event_tablet_tool_get_y_transformed(
			self._handle, height)
		x_changed = self._libinput.libinput_event_tablet_tool_x_has_changed(
			self._handle)
		y_changed = self._libinput.libinput_event_tablet_tool_y_has_changed(
			self._handle)
		return (x, y), x_changed or y_changed


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

		htablettool = self._libinput.libinput_event_tablet_tool_get_tool(
			self._handle)
		return TabletTool(htablettool, self._libinput)

Response: The tool that was in use during this event.


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

		if self.type != EventType.TABLET_TOOL_BUTTON:
			raise AttributeError(_wrong_prop.format(self.type))
		return self._libinput.libinput_event_tablet_tool_get_button_state(
			self._handle)

Response: The button state of the event. This property is only available for tables that are not of type EventType. TABLET_TOOL_BUTTON.


Instruction:
How would you explain what the following Python 3 function does
def ring_position(self):

		if self.type != EventType.TABLET_PAD_RING:
			raise AttributeError(_wrong_prop.format(self.type))
		return self._libinput.libinput_event_tablet_pad_get_ring_position(
			self._handle)

Response:
The current position of the ring in degrees
	counterclockwise from the northern - most point of the ring in the tablet s current logical orientation.


Instruction:
How would you explain what the following Python 3 function does
def ring_number(self):

		if self.type != EventType.TABLET_PAD_RING:
			raise AttributeError(_wrong_prop.format(self.type))
		return self._libinput.libinput_event_tablet_pad_get_ring_number(
			self._handle)

Response: The number of the ring that has changed state.


Instruction:
How would you explain what the following Python 3 function does
def ring_source(self):

		if self.type != EventType.TABLET_PAD_RING:
			raise AttributeError(_wrong_prop.format(self.type))
		return self._libinput.libinput_event_tablet_pad_get_ring_source(
			self._handle)

Response: The source of the interaction with the ring.


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

		if self.type != EventType.TABLET_PAD_STRIP:
			raise AttributeError(_wrong_prop.format(self.type))
		return self._libinput.libinput_event_tablet_pad_get_strip_position(
			self._handle)

Response: The current position of the strip in the current axis.


Instruction:
Here you have a function in Python 3, explain what it does
def strip_number(self):

		if self.type != EventType.TABLET_PAD_STRIP:
			raise AttributeError(_wrong_prop.format(self.type))
		return self._libinput.libinput_event_tablet_pad_get_strip_number(
			self._handle)

Response: The number of the first strip that has changed state.


Instruction:
Can you write a function in Python 3 where it
returns the mode group that the button ring or strip that triggered this event is considered in.

Response:
def mode_group(self):
		"""The mode group that the button, ring, or strip that
		triggered this event is considered in.

		The mode is a virtual grouping of functionality, usually based on some
		visual feedback like LEDs on the pad. See `Tablet pad modes`_
		for details.

		Returns:
			~libinput.define.TabletPadModeGroup: The mode group of the button,
			ring or strip that caused this event.
		"""

		hmodegroup = self._libinput.libinput_event_tablet_pad_get_mode_group(
			self._handle)
		return TabletPadModeGroup(hmodegroup, self._libinput)


Instruction:
Implement a function in Python 3 to
parse arguments returns options and args.

Response:
def parse_args(args=None):
    """Parses arguments, returns (options, args)."""
    from argparse import ArgumentParser

    if args is None:
        args = sys.argv

    parser = ArgumentParser(description='''
Attempt to automatically find "adjacent" shapes in a SVG layer, and on a second
SVG layer, draw each detected connection between the center points of the
corresponding shapes.'''.strip())
    parser.add_argument('svg_input_file', type=path, default=None)
    parser.add_argument('svg_output_file', type=path, default="-",
                        help='Output file path ("-" for stdout)', nargs='?')
    parser.add_argument('-f', '--overwrite', action='store_true')

    args = parser.parse_args()

    if not args.overwrite and (args.svg_input_file == args.svg_output_file):
        parser.error('Input and output file are the same.  Use `-f` to force '
                     'overwrite.')

    return args


Instruction:
Create a Python 3 function for
returning the Package long description.

Response:
def get_long_description():
    """
    Returns the Package long description.

    :return: Package long description.
    :rtype: unicode
    """

    description = []
    with open("README.rst") as file:
        for line in file:
            if ".. code:: python" in line and len(description) >= 2:
                blockLine = description[-2]
                if re.search(r":$", blockLine) and not re.search(r"::$", blockLine):
                    description[-2] = "::".join(blockLine.rsplit(":", 1))
                continue

            description.append(line)
    return "".join(description)


Instruction:
Create a Python 3 function to
generate the full file paths from a root path.

Response:
def ftp_listing_paths(ftpconn: FTP, root: str) -> Iterable[str]:
    """Generate the full file paths from a root path."""

    for current_path, dirs, files in ftp_walk(ftpconn, root):
        yield from (os.path.join(current_path, file) for file in files)


Instruction:
How would you code a function in Python 3 to
return the directories and files for single FTP listing.

Response:
def directory_listing(conn: FTP, path: str) -> Tuple[List, List]:
    """Return the directories and files for single FTP listing."""

    entries = deque()
    conn.dir(path, entries.append)
    entries = map(parse_line, entries)
    grouped_entries = defaultdict(list)

    for key, value in entries:
        grouped_entries[key].append(value)

    directories = grouped_entries[ListingType.directory]
    files = grouped_entries[ListingType.file]

    return directories, files


Instruction:
Write a Python 3 script to
parse a single line into a listing type and filename.

Response:
def parse_line(line: str, char_index=0) -> Tuple[ListingType, str]:
    """Parse FTP directory listing into (type, filename)."""

    entry_name = str.rpartition(line, ' ')[-1]
    entry_type = LISTING_FLAG_MAP.get(line[char_index], ListingType.other)
    return entry_type, entry_name


Instruction:
How would you implement a function in Python 3 that
downloads a file from a ftp source url to a target URI.

Response:
def download_ftp_url(source_url, target_uri, buffer_size=8192):
    """Uses urllib. thread safe?"""

    ensure_file_directory(target_uri)

    with urllib.request.urlopen(source_url) as source_file:
        with open(target_uri, 'wb') as target_file:
            shutil.copyfileobj(source_file, target_file, buffer_size)


Instruction:
How would you implement a function in Python 3 that
lists of devices to test

Response:
def devices(self):
        """List of devices to test
        """
        eax = self.attributes.get('devices')
        if eax is None:
            eax = self._all_devices
        if not isinstance(eax, list):
            eax = [eax]
        return [str(dev) for dev in eax]


Instruction:
Create a Python 3 function for
handling SQLAlchemy exceptions in a sane way. Args: func: An arbitrary function to wrap. error_is_fatal: Should we exit the program on exception? reraise: Should we reraise the exception, after logging? Only makes sense if error_is_fatal is False. error_messages: A dictionary that assigns an exception class to a customized error message.

Response:
def exceptions(error_is_fatal=True, error_messages=None):
    """
    Handle SQLAlchemy exceptions in a sane way.

    Args:
        func: An arbitrary function to wrap.
        error_is_fatal: Should we exit the program on exception?
        reraise: Should we reraise the exception, after logging? Only makes sense
            if error_is_fatal is False.
        error_messages: A dictionary that assigns an exception class to a
            customized error message.
    """

    def exception_decorator(func):
        nonlocal error_messages

        @functools.wraps(func)
        def exc_wrapper(*args, **kwargs):
            nonlocal error_messages
            try:
                result = func(*args, **kwargs)
            except sa.exc.SQLAlchemyError as err:
                result = None
                details = None
                err_type = err.__class__
                if error_messages and err_type in error_messages:
                    details = error_messages[err_type]
                if details:
                    LOG.error(details)
                LOG.error("For developers: (%s) %s", err.__class__, str(err))
                if error_is_fatal:
                    sys.exit("Abort, SQL operation failed.")
                if not ui.ask(
                        "I can continue at your own risk, do you want that?"):
                    raise err
            return result

        return exc_wrapper

    return exception_decorator


Instruction:
Implement a function in Python 3 to
install versioning on the db.

Response:
def enforce_versioning(force=False):
    """Install versioning on the db."""
    connect_str, repo_url = get_version_data()
    LOG.warning("Your database uses an unversioned benchbuild schema.")
    if not force and not ui.ask(
            "Should I enforce version control on your schema?"):
        LOG.error("User declined schema versioning.")
        return None
    repo_version = migrate.version(repo_url, url=connect_str)
    migrate.version_control(connect_str, repo_url, version=repo_version)
    return repo_version


Instruction:
Make a summary of the following Python 3 code
def init_functions(connection):
    if settings.CFG["db"]["create_functions"]:
        print("Refreshing SQL functions...")
        for file in path.template_files("../sql/", exts=[".sql"]):
            func = sa.DDL(path.template_str(file))
            LOG.info("Loading: '%s' into database", file)
            connection.execute(func)
            connection.commit()

Response: Initialize all SQL functions in the database.


Instruction:
Can you generate the documentation for the following Python 3 function
def connect_engine(self):
        try:
            self.connection = self.engine.connect()
            return True
        except sa.exc.OperationalError as opex:
            LOG.fatal("Could not connect to the database. The error was: '%s'",
                      str(opex))
        return False

Response: Establish a connection to the database.


Instruction:
Can you create a Python 3 function that
configures the databse connection.

Response:
def configure_engine(self):
        """
        Configure the databse connection.

        Sets appropriate transaction isolation levels and handle errors.

        Returns:
            True, if we did not encounter any unrecoverable errors, else False.
        """
        try:
            self.connection.execution_options(isolation_level="SERIALIZABLE")
        except sa.exc.ArgumentError:
            LOG.debug("Unable to set isolation level to SERIALIZABLE")
        return True


Instruction:
Explain what the following Python 3 code does
def parse_date(date_str: str, pattern=_RE_DATE) -> dt.date:

    groups = re.match(pattern, date_str)

    return dt.date(*_date_to_tuple(groups.groupdict()))

Response: Parse datetime. date from YYYY - MM - DD format.


Instruction:
Create a Python 3 function for
converting a dictionary to a tuple.

Response:
def _datetime_to_tuple(dt_dict):
    """datetime.datetime components from dictionary to tuple.

    Example
    -------
    dt_dict = {'year': '2014','month': '07','day': '23',
            'hour': '13','minute': '12','second': '45','microsecond': '321'}

    _datetime_to_tuple(dt_dict) -> (2014, 7, 23, 13, 12, 45, 321)
    """

    year, month, day = _date_to_tuple(dt_dict)
    hour, minute, second, microsecond = _time_to_tuple(dt_dict)

    return year, month, day, hour, minute, second, microsecond


Instruction:
Can you tell what is the following Python 3 function doing
def convert_2_utc(self, datetime_, timezone):

        datetime_ = self.tz_mapper[timezone].localize(datetime_)
        return datetime_.astimezone(pytz.UTC)

Response: convert to datetime to UTC offset.


Instruction:
Can you implement a function in Python 3 that
searches for a query using the Entrez ESearch API.

Response:
def esearch(database, query, userhistory=True, webenv=False, query_key=False, retstart=False, retmax=False,
            api_key=False, email=False, **kwargs) -> Optional[EsearchResult]:
    """Search for a query using the Entrez ESearch API.

    Parameters
    ----------
    database : str
        Entez database to search.
    query : str
        Query string
    userhistory : bool
        Tells API to return a WebEnV and query_key.
    webenv : str
        An Entrez WebEnv to use saved history.
    query_key : str
        An Entrez query_key to use saved history.
    retstart : int
        Return values starting at this index.
    retmax : int
        Return at most this number of values.
    api_key : str
        A users API key which allows more requests per second
    email : str
        A users email which is required if not using API.

    Returns
    -------
    EsearchResult
        A named tuple with values [ids, count, webenv, query_key]

    """
    cleaned_query = urllib.parse.quote_plus(query, safe='/+')

    url = BASE_URL + f'esearch.fcgi?db={database}&term={cleaned_query}&retmode=json'
    url = check_userhistory(userhistory, url)
    url = check_webenv(webenv, url)
    url = check_query_key(query_key, url)
    url = check_retstart(retstart, url)
    url = check_retmax(retmax, url)
    url = check_api_key(api_key, url)
    url = check_email(email, url)

    time.sleep(PAUSE)
    resp = requests.get(url)
    if resp.status_code != 200:
        print('There was a server error')
        return

    text = resp.json()
    time.sleep(.5)
    return EsearchResult(
        text['esearchresult'].get('idlist', []),
        make_number(text['esearchresult'].get('count', ''), int),
        text['esearchresult'].get('webenv', ''),
        text['esearchresult'].get('querykey', '')
    )


Instruction:
Create a Python 3 function for
posting IDs using the Entrez ESearch API.

Response:
def epost(database, ids: List[str], webenv=False, api_key=False, email=False, **kwargs) -> Optional[EpostResult]:
    """Post IDs using the Entrez ESearch API.

    Parameters
    ----------
    database : str
        Entez database to search.
    ids : list
        List of IDs to submit to the server.
    webenv : str
        An Entrez WebEnv to post ids to.
    api_key : str
        A users API key which allows more requests per second
    email : str
        A users email which is required if not using API.

    Returns
    -------
    requests.Response

    """
    url = BASE_URL + f'epost.fcgi'
    id = ','.join(ids)
    url_params = f'db={database}&id={id}'
    url_params = check_webenv(webenv, url_params)
    url_params = check_api_key(api_key, url_params)
    url_params = check_email(email, url_params)
    resp = entrez_try_put_multiple_times(url, url_params, num_tries=3)
    time.sleep(.5)
    return parse_epost(resp.text)


Instruction:
Given the following Python 3 function, write the documentation
def elink(db: str, dbfrom: str, ids=False, webenv=False, query_key=False, api_key=False, email=False,
          **kwargs) -> Optional[ElinkResult]:
    """Get document summaries using the Entrez ESearch API.

    Parameters
    ----------
    db : str
        Entez database to get ids from.
    dbfrom : str
        Entez database the provided ids are from.
    ids : list or str
        List of IDs to submit to the server.
    webenv : str
        An Entrez WebEnv to use saved history.
    query_key : str
        An Entrez query_key to use saved history.
    api_key : str
        A users API key which allows more requests per second
    email : str
        A users email which is required if not using API.

    Returns
    -------
    list
        A list of ElinkResult with values [id, srx, create_date, update_date]

    """
    url = BASE_URL + f'elink.fcgi?dbfrom={dbfrom}&db={db}&retmode=json&cmd=neighbor_history'
    url = check_webenv(webenv, url)
    url = check_query_key(query_key, url)
    url = check_api_key(api_key, url)
    url = check_email(email, url)

    if ids:
        if isinstance(ids, str):
            id = ids
        else:
            id = ','.join(ids)
        url += f'&id={id}'

    time.sleep(PAUSE)
    resp = requests.get(url)
    if resp.status_code != 200:
        print('There was a server error')
        return

    text = resp.json()
    time.sleep(.5)
    return ElinkResult(
        text['linksets'][0].get('dbfrom', ''),
        text['linksets'][0].get('linksetdbhistories', [{'dbto': ''}])[0].get('dbto', ''),
        text['linksets'][0].get('webenv', ''),
        text['linksets'][0].get('linksetdbhistories', [{'querykey': ''}])[0].get('querykey', ''),
    )

Response: Get document summaries using the ESearch API.


Instruction:
Given the following Python 3 function, write the documentation
def esummary(database: str, ids=False, webenv=False, query_key=False, count=False, retstart=False, retmax=False,
             api_key=False, email=False, **kwargs) -> Optional[List[EsummaryResult]]:
    """Get document summaries using the Entrez ESearch API.

    Parameters
    ----------
    database : str
        Entez database to search.
    ids : list or str
        List of IDs to submit to the server.
    webenv : str
        An Entrez WebEnv to use saved history.
    query_key : str
        An Entrez query_key to use saved history.
    count : int
        Number of records in the webenv
    retstart : int
        Return values starting at this index.
    retmax : int
        Return at most this number of values.
    api_key : str
        A users API key which allows more requests per second
    email : str
        A users email which is required if not using API.

    Returns
    -------
    list
        A list of EsummaryResults with values [id, srx, create_date, update_date]

    """
    url = BASE_URL + f'esummary.fcgi?db={database}'
    url = check_webenv(webenv, url)
    url = check_query_key(query_key, url)
    url = check_api_key(api_key, url)
    url = check_email(email, url)

    if ids:
        if isinstance(ids, str):
            id = ids
        else:
            id = ','.join(ids)
        url += f'&id={id}'
        count = len(id.split(','))

    for resp in entrez_sets_of_results(url, retstart, retmax, count):
        yield resp.text

Response: Get document summaries using the Entrez ESearch API.


Instruction:
Explain what the following Python 3 code does
def efetch(database, ids=False, webenv=False, query_key=False, count=False, retstart=False, retmax=False,
           rettype='full', retmode='xml', api_key=False, email=False, **kwargs) -> str:
    """Get documents using the Entrez ESearch API.gg

    Parameters
    ----------
    database : str
        Entez database to search.
    ids : list or str
        List of IDs to submit to the server.
    webenv : str
        An Entrez WebEnv to use saved history.
    query_key : str
        An Entrez query_key to use saved history.
    count : int
        Number of records in the webenv
    retstart : int
        Return values starting at this index.
    retmax : int
        Return at most this number of values.
    rettype : str
        The type of document to return. Refer to link for valid return types for each database.
        https://www.ncbi.nlm.nih.gov/books/NBK25499/table/chapter4.T._valid_values_of__retmode_and/?report=objectonly
    retmode : str
        The format of document to return. Refer to link for valid formats for each database.
        https://www.ncbi.nlm.nih.gov/books/NBK25499/table/chapter4.T._valid_values_of__retmode_and/?report=objectonly
    api_key : str
        A users API key which allows more requests per second
    email : str
        A users email which is required if not using API.

    Yields
    ------
    str
        Text from effect results. Format depends on parameters passed to retmode

    """
    url = BASE_URL + f'efetch.fcgi?db={database}&retmode={retmode}&rettype={rettype}'
    url = check_webenv(webenv, url)
    url = check_query_key(query_key, url)
    url = check_api_key(api_key, url)
    url = check_email(email, url)

    if ids:
        if isinstance(ids, str):
            id = ids
        else:
            id = ','.join(ids)
        url += f'&id={id}'
        count = len(id.split(','))

    for resp in entrez_sets_of_results(url, retstart, retmax, count):
        yield resp.text

Response: Get documents using the Entrez ESearch API. gg


Instruction:
Can you generate the documentation for the following Python 3 function
def entrez_sets_of_results(url, retstart=False, retmax=False, count=False) -> Optional[List[requests.Response]]:
    if not retstart:
        retstart = 0

    if not retmax:
        retmax = 500

    if not count:
        count = retmax

    retmax = 500  # Entrez can return a max of 500
    while retstart < count:
        diff = count - retstart
        if diff < 500:
            retmax = diff

        _url = url + f'&retstart={retstart}&retmax={retmax}'
        resp = entrez_try_get_multiple_times(_url)
        if resp is None:
            return

        retstart += retmax
        yield resp

Response: Returns a generator that returns all the sets of results from Entrez.


Instruction:
Make a summary of the following Python 3 code
def print_runs(query):

    if query is None:
        return

    for tup in query:
        print(("{0} @ {1} - {2} id: {3} group: {4}".format(
            tup.end, tup.experiment_name, tup.project_name,
            tup.experiment_group, tup.run_group)))

Response: Print all rows in this result query.


Instruction:
How would you implement a function in Python 3 that
returns a list of layers from input SVG sources.

Response:
def get_svg_layers(svg_sources):
    '''
    Collect layers from input svg sources.

    Args:

        svg_sources (list) : A list of file-like objects, each containing
            one or more XML layers.

    Returns
    -------
    (width, height), layers : (int, int), list
        The first item in the tuple is the shape of the largest layer, and the
        second item is a list of ``Element`` objects (from :mod:`lxml.etree`
        module), one per SVG layer.
    '''
    layers = []
    width, height = None, None

    def extract_length(attr):
        'Extract length in pixels.'
        match = CRE_MM_LENGTH.match(attr)
        if match:
            # Length is specified in millimeters.
            return INKSCAPE_PPmm.magnitude * float(match.group('length'))
        else:
            return float(attr)

    for svg_source_i in svg_sources:
        # Parse input file.
        xml_root = etree.parse(svg_source_i)
        svg_root = xml_root.xpath('/svg:svg', namespaces=INKSCAPE_NSMAP)[0]
        width = max(extract_length(svg_root.attrib['width']), width)
        height = max(extract_length(svg_root.attrib['height']), height)
        layers += svg_root.xpath('//svg:g[@inkscape:groupmode="layer"]',
                                 namespaces=INKSCAPE_NSMAP)

    for i, layer_i in enumerate(layers):
        layer_i.attrib['id'] = 'layer%d' % (i + 1)
    return (width, height), layers


Instruction:
Can you implement a function in Python 3 that
merges layers from input SVG sources into a single XML document.

Response:
def merge_svg_layers(svg_sources, share_transform=True):
    '''
    Merge layers from input svg sources into a single XML document.

    Args:

        svg_sources (list) : A list of file-like objects, each containing
            one or more XML layers.
        share_transform (bool) : If exactly one layer has a transform, apply it
            to *all* other layers as well.

    Returns:

        StringIO.StringIO : File-like object containing merge XML document.
    '''
    # Get list of XML layers.
    (width, height), layers = get_svg_layers(svg_sources)

    if share_transform:
        transforms = [layer_i.attrib['transform'] for layer_i in layers
                      if 'transform' in layer_i.attrib]
        if len(transforms) > 1:
            raise ValueError('Transform can only be shared if *exactly one* '
                             'layer has a transform ({} layers have '
                             '`transform` attributes)'.format(len(transforms)))
        elif transforms:
            # Apply single common transform to all layers.
            for layer_i in layers:
                layer_i.attrib['transform'] = transforms[0]

    # Create blank XML output document.
    dwg = svgwrite.Drawing(profile='tiny', debug=False, size=(width, height))

    # Add append layers to output XML root element.
    output_svg_root = etree.fromstring(dwg.tostring())
    output_svg_root.extend(layers)

    # Write merged XML document to output file-like object.
    output = StringIO.StringIO()
    output.write(etree.tostring(output_svg_root))
    output.seek(0)
    return output


Instruction:
Explain what the following Python 3 code does
def main(argv=None):
    arguments = cli_common(__doc__, argv=argv)
    driver = CampaignDriver(arguments['CAMPAIGN-DIR'], expandcampvars=False)
    driver(no_exec=True)
    if argv is not None:
        return driver

Response: ben - ub entry point


Instruction:
Implement a Python 3 function for
counting the number of times each value occurs in a record sequence for a given field name.

Response:
def count_by(records: Sequence[Dict], field_name: str) -> defaultdict:
    """
    Frequency each value occurs in a record sequence for a given field name.
    """

    counter = defaultdict(int)

    for record in records:
        name = record[field_name]
        counter[name] += 1

    return counter


Instruction:
Can you generate the documentation for the following Python 3 function
def generate(self):
        exp_name = self.exp_name()

        fname = os.path.basename(self.out_path)
        fname = "{exp}_{prefix}_{name}{ending}".format(
            exp=exp_name,
            prefix=os.path.splitext(fname)[0],
            ending=os.path.splitext(fname)[-1],
            name="full")
        first = True
        for chunk in self.report():
            print("Writing chunk to :'{0}'".format(fname))
            chunk.to_csv(fname, header=first, mode='a')
            first = False

Response: Generate a huge. csv file for the current experiment.


Instruction:
Write a Python 3 function for
returning the metatab document for the URL

Response:
def doc(self):
        """Return the metatab document for the URL"""
        from metatab import MetatabDoc
        t = self.get_resource().get_target()
        return MetatabDoc(t.inner)


Instruction:
Can you tell what is the following Python 3 function doing
def main(argv=None):
    arguments = cli_common(__doc__, argv=argv)
    campaign_path = arguments['CAMPAIGN-DIR']
    driver = CampaignDriver(campaign_path, expandcampvars=False)
    with pushd(campaign_path):
        render(
            template=arguments['--template'],
            ostr=arguments['--output'],
            campaign=driver,
        )
    if argv is not None:
        return driver

Response: ben - doc entry point


Instruction:
Make a summary of the following Python 3 code
def class_to_str(obj):
    mod_str = obj.__module__
    name_str = obj.__name__
    if mod_str == '__main__':
        return name_str
    else:
        return '.'.join([mod_str, name_str])

Response: get class string from object


Instruction:
How would you explain what the following Python 3 function does
def get_module_path(module):
    return pathlib.Path(
        os.path.dirname(os.path.abspath(inspect.getfile(module))))

Response: return a directory path to a module


Instruction:
Can you generate the documentation for the following Python 3 function
def get_data_path(data, module, check_exists=True):
    basepath = os.path.dirname(os.path.abspath(inspect.getfile(module)))

    if isinstance(data, basestring):
        data = [data]

    dirpath = os.path.join(basepath, *data)

    if check_exists:
        assert os.path.exists(dirpath), '{0} does not exist'.format(dirpath)

    return pathlib.Path(dirpath)

Response: returns a directory path to data within a module


Instruction:
Given the following Python 3 function, write the documentation
def memory_usage():
    try:
        import psutil
        import os
    except ImportError:
        return _memory_usage_ps()

    process = psutil.Process(os.getpid())
    mem = process.memory_info()[0] / float(2 ** 20)
    return mem

Response: return memory usage of python process in MB


Instruction:
How would you code a function in Python 3 to
return memory usage of python process in MB >>> is_memory_usage_ps(),float

Response:
def _memory_usage_ps():
    """return memory usage of python process in MB

    >>> isinstance(_memory_usage_ps(),float)
    True

    """
    out = subprocess.Popen(
        ['ps', 'v', '-p', str(os.getpid())],
        stdout=subprocess.PIPE).communicate()[0].split(b'\n')
    vsz_index = out[0].split().index(b'RSS')
    mem = float(out[1].split()[vsz_index]) / 1024
    return mem


Instruction:
Can you create a Python 3 function that
loads memory usage ipython magic and magics class

Response:
def load_memit():
    """load memory usage ipython magic,
    require memory_profiler package to be installed

    to get usage: %memit?

    Author: Vlad Niculae <vlad@vene.ro>
    Makes use of memory_profiler from Fabian Pedregosa
    available at https://github.com/fabianp/memory_profiler
    """
    from IPython.core.magic import Magics, line_magic, magics_class
    from memory_profiler import memory_usage as _mu

    try:
        ip = get_ipython()
    except NameError as err:
        raise Exception('not in ipython/jupyter kernel:\n {}'.format(err))

    @magics_class
    class MemMagics(Magics):
        @line_magic
        def memit(self, line='', setup='pass'):
            """Measure memory usage of a Python statement

            Usage, in line mode:
              %memit [-ir<R>t<T>] statement

            Options:
            -r<R>: repeat the loop iteration <R> times
            and take the best result. Default: 3

            -i: run the code in the current environment, without forking a new
            process.
            This is required on some MacOS versions of Accelerate if your
            line contains a call to `np.dot`.

            -t<T>: timeout after <T> seconds. Unused if `-i` is active.
            Default: None

            Examples
            --------
            ::

              In [1]: import numpy as np

              In [2]: %memit np.zeros(1e7)
              maximum of 3: 76.402344 MB per loop

              In [3]: %memit np.ones(1e6)
              maximum of 3: 7.820312 MB per loop

              In [4]: %memit -r 10 np.empty(1e8)
              maximum of 10: 0.101562 MB per loop

              In [5]: memit -t 3 while True: pass;
              Subprocess timed out.
              Subprocess timed out.
              Subprocess timed out.
              ERROR: all subprocesses exited unsuccessfully. Try again with the
              `-i` option.
              maximum of 3: -inf MB per loop

            """
            opts, stmt = self.parse_options(line, 'r:t:i', posix=False,
                                            strict=False)
            repeat = int(getattr(opts, 'r', 3))
            if repeat < 1:
                repeat == 1
            timeout = int(getattr(opts, 't', 0))
            if timeout <= 0:
                timeout = None
            run_in_place = hasattr(opts, 'i')

            # Don't depend on multiprocessing:
            try:
                import multiprocessing as pr
                from multiprocessing.queues import SimpleQueue
                q = SimpleQueue()
            except ImportError:
                class ListWithPut(list):
                    """Just a list,
                    where the `append` method is aliased to `put`."""

                    def put(self, x):
                        self.append(x)

                q = ListWithPut()
                print(
                    'WARNING: cannot import module `multiprocessing`. Forcing '
                    'the `-i` option.')
                run_in_place = True

            ns = self.shell.user_ns

            def _get_usage(q, stmt, setup='pass', ns={}):
                try:
                    exec(setup) in ns
                    _mu0 = _mu()[0]
                    exec(stmt) in ns
                    _mu1 = _mu()[0]
                    q.put(_mu1 - _mu0)
                except Exception as e:
                    q.put(float('-inf'))
                    raise e

            if run_in_place:
                for _ in range(repeat):
                    _get_usage(q, stmt, ns=ns)
            else:
                # run in consecutive subprocesses
                at_least_one_worked = False
                for _ in range(repeat):
                    p = pr.Process(
                        target=_get_usage, args=(q, stmt, 'pass', ns))
                    p.start()
                    p.join(timeout=timeout)
                    if p.exitcode == 0:
                        at_least_one_worked = True
                    else:
                        p.terminate()
                        if p.exitcode is None:
                            print('Subprocess timed out.')
                        else:
                            print(
                                'Subprocess exited with code %d.' % p.exitcode)
                        q.put(float('-inf'))

                if not at_least_one_worked:
                    print('ERROR: all subprocesses exited unsuccessfully. Try '
                          'again with the `-i` option.')

            usages = [q.get() for _ in range(repeat)]
            usage = max(usages)
            print("maximum of %d: %f MB per loop" % (repeat, usage))

    ip.register_magics(MemMagics)


Instruction:
Write a Python 3 function that can
process key locations in the xml. etree. ElementTree. elementTree object node.

Response:
def parse_tree_from_dict(node, locs):
    """Processes key locations.

    Parameters
    ----------
    node: xml.etree.ElementTree.ElementTree.element
        Current node.
    locs: dict
        A dictionary mapping key to a tuple. The tuple can either be 2 or 3
        elements long. The first element maps to the location in the
        current node. The second element given a processing hint. Possible
        values are:

            * 'text': assumes the text element of the path is wanted.
            * 'child': assumes that the child of the given path is wanted.
            * str: Any other string will be treated as an attribute lookup
                   of the path.

        If 'child' is given, then a third element needs to be given
        indicating the type of processing. Possible values are:

            * 'text': assumes the text element of the path is wanted.
            * 'tag': assumes the class tag of the path is wanted.
            * str: Any other string will be treated as an attribute lookup
                   of the path.
    """
    d = dict()
    for n, l in locs.items():
        try:
            if l[1] == 'text':
                d[n] = node.find(l[0]).text
            elif l[1] == 'child':
                child = node.find(l[0]).getchildren()

                if len(child) > 1:
                    raise AmbiguousElementException(
                            'There are too many elements')
                elif l[2] == 'text':
                    d[n] = child[0].text
                elif l[2] == 'tag':
                    d[n] = child[0].tag
            else:
                d[n] = node.find(l[0]).get(l[1])
        except:
            pass

    return d


Instruction:
Can you generate the documentation for the following Python 3 function
def xml_to_root(xml: Union[str, IO]) -> ElementTree.Element:
    if isinstance(xml, str):
        if '<' in xml:
            return ElementTree.fromstring(xml)
        else:
            with open(xml) as fh:
                xml_to_root(fh)
    tree = ElementTree.parse(xml)
    return tree.getroot()

Response: Parse XML into an ElemeTree object.


Instruction:
Can you generate the documentation for the following Python 3 function
def newLayer(self, effect=''):
		self.layers.append(Layer(effect = effect))
		self.activeLayer = len(self.layers)-1

Response: Creates a new layer and set it as the active.


Instruction:
Implement a Python 3 function for
duplicating the current active layer.

Response:
def duplicateActiveLayer(self):
		"""
		Duplicates the current active :py:class:`Layer`.

		:rtype: Nothing.
		"""
		activeLayer = self.layers[self.activeLayer]
		newLayer = Layer(data=activeLayer.data, effect=activeLayer.effect)
		self.layers.append(newLayer)
		self.activeLayer = len(self.layers)-1


Instruction:
Can you generate a brief explanation for the following Python 3 code
def mergeAllLayers(self):
		start = time.time()
		while(len(self.layers)>1):
			self.mergeBottomLayers()
		print('merge time:'+str(time.time()-start))
		return self.layers[0]

Response: Merge all the layers together.


Instruction:
Can you tell what is the following Python 3 function doing
def synonyms(self):
        syns = {}

        for k, v in self._declared_terms.items():
            k = k.strip()
            if v.get('synonym'):
                syns[k.lower()] = v['synonym']

                if not '.' in k:
                    syns[ROOT_TERM + '.' + k.lower()] = v['synonym']

        return syns

Response: Return a dict of term synonyms


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

        # If the doc already has super terms, we've already parsed something, so
        # assume we parsed the declaration, and can use re-use the old decls.
        if self.doc and self.doc.super_terms:
            return self.doc.super_terms

        return  {k.lower(): v['inheritsfrom'].lower()
                         for k, v in self._declared_terms.items() if 'inheritsfrom' in v}

Response: Return a dictionary mapping term names to their super terms


Instruction:
Can you generate the documentation for the following Python 3 function
def declare_dict(self):
        # Run the parser, if it has not been run yet.
        if not self.root:
            for _ in self: pass

        return {
            'sections': self._declared_sections,
            'terms': self._declared_terms,
            'synonyms': self.synonyms
        }

Response: Return the declared sections terms and synonyms as a dict


Instruction:
Create a Python 3 function for
replacing the record_term and parent_term with a synonym

Response:
def substitute_synonym(self, nt):
        """
        Replace the record_term and parent_term with a synonym
        :param nt:
        :return:
        """

        if nt.join_lc in self.synonyms:
            nt.parent_term, nt.record_term = Term.split_term_lower(self.synonyms[nt.join_lc]);


Instruction:
How would you explain what the following Python 3 function does
def errors_as_dict(self):
        errors = []

        for e in self.errors:

            errors.append({
                'file': e.term.file_name,
                'row': e.term.row if e.term else '<unknown>',
                'col': e.term.col if e.term else '<unknown>',
                'term': e.term.join if e.term else '<unknown>',
                'error': str(e)
            })

        return errors

Response: Return parse errors as a dict


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_declare_doc(self, d, name):

        path = None
        while True:
            if exists(name):
                path =name
                break

            try:
                # Look for a local document
                path = declaration_path(name)
                break
            except IncludeError:
                pass

            for fn in (join(d, name), join(d, name + '.csv')):
                if exists(fn):
                    path = fn
                    break
            if path:
                break

            if name.startswith('http'):
                path = name.strip('/')  # Look for the file on the web
                break
            elif exists(name):
                path = name
                break
            else:
                path = self.resolver.find_decl_doc(name)
                break

            raise IncludeError("No local declaration file for '{}'".format(name))

        return parse_app_url(path)

Response: Given a name try to resolve it to a path or URL to a declaration document.


Instruction:
How would you explain what the following Python 3 function does
def find_include_doc(self, d, name):
        from metatab import parse_app_url

        include_ref = name.strip('/')

        if include_ref.startswith('http'):
            path = include_ref
        else:
            if not d:
                raise IncludeError("Can't include '{}' because don't know current path "
                                   .format(name))

            path = join(d, include_ref)

        return parse_app_url(path)

Response: Resolve a name or path for an include doc to an absolute path or url


Instruction:
Here you have a function in Python 3, explain what it does
def generate_terms(self, ref, root, file_type=None):

        last_section = root
        t = None

        if isinstance(ref, Source):
            row_gen = ref
            ref_path = row_gen.__class__.__name__
        else:
            row_gen = get_generator(ref)
            ref_path = ref.path

        try:
            for line_n, row in enumerate(row_gen, 1):

                if not row or not row[0] or not row[0].strip() or row[0].strip().startswith('#'):
                    continue

                tt = Term(row[0], None) # Just to get the qualified name constructed property

                term_class = self.get_term_class(tt.join_lc)

                t = term_class(tt.join_lc,
                         row[1] if len(row) > 1 else '',
                         row[2:] if len(row) > 2 else [],
                         row=line_n,
                         col=1,
                         file_name=ref_path, file_type=file_type, doc=self.doc)

                # Why did we remove comments from values? It strips out Markdown
                #if t.value and str(t.value).startswith('#'): # Comments are ignored
                #    continue

                if t.term_is('include') or t.term_is('declare'):

                    if t.term_is('include'):
                        resolved = self.find_include_doc(dirname(ref_path), t.value.strip())
                    else:
                        resolved = self.find_declare_doc(dirname(ref_path), t.value.strip())

                    if row_gen.ref == resolved:
                        raise IncludeError("Include loop for '{}' ".format(resolved))

                    yield t

                    try:

                        sub_gen = get_generator(resolved.get_resource().get_target())

                        for t in self.generate_terms(sub_gen, root, file_type=t.record_term_lc):
                            yield t

                        if last_section:
                            yield last_section  # Re-assert the last section

                    except IncludeError as e:
                        e.term = t
                        raise

                    except (OSError,  FileNotFoundError, GenerateError, DownloadError) as e:
                        e = IncludeError("Failed to Include; {}".format(e))
                        e.term = t
                        raise e

                    continue  # Already yielded the include/declare term, and includes can't have children

                elif t.term_is('section'):

                    # If there is already a section in the document, emit the existing section,
                    # rather than a new one.
                    try:
                        last_section = self.doc[t.name]
                        t = last_section

                    except (KeyError, TypeError): # TypeError -> self.doc is None
                        last_section = t

                yield t

                # Yield any child terms, from the term row arguments
                if not t.term_is('section') and not t.term_is('header'):
                    for col, value in enumerate(t.args, 0):
                        if str(value).strip():

                            term_name = t.record_term_lc + '.' + str(col)

                            term_class = self.get_term_class(term_name)

                            yield term_class(term_name, str(value), [],
                                       row=line_n,
                                       col=col + 2,  # The 0th argument starts in col 2
                                       file_name=ref_path,
                                       file_type=file_type,
                                       parent=t) #,
                                       #doc=None,
                                       #section=last_section)
        except IncludeError as e:
            exc = IncludeError(str(e) + "; in '{}' ".format(ref_path))
            exc.term = e.term if hasattr(e, 'term') else None
            raise exc

Response:
Generates a generator that yields term objects handling includes and argument
        children.


Instruction:
How would you code a function in Python 3 to
yield the inherited children of the input term

Response:
def inherited_children(self, t):
        """Generate inherited children based on a terms InhertsFrom property.
        The input term must have both an InheritsFrom property and a defined Section

        :param t: A subclassed terms -- has an InheritsFrom value
        """

        if not t.get('inheritsfrom'):
            return

        if not 'section' in t:
            raise DeclarationError("DeclareTerm for '{}' must specify a section to use InheritsFrom"
                                   .format(t['term']))

        t_p, t_r = Term.split_term(t['term'])
        ih_p, ih_r = Term.split_term(t['inheritsfrom'])

        # The inherited terms must come from the same section
        section_terms = self._declared_sections[t['section'].lower()]['terms']

        # For each of the terms in the section, look for terms that are children
        # of the term that the input term inherits from. Then yield each of those terms
        # after chang the term name to be a child of the input term.
        for st_name in section_terms:
            if st_name.lower().startswith(ih_r.lower() + '.'):
                st_p, st_r = Term.split_term(st_name)
                # Yield the term, but replace the parent part
                subtype_name = t_r + '.' + st_r

                subtype_d = dict(self._declared_terms[st_name.lower()].items())
                subtype_d['inheritsfrom'] = '';
                subtype_d['term'] = subtype_name

                yield subtype_d


Instruction:
Can you tell what is the following Python 3 function doing
def get_remote_peer_list(self):
        '''
        listPeer 返回列表
        {
            "rtn":0,
            "peerList": [{
                "category": "",
                "status": 0,
                "name": "GUNNER_HOME",
                "vodPort": 43566,
                "company": "XUNLEI_MIPS_BE_MIPS32",
                "pid": "8498352EB4F5208X0001",
                "lastLoginTime": 1412053233,
                "accesscode": "",
                "localIP": "",
                "location": "\u6d59\u6c5f\u7701  \u8054\u901a",
                "online": 1,
                "path_list": "C:/",
                "type": 30,
                "deviceVersion": 22083310
            }]
        }
        '''

        params = {
            'type': 0,
            'v': DEFAULT_V,
            'ct': 2
        }
        res = self._get('listPeer', params=params)
        return res['peerList']

Response: Get remote peer list.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_remote_task_list(
            self, peer_id, list_type=ListType.downloading, pos=0, number=10):
        '''
        list 返回列表
        {
            "recycleNum": 0,
            "serverFailNum": 0,
            "rtn": 0,
            "completeNum": 34,
            "sync": 0,
            "tasks": [{
                "failCode": 15414,
                "vipChannel": {
                    "available": 0,
                    "failCode": 0,
                    "opened": 0,
                    "type": 0,
                    "dlBytes": 0,
                    "speed": 0
                },
                "name": "Blablaba",
                "url": "magnet:?xt=urn:btih:5DF6B321CCBDEBE1D52E8E15CBFC6F002",
                "speed": 0,
                "lixianChannel": {
                    "failCode": 0,
                    "serverProgress": 0,
                    "dlBytes": 0,
                    "state": 0,
                    "serverSpeed": 0,
                    "speed": 0
                },
                "downTime": 0,
                "subList": [],
                "createTime": 1412217010,
                "state": 8,
                "remainTime": 0,
                "progress": 0,
                "path": "/tmp/thunder/volumes/C:/TDDOWNLOAD/",
                "type": 2,
                "id": "39",
                "completeTime": 0,
                "size": 0
            },
            ...
            ]
        }
        '''

        params = {
            'pid': peer_id,
            'type': list_type,
            'pos': pos,
            'number': number,
            'needUrl': 1,
            'v': DEFAULT_V,
            'ct': DEFAULT_CT
        }
        res = self._get('list', params=params)
        return res['tasks']

Response: Get remote task list.


Instruction:
Here you have a function in Python 3, explain what it does
def check_url(self, pid, url_list):
        '''
        urlCheck 返回数据
        {
            "rtn": 0,
            "taskInfo": {
                "failCode": 0,
                "name": ".HDTVrip.1024X576.mkv",
                "url": "ed2k://|file|%E6%B0%",
                "type": 1,
                "id": "0",
                "size": 505005442
            }
        }
        '''
        task_list = []
        for url in url_list:
            params = {
                'pid': pid,
                'url': url,
                'type': 1,
                'v': DEFAULT_V,
                'ct': DEFAULT_CT
            }
            res = self._get('urlCheck', params=params)

            if res['rtn'] == 0:
                task_info = res['taskInfo']
                task_list.append({
                    'url': task_info['url'],
                    'name': task_info['name'],
                    'filesize': task_info['size'],
                    'gcid': '',
                    'cid': ''
                })
            else:
                print(
                    'url [%s] check failed, code:%s.',
                    url,
                    task_info['failCode']
                )

        return task_list

Response: Check if a URL is in the list of URLs.


Instruction:
Implement a Python 3 function for
adding tasks to remote

Response:
def add_tasks_to_remote(self, pid, path='C:/TDDOWNLOAD/', task_list=[]):
        '''
        post data:
        {
            "path":"C:/TDDOWNLOAD/",
            "tasks":[{
                "url":"ed2k://|file|%E6%B0%B8%E6%81%92.Forever...",
                "name":"永恒.Forever.S01E02.中英字幕.WEB-HR.mkv",
                "gcid":"",
                "cid":"",
                "filesize":512807020
            }]
        }

        return data:
        {
            "tasks": [{
                "name": "\u6c38\u6052.Fore76.x264.mkv",
                "url": "ed2k://|file|%E6%B0%B8%E6%81%92",
                "result": 202,
                "taskid": "48",
                "msg": "repeate_taskid:48",
                "id": 1
            }],
            "rtn": 0
        }
        '''

        if len(task_list) == 0:
            return []

        params = {
            'pid': pid,
            'v': DEFAULT_V,
            'ct': DEFAULT_CT,
        }

        data = {
            'path': path,
            'tasks': task_list
        }

        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        data = json.dumps(data)
        data = quote(data)
        data = 'json=' + data

        res = self._post(
            'createTask',
            params=params,
            data=data,
            headers=headers
        )

        return res


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read_levels(text):
    x = []
    for i in range(0, len(NAUTILUSRESOLVER.getMetadata(text).citation)):
        x.append(NAUTILUSRESOLVER.getReffs(text, level=i))
    return x

Response: Read text and get there reffs


Instruction:
Write a Python 3 function for
providing a manager for flask scripts to perform specific maintenance operations :param resolver: Nautilus Extension Instance :type resolver: NautilusCtsResolver :param flask_nautilus: Flask Application :type flask_nautilus: FlaskNautilus :return: CLI :rtype: click.group Import with .. code-block:: python :lineno: from capitains_nautilus.manager import FlaskNautilusManager manager = FlaskNautilusManager(resolver, flask_nautilus, app) # Where app is the name of your app if __name__ == "__main__": manager()

Response:
def FlaskNautilusManager(resolver, flask_nautilus):
    """ Provides a manager for flask scripts to perform specific maintenance operations

    :param resolver: Nautilus Extension Instance
    :type resolver: NautilusCtsResolver
    :param flask_nautilus: Flask Application
    :type flask_nautilus: FlaskNautilus
    :return: CLI
    :rtype: click.group

    Import with

    .. code-block:: python
        :lineno:

        from capitains_nautilus.manager import FlaskNautilusManager
        manager = FlaskNautilusManager(resolver, flask_nautilus, app)  # Where app is the name of your app

        if __name__ == "__main__":
            manager()

    """
    global NAUTILUSRESOLVER
    NAUTILUSRESOLVER = resolver

    @click.group()
    @click.option('--verbose', default=False)
    def CLI(verbose):
        """ CLI for Flask Nautilus """
        click.echo("Command Line Interface of Flask")
        resolver.logger.disabled = not verbose

    @CLI.command()
    def flush_resolver():
        """ Flush the resolver cache system """
        if resolver.clear() is True:
            click.echo("Caching of Resolver Cleared")

    @CLI.command()
    def flush_http_cache():
        """ Flush the http cache

        Warning : Might flush other Flask Caching data !
        """
        flask_nautilus.flaskcache.clear()

    @CLI.command()
    def flush_both():
        """ Flush all caches

        """
        if resolver.cache.clear() is True:
            click.echo("Caching of Resolver Cleared")
        if flask_nautilus.flaskcache.clear() is True:
            click.echo("Caching of HTTP Cleared")

    @CLI.command()
    def parse():
        """ Preprocess the inventory and cache it """
        ret = resolver.parse()
        click.echo("Preprocessed %s texts" % len(ret.readableDescendants))

    @CLI.command()
    @click.option('--threads', default=0, type=int)
    def process_reffs(threads):
        """ Preprocess the inventory and cache it """
        if threads < 1:
            threads = THREADS
        texts = list(resolver.getMetadata().readableDescendants)
        click.echo("Using {} processes to parse references of {} texts".format(threads, len(texts)))
        with Pool(processes=threads) as executor:
            for future in executor.imap_unordered(read_levels, [t.id for t in texts]):
                del future
        click.echo("References parsed")

    return CLI


Instruction:
How would you explain what the following Python 3 function does
def is_iter_non_string(obj):
    if isinstance(obj, list) or isinstance(obj, tuple):
        return True
    return False

Response: test if object is a list or tuple


Instruction:
Create a Python 3 function for
testing if object is dict like

Response:
def is_dict_like(obj, attr=('keys', 'items')):
    """test if object is dict like"""
    for a in attr:
        if not hasattr(obj, a):
            return False
    return True


Instruction:
Create a Python 3 function to
test if object is a list containing dict like items

Response:
def is_list_of_dict_like(obj, attr=('keys', 'items')):
    """test if object is a list only containing dict like items """
    try:
        if len(obj) == 0:
            return False
        return all([is_dict_like(i, attr) for i in obj])
    except Exception:
        return False


Instruction:
Here you have a function in Python 3, explain what it does
def is_path_like(obj, attr=('name', 'is_file', 'is_dir', 'iterdir')):
    for a in attr:
        if not hasattr(obj, a):
            return False
    return True

Response: test if object is pathlib. Path like


Instruction:
How would you implement a function in Python 3 that
converts all values of one type to another type

Response:
def convert_type(d, intype, outtype, convert_list=True, in_place=True):
    """ convert all values of one type to another

    Parameters
    ----------
    d : dict
    intype : type_class
    outtype : type_class
    convert_list : bool
        whether to convert instances inside lists and tuples
    in_place : bool
        if True, applies conversions to original dict, else returns copy

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {'a':'1','b':'2'}
    >>> pprint(convert_type(d,str,float))
    {'a': 1.0, 'b': 2.0}

    >>> d = {'a':['1','2']}
    >>> pprint(convert_type(d,str,float))
    {'a': [1.0, 2.0]}

    >>> d = {'a':[('1','2'),[3,4]]}
    >>> pprint(convert_type(d,str,float))
    {'a': [(1.0, 2.0), [3, 4]]}

    """
    if not in_place:
        out_dict = copy.deepcopy(d)
    else:
        out_dict = d

    def _convert(obj):
        if isinstance(obj, intype):
            try:
                obj = outtype(obj)
            except Exception:
                pass
        elif isinstance(obj, list) and convert_list:
            obj = _traverse_iter(obj)
        elif isinstance(obj, tuple) and convert_list:
            obj = tuple(_traverse_iter(obj))

        return obj

    def _traverse_dict(dic):
        for key in dic.keys():
            if is_dict_like(dic[key]):
                _traverse_dict(dic[key])
            else:
                dic[key] = _convert(dic[key])

    def _traverse_iter(iter):
        new_iter = []
        for key in iter:
            if is_dict_like(key):
                _traverse_dict(key)
                new_iter.append(key)
            else:
                new_iter.append(_convert(key))

        return new_iter

    if is_dict_like(out_dict):
        _traverse_dict(out_dict)
    else:
        _convert(out_dict)

    return out_dict


Instruction:
Explain what the following Python 3 code does
def pprint(d, lvlindent=2, initindent=0, delim=':',
           max_width=80, depth=3, no_values=False,
           align_vals=True, print_func=None,
           keycolor=None, compress_lists=None,
           round_floats=None, _dlist=False):
    """ print a nested dict in readable format
        (- denotes an element in a list of dictionaries)

    Parameters
    ----------
    d : object
    lvlindent : int
        additional indentation spaces for each level
    initindent : int
        initial indentation spaces
    delim : str
        delimiter between key and value nodes
    max_width : int
        max character width of each line
    depth : int or None
        maximum levels to display
    no_values : bool
        whether to print values
    align_vals : bool
        whether to align values for each level
    print_func : callable or None
        function to print strings (print if None)
    keycolor : None or str
         if str, color keys by this color,
         allowed: red, green, yellow, blue, magenta, cyan, white
    compress_lists : int
         compress lists/tuples longer than this,
          e.g. [1,1,1,1,1,1] -> [1, 1,..., 1]
    round_floats : int
         significant figures for floats

    Examples
    --------

    >>> d = {'a':{'b':{'c':'Å','de':[4,5,[7,'x'],9]}}}
    >>> pprint(d,depth=None)
    a:
      b:
        c:  Å
        de: [4, 5, [7, x], 9]
    >>> pprint(d,max_width=17,depth=None)
    a:
      b:
        c:  Å
        de: [4, 5,
            [7, x],
            9]
    >>> pprint(d,no_values=True,depth=None)
    a:
      b:
        c:
        de:
    >>> pprint(d,depth=2)
    a:
      b: {...}
    >>> pprint({'a':[1,1,1,1,1,1,1,1]},
    ...        compress_lists=3)
    a: [1, 1, 1, ...(x5)]

    """
    if print_func is None:
        print_func = _default_print_func

    if not is_dict_like(d):
        d = {'': d}
        # print_func('{}'.format(d))
        # return

    extra = lvlindent if _dlist else 0

    def decode_to_str(obj):
        val_string = obj
        if isinstance(obj, list):
            if compress_lists is not None:
                if len(obj) > compress_lists:
                    diff = str(len(obj) - compress_lists)
                    obj = obj[:compress_lists] + ['...(x{})'.format(diff)]
            val_string = '[' + ', '.join([decode_to_str(o) for o in obj]) + ']'
        elif isinstance(obj, tuple):
            if compress_lists is not None:
                if len(obj) > compress_lists:
                    diff = str(len(obj) - compress_lists)
                    obj = list(
                        obj[:compress_lists]) + ['...(x{})'.format(diff)]
            val_string = '(' + ', '.join([decode_to_str(o) for o in obj]) + ')'
        elif isinstance(obj, float) and round_floats is not None:
            round_str = '{0:.' + str(round_floats - 1) + 'E}'
            val_string = str(float(round_str.format(obj)))
        else:
            try:
                val_string = encode(obj, outtype='str')
            except (TypeError, UnicodeError):
                pass
        # convert unicode to str (so no u'' prefix in python 2)
        try:
            return str(val_string)
        except Exception:
            return unicode(val_string)

    if align_vals:
        key_width = 0
        for key, val in d.items():
            if not is_dict_like(val):
                key_str = decode_to_str(key)
                key_width = max(key_width, len(key_str))

    max_depth = depth
    for i, key in enumerate(natural_sort(d.keys())):
        value = d[key]
        if _dlist and i == 0:
            key_str = '- ' + decode_to_str(key)
        elif _dlist:
            key_str = '  ' + decode_to_str(key)
        else:
            key_str = decode_to_str(key)

        if keycolor is not None:
            key_str = colortxt(key_str, keycolor)

        if align_vals:
            key_str = '{0: <{1}} '.format(
                key_str + delim, key_width + len(delim))
        else:
            key_str = '{0}{1} '.format(key_str, delim)

        depth = max_depth if max_depth is not None else 2
        if keycolor is not None:
            key_length = len(_strip_ansi(key_str))
        else:
            key_length = len(key_str)
        key_line = ' ' * initindent + key_str
        new_line = ' ' * initindent + ' ' * key_length

        if depth <= 0:
            continue
        if is_dict_like(value):
            if depth <= 1:
                print_func(' ' * initindent + key_str + '{...}')
            else:
                print_func(' ' * initindent + key_str)
                pprint(value, lvlindent, initindent + lvlindent + extra, delim,
                       max_width,
                       depth=max_depth - 1 if max_depth is not None else None,
                       no_values=no_values, align_vals=align_vals,
                       print_func=print_func, keycolor=keycolor,
                       compress_lists=compress_lists,
                       round_floats=round_floats)
            continue

        if isinstance(value, list):
            if all([is_dict_like(o) for o in value]) and value:
                if depth <= 1:
                    print_func(key_line + '[...]')
                    continue
                print_func(key_line)
                for obj in value:
                    pprint(
                        obj, lvlindent, initindent + lvlindent + extra, delim,
                        max_width,
                        depth=max_depth - 1 if max_depth is not None else None,
                        no_values=no_values, align_vals=align_vals,
                        print_func=print_func, keycolor=keycolor,
                        compress_lists=compress_lists,
                        round_floats=round_floats, _dlist=True)
                continue

        val_string_all = decode_to_str(value) if not no_values else ''
        for i, val_string in enumerate(val_string_all.split('\n')):
            if max_width is not None:
                if len(key_line) + 1 > max_width:
                    raise Exception(
                        'cannot fit keys and data within set max_width')
                # divide into chuncks and join by same indentation
                val_indent = ' ' * (initindent + key_length)
                n = max_width - len(val_indent)
                val_string = val_indent.join(
                    [s + '\n' for s in textwrap.wrap(val_string, n)])[:-1]

            if i == 0:
                print_func(key_line + val_string)
            else:
                print_func(new_line + val_string)

Response: Pretty print a nested dict in readable format


Instruction:
Given the following Python 3 function, write the documentation
def extract(d, path=None):
    path = [] if path is None else path

    d_new = copy.deepcopy(d)
    d_sub = d_new
    for key in path[:-1]:
        d_sub = d_sub[key]

    key = path[-1]
    d_extract = {key: d_sub[key]}
    d_sub.pop(key)

    return d_new, d_extract

Response: extract section of dictionary d


Instruction:
Write a Python 3 script for
indexing dictionary by multiple keys

Response:
def indexes(dic, keys=None):
    """ index dictionary by multiple keys

    Parameters
    ----------
    dic : dict
    keys : list

    Examples
    --------

    >>> d = {1:{"a":"A"},2:{"b":"B"}}
    >>> indexes(d,[1,'a'])
    'A'

    """
    keys = [] if keys is None else keys

    assert hasattr(dic, 'keys')
    new = dic.copy()
    old_key = None
    for key in keys:
        if not hasattr(new, 'keys'):
            raise KeyError('No indexes after: {}'.format(old_key))
        old_key = key
        new = new[key]
    return new


Instruction:
Write a Python 3 script for
getting nested dict as flat dict

Response:
def flatten(d, key_as_tuple=True, sep='.', list_of_dicts=None, all_iters=None):
    """ get nested dict as flat {key:val,...},
    where key is tuple/string of all nested keys

    Parameters
    ----------
    d : object
    key_as_tuple : bool
        whether keys are list of nested keys or delimited string of nested keys
    sep : str
        if key_as_tuple=False, delimiter for keys
    list_of_dicts: str or None
        if not None, flatten lists of dicts using this prefix
    all_iters: str or None
        if not None, flatten all lists and tuples using this prefix

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {1:{"a":"A"}, 2:{"b":"B"}}
    >>> pprint(flatten(d))
    {(1, 'a'): 'A', (2, 'b'): 'B'}

    >>> d = {1:{"a":"A"},2:{"b":"B"}}
    >>> pprint(flatten(d,key_as_tuple=False))
    {'1.a': 'A', '2.b': 'B'}

    >>> d = [{'a':1},{'b':[1, 2]}]
    >>> pprint(flatten(d,list_of_dicts='__list__'))
    {('__list__0', 'a'): 1, ('__list__1', 'b'): [1, 2]}

    >>> d = [{'a':1},{'b':[1, 2]}]
    >>> pprint(flatten(d,all_iters='__iter__'))
    {('__iter__0', 'a'): 1,
     ('__iter__1', 'b', '__iter__0'): 1,
     ('__iter__1', 'b', '__iter__1'): 2}

    """

    def expand(key, value):
        if is_dict_like(value):
            flatten_dict = flatten(value, key_as_tuple, sep,
                                   list_of_dicts, all_iters)
            if key_as_tuple:
                return [(key + k, v) for k, v in flatten_dict.items()]
            else:
                return [(str(key) + sep + k, v)
                        for k, v in flatten_dict.items()]
        elif is_iter_non_string(value) and all_iters is not None:
            value = {'{0}{1}'.format(all_iters, i): v
                     for i, v in enumerate(value)}
            flatten_dict = flatten(value, key_as_tuple, sep,
                                   list_of_dicts, all_iters)
            if key_as_tuple:
                return [(key + k, v) for k, v in flatten_dict.items()]
            else:
                return [(str(key) + sep + k, v)
                        for k, v in flatten_dict.items()]
        elif is_list_of_dict_like(value) and list_of_dicts is not None:
            value = {'{0}{1}'.format(list_of_dicts, i): v
                     for i, v in enumerate(value)}
            flatten_dict = flatten(value, key_as_tuple, sep,
                                   list_of_dicts, all_iters)
            if key_as_tuple:
                return [(key + k, v) for k, v in flatten_dict.items()]
            else:
                return [(str(key) + sep + k, v)
                        for k, v in flatten_dict.items()]
        else:
            return [(key, value)]

    if is_iter_non_string(d) and all_iters is not None:
        d = {'{0}{1}'.format(all_iters, i): v for i, v in enumerate(d)}
    elif is_list_of_dict_like(d) and list_of_dicts is not None:
        d = {'{0}{1}'.format(list_of_dicts, i): v for i, v in enumerate(d)}
    elif not is_dict_like(d):
        raise TypeError('d is not dict like: {}'.format(d))

    if key_as_tuple:
        items = [item for k, v in d.items() for item in expand((k,), v)]
    else:
        items = [item for k, v in d.items() for item in expand(k, v)]

    return dict(items)


Instruction:
Given the following Python 3 function, write the documentation
def _single_merge(a, b, error_path=None, overwrite=False,
                  append=False, list_of_dicts=False):
    """merges b into a
    """
    if error_path is None:
        error_path = []

    if list_of_dicts and is_list_of_dict_like(a) and is_list_of_dict_like(b):
        if len(a) != len(b):
            raise ValueError(
                'list of dicts are of different lengths at '
                '"{0}": old: {1}, new: {2}'.format('.'.join(error_path), a, b))
        return [_single_merge(a_item, b_item,
                              error_path + ["iter_{}".format(i)],
                              overwrite, append, list_of_dicts)
                for i, (a_item, b_item) in enumerate(zip(a, b))]

    for key in b:
        if key in a:
            if is_dict_like(a[key]) and is_dict_like(b[key]):
                _single_merge(a[key], b[key], error_path +
                              [str(key)], overwrite, append, list_of_dicts)
            elif (isinstance(a[key], list)
                  and isinstance(b[key], list) and append):
                a[key] += b[key]
            elif (list_of_dicts
                  and is_list_of_dict_like(a[key])
                  and is_list_of_dict_like(b[key])):
                if len(a[key]) != len(b[key]):
                    raise ValueError(
                        'list of dicts are of different lengths at '
                        '"{0}": old: {1}, new: {2}'.format(
                            '.'.join(error_path + [str(key)]), a[key], b[key]))
                for i, (a_item, b_item) in enumerate(zip(a[key], b[key])):
                    _single_merge(a_item, b_item,
                                  error_path + [str(key), "iter_{}".format(i)],
                                  overwrite, append, list_of_dicts)
            elif a[key] == b[key]:
                pass  # same leaf value
            elif overwrite:
                a[key] = b[key]
            else:
                raise ValueError(
                    'different data already exists at '
                    '"{0}": old: {1}, new: {2}'.format(
                        '.'.join(error_path + [str(key)]), a[key], b[key]))
        else:
            a[key] = b[key]
    return a

Response: merges a into b


Instruction:
Explain what the following Python 3 code does
def merge(dicts, overwrite=False, append=False, list_of_dicts=False):
    outdict = copy.deepcopy(dicts[0])

    def single_merge(a, b):
        return _single_merge(a, b, overwrite=overwrite, append=append,
                             list_of_dicts=list_of_dicts)

    reduce(single_merge, [outdict] + dicts[1:])

    return outdict

Response: Merge dicts into a single base base.


Instruction:
Explain what the following Python 3 code does
def flattennd(d, levels=0, key_as_tuple=True, delim='.',
              list_of_dicts=None):
    """ get nested dict as {key:dict,...},
    where key is tuple/string of all-n levels of nested keys

    Parameters
    ----------
    d : dict
    levels : int
        the number of levels to leave unflattened
    key_as_tuple : bool
        whether keys are list of nested keys or delimited string of nested keys
    delim : str
        if key_as_tuple=False, delimiter for keys
    list_of_dicts: str or None
        if not None, flatten lists of dicts using this prefix

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {1:{2:{3:{'b':'B','c':'C'},4:'D'}}}
    >>> pprint(flattennd(d,0))
    {(1, 2, 3, 'b'): 'B', (1, 2, 3, 'c'): 'C', (1, 2, 4): 'D'}

    >>> pprint(flattennd(d,1))
    {(1, 2): {4: 'D'}, (1, 2, 3): {'b': 'B', 'c': 'C'}}

    >>> pprint(flattennd(d,2))
    {(1,): {2: {4: 'D'}}, (1, 2): {3: {'b': 'B', 'c': 'C'}}}

    >>> pprint(flattennd(d,3))
    {(): {1: {2: {4: 'D'}}}, (1,): {2: {3: {'b': 'B', 'c': 'C'}}}}

    >>> pprint(flattennd(d,4))
    {(): {1: {2: {3: {'b': 'B', 'c': 'C'}, 4: 'D'}}}}

    >>> pprint(flattennd(d,5))
    {(): {1: {2: {3: {'b': 'B', 'c': 'C'}, 4: 'D'}}}}

    >>> pprint(flattennd(d,1,key_as_tuple=False,delim='.'))
    {'1.2': {4: 'D'}, '1.2.3': {'b': 'B', 'c': 'C'}}

    >>> test_dict = {"a":[{"b":[{"c":1, "d": 2}, {"e":3, "f": 4}]}, {"b":[{"c":5, "d": 6}, {"e":7, "f": 8}]}]}
    >>> pprint(flattennd(test_dict, list_of_dicts="__list__", levels=2))
    {('a', '__list__0', 'b'): [{'c': 1, 'd': 2}, {'e': 3, 'f': 4}],
     ('a', '__list__1', 'b'): [{'c': 5, 'd': 6}, {'e': 7, 'f': 8}]}

    >>> pprint(flattennd(test_dict, list_of_dicts="__list__", levels=3))
    {('a', '__list__0'): {'b': [{'c': 1, 'd': 2}, {'e': 3, 'f': 4}]},
     ('a', '__list__1'): {'b': [{'c': 5, 'd': 6}, {'e': 7, 'f': 8}]}}

    """  # noqa: E501
    if levels < 0:
        raise ValueError('unflattened levels must be greater than 0')

    new_d = {}
    flattened = flatten(d, True, delim, list_of_dicts=list_of_dicts)
    if levels == 0:
        return flattened

    for key, value in flattened.items():
        if key_as_tuple:
            new_key = key[: - (levels)]
        else:
            new_key = delim.join([str(k) for k in key[:-(levels)]])
        new_levels = key[-(levels):]

        # val_dict = {new_levels: value}
        # val_dict = unflatten(val_dict, True, delim)

        if new_key not in new_d:
            new_d[new_key] = {new_levels: value}
        else:
            if new_levels in new_d[new_key]:
                raise ValueError(
                    "key clash for: {0}; {1}".format(new_key, new_levels))
            new_d[new_key][new_levels] = value

    for nkey, nvalue in new_d.items():
        new_d[nkey] = unflatten(
            nvalue, list_of_dicts=list_of_dicts, deepcopy=False)

    return new_d

Response: get nested dict as dict where key is tuple or delimited string of all - n levels of nested keys or list of dicts with delimiter


Instruction:
Make a summary of the following Python 3 code
def flatten2d(d, key_as_tuple=True, delim='.',
              list_of_dicts=None):
    """ get nested dict as {key:dict,...},
    where key is tuple/string of all-1 nested keys

    NB: is same as flattennd(d,1,key_as_tuple,delim)

    Parameters
    ----------
    d : dict
    key_as_tuple : bool
        whether keys are list of nested keys or delimited string of nested keys
    delim : str
        if key_as_tuple=False, delimiter for keys
    list_of_dicts: str or None
        if not None, flatten lists of dicts using this prefix

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {1:{2:{3:{'b':'B','c':'C'},4:'D'}}}
    >>> pprint(flatten2d(d))
    {(1, 2): {4: 'D'}, (1, 2, 3): {'b': 'B', 'c': 'C'}}

    >>> pprint(flatten2d(d,key_as_tuple=False,delim=','))
    {'1,2': {4: 'D'}, '1,2,3': {'b': 'B', 'c': 'C'}}

    """
    return flattennd(d, 1, key_as_tuple, delim, list_of_dicts=list_of_dicts)

Response: get nested dict as dict where key is tuple or delimited string of all - 1 nested keys


Instruction:
Can you generate a brief explanation for the following Python 3 code
def remove_keys(d, keys=None, use_wildcards=True,
                list_of_dicts=False, deepcopy=True):
    """remove certain keys from nested dict, retaining preceeding paths

    Parameters
    ----------
    keys: list
    use_wildcards : bool
        if true, can use * (matches everything)
        and ? (matches any single character)
    list_of_dicts: bool
        treat list of dicts as additional branches
    deepcopy: bool

    Examples
    --------

    >>> from pprint import pprint
    >>> d = {1:{"a":"A"},"a":{"b":"B"}}
    >>> pprint(remove_keys(d,['a']))
    {1: 'A', 'b': 'B'}

    >>> pprint(remove_keys({'abc':1},['a*'],use_wildcards=False))
    {'abc': 1}
    >>> pprint(remove_keys({'abc':1},['a*'],use_wildcards=True))
    {}

    """
    keys = [] if keys is None else keys
    list_of_dicts = '__list__' if list_of_dicts else None

    def is_in(a, bs):
        if use_wildcards:
            for b in bs:
                try:
                    if a == b:
                        return True
                    if fnmatch(a, b):
                        return True
                except Exception:
                    pass
            return False
        else:
            try:
                return a in bs
            except Exception:
                return False

    if not hasattr(d, 'items'):
        return d
    else:
        dic = flatten(d, list_of_dicts=list_of_dicts)
        new_dic = {}
        for key, value in dic.items():
            new_key = tuple([i for i in key if not is_in(i, keys)])
            if not new_key:
                continue
            try:
                if new_key[-1].startswith(list_of_dicts):
                    continue
            except Exception:
                pass
            new_dic[new_key] = value
        return unflatten(
            new_dic, list_of_dicts=list_of_dicts, deepcopy=deepcopy)

Response: remove certain keys from nested dict retaining preceeding paths


Instruction:
Make a summary of the following Python 3 code
def remove_keyvals(d, keyvals=None, list_of_dicts=False, deepcopy=True):
    keyvals = [] if keyvals is None else keyvals
    list_of_dicts = '__list__' if list_of_dicts else None

    if hasattr(keyvals, 'items'):
        keyvals = [(k, v) for k, v in keyvals.items()]

    if not hasattr(d, 'items'):
        return d

    flatd = flatten(d, list_of_dicts=list_of_dicts)

    def is_in(a, b):
        try:
            return a in b
        except Exception:
            return False

    prune = [k[0] for k, v in flatd.items() if is_in((k[-1], v), keyvals)]
    flatd = {k: v for k, v in flatd.items() if not is_in(k[0], prune)}

    return unflatten(flatd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)

Response:
remove paths with at least one branch leading
    to certain keyvals from dict d


Instruction:
Can you create a Python 3 function that
removes paths containing certain keys from dict d

Response:
def remove_paths(d, keys, list_of_dicts=False, deepcopy=True):
    """ remove paths containing certain keys from dict

    Parameters
    ----------
    d: dict
    keys : list
        list of keys to find and remove path
    list_of_dicts: bool
        treat list of dicts as additional branches
    deepcopy: bool
        deepcopy values

    Examples
    --------

    >>> from pprint import pprint
    >>> d = {1:{"a":"A"},2:{"b":"B"},4:{5:{6:'a',7:'b'}}}
    >>> pprint(remove_paths(d,[6,'a']))
    {2: {'b': 'B'}, 4: {5: {7: 'b'}}}

    >>> d = {1:{2: 3}, 1:{4: 5}}
    >>> pprint(remove_paths(d,[(1, 2)]))
    {1: {4: 5}}

    >>> d2 = {'a':[{'b':1,'c':{'b':3}},{'b':1,'c':2}]}
    >>> pprint(remove_paths(d2,["b"],list_of_dicts=False))
    {'a': [{'b': 1, 'c': {'b': 3}}, {'b': 1, 'c': 2}]}

    >>> pprint(remove_paths(d2,["b"],list_of_dicts=True))
    {'a': [{'c': 2}]}

    """
    keys = [(key,) if not isinstance(key, tuple) else key for key in keys]
    list_of_dicts = '__list__' if list_of_dicts else None

    def contains(path):
        for k in keys:
            if set(k).issubset(path):
                return True
        return False

    flatd = flatten(d, list_of_dicts=list_of_dicts)

    flatd = {path: v for path, v in flatd.items() if not contains(path)}

    return unflatten(flatd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)


Instruction:
Implement a Python 3 function for
filtering leaf nodes of nested dictionary d by values vals

Response:
def filter_values(d, vals=None, list_of_dicts=False, deepcopy=True):
    """ filters leaf nodes of nested dictionary

    Parameters
    ----------
    d : dict
    vals : list
        values to filter by
    list_of_dicts: bool
        treat list of dicts as additional branches
    deepcopy: bool
        deepcopy values

    Examples
    --------

    >>> d = {1:{"a":"A"},2:{"b":"B"},4:{5:{6:'a'}}}
    >>> filter_values(d,['a'])
    {4: {5: {6: 'a'}}}

    """
    vals = [] if vals is None else vals
    list_of_dicts = '__list__' if list_of_dicts else None

    flatd = flatten(d, list_of_dicts=list_of_dicts)

    def is_in(a, b):
        try:
            return a in b
        except Exception:
            return False

    flatd = {k: v for k, v in flatd.items() if is_in(v, vals)}
    return unflatten(flatd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)


Instruction:
Here you have a function in Python 3, explain what it does
def filter_keyvals(d, keyvals, logic="OR", keep_siblings=False,
                   list_of_dicts=False, deepcopy=True):
    """ filters leaf nodes key:value pairs of nested dictionary

    Parameters
    ----------
    d : dict
    keyvals : dict or list[tuple]
        (key,value) pairs to filter by
    logic : str
        "OR" or "AND" for matching pairs
    keep_siblings : bool
        keep all sibling paths
    list_of_dicts : bool
        treat list of dicts as additional branches
    deepcopy: bool
        deepcopy values

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {1:{6:'a'},3:{7:'a'},2:{6:"b"},4:{5:{6:'a'}}}
    >>> pprint(filter_keyvals(d,[(6,'a')]))
    {1: {6: 'a'}, 4: {5: {6: 'a'}}}

    >>> d2 = {'a':{'b':1,'c':2,'d':3}, 'e':4}

    >>> pprint(filter_keyvals(d2, {'b': 1, 'e': 4}, logic="OR", keep_siblings=False))
    {'a': {'b': 1}, 'e': 4}

    >>> pprint(filter_keyvals(d2,[('b',1)], logic="OR", keep_siblings=True))
    {'a': {'b': 1, 'c': 2, 'd': 3}}

    >>> pprint(filter_keyvals(d2, {'b': 1, 'e': 4}, logic="AND", keep_siblings=False))
    {}

    >>> pprint(filter_keyvals(d2, {'b': 1, 'c': 2}, logic="AND", keep_siblings=False))
    {'a': {'b': 1, 'c': 2}}

    >>> pprint(filter_keyvals(d2,[('b',1), ('c',2)], logic="AND", keep_siblings=True))
    {'a': {'b': 1, 'c': 2, 'd': 3}}

    >>> d3 = {"a": {"b": 1, "f": {"d": 3}}, "e": {"b": 1, "c": 2, "f": {"d": 3}}, "g": 5}
    >>> pprint(filter_keyvals(d3,[('b',1), ('c', 2)], logic="OR", keep_siblings=True))
    {'a': {'b': 1, 'f': {'d': 3}}, 'e': {'b': 1, 'c': 2, 'f': {'d': 3}}}

    >>> pprint(filter_keyvals(d3,[('b',1), ('c', 2)], logic="AND", keep_siblings=True))
    {'e': {'b': 1, 'c': 2, 'f': {'d': 3}}}

    """  # noqa: E501
    if len(keyvals) != len(dict(keyvals)):
        raise ValueError("repeating keys in keyvals: {}".format(keyvals))

    keyvals = dict(keyvals)
    list_of_dicts = '__list__' if list_of_dicts else None

    flattened = flatten(d, list_of_dicts=list_of_dicts)

    if logic == "OR":
        if keep_siblings:
            pruned = {
                tuple(k[:-1]) for k, v in flattened.items()
                if any(key == k[-1] and v == keyvals[key] for key in keyvals)}
            filtered = {k: v for k, v in flattened.items()
                        if _in_pruned(k, pruned)}
        else:
            filtered = {
                k: v for k, v in flattened.items()
                if any(key == k[-1] and v == keyvals[key] for key in keyvals)}
    elif logic == "AND":
        pruned = {}
        for k, v in flattened.items():
            if any(key == k[-1] and v == keyvals[key] for key in keyvals):
                pruned[tuple(k[:-1])] = pruned.get(tuple(k[:-1]), []) + [k[-1]]
        all_keys = set(keyvals.keys())
        pruned = [k for k, v in pruned.items() if set(v) == all_keys]

        if keep_siblings:
            filtered = {k: v for k, v in flattened.items()
                        if _in_pruned(k, pruned)}
        else:
            filtered = {k: v for k, v in flattened.items(
            ) if k[-1] in all_keys and _in_pruned(k, pruned)}
    else:
        raise ValueError("logic must be AND or OR: {}".format(logic))

    return unflatten(filtered, list_of_dicts=list_of_dicts, deepcopy=deepcopy)

Response: Filters the nested dictionary d by the given keyvals.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def filter_keys(d, keys, use_wildcards=False,
                list_of_dicts=False, deepcopy=True):
    """ filter dict by certain keys

    Parameters
    ----------
    d : dict
    keys: list
    use_wildcards : bool
        if true, can use * (matches everything)
        and ? (matches any single character)
    list_of_dicts: bool
        treat list of dicts as additional branches
    deepcopy: bool
        deepcopy values

    Examples
    --------

    >>> from pprint import pprint

    >>> d = {1:{"a":"A"},2:{"b":"B"},4:{5:{6:'a',7:'b'}}}
    >>> pprint(filter_keys(d,['a',6]))
    {1: {'a': 'A'}, 4: {5: {6: 'a'}}}

    >>> d = {1:{"axxxx":"A"},2:{"b":"B"}}
    >>> pprint(filter_keys(d,['a*'],use_wildcards=True))
    {1: {'axxxx': 'A'}}

    """
    list_of_dicts = '__list__' if list_of_dicts else None
    flatd = flatten(d, list_of_dicts=list_of_dicts)

    def is_in(a, bs):
        if use_wildcards:
            for b in bs:
                try:
                    if a == b:
                        return True
                    if fnmatch(b, a):
                        return True
                except Exception:
                    pass
            return False
        else:
            try:
                return a in bs
            except Exception:
                return False

    flatd = {paths: v for paths, v in flatd.items() if any(
        [is_in(k, paths) for k in keys])}
    return unflatten(flatd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)

Response: filter dict by certain keys


Instruction:
Can you generate the documentation for the following Python 3 function
def filter_paths(d, paths, list_of_dicts=False, deepcopy=True):
    list_of_dicts = '__list__' if list_of_dicts else None

    all_keys = [x for y in paths if isinstance(y, tuple) for x in y]
    all_keys += [x for x in paths if not isinstance(x, tuple)]
    # faster to filter first I think
    new_d = filter_keys(d, all_keys, list_of_dicts=list_of_dicts)
    new_d = flatten(d, list_of_dicts=list_of_dicts)
    for key in list(new_d.keys()):
        if not any([
                set(key).issuperset(path if isinstance(path, tuple) else[path])
                for path in paths]):
            new_d.pop(key)
    return unflatten(new_d, list_of_dicts=list_of_dicts, deepcopy=deepcopy)

Response: filter dict by certain paths containing key sets


Instruction:
Create a Python 3 function for
renaming keys in dict

Response:
def rename_keys(d, keymap=None, list_of_dicts=False, deepcopy=True):
    """ rename keys in dict

    Parameters
    ----------
    d : dict
    keymap : dict
        dictionary of key name mappings
    list_of_dicts: bool
        treat list of dicts as additional branches
    deepcopy: bool
        deepcopy values

    Examples
    --------

    >>> from pprint import pprint
    >>> d = {'a':{'old_name':1}}
    >>> pprint(rename_keys(d,{'old_name':'new_name'}))
    {'a': {'new_name': 1}}

    """
    list_of_dicts = '__list__' if list_of_dicts else None
    keymap = {} if keymap is None else keymap

    flatd = flatten(d, list_of_dicts=list_of_dicts)

    flatd = {
        tuple([keymap.get(k, k) for k in path]): v for path, v in flatd.items()
    }

    return unflatten(flatd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)


Instruction:
How would you explain what the following Python 3 function does
def split_key(d, key, new_keys, before=True,
              list_of_dicts=False, deepcopy=True):
    """ split an existing key(s) into multiple levels

    Parameters
    ----------
    d : dict
        or dict like
    key: str
        existing key value
    new_keys: list[str]
        new levels to add
    before: bool
        add level before existing key (else after)
    list_of_dicts: bool
        treat list of dicts as additional branches

    Examples
    --------
    >>> from pprint import pprint
    >>> d = {'a':1,'b':2}
    >>> pprint(split_key(d,'a',['c','d']))
    {'b': 2, 'c': {'d': {'a': 1}}}

    >>> pprint(split_key(d,'a',['c','d'],before=False))
    {'a': {'c': {'d': 1}}, 'b': 2}

    >>> d2 = [{'a':1},{'a':2},{'a':3}]
    >>> pprint(split_key(d2,'a',['b'],list_of_dicts=True))
    [{'b': {'a': 1}}, {'b': {'a': 2}}, {'b': {'a': 3}}]

    """
    list_of_dicts = '__list__' if list_of_dicts else None
    flatd = flatten(d, list_of_dicts=list_of_dicts)

    newd = {}
    for path, v in flatd.items():
        if key in path:
            newk = []
            for k in path:
                if k == key:
                    if before:
                        newk = newk + new_keys + [k]
                    else:
                        newk = newk + [k] + new_keys
                else:
                    newk.append(k)
            newd[tuple(newk)] = v
        else:
            newd[path] = v

    return unflatten(newd, list_of_dicts=list_of_dicts, deepcopy=deepcopy)

Response: split an existing key into multiple levels