Instruction:
Can you generate a brief explanation for the following Python 3 code
def write_PIA0_B_control(self, cpu_cycles, op_address, address, value):
        log.critical(
            "%04x| write $%02x (%s) to $%04x -> PIA 0 B side Control reg.\t|%s",
            op_address, value, byte2bit_string(value),
            address, self.cfg.mem_info.get_shortest(op_address)
        )

        if is_bit_set(value, bit=0):
            log.critical(
                "%04x| write $%02x (%s) to $%04x -> VSYNC IRQ: enable\t|%s",
                op_address, value, byte2bit_string(value),
                address, self.cfg.mem_info.get_shortest(op_address)
            )
            self.cpu.irq_enabled = True
            value = set_bit(value, bit=7)
        else:
            log.critical(
                "%04x| write $%02x (%s) to $%04x -> VSYNC IRQ: disable\t|%s",
                op_address, value, byte2bit_string(value),
                address, self.cfg.mem_info.get_shortest(op_address)
            )
            self.cpu.irq_enabled = False

        if not is_bit_set(value, bit=2):
            self.pia_0_B_control.select_pdr()
        else:
            self.pia_0_B_control.deselect_pdr()

        self.pia_0_B_control.set(value)

Response: write to 0xff03 - > PIA 0 B side Control reg.


Instruction:
How would you explain what the following Python 3 function does
def basic_addresses_write(self, cycles, last_op_address, address, word):
        log.critical("%04x| write $%04x to $%04x", last_op_address, word, address)
        return word

Response: write a word to an address in the basic tables of the current process


Instruction:
Create a Python 3 function for
converting all lowercase letters to uppercase and vice versa.

Response:
def event_text_key(self, event):
        """
        So a "invert shift" for user inputs:
        Convert all lowercase letters to uppercase and vice versa.
        """
        char = event.char
        if not char or char not in string.ascii_letters:
            # ignore all non letter inputs
            return

        converted_char = invert_shift(char)
        log.debug("convert keycode %s - char %s to %s", event.keycode, repr(char), converted_char)
#         self.text.delete(Tkinter.INSERT + "-1c") # Delete last input char
        self.text.insert(tkinter.INSERT, converted_char) # Insert converted char
        return "break"


Instruction:
How would you code a function in Python 3 to
return a list of bytes from a given list of bits.

Response:
def pop_bytes_from_bit_list(bit_list, count):
    """
    >>> bit_str = (
    ... "00110011"
    ... "00001111"
    ... "01010101"
    ... "11001100")
    >>> bit_list = [int(i) for i in bit_str]
    >>> bit_list, bytes = pop_bytes_from_bit_list(bit_list, 1)
    >>> bytes
    [[0, 0, 1, 1, 0, 0, 1, 1]]
    >>> bit_list, bytes = pop_bytes_from_bit_list(bit_list, 2)
    >>> bytes
    [[0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 0, 1, 0, 1, 0, 1]]
    >>> bit_list, bytes = pop_bytes_from_bit_list(bit_list, 1)
    >>> bytes
    [[1, 1, 0, 0, 1, 1, 0, 0]]
    """
    data_bit_count = count * 8

    data_bit_list = bit_list[:data_bit_count]
    data = list(iter_steps(data_bit_list, steps=8))

    bit_list = bit_list[data_bit_count:]
    return bit_list, data


Instruction:
Can you write a function in Python 3 where it
prints the bit list stats for the current node.

Response:
def print_bit_list_stats(bit_list):
    """
    >>> print_bit_list_stats([1,1,1,1,0,0,0,0])
    8 Bits: 4 positive bits and 4 negative bits
    """
    print "%i Bits:" % len(bit_list),
    positive_count = 0
    negative_count = 0
    for bit in bit_list:
        if bit == 1:
            positive_count += 1
        elif bit == 0:
            negative_count += 1
        else:
            raise TypeError("Not a bit: %s" % repr(bit))
    print "%i positive bits and %i negative bits" % (positive_count, negative_count)


Instruction:
Can you generate the documentation for the following Python 3 function
def inject_basic_program(self, ascii_listing):
        program_start = self.cpu.memory.read_word(
            self.machine_api.PROGRAM_START_ADDR
        )
        tokens = self.machine_api.ascii_listing2program_dump(ascii_listing)
        self.cpu.memory.load(program_start, tokens)
        log.critical("BASIC program injected into Memory.")

        # Update the BASIC addresses:
        program_end = program_start + len(tokens)
        self.cpu.memory.write_word(self.machine_api.VARIABLES_START_ADDR, program_end)
        self.cpu.memory.write_word(self.machine_api.ARRAY_START_ADDR, program_end)
        self.cpu.memory.write_word(self.machine_api.FREE_SPACE_START_ADDR, program_end)
        log.critical("BASIC addresses updated.")

Response: Injects the given ASCII BASIC program listing into the emulator RAM.


Instruction:
Implement a Python 3 function for
getting the foreground and background color of a node.

Response:
def get_rgb_color(color):
    """
    >>> get_rgb_color(BLUE)
    ((0, 0, 0), (33, 16, 181))

    >>> get_rgb_color(NORMAL)
    ((0, 65, 0), (0, 255, 0))
    """
    if color == INVERTED:
        foreground = (0, 255, 0)
        background = (0, 65, 0)
    elif color == NORMAL:
        foreground = (0, 65, 0)
        background = (0, 255, 0)
    else:
        foreground = (0, 0, 0)
        background = COLOR_INFO[color]
    return (foreground, background)


Instruction:
Implement a function in Python 3 to
create the wiki page for the character map.

Response:
def create_wiki_page():
    """
    for http://archive.worldofdragon.org/index.php?title=CharMap
    """
    print (
        '{| class="wikitable"'
        ' style="font-family: monospace;'
        ' background-color:#ffffcc;"'
        ' cellpadding="10"'
    )
    print("|-")
    print("! POKE")
    print("value")
    print("! ")
    print("! unicode")
    print("codepoint")
    print("! type")
    print("|-")
    for no, data in enumerate(DRAGON_CHARS_MAP):
        item, item_type = data

        codepoint = ord(item)
        print("|%i" % no)

        foreground, background = get_rgb_color(item_type)
        foreground = "#%02x%02x%02x" % foreground
        background = "#%02x%02x%02x" % background

        style = "color: #%s;"
        print('| style="color:%s; background-color:%s;" | &#x%x;' % (
            foreground, background, codepoint
        ))
        print("|%i" % codepoint)
        print("|%s" % item_type)
        print("|-")
    print("|}")


Instruction:
Given the following Python 3 function, write the documentation
def bytes2codeline(raw_bytes):
    code_line = ""
    func_token = False
    for byte_no in raw_bytes:
        if byte_no == 0xff: # Next byte is a function token
            func_token = True
            continue
        elif func_token == True:
            func_token = False
            try:
                character = FUNCTION_TOKEN[byte_no]
            except KeyError, err:
                print "Error: BASIC function torken for '%s' not found!" % hex(byte_no)
                character = chr(byte_no)
        elif byte_no in BASIC_TOKENS:
            try:
                character = BASIC_TOKENS[byte_no]
            except KeyError, err:
                print "Error: BASIC torken for '%s' not found!" % hex(byte_no)
                character = chr(byte_no)
        else:
            character = chr(byte_no)
#         print byte_no, repr(character)
        code_line += character
    return code_line

Response: Convert a byte array to a Codeline code string.


Instruction:
Here you have a function in Python 3, explain what it does
def analyze(self):
        log.debug("enable half sinus scan")
        self.half_sinus = True
        statistics = self._get_statistics()

        width = 50
        max_count = max(statistics.values())

        print
        print "Found this zeror crossing timings in the wave file:"
        print

        for duration, count in sorted(statistics.items(), reverse=True):
            hz = duration2hz(duration, self.framerate / 2)
            w = int(round(float(width) / max_count * count))
            stars = "*"*w
            print "%5sHz (%5s Samples) exist: %4s %s" % (hz, duration, count, stars)

        print
        print "Notes:"
        print " - Hz values are converted to full sinus cycle duration."
        print " - Sample cound is from half sinus cycle."

Response: This function is called by the _get_statistics method. It will print out the information that is needed to analyze the wave file.


Instruction:
Make a summary of the following Python 3 code
def sync(self, length):

        # go in wave stream to the first bit
        try:
            self.next()
        except StopIteration:
            print "Error: no bits identified!"
            sys.exit(-1)

        log.info("First bit is at: %s" % self.pformat_pos())
        log.debug("enable half sinus scan")
        self.half_sinus = True

        # Toggle sync test by consuming one half sinus sample
#         self.iter_trigger_generator.next() # Test sync

        # get "half sinus cycle" test data
        test_durations = itertools.islice(self.iter_duration_generator, length)
        # It's a tuple like: [(frame_no, duration)...]

        test_durations = list(test_durations)

        diff1, diff2 = diff_info(test_durations)
        log.debug("sync diff info: %i vs. %i" % (diff1, diff2))

        if diff1 > diff2:
            log.info("\nbit-sync one step.")
            self.iter_trigger_generator.next()
            log.debug("Synced.")
        else:
            log.info("\nNo bit-sync needed.")

        self.half_sinus = False
        log.debug("disable half sinus scan")

Response: Synchronize weave with another bit.


Instruction:
Make a summary of the following Python 3 code
def iter_bitstream(self, iter_duration_generator):
        assert self.half_sinus == False # Allways trigger full sinus cycle

        # build min/max Hz values
        bit_nul_min_hz = self.cfg.BIT_NUL_HZ - self.cfg.HZ_VARIATION
        bit_nul_max_hz = self.cfg.BIT_NUL_HZ + self.cfg.HZ_VARIATION

        bit_one_min_hz = self.cfg.BIT_ONE_HZ - self.cfg.HZ_VARIATION
        bit_one_max_hz = self.cfg.BIT_ONE_HZ + self.cfg.HZ_VARIATION

        bit_nul_max_duration = self._hz2duration(bit_nul_min_hz)
        bit_nul_min_duration = self._hz2duration(bit_nul_max_hz)

        bit_one_max_duration = self._hz2duration(bit_one_min_hz)
        bit_one_min_duration = self._hz2duration(bit_one_max_hz)

        log.info("bit-0 in %sHz - %sHz (duration: %s-%s)  |  bit-1 in %sHz - %sHz (duration: %s-%s)" % (
            bit_nul_min_hz, bit_nul_max_hz, bit_nul_min_duration, bit_nul_max_duration,
            bit_one_min_hz, bit_one_max_hz, bit_one_min_duration, bit_one_max_duration,
        ))
        assert bit_nul_max_hz < bit_one_min_hz, "HZ_VARIATION value is %sHz too high!" % (
            ((bit_nul_max_hz - bit_one_min_hz) / 2) + 1
        )
        assert bit_one_max_duration < bit_nul_min_duration, "HZ_VARIATION value is too high!"

        # for end statistics
        bit_one_count = 0
        one_hz_min = sys.maxint
        one_hz_avg = None
        one_hz_max = 0
        bit_nul_count = 0
        nul_hz_min = sys.maxint
        nul_hz_avg = None
        nul_hz_max = 0

        for duration in iter_duration_generator:

            if bit_one_min_duration < duration < bit_one_max_duration:
                hz = self._duration2hz(duration)
                log.log(5,
                    "bit 1 at %s in %sSamples = %sHz" % (
                        self.pformat_pos(), duration, hz
                    )
                )
                yield 1
                bit_one_count += 1
                if hz < one_hz_min:
                    one_hz_min = hz
                if hz > one_hz_max:
                    one_hz_max = hz
                one_hz_avg = average(one_hz_avg, hz, bit_one_count)
            elif bit_nul_min_duration < duration < bit_nul_max_duration:
                hz = self._duration2hz(duration)
                log.log(5,
                    "bit 0 at %s in %sSamples = %sHz" % (
                        self.pformat_pos(), duration, hz
                    )
                )
                yield 0
                bit_nul_count += 1
                if hz < nul_hz_min:
                    nul_hz_min = hz
                if hz > nul_hz_max:
                    nul_hz_max = hz
                nul_hz_avg = average(nul_hz_avg, hz, bit_nul_count)
            else:
                hz = self._duration2hz(duration)
                log.log(7,
                    "Skip signal at %s with %sHz (%sSamples) out of frequency range." % (
                        self.pformat_pos(), hz, duration
                    )
                )
                continue

        bit_count = bit_one_count + bit_nul_count

        if bit_count == 0:
            print "ERROR: No information from wave to generate the bits"
            print "trigger volume to high?"
            sys.exit(-1)

        log.info("\n%i Bits: %i positive bits and %i negative bits" % (
            bit_count, bit_one_count, bit_nul_count
        ))
        if bit_one_count > 0:
            log.info("Bit 1: %sHz - %sHz avg: %.1fHz variation: %sHz" % (
                one_hz_min, one_hz_max, one_hz_avg, one_hz_max - one_hz_min
            ))
        if bit_nul_count > 0:
            log.info("Bit 0: %sHz - %sHz avg: %.1fHz variation: %sHz" % (
                nul_hz_min, nul_hz_max, nul_hz_avg, nul_hz_max - nul_hz_min
            ))

Response: iterate over the bitstream and yield the bits


Instruction:
Can you generate a brief explanation for the following Python 3 code
def iter_duration(self, iter_trigger):
        print
        process_info = ProcessInfo(self.frame_count, use_last_rates=4)
        start_time = time.time()
        next_status = start_time + 0.25

        old_pos = next(iter_trigger)
        for pos in iter_trigger:
            duration = pos - old_pos
#             log.log(5, "Duration: %s" % duration)
            yield duration
            old_pos = pos

            if time.time() > next_status:
                next_status = time.time() + 1
                self._print_status(process_info)

        self._print_status(process_info)
        print

Response: Yields the duration of two frames in a row.


Instruction:
Implement a function in Python 3 to
iterate over the wave values and yield the mid crossing of the wave.

Response:
def iter_trigger(self, iter_wave_values):
        """
        trigger middle crossing of the wave sinus curve
        """
        window_size = (2 * self.cfg.END_COUNT) + self.cfg.MID_COUNT

        # sinus curve goes from negative into positive:
        pos_null_transit = [(0, self.cfg.END_COUNT), (self.cfg.END_COUNT, 0)]

        # sinus curve goes from positive into negative:
        neg_null_transit = [(self.cfg.END_COUNT, 0), (0, self.cfg.END_COUNT)]

        if self.cfg.MID_COUNT > 3:
            mid_index = int(round(self.cfg.MID_COUNT / 2.0))
        else:
            mid_index = 0

        in_pos = False
        for values in iter_window(iter_wave_values, window_size):

            # Split the window
            previous_values = values[:self.cfg.END_COUNT] # e.g.: 123-----
            mid_values = values[self.cfg.END_COUNT:self.cfg.END_COUNT + self.cfg.MID_COUNT] # e.g.: ---45---
            next_values = values[-self.cfg.END_COUNT:] # e.g.: -----678

            # get only the value and strip the frame_no
            # e.g.: (frame_no, value) tuple -> value list
            previous_values = [i[1] for i in previous_values]
            next_values = [i[1] for i in next_values]

            # Count sign from previous and next values
            sign_info = [
                count_sign(previous_values, 0),
                count_sign(next_values, 0)
            ]
#             log.log(5, "sign info: %s" % repr(sign_info))
            # yield the mid crossing
            if in_pos == False and sign_info == pos_null_transit:
                log.log(5, "sinus curve goes from negative into positive")
#                 log.debug(" %s | %s | %s" % (previous_values, mid_values, next_values))
                yield mid_values[mid_index][0]
                in_pos = True
            elif  in_pos == True and sign_info == neg_null_transit:
                if self.half_sinus:
                    log.log(5, "sinus curve goes from positive into negative")
#                     log.debug(" %s | %s | %s" % (previous_values, mid_values, next_values))
                    yield mid_values[mid_index][0]
                in_pos = False


Instruction:
Can you create a Python 3 function that
iterates over the wave file entries.

Response:
def iter_wave_values(self):
        """
        yield frame numer + volume value from the WAVE file
        """
        typecode = self.get_typecode(self.samplewidth)

        if log.level >= 5:
            if self.cfg.AVG_COUNT > 1:
                # merge samples -> log output in iter_avg_wave_values
                tlm = None
            else:
                tlm = TextLevelMeter(self.max_value, 79)

        # Use only a read size which is a quare divider of the samplewidth
        # Otherwise array.array will raise: ValueError: string length not a multiple of item size
        divider = int(round(float(WAVE_READ_SIZE) / self.samplewidth))
        read_size = self.samplewidth * divider
        if read_size != WAVE_READ_SIZE:
            log.info("Real use wave read size: %i Bytes" % read_size)

        get_wave_block_func = functools.partial(self.wavefile.readframes, read_size)
        skip_count = 0

        manually_audioop_bias = self.samplewidth == 1 and audioop is None

        for frames in iter(get_wave_block_func, ""):

            if self.samplewidth == 1:
                if audioop is None:
                    log.warning("use audioop.bias() work-a-round for missing audioop.")
                else:
                    # 8 bit samples are unsigned, see:
                    # http://docs.python.org/2/library/audioop.html#audioop.lin2lin
                    frames = audioop.bias(frames, 1, 128)

            try:
                values = array.array(typecode, frames)
            except ValueError, err:
                # e.g.:
                #     ValueError: string length not a multiple of item size
                # Work-a-round: Skip the last frames of this block
                frame_count = len(frames)
                divider = int(math.floor(float(frame_count) / self.samplewidth))
                new_count = self.samplewidth * divider
                frames = frames[:new_count] # skip frames
                log.error(
                    "Can't make array from %s frames: Value error: %s (Skip %i and use %i frames)" % (
                        frame_count, err, frame_count - new_count, len(frames)
                ))
                values = array.array(typecode, frames)

            for value in values:
                self.wave_pos += 1 # Absolute position in the frame stream

                if manually_audioop_bias:
                    # audioop.bias can't be used.
                    # See: http://hg.python.org/cpython/file/482590320549/Modules/audioop.c#l957
                    value = value % 0xff - 128

#                 if abs(value) < self.min_volume:
# #                     log.log(5, "Ignore to lower amplitude")
#                     skip_count += 1
#                     continue

                yield (self.wave_pos, value)

        log.info("Skip %i samples that are lower than %i" % (
            skip_count, self.min_volume
        ))
        log.info("Last readed Frame is: %s" % self.pformat_pos())


Instruction:
Explain what the following Python 3 code does
def iter_steps(g, steps):
    values = []
    for value in g:
        values.append(value)
        if len(values) == steps:
            yield list(values)
            values = []
    if values:
        yield list(values)

Response: iterate over the given number of steps in the given sequence.


Instruction:
Can you generate the documentation for the following Python 3 function
def get(self, start, end):
        return [self.read_byte(addr) for addr in xrange(start, end)]

Response: Get a list of bytes from the cache.


Instruction:
Given the following Python 3 function, write the documentation
def _new_output_char(self, char):
        self.text.config(state=tkinter.NORMAL)
        self.text.insert("end", char)
        self.text.see("end")
        self.text.config(state=tkinter.DISABLED)

Response: insert in text field


Instruction:
Given the following Python 3 function, write the documentation
def check_cpu_interval(self, cpu_process):
        try:
#            log.critical("check_cpu_interval()")
            if not cpu_process.is_alive():
                log.critical("raise SystemExit, because CPU is not alive.")
                _thread.interrupt_main()
                raise SystemExit("Kill pager.getch()")
        except KeyboardInterrupt:
            _thread.interrupt_main()
        else:
            t = threading.Timer(1.0, self.check_cpu_interval, args=[cpu_process])
            t.start()

Response: check the CPU interval


Instruction:
Implement a Python 3 function for
managing the dragon - py emulator.

Response:
def cli(ctx, **kwargs):
    """
    DragonPy is a Open source (GPL v3 or later) emulator
    for the 30 years old homecomputer Dragon 32
    and Tandy TRS-80 Color Computer (CoCo)...

    Homepage: https://github.com/jedie/DragonPy
    """
    log.critical("cli kwargs: %s", repr(kwargs))
    ctx.obj = CliConfig(**kwargs)


Instruction:
Can you tell what is the following Python 3 function doing
def _windows_get_window_size():
    sbi = CONSOLE_SCREEN_BUFFER_INFO()
    ret = windll.kernel32.GetConsoleScreenBufferInfo(console_handle, byref(sbi))
    if ret == 0:
        return (0, 0)
    return (sbi.srWindow.Right - sbi.srWindow.Left + 1,
            sbi.srWindow.Bottom - sbi.srWindow.Top + 1)

Response: Get the width and height of the available window area on Windows.


Instruction:
Given the following Python 3 function, write the documentation
def _posix_get_window_size():
    # see README.txt for reference information
    # http://www.kernel.org/doc/man-pages/online/pages/man4/tty_ioctl.4.html

    from fcntl import ioctl
    from termios import TIOCGWINSZ
    from array import array

    """
    struct winsize {
        unsigned short ws_row;
        unsigned short ws_col;
        unsigned short ws_xpixel;   /* unused */
        unsigned short ws_ypixel;   /* unused */
    };
    """
    winsize = array("H", [0] * 4)
    try:
        ioctl(sys.stdout.fileno(), TIOCGWINSZ, winsize)
    except IOError:
        # for example IOError: [Errno 25] Inappropriate ioctl for device
        # when output is redirected
        # [ ] TODO: check fd with os.isatty
        pass
    return (winsize[1], winsize[0])

Response: Get the width and height of the terminal on POSIX system.


Instruction:
Can you generate the documentation for the following Python 3 function
def dumpkey(key):
    def hex3fy(key):
        """Helper to convert string into hex string (Python 3 compatible)"""
        from binascii import hexlify
        # Python 3 strings are no longer binary, encode them for hexlify()
        if PY3K:
           key = key.encode('utf-8')
        keyhex = hexlify(key).upper()
        if PY3K:
           keyhex = keyhex.decode('utf-8')
        return keyhex
    if type(key) == str:
        return hex3fy(key)
    else:
        return ' '.join( [hex3fy(s) for s in key] )

Response: Dump a key into a string.


Instruction:
Can you generate the documentation for the following Python 3 function
def _getch_unix(_getall=False):
    import sys, termios

    fd = sys.stdin.fileno()
    # save old terminal settings
    old_settings = termios.tcgetattr(fd)

    chars = []
    try:
        # change terminal settings - turn off canonical mode and echo.
        # in canonical mode read from stdin returns one line at a time
        # and we need one char at a time (see DESIGN.rst for more info)
        newattr = list(old_settings)
        newattr[3] &= ~termios.ICANON
        newattr[3] &= ~termios.ECHO
        newattr[6][termios.VMIN] = 1   # block until one char received
        newattr[6][termios.VTIME] = 0
        # TCSANOW below means apply settings immediately
        termios.tcsetattr(fd, termios.TCSANOW, newattr)

        # [ ] this fails when stdin is redirected, like
        #       ls -la | pager.py
        #   [ ] also check on Windows
        ch = sys.stdin.read(1)
        chars = [ch]

        if _getall:
            # move rest of chars (if any) from input buffer
            # change terminal settings - enable non-blocking read
            newattr = termios.tcgetattr(fd)
            newattr[6][termios.VMIN] = 0      # CC structure
            newattr[6][termios.VTIME] = 0
            termios.tcsetattr(fd, termios.TCSANOW, newattr)

            while True:
                ch = sys.stdin.read(1)
                if ch != '':
                    chars.append(ch)
                else:
                    break
    finally:
        # restore terminal settings. Do this when all output is
        # finished - TCSADRAIN flag
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

    if _getall:
        return chars
    else:
        return chars[0]

Response: getch_unix - get a single character from the terminal


Instruction:
How would you explain what the following Python 3 function does
def prompt(pagenum):
    prompt = "Page -%s-. Press any key to continue . . . " % pagenum
    echo(prompt)
    if getch() in [ESC_, CTRL_C_, 'q', 'Q']:
        return False
    echo('\r' + ' '*(len(prompt)-1) + '\r')

Response: Show default prompt to continue and process keypress.


Instruction:
Can you create a Python 3 function that
outputs content call pagecallback after every page with pagenumber as a parameter. pagecallback may return False to terminate pagination on Ctrl - C or Ctrl - C.

Response:
def page(content, pagecallback=prompt):
    """
    Output `content`, call `pagecallback` after every page with page
    number as a parameter. `pagecallback` may return False to terminate
    pagination.

    Default callback shows prompt, waits for keypress and aborts on
    'q', ESC or Ctrl-C.
    """
    width = getwidth()
    height = getheight()
    pagenum = 1

    try:
        try:
            line = content.next().rstrip("\r\n")
        except AttributeError:
            # Python 3 compatibility
            line = content.__next__().rstrip("\r\n")
    except StopIteration:
        pagecallback(pagenum)
        return

    while True:     # page cycle
        linesleft = height-1 # leave the last line for the prompt callback
        while linesleft:
            linelist = [line[i:i+width] for i in range(0, len(line), width)]
            if not linelist:
                linelist = ['']
            lines2print = min(len(linelist), linesleft)
            for i in range(lines2print):
                if WINDOWS and len(line) == width:
                    # avoid extra blank line by skipping linefeed print
                    echo(linelist[i])
                else:
                    print((linelist[i]))
            linesleft -= lines2print
            linelist = linelist[lines2print:]

            if linelist: # prepare symbols left on the line for the next iteration
                line = ''.join(linelist)
                continue
            else:
                try:
                    try:
                        line = content.next().rstrip("\r\n")
                    except AttributeError:
                        # Python 3 compatibility
                        line = content.__next__().rstrip("\r\n")
                except StopIteration:
                    pagecallback(pagenum)
                    return
        if pagecallback(pagenum) == False:
            return
        pagenum += 1


Instruction:
Explain what the following Python 3 code does
def reformat_v09_trace(raw_trace, max_lines=None):
    print()
    print("Reformat v09 trace...")
    mem_info = SBC09MemInfo(sys.stderr)

    result = []
    next_update = time.time() + 1
    old_line = None
    for line_no, line in enumerate(raw_trace.splitlines()):
        if max_lines is not None and line_no >= max_lines:
            msg = "max lines %i arraived -> Abort." % max_lines
            print(msg)
            result.append(msg)
            break

        if time.time() > next_update:
            print("reformat %i trace lines..." % line_no)
            next_update = time.time() + 1

        try:
            pc = int(line[3:7], 16)
            op_code = int(line[10:15].strip().replace(" ", ""), 16)
            cc = int(line[57:59], 16)
            a = int(line[46:48], 16)
            b = int(line[51:53], 16)
            x = int(line[18:22], 16)
            y = int(line[25:29], 16)
            u = int(line[32:36], 16)
            s = int(line[39:43], 16)
        except ValueError as err:
            print("Error in line %i: %s" % (line_no, err))
            print("Content on line %i:" % line_no)
            print("-"*79)
            print(repr(line))
            print("-"*79)
            continue

        op_data = MC6809OP_DATA_DICT[op_code]
        mnemonic = op_data["mnemonic"]

        cc_txt = cc_value2txt(cc)
        mem = mem_info.get_shortest(pc)
#        print op_data

        register_line = "cc=%02x a=%02x b=%02x dp=?? x=%04x y=%04x u=%04x s=%04x| %s" % (
            cc, a, b, x, y, u, s, cc_txt
        )
        if old_line is None:
            line = "(init with: %s)" % register_line
        else:
            line = old_line % register_line

        old_line = "%04x| %-11s %-27s %%s | %s" % (
            pc, "%x" % op_code, mnemonic,
            mem
        )

        result.append(line)

    print("Done, %i trace lines converted." % line_no)
#    print raw_trace[:700]
    return result

Response:
reformat v09 trace simmilar to XRoar one
    and add CC and Memory-Information.

    Note:
        v09 traces contains the register info line one trace line later!
        We reoder it as XRoar done: addr+Opcode with resulted registers


Instruction:
Make a summary of the following Python 3 code
def human_duration(t):
    if not isinstance(t, (int, float)):
        raise TypeError("human_duration() argument must be integer or float")

    chunks = (
      (60 * 60 * 24 * 365, u'years'),
      (60 * 60 * 24 * 30, u'months'),
      (60 * 60 * 24 * 7, u'weeks'),
      (60 * 60 * 24, u'days'),
      (60 * 60, u'hours'),
    )

    if t < 1:
        return u"%.1f ms" % round(t * 1000, 1)
    if t < 60:
        return u"%.1f sec" % round(t, 1)
    if t < 60 * 60:
        return u"%.1f min" % round(t / 60, 1)

    for seconds, name in chunks:
        count = t / seconds
        if count >= 1:
            count = round(count, 1)
            break
    return u"%(number).1f %(type)s" % {'number': count, 'type': name}

Response: Return a human readable text representation of a time duration.


Instruction:
Can you implement a function in Python 3 that
calculates the average value of a count of a single object.

Response:
def average(old_avg, current_value, count):
    """
    Calculate the average. Count must start with 0

    >>> average(None, 3.23, 0)
    3.23
    >>> average(0, 1, 0)
    1.0
    >>> average(2.5, 5, 4)
    3.0
    """
    if old_avg is None:
        return current_value
    return (float(old_avg) * count + current_value) / (count + 1)


Instruction:
How would you explain what the following Python 3 function does
def iter_window(g, window_size):
    values = collections.deque(maxlen=window_size)
    for value in g:
        values.append(value)
        if len(values) == window_size:
            yield list(values)

Response: Iterate over a sequence of elements in a given size.


Instruction:
Explain what the following Python 3 code does
def count_continuous_pattern(bitstream, pattern):
    assert isinstance(bitstream, (collections.Iterable, types.GeneratorType))
    assert isinstance(pattern, (list, tuple))

    window_size = len(pattern)
    count = -1
    for count, data in enumerate(iter_steps(bitstream, window_size), 1):
#         print count, data, pattern
        if data != pattern:
            count -= 1
            break

    return count

Response: Count the number of continuous patterns in a single element of a sequence.


Instruction:
Implement a function in Python 3 to
find the next available language in a sequence of bytes.

Response:
def find_iter_window(bitstream, pattern, max_pos=None):
    """
    >>> pattern = list(bytes2bit_strings("B"))
    >>> bitstream = bytes2bit_strings("AAABCCC")
    >>> find_iter_window(bitstream, pattern)
    24
    >>> "".join(list(bitstream2string(bitstream)))
    'CCC'

    >>> find_iter_window(bytes2bit_strings("HELLO!"), list(bytes2bit_strings("LO")))
    24

    >>> find_iter_window(bytes2bit_strings("HELLO!"), list(bytes2bit_strings("LO")), max_pos=16)
    Traceback (most recent call last):
    ...
    MaxPosArraived: 17

    >>> find_iter_window(bytes2bit_strings("HELLO!"), list(bytes2bit_strings("X")))
    Traceback (most recent call last):
    ...
    PatternNotFound: 40
    """
    assert isinstance(bitstream, (collections.Iterable, types.GeneratorType))
    assert isinstance(pattern, (list, tuple))

    window_size = len(pattern)
    pos = -1
    for pos, data in enumerate(iter_window(bitstream, window_size)):
#         print pos, data, pattern
        if data == pattern:
            return pos
        if max_pos is not None and pos > max_pos:
            raise MaxPosArraived(pos)
    raise PatternNotFound(pos)


Instruction:
Write a Python 3 script to
count the number of elements in the given iterable that are the same.

Response:
def count_the_same(iterable, sentinel):
    """
    >>> count_the_same([0x55,0x55,0x55,0x55,0x3C,"foo","bar"],0x55)
    (4, 60)
    >>> 0x3C == 60
    True
    """
    count = 0
    x = None
    for count, x in enumerate(iterable):
        if x != sentinel:
            break
    return count, x


Instruction:
Here you have a function in Python 3, explain what it does
def diff_info(data):
    def get_diff(l):
        diff = 0
        for no1, no2 in iter_steps(l, steps=2):
            diff += abs(no1 - no2)
        return diff

    data1 = data[2:]
    diff1 = get_diff(data1)

    data2 = data[1:-1]
    diff2 = get_diff(data2)

    return diff1, diff2

Response: Return the diff between two words.


Instruction:
Make a summary of the following Python 3 code
def count_sign(values, min_value):
    positive_count = 0
    negative_count = 0
    for value in values:
        if value > min_value:
            positive_count += 1
        elif value < -min_value:
            negative_count += 1
    return positive_count, negative_count

Response: Count the number of times a given value is in a given order.


Instruction:
Can you implement a function in Python 3 that
converts a sequence of bits into a single codepoint.

Response:
def bits2codepoint(bits):
    """
    >>> c = bits2codepoint([0, 0, 0, 1, 0, 0, 1, 0])
    >>> c
    72
    >>> chr(c)
    'H'

    >>> bits2codepoint("00010010")
    72

    >>> bits2codepoint([0, 0, 1, 1, 0, 0, 1, 0])
    76
    """
    bit_string = "".join([str(c) for c in reversed(bits)])
    return int(bit_string, 2)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def byte2bit_string(data):
    if isinstance(data, basestring):
        assert len(data) == 1
        data = ord(data)

    bits = '{0:08b}'.format(data)
    bits = bits[::-1]
    return bits

Response: Convert a byte string to a base 16 bit string.


Instruction:
Here you have a function in Python 3, explain what it does
def codepoints2bitstream(codepoints):
    if isinstance(codepoints, int):
        codepoints = [codepoints]
    for codepoint in codepoints:
        bit_string = byte2bit_string(codepoint)
        for bit in bit_string:
            yield int(bit)

Response: Convert a list of codepoints into a list of bit streams.


Instruction:
Make a summary of the following Python 3 code
def print_codepoint_stream(codepoint_stream, display_block_count=8, no_repr=False):
    in_line_count = 0

    line = []
    for no, codepoint in enumerate(codepoint_stream, 1):
        r = repr(chr(codepoint))
        if "\\x" in r: # FIXME
            txt = "%s %i" % (hex(codepoint), codepoint)
        else:
            txt = "%s %s" % (hex(codepoint), r)

        line.append(txt.center(8))

        in_line_count += 1
        if in_line_count >= display_block_count:
            in_line_count = 0
            print "%4s | %s |" % (no, " | ".join(line))
            line = []
    if line:
        print "%4s | %s |" % (no, " | ".join(line))

    if in_line_count > 0:
        print

Response: Print a stream of codepoints.


Instruction:
Here you have a function in Python 3, explain what it does
def pformat_codepoints(codepoints):
    printable = string.printable.replace("\n", "").replace("\r", "")
    line = []
    strings = ""
    for codepoint in codepoints:
        char = chr(codepoint)
        if char in printable:
            strings += char
        else:
            if strings != "":
                line.append(strings)
                strings = ""
            line.append(char)
    return line

Response: Return a list of strings for a list of codepoints.


Instruction:
Create a Python 3 function to
print a list of blocks of size display_block_count

Response:
def print_block_bit_list(block_bit_list, display_block_count=8, no_repr=False):
    """
    >>> bit_list = (
    ... [0,0,1,1,0,0,1,0], # L
    ... [1,0,0,1,0,0,1,0], # I
    ... )
    >>> print_block_bit_list(bit_list)
    ... # doctest: +NORMALIZE_WHITESPACE
       2 - 00110010 10010010
           0x4c 'L' 0x49 'I'
    """
    def print_line(no, line, line_info):
        print "%4s - %s" % (no, line)
        if no_repr:
            return

        line = []
        for codepoint in line_info:
            r = repr(chr(codepoint))
            if "\\x" in r: # FIXME
                txt = "%s" % hex(codepoint)
            else:
                txt = "%s %s" % (hex(codepoint), r)
            txt = txt.center(8)
            line.append(txt)

        print "       %s" % " ".join(line)

    in_line_count = 0

    line = ""
    line_info = []
    for no, bits in enumerate(block_bit_list, 1):
        line += "%s " % "".join([str(c) for c in bits])

        codepoint = bits2codepoint(bits)
        line_info.append(codepoint)

        in_line_count += 1
        if in_line_count >= display_block_count:
            in_line_count = 0
            print_line(no, line, line_info)
            line_info = []
            line = ""
    if line:
        print_line(no, line, line_info)

    if in_line_count > 0:
        print


Instruction:
Explain what the following Python 3 code does
def print_bitlist(bitstream, no_repr=False):
    block_bit_list = iter_steps(bitstream, steps=8)
    print_block_bit_list(block_bit_list, no_repr=no_repr)

Response: Print a list of blocks in a single bitstream.


Instruction:
Can you create a Python 3 function that
gets a word from a byte iterator.

Response:
def get_word(byte_iterator):
    """
    return a uint16 value

    >>> g=iter([0x1e, 0x12])
    >>> v=get_word(g)
    >>> v
    7698
    >>> hex(v)
    '0x1e12'
    """
    byte_values = list(itertools.islice(byte_iterator, 2))
    try:
        word = (byte_values[0] << 8) | byte_values[1]
    except TypeError, err:
        raise TypeError("Can't build word from %s: %s" % (repr(byte_values), err))
    return word


Instruction:
Can you write a function in Python 3 where it
creates sinus values for a given framerate and Hz.

Response:
def sinus_values_by_hz(framerate, hz, max_value):
    """
    Create sinus values with the given framerate and Hz.
    Note:
    We skip the first zero-crossing, so the values can be used directy in a loop.

    >>> values = sinus_values_by_hz(22050, 1200, 255)
    >>> len(values) # 22050 / 1200Hz = 18,375
    18
    >>> values
    (87, 164, 221, 251, 251, 221, 164, 87, 0, -87, -164, -221, -251, -251, -221, -164, -87, 0)

    >>> tl = TextLevelMeter(255, width=40)
    >>> for v in values:
    ...     tl.feed(v)
    '|                  |     *            |'
    '|                  |           *      |'
    '|                  |               *  |'
    '|                  |                 *|'
    '|                  |                 *|'
    '|                  |               *  |'
    '|                  |           *      |'
    '|                  |     *            |'
    '|                  *                  |'
    '|            *     |                  |'
    '|      *           |                  |'
    '|  *               |                  |'
    '|*                 |                  |'
    '|*                 |                  |'
    '|  *               |                  |'
    '|      *           |                  |'
    '|            *     |                  |'
    '|                  *                  |'

    >>> values = sinus_values_by_hz(44100, 1200, 255)
    >>> len(values) # 44100 / 1200Hz = 36,75
    37
    """
    count = int(round(float(framerate) / float(hz)))
    count += 1
    values = tuple(sinus_values(count, max_value))
    values = values[1:]
    return values


Instruction:
Implement a Python 3 function for
returning the module name of the given package.

Response:
def get_module_name(package):
    """
    package must have these attributes:
    e.g.:
        package.DISTRIBUTION_NAME = "DragonPyEmulator"
        package.DIST_GROUP = "console_scripts"
        package.ENTRY_POINT = "DragonPy"

    :return: a string like: "dragonpy.core.cli"
    """
    distribution = get_distribution(package.DISTRIBUTION_NAME)
    entry_info = distribution.get_entry_info(package.DIST_GROUP, package.ENTRY_POINT)
    if not entry_info:
        raise RuntimeError(
            "Can't find entry info for distribution: %r (group: %r, entry point: %r)" % (
                package.DISTRIBUTION_NAME, package.DIST_GROUP, package.ENTRY_POINT
            )
        )
    return entry_info.module_name


Instruction:
Explain what the following Python 3 code does
def _run(*args, **kwargs):
    verbose = kwargs.pop("verbose", False)
    if verbose:
        click.secho(" ".join([repr(i) for i in args]), bg='blue', fg='white')

    executable = args[0]
    if not os.path.isfile(executable):
        raise RuntimeError("First argument %r is not a existing file!" % executable)
    if not os.access(executable, os.X_OK):
        raise RuntimeError("First argument %r exist, but is not executeable!" % executable)

    return subprocess.Popen(args, **kwargs)

Response: Run current executable via subprocess and given args


Instruction:
Can you generate a brief explanation for the following Python 3 code
def after_install(options, home_dir):
    # --- CUT here ---
    """
    called after virtualenv was created and pip/setuptools installed.
    Now we installed requirement libs/packages.
    """
    if options.install_type==INST_PYPI:
        requirements=NORMAL_INSTALLATION
    elif options.install_type==INST_GIT:
        requirements=GIT_READONLY_INSTALLATION
    elif options.install_type==INST_DEV:
        requirements=DEVELOPER_INSTALLATION
    else:
        # Should never happen
        raise RuntimeError("Install type %r unknown?!?" % options.install_type)

    env_subprocess = EnvSubprocess(home_dir) # from bootstrap_env.bootstrap_install_pip

    logfile = os.path.join(env_subprocess.abs_home_dir, "install.log")

    for requirement in requirements:
        sys.stdout.write("\n\nInstall %r:\n" % requirement)
        env_subprocess.call_env_pip(["install", "--log=%s" % logfile, requirement])
        sys.stdout.write("\n")

Response: Called after virtualenv is created and pip is installed.


Instruction:
How would you code a function in Python 3 to
print the usual traceback information followed by a listing of all the available local variables in each frame.

Response:
def print_exc_plus():
    """
    Print the usual traceback information, followed by a listing of all the
    local variables in each frame.
    """
    sys.stderr.flush() # for eclipse
    sys.stdout.flush() # for eclipse

    tb = sys.exc_info()[2]
    while True:
        if not tb.tb_next:
            break
        tb = tb.tb_next
    stack = []
    f = tb.tb_frame
    while f:
        stack.append(f)
        f = f.f_back

    txt = traceback.format_exc()
    txt_lines = txt.splitlines()
    first_line = txt_lines.pop(0)
    last_line = txt_lines.pop(-1)
    click.secho(first_line, fg="red")
    for line in txt_lines:
        if line.strip().startswith("File"):
            click.echo(line)
        else:
            click.secho(line, fg="white", bold=True)
    click.secho(last_line, fg="red")

    click.echo()
    click.secho(
        "Locals by frame, most recent call first:",
        fg="blue", bold=True
    )
    for frame in stack:
        msg = 'File "%s", line %i, in %s' % (
            frame.f_code.co_filename,
            frame.f_lineno,
            frame.f_code.co_name,
        )
        msg = click.style(msg, fg="white", bold=True, underline=True)
        click.echo("\n *** %s" % msg)

        for key, value in list(frame.f_locals.items()):
            click.echo("%30s = " % click.style(key, bold=True), nl=False)
            # We have to be careful not to cause a new error in our error
            # printer! Calling str() on an unknown object could cause an
            # error we don't want.
            if isinstance(value, int):
                value = "$%x (decimal: %i)" % (value, value)
            else:
                value = repr(value)

            if len(value) > MAX_CHARS:
                value = "%s..." % value[:MAX_CHARS]

            try:
                click.echo(value)
            except:
                click.echo("<ERROR WHILE PRINTING VALUE>")


Instruction:
How would you implement a function in Python 3 that
adds a block of tokenized BASIC source code lines to the current file

Response:
def add_block_data(self, block_length, data):
        """
        add a block of tokenized BASIC source code lines.

        >>> cfg = Dragon32Config
        >>> fc = FileContent(cfg)

        >>> block = [
        ... 0x1e,0x12,0x0,0xa,0x80,0x20,0x49,0x20,0xcb,0x20,0x31,0x20,0xbc,0x20,0x31,0x30,0x0,
        ... 0x0,0x0]
        >>> len(block)
        19
        >>> fc.add_block_data(19,iter(block))
        19 Bytes parsed
        >>> fc.print_code_lines()
        10 FOR I = 1 TO 10

        >>> block = iter([
        ... 0x1e,0x29,0x0,0x14,0x87,0x20,0x49,0x3b,0x22,0x48,0x45,0x4c,0x4c,0x4f,0x20,0x57,0x4f,0x52,0x4c,0x44,0x21,0x22,0x0,
        ... 0x0,0x0])
        >>> fc.add_block_data(999,block)
        25 Bytes parsed
        ERROR: Block length value 999 is not equal to parsed bytes!
        >>> fc.print_code_lines()
        10 FOR I = 1 TO 10
        20 PRINT I;"HELLO WORLD!"

        >>> block = iter([
        ... 0x1e,0x31,0x0,0x1e,0x8b,0x20,0x49,0x0,
        ... 0x0,0x0])
        >>> fc.add_block_data(10,block)
        10 Bytes parsed
        >>> fc.print_code_lines()
        10 FOR I = 1 TO 10
        20 PRINT I;"HELLO WORLD!"
        30 NEXT I

        Test function tokens in code

        >>> fc = FileContent(cfg)
        >>> data = iter([
        ... 0x1e,0x4a,0x0,0x1e,0x58,0xcb,0x58,0xc3,0x4c,0xc5,0xff,0x88,0x28,0x52,0x29,0x3a,0x59,0xcb,0x59,0xc3,0x4c,0xc5,0xff,0x89,0x28,0x52,0x29,0x0,
        ... 0x0,0x0
        ... ])
        >>> fc.add_block_data(30, data)
        30 Bytes parsed
        >>> fc.print_code_lines()
        30 X=X+L*SIN(R):Y=Y+L*COS(R)

        Test high line numbers

        >>> fc = FileContent(cfg)
        >>> data = [
        ... 0x1e,0x1a,0x0,0x1,0x87,0x20,0x22,0x4c,0x49,0x4e,0x45,0x20,0x4e,0x55,0x4d,0x42,0x45,0x52,0x20,0x54,0x45,0x53,0x54,0x22,0x0,
        ... 0x1e,0x23,0x0,0xa,0x87,0x20,0x31,0x30,0x0,
        ... 0x1e,0x2d,0x0,0x64,0x87,0x20,0x31,0x30,0x30,0x0,
        ... 0x1e,0x38,0x3,0xe8,0x87,0x20,0x31,0x30,0x30,0x30,0x0,
        ... 0x1e,0x44,0x27,0x10,0x87,0x20,0x31,0x30,0x30,0x30,0x30,0x0,
        ... 0x1e,0x50,0x80,0x0,0x87,0x20,0x33,0x32,0x37,0x36,0x38,0x0,
        ... 0x1e,0x62,0xf9,0xff,0x87,0x20,0x22,0x45,0x4e,0x44,0x22,0x3b,0x36,0x33,0x39,0x39,0x39,0x0,0x0,0x0
        ... ]
        >>> len(data)
        99
        >>> fc.add_block_data(99, iter(data))
        99 Bytes parsed
        >>> fc.print_code_lines()
        1 PRINT "LINE NUMBER TEST"
        10 PRINT 10
        100 PRINT 100
        1000 PRINT 1000
        10000 PRINT 10000
        32768 PRINT 32768
        63999 PRINT "END";63999
        """

#         data = list(data)
# #         print repr(data)
#         print_as_hex_list(data)
#         print_codepoint_stream(data)
#         sys.exit()

        # create from codepoint list a iterator
        data = iter(data)

        byte_count = 0
        while True:
            try:
                line_pointer = get_word(data)
            except (StopIteration, IndexError), err:
                log.error("No line pointer information in code line data. (%s)" % err)
                break
#             print "line_pointer:", repr(line_pointer)
            byte_count += 2
            if not line_pointer:
                # arrived [0x00, 0x00] -> end of block
                break

            try:
                line_number = get_word(data)
            except (StopIteration, IndexError), err:
                log.error("No line number information in code line data. (%s)" % err)
                break
#             print "line_number:", repr(line_number)
            byte_count += 2

#             data = list(data)
#             print_as_hex_list(data)
#             print_codepoint_stream(data)
#             data = iter(data)

            # get the code line:
            # new iterator to get all characters until 0x00 arraived
            code = iter(data.next, 0x00)

            code = list(code) # for len()
            byte_count += len(code) + 1 # from 0x00 consumed in iter()

#             print_as_hex_list(code)
#             print_codepoint_stream(code)

            # convert to a plain ASCII string
            code = bytes2codeline(code)

            self.code_lines.append(
                CodeLine(line_pointer, line_number, code)
            )

        print "%i Bytes parsed" % byte_count
        if block_length != byte_count:
            print "ERROR: Block length value %i is not equal to parsed bytes!" % block_length


Instruction:
How would you code a function in Python 3 to
add a block of ASCII BASIC source code lines.

Response:
def add_ascii_block(self, block_length, data):
        """
        add a block of ASCII BASIC source code lines.

        >>> data = [
        ... 0xd,
        ... 0x31,0x30,0x20,0x50,0x52,0x49,0x4e,0x54,0x20,0x22,0x54,0x45,0x53,0x54,0x22,
        ... 0xd,
        ... 0x32,0x30,0x20,0x50,0x52,0x49,0x4e,0x54,0x20,0x22,0x48,0x45,0x4c,0x4c,0x4f,0x20,0x57,0x4f,0x52,0x4c,0x44,0x21,0x22,
        ... 0xd
        ... ]
        >>> len(data)
        41
        >>> fc = FileContent(Dragon32Config)
        >>> fc.add_ascii_block(41, iter(data))
        41 Bytes parsed
        >>> fc.print_code_lines()
        10 PRINT "TEST"
        20 PRINT "HELLO WORLD!"
        """
        data = iter(data)

        data.next() # Skip first \r
        byte_count = 1 # incl. first \r
        while True:
            code = iter(data.next, 0xd) # until \r
            code = "".join([chr(c) for c in code])

            if not code:
                log.warning("code ended.")
                break

            byte_count += len(code) + 1 # and \r consumed in iter()

            try:
                line_number, code = code.split(" ", 1)
            except ValueError, err:
                print "\nERROR: Splitting linenumber in %s: %s" % (repr(code), err)
                break

            try:
                line_number = int(line_number)
            except ValueError, err:
                print "\nERROR: Part '%s' is not a line number!" % repr(line_number)
                continue

            self.code_lines.append(
                CodeLine(None, line_number, code)
            )

        print "%i Bytes parsed" % byte_count
        if block_length != byte_count:
            log.error(
                "Block length value %i is not equal to parsed bytes!" % block_length
            )


Instruction:
Write a Python 3 script for
getting the filename block as a list of codepoints.

Response:
def get_filename_block_as_codepoints(self):
        """
        TODO: Support tokenized BASIC. Now we only create ASCII BASIC.
        """
        codepoints = []
        codepoints += list(string2codepoint(self.filename.ljust(8, " ")))
        codepoints.append(self.cfg.FTYPE_BASIC) # one byte file type
        codepoints.append(self.cfg.BASIC_ASCII) # one byte ASCII flag

        # one byte gap flag (0x00=no gaps, 0xFF=gaps)
        # http://archive.worldofdragon.org/phpBB3/viewtopic.php?f=8&t=4231&p=9110#p9110
        codepoints.append(self.gap_flag)

        # machine code starting/loading address
        if self.file_type != self.cfg.FTYPE_BASIC: # BASIC programm (0x00)
            codepoints = iter(codepoints)

            self.start_address = get_word(codepoints)
            log.info("machine code starting address: %s" % hex(self.start_address))

            self.load_address = get_word(codepoints)
            log.info("machine code loading address: %s" % hex(self.load_address))
        else:
            # not needed in BASIC files
            # http://archive.worldofdragon.org/phpBB3/viewtopic.php?f=8&t=4341&p=9109#p9109
            pass

        log.debug("filename block: %s" % pformat_codepoints(codepoints))
        return codepoints


Instruction:
Explain what the following Python 3 code does
def buffer2file(self):
        if self.current_file is not None and self.buffer:
            self.current_file.add_block_data(self.buffered_block_length, self.buffer)
            self.buffer = []
            self.buffered_block_length = 0

Response: add the code buffer content to the CassetteFile instance


Instruction:
How would you explain what the following Python 3 function does
def validate(validation, dictionary):

    errors = defaultdict(list)
    for key in validation:
        if isinstance(validation[key], (list, tuple)):
            if Required in validation[key]:
                if not Required(key, dictionary):
                    errors[key] = ["must be present"]
                    continue
            _validate_list_helper(validation, dictionary, key, errors)
        else:
            v = validation[key]
            if v == Required:
                if not Required(key, dictionary):
                    errors[key] = ["must be present"]
            else:
                _validate_and_store_errs(v, dictionary, key, errors)
    if len(errors) > 0:
        # `errors` gets downgraded from defaultdict to dict
        # because it makes for prettier output
        return ValidationResult(valid=False, errors=dict(errors))
    else:
        return ValidationResult(valid=True, errors={})

Response: Validate that a dictionary passes a set of key - based validators.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ArgSpec(*args, **kwargs):

    def argspec_lambda(value):
        argspec = getargspec(value)
        argspec_kw_vals = ()
        if argspec.defaults is not None:
            argspec_kw_vals = argspec.defaults
        kw_vals = {}
        arg_offset = 0
        arg_len = len(argspec.args) - 1
        for val in argspec_kw_vals[::-1]:
            kw_vals[argspec.args[arg_len - arg_offset]] = val
            arg_offset += 1
        if kwargs == kw_vals:
            if len(args) != arg_len - arg_offset + 1:
                return False
            index = 0
            for arg in args:
                if argspec.args[index] != arg:
                    return False
                index += 1
            return True
        return False
    argspec_lambda.err_message = "must match argspec ({0}) {{{1}}}".format(args, kwargs)
    # as little sense as negating this makes, best to just be consistent.
    argspec_lambda.not_message = "must not match argspec ({0}) {{{1}}}".format(args, kwargs)
    return argspec_lambda

Response: Validate a function based on the given argspec.


Instruction:
Can you tell what is the following Python 3 function doing
def get_first_date_for_group(start_date,group_type,n):
    current_date = start_date
    if group_type == 'monthly':
        current_year = start_date.year
        current_month = start_date.month
        for i in range(n-1):
            current_month-=1
            if current_month == 0:
                current_month = 12
                current_year -= 1
        first_date = datetime.datetime(current_year,current_month,1)
    elif group_type == 'weekly':
        first_date=start_date-datetime.timedelta(days = start_date.weekday()+(n-1)*7)
    elif group_type == 'daily':
        first_date = start_date-datetime.timedelta(days = n-1)
    first_date = datetime.datetime(first_date.year,first_date.month,first_date.day,0,0,0)
    return first_date

Response: Get the first date for a group


Instruction:
Here you have a function in Python 3, explain what it does
def get_snapshots(self,**kwargs):
        commits = self.repository.get_commits(**kwargs)
        snapshots = []
        for commit in commits:
            for key in ('committer_date','author_date'):
                commit[key] = datetime.datetime.fromtimestamp(commit[key+'_ts'])
            snapshot = GitSnapshot(commit)
            hasher = Hasher()
            hasher.add(snapshot.sha)
            snapshot.hash = hasher.digest.hexdigest()
            snapshot.project = self.project
            snapshot.pk = uuid.uuid4().hex
            snapshots.append(snapshot)
        return snapshots

Response: Returns a list of snapshots in a given repository.


Instruction:
Create a Python 3 function for
returning a diff between two lists of objects.

Response:
def diff_objects(objects_a,objects_b,key,comparator = None,with_unchanged = False):
    """
    Returns a "diff" between two lists of objects.

    :param key: The key that identifies objects with identical location in each set,
                such as files with the same path or code objects with the same URL.
    :param comparator: Comparison functions that decides if two objects are identical.
    """

    objects_by_key = {'a' :defaultdict(list),
                      'b' : defaultdict(list)}

    for name,objects in ('a',objects_a),('b',objects_b):
        d = objects_by_key[name]
        for obj in objects:
            d[key(obj)].append(obj)

    added_objects = [obj for key,objs in objects_by_key['b'].items()
                     if key not in objects_by_key['a'] for obj in objs]

    deleted_objects = [obj for key,objs in objects_by_key['a'].items()
                       if key not in objects_by_key['b'] for obj in objs]

    joint_keys = [key for key in objects_by_key['a']
                  if key in objects_by_key['b']]

    modified_objects = []

    #we go through the keys that exist in both object sets
    for key in joint_keys:
        objects_a = objects_by_key['a'][key]
        objects_b = objects_by_key['b'][key]

        if len(objects_a) > 1 or len(objects_b) > 1:

            #this is an ambiguous situation: we have more than one object for the same
            #key, so we have to decide which ones have been added or not
            #we try to remove identical objects from the set

            objects_a_copy = objects_a[:]
            objects_b_copy = objects_b[:]

            #for the next step, we need a comparator
            if comparator:
                #we iterate through the list and try to find different objects...
                for obj_a in objects_a:
                    for obj_b in objects_b_copy:
                        if comparator(obj_a,obj_b) == 0:
                            #these objects are identical, we remove them from both sets...
                            objects_a_copy.remove(obj_a)
                            objects_b_copy.remove(obj_b)
                            break

            #here we cannot distinguish objects...
            if len(objects_b_copy) > len(objects_a_copy):
                #we arbitrarily mark the last objects in objects_b as added
                added_objects.extend(objects_b_copy[len(objects_a_copy):])
            elif len(objects_a_copy) > len(objects_b_copy):
                #we arbitrarily mark the last objects in objects_a as deleted
                deleted_objects.extend(objects_a_copy[len(objects_b_copy):])
        else:
            if comparator and comparator(objects_a[0],objects_b[0]) != 0:
                #these objects are different
                modified_objects.append(objects_a[0])

    result = {
        'added' : added_objects,
        'deleted' : deleted_objects,
        'modified' : modified_objects,
    }

    if with_unchanged:
        unchanged_objects = [objects_b_by_key[key]
                             for key in joint_keys
                             if not objects_b_by_key[key] in modified_objects]
        result['unchanged'] = unchanged_objects

    return result


Instruction:
Write a Python 3 script for
returning a list of differences between two snapshots.

Response:
def diff_snapshots(self,snapshot_a,snapshot_b,save = True, diff=None):

        """
        Returns a list of
        """

        file_revisions_a = snapshot_a.file_revisions
        file_revisions_b = snapshot_b.file_revisions

        file_revisions_diff = diff_objects(file_revisions_a,
                                           file_revisions_b,
                                           file_revision_key,
                                           file_revision_comparator)

        #We just generate code objects and issues
        #for the modified file revisions, to save time when diffing.

        logger.debug("Generating list of modified file revisions...")
        modified_file_revisions_by_path = {}
        for fr_type in ('modified','added','deleted'):
            for fr in file_revisions_diff[fr_type]:
                if not fr.path in modified_file_revisions_by_path:
                    modified_file_revisions_by_path[fr.path] = fr

        logger.debug("Generating list of modified issues...")

        modified_file_revisions_a = [fr for fr in file_revisions_a
                                     if fr.path in modified_file_revisions_by_path]
        modified_file_revisions_b = [fr for fr in file_revisions_b
                                     if fr.path in modified_file_revisions_by_path]

        if modified_file_revisions_a:
            #to do: check the file revisions chunk-wise to avoid DB query errors
            issue_occurrences_a = self.project.backend.filter(IssueOccurrence,
                                         {
                                            'file_revision' : {'$in' : modified_file_revisions_a}
                                         },
                                      include = ('file_revision','issue'))
        else:
            issue_occurrences_a = []

        if modified_file_revisions_b:
            #to do: check the file revisions chunk-wise to avoid DB query errors
            issue_occurrences_b = self.project.backend.filter(IssueOccurrence,
                                           {
                                            'file_revision' : {'$in' : modified_file_revisions_b}
                                            },
                                      include = ('file_revision','issue'))
        else:
            issue_occurrences_b = []

        logger.debug("Diffing issues (%d in A, %d in B)" % (len(issue_occurrences_a),
                                                           len(issue_occurrences_b)))

        issue_occurrences_diff = diff_objects(issue_occurrences_a,
                                              issue_occurrences_b,
                                              issue_occurrence_key,
                                              issue_occurrence_comparator)

        logger.debug("Diffing summary...")
        summary_diff = self.diff_summaries(snapshot_a,snapshot_b)

        if diff is None:
            diff = Diff({'summary' : summary_diff,
                        'snapshot_a' : snapshot_a,
                        'project' : self.project,
                        'configuration' : self.project.configuration,
                        'snapshot_b' : snapshot_b})
            #we generate the hash value for this diff
            hasher = Hasher()
            hasher.add(diff.snapshot_a.hash)
            hasher.add(diff.snapshot_b.hash)
            diff.hash = hasher.digest.hexdigest()
        elif save:
            with self.project.backend.transaction():
                self.project.backend.filter(DiffFileRevision,{'diff' : diff}).delete()
                self.project.backend.filter(DiffIssueOccurrence,{'diff' : diff}).delete()
        if save:
            with self.project.backend.transaction():
                self.project.backend.save(diff)

        diff_file_revisions = []

        with self.project.backend.transaction():
            for key,file_revisions in file_revisions_diff.items():
                for file_revision in file_revisions:
                    hasher = Hasher()
                    hasher.add(file_revision.hash)
                    hasher.add(diff.hash)
                    hasher.add(key)
                    diff_file_revision = DiffFileRevision({
                                            'diff' : diff,
                                            'file_revision' : file_revision,
                                            'hash' : hasher.digest.hexdigest(),
                                            'key' : key})
                    if save:
                        self.project.backend.save(diff_file_revision)
                    diff_file_revisions.append(diff_file_revision)

        diff_issue_occurrences = []
        mapping = {'deleted' : 'fixed','added' : 'added'}
        with self.project.backend.transaction():
            for key,issue_occurrences in issue_occurrences_diff.items():
                if not key in mapping:
                    continue
                for issue_occurrence in issue_occurrences:
                    hasher = Hasher()
                    hasher.add(issue_occurrence.hash)
                    hasher.add(diff.hash)
                    hasher.add(mapping[key])
                    diff_issue_occurrence = DiffIssueOccurrence({
                            'diff' : diff,
                            'hash' : hasher.digest.hexdigest(),
                            'issue_occurrence' : issue_occurrence,
                            'key' : mapping[key]
                        })
                    if save:
                        self.project.backend.save(diff_issue_occurrence)
                    diff_issue_occurrences.append(diff_issue_occurrence)

        return diff,diff_file_revisions,diff_issue_occurrences


Instruction:
Can you generate a brief explanation for the following Python 3 code
def analyze(self,file_revisions, save_if_empty = False, snapshot=None):

        """
        Handling dependencies:

        * First, genreate a list of file revisions for this snapshot
        * Then, check which ones of of them already exist
        * For the existing ones, check their dependencies
        * If any of the dependencies are outdated, add the dependent file revision to the analyze list

        How to handle hashes? Dependencies should be included in the hash sum.

        * Just load the files based on their SHA values
        * Check if dependencies match with the current set based on SHA values
        * If not, re-analyze the file revision
        * After analysis, calculate the hash value based on path, SHA and dependencies
        """

        logger.debug("Analyzing code environment...")

        if snapshot is None:
            snapshot = Snapshot()
            snapshot.configuration = self.project.configuration

        file_revisions_by_pk = dict([(fr.hash,fr) for fr in file_revisions])

        filtered_file_revisions = self.filter_file_revisions(file_revisions)
        filtered_file_revisions_by_pk = dict([(fr.hash,fr) for fr in filtered_file_revisions])

        excluded_file_revisions = [file_revisions_by_pk[pk]
                                    for pk in file_revisions_by_pk.keys()
                                    if not pk in filtered_file_revisions_by_pk
                                  ]

        logger.info("Excluding %d file revisions" % len(excluded_file_revisions))

        file_revisions = filtered_file_revisions
        file_revisions_by_pk = filtered_file_revisions_by_pk

        max_file_revisions = 10000

        if len(file_revisions) > max_file_revisions:

            logger.warning("Too many file revisions (%d) in snapshot, truncating at %d" %
                         (len(file_revisions),max_file_revisions))
            file_revisions_by_pk = dict(sorted(file_revisions_by_pk.items(),
                                               key = lambda x:x[0])[:max_file_revisions])
            file_revisions = file_revisions_by_pk.values()

        i = 0
        chunk_size = 50
        existing_file_revisions = []
        file_revisions_by_pk_keys = file_revisions_by_pk.keys()

        #we only check 50 keys at a time and then incrementally save them
        while i < len(file_revisions_by_pk_keys):
            file_revisions_by_pk_chunk = file_revisions_by_pk_keys[i:i+chunk_size]
            if not file_revisions_by_pk_chunk:
                break
            existing_file_revisions.extend(list(self.project.backend.filter(FileRevision,{
                    'project' : self.project,
                    'hash' : {'$in' : file_revisions_by_pk_chunk}
                    })))
            i+=chunk_size

        existing_file_revisions_by_pk = dict([(fr.hash,fr) for fr in existing_file_revisions])
        new_file_revisions = [file_revision for file_revision in file_revisions
                                if not file_revision.hash in existing_file_revisions_by_pk]

        new_file_revisions = []

        for file_revision in file_revisions:
            if not file_revision.hash in existing_file_revisions_by_pk:
                file_revision.configuration = self.project.configuration
                new_file_revisions.append(file_revision)
            elif existing_file_revisions_by_pk[file_revision.hash].configuration != self.project.configuration:
                #we replace the pk and configuration values of the new file_revision object, so that
                #it will overwrite the old version...
                file_revision.pk = existing_file_revisions_by_pk[file_revision.hash].pk
                file_revision.configuration = self.project.configuration
                new_file_revisions.append(file_revision)

        file_revisions_dict = {}

        for file_revision in existing_file_revisions+new_file_revisions:
            file_revisions_dict[file_revision.path] = file_revision

        logger.info("Analyzing %d new file revisions (%d are already analyzed)" % (
                len(new_file_revisions),
                len(existing_file_revisions)
                ))
        i = 0

        #We set the project information in the snapshot.
        snapshot.project = self.project
        snapshot.file_revisions = file_revisions_dict.values()
        self.env['snapshot'] = snapshot

        try:
            while i < len(new_file_revisions):
                j = i+10 if i+10 < len(new_file_revisions) else len(new_file_revisions)
                logger.info("Analyzing and saving: %d - %d (%d remaining)" %
                    (i, j, len(new_file_revisions) - i ))
                file_revisions_slice = new_file_revisions[i:j]
                analyzed_file_revisions = self.analyze_file_revisions(file_revisions_slice)
                logger.info("Annotating and saving file revisions...")
                self.save_file_revisions(snapshot,analyzed_file_revisions)
                i+=10
            logger.info("Summarizing file revisions...")
            snapshot.summary = self.summarize(file_revisions_dict.values())
        finally:
            del self.env['snapshot']

        snapshot.analyzed = True

        logger.info("Saving snapshot...")

        with self.project.backend.transaction():
            self.project.backend.save(snapshot)

        logger.info("Done analyzing snapshot %s" % snapshot.pk)

        return snapshot

Response: Analyzes the code environment and returns a dictionary of file revision names and their values.


Instruction:
Given the following Python 3 function, write the documentation
def save_file_revisions(self,snapshot,file_revisions):

        annotations = defaultdict(list)

        for file_revision in file_revisions:
            issues_results = {}

            for analyzer_name,results in file_revision.results.items():

                if 'issues' in results:
                    issues_results[analyzer_name] = results['issues']
                    del results['issues']
                    if len(issues_results) > 1000:
                        issues_results[analyzer_name] = [{
                                'code' : 'TooManyIssues',
                                'analyzer' : analyzer_name,
                            }]

            with self.project.backend.transaction():
                self.project.backend.save(file_revision)

            def location_sorter(issue):
                if issue['location'] and issue['location'][0] and issue['location'][0][0]:
                    return issue['location'][0][0][0]
                return 0

            with self.project.backend.transaction():
                for analyzer_name,issues in issues_results.items():
                    grouped_issues = group_issues_by_fingerprint(issues)
                    for issue_dict in grouped_issues:

                        hasher = Hasher()
                        hasher.add(analyzer_name)
                        hasher.add(issue_dict['code'])
                        hasher.add(issue_dict['fingerprint'])
                        issue_dict['hash'] = hasher.digest.hexdigest()

                        try:
                            #we check if the issue already exists
                            issue = self.project.backend.get(Issue,{'hash' : issue_dict['hash'],
                                                                    'project' : self.project
                                                                   })
                        except Issue.DoesNotExist:
                            #if not, we create it
                            d = issue_dict.copy()
                            d['analyzer'] = analyzer_name
                            if 'location' in d:
                                del d['location']
                            if 'occurrences' in d:
                                del d['occurrences']
                            issue = Issue(d)
                            issue.project = self.project
                            self.project.backend.save(issue)

                        for occurrence in issue_dict['occurrences']:
                            hasher = Hasher()
                            hasher.add(file_revision.hash)
                            hasher.add(issue.hash)
                            hasher.add(occurrence.get('from_row'))
                            hasher.add(occurrence.get('from_column'))
                            hasher.add(occurrence.get('to_row'))
                            hasher.add(occurrence.get('to_column'))
                            hasher.add(occurrence.get('sequence'))
                            occurrence['hash'] = hasher.digest.hexdigest()

                            try:
                                #we check if the occurrence already exists
                                occurrence = self.project.backend.get(IssueOccurrence,{'hash' : occurrence['hash'],
                                                                                'issue' : issue
                                                                                })
                            except IssueOccurrence.DoesNotExist:
                                #if not, we create it
                                occurrence = IssueOccurrence(occurrence)
                                occurrence.issue = issue
                                occurrence.file_revision = file_revision
                                self.project.backend.save(occurrence)
                            annotations['occurrences'].append(occurrence)
                        annotations['issues'].append(issue)

        return annotations

Response: Save the file revisions to the database.


Instruction:
Can you create a Python 3 function that
tries to find a directory with a. git repository

Response:
def find_git_repository(self, path):
        """
        Tries to find a directory with a .git repository
        """
        while path is not None:
            git_path = os.path.join(path,'.git')
            if os.path.exists(git_path) and os.path.isdir(git_path):
                return path
            path = os.path.dirname(path)
        return None


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

    project_path = get_project_path()
    project_config = get_project_config(project_path)
    backend = get_backend(project_path,project_config,initialize_db = False)
    url = str(backend.engine.url)
    with backend.transaction():
        context.configure(
            connection=backend.connection,
            url=url, target_metadata=backend.metadata,
            literal_binds=True)

        with context.begin_transaction():
            context.run_migrations()

Response: Run migrations in offline mode.


Instruction:
How would you implement a function in Python 3 that
runs migrations in online mode.

Response:
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    print("Running migrations online")

    project_path = get_project_path()
    project_config = get_project_config(project_path)

    backend = get_backend(project_path,project_config,initialize_db = False)

    context.configure(
        connection=backend.connection,
        target_metadata=backend.metadata,
    )

    with context.begin_transaction():
        context.run_migrations()


Instruction:
Here you have a function in Python 3, explain what it does
def get_hash(node,fields = None,exclude = ['pk','_id'],target = 'pk'):

    hasher = Hasher()

    def add_to_hash(value):

        if isinstance(value,dict):
            if target in value:
                add_to_hash(value[target])
            else:
                attribute_list = []
                for key,v in sorted(value.items(),key = lambda x: x[0]):

                    if (fields is not None and key not in fields) \
                        or (exclude is not None and key in exclude):
                        continue
                    add_to_hash(key)
                    add_to_hash(v)
        elif isinstance(value,(tuple,list)) and value and isinstance(value[0],(dict,node_class)):
            for i,v in enumerate(value):
                hasher.add(i)
                add_to_hash(v)
        else:
            hasher.add(value)

    add_to_hash(node)

    return hasher.digest.hexdigest()

Response: This function generates a unique hash for a given node in the syntax tree.


Instruction:
Write a Python 3 function for
adding a message to the list of messages to send

Response:
def add_message(self, msg_id, location, msg):
        """Client API to send a message"""

        self._messages.append((msg_id,location,msg))


Instruction:
Can you generate the documentation for the following Python 3 function
def get_fingerprint_from_code(self,file_revision,location, extra_data=None):
        code = file_revision.get_file_content()
        if not isinstance(code,unicode):
            code = unicode(code,errors = 'ignore')
        lines = code.split(u"\n")
        s = ""
        for l in location:
            ((from_row,from_column),(to_row,to_column)) = l
            if from_column is None:
                continue
            if from_row == to_row:
                s+=lines[from_row-1][from_column:to_column]
            else:
                if to_row < from_row:
                    raise ValueError("from_row must be smaller than to_row")
                s+=lines[from_row-1][from_column:]
                current_row = from_row+1
                while current_row < to_row:
                    s+=lines[current_row-1]
                    current_row+=1
                s+=lines[current_row-1][:to_column]

        hasher = Hasher()
        hasher.add(s)

        if extra_data is not None:
            hasher.add(extra_data)

        return hasher.digest.hexdigest()

Response: This function generates a fingerprint from a series of code snippets.


Instruction:
How would you code a function in Python 3 to
group issues by fingerprint.

Response:
def group_issues_by_fingerprint(issues):
    """
    Groups issues by fingerprint. Grouping is done by issue code in addition.
    IMPORTANT: It is assumed that all issues come from the SAME analyzer.
    """
    issues_by_fingerprint = defaultdict(list)
    for issue in issues:
        if not 'fingerprint' in issue:
            raise AttributeError("No fingerprint defined for issue with analyzer %s and code %s!" %
                (issue.get('analyzer','(undefined)'),issue['code']))
        fp_code = "%s:%s" % (issue['fingerprint'],issue['code'])
        if fp_code in issues_by_fingerprint:
            grouped_issue = issues_by_fingerprint[fp_code]
        else:
            grouped_issue = issue.copy()
            grouped_issue['occurrences'] = []
            if 'location' in grouped_issue:
                del grouped_issue['location']
            issues_by_fingerprint[fp_code] = grouped_issue

        locations = issue.get('location',[])
        if locations:
            for i,start_stop in enumerate(locations):

                occurrence = {
                    'from_row' : None,
                    'to_row' : None,
                    'from_column' : None,
                    'to_column' : None,
                    'sequence' : i
                }

                grouped_issue['occurrences'].append(occurrence)

                if not isinstance(start_stop,(list,tuple)) or not len(start_stop) == 2:
                    continue

                start,stop = start_stop

                if isinstance(start,(list,tuple)) and len(start) == 2:
                    occurrence['from_row'] = start[0]
                    occurrence['from_column'] = start[1]

                if isinstance(stop,(list,tuple)) and len(stop) == 2:
                    occurrence['to_row'] = stop[0]
                    occurrence['to_column'] = stop[1]

            grouped_issue['occurrences'] = sorted(grouped_issue['occurrences'],key = lambda x: (x['from_row'],x['from_column']))

    return issues_by_fingerprint.values()


Instruction:
Write a Python 3 function for
retrieving the issue classes for a given backend and enabled or disabled.

Response:
def get_issue_classes(self,backend = None,enabled = True,sort = None,**kwargs):
        """
        Retrieves the issue classes for a given backend

        :param backend: A backend to use. If None, the default backend will be used
        :param enabled: Whether to retrieve enabled or disabled issue classes.
                        Passing `None` will retrieve all issue classes.
        """
        if backend is None:
            backend = self.backend

        query = {'project_issue_classes.project' : self}
        if enabled is not None:
            query['project_issue_classes.enabled'] = enabled

        issue_classes = backend.filter(self.IssueClass,query,
                                   **kwargs)

        if sort is not None:
            issue_classes = issue_classes.sort(sort)

        return issue_classes


Instruction:
Can you create a Python 3 function that
validates that the input text contains a valid symbol.

Response:
def validate_symbol(text):
    """
    Verifying whether symbol(s) included in the ``text`` or not.

    :param str text: Input text.
    :raises pathvalidate.InvalidCharError:
        If symbol(s) included in the ``text``.
    """

    match_list = __RE_SYMBOL.findall(preprocess(text))
    if match_list:
        raise InvalidCharError("invalid symbols found: {}".format(match_list))


Instruction:
Here you have a function in Python 3, explain what it does
def replace_symbol(text, replacement_text="", is_replace_consecutive_chars=False, is_strip=False):

    try:
        new_text = __RE_SYMBOL.sub(replacement_text, preprocess(text))
    except (TypeError, AttributeError):
        raise TypeError("text must be a string")

    if not replacement_text:
        return new_text

    if is_replace_consecutive_chars:
        new_text = re.sub("{}+".format(re.escape(replacement_text)), replacement_text, new_text)

    if is_strip:
        new_text = new_text.strip(replacement_text)

    return new_text

Response: Replaces all of the symbols in the text.


Instruction:
Make a summary of the following Python 3 code
def validate_filename(filename, platform=None, min_len=1, max_len=_DEFAULT_MAX_FILENAME_LEN):

    FileNameSanitizer(platform=platform, min_len=min_len, max_len=max_len).validate(filename)

Response: Validates that the filename is a valid file name or not.


Instruction:
Can you write a function in Python 3 where it
validates that the file_path is a valid file path.

Response:
def validate_filepath(file_path, platform=None, min_len=1, max_len=None):
    """Verifying whether the ``file_path`` is a valid file path or not.

    Args:
        file_path (str):
            File path to validate.
        platform (str, optional):
            .. include:: platform.txt
        min_len (int, optional):
            Minimum length of the ``file_path``. The value must be greater or equal to one.
            Defaults to ``1``.
        max_len (int, optional):
            Maximum length of the ``file_path`` length. If the value is |None|,
            in the default, automatically determined by the ``platform``:

                - ``Linux``: 4096
                - ``macOS``: 1024
                - ``Windows``: 260

    Raises:
        NullNameError:
            If the ``file_path`` is empty.
        InvalidCharError:
            If the ``file_path`` includes invalid char(s):
            |invalid_file_path_chars|.
            The following characters are also invalid for Windows platform:
            |invalid_win_file_path_chars|
        InvalidLengthError:
            If the ``file_path`` is longer than ``max_len`` characters.

    Example:
        :ref:`example-validate-file-path`

    See Also:
        `Naming Files, Paths, and Namespaces (Windows)
        <https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx>`__
    """

    FilePathSanitizer(platform=platform, min_len=min_len, max_len=max_len).validate(file_path)


Instruction:
Here you have a function in Python 3, explain what it does
def sanitize_filename(
    filename, replacement_text="", platform=None, max_len=_DEFAULT_MAX_FILENAME_LEN
):
    """Make a valid filename from a string.

    To make a valid filename the function does:

        - Replace invalid characters as file names included in the ``filename``
          with the ``replacement_text``. Invalid characters are:

            - unprintable characters
            - |invalid_filename_chars|
            - for Windows only: |invalid_win_filename_chars|

        - Append underscore (``"_"``) at the tail of the name if sanitized name
          is one of the reserved names by the operating system.

    Args:
        filename (str or PathLike object): Filename to sanitize.
        replacement_text (str, optional):
            Replacement text for invalid characters. Defaults to ``""``.
        platform (str, optional):
            .. include:: platform.txt
        max_len (int, optional):
            The upper limit of the ``filename`` length. Truncate the name length if
            the ``filename`` length exceeds this value.
            Defaults to ``255``.

    Returns:
        Same type as the ``filename`` (str or PathLike object):
            Sanitized filename.

    Raises:
        ValueError:
            If the ``filename`` is an invalid filename.

    Example:
        :ref:`example-sanitize-filename`
    """

    return FileNameSanitizer(platform=platform, max_len=max_len).sanitize(
        filename, replacement_text
    )

Response: Make a valid filename from a string.


Instruction:
Can you create a Python 3 function that
sanitizes a file path.

Response:
def sanitize_filepath(file_path, replacement_text="", platform=None, max_len=None):
    """Make a valid file path from a string.

    Replace invalid characters for a file path within the ``file_path``
    with the ``replacement_text``.
    Invalid characters are as followings:
    |invalid_file_path_chars|, |invalid_win_file_path_chars| (and non printable characters).

    Args:
        file_path (str or PathLike object):
            File path to sanitize.
        replacement_text (str, optional):
            Replacement text for invalid characters.
            Defaults to ``""``.
        platform (str, optional):
            .. include:: platform.txt
        max_len (int, optional):
            The upper limit of the ``file_path`` length. Truncate the name if the ``file_path``
            length exceedd this value. If the value is |None|, the default value automatically
            determined by the execution platform:

                - ``Linux``: 4096
                - ``macOS``: 1024
                - ``Windows``: 260

    Returns:
        Same type as the argument (str or PathLike object):
            Sanitized filepath.

    Raises:
        ValueError:
            If the ``file_path`` is an invalid file path.

    Example:
        :ref:`example-sanitize-file-path`
    """

    return FilePathSanitizer(platform=platform, max_len=max_len).sanitize(
        file_path, replacement_text
    )


Instruction:
Can you implement a function in Python 3 that
validates that label is a valid LTSV label.

Response:
def validate_ltsv_label(label):
    """
    Verifying whether ``label`` is a valid
    `Labeled Tab-separated Values (LTSV) <http://ltsv.org/>`__ label or not.

    :param str label: Label to validate.
    :raises pathvalidate.NullNameError: If the ``label`` is empty.
    :raises pathvalidate.InvalidCharError:
        If invalid character(s) found in the ``label`` for a LTSV format label.
    """

    validate_null_string(label, error_msg="label is empty")

    match_list = __RE_INVALID_LTSV_LABEL.findall(preprocess(label))
    if match_list:
        raise InvalidCharError(
            "invalid character found for a LTSV format label: {}".format(match_list)
        )


Instruction:
Write a Python 3 script for
sanitizing a LTSV label.

Response:
def sanitize_ltsv_label(label, replacement_text=""):
    """
    Replace all of the symbols in text.

    :param str label: Input text.
    :param str replacement_text: Replacement text.
    :return: A replacement string.
    :rtype: str
    """

    validate_null_string(label, error_msg="label is empty")

    return __RE_INVALID_LTSV_LABEL.sub(replacement_text, preprocess(label))


Instruction:
How would you code a function in Python 3 to
return the domain from the url

Response:
def domain(self):
        """
        Return domain from the url
        """
        remove_pac = self.cleanup.replace(
            "https://", "").replace("http://", "").replace("www.", "")
        try:
            return remove_pac.split('/')[0]
        except:
            return None


Instruction:
How would you explain what the following Python 3 function does
def to_xml(self):
        root = ET.Element("entry")
        for x in self.xml_tags:
            if getattr(self, x):
                if x in ['episodes', 'scores', 'status', 'dates', 'storage', 'rewatched', 'flags', 'tags']:
                    if x == 'episodes':
                        if self.episodes.current:
                            temp = ET.SubElement(root, 'episode')
                            temp.text = str(self.episodes.current)
                    elif x == 'scores':
                        if self.scores.user:
                            temp = ET.SubElement(root, 'score')
                            temp.text = str(self.scores.user)
                    elif x == 'status':
                        if self.status.user:
                            temp = ET.SubElement(root, 'status')
                            temp.text = str(self.status.user)
                    elif x == 'dates':
                        if self.dates.user.start:
                            start = ET.SubElement(root, 'date_start')
                            start.text = format_date(self.dates.user.start)
                        if self.dates.user.end:
                            end = ET.SubElement(root, 'date_finish')
                            end.text = format_date(self.dates.user.end)
                    elif x == 'storage':
                        if self.storage.type:
                            stype = ET.SubElement(root, 'storage_type')
                            stype.text = str(self.storage.type)
                        if self.storage.value:
                            sval = ET.SubElement(root, 'storage_value')
                            sval.text = str(self.storage.value)
                    elif x == 'rewatched':
                        if self.rewatched.times:
                            rt = ET.SubElement(root, 'times_rewatched')
                            rt.text = str(self.rewatched.times)
                        if self.rewatched.value:
                            rv = ET.SubElement(root, 'rewatch_value')
                            rv.text = str(self.rewatched.value)
                    elif x == 'flags':
                        if self.flags.discussion:
                            df = ET.SubElement(root, 'enable_discussion')
                            df.text = '1' if self.flags.discussion else '0'
                        if self.flags.rewatching:
                            rf = ET.SubElement(root, 'enable_rewatching')
                            rf.text = '1' if self.flags.rewatching else '0'
                    else:
                        if self.tags:
                            temp = ET.SubElement(root, 'tags')
                            temp.text = ','.join(self.tags)
                else:
                    temp = ET.SubElement(root, x)
                    temp.text = str(getattr(self, x))
        return '<?xml version="1.0" encoding="UTF-8"?>{}'.format(ET.tostring(root, encoding="unicode"))

Response: Convert data to XML String.


Instruction:
Given the following Python 3 function, write the documentation
def search(self, term):
        r = requests.get(self.apiurl + "/users", params={"filter[name]": term}, headers=self.header)

        if r.status_code != 200:
            raise ServerError

        jsd = r.json()

        if jsd['meta']['count']:
            return SearchWrapper(jsd['data'], jsd['links']['next'] if 'next' in jsd['links'] else None, self.header)
        else:
            return None

Response: Search for a user by name.


Instruction:
Implement a Python 3 function for
creating a user. Please review the attributes required. You need only provide the attributes.

Response:
def create(self, data):
        """
        Create a user. Please review the attributes required. You need only provide the attributes.

        :param data: A dictionary of the required attributes
        :return: Dictionary returned by server or a ServerError exception
        :rtype: Dictionary or Exception
        """
        final_dict = {"data": {"type": "users", "attributes": data}}
        r = requests.post(self.apiurl + "/users", json=final_dict, headers=self.header)

        if r.status_code != 200:
            raise ServerError

        return r.json()


Instruction:
Here you have a function in Python 3, explain what it does
def get(self, uid):
        r = requests.get(self.apiurl + "/users/{}".format(uid), headers=self.header)

        if r.status_code != 200:
            raise ServerError

        jsd = r.json()

        if jsd['data']:
            return jsd['data']
        else:
            return None

Response: Get a user s information by their id.


Instruction:
Can you generate the documentation for the following Python 3 function
def update(self, uid, data, token):
        final_dict = {"data": {"id": uid, "type": "users", "attributes": data}}
        final_headers = self.header
        final_headers['Authorization'] = "Bearer {}".format(token)
        r = requests.patch(self.apiurl + "/users/{}".format(uid), json=final_dict, headers=final_headers)

        if r.status_code != 200:
            raise ServerError

        return True

Response: Update a user s data attributes. Requires an auth token.


Instruction:
Can you generate the documentation for the following Python 3 function
def character(self, term, page = 1, perpage = 3):
        query_string = """\
            query ($query: String, $page: Int, $perpage: Int) {
                Page (page: $page, perPage: $perpage) {
                    pageInfo {
                        total
                        currentPage
                        lastPage
                        hasNextPage
                    }
                    characters (search: $query) {
                        id
                        name {
                            first
                            last
                        }
                        image {
                            large
                        }
                    }
                }
            }
        """
        vars = {"query": term, "page": page, "perpage": perpage}
        r = requests.post(self.settings['apiurl'],
                          headers=self.settings['header'],
                          json={'query': query_string, 'variables': vars})
        jsd = r.text

        try:
            jsd = json.loads(jsd)
        except ValueError:
            return None
        else:
            return jsd

Response: Search for a character by term.