Instruction:
How would you implement a function in Python 3 that
decodes the stanza subelements.

Response:
def _decode_subelements(self):
        """Decode the stanza subelements."""
        for child in self._element:
            if child.tag == self._show_tag:
                self._show = child.text
            elif child.tag == self._status_tag:
                self._status = child.text
            elif child.tag == self._priority_tag:
                try:
                    self._priority = int(child.text.strip())
                    if self._priority < -128 or self._priority > 127:
                        raise ValueError
                except ValueError:
                    raise BadRequestProtocolError(
                                            "Presence priority not an integer")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def as_xml(self):
        result = Stanza.as_xml(self)
        if self._show:
            child = ElementTree.SubElement(result, self._show_tag)
            child.text = self._show
        if self._status:
            child = ElementTree.SubElement(result, self._status_tag)
            child.text = self._status
        if self._priority:
            child = ElementTree.SubElement(result, self._priority_tag)
            child.text = unicode(self._priority)
        return result

Response: Return the XML stanza representation.


Instruction:
Explain what the following Python 3 code does
def copy(self):
        result = Presence(None, self.from_jid, self.to_jid,
                        self.stanza_type, self.stanza_id, self.error,
                        self._return_path(),
                        self._show, self._status, self._priority)
        if self._payload is None:
            self.decode_payload()
        for payload in self._payload:
            result.add_payload(payload.copy())
        return result

Response: Create a deep copy of the stanza.


Instruction:
Can you generate the documentation for the following Python 3 function
def make_accept_response(self):
        if self.stanza_type not in ("subscribe", "subscribed",
                                                "unsubscribe", "unsubscribed"):
            raise ValueError("Results may only be generated for 'subscribe',"
                "'subscribed','unsubscribe' or 'unsubscribed' presence")
        stanza = Presence(stanza_type = ACCEPT_RESPONSES[self.stanza_type],
                            from_jid = self.to_jid, to_jid = self.from_jid,
                                                    stanza_id = self.stanza_id)
        return stanza

Response: Create an accept response stanza.


Instruction:
How would you implement a function in Python 3 that
creates a deny response for the current presence stanza.

Response:
def make_deny_response(self):
        """Create "deny" response for the "subscribe" / "subscribed" /
        "unsubscribe" / "unsubscribed" presence stanza.

        :return: new presence stanza.
        :returntype: `Presence`
        """
        if self.stanza_type not in ("subscribe", "subscribed",
                                                "unsubscribe", "unsubscribed"):
            raise ValueError("Results may only be generated for 'subscribe',"
                "'subscribed','unsubscribe' or 'unsubscribed' presence")
        stanza = Presence(stanza_type = DENY_RESPONSES[self.stanza_type],
                            from_jid = self.to_jid, to_jid = self.from_jid,
                                                    stanza_id = self.stanza_id)
        return stanza


Instruction:
Can you tell what is the following Python 3 function doing
def make_error_response(self, cond):

        if self.stanza_type == "error":
            raise ValueError("Errors may not be generated in response"
                                                                " to errors")

        stanza = Presence(stanza_type = "error", from_jid = self.from_jid,
                            to_jid = self.to_jid, stanza_id = self.stanza_id,
                            status = self._status, show = self._show,
                            priority = self._priority, error_cond = cond)

        if self._payload is None:
            self.decode_payload()

        for payload in self._payload:
            stanza.add_payload(payload)

        return stanza

Response: Create an error response for the any non - error presence stanza.


Instruction:
Write a Python 3 script to
activate an existing billing plan in a CREATED state.

Response:
def activate(self):
		"""
		Activate an plan in a CREATED state.
		"""
		obj = self.find_paypal_object()
		if obj.state == enums.BillingPlanState.CREATED:
			success = obj.activate()
			if not success:
				raise PaypalApiError("Failed to activate plan: %r" % (obj.error))
		# Resync the updated data to the database
		self.get_or_update_from_api_data(obj, always_sync=True)
		return obj


Instruction:
Make a summary of the following Python 3 code
def execute(self):
		# Save the execution time first.
		# If execute() fails, executed_at will be set, with no executed_agreement set.
		self.executed_at = now()
		self.save()

		with transaction.atomic():
			ret = BillingAgreement.execute(self.id)
			ret.user = self.user
			ret.save()
			self.executed_agreement = ret
			self.save()

		return ret

Response:
Execute the PreparedBillingAgreement by creating and executing a
	 matching BillingAgreement.


Instruction:
Here you have a function in Python 3, explain what it does
def from_request(cls, request, webhook_id=PAYPAL_WEBHOOK_ID):

		headers = fix_django_headers(request.META)
		assert headers
		try:
			body = request.body.decode(request.encoding or "utf-8")
		except Exception:
			body = "(error decoding body)"

		ip = request.META["REMOTE_ADDR"]
		obj = cls.objects.create(headers=headers, body=body, remote_ip=ip)

		try:
			obj.valid = obj.verify(PAYPAL_WEBHOOK_ID)
			if obj.valid:
				# Process the item (do not save it, it'll get saved below)
				obj.process(save=False)
		except Exception as e:
			max_length = WebhookEventTrigger._meta.get_field("exception").max_length
			obj.exception = str(e)[:max_length]
			obj.traceback = format_exc()
		finally:
			obj.save()

		return obj

Response: Create a WebhookEventTrigger object from a Django request object.


Instruction:
Create a Python 3 function to
run the command line interface.

Response:
def run(*args):
	"""
	Check and/or create Django migrations.

	If --check is present in the arguments then migrations are checked only.
	"""
	if not settings.configured:
		settings.configure(**DEFAULT_SETTINGS)

	django.setup()

	parent = os.path.dirname(os.path.abspath(__file__))
	sys.path.insert(0, parent)

	if "--check" in args:
		check_migrations()
	else:
		django.core.management.call_command("makemigrations", APP_NAME, *args)


Instruction:
Explain what the following Python 3 code does
def check_paypal_api_key(app_configs=None, **kwargs):
	messages = []

	mode = getattr(djpaypal_settings, "PAYPAL_MODE", None)
	if mode not in VALID_MODES:
		msg = "Invalid PAYPAL_MODE specified: {}.".format(repr(mode))
		hint = "PAYPAL_MODE must be one of {}".format(", ".join(repr(k) for k in VALID_MODES))
		messages.append(checks.Critical(msg, hint=hint, id="djpaypal.C001"))

	for setting in "PAYPAL_CLIENT_ID", "PAYPAL_CLIENT_SECRET":
		if not getattr(djpaypal_settings, setting, None):
			msg = "Invalid value specified for {}".format(setting)
			hint = "Add PAYPAL_CLIENT_ID and PAYPAL_CLIENT_SECRET to your settings."
			messages.append(checks.Critical(msg, hint=hint, id="djpaypal.C002"))

	return messages

Response: Check that the Paypal API keys are configured correctly.


Instruction:
Write a Python 3 script for
creating the upstream applications.

Response:
async def _create_upstream_applications(self):
        """
        Create the upstream applications.
        """
        loop = asyncio.get_event_loop()
        for steam_name, ApplicationsCls in self.applications.items():
            application = ApplicationsCls(self.scope)
            upstream_queue = asyncio.Queue()
            self.application_streams[steam_name] = upstream_queue
            self.application_futures[steam_name] = loop.create_task(
                application(
                    upstream_queue.get,
                    partial(self.dispatch_downstream, steam_name=steam_name)
                )
            )


Instruction:
Given the following Python 3 function, write the documentation
async def send_upstream(self, message, stream_name=None):
        if stream_name is None:
            for steam_queue in self.application_streams.values():
                await steam_queue.put(message)
            return
        steam_queue = self.application_streams.get(stream_name)
        if steam_queue is None:
            raise ValueError("Invalid multiplexed frame received (stream not mapped)")
        await steam_queue.put(message)

Response: Send a message upstream to a de - multiplexed application.


Instruction:
Make a summary of the following Python 3 code
async def dispatch_downstream(self, message, steam_name):
        handler = getattr(self, get_handler_name(message), None)
        if handler:
            await handler(message, stream_name=steam_name)
        else:
            # if there is not handler then just pass the message further downstream.
            await self.base_send(message)

Response: Dispatch a message to the downstream.


Instruction:
How would you explain what the following Python 3 function does
async def receive_json(self, content, **kwargs):
        # Check the frame looks good
        if isinstance(content, dict) and "stream" in content and "payload" in content:
            # Match it to a channel
            steam_name = content["stream"]
            payload = content["payload"]
            # block upstream frames
            if steam_name not in self.applications_accepting_frames:
                raise ValueError("Invalid multiplexed frame received (stream not mapped)")
            # send it on to the application that handles this stream
            await self.send_upstream(
                message={
                    "type": "websocket.receive",
                    "text": await self.encode_json(payload)
                },
                stream_name=steam_name
            )
            return
        else:
            raise ValueError("Invalid multiplexed **frame received (no channel/payload key)")

Response: Rout the message down the correct stream.


Instruction:
How would you code a function in Python 3 to
handle the disconnect message. This is a no - op if the connection is closed.

Response:
async def websocket_disconnect(self, message):
        """
        Handle the disconnect message.

        This is propagated to all upstream applications.
        """
        # set this flag so as to ensure we don't send a downstream `websocket.close` message due to all
        # child applications closing.
        self.closing = True
        # inform all children
        await self.send_upstream(message)
        await super().websocket_disconnect(message)


Instruction:
Make a summary of the following Python 3 code
async def disconnect(self, code):
        try:
            await asyncio.wait(
                self.application_futures.values(),
                return_when=asyncio.ALL_COMPLETED,
                timeout=self.application_close_timeout
            )
        except asyncio.TimeoutError:
            pass

Response: Disconnects the child application.


Instruction:
Explain what the following Python 3 code does
async def websocket_send(self, message, stream_name):
        text = message.get("text")
        # todo what to do on binary!
        json = await self.decode_json(text)
        data = {
            "stream": stream_name,
            "payload": json
        }
        await self.send_json(data)

Response: Capture downstream websocket. send messages from the upstream applications.


Instruction:
Explain what the following Python 3 code does
async def websocket_accept(self, message, stream_name):
        is_first = not self.applications_accepting_frames
        self.applications_accepting_frames.add(stream_name)
        # accept the connection after the first upstream application accepts.
        if is_first:
            await self.accept()

Response: Intercept downstream websocket. accept message.


Instruction:
Make a summary of the following Python 3 code
async def websocket_close(self, message, stream_name):
        if stream_name in self.applications_accepting_frames:
            # remove from set of upsteams steams than can receive new messages
            self.applications_accepting_frames.remove(stream_name)

        # we are already closing due to an upstream websocket.disconnect command

        if self.closing:
            return
        # if none of the upstream applications are listing we need to close.
        if not self.applications_accepting_frames:
            await self.close(message.get("code"))

Response: Handle downstream websocket. close message.


Instruction:
How would you code a function in Python 3 to
update interpolation data :param list(float) x_list: x values :param list(float) y_list: y values

Response:
def update(self, x_list=list(), y_list=list()):
        """
        update interpolation data
        :param list(float) x_list: x values
        :param list(float) y_list: y values
        """
        if not y_list:
            for x in x_list:
                if x in self.x_list:
                    i = self.x_list.index(float(x))
                    self.x_list.pop(i)
                    self.y_list.pop(i)
        else:
            x_list = map(float, x_list)
            y_list = map(float, y_list)
            data = [(x, y) for x, y in zip(self.x_list, self.y_list) if x not in x_list]
            data.extend(zip(x_list, y_list))
            data = sorted(data)
            self.x_list = [float(x) for (x, y) in data]
            self.y_list = [float(y) for (x, y) in data]


Instruction:
Create a Python 3 function to
return the interval of the interpolation in which x lies.

Response:
def get_interval(x, intervals):
        """
        finds interval of the interpolation in which x lies.
        :param x:
        :param intervals: the interpolation intervals
        :return:
        """
        n = len(intervals)
        if n < 2:
            return intervals[0]
        n2 = n / 2
        if x < intervals[n2][0]:
            return spline.get_interval(x, intervals[:n2])
        else:
            return spline.get_interval(x, intervals[n2:])


Instruction:
How would you implement a function in Python 3 that
sets the interpolation coefficients for the single polynomials of the spline.

Response:
def set_interpolation_coefficients(self):
        """
        computes the coefficients for the single polynomials of the spline.
        """

        left_boundary_slope = 0
        right_boundary_slope = 0

        if isinstance(self.boundary_condition, tuple):
            left_boundary_slope = self.boundary_condition[0]
            right_boundary_slope = self.boundary_condition[1]
        elif self.boundary_condition is None:
            pass
        else:
            msg = 'The given object {} of type {} is not a valid condition ' \
                  'for the border'.format(self.boundary_condition, type(self.boundary_condition))
            raise ValueError(msg)

        # getting the values such that we get a continuous second derivative
        # by solving a system of linear equations

        # setup the matrix
        n = len(self.x_list)
        mat = numpy.zeros((n, n))
        b = numpy.zeros((n, 1))
        x = self.x_list
        y = self.y_list

        if n > 2:
            for i in range(1, n - 1):
                mat[i, i - 1] = 1.0 / (x[i] - x[i - 1])
                mat[i, i + 1] = 1.0 / (x[i + 1] - x[i])
                mat[i, i] = 2 * (mat[i, i - 1] + mat[i, i + 1])

                b[i, 0] = 3 * ((y[i] - y[i - 1]) / (x[i] - x[i - 1]) ** 2
                               + (y[i + 1] - y[i]) / (x[i + 1] - x[i]) ** 2)
        elif n < 2:
            raise ValueError('too less points for interpolation')

        if self.boundary_condition is None:  # not a knot
            mat[0, 0] = 1.0 / (x[1] - x[0]) ** 2
            mat[0, 2] = -1.0 / (x[2] - x[1]) ** 2
            mat[0, 1] = mat[0, 0] + mat[0, 2]

            b[0, 0] = 2.0 * ((y[1] - y[0]) / (x[1] - x[0]) ** 3
                             - (y[2] - y[1]) / (x[2] - x[1]) ** 3)

            mat[n - 1, n - 3] = 1.0 / (x[n - 2] - x[n - 3]) ** 2
            mat[n - 1, n - 1] = -1.0 / (x[n - 1] - x[n - 2]) ** 2
            mat[n - 1, n - 2] = mat[n - 1, n - 3] + mat[n - 1, n - 1]

            b[n - 1, 0] = 2.0 * ((y[n - 2] - y[n - 3]) / (x[n - 2] - x[n - 3]) ** 3
                                 - (y[n - 1] - y[n - 2]) / (x[n - 1] - x[n - 2]) ** 3)
        else:
            mat[0, 0] = 2.0 / (x[1] - x[0])
            mat[0, 1] = 1.0 / (x[1] - x[0])

            b[0, 0] = 3 * (y[1] - y[0]) / (x[1] - x[0]) ** 2 - 0.5 * left_boundary_slope

            mat[n - 1, n - 2] = 1.0 / (x[n - 1] - x[n - 2])
            mat[n - 1, n - 1] = 2.0 / (x[n - 1] - x[n - 2])

            b[n - 1, 0] = 3 * (y[n - 1] - y[n - 2]) / (x[n - 1] - x[n - 2]) ** 2 + 0.5 * right_boundary_slope

        k = numpy.linalg.solve(mat, b)

        for i in range(1, n):
            c1 = k[i - 1, 0] * (x[i] - x[i - 1]) - (y[i] - y[i - 1])
            c2 = -k[i, 0] * (x[i] - x[i - 1]) + (y[i] - y[i - 1])
            self.interpolation_coefficients.append([c1, c2])


Instruction:
Here you have a function in Python 3, explain what it does
def cast(cls, fx_spot, domestic_curve=None, foreign_curve=None):
        assert domestic_curve.origin == foreign_curve.origin
        return cls(fx_spot, domestic_curve=domestic_curve, foreign_curve=foreign_curve)

Response: Create a new FxCurve object from fx spot rate and domestic discount curve and foreign discount curve.


Instruction:
Write a Python 3 script to
add contents to self.

Response:
def add(self, foreign_currency, foreign_curve=None, fx_spot=1.0):
        """
        adds contents to FxShelf.
        If curve is FxCurve or FxDict, spot should turn curve.currency into self.currency,
        else spot should turn currency into self.currency by
        N in EUR * spot = N in USD for currency = EUR and self.currency = USD
        """
        assert isinstance(foreign_currency, type(self.currency))
        assert isinstance(foreign_curve, curve.RateCurve)
        assert isinstance(fx_spot, float)

        # create missing FxCurves
        self[self.currency, foreign_currency] = FxCurve.cast(fx_spot, self.domestic_curve, foreign_curve)
        self[foreign_currency, self.currency] = FxCurve.cast(1 / fx_spot, foreign_curve, self.domestic_curve)
        # update relevant FxCurves
        f = foreign_currency
        new = dict()
        for d, s in self:
            if s is self.currency and d is not foreign_currency:
                triangulated = self[d, s](self.domestic_curve.origin) * fx_spot
                if (d, f) in self:
                    self[d, f].foreign_curve = foreign_curve
                    self[d, f].fx_spot = triangulated
                    self[f, d].domestic_curve = foreign_curve
                    self[f, d].fx_spot = 1 / triangulated
                else:
                    new[d, f] = FxCurve.cast(triangulated, self[d, s].domestic_curve, foreign_curve)
                    new[f, d] = FxCurve.cast(1 / triangulated, foreign_curve, self[d, s].domestic_curve)
        self.update(new)


Instruction:
How would you explain what the following Python 3 function does
def _frange(start, stop=None, step=None):
    if stop is None:
        stop = start
        start = 0.0
    if step is None:
        step = 1.0
    r = start
    while r < stop:
        yield r
        r += step

Response: _frange like function for float inputs


Instruction:
Can you generate a brief explanation for the following Python 3 code
def interest_accrued(self, valuation_date):
        return sum([l.interest_accrued(valuation_date) for l in self.legs if hasattr(l, 'interest_accrued')])

Response: Gets the number of interest accrued for a given date.


Instruction:
Make a summary of the following Python 3 code
def get_secrets(prefixes, relative_paths):
    try:
        return os.path.join(sys._MEIPASS, relative_paths[-1])
    except Exception:
        for prefix in prefixes:
            for relative_path in relative_paths:
                path = os.path.join(prefix, relative_path)
                if os.path.exists(path):
                    return path
        else:
            return None

Response: Get the first existing file of relative_path in prefixes directories.


Instruction:
Given the following Python 3 function, write the documentation
def multiglob_compile(globs, prefix=False):
    if not globs:
        # An empty globs list should only match empty strings
        return re.compile('^$')
    elif prefix:
        globs = [x + '*' for x in globs]
    return re.compile('|'.join(fnmatch.translate(x) for x in globs))

Response: Generates a single A or B or C regex from a list of shell globs.


Instruction:
Can you implement a function in Python 3 that
generates a SHA1 hash from a potentially long file.

Response:
def hashFile(handle, want_hex=False, limit=None, chunk_size=CHUNK_SIZE):
    """Generate a hash from a potentially long file.
    Digesting will obey :const:`CHUNK_SIZE` to conserve memory.

    :param handle: A file-like object or path to hash from.
    :param want_hex: If ``True``, returned hash will be hex-encoded.
    :type want_hex: :class:`~__builtins__.bool`

    :param limit: Maximum number of bytes to read (rounded up to a multiple of
        ``CHUNK_SIZE``)
    :type limit: :class:`~__builtins__.int`

    :param chunk_size: Size of :meth:`~__builtins__.file.read` operations
        in bytes.
    :type chunk_size: :class:`~__builtins__.int`

    :rtype: :class:`~__builtins__.str`
    :returns: A binary or hex-encoded SHA1 hash.

    .. note:: It is your responsibility to close any file-like objects you pass
        in
    """
    fhash, read = hashlib.sha1(), 0
    if isinstance(handle, basestring):
        handle = file(handle, 'rb')

    if limit:
        chunk_size = min(chunk_size, limit)

    # Chunked digest generation (conserve memory)
    for block in iter(lambda: handle.read(chunk_size), ''):
        fhash.update(block)
        read += chunk_size
        if 0 < limit <= read:
            break

    return want_hex and fhash.hexdigest() or fhash.digest()


Instruction:
Here you have a function in Python 3, explain what it does
def getPaths(roots, ignores=None):
    paths, count, ignores = [], 0, ignores or []

    # Prepare the ignores list for most efficient use
    ignore_re = multiglob_compile(ignores, prefix=False)

    for root in roots:
        # For safety, only use absolute, real paths.
        root = os.path.realpath(root)

        # Handle directly-referenced filenames properly
        # (And override ignores to "do as I mean, not as I say")
        if os.path.isfile(root):
            paths.append(root)
            continue

        for fldr in os.walk(root):
            out.write("Gathering file paths to compare... (%d files examined)"
                      % count)

            # Don't even descend into IGNOREd directories.
            for subdir in fldr[1]:
                dirpath = os.path.join(fldr[0], subdir)
                if ignore_re.match(dirpath):
                    fldr[1].remove(subdir)

            for filename in fldr[2]:
                filepath = os.path.join(fldr[0], filename)
                if ignore_re.match(filepath):
                    continue  # Skip IGNOREd files.

                paths.append(filepath)
                count += 1

    out.write("Found %s files to be compared for duplication." % (len(paths)),
              newline=True)
    return paths

Response: Recursively walk a set of paths and return a listing of contained files.


Instruction:
Implement a function in Python 3 to
group a set of paths according to a function.

Response:
def groupBy(groups_in, classifier, fun_desc='?', keep_uniques=False,
            *args, **kwargs):
    """Subdivide groups of paths according to a function.

    :param groups_in: Grouped sets of paths.
    :type groups_in: :class:`~__builtins__.dict` of iterables

    :param classifier: Function to group a list of paths by some attribute.
    :type classifier: ``function(list, *args, **kwargs) -> str``

    :param fun_desc: Human-readable term for what the classifier operates on.
        (Used in log messages)
    :type fun_desc: :class:`~__builtins__.str`

    :param keep_uniques: If ``False``, discard groups with only one member.
    :type keep_uniques: :class:`~__builtins__.bool`

    :returns: A dict mapping classifier keys to groups of matches.
    :rtype: :class:`~__builtins__.dict`

    :attention: Grouping functions generally use a :class:`~__builtins__.set`
        ``groups`` as extra protection against accidentally counting a given
        file twice. (Complimentary to use of :func:`os.path.realpath` in
        :func:`~fastdupes.getPaths`)

    .. todo:: Find some way to bring back the file-by-file status text
    """
    groups, count, group_count = {}, 0, len(groups_in)
    for pos, paths in enumerate(groups_in.values()):
        out.write("Subdividing group %d of %d by %s... (%d files examined, %d "
                  "in current group)" % (
                      pos + 1, group_count, fun_desc, count, len(paths)
                  ))

        for key, group in classifier(paths, *args, **kwargs).items():
            groups.setdefault(key, set()).update(group)
            count += len(group)

    if not keep_uniques:
        # Return only the groups with more than one file.
        groups = dict([(x, groups[x]) for x in groups if len(groups[x]) > 1])

    out.write("Found %s sets of files with identical %s. (%d files examined)"
              % (len(groups), fun_desc, count), newline=True)
    return groups


Instruction:
Make a summary of the following Python 3 code
def groupify(function):

    @wraps(function)
    def wrapper(paths, *args, **kwargs):  # pylint: disable=missing-docstring
        groups = {}

        for path in paths:
            key = function(path, *args, **kwargs)
            if key is not None:
                groups.setdefault(key, set()).add(path)

        return groups
    return wrapper

Response: Decorator to group a function into a dict of key - group tuples.


Instruction:
Given the following Python 3 function, write the documentation
def sizeClassifier(path, min_size=DEFAULTS['min_size']):
    filestat = _stat(path)
    if stat.S_ISLNK(filestat.st_mode):
        return  # Skip symlinks.

    if filestat.st_size < min_size:
        return  # Skip files below the size limit

    return filestat.st_size

Response: Sort a file into a group based on - disk size.


Instruction:
Here you have a function in Python 3, explain what it does
def groupByContent(paths):
    handles, results = [], []

    # Silently ignore files we don't have permission to read.
    hList = []
    for path in paths:
        try:
            hList.append((path, open(path, 'rb'), ''))
        except IOError:
            pass  # TODO: Verbose-mode output here.
    handles.append(hList)

    while handles:
        # Process more blocks.
        more, done = compareChunks(handles.pop(0))

        # Add the results to the top-level lists.
        handles.extend(more)
        results.extend(done)

    # Keep the same API as the others.
    return dict((x[0], x) for x in results)

Response: This function takes a list of paths and returns a dict mapping one path to a list of all paths with the same contents.


Instruction:
Given the following Python 3 function, write the documentation
def compareChunks(handles, chunk_size=CHUNK_SIZE):
    chunks = [(path, fh, fh.read(chunk_size)) for path, fh, _ in handles]
    more, done = [], []

    # While there are combinations not yet tried...
    while chunks:
        # Compare the first chunk to all successive chunks
        matches, non_matches = [chunks[0]], []
        for chunk in chunks[1:]:
            if matches[0][2] == chunk[2]:
                matches.append(chunk)
            else:
                non_matches.append(chunk)
        # Check for EOF or obviously unique files
        if len(matches) == 1 or matches[0][2] == "":
            for x in matches:
                x[1].close()
            done.append([x[0] for x in matches])
        else:
            more.append(matches)
        chunks = non_matches

    return more, done

Response: This function takes a list of file handles and returns a list of lists of lists that are fed back into this function individually.


Instruction:
Can you implement a function in Python 3 that
displays a list of files and prompt for ones to be kept.

Response:
def pruneUI(dupeList, mainPos=1, mainLen=1):
    """Display a list of files and prompt for ones to be kept.

    The user may enter ``all`` or one or more numbers separated by spaces
    and/or commas.

    .. note:: It is impossible to accidentally choose to keep none of the
        displayed files.

    :param dupeList: A list duplicate file paths
    :param mainPos: Used to display "set X of Y"
    :param mainLen: Used to display "set X of Y"
    :type dupeList: :class:`~__builtins__.list`
    :type mainPos: :class:`~__builtins__.int`
    :type mainLen: :class:`~__builtins__.int`

    :returns: A list of files to be deleted.
    :rtype: :class:`~__builtins__.int`
    """
    dupeList = sorted(dupeList)
    print
    for pos, val in enumerate(dupeList):
        print "%d) %s" % (pos + 1, val)
    while True:
        choice = raw_input("[%s/%s] Keepers: " % (mainPos, mainLen)).strip()
        if not choice:
            print ("Please enter a space/comma-separated list of numbers or "
                   "'all'.")
            continue
        elif choice.lower() == 'all':
            return []
        try:
            out = [int(x) - 1 for x in choice.replace(',', ' ').split()]
            return [val for pos, val in enumerate(dupeList) if pos not in out]
        except ValueError:
            print("Invalid choice. Please enter a space/comma-separated list"
                  "of numbers or 'all'.")


Instruction:
Can you implement a function in Python 3 that
deletes duplicate files in a single tree tree.

Response:
def delete_dupes(groups, prefer_list=None, interactive=True, dry_run=False):
    """Code to handle the :option:`--delete` command-line option.

    :param groups: A list of groups of paths.
    :type groups: iterable

    :param prefer_list: A whitelist to be compiled by
        :func:`~fastdupes.multiglob_compile` and used to skip some prompts.

    :param interactive: If ``False``, assume the user wants to keep all copies
        when a prompt would otherwise be displayed.
    :type interactive: :class:`~__builtins__.bool`

    :param dry_run: If ``True``, only pretend to delete files.
    :type dry_run: :class:`~__builtins__.bool`

    .. todo:: Add a secondary check for symlinks for safety.
    """
    prefer_list = prefer_list or []
    prefer_re = multiglob_compile(prefer_list, prefix=True)

    for pos, group in enumerate(groups.values()):
        preferred = [x for x in group if prefer_re.match(x)]
        pruneList = [x for x in group if x not in preferred]
        if not preferred:
            if interactive:
                pruneList = pruneUI(group, pos + 1, len(groups))
                preferred = [x for x in group if x not in pruneList]
            else:
                preferred, pruneList = pruneList, []

        assert preferred  # Safety check
        for path in pruneList:
            print "Removing %s" % path
            if not dry_run:
                os.remove(path)


Instruction:
Can you implement a function in Python 3 that
writes the given text to the current line.

Response:
def write(self, text, newline=False):
        """Use ``\\r`` to overdraw the current line with the given text.

        This function transparently handles tracking how much overdrawing is
        necessary to erase the previous line when used consistently.

        :param text: The text to be outputted
        :param newline: Whether to start a new line and reset the length count.
        :type text: :class:`~__builtins__.str`
        :type newline: :class:`~__builtins__.bool`
        """
        if not self.isatty:
            self.fobj.write('%s\n' % text)
            return

        msg_len = len(text)
        self.max_len = max(self.max_len, msg_len)

        self.fobj.write("\r%-*s" % (self.max_len, text))
        if newline or not self.isatty:
            self.fobj.write('\n')
            self.max_len = 0


Instruction:
Can you write a function in Python 3 where it
summarizes the text in terms of maximum coverage problem.

Response:
def summarize(text, char_limit, sentence_filter=None, debug=False):
    '''
    select sentences in terms of maximum coverage problem

    Args:
      text: text to be summarized (unicode string)
      char_limit: summary length (the number of characters)

    Returns:
      list of extracted sentences

    Reference:
      Hiroya Takamura, Manabu Okumura.
      Text summarization model based on maximum coverage problem and its
      variant. (section 3)
      http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.222.6945
    '''
    debug_info = {}

    sents = list(tools.sent_splitter_ja(text))
    words_list = [
        # pulp variables should be utf-8 encoded
        w.encode('utf-8') for s in sents for w in tools.word_segmenter_ja(s)
    ]

    tf = collections.Counter()
    for words in words_list:
        for w in words:
            tf[w] += 1.0

    if sentence_filter is not None:
        valid_indices = [i for i, s in enumerate(sents) if sentence_filter(s)]
        sents = [sents[i] for i in valid_indices]
        words_list = [words_list[i] for i in valid_indices]

    sent_ids = [str(i) for i in range(len(sents))]  # sentence id
    sent_id2len = dict((id_, len(s)) for id_, s in zip(sent_ids, sents))  # c

    word_contain = dict()  # a
    for id_, words in zip(sent_ids, words_list):
        word_contain[id_] = collections.defaultdict(lambda: 0)
        for w in words:
            word_contain[id_][w] = 1

    prob = pulp.LpProblem('summarize', pulp.LpMaximize)

    # x
    sent_vars = pulp.LpVariable.dicts('sents', sent_ids, 0, 1, pulp.LpBinary)
    # z
    word_vars = pulp.LpVariable.dicts('words', tf.keys(), 0, 1, pulp.LpBinary)

    # first, set objective function: sum(w*z)
    prob += pulp.lpSum([tf[w] * word_vars[w] for w in tf])

    # next, add constraints
    # limit summary length: sum(c*x) <= K
    prob += pulp.lpSum(
        [sent_id2len[id_] * sent_vars[id_] for id_ in sent_ids]
    ) <= char_limit, 'lengthRequirement'
    # for each term, sum(a*x) <= z
    for w in tf:
        prob += pulp.lpSum(
            [word_contain[id_][w] * sent_vars[id_] for id_ in sent_ids]
        ) >= word_vars[w], 'z:{}'.format(w)

    prob.solve()
    # print("Status:", pulp.LpStatus[prob.status])

    sent_indices = []
    for v in prob.variables():
        # print v.name, "=", v.varValue
        if v.name.startswith('sents') and v.varValue == 1:
            sent_indices.append(int(v.name.split('_')[-1]))

    return [sents[i] for i in sent_indices], debug_info


Instruction:
Create a Python 3 function to
compute the lexical centrality score of a list of sentences.

Response:
def lexrank(sentences, continuous=False, sim_threshold=0.1, alpha=0.9,
            use_divrank=False, divrank_alpha=0.25):
    '''
    compute centrality score of sentences.

    Args:
      sentences: [u'こんにちは．', u'私の名前は飯沼です．', ... ]
      continuous: if True, apply continuous LexRank. (see reference)
      sim_threshold: if continuous is False and smilarity is greater or
        equal to sim_threshold, link the sentences.
      alpha: the damping factor of PageRank and DivRank
      divrank: if True, apply DivRank instead of PageRank
      divrank_alpha: strength of self-link [0.0-1.0]
        (it's not the damping factor, see divrank.py)

    Returns: tuple
      (
        {
          # sentence index -> score
          0: 0.003,
          1: 0.002,
          ...
        },
        similarity_matrix
      )

    Reference:
      Günes Erkan and Dragomir R. Radev.
      LexRank: graph-based lexical centrality as salience in text
      summarization. (section 3)
      http://www.cs.cmu.edu/afs/cs/project/jair/pub/volume22/erkan04a-html/erkan04a.html
    '''
    # configure ranker
    ranker_params = {'max_iter': 1000}
    if use_divrank:
        ranker = divrank_scipy
        ranker_params['alpha'] = divrank_alpha
        ranker_params['d'] = alpha
    else:
        ranker = networkx.pagerank_scipy
        ranker_params['alpha'] = alpha

    graph = networkx.DiGraph()

    # sentence -> tf
    sent_tf_list = []
    for sent in sentences:
        words = tools.word_segmenter_ja(sent)
        tf = collections.Counter(words)
        sent_tf_list.append(tf)

    sent_vectorizer = DictVectorizer(sparse=True)
    sent_vecs = sent_vectorizer.fit_transform(sent_tf_list)

    # compute similarities between senteces
    sim_mat = 1 - pairwise_distances(sent_vecs, sent_vecs, metric='cosine')

    if continuous:
        linked_rows, linked_cols = numpy.where(sim_mat > 0)
    else:
        linked_rows, linked_cols = numpy.where(sim_mat >= sim_threshold)

    # create similarity graph
    graph.add_nodes_from(range(sent_vecs.shape[0]))
    for i, j in zip(linked_rows, linked_cols):
        if i == j:
            continue
        weight = sim_mat[i,j] if continuous else 1.0
        graph.add_edge(i, j, {'weight': weight})

    scores = ranker(graph, **ranker_params)
    return scores, sim_mat


Instruction:
Can you tell what is the following Python 3 function doing
def summarize(text, sent_limit=None, char_limit=None, imp_require=None,
              debug=False, **lexrank_params):
    '''
    Args:
      text: text to be summarized (unicode string)
      sent_limit: summary length (the number of sentences)
      char_limit: summary length (the number of characters)
      imp_require: cumulative LexRank score [0.0-1.0]

    Returns:
      list of extracted sentences
    '''
    debug_info = {}
    sentences = list(tools.sent_splitter_ja(text))
    scores, sim_mat = lexrank(sentences, **lexrank_params)
    sum_scores = sum(scores.itervalues())
    acc_scores = 0.0
    indexes = set()
    num_sent, num_char = 0, 0
    for i in sorted(scores, key=lambda i: scores[i], reverse=True):
        num_sent += 1
        num_char += len(sentences[i])
        if sent_limit is not None and num_sent > sent_limit:
            break
        if char_limit is not None and num_char > char_limit:
            break
        if imp_require is not None and acc_scores / sum_scores >= imp_require:
            break
        indexes.add(i)
        acc_scores += scores[i]

    if len(indexes) > 0:
        summary_sents = [sentences[i] for i in sorted(indexes)]
    else:
        summary_sents = sentences

    if debug:
        debug_info.update({
            'sentences': sentences, 'scores': scores
        })

    return summary_sents, debug_info

Response: Summarize the text into a list of sentences and their scores.


Instruction:
How would you implement a function in Python 3 that
returns the summarizer for the given name.

Response:
def get_summarizer(self, name):
        '''
        import summarizers on-demand
        '''
        if name in self.summarizers:
            pass
        elif name == 'lexrank':
            from . import lexrank
            self.summarizers[name] = lexrank.summarize
        elif name == 'mcp':
            from . import mcp_summ
            self.summarizers[name] = mcp_summ.summarize

        return self.summarizers[name]


Instruction:
Make a summary of the following Python 3 code
def summarize(self, text=None, algo=u'lexrank', **summarizer_params):
        '''
        Args:
          text: text to be summarized
          algo: summarizaion algorithm
              - 'lexrank' (default) graph-based
              - 'clexrank' Continuous LexRank
              - 'divrank' DivRank (Diverse Rank)
              - 'mcp' select sentences in terms of maximum coverage problem

          summarizer_params examples:
            char_limit: summary length (the number of characters)
            sent_limit: (not supported with mcp)
              summary length (the number of sentences)
            imp_require: (lexrank only)
              cumulative LexRank score [0.0-1.0]
        '''
        try:  # TODO: generate more useful error message
            # fix parameter type
            for param, value in summarizer_params.items():
                if value == '':
                    del summarizer_params[param]
                    continue
                elif re.match(r'^\d*.\d+$', value):
                    value = float(value)
                elif re.match(r'^\d+$', value):
                    value = int(value)
                elif value == 'true':
                    value = True
                elif value == 'false':
                    value = False
                summarizer_params[param] = value

            if algo in ('lexrank', 'clexrank', 'divrank'):
                summarizer = self.get_summarizer('lexrank')
                if algo == 'clexrank':
                    summarizer_params['continuous'] = True
                if algo == 'divrank':
                    summarizer_params['use_divrank'] = True
            elif algo == 'mcp':
                summarizer = self.get_summarizer('mcp')

            summary, debug_info = summarizer(text, **summarizer_params)

        except Exception, e:
            return json.dumps({'error': str(e)}, ensure_ascii=False, indent=2)
        else:
            res = json.dumps(
                tools.tree_encode({
                    'summary': summary, 'debug_info': debug_info
                }),
                ensure_ascii=False, indent=2
            )
            return res

Response: This function is used to summarize a single object in a tree file.


Instruction:
How would you implement a function in Python 3 that
takes a unicode string that contains multiple Japanese sentences and returns a generator that yields all the sentences that are in the given set of delimiters.

Response:
def sent_splitter_ja(text, delimiters=set(u'。．？！\n\r'),
                     parenthesis=u'（）「」『』“”'):
    '''
    Args:
      text: unicode string that contains multiple Japanese sentences.
      delimiters: set() of sentence delimiter characters.
      parenthesis: to be checked its correspondence.
    Returns:
      generator that yields sentences.
    '''
    paren_chars = set(parenthesis)
    close2open = dict(zip(parenthesis[1::2], parenthesis[0::2]))
    pstack = []
    buff = []

    for i, c in enumerate(text):
        c_next = text[i+1] if i+1 < len(text) else None
        # check correspondence of parenthesis
        if c in paren_chars:
            if c in close2open:  # close
                if len(pstack) > 0 and pstack[-1] == close2open[c]:
                    pstack.pop()
            else:  # open
                pstack.append(c)

        buff.append(c)
        if c in delimiters:
            if len(pstack) == 0 and c_next not in delimiters:
                yield ''.join(buff)
                buff = []

    if len(buff) > 0:
        yield ''.join(buff)


Instruction:
Can you generate the documentation for the following Python 3 function
def divrank(G, alpha=0.25, d=0.85, personalization=None,
            max_iter=100, tol=1.0e-6, nstart=None, weight='weight',
            dangling=None):
    '''
    Returns the DivRank (Diverse Rank) of the nodes in the graph.
    This code is based on networkx.pagerank.

    Args: (diff from pagerank)
      alpha: controls strength of self-link [0.0-1.0]
      d: the damping factor

    Reference:
      Qiaozhu Mei and Jian Guo and Dragomir Radev,
      DivRank: the Interplay of Prestige and Diversity in Information Networks,
      http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.174.7982
    '''

    if len(G) == 0:
        return {}

    if not G.is_directed():
        D = G.to_directed()
    else:
        D = G

    # Create a copy in (right) stochastic form
    W = nx.stochastic_graph(D, weight=weight)
    N = W.number_of_nodes()

    # self-link (DivRank)
    for n in W.nodes_iter():
        for n_ in W.nodes_iter():
            if n != n_ :
                if n_ in W[n]:
                    W[n][n_][weight] *= alpha
            else:
                if n_ not in W[n]:
                    W.add_edge(n, n_)
                W[n][n_][weight] = 1.0 - alpha

    # Choose fixed starting vector if not given
    if nstart is None:
        x = dict.fromkeys(W, 1.0 / N)
    else:
        # Normalized nstart vector
        s = float(sum(nstart.values()))
        x = dict((k, v / s) for k, v in nstart.items())

    if personalization is None:
        # Assign uniform personalization vector if not given
        p = dict.fromkeys(W, 1.0 / N)
    else:
        missing = set(G) - set(personalization)
        if missing:
            raise NetworkXError('Personalization dictionary '
                                'must have a value for every node. '
                                'Missing nodes %s' % missing)
        s = float(sum(personalization.values()))
        p = dict((k, v / s) for k, v in personalization.items())

    if dangling is None:
        # Use personalization vector if dangling vector not specified
        dangling_weights = p
    else:
        missing = set(G) - set(dangling)
        if missing:
            raise NetworkXError('Dangling node dictionary '
                                'must have a value for every node. '
                                'Missing nodes %s' % missing)
        s = float(sum(dangling.values()))
        dangling_weights = dict((k, v/s) for k, v in dangling.items())
    dangling_nodes = [n for n in W if W.out_degree(n, weight=weight) == 0.0]

    # power iteration: make up to max_iter iterations
    for _ in range(max_iter):
        xlast = x
        x = dict.fromkeys(xlast.keys(), 0)
        danglesum = d * sum(xlast[n] for n in dangling_nodes)
        for n in x:
            D_t = sum(W[n][nbr][weight] * xlast[nbr] for nbr in W[n])
            for nbr in W[n]:
                #x[nbr] += d * xlast[n] * W[n][nbr][weight]
                x[nbr] += (
                    d * (W[n][nbr][weight] * xlast[nbr] / D_t) * xlast[n]
                )
            x[n] += danglesum * dangling_weights[n] + (1.0 - d) * p[n]

        # check convergence, l1 norm
        err = sum([abs(x[n] - xlast[n]) for n in x])
        if err < N*tol:
            return x
    raise NetworkXError('divrank: power iteration failed to converge '
                        'in %d iterations.' % max_iter)

Response: Returns the DivRank of the nodes in the graph G.


Instruction:
Can you tell what is the following Python 3 function doing
def divrank_scipy(G, alpha=0.25, d=0.85, personalization=None,
                  max_iter=100, tol=1.0e-6, nstart=None, weight='weight',
                  dangling=None):
    '''
    Returns the DivRank (Diverse Rank) of the nodes in the graph.
    This code is based on networkx.pagerank_scipy
    '''
    import scipy.sparse

    N = len(G)
    if N == 0:
        return {}

    nodelist = G.nodes()
    M = nx.to_scipy_sparse_matrix(G, nodelist=nodelist, weight=weight,
                                  dtype=float)
    S = scipy.array(M.sum(axis=1)).flatten()
    S[S != 0] = 1.0 / S[S != 0]
    Q = scipy.sparse.spdiags(S.T, 0, *M.shape, format='csr')
    M = Q * M

    # self-link (DivRank)
    M = scipy.sparse.lil_matrix(M)
    M.setdiag(0.0)
    M = alpha * M
    M.setdiag(1.0 - alpha)
    #print M.sum(axis=1)

    # initial vector
    x = scipy.repeat(1.0 / N, N)

    # Personalization vector
    if personalization is None:
        p = scipy.repeat(1.0 / N, N)
    else:
        missing = set(nodelist) - set(personalization)
        if missing:
            raise NetworkXError('Personalization vector dictionary '
                                'must have a value for every node. '
                                'Missing nodes %s' % missing)
        p = scipy.array([personalization[n] for n in nodelist],
                        dtype=float)
        p = p / p.sum()

    # Dangling nodes
    if dangling is None:
        dangling_weights = p
    else:
        missing = set(nodelist) - set(dangling)

        if missing:
            raise NetworkXError('Dangling node dictionary '
                                'must have a value for every node. '
                                'Missing nodes %s' % missing)
        # Convert the dangling dictionary into an array in nodelist order
        dangling_weights = scipy.array([dangling[n] for n in nodelist],
                                       dtype=float)
        dangling_weights /= dangling_weights.sum()
    is_dangling = scipy.where(S == 0)[0]

    # power iteration: make up to max_iter iterations
    for _ in range(max_iter):
        xlast = x
        D_t =  M * x
        x = (
            d * (x / D_t * M * x + sum(x[is_dangling]) * dangling_weights)
            + (1.0 - d) * p
        )
        # check convergence, l1 norm
        err = scipy.absolute(x - xlast).sum()
        if err < N * tol:
            return dict(zip(nodelist, map(float, x)))

    raise NetworkXError('divrank_scipy: power iteration failed to converge '
                        'in %d iterations.' % max_iter)

Response: This function is based on pagerank_scipy. It returns the DivRank of the nodes in the graph.


Instruction:
How would you code a function in Python 3 to
return an error code between 0 and 99.

Response:
def code_mapping(level, msg, default=99):
    """Return an error code between 0 and 99."""
    try:
        return code_mappings_by_level[level][msg]
    except KeyError:
        pass
    # Following assumes any variable messages take the format
    # of 'Fixed text "variable text".' only:
    # e.g. 'Unknown directive type "req".'
    # ---> 'Unknown directive type'
    # e.g. 'Unknown interpreted text role "need".'
    # ---> 'Unknown interpreted text role'
    if msg.count('"') == 2 and ' "' in msg and msg.endswith('".'):
        txt = msg[: msg.index(' "')]
        return code_mappings_by_level[level].get(txt, default)
    return default


Instruction:
Implement a function in Python 3 to
remove the quotes delimiting a docstring.

Response:
def dequote_docstring(text):
    """Remove the quotes delimiting a docstring."""
    # TODO: Process escaped characters unless raw mode?
    text = text.strip()
    if len(text) > 6 and text[:3] == text[-3:] == '"""':
        # Standard case, """..."""
        return text[3:-3]
    if len(text) > 7 and text[:4] in ('u"""', 'r"""') and text[-3:] == '"""':
        # Unicode, u"""...""", or raw r"""..."""
        return text[4:-3]
    # Other flake8 tools will report atypical quotes:
    if len(text) > 6 and text[:3] == text[-3:] == "'''":
        return text[3:-3]
    if len(text) > 7 and text[:4] in ("u'''", "r'''") and text[-3:] == "'''":
        return text[4:-3]
    if len(text) > 2 and text[0] == text[-1] == '"':
        return text[1:-1]
    if len(text) > 3 and text[:2] in ('u"', 'r"') and text[-1] == '"':
        return text[2:-1]
    if len(text) > 2 and text[0] == text[-1] == "'":
        return text[1:-1]
    if len(text) > 3 and text[:2] in ("u'", "r'") and text[-1] == "'":
        return text[2:-1]
    raise ValueError("Bad quotes!")


Instruction:
Write a Python 3 function that can
return True iff this function should be considered public.

Response:
def is_public(self):
        """Return True iff this function should be considered public."""
        if self.all is not None:
            return self.name in self.all
        else:
            return not self.name.startswith("_")


Instruction:
Can you generate the documentation for the following Python 3 function
def is_public(self):
        # Check if we are a setter/deleter method, and mark as private if so.
        for decorator in self.decorators:
            # Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
            if re.compile(r"^{}\.".format(self.name)).match(decorator.name):
                return False
        name_is_public = (
            not self.name.startswith("_")
            or self.name in VARIADIC_MAGIC_METHODS
            or self.is_magic
        )
        return self.parent.is_public and name_is_public

Response: Return True iff this method should be considered public.


Instruction:
Create a Python 3 function for
returning True iff this class should be considered public.

Response:
def is_public(self):
        """Return True iff this class should be considered public."""
        return (
            not self.name.startswith("_")
            and self.parent.is_class
            and self.parent.is_public
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def move(self):
        previous = self.current
        current = self._next_from_generator()
        self.current = None if current is None else Token(*current)
        self.line = self.current.start[0] if self.current else self.line
        self.got_logical_newline = previous.kind in self.LOGICAL_NEWLINES
        return previous

Response: Moves the cursor to the next token.


Instruction:
Can you implement a function in Python 3 that
parses the given file - like object and return its Module object.

Response:
def parse(self, filelike, filename):
        """Parse the given file-like object and return its Module object."""
        self.log = log
        self.source = filelike.readlines()
        src = "".join(self.source)
        # This may raise a SyntaxError:
        compile(src, filename, "exec")
        self.stream = TokenStream(StringIO(src))
        self.filename = filename
        self.all = None
        self.future_imports = set()
        self._accumulated_decorators = []
        return self.parse_module()


Instruction:
Implement a Python 3 function for
consuming one token and verify it is of the expected kind.

Response:
def consume(self, kind):
        """Consume one token and verify it is of the expected kind."""
        next_token = self.stream.move()
        assert next_token.kind == kind


Instruction:
Can you create a Python 3 function that
skips tokens in the stream until a certain token kind is reached.

Response:
def leapfrog(self, kind, value=None):
        """Skip tokens in the stream until a certain token kind is reached.

        If `value` is specified, tokens whose values are different will also
        be skipped.
        """
        while self.current is not None:
            if self.current.kind == kind and (
                value is None or self.current.value == value
            ):
                self.consume(kind)
                return
            self.stream.move()


Instruction:
How would you explain what the following Python 3 function does
def parse_docstring(self):
        self.log.debug(
            "parsing docstring, token is %r (%s)", self.current.kind, self.current.value
        )
        while self.current.kind in (tk.COMMENT, tk.NEWLINE, tk.NL):
            self.stream.move()
            self.log.debug(
                "parsing docstring, token is %r (%s)",
                self.current.kind,
                self.current.value,
            )
        if self.current.kind == tk.STRING:
            docstring = self.current.value
            self.stream.move()
            return docstring
        return None

Response: Parse a single docstring and return its value.


Instruction:
Implement a Python 3 function for
parsing multiple definitions and yield them.

Response:
def parse_definitions(self, class_, all=False):
        """Parse multiple definitions and yield them."""
        while self.current is not None:
            self.log.debug(
                "parsing definition list, current token is %r (%s)",
                self.current.kind,
                self.current.value,
            )
            self.log.debug("got_newline: %s", self.stream.got_logical_newline)
            if all and self.current.value == "__all__":
                self.parse_all()
            elif (
                self.current.kind == tk.OP
                and self.current.value == "@"
                and self.stream.got_logical_newline
            ):
                self.consume(tk.OP)
                self.parse_decorators()
            elif self.current.value in ["def", "class"]:
                yield self.parse_definition(class_._nest(self.current.value))
            elif self.current.kind == tk.INDENT:
                self.consume(tk.INDENT)
                for definition in self.parse_definitions(class_):
                    yield definition
            elif self.current.kind == tk.DEDENT:
                self.consume(tk.DEDENT)
                return
            elif self.current.value == "from":
                self.parse_from_import_statement()
            else:
                self.stream.move()


Instruction:
Can you tell what is the following Python 3 function doing
def parse_all(self):
        assert self.current.value == "__all__"
        self.consume(tk.NAME)
        if self.current.value != "=":
            raise AllError("Could not evaluate contents of __all__. ")
        self.consume(tk.OP)
        if self.current.value not in "([":
            raise AllError("Could not evaluate contents of __all__. ")
        self.consume(tk.OP)

        self.all = []
        all_content = "("
        while self.current.kind != tk.OP or self.current.value not in ")]":
            if self.current.kind in (tk.NL, tk.COMMENT):
                pass
            elif self.current.kind == tk.STRING or self.current.value == ",":
                all_content += self.current.value
            else:
                raise AllError(
                    "Unexpected token kind in  __all__: {!r}. ".format(
                        self.current.kind
                    )
                )
            self.stream.move()
        self.consume(tk.OP)
        all_content += ")"
        try:
            self.all = eval(all_content, {})
        except BaseException as e:
            raise AllError(
                "Could not evaluate contents of __all__."
                "\bThe value was {}. The exception was:\n{}".format(all_content, e)
            )

Response: Parse the __all__ definition in a module.


Instruction:
Explain what the following Python 3 code does
def parse_module(self):
        self.log.debug("parsing module.")
        start = self.line
        docstring = self.parse_docstring()
        children = list(self.parse_definitions(Module, all=True))
        assert self.current is None, self.current
        end = self.line
        cls = Module
        if self.filename.endswith("__init__.py"):
            cls = Package
        module = cls(
            self.filename,
            self.source,
            start,
            end,
            [],
            docstring,
            children,
            None,
            self.all,
            None,
            "",
        )
        for child in module.children:
            child.parent = module
        module.future_imports = self.future_imports
        self.log.debug("finished parsing module.")
        return module

Response: Parse a module and its children and return a Module object.


Instruction:
Make a summary of the following Python 3 code
def check_current(self, kind=None, value=None):
        msg = textwrap.dedent(
            """
        Unexpected token at line {self.line}:
        In file: {self.filename}
        Got kind {self.current.kind!r}
        Got value {self.current.value}
        """.format(
                self=self
            )
        )
        kind_valid = self.current.kind == kind if kind else True
        value_valid = self.current.value == value if value else True
        assert kind_valid and value_valid, msg

Response: Verify the current token is of type kind and equals value.


Instruction:
How would you code a function in Python 3 to
parse a from x import y statement.

Response:
def parse_from_import_statement(self):
        """Parse a 'from x import y' statement.

        The purpose is to find __future__ statements.
        """
        self.log.debug("parsing from/import statement.")
        is_future_import = self._parse_from_import_source()
        self._parse_from_import_names(is_future_import)


Instruction:
Create a Python 3 function for
parsing the y part in a from x import y statement.

Response:
def _parse_from_import_names(self, is_future_import):
        """Parse the 'y' part in a 'from x import y' statement."""
        if self.current.value == "(":
            self.consume(tk.OP)
            expected_end_kinds = (tk.OP,)
        else:
            expected_end_kinds = (tk.NEWLINE, tk.ENDMARKER)
        while self.current.kind not in expected_end_kinds and not (
            self.current.kind == tk.OP and self.current.value == ";"
        ):
            if self.current.kind != tk.NAME:
                self.stream.move()
                continue
            self.log.debug(
                "parsing import, token is %r (%s)",
                self.current.kind,
                self.current.value,
            )
            if is_future_import:
                self.log.debug("found future import: %s", self.current.value)
                self.future_imports.add(self.current.value)
            self.consume(tk.NAME)
            self.log.debug(
                "parsing import, token is %r (%s)",
                self.current.kind,
                self.current.value,
            )
            if self.current.kind == tk.NAME and self.current.value == "as":
                self.consume(tk.NAME)  # as
                if self.current.kind == tk.NAME:
                    self.consume(tk.NAME)  # new name, irrelevant
            if self.current.value == ",":
                self.consume(tk.OP)
            self.log.debug(
                "parsing import, token is %r (%s)",
                self.current.kind,
                self.current.value,
            )


Instruction:
Here you have a function in Python 3, explain what it does
def run(self):
        # Is there any reason not to call load_source here?
        if self.err is not None:
            assert self.source is None
            msg = "%s%03i %s" % (
                rst_prefix,
                rst_fail_load,
                "Failed to load file: %s" % self.err,
            )
            yield 0, 0, msg, type(self)
            module = []
        try:
            module = parse(StringIO(self.source), self.filename)
        except SyntaxError as err:
            msg = "%s%03i %s" % (
                rst_prefix,
                rst_fail_parse,
                "Failed to parse file: %s" % err,
            )
            yield 0, 0, msg, type(self)
            module = []
        except AllError:
            msg = "%s%03i %s" % (
                rst_prefix,
                rst_fail_all,
                "Failed to parse __all__ entry.",
            )
            yield 0, 0, msg, type(self)
            module = []
        for definition in module:
            if not definition.docstring:
                # People can use flake8-docstrings to report missing
                # docstrings
                continue
            try:
                # Note we use the PEP257 trim algorithm to remove the
                # leading whitespace from each line - this avoids false
                # positive severe error "Unexpected section title."
                unindented = trim(dequote_docstring(definition.docstring))
                # Off load RST validation to reStructuredText-lint
                # which calls docutils internally.
                # TODO: Should we pass the Python filename as filepath?
                rst_errors = list(rst_lint.lint(unindented))
            except Exception as err:
                # e.g. UnicodeDecodeError
                msg = "%s%03i %s" % (
                    rst_prefix,
                    rst_fail_lint,
                    "Failed to lint docstring: %s - %s" % (definition.name, err),
                )
                yield definition.start, 0, msg, type(self)
                continue
            for rst_error in rst_errors:
                # TODO - make this a configuration option?
                if rst_error.level <= 1:
                    continue
                # Levels:
                #
                # 0 - debug   --> we don't receive these
                # 1 - info    --> RST1## codes
                # 2 - warning --> RST2## codes
                # 3 - error   --> RST3## codes
                # 4 - severe  --> RST4## codes
                #
                # Map the string to a unique code:
                msg = rst_error.message.split("\n", 1)[0]
                code = code_mapping(rst_error.level, msg)
                assert code < 100, code
                code += 100 * rst_error.level
                msg = "%s%03i %s" % (rst_prefix, code, msg)

                # This will return the line number by combining the
                # start of the docstring with the offet within it.
                # We don't know the column number, leaving as zero.
                yield definition.start + rst_error.line, 0, msg, type(self)

Response: Yields a list of tuples each containing the start and length of the entry.


Instruction:
Can you write a function in Python 3 where it
loads the source for the specified file.

Response:
def load_source(self):
        """Load the source for the specified file."""
        if self.filename in self.STDIN_NAMES:
            self.filename = "stdin"
            if sys.version_info[0] < 3:
                self.source = sys.stdin.read()
            else:
                self.source = TextIOWrapper(sys.stdin.buffer, errors="ignore").read()
        else:
            # Could be a Python 2.7 StringIO with no context manager, sigh.
            # with tokenize_open(self.filename) as fd:
            #     self.source = fd.read()
            handle = tokenize_open(self.filename)
            self.source = handle.read()
            handle.close()


Instruction:
Can you write a function in Python 3 where it
converts a CIE Lab to RGB components.

Response:
def lab_to_rgb(l, a, b):

    """ Converts CIE Lab to RGB components.

    First we have to convert to XYZ color space.
    Conversion involves using a white point,
    in this case D65 which represents daylight illumination.

    Algorithms adopted from:
    http://www.easyrgb.com/math.php

    """

    y = (l+16) / 116.0
    x = a/500.0 + y
    z = y - b/200.0
    v = [x,y,z]
    for i in range(3):
        if pow(v[i],3) > 0.008856:
            v[i] = pow(v[i],3)
        else:
            v[i] = (v[i]-16/116.0) / 7.787

    # Observer = 2, Illuminant = D65
    x = v[0] * 95.047/100
    y = v[1] * 100.0/100
    z = v[2] * 108.883/100

    r = x * 3.2406 + y *-1.5372 + z *-0.4986
    g = x *-0.9689 + y * 1.8758 + z * 0.0415
    b = x * 0.0557 + y *-0.2040 + z * 1.0570
    v = [r,g,b]
    for i in range(3):
        if v[i] > 0.0031308:
            v[i] = 1.055 * pow(v[i], 1/2.4) - 0.055
        else:
            v[i] = 12.92 * v[i]

    #r, g, b = v[0]*255, v[1]*255, v[2]*255
    r, g, b = v[0], v[1], v[2]
    return r, g, b


Instruction:
Implement a function in Python 3 to
return the darkest swatch.

Response:
def _darkest(self):

        """ Returns the darkest swatch.

        Knowing the contract between a light and a dark swatch
        can help us decide how to display readable typography.

        """

        rgb, n = (1.0, 1.0, 1.0), 3.0
        for r,g,b in self:
            if r+g+b < n:
                rgb, n = (r,g,b), r+g+b

        return rgb


Instruction:
Write a Python 3 function for
parsing a theme from XML.

Response:
def parse_theme(self, xml):

        """ Parses a theme from XML returned by Kuler.

        Gets the theme's id, label and swatches.
        All of the swatches are converted to RGB.
        If we have a full description for a theme id in cache,
        parse that to get tags associated with the theme.

        """

        kt = KulerTheme()
        kt.author = xml.getElementsByTagName("author")[0]
        kt.author = kt.author.childNodes[1].childNodes[0].nodeValue
        kt.id = int(self.parse_tag(xml, "id"))
        kt.label = self.parse_tag(xml, "label")
        mode = self.parse_tag(xml, "mode")

        for swatch in xml.getElementsByTagName("swatch"):

            c1 = float(self.parse_tag(swatch, "c1"))
            c2 = float(self.parse_tag(swatch, "c2"))
            c3 = float(self.parse_tag(swatch, "c3"))
            c4 = float(self.parse_tag(swatch, "c4"))

            if mode == "rgb":
                kt.append((c1,c2,c3))
            if mode == "cmyk":
                kt.append(cmyk_to_rgb(c1,c2,c3,c4))
            if mode == "hsv":
                kt.append(colorsys.hsv_to_rgb(c1,c2,c3))
            if mode == "hex":
                kt.append(hex_to_rgb(c1))
            if mode == "lab":
                kt.append(lab_to_rgb(c1,c2,c3))

        # If we have the full theme in cache,
        # parse tags from it.
        if self._cache.exists(self.id_string + str(kt.id)):
            xml = self._cache.read(self.id_string + str(kt.id))
            xml = minidom.parseString(xml)
        for tags in xml.getElementsByTagName("tag"):
            tags = self.parse_tag(tags, "label")
            tags = tags.split(" ")
            kt.tags.extend(tags)

        return kt


Instruction:
Can you tell what is the following Python 3 function doing
def create_listening_socket(host, port, handler):
    sock = socket.socket()
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    sock.bind((host, port))
    sock.listen(1)

    GObject.io_add_watch(sock, GObject.IO_IN, handler)
    return sock

Response: Create a listening socket and set listening options


Instruction:
Make a summary of the following Python 3 code
def listener(self, sock, *args):
        '''Asynchronous connection listener. Starts a handler for each connection.'''
        conn, addr = sock.accept()
        f = conn.makefile(conn)
        self.shell = ShoebotCmd(self.bot, stdin=f, stdout=f, intro=INTRO)

        print(_("Connected"))
        GObject.io_add_watch(conn, GObject.IO_IN, self.handler)
        if self.shell.intro:
            self.shell.stdout.write(str(self.shell.intro)+"\n")
            self.shell.stdout.flush()
        return True

Response: Asynchronous connection listener. Starts a handler for each connection.


Instruction:
How would you code a function in Python 3 to
trust commands cannot be run remotely :param f: :return:

Response:
def trusted_cmd(f):
    """
    Trusted commands cannot be run remotely

    :param f:
    :return:
    """
    def run_cmd(self, line):
        if self.trusted:
            f(self, line)
        else:
            print("Sorry cannot do %s here." % f.__name__[3:])

    global trusted_cmds
    trusted_cmds.add(f.__name__)
    run_cmd.__doc__ = f.__doc__
    return run_cmd


Instruction:
Create a Python 3 function for
printing the response of a single line of the text.

Response:
def print_response(self, input='', keep=False, *args, **kwargs):
        """
        print response, if cookie is set then print that each line
        :param args:
        :param keep: if True more output is to come
        :param cookie: set a custom cookie,
                       if set to 'None' then self.cookie will be used.
                       if set to 'False' disables cookie output entirely
        :return:
        """
        cookie = kwargs.get('cookie')
        if cookie is None:
            cookie = self.cookie or ''
        status = kwargs.get('status')
        lines = input.splitlines()
        if status and not lines:
            lines = ['']

        if cookie:
            output_template = '{cookie} {status}{cookie_char}{line}'
        else:
            output_template = '{line}'

        for i, line in enumerate(lines):
            if i != len(lines) - 1 or keep is True:
                cookie_char = '>'
            else:
                # last line
                cookie_char = ':'

            print(output_template.format(
                cookie_char=cookie_char,
                cookie=cookie,
                status=status or '',
                line=line.strip()), file=self.stdout)


Instruction:
Explain what the following Python 3 code does
def do_escape_nl(self, arg):
        if arg.lower() == 'off':
            self.escape_nl = False
        else:
            self.escape_nl = True

Response: Escape newlines in any responses


Instruction:
Here you have a function in Python 3, explain what it does
def do_prompt(self, arg):
        if arg.lower() == 'off':
            self.response_prompt = ''
            self.prompt = ''
            return
        elif arg.lower() == 'on':
            self.prompt = PROMPT
            self.response_prompt = RESPONSE_PROMPT
        self.print_response('prompt: %s' % self.prompt, '\n', 'response: %s' % self.response_prompt)

Response: Enable or disable prompt


Instruction:
Write a Python 3 function for
setting the framerate of the current session

Response:
def do_speed(self, speed):
        """
        rewind
        """
        if speed:
            try:
                self.bot._speed = float(speed)
            except Exception as e:
                self.print_response('%s is not a valid framerate' % speed)
                return
        self.print_response('Speed: %s FPS' % self.bot._speed)


Instruction:
Create a Python 3 function for
restarting the current thread.

Response:
def do_restart(self, line):
        """
        Attempt to restart the bot.
        """
        self.bot._frame = 0
        self.bot._namespace.clear()
        self.bot._namespace.update(self.bot._initial_namespace)


Instruction:
Can you generate the documentation for the following Python 3 function
def do_pause(self, line):
        # along with stuff in socketserver and shell
        if self.pause_speed is None:
            self.pause_speed = self.bot._speed
            self.bot._speed = 0
            self.print_response('Paused')
        else:
            self.bot._speed = self.pause_speed
            self.pause_speed = None
            self.print_response('Playing')

Response: Pause the current locale.


Instruction:
Can you create a Python 3 function that
plays the current user.

Response:
def do_play(self, line):
        """
        Resume playback if bot is paused
        """
        if self.pause_speed is None:
            self.bot._speed = self.pause_speed
            self.pause_speed = None
        self.print_response("Play")


Instruction:
Create a Python 3 function for
going to specific frame

Response:
def do_goto(self, line):
        """
        Go to specific frame
        :param line:
        :return:
        """
        self.print_response("Go to frame %s" % line)
        self.bot._frame = int(line)


Instruction:
Can you implement a function in Python 3 that
rewinds the current frame

Response:
def do_rewind(self, line):
        """
        rewind
        """
        self.print_response("Rewinding from frame %s to 0" % self.bot._frame)
        self.bot._frame = 0


Instruction:
How would you code a function in Python 3 to
list bot variables and values

Response:
def do_vars(self, line):
        """
        List bot variables and values
        """
        if self.bot._vars:
            max_name_len = max([len(name) for name in self.bot._vars])
            for i, (name, v) in enumerate(self.bot._vars.items()):
                keep = i < len(self.bot._vars) - 1
                self.print_response("%s = %s" % (name.ljust(max_name_len), v.value), keep=keep)
        else:
            self.print_response("No vars")


Instruction:
Can you generate the documentation for the following Python 3 function
def do_load_base64(self, line):
        cookie = self.cookie
        executor = self.bot._executor

        def source_good():
            self.print_response(status=RESPONSE_CODE_OK, cookie=cookie)
            executor.clear_callbacks()

        def source_bad(tb):
            if called_good:
                # good and bad callbacks shouldn't both be called
                raise ValueError('Good AND Bad callbacks called !')
            self.print_response(status=RESPONSE_REVERTED, keep=True, cookie=cookie)
            self.print_response(tb.replace('\n', '\\n'), cookie=cookie)
            executor.clear_callbacks()

        called_good = False
        source = str(base64.b64decode(line))
        # Test compile
        publish_event(SOURCE_CHANGED_EVENT, data=source, extra_channels="shoebot.source")
        self.bot._executor.load_edited_source(source, good_cb=source_good, bad_cb=source_bad)

Response: Load base64 encoded source file and send new code to Editors.


Instruction:
Can you create a Python 3 function that
exits shell and shoebot

Response:
def do_exit(self, line):
        """
        Exit shell and shoebot
        """
        if self.trusted:
            publish_event(QUIT_EVENT)
        self.print_response('Bye.\n')
        return True


Instruction:
Create a Python 3 function for
making the current window fullscreen

Response:
def do_fullscreen(self, line):
        """
        Make the current window fullscreen
        """
        self.bot.canvas.sink.trigger_fullscreen_action(True)
        print(self.response_prompt, file=self.stdout)


Instruction:
How would you explain what the following Python 3 function does
def do_windowed(self, line):
        self.bot.canvas.sink.trigger_fullscreen_action(False)
        print(self.response_prompt, file=self.stdout)

Response: Un - fullscreen the current window.


Instruction:
How would you code a function in Python 3 to
exit shell and shoebot Alias for exit.

Response:
def do_EOF(self, line):
        """
        Exit shell and shoebot

        Alias for exit.
        """
        print(self.response_prompt, file=self.stdout)
        return self.do_exit(line)


Instruction:
How would you code a function in Python 3 to
show help on all commands.

Response:
def do_help(self, arg):
        """
        Show help on all commands.
        """
        print(self.response_prompt, file=self.stdout)
        return cmd.Cmd.do_help(self, arg)


Instruction:
Can you write a function in Python 3 where it
allows commands to have a last parameter of 'cookie=somevalue' TODO somevalue will be prepended onto any output lines so that editors can distinguish output from certain kinds of events they have sent. :param line: :return:

Response:
def precmd(self, line):
        """
        Allow commands to have a last parameter of 'cookie=somevalue'

        TODO somevalue will be prepended onto any output lines so
        that editors can distinguish output from certain kinds
        of events they have sent.

        :param line:
        :return:
        """
        args = shlex.split(line or "")
        if args and 'cookie=' in args[-1]:
            cookie_index = line.index('cookie=')
            cookie = line[cookie_index + 7:]
            line = line[:cookie_index].strip()
            self.cookie = cookie
        if line.startswith('#'):
            return ''
        elif '=' in line:
            # allow  somevar=somevalue

            # first check if we really mean a command
            cmdname = line.partition(" ")[0]
            if hasattr(self, "do_%s" % cmdname):
                return line

            if not line.startswith("set "):
                return "set " + line
            else:
                return line
        if len(args) and args[0] in self.shortcuts:
            return "%s %s" % (self.shortcuts[args[0]], " ".join(args[1:]))
        else:
            return line


Instruction:
How would you implement a function in Python 3 that
draws a daisy at x y

Response:
def drawdaisy(x, y, color='#fefefe'):
    """
    Draw a daisy at x, y
    """
    # save location, size etc
    _ctx.push()

    # save fill and stroke
    _fill =_ctx.fill()
    _stroke = _ctx.stroke()

    sc = (1.0 / _ctx.HEIGHT) * float(y * 0.5) * 4.0

    # draw stalk
    _ctx.strokewidth(sc * 2.0)
    _ctx.stroke('#3B240B')

    _ctx.line(x + (sin(x * 0.1) * 10.0), y + 80, x + sin(_ctx.FRAME * 0.1), y)

    # draw flower
    _ctx.translate(-20, 0)
    _ctx.scale(sc)

    # draw petals
    _ctx.fill(color)
    _ctx.nostroke()
    for angle in xrange(0, 360, 45):
        _ctx.rotate(degrees=45)
        _ctx.rect(x, y, 40, 8, 1)

    # draw centre
    _ctx.fill('#F7FE2E')
    _ctx.ellipse(x + 15, y, 10, 10)

    # restore fill and stroke
    _ctx.fill(_fill)
    _ctx.stroke(_stroke)

    # restore location, size etc
    _ctx.pop()


Instruction:
Can you tell what is the following Python 3 function doing
def flatten_fft(scale=1.0):
    _len = len(audio.spectrogram)
    for i, v in enumerate(audio.spectrogram):
        yield scale * (i * v) / _len

Response: Flattens the entire tree into a single graph.


Instruction:
Make a summary of the following Python 3 code
def scaled_fft(fft, scale=1.0):
    data = np.zeros(len(fft))
    for i, v in enumerate(fft):
        data[i] = scale * (i * v) / NUM_SAMPLES

    return data

Response: Returns a scaled version of the FFT.


Instruction:
Write a Python 3 function that can
grab the contents of doc and return it

Response:
def get_source(self, doc):
        """
        Grab contents of 'doc' and return it

        :param doc: The active document
        :return:
        """
        start_iter = doc.get_start_iter()
        end_iter = doc.get_end_iter()
        source = doc.get_text(start_iter, end_iter, False)
        return source


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_view(self, name="shoebot-output"):
        view = gtk.TextView()
        view.set_editable(False)

        fontdesc = pango.FontDescription("Monospace")
        view.modify_font(fontdesc)
        view.set_name(name)

        buff = view.get_buffer()
        buff.create_tag('error', foreground='red')
        return view

Response: Create the gtk. TextView used for shell output


Instruction:
Write a Python 3 function that can
create the gtk. TextView inside a Gtk. ScrolledWindow containing the names of the current user s log entries.

Response:
def _create_view(self, name="shoebot-output"):
        """
        Create the gtk.TextView inside a Gtk.ScrolledWindow
        :return: container, text_view
        """
        text_view = Gtk.TextView()
        text_view.set_editable(False)

        fontdesc = Pango.FontDescription("Monospace")
        text_view.modify_font(fontdesc)
        text_view.set_name(name)

        buff = text_view.get_buffer()
        buff.create_tag('error', foreground='red')

        container = Gtk.ScrolledWindow()
        container.add(text_view)
        container.show_all()
        return container, text_view


Instruction:
Given the following Python 3 function, write the documentation
def openAnything(source, searchpaths=None):

    if hasattr(source, "read"):
        return source

    if source == "-":
        import sys
        return sys.stdin

    # try to open with urllib (if source is http, ftp, or file URL)
    import urllib
    try:
        return urllib.urlopen(source)
    except (IOError, OSError):
        pass

    # try to open with native open function (if source is pathname)
    for path in searchpaths or ['.']:
        try:
            return open(os.path.join(path, source))
        except (IOError, OSError):
            pass

    # treat source as string
    import StringIO
    return StringIO.StringIO(str(source))

Response: Open a file or file - like object and return it as a stream.


Instruction:
Can you write a function in Python 3 where it
loads XML input source return parsed XML document

Response:
def _load(self, source, searchpaths=None):
        """load XML input source, return parsed XML document

        - a URL of a remote XML file ("http://diveintopython.org/kant.xml")
        - a filename of a local XML file ("~/diveintopython/common/py/kant.xml")
        - standard input ("-")
        - the actual XML document, as a string

        :param searchpaths: optional searchpaths if file is used.
        """
        sock = openAnything(source, searchpaths=searchpaths)
        xmldoc = minidom.parse(sock).documentElement
        sock.close()
        return xmldoc


Instruction:
How would you code a function in Python 3 to
guess default source of the current grammar

Response:
def getDefaultSource(self):
        """guess default source of the current grammar

        The default source will be one of the <ref>s that is not
        cross-referenced.  This sounds complicated but it's not.
        Example: The default source for kant.xml is
        "<xref id='section'/>", because 'section' is the one <ref>
        that is not <xref>'d anywhere in the grammar.
        In most grammars, the default source will produce the
        longest (and most interesting) output.
        """
        xrefs = {}
        for xref in self.grammar.getElementsByTagName("xref"):
            xrefs[xref.attributes["id"].value] = 1
        xrefs = xrefs.keys()
        standaloneXrefs = [e for e in self.refs.keys() if e not in xrefs]
        if not standaloneXrefs:
            raise NoSourceError, "can't guess source, and no source specified"
        return '<xref id="%s"/>' % random.choice(standaloneXrefs)


Instruction:
Create a Python 3 function for
resetting output buffer re - parse entire source file and return output

Response:
def refresh(self):
        """reset output buffer, re-parse entire source file, and return output

        Since parsing involves a good deal of randomness, this is an
        easy way to get new output without having to reload a grammar file
        each time.
        """
        self.reset()
        self.parse(self.source)
        return self.output()


Instruction:
Can you implement a function in Python 3 that
chooses a random child element of a node

Response:
def randomChildElement(self, node):
        """choose a random child element of a node

        This is a utility method used by do_xref and do_choice.
        """
        choices = [e for e in node.childNodes
                   if e.nodeType == e.ELEMENT_NODE]
        chosen = random.choice(choices)
        if _debug:
            sys.stderr.write('%s available choices: %s\n' % \
                (len(choices), [e.toxml() for e in choices]))
            sys.stderr.write('Chosen: %s\n' % chosen.toxml())
        return chosen


Instruction:
Write a Python 3 script for
parsing a single XML node

Response:
def parse(self, node):
        """parse a single XML node

        A parsed XML document (from minidom.parse) is a tree of nodes
        of various types.  Each node is represented by an instance of the
        corresponding Python class (Element for a tag, Text for
        text data, Document for the top-level document).  The following
        statement constructs the name of a class method based on the type
        of node we're parsing ("parse_Element" for an Element node,
        "parse_Text" for a Text node, etc.) and then calls the method.
        """
        parseMethod = getattr(self, "parse_%s" % node.__class__.__name__)
        parseMethod(node)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_Text(self, node):
        text = node.data
        if self.capitalizeNextWord:
            self.pieces.append(text[0].upper())
            self.pieces.append(text[1:])
            self.capitalizeNextWord = 0
        else:
            self.pieces.append(text)

Response: parse a text node


Instruction:
How would you explain what the following Python 3 function does
def parse_Element(self, node):
        handlerMethod = getattr(self, "do_%s" % node.tagName)
        handlerMethod(node)

Response: parse an element of the specified node


Instruction:
Implement a function in Python 3 to
handle an xref tag

Response:
def do_xref(self, node):
        """handle <xref id='...'> tag

        An <xref id='...'> tag is a cross-reference to a <ref id='...'>
        tag.  <xref id='sentence'/> evaluates to a randomly chosen child of
        <ref id='sentence'>.
        """
        id = node.attributes["id"].value
        self.parse(self.randomChildElement(self.refs[id]))


Instruction:
Can you create a Python 3 function that
handles the p tag

Response:
def do_p(self, node):
        """handle <p> tag

        The <p> tag is the core of the grammar.  It can contain almost
        anything: freeform text, <choice> tags, <xref> tags, even other
        <p> tags.  If a "class='sentence'" attribute is found, a flag
        is set and the next word will be capitalized.  If a "chance='X'"
        attribute is found, there is an X% chance that the tag will be
        evaluated (and therefore a (100-X)% chance that it will be
        completely ignored)
        """
        keys = node.attributes.keys()
        if "class" in keys:
            if node.attributes["class"].value == "sentence":
                self.capitalizeNextWord = 1
        if "chance" in keys:
            chance = int(node.attributes["chance"].value)
            doit = (chance > random.randrange(100))
        else:
            doit = 1
        if doit:
            for child in node.childNodes: self.parse(child)


Instruction:
How would you implement a function in Python 3 that
replaces HTML special characters by readable characters.

Response:
def replace_entities(ustring, placeholder=" "):

    """Replaces HTML special characters by readable characters.

    As taken from Leif K-Brooks algorithm on:
    http://groups-beta.google.com/group/comp.lang.python

    """

    def _repl_func(match):
        try:
            if match.group(1): # Numeric character reference
                return unichr( int(match.group(2)) )
            else:
                try: return cp1252[ unichr(int(match.group(3))) ].strip()
                except: return unichr( name2codepoint[match.group(3)] )
        except:
            return placeholder

    # Force to Unicode.
    if not isinstance(ustring, unicode):
        ustring = UnicodeDammit(ustring).unicode

    # Don't want some weird unicode character here
    # that truncate_spaces() doesn't know of:
    ustring = ustring.replace(" ", " ")

    # The ^> makes sure nothing inside a tag (i.e. href with query arguments) gets processed.
    _entity_re = re.compile(r'&(?:(#)(\d+)|([^;^> ]+));')
    return _entity_re.sub(_repl_func, ustring)


Instruction:
Create a Python 3 function for
opening a url and return a connection to it.

Response:
def open(url, wait=10):

    """ Returns a connection to a url which you can read().

    When the wait amount is exceeded, raises a URLTimeout.
    When an error occurs, raises a URLError.
    404 errors specifically return a HTTP404NotFound.

    """

    # If the url is a URLParser, get any POST parameters.
    post = None
    if isinstance(url, URLParser) and url.method == "post":
        post = urllib.urlencode(url.query)

    # If the url is a URLParser (or a YahooResult or something),
    # use its string representation.
    url = str(url)

    # Use urllib instead of urllib2 for local files.
    if os.path.exists(url):
        return urllib.urlopen(url)

    else:
        socket.setdefaulttimeout(wait)
        try:
            #connection = urllib2.urlopen(url, post)
            request = urllib2.Request(url, post, {"User-Agent": USER_AGENT, "Referer": REFERER})
            if PROXY:
                p = urllib2.ProxyHandler({PROXY[1]: PROXY[0]})
                o = urllib2.build_opener(p, urllib2.HTTPHandler)
                urllib2.install_opener(o)
            connection = urllib2.urlopen(request)
        except urllib2.HTTPError, e:
            if e.code == 401: raise HTTP401Authentication
            if e.code == 403: raise HTTP403Forbidden
            if e.code == 404: raise HTTP404NotFound
            raise HTTPError
        except urllib2.URLError, e:
            if e.reason[0] == 36: raise URLTimeout
            raise URLError

    return connection


Instruction:
Make a summary of the following Python 3 code
def not_found(url, wait=10):

    """ Returns True when the url generates a "404 Not Found" error.
    """

    try: connection = open(url, wait)
    except HTTP404NotFound:
        return True
    except:
        return False

    return False

Response: Returns True when the url generates a 404 Not Found error.


Instruction:
Create a Python 3 function for
determining if the given URL is a MIME - type of one of the given types.

Response:
def is_type(url, types=[], wait=10):

    """ Determine the MIME-type of the document behind the url.

    MIME is more reliable than simply checking the document extension.
    Returns True when the MIME-type starts with anything in the list of types.

    """

    # Types can also be a single string for convenience.
    if isinstance(types, str):
        types = [types]

    try: connection = open(url, wait)
    except:
        return False

    type = connection.info()["Content-Type"]
    for t in types:
        if type.startswith(t): return True

    return False


Instruction:
Given the following Python 3 function, write the documentation
def requirements(debug=True, with_examples=True, with_pgi=None):
    reqs = list(BASE_REQUIREMENTS)
    if with_pgi is None:
        with_pgi = is_jython

    if debug:
        print("setup options: ")
        print("with_pgi:      ", "yes" if with_pgi else "no")
        print("with_examples: ", "yes" if with_examples else "no")
    if with_pgi:
        reqs.append("pgi")
        if debug:
            print("warning, as of April 2019 typography does not work with pgi")
    else:
        reqs.append(PYGOBJECT)

    if with_examples:
        reqs.extend(EXAMPLE_REQUIREMENTS)

    if debug:
        print("")
        print("")
        for req in reqs:
            print(req)
    return reqs

Response: Build requirements based on flags


Instruction:
Implement a function in Python 3 to
draw a image form path in x y and resize it to width height dimensions.

Response:
def image(self, path, x, y, width=None, height=None, alpha=1.0, data=None, draw=True, **kwargs):
        '''Draws a image form path, in x,y and resize it to width, height dimensions.
        '''
        return self.Image(path, x, y, width, height, alpha, data, **kwargs)


Instruction:
Make a summary of the following Python 3 code
def rect(self, x, y, width, height, roundness=0.0, draw=True, **kwargs):
        '''
        Draw a rectangle from x, y of width, height.

        :param startx: top left x-coordinate
        :param starty: top left y-coordinate

        :param width: height  Size of rectangle.
        :roundness: Corner roundness defaults to 0.0 (a right-angle).
        :draw: If True draws immediately.
        :fill: Optionally pass a fill color.

        :return: path representing the rectangle.

        '''
        path = self.BezierPath(**kwargs)
        path.rect(x, y, width, height, roundness, self.rectmode)
        if draw:
            path.draw()
        return path

Response: Draw a rectangle from x y width height.


Instruction:
How would you explain what the following Python 3 function does
def rectmode(self, mode=None):
        '''
        Set the current rectmode.

        :param mode: CORNER, CENTER, CORNERS
        :return: rectmode if mode is None or valid.
        '''
        if mode in (self.CORNER, self.CENTER, self.CORNERS):
            self.rectmode = mode
            return self.rectmode
        elif mode is None:
            return self.rectmode
        else:
            raise ShoebotError(_("rectmode: invalid input"))

Response: Set the current rectmode.


Instruction:
Can you generate the documentation for the following Python 3 function
def ellipsemode(self, mode=None):
        '''
        Set the current ellipse drawing mode.

        :param mode: CORNER, CENTER, CORNERS
        :return: ellipsemode if mode is None or valid.
        '''
        if mode in (self.CORNER, self.CENTER, self.CORNERS):
            self.ellipsemode = mode
            return self.ellipsemode
        elif mode is None:
            return self.ellipsemode
        else:
            raise ShoebotError(_("ellipsemode: invalid input"))

Response: Set the current ellipse drawing mode.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def circle(self, x, y, diameter, draw=True, **kwargs):
        '''Draw a circle
        :param x: x-coordinate of the top left corner
        :param y: y-coordinate of the top left corner
        :param diameter: Diameter of circle.
        :param draw: Draw immediately (defaults to True, set to False to inhibit drawing)
        :return: Path object representing circle
        '''
        return self.ellipse(x, y, diameter, diameter, draw, **kwargs)

Response: Draw a circle with the specified diameter


Instruction:
How would you explain what the following Python 3 function does
def arrow(self, x, y, width, type=NORMAL, draw=True, **kwargs):
        '''Draw an arrow.

        Arrows can be two types: NORMAL or FORTYFIVE.

        :param x: top left x-coordinate
        :param y: top left y-coordinate
        :param width: width of arrow
        :param type:  NORMAL or FORTYFIVE
        :draw:  If True draws arrow immediately

        :return: Path object representing the arrow.
        '''
        # Taken from Nodebox
        path = self.BezierPath(**kwargs)
        if type == self.NORMAL:
            head = width * .4
            tail = width * .2
            path.moveto(x, y)
            path.lineto(x - head, y + head)
            path.lineto(x - head, y + tail)
            path.lineto(x - width, y + tail)
            path.lineto(x - width, y - tail)
            path.lineto(x - head, y - tail)
            path.lineto(x - head, y - head)
            path.lineto(x, y)
        elif type == self.FORTYFIVE:
            head = .3
            tail = 1 + head
            path.moveto(x, y)
            path.lineto(x, y + width * (1 - head))
            path.lineto(x - width * head, y + width)
            path.lineto(x - width * head, y + width * tail * .4)
            path.lineto(x - width * tail * .6, y + width)
            path.lineto(x - width, y + width * tail * .6)
            path.lineto(x - width * tail * .4, y + width * head)
            path.lineto(x - width, y + width * head)
            path.lineto(x - width * (1 - head), y)
            path.lineto(x, y)
        else:
            raise NameError(_("arrow: available types for arrow() are NORMAL and FORTYFIVE\n"))
        if draw:
            path.draw()
        return path

Response:
Draw an arrow.

        Arrows can be two types: NORMAL or FORTYFIVE.

        :param x: top left x-coordinate
        :param y: top left y-coordinate
        :param width: width of arrow
        :param type:  NORMAL or FORTYFIVE
        :draw:  If True draws arrow immediately

        :return: Path object representing the arrow.