Instruction:
Can you generate a brief explanation for the following Python 3 code
def handle_err(*args):
    if DEBUG:
        print_err(traceback.format_exc(), color=False)
    else:
        print_err(*args, newline=True)

Response: Handle fatal errors caught in __main__ scope.


Instruction:
Can you generate the documentation for the following Python 3 function
def justify(clr, argd):
    methodmap = {
        '--ljust': clr.ljust,
        '--rjust': clr.rjust,
        '--center': clr.center,
    }
    for flag in methodmap:
        if argd[flag]:
            if argd[flag] in ('0', '-'):
                val = get_terminal_size(default=(80, 35))[0]
            else:
                val = try_int(argd[flag], minimum=None)
                if val < 0:
                    # Negative value, subtract from terminal width.
                    val = get_terminal_size(default=(80, 35))[0] + val
            return methodmap[flag](val)

    # No justify args given.
    return clr

Response: Justify str or Colr based on user args.


Instruction:
Make a summary of the following Python 3 code
def list_known_codes(s, unique=True, rgb_mode=False):
    total = 0
    for codedesc in get_known_codes(s, unique=unique, rgb_mode=rgb_mode):
        total += 1
        print(codedesc)
    plural = 'code' if total == 1 else 'codes'
    codetype = ' unique' if unique else ''
    print('\nFound {}{} escape {}.'.format(total, codetype, plural))
    return 0 if total > 0 else 1

Response: Find and print all known escape codes in a string


Instruction:
Create a Python 3 function for
listing all known color names.

Response:
def list_names():
    """ List all known color names. """
    names = get_all_names()
    # This is 375 right now. Probably won't ever change, but I'm not sure.
    nameslen = len(names)
    print('\nListing {} names:\n'.format(nameslen))
    # Using 3 columns of names, still alphabetically sorted from the top down.
    # Longest name so far: lightgoldenrodyellow (20 chars)
    namewidth = 20
    # namewidth * columns == 60, colorwidth * columns == 18, final == 78.
    swatch = ' ' * 9

    third = nameslen // 3
    lastthird = third * 2
    cols = (
        names[0: third],
        names[third: lastthird],
        names[lastthird:],
    )
    # Exactly enough spaces to fill in a blank item (+2 for ': ').
    # This may not ever be used, unless another 'known name' is added.
    blankitem = ' ' * (namewidth + len(swatch) + 2)

    for i in range(third):
        nameset = []
        for colset in cols:
            try:
                nameset.append(colset[i])
            except IndexError:
                nameset.append(None)
                continue
        line = C('').join(
            C(': ').join(
                C(name.rjust(namewidth)),
                C(swatch, back=name),
            ) if name else blankitem
            for name in nameset
        )
        print(line)

    return 0


Instruction:
Implement a Python 3 function for
parsing one or two rgb args given with - G flag.

Response:
def parse_gradient_rgb_args(args):
    """ Parse one or two rgb args given with --gradientrgb.
        Raises InvalidArg for invalid rgb values.
        Returns a tuple of (start_rgb, stop_rgb), where the stop_rgb may be
        None if only one arg value was given and start_rgb may be None if
        no values were given.
    """
    arglen = len(args)
    if arglen < 1 or arglen > 2:
        raise InvalidArg(arglen, label='Expecting 1 or 2 \'-G\' flags, got')
    start_rgb = try_rgb(args[0]) if args else None
    stop_rgb = try_rgb(args[1]) if arglen > 1 else None
    return start_rgb, stop_rgb


Instruction:
Here you have a function in Python 3, explain what it does
def print_err(*args, **kwargs):
    if kwargs.get('file', None) is None:
        kwargs['file'] = sys.stderr

    color = dict_pop_or(kwargs, 'color', True)
    # Use color if asked, but only if the file is a tty.
    if color and kwargs['file'].isatty():
        # Keep any Colr args passed, convert strs into Colrs.
        msg = kwargs.get('sep', ' ').join(
            str(a) if isinstance(a, C) else str(C(a, 'red'))
            for a in args
        )
    else:
        # The file is not a tty anyway, no escape codes.
        msg = kwargs.get('sep', ' ').join(
            str(a.stripped() if isinstance(a, C) else a)
            for a in args
        )
    newline = dict_pop_or(kwargs, 'newline', False)
    if newline:
        msg = '\n{}'.format(msg)
    print(msg, **kwargs)

Response: A wrapper for print that uses stderr by default.


Instruction:
Given the following Python 3 function, write the documentation
def read_stdin():
    if sys.stdin.isatty() and sys.stdout.isatty():
        print('\nReading from stdin until end of file (Ctrl + D)...')

    return sys.stdin.read()

Response: Read text from stdin and print a helpful message for ttys.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def translate(usercodes, rgb_mode=False):
    for code in usercodes:
        code = code.strip().lower()
        if code.isalpha() and (code in codes['fore']):
            # Basic color name.
            yield translate_basic(code)
        else:
            if ',' in code:
                try:
                    r, g, b = (int(c.strip()) for c in code.split(','))
                except (TypeError, ValueError):
                    raise InvalidColr(code)
                code = (r, g, b)

            colorcode = ColorCode(code, rgb_mode=rgb_mode)

            if disabled():
                yield str(colorcode)
            yield colorcode.example()

Response: Translate one or more hex term or rgb value into the others.


Instruction:
Can you create a Python 3 function that
translates a basic color name to color with explanation.

Response:
def translate_basic(usercode):
    """ Translate a basic color name to color with explanation. """
    codenum = get_code_num(codes['fore'][usercode])
    colorcode = codeformat(codenum)
    msg = 'Name: {:>10}, Number: {:>3}, EscapeCode: {!r}'.format(
        usercode,
        codenum,
        colorcode
    )
    if disabled():
        return msg
    return str(C(msg, fore=usercode))


Instruction:
Can you generate the documentation for the following Python 3 function
def try_float(s, default=None, minimum=None):
    if not s:
        return default
    try:
        val = float(s)
    except (TypeError, ValueError):
        raise InvalidNumber(s, label='Invalid float value')
    if (minimum is not None) and (val < minimum):
        val = minimum
    return val

Response: Try parsing a string into a float. If the string is not a float default is returned. If the string is not a float default is returned. If the string is not a float default is returned.


Instruction:
How would you code a function in Python 3 to
try parsing a string into an integer.

Response:
def try_int(s, default=None, minimum=None):
    """ Try parsing a string into an integer.
        If None is passed, default is returned.
        On failure, InvalidNumber is raised.
    """
    if not s:
        return default
    try:
        val = int(s)
    except (TypeError, ValueError):
        raise InvalidNumber(s)
    if (minimum is not None) and (val < minimum):
        val = minimum
    return val


Instruction:
Implement a Python 3 function for
trying parsing a string into an rgb value.

Response:
def try_rgb(s, default=None):
    """ Try parsing a string into an rgb value (int, int, int),
        where the ints are 0-255 inclusive.
        If None is passed, default is returned.
        On failure, InvalidArg is raised.
    """
    if not s:
        return default
    try:
        r, g, b = (int(x.strip()) for x in s.split(','))
    except ValueError:
        raise InvalidRgb(s)
    if not all(in_range(x, 0, 255) for x in (r, g, b)):
        raise InvalidRgb(s)

    return r, g, b


Instruction:
Can you tell what is the following Python 3 function doing
def create_default_context(purpose=None, **kwargs):
    if hasattr(ssl, 'create_default_context'):
        # Python 2.7.9+, Python 3.4+: take a server_side boolean or None, in
        # addition to the ssl.Purpose.XX values. This allows a user to write
        # code that works on all supported Python versions.
        if purpose is None or purpose is False:
            purpose = ssl.Purpose.SERVER_AUTH
        elif purpose is True:
            purpose = ssl.Purpose.CLIENT_AUTH
        return ssl.create_default_context(purpose, **kwargs)
    # Python 2.7.8, Python 3.3
    context = SSLContext(ssl.PROTOCOL_SSLv23)
    if kwargs.get('cafile'):
        context.load_verify_locations(kwargs['cafile'])
    return context

Response:
Create a new SSL context in the most secure way available on the current
    Python version. See ssl. create_default_context.


Instruction:
Explain what the following Python 3 code does
def get_logger(context=None, name=None):
    # Many class instances have their own logger. Share them to save memory if
    # possible, i.e. when *context* is not set.
    if name is None:
        name = _logger_name
    if context is None and name in _logger_dict:
        return _logger_dict[name]
    if context is not None and not isinstance(context, six.string_types):
        context = util.objref(context)
    logger = logging.getLogger(name)
    logger = ContextLogger(logger, context)
    if context is None:
        _logger_dict[name] = logger
    return logger

Response: Return a logger for the given context.


Instruction:
Write a Python 3 function that can
return a string identifying the current thread and fiber.

Response:
def thread_info(self):
        """Return a string identifying the current thread and fiber."""
        tid = threading.current_thread().name
        if tid == 'MainThread':
            tid = 'Main'
        current = fibers.current()
        fid = getattr(current, 'name') if current.parent else 'Root'
        return '{}/{}'.format(tid, fid)


Instruction:
Make a summary of the following Python 3 code
def frame_info(self):
        if not self._logger.isEnabledFor(logging.DEBUG):
            return ''
        f = sys._getframe(3)
        fname = os.path.split(f.f_code.co_filename)[1]
        return '{}:{}'.format(fname, f.f_lineno)

Response: Return a string identifying the current frame.


Instruction:
Implement a Python 3 function for
setting the high and low watermarks for the read buffer.

Response:
def set_buffer_limits(self, high=None, low=None):
        """Set the low and high watermarks for the read buffer."""
        if high is None:
            high = self.default_buffer_size
        if low is None:
            low = high // 2
        self._buffer_high = high
        self._buffer_low = low


Instruction:
Given the following Python 3 function, write the documentation
def feed(self, data):
        self._buffers.append(data)
        self._buffer_size += len(data)
        self._maybe_pause_transport()
        self._can_read.set()

Response: Add data to the buffer and update the internal buffer size.


Instruction:
Implement a Python 3 function for
wrapping the stream with the specified encoding and keyword arguments.

Response:
def wrap(self, encoding, **textio_args):
        """Return a :class:`io.TextIOWrapper` that wraps the stream.

        The wrapper provides text IO on top of the byte stream, using the
        specified *encoding*. The *textio_args* keyword arguments are
        additional keyword arguments passed to the :class:`~io.TextIOWrapper`
        constructor. Unless another buffering scheme is specified, the
        *write_through* option is enabled.
        """
        # By default we want write_through behavior, unless the user specifies
        # something else.
        if 'line_buffering' not in textio_args and 'write_through' not in textio_args:
            textio_args['write_through'] = True
        return compat.TextIOWrapper(self, encoding, **textio_args)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read(self, size=-1):
        self._check_readable()
        chunks = []
        bytes_read = 0
        bytes_left = size
        while True:
            chunk = self._buffer.get_chunk(bytes_left)
            if not chunk:
                break
            chunks.append(chunk)
            bytes_read += len(chunk)
            if bytes_read == size or not chunk:
                break
            if bytes_left > 0:
                bytes_left -= len(chunk)
        # If EOF was set, always return that instead of any error.
        if not chunks and not self._buffer.eof and self._buffer.error:
            raise compat.saved_exc(self._buffer.error)
        return b''.join(chunks)

Response: Read up to size bytes from the internal buffer.


Instruction:
Here you have a function in Python 3, explain what it does
def read1(self, size=-1):
        self._check_readable()
        chunk = self._buffer.get_chunk(size)
        if not chunk and not self._buffer.eof and self._buffer.error:
            raise compat.saved_exc(self._buffer.error)
        return chunk

Response: Read up to size bytes from the internal buffer.


Instruction:
Can you write a function in Python 3 where it
reads a single line from the file.

Response:
def readline(self, limit=-1, delim=b'\n'):
        """Read a single line.

        If EOF is reached before a full line can be read, a partial line is
        returned. If *limit* is specified, at most this many bytes will be read.
        """
        self._check_readable()
        chunks = []
        while True:
            chunk = self._buffer.get_chunk(limit, delim)
            if not chunk:
                break
            chunks.append(chunk)
            if chunk.endswith(delim):
                break
            if limit >= 0:
                limit -= len(chunk)
                if limit == 0:
                    break
        if not chunks and not self._buffer.eof and self._buffer.error:
            raise compat.saved_exc(self._buffer.error)
        return b''.join(chunks)


Instruction:
Create a Python 3 function for
reading lines until EOF and return them as a list.

Response:
def readlines(self, hint=-1):
        """Read lines until EOF, and return them as a list.

        If *hint* is specified, then stop reading lines as soon as the total
        size of all lines exceeds *hint*.
        """
        self._check_readable()
        lines = []
        chunks = []
        bytes_read = 0
        while True:
            chunk = self._buffer.get_chunk(-1, b'\n')
            if not chunk:
                break
            chunks.append(chunk)
            if chunk.endswith(b'\n'):
                lines.append(b''.join(chunks))
                del chunks[:]
                bytes_read += len(lines[-1])
            if hint >= 0 and bytes_read > hint:
                break
        if chunks:
            lines.append(b''.join(chunks))
        if not lines and not self._buffer.eof and self._buffer.error:
            raise compat.saved_exc(self._buffer.error)
        return lines


Instruction:
Explain what the following Python 3 code does
def write(self, data):
        self._check_writable()
        self._transport._can_write.wait()
        self._transport.write(data)

Response: Write data to the transport.


Instruction:
Write a Python 3 script to
write the elements of the sequence seq to the transport.

Response:
def writelines(self, seq):
        """Write the elements of the sequence *seq* to the transport.

        This method will block if the transport's write buffer is at capacity.
        """
        self._check_writable()
        for line in seq:
            self._transport._can_write.wait()
            self._transport.write(line)


Instruction:
How would you explain what the following Python 3 function does
def write_eof(self):
        self._check_writable()
        self._transport._can_write.wait()
        self._transport.write_eof()

Response: Close the write direction of the transport.


Instruction:
Implement a Python 3 function for
closing the underlying transport and the underlying transport.

Response:
def close(self):
        """Close the stream.

        If *autoclose* was passed to the constructor then the underlying
        transport will be closed as well.
        """
        if self._closed:
            return
        if self._autoclose:
            self._transport.close()
            self._transport._closed.wait()
        self._transport = None
        self._closed = True


Instruction:
Write a Python 3 script for
running a function that uses blocking IO.

Response:
def blocking(func, *args, **kwargs):
    """Run a function that uses blocking IO.

    The function is run in the IO thread pool.
    """
    pool = get_io_pool()
    fut = pool.submit(func, *args, **kwargs)
    return fut.result()


Instruction:
Write a Python 3 function that can
yield the objects as they become ready.

Response:
def as_completed(objects, count=None, timeout=None):
    """Wait for one or more waitable objects, yielding them as they become
    ready.

    This is the iterator/generator version of :func:`wait`.
    """
    for obj in objects:
        if not hasattr(obj, 'add_done_callback'):
            raise TypeError('Expecting sequence of waitable objects')
    if count is None:
        count = len(objects)
    if count < 0 or count > len(objects):
        raise ValueError('count must be between 0 and len(objects)')
    if count == 0:
        return
    pending = list(objects)
    for obj in _wait(pending, timeout):
        yield obj
        count -= 1
        if count == 0:
            break


Instruction:
Here you have a function in Python 3, explain what it does
def wait(objects, count=None, timeout=None):
    for obj in objects:
        if not hasattr(obj, 'add_done_callback'):
            raise TypeError('Expecting sequence of waitable objects')
    if count is None:
        count = len(objects)
    if count < 0 or count > len(objects):
        raise ValueError('count must be between 0 and len(objects)')
    if count == 0:
        return [], objects
    pending = list(objects)
    done = []
    try:
        for obj in _wait(pending, timeout):
            done.append(obj)
            if len(done) == count:
                break
    except Timeout:
        pass
    return done, list(filter(bool, pending))

Response: Wait for one or more waitable objects.


Instruction:
Write a Python 3 script to
return whether this future was successfully cancelled.

Response:
def cancelled(self):
        """Return whether this future was successfully cancelled."""
        return self._state == self.S_EXCEPTION and isinstance(self._result, Cancelled)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cancel(self):
        # We leverage/abuse our _done Event's thread lock as our own lock.
        # Since it's a private copy it should be OK, and it saves some memory.
        # Just be sure that we don't modify the event with the lock held.
        with self._lock:
            if self._state not in (self.S_PENDING, self.S_RUNNING):
                return False
            self._result = Cancelled('cancelled by Future.cancel()')
            self._state = self.S_EXCEPTION
            self._done.set()
            return True

Response: Cancel the execution of the async function if possible.


Instruction:
How would you explain what the following Python 3 function does
def result(self, timeout=None):
        if not self._done.wait(timeout):
            raise Timeout('timeout waiting for future')
        # No more state changes after _done is set so no lock needed.
        if self._state == self.S_EXCEPTION:
            raise compat.saved_exc(self._result)
        return self._result

Response: Wait for the future to complete and return its result.


Instruction:
Write a Python 3 script for
waiting for the async function to complete and return its exception.

Response:
def exception(self, timeout=None):
        """Wait for the async function to complete and return its exception.

        If the function did not raise an exception this returns ``None``.
        """
        if not self._done.wait(timeout):
            raise Timeout('timeout waiting for future')
        if self._state == self.S_EXCEPTION:
            return self._result


Instruction:
Can you tell what is the following Python 3 function doing
def add_done_callback(self, callback, *args):
        with self._lock:
            if self._state not in (self.S_DONE, self.S_EXCEPTION):
                return add_callback(self, callback, args)
        callback(*args)

Response: Add a callback that gets called when the future completes.


Instruction:
How would you explain what the following Python 3 function does
def submit(self, func, *args):
        with self._lock:
            if self._closing:
                raise RuntimeError('pool is closing/closed')
            result = Future()
            self._queue.put_nowait((func, args, result))
            self._spawn_workers()
        return result

Response: Submit a function asynchronously.


Instruction:
How would you implement a function in Python 3 that
applies a function to the elements of the sequences in iterables.

Response:
def map(self, func, *iterables, **kwargs):
        """Apply *func* to the elements of the sequences in *iterables*.

        All invocations of *func* are run in the pool. If multiple iterables
        are provided, then *func* must take this many arguments, and is applied
        with one element from each iterable. All iterables must yield the same
        number of elements.

        An optional *timeout* keyword argument may be provided to specify a
        timeout.

        This returns a generator yielding the results.
        """
        with self._lock:
            if self._closing:
                raise RuntimeError('pool is closing/closed')
            timeout = kwargs.pop('timeout', None)
            futures = []
            for args in zip(*iterables):
                result = Future()
                self._queue.put_nowait((func, args, result))
                futures.append(result)
            self._spawn_workers()
        try:
            with switch_back(timeout):
                for future in futures:
                    yield future.result()
        except Exception:
            # Timeout, GeneratorExit or future.set_exception()
            for future in futures:
                if not future.done():
                    future.cancel()
            raise


Instruction:
Explain what the following Python 3 code does
def close(self):
        with self._lock:
            if self._closing:
                return
            self._closing = True
            if not self._workers:
                self._closed.set()
                return
            self._queue.put_nowait(self._PoolClosing)
        self._closed.wait()

Response: Close the pool and wait for all workers to exit.


Instruction:
Can you generate the documentation for the following Python 3 function
def fingerprint(channel_samples: list, Fs: int = DEFAULT_FS,
                wsize: int = DEFAULT_WINDOW_SIZE,
                wratio: Union[int, float] = DEFAULT_OVERLAP_RATIO,
                fan_value: int = DEFAULT_FAN_VALUE,
                amp_min: Union[int, float] = DEFAULT_AMP_MIN)-> Iterator[tuple]:
    """
    FFT the channel, log transform output, find local maxima, then return
    locally sensitive hashes.
    # """
    # FFT the signal and extract frequency components
    arr2D = mlab.specgram(
        channel_samples,
        NFFT=wsize,
        Fs=Fs,
        window=mlab.window_hanning,
        noverlap=int(wsize * wratio))[0]

    arr2D = 10 * np.log10(arr2D)

    arr2D[arr2D == -np.inf] = 0  # replace infs with zeros

    # find local maxima
    local_maxima = get_2D_peaks(arr2D, plot=False, amp_min=amp_min)

    # return hashes
    return generate_hashes(local_maxima, fan_value=fan_value)

Response: Generate a fingerprint of the given samples.


Instruction:
Explain what the following Python 3 code does
def generate_hashes(peaks, fan_value: int = DEFAULT_FAN_VALUE):
    if PEAK_SORT:
        peaks = sorted(peaks, key=lambda x: x[1])
        # peaks.sort(key=itemgetter(1))

    for i in range(len(peaks)):
        for j in range(1, fan_value):
            if (i + j) < len(peaks):

                freq1 = peaks[i][IDX_FREQ_I]
                freq2 = peaks[i + j][IDX_FREQ_I]
                t1 = peaks[i][IDX_TIME_J]
                t2 = peaks[i + j][IDX_TIME_J]
                t_delta = t2 - t1

                if MIN_HASH_TIME_DELTA <= t_delta <= MAX_HASH_TIME_DELTA:
                    key = "{}|{}|{}".format(freq1, freq2, t_delta)
                    h = hashlib.sha1(key.encode('utf-8'))
                    yield (h.hexdigest()[0:FINGERPRINT_REDUCTION], t1)

Response: Generate a list of hashes for the given list of peaks.


Instruction:
Can you generate the documentation for the following Python 3 function
def do_handshake(self, callback=None):
        if self._state != self.S_UNWRAPPED:
            raise RuntimeError('handshake in progress or completed')
        self._sslobj = sslcompat.wrap_bio(self._context, self._incoming, self._outgoing,
                                          self._server_side, self._server_hostname)
        self._state = self.S_DO_HANDSHAKE
        self._handshake_cb = callback
        ssldata, appdata = self.feed_ssldata(b'')
        assert len(appdata) == 0
        return ssldata

Response: Start the SSL handshake. Return a list of ssldata.


Instruction:
Write a Python 3 script for
starting the SSL shutdown sequence. Return a list of ssldata.

Response:
def shutdown(self, callback=None):
        """Start the SSL shutdown sequence. Return a list of ssldata.

        The optional *callback* argument can be used to install a callback that
        will be called when the shutdown is complete. The callback will be
        called without arguments.
        """
        if self._state == self.S_UNWRAPPED:
            raise RuntimeError('no security layer present')
        self._state = self.S_SHUTDOWN
        self._shutdown_cb = callback
        ssldata, appdata = self.feed_ssldata(b'')
        assert appdata == [] or appdata == [b'']
        return ssldata


Instruction:
Given the following Python 3 function, write the documentation
def feed_eof(self):
        self._incoming.write_eof()
        ssldata, appdata = self.feed_ssldata(b'')
        assert appdata == [] or appdata == [b'']

Response: Send a potentially "ragged" EOF.


Instruction:
Explain what the following Python 3 code does
def feed_ssldata(self, data):
        if self._state == self.S_UNWRAPPED:
            # If unwrapped, pass plaintext data straight through.
            return ([], [data] if data else [])
        ssldata = []; appdata = []
        self._need_ssldata = False
        if data:
            self._incoming.write(data)
        try:
            if self._state == self.S_DO_HANDSHAKE:
                # Call do_handshake() until it doesn't raise anymore.
                self._sslobj.do_handshake()
                self._state = self.S_WRAPPED
                if self._handshake_cb:
                    self._handshake_cb()
            if self._state == self.S_WRAPPED:
                # Main state: read data from SSL until close_notify
                while True:
                    chunk = self._sslobj.read(self.bufsize)
                    appdata.append(chunk)
                    if not chunk:  # close_notify
                        break
            if self._state == self.S_SHUTDOWN:
                # Call shutdown() until it doesn't raise anymore.
                self._sslobj.unwrap()
                self._sslobj = None
                self._state = self.S_UNWRAPPED
                if self._shutdown_cb:
                    self._shutdown_cb()
            if self._state == self.S_UNWRAPPED:
                # Drain possible plaintext data after close_notify.
                appdata.append(self._incoming.read())
        except (ssl.SSLError, sslcompat.CertificateError) as e:
            if getattr(e, 'errno', None) not in (ssl.SSL_ERROR_WANT_READ,
                        ssl.SSL_ERROR_WANT_WRITE, ssl.SSL_ERROR_SYSCALL):
                if self._state == self.S_DO_HANDSHAKE and self._handshake_cb:
                    self._handshake_cb(e)
                raise
            self._need_ssldata = e.errno == ssl.SSL_ERROR_WANT_READ
        # Check for record level data that needs to be sent back.
        # Happens for the initial handshake and renegotiations.
        if self._outgoing.pending:
            ssldata.append(self._outgoing.read())
        return (ssldata, appdata)

Response: Feed SSL record level data into the pipe.


Instruction:
Here you have a function in Python 3, explain what it does
def feed_appdata(self, data, offset=0):
        if self._state == self.S_UNWRAPPED:
            # pass through data in unwrapped mode
            return ([data[offset:]] if offset < len(data) else [], len(data))
        ssldata = []
        view = memoryview(data)
        while True:
            self._need_ssldata = False
            try:
                if offset < len(view):
                    offset += self._sslobj.write(view[offset:])
            except ssl.SSLError as e:
                # It is not allowed to call write() after unwrap() until the
                # close_notify is acknowledged. We return the condition to the
                # caller as a short write.
                if sslcompat.get_reason(e) == 'PROTOCOL_IS_SHUTDOWN':
                    e.errno = ssl.SSL_ERROR_WANT_READ
                if e.errno not in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE,
                                   ssl.SSL_ERROR_SYSCALL):
                    raise
                self._need_ssldata = e.errno == ssl.SSL_ERROR_WANT_READ
            # See if there's any record level data back for us.
            if self._outgoing.pending:
                ssldata.append(self._outgoing.read())
            if offset == len(view) or self._need_ssldata:
                break
        return (ssldata, offset)

Response: Feed plaintext data into the pipe.


Instruction:
Write a Python 3 script for
returning the extra info for the given name.

Response:
def get_extra_info(self, name, default=None):
        """Return transport specific data.

        The following fields are available, in addition to the information
        exposed by :meth:`Transport.get_extra_info`.

        ======================  ===============================================
        Name                    Description
        ======================  ===============================================
        ``'ssl'``               The internal ``ssl.SSLObject`` instance used by
                                this transport.
        ``'sslctx'``            The ``ssl.SSLContext`` instance used to create
                                the SSL object.
        ======================  ===============================================
        """
        if name == 'ssl':
            return self._sslpipe.ssl_object
        elif name == 'sslctx':
            return self._sslpipe.context
        else:
            return super(SslTransport, self).get_extra_info(name, default)


Instruction:
Can you create a Python 3 function that
starts the SSL handshake.

Response:
def do_handshake(self):
        """Start the SSL handshake.

        This method only needs to be called if this transport was created with
        *do_handshake_on_connect* set to False (the default is True).

        The handshake needs to be synchronized between the both endpoints, so
        that SSL record level data is not incidentially interpreted as
        plaintext. Usually this is done by starting the handshake directly
        after a connection is established, but you can also use an application
        level protocol.
        """
        if self._error:
            raise compat.saved_exc(self._error)
        elif self._closing or self._handle.closed:
            raise TransportError('SSL transport is closing/closed')
        self._write_backlog.append([b'', True])
        self._process_write_backlog()


Instruction:
How would you explain what the following Python 3 function does
def unwrap(self):
        if self._error:
            raise compat.saved_exc(self._error)
        elif self._closing or self._handle.closed:
            raise TransportError('SSL transport is closing/closed')
        self._close_on_unwrap = False
        self._write_backlog.append([b'', False])
        self._process_write_backlog()

Response: Unwrap the security layer.


Instruction:
Given the following Python 3 function, write the documentation
def is_locked(lock):
    if hasattr(lock, 'locked'):
        return lock.locked()
    elif hasattr(lock, '_is_owned'):
        return lock._is_owned()
    else:
        raise TypeError('expecting Lock/RLock')

Response: Return whether a lock is locked.


Instruction:
How would you code a function in Python 3 to
acquire a lock and restore its state.

Response:
def acquire_restore(lock, state):
    """Acquire a lock and restore its state."""
    if hasattr(lock, '_acquire_restore'):
        lock._acquire_restore(state)
    elif hasattr(lock, 'acquire'):
        lock.acquire()
    else:
        raise TypeError('expecting Lock/RLock')


Instruction:
Write a Python 3 script for
releasing a lock and return its state.

Response:
def release_save(lock):
    """Release a lock and return its state."""
    if hasattr(lock, '_release_save'):
        return lock._release_save()
    elif hasattr(lock, 'release'):
        lock.release()
    else:
        raise TypeError('expecting Lock/RLock')


Instruction:
How would you implement a function in Python 3 that
returns the thread lock for lock.

Response:
def thread_lock(lock):
    """Return the thread lock for *lock*."""
    if hasattr(lock, '_lock'):
        return lock._lock
    elif hasattr(lock, 'acquire'):
        return lock
    else:
        raise TypeError('expecting Lock/RLock')


Instruction:
Can you implement a function in Python 3 that
acquires the lock. If *blocking* is true (the default), then this will block until the lock can be acquired. The *timeout* parameter specifies an optional timeout in seconds. The return value is a boolean indicating whether the lock was acquired.

Response:
def acquire(self, blocking=True, timeout=None):
        """Acquire the lock.

        If *blocking* is true (the default), then this will block until the
        lock can be acquired. The *timeout* parameter specifies an optional
        timeout in seconds.

        The return value is a boolean indicating whether the lock was acquired.
        """
        hub = get_hub()
        try:
            # switcher.__call__ needs to be synchronized with a lock IF it can
            # be called from different threads. This is the case here because
            # this method may be called from multiple threads and the callbacks
            # are run in the calling thread. So pass it our _lock.
            with switch_back(timeout, lock=self._lock) as switcher:
                with self._lock:
                    if not self._locked:
                        self._locked = 1
                        self._owner = fibers.current()
                        return True
                    elif self._reentrant and self._owner is fibers.current():
                        self._locked += 1
                        return True
                    elif not blocking:
                        return False
                    handle = add_callback(self, switcher)
                # It is safe to call hub.switch() outside the lock. Another
                # thread could have called acquire()+release(), thereby firing
                # the switchback. However the switchback only schedules the
                # switchback in our hub, it won't execute it yet. So the
                # switchback won't actually happen until we switch to the hub.
                hub.switch()
                # Here the lock should be ours because _release() wakes up only
                # the fiber that it passed the lock.
                assert self._locked > 0
                assert self._owner is fibers.current()
        except BaseException as e:
            # Likely a Timeout but could also be e.g. Cancelled
            with self._lock:
                # Clean up the callback. It might have been popped by
                # _release() but that is OK.
                remove_callback(self, handle)
                # This fiber was passed the lock but before that an exception
                # was already scheduled with run_callback() (likely through
                # Fiber.throw())
                if self._owner is fibers.current():
                    self._release()
            if e is switcher.timeout:
                return False
            raise
        return True


Instruction:
Here you have a function in Python 3, explain what it does
def set(self):
        with self._lock:
            if self._flag:
                return
            self._flag = True
            with assert_no_switchpoints():
                run_callbacks(self)

Response: Set the internal flag and wake up any fibers blocked on wait.


Instruction:
Can you generate the documentation for the following Python 3 function
def wait(self, timeout=None):
        # Optimization for the case the Event is already set.
        if self._flag:
            return True
        hub = get_hub()
        try:
            with switch_back(timeout, lock=self._lock) as switcher:
                with self._lock:
                    # Need to check the flag again, now under the lock.
                    if self._flag:
                        return True
                    # Allow other fibers to wake us up via callback in set().
                    # The callback goes to switcher.switch directly() instead of
                    # __call__(), because the latter would try to lock our lock
                    # which is already held when callbacks are run by set().
                    handle = add_callback(self, switcher.switch)
                # See note in Lock.acquire() why we can call to hub.switch()
                # outside the lock.
                hub.switch()
        except BaseException as e:
            with self._lock:
                remove_callback(self, handle)
            if e is switcher.timeout:
                return False
            raise
        return True

Response: Block until the internal flag is set.


Instruction:
Write a Python 3 script for
raising the condition and wake up fibers waiting on it.

Response:
def notify(self, n=1):
        """Raise the condition and wake up fibers waiting on it.

        The optional *n* parameter specifies how many fibers will be notified.
        By default, one fiber is notified.
        """
        if not is_locked(self._lock):
            raise RuntimeError('lock is not locked')
        notified = [0]  # Work around lack of "nonlocal" in py27
        def walker(switcher, predicate):
            if not switcher.active:
                return False  # not not keep switcher that timed out
            if predicate and not predicate():
                return True
            if n >= 0 and notified[0] >= n:
                return True
            switcher.switch()
            notified[0] += 1
            return False  # only notify once
        walk_callbacks(self, walker)


Instruction:
Can you tell what is the following Python 3 function doing
def wait_for(self, predicate, timeout=None):
        if not is_locked(self._lock):
            raise RuntimeError('lock is not locked')
        hub = get_hub()
        try:
            with switch_back(timeout, lock=thread_lock(self._lock)) as switcher:
                handle = add_callback(self, switcher, predicate)
                # See the comment in Lock.acquire() why it is OK to release the
                # lock here before calling hub.switch().
                # Also if this is a reentrant lock make sure it is fully released.
                state = release_save(self._lock)
                hub.switch()
        except BaseException as e:
            with self._lock:
                remove_callback(self, handle)
            if e is switcher.timeout:
                return False
            raise
        finally:
            acquire_restore(self._lock, state)
        return True

Response: Like wait but additionally for predicate to be true.


Instruction:
Can you implement a function in Python 3 that
puts an item into the queue.

Response:
def put(self, item, block=True, timeout=None, size=None):
        """Put *item* into the queue.

        If the queue is currently full and *block* is True (the default), then
        wait up to *timeout* seconds for space to become available. If no
        timeout is specified, then wait indefinitely.

        If the queue is full and *block* is False or a timeout occurs, then
        raise a :class:`QueueFull` exception.

        The optional *size* argument may be used to specify a custom size for
        the item. The total :meth:`qsize` of the queue is the sum of the sizes
        of all the items. The default size for an item is 1.
        """
        if size is None:
            size = 1
        with self._lock:
            priority = self._get_item_priority(item)
            while self._size + size > self.maxsize > 0:
                if not block:
                    raise QueueFull
                if not self._notfull.wait_for(lambda: self._size+size <= self.maxsize, timeout):
                    raise QueueFull
            heapq.heappush(self._heap, (priority, size, item))
            self._size += size
            self._unfinished_tasks += 1
            self._notempty.notify()


Instruction:
Implement a function in Python 3 to
pop an item from the queue.

Response:
def get(self, block=True, timeout=None):
        """Pop an item from the queue.

        If the queue is not empty, an item is returned immediately. Otherwise,
        if *block* is True (the default), wait up to *timeout* seconds for an
        item to become available. If not timeout is provided, then wait
        indefinitely.

        If the queue is empty and *block* is false or a timeout occurs, then
        raise a :class:`QueueEmpty` exception.
        """
        with self._lock:
            while not self._heap:
                if not block:
                    raise QueueEmpty
                if not self._notempty.wait(timeout):
                    raise QueueEmpty
            prio, size, item = heapq.heappop(self._heap)
            self._size -= size
            if 0 <= self._size < self.maxsize:
                self._notfull.notify()
        return item


Instruction:
How would you code a function in Python 3 to
mark a task as done.

Response:
def task_done(self):
        """Mark a task as done."""
        with self._lock:
            unfinished = self._unfinished_tasks - 1
            if unfinished < 0:
                raise RuntimeError('task_done() called too many times')
            elif unfinished == 0:
                self._alldone.notify()
            self._unfinished_tasks = unfinished


Instruction:
Write a Python 3 script to
spawn a new child process.

Response:
def spawn(self, args, executable=None, stdin=None, stdout=None, stderr=None,
              shell=False, cwd=None, env=None, flags=0, extra_handles=None):
        """Spawn a new child process.

        The executable to spawn and its arguments are determined by *args*,
        *executable* and *shell*.

        When *shell* is set to ``False`` (the default), *args* is normally a
        sequence and it contains both the program to execute (at index 0), and
        its arguments.

        When *shell* is set to ``True``, then *args* is normally a string and
        it indicates the command to execute through the shell.

        The *executable* argument can be used to override the executable to
        execute. If *shell* is ``False``, it overrides ``args[0]``. This is
        sometimes used on Unix to implement "fat" executables that behave
        differently based on argv[0]. If *shell* is ``True``, it overrides the
        shell to use. The default shell is ``'/bin/sh'`` on Unix, and the value
        of $COMSPEC (or ``'cmd.exe'`` if it is unset) on Windows.

        The *stdin*, *stdout* and *stderr* arguments specify how to handle
        standard input, output, and error, respectively. If set to None, then
        the child will inherit our respective stdio handle. If set to the
        special constant ``PIPE`` then a pipe is created. The pipe will be
        connected to a :class:`gruvi.StreamProtocol` which you can use to read
        or write from it. The stream protocol instance is available under
        either :attr:`stdin`, :attr:`stdout` or :attr:`stderr`. All 3 stdio
        arguments can also be a file descriptor, a file-like object, or a pyuv
        ``Stream`` instance.

        The *extra_handles* specifies any extra handles to pass to the client.
        It must be a sequence where each element is either a file descriptor, a
        file-like objects, or a ``pyuv.Stream`` instance. The position in the
        sequence determines the file descriptor in the client. The first
        position corresponds to FD 3, the second to 4, etc. This places these
        file descriptors directly after the stdio handles.

        The *cwd* argument specifies the directory to change to before
        executing the child. If not provided, the current directory is used.

        The *env* argument specifies the environment to use when executing the
        child. If provided, it must be a dictionary. By default, the current
        environment is used.

        The *flags* argument can be used to specify optional libuv
        ``uv_process_flags``. The only relevant flags are
        ``pyuv.UV_PROCESS_DETACHED`` and ``pyuv.UV_PROCESS_WINDOWS_HIDE``. Both
        are Windows specific and are silently ignored on Unix.
        """
        if self._process:
            raise RuntimeError('child process already spawned')
        self._child_exited.clear()
        self._closed.clear()
        self._exit_status = None
        self._term_signal = None
        hub = get_hub()
        if isinstance(args, str):
            args = [args]
            flags |= pyuv.UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS
        else:
            args = list(args)
        if shell:
            if hasattr(os, 'fork'):
                # Unix
                if executable is None:
                    executable = '/bin/sh'
                args = [executable, '-c'] + args
            else:
                # Windows
                if executable is None:
                    executable = os.environ.get('COMSPEC', 'cmd.exe')
                args = [executable, '/c'] + args
        if executable is None:
            executable = args[0]
        kwargs = {}
        if env is not None:
            kwargs['env'] = env
        if cwd is not None:
            kwargs['cwd'] = cwd
        kwargs['flags'] = flags
        handles = self._get_child_handles(hub.loop, stdin, stdout, stderr, extra_handles)
        kwargs['stdio'] = handles
        process = pyuv.Process.spawn(hub.loop, args, executable,
                                     exit_callback=self._on_child_exit, **kwargs)
        # Create stdin/stdout/stderr transports/protocols.
        if handles[0].stream:
            self._stdin = self._connect_child_handle(handles[0])
        if handles[1].stream:
            self._stdout = self._connect_child_handle(handles[1])
        if handles[2].stream:
            self._stderr = self._connect_child_handle(handles[2])
        self._process = process


Instruction:
Explain what the following Python 3 code does
def close(self):
        if self._process is None:
            return
        waitfor = []
        if not self._process.closed:
            self._process.close(self._on_close_complete)
            waitfor.append(self._closed)
        # For each of stdin/stdout/stderr, close the transport. This schedules
        # an on-close callback that will close the protocol, which we wait for.
        if self._stdin:
            self._stdin[1].close()
            waitfor.append(self._stdin[1]._closed)
        if self._stdout:
            self._stdout[1].close()
            waitfor.append(self._stdout[1]._closed)
        if self._stderr:
            self._stderr[1].close()
            waitfor.append(self._stderr[1]._closed)
        futures.wait(waitfor)
        self._process = None
        self._stdin = self._stdout = self._stderr = None

Response: Close the process and frees its associated resources.


Instruction:
Make a summary of the following Python 3 code
def send_signal(self, signum):
        if self._process is None:
            raise RuntimeError('no child process')
        self._process.kill(signum)

Response: Send the signal signum to the child process.


Instruction:
Here you have a function in Python 3, explain what it does
def terminate(self):
        try:
            self.send_signal(signal.SIGTERM)
        except pyuv.error.ProcessError as e:
            if e.args[0] != pyuv.errno.UV_ESRCH:
                raise

Response: Terminate the child process.


Instruction:
Can you create a Python 3 function that
waits for the child process to exit.

Response:
def wait(self, timeout=-1):
        """Wait for the child to exit.

        Wait for at most *timeout* seconds, or indefinitely if *timeout* is
        None. Return the value of the :attr:`returncode` attribute.
        """
        if self._process is None:
            raise RuntimeError('no child process')
        if timeout == -1:
            timeout = self._timeout
        if not self._child_exited.wait(timeout):
            raise Timeout('timeout waiting for child to exit')
        return self.returncode


Instruction:
Implement a function in Python 3 to
communicate with the child and return its output.

Response:
def communicate(self, input=None, timeout=-1):
        """Communicate with the child and return its output.

        If *input* is provided, it is sent to the client. Concurrent with
        sending the input, the child's standard output and standard error are
        read, until the child exits.

        The return value is a tuple ``(stdout_data, stderr_data)`` containing
        the data read from standard output and standard error.
        """
        if self._process is None:
            raise RuntimeError('no child process')
        if timeout == -1:
            timeout = self._timeout
        output = [[], []]
        def writer(stream, data):
            offset = 0
            while offset < len(data):
                buf = data[offset:offset+4096]
                stream.write(buf)
                offset += len(buf)
            stream.close()
        def reader(stream, data):
            while True:
                if self._encoding:
                    buf = stream.read(4096)
                else:
                    buf = stream.read1()
                if not buf:
                    break
                data.append(buf)
        if self.stdin:
            fibers.spawn(writer, self.stdin, input or b'')
        if self.stdout:
            fibers.spawn(reader, self.stdout, output[0])
        if self.stderr:
            fibers.spawn(reader, self.stderr, output[1])
        self.wait(timeout)
        empty = '' if self._encoding else b''
        stdout_data = empty.join(output[0])
        stderr_data = empty.join(output[1])
        return (stdout_data, stderr_data)


Instruction:
Write a Python 3 script to
parse a requirements. txt file and return as a list.

Response:
def get_requirements():
    """Parse a requirements.txt file and return as a list."""
    with open(os.path.join(topdir, 'requirements.txt')) as fin:
        lines = fin.readlines()
    lines = [line.strip() for line in lines]
    return lines


Instruction:
Explain what the following Python 3 code does
def remove(self, node):
        if not isinstance(node, Node):
            raise TypeError('expecting Node instance')
        if node._list is None:
            return
        if node._list is not self:
            raise RuntimeError('node is not contained in list')
        if node._next is None:
            self._last = node._prev  # last node
        else:
            node._next._prev = node._prev
        if node._prev is None:
            self._first = node._next  # first node
        else:
            node._prev._next = node._next
        node._list = node._prev = node._next = None
        self._size -= 1

Response: Removes a node from the list.


Instruction:
Write a Python 3 function that can
insert a new node into the list.

Response:
def insert(self, node, before=None):
        """Insert a new node in the list.

        If *before* is specified, the new node is inserted before this node.
        Otherwise, the node is inserted at the end of the list.
        """
        node._list = self
        if self._first is None:
            self._first = self._last = node  # first node in list
            self._size += 1
            return node
        if before is None:
            self._last._next = node  # insert as last node
            node._prev = self._last
            self._last = node
        else:
            node._next = before
            node._prev = before._prev
            if node._prev:
                node._prev._next = node
            else:
                self._first = node  # inserting as first node
            node._next._prev = node
        self._size += 1
        return node


Instruction:
Can you write a function in Python 3 where it
removes all nodes from the list.

Response:
def clear(self):
        """Remove all nodes from the list."""
        node = self._first
        while node is not None:
            next_node = node._next
            node._list = node._prev = node._next = None
            node = next_node
        self._size = 0


Instruction:
Can you implement a function in Python 3 that
builds a code map for all of the 256 - color variants.

Response:
def _build_codes() -> Dict[str, Dict[str, str]]:
    """ Build code map, encapsulated to reduce module-level globals. """
    built = {
        'fore': {},
        'back': {},
        'style': {},
    }  # type: Dict[str, Dict[str, str]]

    # Set codes for forecolors (30-37) and backcolors (40-47)
    # Names are given to some of the 256-color variants as 'light' colors.
    for name, number in _namemap:
        # Not using format_* functions here, no validation needed.
        built['fore'][name] = codeformat(30 + number)
        built['back'][name] = codeformat(40 + number)
        litename = 'light{}'.format(name)  # type: str
        built['fore'][litename] = codeformat(90 + number)
        built['back'][litename] = codeformat(100 + number)

    # Set reset codes for fore/back.
    built['fore']['reset'] = codeformat(39)
    built['back']['reset'] = codeformat(49)

    # Set style codes.
    for code, names in _stylemap:
        for alias in names:
            built['style'][alias] = codeformat(code)

    # Extended (256 color codes)
    for i in range(256):
        built['fore'][str(i)] = extforeformat(i)
        built['back'][str(i)] = extbackformat(i)

    return built


Instruction:
Create a Python 3 function to
build a reverse escape - code to name map based on an existing name to escape - code map.

Response:
def _build_codes_reverse(
        codes: Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, str]]:
    """ Build a reverse escape-code to name map, based on an existing
        name to escape-code map.
    """
    built = {}  # type: Dict[str, Dict[str, str]]
    for codetype, codemap in codes.items():
        for name, escapecode in codemap.items():
            # Skip shorcut aliases to avoid overwriting long names.
            if len(name) < 2:
                continue
            if built.get(codetype, None) is None:
                built[codetype] = {}
            built[codetype][escapecode] = name
    return built


Instruction:
Can you tell what is the following Python 3 function doing
def auto_disable(
        enabled: Optional[bool] = True,
        fds: Optional[Sequence[IO]] = (sys.stdout, sys.stderr)) -> None:
    """ Automatically decide whether to disable color codes if stdout or
        stderr are not ttys.

        Arguments:
            enabled  : Whether to automatically disable color codes.
                       When set to True, the fds will be checked for ttys.
                       When set to False, enable() is called.
            fds      : Open file descriptors to check for ttys.
                       If any non-ttys are found, colors will be disabled.
                       Objects must have a isatty() method.
    """
    if enabled:
        if not all(getattr(f, 'isatty', lambda: False)() for f in fds):
            disable()
    else:
        enable()

Response: Automatically disables color codes if stdout or stderr are not ttys.


Instruction:
Can you generate the documentation for the following Python 3 function
def _format_code(
        number: FormatArg,
        backcolor: Optional[bool] = False,
        light: Optional[bool] = False,
        extended: Optional[bool] = False) -> str:
    """ Return an escape code for a fore/back color, by number.
        This is a convenience method for handling the different code types
        all in one shot.
        It also handles some validation.
        format_fore/format_back wrap this function to reduce code duplication.

        Arguments:
            number    : Integer or RGB tuple to format into an escape code.
            backcolor : Whether this is for a back color, otherwise it's fore.
            light     : Whether this should be a 'light' color.
            extended  : Whether this should be an extended (256) color.

        If `light` and `extended` are both given, only `light` is used.
    """
    if backcolor:
        codetype = 'back'
        # A dict of codeformat funcs. These funcs return an escape code str.
        formatters = {
            'code': lambda n: codeformat(40 + n),
            'lightcode': lambda n: codeformat(100 + n),
            'ext': lambda n: extbackformat(n),
            'rgb': lambda r, g, b: rgbbackformat(r, g, b),
        }  # type: Dict[str, Callable[..., str]]
    else:
        codetype = 'fore'
        formatters = {
            'code': lambda n: codeformat(30 + n),
            'lightcode': lambda n: codeformat(90 + n),
            'ext': lambda n: extforeformat(n),
            'rgb': lambda r, g, b: rgbforeformat(r, g, b),
        }
    try:
        r, g, b = (int(x) for x in number)  # type: ignore
    except (TypeError, ValueError):
        # Not an rgb code.
        # This variable, and it's cast is only to satisfy the type checks.
        try:
            n = int(cast(int, number))
        except ValueError:
            # Not an rgb code, or a valid code number.
            raise InvalidColr(
                number,
                'Expecting RGB or 0-255 for {} code.'.format(codetype)
            )
        if light:
            if not in_range(n, 0, 9):
                raise InvalidColr(
                    n,
                    'Expecting 0-9 for light {} code.'.format(codetype)
                )
            return formatters['lightcode'](n)
        elif extended:
            if not in_range(n, 0, 255):
                raise InvalidColr(
                    n,
                    'Expecting 0-255 for ext. {} code.'.format(codetype)
                )
            return formatters['ext'](n)

        if not in_range(n, 0, 9):
            raise InvalidColr(
                n,
                'Expecting 0-9 for {} code.'.format(codetype)
            )
        return formatters['code'](n)

    # Rgb code.
    try:
        if not all(in_range(x, 0, 255) for x in (r, g, b)):
            raise InvalidColr(
                (r, g, b),
                'RGB value for {} not in range 0-255.'.format(codetype)
            )
    except TypeError:
        # Was probably a 3-char string. Not an rgb code though.
        raise InvalidColr(
            (r, g, b),
            'RGB value for {} contains invalid number.'.format(codetype)
        )
    return formatters['rgb'](r, g, b)

Response: Return an escape code for a specific color by number.


Instruction:
Here you have a function in Python 3, explain what it does
def format_back(
        number: FormatArg,
        light: Optional[bool] = False,
        extended: Optional[bool] = False) -> str:
    """ Return an escape code for a back color, by number.
        This is a convenience method for handling the different code types
        all in one shot.
        It also handles some validation.
    """
    return _format_code(
        number,
        backcolor=True,
        light=light,
        extended=extended
    )

Response: Return an escape code for a back color by number.


Instruction:
Create a Python 3 function for
returning an escape code for a fore color by number.

Response:
def format_fore(
        number: FormatArg,
        light: Optional[bool] = False,
        extended: Optional[bool] = False) -> str:
    """ Return an escape code for a fore color, by number.
        This is a convenience method for handling the different code types
        all in one shot.
        It also handles some validation.
    """
    return _format_code(
        number,
        backcolor=False,
        light=light,
        extended=extended
    )


Instruction:
Create a Python 3 function for
returning an escape code for a style by number.

Response:
def format_style(number: int) -> str:
    """ Return an escape code for a style, by number.
        This handles invalid style numbers.
    """
    if str(number) not in _stylenums:
        raise InvalidStyle(number)
    return codeformat(number)


Instruction:
Write a Python 3 script for
retrieving a tuple of all known color names basic and known names.

Response:
def get_all_names() -> Tuple[str]:
    """ Retrieve a tuple of all known color names, basic and 'known names'.
    """
    names = list(basic_names)
    names.extend(name_data)
    return tuple(sorted(set(names)))


Instruction:
Write a Python 3 function for
getting the code number from an escape code string.

Response:
def get_code_num(s: str) -> Optional[int]:
    """ Get code number from an escape code.
        Raises InvalidEscapeCode if an invalid number is found.
    """
    if ';' in s:
        # Extended fore/back codes.
        numberstr = s.rpartition(';')[-1][:-1]
    else:
        # Fore, back, style, codes.
        numberstr = s.rpartition('[')[-1][:-1]

    num = try_parse_int(
        numberstr,
        default=None,
        minimum=0,
        maximum=255
    )
    if num is None:
        raise InvalidEscapeCode(numberstr)
    return num


Instruction:
Create a Python 3 function to
get rgb code numbers from an RGB escape code.

Response:
def get_code_num_rgb(s: str) -> Optional[Tuple[int, int, int]]:
    """ Get rgb code numbers from an RGB escape code.
        Raises InvalidRgbEscapeCode if an invalid number is found.
    """
    parts = s.split(';')
    if len(parts) != 5:
        raise InvalidRgbEscapeCode(s, reason='Count is off.')
    rgbparts = parts[-3:]
    if not rgbparts[2].endswith('m'):
        raise InvalidRgbEscapeCode(s, reason='Missing \'m\' on the end.')

    rgbparts[2] = rgbparts[2].rstrip('m')
    try:
        r, g, b = [int(x) for x in rgbparts]
    except ValueError as ex:
        raise InvalidRgbEscapeCode(s) from ex

    if not all(in_range(x, 0, 255) for x in (r, g, b)):
        raise InvalidRgbEscapeCode(s, reason='Not in range 0-255.')
    return r, g, b


Instruction:
Make a summary of the following Python 3 code
def get_known_codes(
        s: Union[str, 'Colr'],
        unique: Optional[bool] = True,
        rgb_mode: Optional[bool] = False):
    """ Get all known escape codes from a string, and yield the explanations.
    """

    isdisabled = disabled()
    orderedcodes = tuple((c, get_known_name(c)) for c in get_codes(s))
    codesdone = set()  # type: Set[str]

    for code, codeinfo in orderedcodes:
        # Do the codes in order, but don't do the same code twice.

        if unique:
            if code in codesdone:
                continue
            codesdone.add(code)

        if codeinfo is None:
            continue
        codetype, name = codeinfo

        typedesc = '{:>13}: {!r:<23}'.format(codetype.title(), code)
        if codetype.startswith(('extended', 'rgb')):
            if isdisabled:
                codedesc = str(ColorCode(name, rgb_mode=rgb_mode))
            else:
                codedesc = ColorCode(name, rgb_mode=rgb_mode).example()
        else:
            codedesc = ''.join((
                code,
                str(name).lstrip('(').rstrip(')'),
                codes['style']['reset_all']
            ))

        yield ' '.join((
            typedesc,
            codedesc
        ))

Response: Get all known escape codes from a string and yield the explanations.


Instruction:
Make a summary of the following Python 3 code
def get_known_name(s: str) -> Optional[Tuple[str, ColorArg]]:

    if not s.endswith('m'):
        # All codes end with 'm', so...
        return None
    if s.startswith('\033[38;5;'):
        # Extended fore.
        name = codes_reverse['fore'].get(s, None)
        if name is None:
            num = get_code_num(s)
            return ('extended fore', num)
        else:
            return ('extended fore', name)
    elif s.startswith('\033[48;5;'):
        # Extended back.
        name = codes_reverse['back'].get(s, None)
        if name is None:
            num = get_code_num(s)
            return ('extended back', num)
        else:
            return ('extended back', name)
    elif s.startswith('\033[38;2'):
        # RGB fore.
        vals = get_code_num_rgb(s)
        if vals is not None:
            return ('rgb fore', vals)
    elif s.startswith('\033[48;2'):
        # RGB back.
        vals = get_code_num_rgb(s)
        if vals is not None:
            return ('rgb back', vals)
    elif s.startswith('\033['):
        # Fore, back, style.
        number = get_code_num(s)
        # Get code type based on number.
        if (number <= 7) or (number == 22):
            codetype = 'style'
        elif (((number >= 30) and (number < 40)) or
                ((number >= 90) and (number < 100))):
            codetype = 'fore'
        elif (((number >= 40) and (number < 50)) or
                ((number >= 100) and (number < 110))):
            codetype = 'back'
        else:
            raise InvalidEscapeCode(
                number,
                'Expecting 0-7, 22, 30-39, or 40-49 for escape code',
            )

        name = codes_reverse[codetype].get(s, None)
        if name is not None:
            return (codetype, name)

    # Not a known escape code.
    return None

Response: Reverse translate a terminal code to a known color name.


Instruction:
Here you have a function in Python 3, explain what it does
def in_range(x: int, minimum: int, maximum: int) -> bool:
    return (x >= minimum and x <= maximum)

Response: Return True if x is in the given range.


Instruction:
Given the following Python 3 function, write the documentation
def parse_colr_arg(
        s: str,
        default: Optional[Any] = None,
        rgb_mode: Optional[bool] = False) -> ColorArg:
    """ Parse a user argument into a usable fore/back color value for Colr.
        If a falsey value is passed, default is returned.
        Raises InvalidColr if the argument is unusable.
        Returns: A usable value for Colr(fore/back).

        This validates basic/extended color names.
        This validates the range for basic/extended values (0-255).
        This validates the length/range for rgb values (0-255, 0-255, 0-255).

        Arguments:
            s  : User's color value argument.
                 Example: "1", "255", "black", "25,25,25"
    """
    if not s:
        return default

    val = s.strip().lower()
    try:
        # Try as int.
        intval = int(val)
    except ValueError:
        # Try as rgb.
        try:
            r, g, b = (int(x.strip()) for x in val.split(','))
        except ValueError:
            if ',' in val:
                # User tried rgb value and failed.
                raise InvalidColr(val)

            # Try as name (fore/back have the same names)
            code = codes['fore'].get(val, None)
            if code:
                # Valid basic code from fore, bask, or style.
                return val

            # Not a basic code, try known names.
            named_data = name_data.get(val, None)
            if named_data is not None:
                # A known named color.
                return val

            # Not a basic/extended/known name, try as hex.
            try:
                if rgb_mode:
                    return hex2rgb(val, allow_short=True)
                return hex2termhex(val, allow_short=True)
            except ValueError:
                raise InvalidColr(val)
        else:
            # Got rgb. Do some validation.
            if not all((in_range(x, 0, 255) for x in (r, g, b))):
                raise InvalidColr(val)
            # Valid rgb.
            return r, g, b
    else:
        # Int value.
        if not in_range(intval, 0, 255):
            # May have been a hex value confused as an int.
            if len(val) in (3, 6):
                try:
                    if rgb_mode:
                        return hex2rgb(val, allow_short=True)
                    return hex2termhex(val, allow_short=True)
                except ValueError:
                    raise InvalidColr(val)
            raise InvalidColr(intval)
        # Valid int value.
        return intval

Response: Parses a user argument into a usable color value for the given color.


Instruction:
Create a Python 3 function for
trying parsing a string into an integer.

Response:
def try_parse_int(
        s: str,
        default: Optional[Any] = None,
        minimum: Optional[int] = None,
        maximum: Optional[int] = None) -> Optional[Any]:
    """ Try parsing a string into an integer.
        On failure, return `default`.
        If the number is less then `minimum` or greater than `maximum`,
        return `default`.
        Returns an integer on success.
    """
    try:
        n = int(s)
    except ValueError:
        return default
    if (minimum is not None) and (n < minimum):
        return default
    elif (maximum is not None) and (n > maximum):
        return default
    return n


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

        if attr in codes['fore']:
            # Fore method
            return partial(self.chained, fore=attr)
        elif attr in codes['style']:
            # Style method
            return partial(self.chained, style=attr)
        elif attr.startswith('bg'):
            # Back method
            name = attr[2:].lstrip('_')
            if name in codes['back']:
                return partial(self.chained, back=name)
        elif attr.startswith(('b256_', 'b_')):
            # Back 256 method
            # Remove the b256_ portion.
            name = attr.partition('_')[2]
            return self._ext_attr_to_partial(name, 'back')
        elif attr.startswith(('f256_', 'f_')):
            # Fore 256 method
            name = attr.partition('_')[2]
            return self._ext_attr_to_partial(name, 'fore')

        return None

Response: Return the correct color function by method name.


Instruction:
How would you implement a function in Python 3 that
calls the Colr class method on an object.

Response:
def _call_dunder_colr(cls, obj):
        """ Call __colr__ on an object, after some checks.
            If color is disabled, the object itself is returned.
            If __colr__ doesn't return a Colr instance, TypeError is raised.
            On success, a Colr instance is returned from obj.__colr__().
        """
        if _disabled:
            # No colorization when disabled. Just use str.
            return obj
        clr = obj.__colr__()
        if not isinstance(clr, cls):
            # __colr__ should always return a Colr.
            # Future development may assume a Colr was returned.
            raise TypeError(
                ' '.join((
                    '__colr__ methods should return a {} instance.',
                    'Got: {}',
                )).format(
                    cls.__name__,
                    type(clr).__name__,
                )
            )
        return clr


Instruction:
Can you write a function in Python 3 where it
converts a string like 2223 or aliceblue into a partial for self. chained.

Response:
def _ext_attr_to_partial(self, name, kwarg_key):
        """ Convert a string like '233' or 'aliceblue' into partial for
            self.chained.
        """
        try:
            intval = int(name)
        except ValueError:
            # Try as an extended name_data name.
            info = name_data.get(name, None)
            if info is None:
                # Not an int value or name_data name.
                return None
            kws = {kwarg_key: info['code']}
            return partial(self.chained, **kws)
        # Integer str passed, use the int value.
        kws = {kwarg_key: intval}
        return partial(self.chained, **kws)


Instruction:
How would you explain what the following Python 3 function does
def _gradient_black_line(
            self, text, start, step=1,
            fore=None, back=None, style=None, reverse=False, rgb_mode=False):
        """ Yield colorized characters,
            within the 24-length black gradient.
        """
        if start < 232:
            start = 232
        elif start > 255:
            start = 255
        if reverse:
            codes = list(range(start, 231, -1))
        else:
            codes = list(range(start, 256))
        return ''.join((
            self._iter_text_wave(
                text,
                codes,
                step=step,
                fore=fore,
                back=back,
                style=style,
                rgb_mode=rgb_mode
            )
        ))

Response: Yields black lines within the 24 - length black gradient.


Instruction:
How would you implement a function in Python 3 that
yields colorized characters within the 24 - length black gradient.

Response:
def _gradient_black_lines(
            self, text, start, step=1,
            fore=None, back=None, style=None, reverse=False,
            movefactor=2, rgb_mode=False):
        """ Yield colorized characters,
            within the 24-length black gradient,
            treating each line separately.
        """
        if not movefactor:
            def factor(i):
                return start
        else:
            # Increase the start for each line.
            def factor(i):
                return start + (i * movefactor)
        return '\n'.join((
            self._gradient_black_line(
                line,
                start=factor(i),
                step=step,
                fore=fore,
                back=back,
                style=style,
                reverse=reverse,
                rgb_mode=rgb_mode,
            )
            for i, line in enumerate(text.splitlines())
        ))


Instruction:
Explain what the following Python 3 code does
def _gradient_rgb_line(
            self, text, start, stop, step=1,
            fore=None, back=None, style=None):
        """ Yield colorized characters, morphing from one rgb value to
            another.
        """
        return self._gradient_rgb_line_from_morph(
            text,
            list(self._morph_rgb(start, stop, step=step)),
            fore=fore,
            back=back,
            style=style
        )

Response:
Yield colorized characters from one rgb value to
            another.


Instruction:
Here you have a function in Python 3, explain what it does
def _gradient_rgb_line_from_morph(
            self, text, morphlist, fore=None, back=None, style=None):
        """ Yield colorized characters, morphing from one rgb value to
            another.
        """
        try:
            listlen = len(morphlist)
        except TypeError:
            morphlist = list(morphlist)
            listlen = len(morphlist)
        neededsteps = listlen // len(text)
        iterstep = 1
        if neededsteps > iterstep:
            # Skip some members of morphlist, to be sure to reach the end.
            iterstep = neededsteps
        usevals = morphlist
        if iterstep > 1:
            # Rebuild the morphlist, skipping some.
            usevals = [usevals[i] for i in range(0, listlen, iterstep)]
        return ''.join((
            self._iter_text_wave(
                text,
                usevals,
                fore=fore,
                back=back,
                style=style,
                rgb_mode=False,
            )
        ))

Response:
Yield colorized characters from one rgb value to
            another.


Instruction:
Explain what the following Python 3 code does
def _gradient_rgb_lines(
            self, text, start, stop, step=1,
            fore=None, back=None, style=None, movefactor=None):
        """ Yield colorized characters, morphing from one rgb value to
            another. This treats each line separately.
        """
        morphlist = list(self._morph_rgb(start, stop, step=step))
        if movefactor:
            # Moving means we need the morph to wrap around.
            morphlist.extend(self._morph_rgb(stop, start, step=step))
            if movefactor < 0:
                # Increase the start for each line.
                def move():
                    popped = []
                    for _ in range(abs(movefactor)):
                        try:
                            popped.append(morphlist.pop(0))
                        except IndexError:
                            pass
                    morphlist.extend(popped)
                    return morphlist
            else:
                # Decrease start for each line.
                def move():
                    for _ in range(movefactor):
                        try:
                            val = morphlist.pop(-1)
                        except IndexError:
                            pass
                        else:
                            morphlist.insert(0, val)
                    return morphlist

        return '\n'.join((
            self._gradient_rgb_line_from_morph(
                line,
                move() if movefactor else morphlist,
                fore=fore,
                back=back,
                style=style,
            )
            for i, line in enumerate(text.splitlines())
        ))

Response:
Yield colorized characters from one rgb value to
            another.


Instruction:
Write a Python 3 function for
yielding colorized characters from text using a wave of numbers.

Response:
def _iter_text_wave(
            self, text, numbers, step=1,
            fore=None, back=None, style=None, rgb_mode=False):
        """ Yield colorized characters from `text`, using a wave of `numbers`.
            Arguments:
                text      : String to be colorized.
                numbers   : A list/tuple of numbers (256 colors).
                step      : Number of characters to colorize per color.
                fore      : Fore color to use (name or number).
                            (Back will be gradient)
                back      : Background color to use (name or number).
                            (Fore will be gradient)
                style     : Style name to use.
                rgb_mode  : Use number for rgb value.
                            This should never be used when the numbers
                            are rgb values themselves.
        """
        if fore and back:
            raise ValueError('Both fore and back colors cannot be specified.')

        pos = 0
        end = len(text)
        numbergen = self._iter_wave(numbers)

        def make_color(n):
            try:
                r, g, b = n
            except TypeError:
                if rgb_mode:
                    return n, n, n
                return n
            return r, g, b

        for value in numbergen:
            lastchar = pos + step
            yield self.color(
                text[pos:lastchar],
                fore=make_color(value) if fore is None else fore,
                back=make_color(value) if fore is not None else back,
                style=style
            )
            if lastchar >= end:
                numbergen.send(True)
            pos = lastchar


Instruction:
Can you tell what is the following Python 3 function doing
def _iter_wave(iterable, count=0):
        up = True
        pos = 0
        i = 0
        try:
            end = len(iterable)
        except TypeError:
            iterable = list(iterable)
            end = len(iterable)

        # Stop on count, or run forever.
        while (i < count) if count > 0 else True:
            try:
                stop = yield iterable[pos]
                # End of generator (user sent the stop signal)
                if stop:
                    break
            except IndexError:
                # End of iterable, when len(iterable) is < count.
                up = False

            # Change directions if needed, otherwise increment/decrement.
            if up:
                pos += 1
                if pos == end:
                    up = False
                    pos = end - 2
            else:
                pos -= 1
                if pos < 0:
                    up = True
                    pos = 1
            i += 1

Response: A generator that yields the given iterable of A B C and D.


Instruction:
How would you explain what the following Python 3 function does
def _morph_rgb(self, rgb1, rgb2, step=1):
        pos1, pos2 = list(rgb1), list(rgb2)
        indexes = [i for i, _ in enumerate(pos1)]

        def step_value(a, b):
            """ Returns the amount to add to `a` to make it closer to `b`,
                multiplied by `step`.
            """
            if a < b:
                return step
            if a > b:
                return -step
            return 0

        steps = [step_value(pos1[x], pos2[x]) for x in indexes]
        stepcnt = 0
        while (pos1 != pos2):
            stepcnt += 1
            stop = yield tuple(pos1)
            if stop:
                break
            for x in indexes:
                if pos1[x] != pos2[x]:
                    pos1[x] += steps[x]
                    if (steps[x] < 0) and (pos1[x] < pos2[x]):
                        # Over stepped, negative.
                        pos1[x] = pos2[x]
                    if (steps[x] > 0) and (pos1[x] > pos2[x]):
                        # Over stepped, positive.
                        pos1[x] = pos2[x]
        yield tuple(pos1)

Response: Yields the rgb value from rgb1 to rgb2 in a loop.


Instruction:
Can you tell what is the following Python 3 function doing
def _rainbow_hex_chars(self, s, freq=0.1, spread=3.0, offset=0):
        return (
            (c, self._rainbow_color(freq, offset + i / spread))
            for i, c in enumerate(s)
        )

Response: Iterate over characters in a string to build data needed for rainbow effect.


Instruction:
Write a Python 3 script to
create a rainbow line from a string.

Response:
def _rainbow_line(
            self, text, freq=0.1, spread=3.0, offset=0,
            rgb_mode=False, **colorargs):
        """ Create rainbow using the same offset for all text.
            Arguments:
                text       : String to colorize.
                freq       : Frequency/"tightness" of colors in the rainbow.
                             Best results when in the range 0.0-1.0.
                             Default: 0.1
                spread     : Spread/width of colors.
                             Default: 3.0
                offset     : Offset for start of rainbow.
                             Default: 0
                rgb_mode   : If truthy, use RGB escape codes instead of
                             extended 256 and approximate hex match.
            Keyword Arguments:
                colorargs  : Any extra arguments for the color function,
                             such as fore, back, style.
                             These need to be treated carefully to not
                            'overwrite' the rainbow codes.
        """
        fore = colorargs.get('fore', None)
        back = colorargs.get('back', None)
        style = colorargs.get('style', None)
        if fore:
            color_args = (lambda value: {
                'back': value if rgb_mode else hex2term(value),
                'style': style,
                'fore': fore
            })
        else:
            color_args = (lambda value: {
                'fore': value if rgb_mode else hex2term(value),
                'style': style,
                'back': back
            })

        if rgb_mode:
            method = self._rainbow_rgb_chars
        else:
            method = self._rainbow_hex_chars
        return ''.join(
            self.color(c, **color_args(hval))
            for c, hval in method(
                text,
                freq=freq,
                spread=spread,
                offset=offset)
        )


Instruction:
Write a Python 3 function for
creating rainbow text from text.

Response:
def _rainbow_lines(
            self, text, freq=0.1, spread=3.0, offset=0, movefactor=0,
            rgb_mode=False, **colorargs):
        """ Create rainbow text, using the same offset for each line.
            Arguments:
                text       : String to colorize.
                freq       : Frequency/"tightness" of colors in the rainbow.
                             Best results when in the range 0.0-1.0.
                             Default: 0.1
                spread     : Spread/width of colors.
                             Default: 3.0
                offset     : Offset for start of rainbow.
                             Default: 0
                movefactor : Factor for offset increase on each new line.
                             Default: 0
                rgb_mode   : If truthy, use RGB escape codes instead of
                             extended 256 and approximate hex match.

            Keyword Arguments:
                fore, back, style  : Other args for the color() function.
        """
        if not movefactor:
            def factor(i):
                return offset
        else:
            # Increase the offset for each line.
            def factor(i):
                return offset + (i * movefactor)
        return '\n'.join(
            self._rainbow_line(
                line,
                freq=freq,
                spread=spread,
                offset=factor(i),
                rgb_mode=rgb_mode,
                **colorargs)
            for i, line in enumerate(text.splitlines()))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _rainbow_rgb(self, freq, i):
        # Borrowed from lolcat, translated from ruby.
        red = math.sin(freq * i + 0) * 127 + 128
        green = math.sin(freq * i + 2 * math.pi / 3) * 127 + 128
        blue = math.sin(freq * i + 4 * math.pi / 3) * 127 + 128
        return int(red), int(green), int(blue)

Response: Calculate a single rgb value for a piece of a rainbow.


Instruction:
Can you generate the documentation for the following Python 3 function
def _rainbow_rgb_chars(self, s, freq=0.1, spread=3.0, offset=0):
        return (
            (c, self._rainbow_rgb(freq, offset + i / spread))
            for i, c in enumerate(s)
        )

Response: Iterate over characters in a string to build data needed for rainbow effect.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def b_rgb(self, r, g, b, text=None, fore=None, style=None):
        return self.chained(text=text, fore=fore, back=(r, g, b), style=style)

Response: A chained method that sets the back color to an RGB value.


Instruction:
Create a Python 3 function to
call by the various 'color' methods to colorize a single string. The RESET_ALL code is appended to the string unless text is empty. Raises ValueError on invalid color names. Arguments: text : String to colorize, or None for BG/Style change. fore : Name of fore color to use. back : Name of back color to use. style : Name of style to use.

Response:
def chained(self, text=None, fore=None, back=None, style=None):
        """ Called by the various 'color' methods to colorize a single string.
            The RESET_ALL code is appended to the string unless text is empty.
            Raises ValueError on invalid color names.

            Arguments:
                text  : String to colorize, or None for  BG/Style change.
                fore  : Name of fore color to use.
                back  : Name of back color to use.
                style : Name of style to use.
        """
        self.data = ''.join((
            self.data,
            self.color(text=text, fore=fore, back=back, style=style),
        ))
        return self


Instruction:
Given the following Python 3 function, write the documentation
def color_code(self, fore=None, back=None, style=None):
        # Map from style type to raw code formatter function.
        colorcodes = []
        resetcodes = []
        userstyles = {'style': style, 'back': back, 'fore': fore}
        for stype in userstyles:
            stylearg = userstyles.get(stype, None)
            if not stylearg:
                # No value for this style name, don't use it.
                continue
            # Get escape code for this style.
            code = self.get_escape_code(stype, stylearg)
            stylename = str(stylearg).lower()
            if (stype == 'style') and (stylename in ('0', )):
                resetcodes.append(code)
            elif stylename.startswith('reset'):
                resetcodes.append(code)
            else:
                colorcodes.append(code)
        # Reset codes come first, to not override colors.
        return ''.join((''.join(resetcodes), ''.join(colorcodes)))

Response: Return the color codes for this style and colors.


Instruction:
Explain what the following Python 3 code does
def format(self, *args, **kwargs):
        return self.__class__(self.data.format(*args, **kwargs))

Response: Like str. format except it returns a Colr


Instruction:
Can you implement a function in Python 3 that
converts user arg to escape code.

Response:
def get_escape_code(self, codetype, value):
        """ Convert user arg to escape code. """
        valuefmt = str(value).lower()
        code = codes[codetype].get(valuefmt, None)
        if code:
            # Basic code from fore, back, or style.
            return code

        named_funcs = {
            'fore': format_fore,
            'back': format_back,
            'style': format_style,
        }

        # Not a basic code, try known names.
        converter = named_funcs.get(codetype, None)
        if converter is None:
            raise ValueError(
                'Invalid code type. Expecting {}, got: {!r}'.format(
                    ', '.join(named_funcs),
                    codetype
                )
            )
        # Try as hex.
        with suppress(ValueError):
            value = int(hex2term(value, allow_short=True))
            return converter(value, extended=True)

        named_data = name_data.get(valuefmt, None)
        if named_data is not None:
            # A known named color.
            try:
                return converter(named_data['code'], extended=True)
            except TypeError:
                # Passing a known name as a style?
                if codetype == 'style':
                    raise InvalidStyle(value)
                raise
        # Not a known color name/value, try rgb.
        try:
            r, g, b = (int(x) for x in value)
            # This does not mean we have a 3 int tuple. It could '111'.
            # The converter should catch it though.
        except (TypeError, ValueError):
            # Not an rgb value.
            if codetype == 'style':
                raise InvalidStyle(value)
        try:
            escapecode = converter(value)
        except ValueError as ex:
            raise InvalidColr(value) from ex
        return escapecode


Instruction:
Can you tell what is the following Python 3 function doing
def gradient(
            self, text=None, name=None, fore=None, back=None, style=None,
            freq=0.1, spread=None, linemode=True,
            movefactor=2, rgb_mode=False):
        """ Return a gradient by color name. Uses rainbow() underneath to
            build the gradients, starting at a known offset.
            Arguments:
                text       : Text to make gradient (self.data when not given).
                             The gradient text is joined to self.data when
                             this is used.
                name       : Color name for the gradient (same as fore names).
                             Default: black
                fore       : Fore color. Back will be gradient when used.
                             Default: None (fore is gradient)
                back       : Back color. Fore will be gradient when used.
                             Default: None (back=reset/normal)
                style      : Style for the gradient.
                             Default: None (reset/normal)
                freq       : Frequency of color change.
                             Higher means more colors.
                             Best when in the 0.0-1.0 range.
                             Default: 0.1
                spread     : Spread/width of each color (in characters).
                             Default: 3.0 for colors, 1 for black/white
                linemode   : Colorize each line in the input.
                             Default: True
                movefactor : Factor for offset increase on each line when
                             using linemode.
                             Minimum value: 0
                             Default: 2
                rgb_mode   : Use true color (rgb) codes.
        """
        try:
            # Try explicit offset (passed in with `name`).
            offset = int(name)
        except (TypeError, ValueError):
            name = name.lower().strip() if name else 'black'
            # Black and white are separate methods.
            if name == 'black':
                return self.gradient_black(
                    text=text,
                    fore=fore,
                    back=back,
                    style=style,
                    step=int(spread) if spread else 1,
                    linemode=linemode,
                    movefactor=movefactor,
                    rgb_mode=rgb_mode
                )
            elif name == 'white':
                return self.gradient_black(
                    text=text,
                    fore=fore,
                    back=back,
                    style=style,
                    step=int(spread) if spread else 1,
                    linemode=linemode,
                    movefactor=movefactor,
                    reverse=True,
                    rgb_mode=rgb_mode
                )
            try:
                # Get rainbow offset from known name.
                offset = self.gradient_names[name]
            except KeyError:
                raise ValueError('Unknown gradient name: {}'.format(name))

        return self.rainbow(
            text=text,
            fore=fore,
            back=back,
            style=style,
            offset=offset,
            freq=freq,
            spread=spread or 3.0,
            linemode=linemode,
            movefactor=movefactor,
            rgb_mode=rgb_mode,
        )

Response: Return a gradient of the current color.


Instruction:
Can you write a function in Python 3 where it
returns a new instance of the color black gradient.

Response:
def gradient_black(
            self, text=None, fore=None, back=None, style=None,
            start=None, step=1, reverse=False,
            linemode=True, movefactor=2, rgb_mode=False):
        """ Return a black and white gradient.
            Arguments:
                text       : String to colorize.
                             This will always be greater than 0.
                fore       : Foreground color, background will be gradient.
                back       : Background color, foreground will be gradient.
                style      : Name of style to use for the gradient.
                start      : Starting 256-color number.
                             The `start` will be adjusted if it is not within
                             bounds.
                             This will always be > 15.
                             This will be adjusted to fit within a 6-length
                             gradient, or the 24-length black/white gradient.
                step       : Number of characters to colorize per color.
                             This allows a "wider" gradient.
                linemode   : Colorize each line in the input.
                             Default: True
                movefactor : Factor for offset increase on each line when
                             using linemode.
                             Minimum value: 0
                             Default: 2
                rgb_mode   : Use true color (rgb) method and codes.
        """
        gradargs = {
            'step': step,
            'fore': fore,
            'back': back,
            'style': style,
            'reverse': reverse,
            'rgb_mode': rgb_mode,
        }

        if linemode:
            gradargs['movefactor'] = 2 if movefactor is None else movefactor
            method = self._gradient_black_lines
        else:
            method = self._gradient_black_line

        if text:
            return self.__class__(
                ''.join((
                    self.data or '',
                    method(
                        text,
                        start or (255 if reverse else 232),
                        **gradargs)
                ))
            )

        # Operating on self.data.
        return self.__class__(
            method(
                self.stripped(),
                start or (255 if reverse else 232),
                **gradargs)
        )