Instruction:
Can you tell what is the following Python 3 function doing
def build_options(payload, options, maxsize = 576, overload = OVERLOAD_FILE | OVERLOAD_SNAME, allowpartial = True):
    '''
    Split a list of options

    This is the reverse operation of `reassemble_options`, it splits `dhcp_option` into
    `dhcp_option_partial` if necessary, and set overload option if field overloading is
    used.

    :param options: a list of `dhcp_option`

    :param maxsize: Limit the maximum DHCP message size. If options cannot fit into the DHCP
                    message, specified fields are overloaded for options. If options cannot
                    fit after overloading, extra options are DROPPED if allowpartial = True.

                    It is important to sort the dhcp options by priority.

    :param overload: fields that are allowed to be overloaded

    :param allowpartial: When options cannot fit into the DHCP message, allow the rest options
                         to be dropped.

    :return: Number of options that are dropped i.e. `options[:-return_value]` are dropped
    '''
    if maxsize < 576:
        maxsize = 576
    max_options_size = maxsize - 240
    # Ignore OPTION_PAD and OPTION_END
    options = [o for o in options if o.tag not in (OPTION_PAD, OPTION_END)]
    # Only preserve data
    option_data = [(o.tag, o._tobytes()[2:]) for o in options]
    def split_options(option_data, limits):
        """
        Split options into multiple fields

        :param option_data: list of (tag, data) pair

        :param limits: list of int for limit of each field (excluding PAD and END)

        :return: number of options that are dropped
        """
        # List of (dhcp_option_partial, option_not_finished)
        partial_options = []
        buffers = [0]
        if not options:
            return ([], 0)
        def create_result():
            # Remove any unfinished partial options
            while partial_options and partial_options[-1][1]:
                partial_options.pop()
            buffers.append(len(partial_options))
            r = [[po for po,_ in partial_options[buffers[i]:buffers[i+1]]] for i in range(0, len(buffers) - 1)]
            # Remove empty fields
            while r and not r[-1]:
                r.pop()
            return r
        # Current field used size
        current_size = 0
        limit_iter = iter(limits)
        try:
            next_limit = next(limit_iter)
        except (StopIteration, GeneratorExit):
            return ([], False)
        for i, (tag, data) in enumerate(option_data):
            # Current used data size
            data_size = 0
            # Do not split very small options on boundary, this may prevent some broken DHCP clients/servers
            # to cause problem
            nosplit = (len(data) <= 32)
            while True:
                # next partial option size should be:
                # 1. no more than the current field limit (minus 1-byte tag and 1-byte length)
                # 2. no more than the single dhcp_option_partial data limit (255 due to single byte length)
                # 3. no more than the rest data size
                next_size = min(next_limit - current_size - 2, 255, len(data) - data_size)
                if next_size < 0 or (next_size == 0 and data_size < len(data)) \
                        or (next_size < len(data) - data_size and nosplit):
                    # Cannot put this part of data on the current field, find the next field
                    try:
                        next_limit = next(limit_iter)
                    except (StopIteration, GeneratorExit):
                        return (create_result(), len(option_data) - i)
                    # Record field boundary
                    buffers.append(len(partial_options))
                    current_size = 0
                else:
                    # Put this partial option on current field
                    partial_options.append((dhcp_option_partial(tag = tag, data = data[data_size : data_size + next_size]),
                                            (next_size < len(data) - data_size)))
                    data_size += next_size
                    current_size += next_size + 2
                    if data_size >= len(data):
                        # finished current option
                        break
        return (create_result(), 0)
    # First try to fit all options in options field
    # preserve a byte for OPTION_END
    result, not_finished = split_options(option_data, [max_options_size - 1])
    if not_finished:
        if overload & (OVERLOAD_FILE | OVERLOAD_SNAME):
            # Try overload
            # minus a overload option (1-byte tag, 1-byte lenght, 1-byte dhcp_overload) and 1-byte OPTION_END
            limits = [max_options_size - 4]
            if overload & OVERLOAD_FILE:
                # preserve a byte for OPTION_END
                limits.append(127)
            if overload & OVERLOAD_SNAME:
                # preserve a byte for OPTION_END
                limits.append(63)
            result2, not_finished2 = split_options(option_data, limits)
            # Only overload if we have a better result
            if len(result2) > 1:
                result = result2
                not_finished = not_finished2
    if not allowpartial and not_finished:
        raise ValueError("%d options cannot fit into a DHCP message" % (not_finished,))
    if not result:
        return not_finished
    elif len(result) <= 1:
        # No overload
        payload.options = result[0] + [dhcp_option_partial(tag = OPTION_END)]
    else:
        overload_option = 0
        if len(result) >= 2 and result[1]:
            overload_option |= OVERLOAD_FILE
            # overload file field
            payload.file = dhcp_option_partial[0].tobytes(result[1] + [dhcp_option_partial(tag = OPTION_END)])
        if len(result) >= 3 and result[2]:
            overload_option |= OVERLOAD_SNAME
            # overload sname field
            payload.sname = dhcp_option_partial[0].tobytes(result[2] + [dhcp_option_partial(tag = OPTION_END)])
        # Put an overload option before any other options
        payload.options = [dhcp_option_partial(tag = OPTION_OVERLOAD, data = dhcp_overload.tobytes(overload_option))] \
                        + result[0] + [dhcp_option_partial(tag = OPTION_END)]
    return not_finished

Response: Builds the dhcp options for a given message.


Instruction:
Can you write a function in Python 3 where it
creates DHCP option from value.

Response:
def create_option_from_value(tag, value):
    """
    Set DHCP option with human friendly value
    """
    dhcp_option.parser()
    fake_opt = dhcp_option(tag = tag)
    for c in dhcp_option.subclasses:
        if c.criteria(fake_opt):
            if hasattr(c, '_parse_from_value'):
                return c(tag = tag, value = c._parse_from_value(value))
            else:
                raise ValueError('Invalid DHCP option ' + str(tag) + ": " + repr(value))
    else:
        fake_opt._setextra(_tobytes(value))
        return fake_opt


Instruction:
How would you explain what the following Python 3 function does
def create_dhcp_options(input_dict, ignoreError = False, generateNone = False):
    retdict = {}
    for k,v in dict(input_dict).items():
        try:
            if generateNone and v is None:
                retdict[k] = None
            else:
                try:
                    retdict[k] = create_option_from_value(k, v)
                except _EmptyOptionException:
                    if generateNone:
                        retdict[k] = None
        except Exception:
            if ignoreError:
                continue
            else:
                raise
    return retdict

Response: Try to create dhcp_options from human friendly values ignoring invalid values ignoring invalid values generateNone = True


Instruction:
Here you have a function in Python 3, explain what it does
def with_indices(*args):
    '''
    Create indices for an event class. Every event class must be decorated with this decorator.
    '''
    def decorator(cls):
        for c in cls.__bases__:
            if hasattr(c, '_indicesNames'):
                cls._classnameIndex = c._classnameIndex + 1
                for i in range(0, cls._classnameIndex):
                    setattr(cls, '_classname' + str(i), getattr(c, '_classname' + str(i)))
                setattr(cls, '_classname' + str(cls._classnameIndex), cls._getTypename())
                cls._indicesNames = c._indicesNames + ('_classname' + str(cls._classnameIndex),) + args
                cls._generateTemplate()
                return cls
        cls._classnameIndex = -1
        cls._indicesNames = args
        cls._generateTemplate()
        return cls
    return decorator

Response: Decorator to create indices for an event class. Every event class must be decorated with this decorator.


Instruction:
Write a Python 3 script to
return two way difference between two sets of keys.

Response:
def two_way_difference(self, b, extra_add = (), extra_remove = ()):
        """
        Return (self - b, b - self)
        """
        if self is b:
            return ((), ())
        if isinstance(b, DiffRef_):
            extra_remove = extra_remove + b.add
            b = b.origin
        if extra_add == extra_remove:
            extra_add = extra_remove = ()
        if isinstance(b, Diff_):
            if self.base is b.base:
                first = self.add + b.remove
                second = self.remove + b.add
            elif self.base is b:
                first = self.add
                second = self.remove
            elif b.base is self:
                first = b.remove
                second = b.add
            else:
                first = self
                second = b
        else:
            first = self
            second = b
        if not first and not extra_add:
            return ((), tuple(second) + tuple(extra_remove))
        elif not second and not extra_remove:
            return (tuple(first) + tuple(extra_add), ())
        else:
            first = set(first)
            first.update(extra_add)
            second = set(second)
            second.update(extra_remove)
            return tuple(first.difference(second)), tuple(second.difference(first))


Instruction:
How would you explain what the following Python 3 function does
def getTypename(cls):
        '''
        :returns: return the proper name to match
        '''
        if cls is Event:
            return None
        else:
            for c in cls.__bases__:
                if issubclass(c, Event):
                    if c is Event:
                        return cls._getTypename()
                    else:
                        return c.getTypename()

Response: returns the proper name to match


Instruction:
Make a summary of the following Python 3 code
def createMatcher(cls, *args, **kwargs):
        '''
        :param _ismatch: user-defined function ismatch(event) for matching test
        :param \*args: indices
        :param \*\*kwargs: index_name=index_value for matching criteria
        '''
        if kwargs and not args:
            return EventMatcher(tuple(getattr(cls, ind) if ind[:10] == '_classname' else kwargs.get(ind) for ind in cls.indicesNames()), kwargs.get('_ismatch'))
        else:
            return EventMatcher(tuple(cls._generateIndices(args)), kwargs.get('_ismatch'))

Response: Create a new EventMatcher object for the given class.


Instruction:
Create a Python 3 function for
limiting the number of resources acquired by the current task.

Response:
async def limit(self, use = 1):
        """
        Acquire "resources", wait until enough "resources" are acquired. For each loop,
        `limit` number of "resources" are permitted.

        :param use: number of "resouces" to be used.

        :return: True if is limited
        """
        c = self._counter
        self._counter = c + use
        if self._task is None:
            self._task = self._container.subroutine(self._limiter_task(), False)
        if c >= self._bottom_line:
            # Limited
            await RateLimitingEvent.createMatcher(self, c // self._limit)
            return True
        else:
            return False


Instruction:
Implement a Python 3 function for
running task in task pool. Raise an exception or return the return value

Response:
async def run_task(self, container, task, newthread = False):
        "Run task() in task pool. Raise an exception or return the return value"
        e = TaskEvent(self, task=task, newthread = newthread)
        await container.wait_for_send(e)
        ev = await TaskDoneEvent.createMatcher(e)
        if hasattr(ev, 'exception'):
            raise ev.exception
        else:
            return ev.result


Instruction:
Explain what the following Python 3 code does
async def run_gen_task(self, container, gentask, newthread = True):
        "Run generator gentask() in task pool, yield customized events"
        e = TaskEvent(self, gen_task = gentask, newthread = newthread)
        await container.wait_for_send(e)
        ev = await TaskDoneEvent.createMatcher(e)
        if hasattr(ev, 'exception'):
            raise ev.exception
        else:
            return ev.result

Response: Run generator gentask() in task pool yield customized events


Instruction:
Implement a Python 3 function for
running asynctask in task pool call sender ( events to send customized events return result

Response:
async def run_async_task(self, container, asynctask, newthread = True):
        "Run asynctask(sender) in task pool, call sender(events) to send customized events, return result"
        e = TaskEvent(self, async_task = asynctask, newthread = newthread)
        await container.wait_for_send(e)
        ev = await TaskDoneEvent.createMatcher(e)
        if hasattr(ev, 'exception'):
            raise ev.exception
        else:
            return ev.result


Instruction:
Given the following Python 3 function, write the documentation
async def lock(self, container = None):
        "Wait for lock acquire"
        if container is None:
            container = RoutineContainer.get_container(self.scheduler)
        if self.locked:
            pass
        elif self.lockroutine:
            await LockedEvent.createMatcher(self)
        else:
            await container.wait_for_send(LockEvent(self.context, self.key, self))
            self.locked = True

Response: Wait for lock acquire


Instruction:
Can you generate the documentation for the following Python 3 function
def trylock(self):
        "Try to acquire lock and return True; if cannot acquire the lock at this moment, return False."
        if self.locked:
            return True
        if self.lockroutine:
            return False
        waiter = self.scheduler.send(LockEvent(self.context, self.key, self))
        if waiter:
            return False
        else:
            self.locked = True
            return True

Response: Try to acquire lock and return True ; if cannot acquire the lock at this moment return False.


Instruction:
Implement a function in Python 3 to
start to acquire lock in another routine. Call trylock or lock later to acquire the lock. Call unlock to cancel the lock routine

Response:
def beginlock(self, container):
        "Start to acquire lock in another routine. Call trylock or lock later to acquire the lock. Call unlock to cancel the lock routine"
        if self.locked:
            return True
        if self.lockroutine:
            return False
        self.lockroutine = container.subroutine(self._lockroutine(container), False)
        return self.locked


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create(self):
        self.queue = self.scheduler.queue.addSubQueue(self.priority, LockEvent.createMatcher(self.context, self.key),
                                         maxdefault = self.size, defaultQueueClass = CBQueue.AutoClassQueue.initHelper('locker', subqueuelimit = 1))

Response: Create the subqueue for this lock.


Instruction:
How would you explain what the following Python 3 function does
async def destroy(self, container = None):
        if container is None:
            container = RoutineContainer(self.scheduler)
        if self.queue is not None:
            await container.syscall_noreturn(syscall_removequeue(self.scheduler.queue, self.queue))
            self.queue = None

Response: Destroy the created subqueue


Instruction:
Here you have a function in Python 3, explain what it does
async def shutdown(self, force = False, connmark = -1):
        '''
        Can call without delegate
        '''
        if connmark is None:
            connmark = self.connmark
        self.scheduler.emergesend(ConnectionControlEvent(self, ConnectionControlEvent.SHUTDOWN, force, connmark))

Response: Send a shutdown event to the scheduler.


Instruction:
Create a Python 3 function for
reconnecting to the next available session.

Response:
async def reconnect(self, force = True, connmark = None):
        '''
        Can call without delegate
        '''
        if connmark is None:
            connmark = self.connmark
        self.scheduler.emergesend(ConnectionControlEvent(self, ConnectionControlEvent.RECONNECT, force, connmark))


Instruction:
Write a Python 3 script for
resetting the internal state of the object.

Response:
async def reset(self, force = True, connmark = None):
        '''
        Can call without delegate
        '''
        if connmark is None:
            connmark = self.connmark
        self.scheduler.emergesend(ConnectionControlEvent(self, ConnectionControlEvent.RESET, force, connmark))


Instruction:
Can you implement a function in Python 3 that
writes an event to the broker.

Response:
async def write(self, event, ignoreException = True):
        '''
        Can call without delegate
        '''
        connmark = self.connmark
        if self.connected:
            def _until():
                if not self.connected or self.connmark != connmark:
                    return True
            r = await self.wait_for_send(event, until=_until)
            if r:
                if ignoreException:
                    return
                else:
                    raise
        else:
            if not ignoreException:
                raise ConnectionResetException


Instruction:
Here you have a function in Python 3, explain what it does
async def shutdown(self, connmark = -1):
        '''
        Can call without delegate
        '''
        if connmark is None:
            connmark = self.connmark
        self.scheduler.emergesend(ConnectionControlEvent(self, ConnectionControlEvent.SHUTDOWN, True, connmark))

Response: Send a shutdown event to the scheduler.


Instruction:
Can you create a Python 3 function that
stops listens for a specific object.

Response:
async def stoplisten(self, connmark = -1):
        '''
        Can call without delegate
        '''
        if connmark is None:
            connmark = self.connmark
        self.scheduler.emergesend(ConnectionControlEvent(self, ConnectionControlEvent.STOPLISTEN, True, connmark))


Instruction:
Make a summary of the following Python 3 code
async def startlisten(self, connmark = -1):
        '''
        Can call without delegate
        '''
        if connmark is None:
            connmark = self.connmark
        self.scheduler.emergesend(ConnectionControlEvent(self, ConnectionControlEvent.STARTLISTEN, True, connmark))

Response: Start listening for a specific object.


Instruction:
Can you generate the documentation for the following Python 3 function
def default_start():
    (config, daemon, pidfile, startup, fork) = parsearg()
    if config is None:
        if os.path.isfile('/etc/vlcp.conf'):
            config = '/etc/vlcp.conf'
        else:
            print('/etc/vlcp.conf is not found; start without configurations.')
    elif not config:
        config = None
    main(config, startup, daemon, pidfile, fork)

Response: Use `sys.argv` for starting parameters. This is the entry-point of `vlcp-start`


Instruction:
Write a Python 3 function for
stopping the output stream but further download will still perform

Response:
def close(self):
        "Stop the output stream, but further download will still perform"
        if self.stream:
            self.stream.close(self.scheduler)
            self.stream = None


Instruction:
Write a Python 3 script to
force stop the output stream if there are more data to download shutdown the connection

Response:
async def shutdown(self):
        "Force stop the output stream, if there are more data to download, shutdown the connection"
        if self.stream:
            if not self.stream.dataeof and not self.stream.dataerror:
                self.stream.close(self.scheduler)
                await self.connection.shutdown()
            else:
                self.stream.close(self.scheduler)
            self.stream = None


Instruction:
Can you tell what is the following Python 3 function doing
def createphysicalnetwork(type, create_processor = partial(default_processor, excluding=('id', 'type')),
                                      reorder_dict = default_iterate_dict):
    """
    :param type: physical network type

    :param create_processor: create_processor(physicalnetwork, walk, write, \*, parameters)
    """
    # create an new physical network
    def walker(walk, write, timestamp, parameters_dict):
        for key, parameters in reorder_dict(parameters_dict):
            try:
                value = walk(key)
            except KeyError:
                pass
            else:
                id_ = parameters['id']
                new_network = create_new(PhysicalNetwork, value, id_)
                new_network.type = type

                create_processor(new_network, walk, write, parameters=parameters)
                write(key, new_network)
                new_networkmap = PhysicalNetworkMap.create_instance(id_)
                new_networkmap.network = new_network.create_weakreference()
                write(new_networkmap.getkey(), new_networkmap)

                # Save into network set
                try:
                    physet = walk(PhysicalNetworkSet.default_key())
                except KeyError:
                    pass
                else:
                    physet.set.dataset().add(new_network.create_weakreference())
                    write(physet.getkey(), physet)
    return walker

Response: Create a new physical network of the specified type.


Instruction:
Can you generate the documentation for the following Python 3 function
def deletephysicalnetwork(check_processor = default_physicalnetwork_delete_check,
                                reorder_dict = default_iterate_dict):
    """
    :param check_processor: check_processor(physicalnetwork, physicalnetworkmap, walk, write, \*, parameters)
    """
    def walker(walk, write, timestamp, parameters_dict):
        for key, parameters in reorder_dict(parameters_dict):
            try:
                value = walk(key)
            except KeyError:
                pass
            else:
                id_ = parameters['id']
                try:
                    phy_map = walk(PhysicalNetworkMap.default_key(id_))
                except KeyError:
                    pass
                else:
                    check_processor(value, phy_map, walk, write, parameters=parameters)
                    write(phy_map.getkey(), None)
                try:
                    phynetset = walk(PhysicalNetworkSet.default_key())
                except KeyError:
                    pass
                else:
                    phynetset.set.dataset().discard(value.create_weakreference())
                    write(phynetset.getkey(), phynetset)
                write(key, None)
    return walker

Response: Returns a walker that deletes all physical network in the virtual network.


Instruction:
Create a Python 3 function to
create a new PhysicalPort object.

Response:
def createphysicalport(create_processor = partial(default_processor, excluding=('vhost', 'systemid',
                                                                                'bridge', 'name',
                                                                                'physicalnetwork')),
                       reorder_dict = default_iterate_dict):
    """
    :param create_processor: create_processor(physicalport, physicalnetwork, physicalnetworkmap, walk, write, \*, parameters)
    """
    def walker(walk, write, timestamp, parameters_dict):
        for key, parameters in reorder_dict(parameters_dict):
            try:
                value = walk(key)
            except KeyError:
                pass
            else:
                p = create_new(PhysicalPort, value, parameters['vhost'], parameters['systemid'],
                                                    parameters['bridge'], parameters['name'])
                try:
                    physicalnetwork = walk(PhysicalNetwork.default_key(parameters['physicalnetwork']))
                except KeyError:
                    pass
                else:
                    # Should already been check from outside
                    p.physicalnetwork = physicalnetwork.create_reference()
                    try:
                        phymap = walk(PhysicalNetworkMap._network.leftkey(physicalnetwork))
                    except KeyError:
                        pass
                    else:
                        create_processor(p, physicalnetwork, phymap, walk, write, parameters=parameters)
                        phymap.ports.dataset().add(p.create_weakreference())
                        write(phymap.getkey(), phymap)
                try:
                    phyportset = walk(PhysicalPortSet.default_key())
                except KeyError:
                    pass
                else:
                    phyportset.set.dataset().add(p.create_weakreference())
                    write(phyportset.getkey(), phyportset)
                write(p.getkey(), p)
    return walker


Instruction:
Here you have a function in Python 3, explain what it does
def deletephysicalport(check_processor=_false_processor,
                       reorder_dict = default_iterate_dict):
    """
    :param check_processor: check_processor(physicalport, physicalnetwork, physicalnetworkmap,
                            walk, write \*, parameters)
    """
    def walker(walk, write, timestamp, parameters_dict):
        for key, parameters in reorder_dict(parameters_dict):
            try:
                value = walk(key)
            except KeyError:
                pass
            else:
                try:
                    phynet = walk(value.physicalnetwork.getkey())
                except KeyError:
                    pass
                else:
                    try:
                        phymap = walk(PhysicalNetworkMap._network.leftkey(phynet))
                    except KeyError:
                        pass
                    else:
                        check_processor(value, phynet, phymap, walk, write, parameters=parameters)
                        phymap.ports.dataset().discard(value.create_weakreference())
                        write(phymap.getkey(), phymap)
                try:
                    physet = walk(PhysicalPortSet.default_key())
                except KeyError:
                    pass
                else:
                    physet.set.dataset().discard(value.create_weakreference())
                    write(physet.getkey(), physet)
                write(key, None)
    return walker

Response: This function deletes a physicalport from the database.


Instruction:
Write a Python 3 function for
creating a new LogicalNetwork object and return it.

Response:
def createlogicalnetwork(create_processor = partial(default_processor, excluding=('id', 'physicalnetwork')),
                         reorder_dict = default_iterate_dict):
    """
    :param create_processor: create_processor(logicalnetwork, logicalnetworkmap, physicalnetwork,
                             physicalnetworkmap, walk, write, \*, parameters)
    """
    def walker(walk, write, timestamp, parameters_dict):
        for key, parameters in reorder_dict(parameters_dict):
            try:
                value = walk(key)
            except KeyError:
                pass
            else:
                id_ = parameters['id']
                lognet = create_new(LogicalNetwork, value, id_)
                logmap = LogicalNetworkMap.create_instance(id_)
                logmap.network = lognet.create_reference()
                try:
                    phynet = walk(PhysicalNetwork.default_key(parameters['physicalnetwork']))
                except KeyError:
                    pass
                else:
                    lognet.physicalnetwork = phynet.create_reference()
                    try:
                        phymap = walk(PhysicalNetworkMap._network.leftkey(phynet))
                    except KeyError:
                        pass
                    else:
                        create_processor(lognet, logmap, phynet, phymap, walk, write, parameters=parameters)
                        phymap.logicnetworks.dataset().add(lognet.create_weakreference())
                        write(phymap.getkey(), phymap)
                        write(lognet.getkey(), lognet)
                        write(logmap.getkey(), logmap)
                try:
                    logicalnetworkset = walk(LogicalNetworkSet.default_key())
                except KeyError:
                    pass
                else:
                    logicalnetworkset.set.dataset().add(lognet.create_weakreference())
                    write(logicalnetworkset.getkey(), logicalnetworkset)
    return walker


Instruction:
How would you implement a function in Python 3 that
returns a walker that deletes the logicalnetwork from the logmap and physicalnetworkmap.

Response:
def deletelogicalnetwork(check_processor=default_logicalnetwork_delete_check,
                         reorder_dict = default_iterate_dict):
    """
    :param check_processor: check_processor(logicalnetwork, logicalnetworkmap,
                                            physicalnetwork, physicalnetworkmap,
                                            walk, write, \*, parameters)
    """
    def walker(walk, write, timestamp, parameters_dict):
        for key, parameters in reorder_dict(parameters_dict):
            try:
                value = walk(key)
            except KeyError:
                pass
            else:
                try:
                    logmap = walk(LogicalNetworkMap._network.leftkey(key))
                except KeyError:
                    pass
                else:
                    try:
                        phynet = walk(value.physicalnetwork.getkey())
                    except KeyError:
                        pass
                    else:
                        try:
                            phymap = walk(PhysicalNetworkMap._network.leftkey(phynet))
                        except KeyError:
                            pass
                        else:
                            check_processor(value, logmap, phynet, phymap, walk, write, parameters=parameters)
                            phymap.logicnetworks.dataset().discard(value.create_weakreference())
                            write(phymap.getkey(), phymap)
                            write(key, None)
                            write(logmap.getkey(), None)
                try:
                    logicalnetworkset = walk(LogicalNetworkSet.default_key())
                except KeyError:
                    pass
                else:
                    logicalnetworkset.set.dataset().discard(value.create_weakreference())
                    write(logicalnetworkset.getkey(), logicalnetworkset)
    return walker


Instruction:
Can you create a Python 3 function that
forces an update notification on specified objects even if they are not actually updated in the DB.

Response:
def updateobjects(self, updatedvalues):
        """
        Force a update notification on specified objects, even if they are not actually updated
        in ObjectDB
        """
        if not self._updatedset:
            self.scheduler.emergesend(FlowUpdaterNotification(self, FlowUpdaterNotification.DATAUPDATED))
        self._updatedset.update(set(updatedvalues).intersection(self._savedresult))


Instruction:
Here you have a function in Python 3, explain what it does
async def _flowupdater(self):
        lastresult = set(v for v in self._savedresult if v is not None and not v.isdeleted())
        flowupdate = FlowUpdaterNotification.createMatcher(self, FlowUpdaterNotification.FLOWUPDATE)
        while True:
            currentresult = [v for v in self._savedresult if v is not None and not v.isdeleted()]
            # Calculating differences
            additems = []
            updateditems = []
            updatedset2 = self._updatedset2
            for v in currentresult:
                if v not in lastresult:
                    additems.append(v)
                else:
                    lastresult.remove(v)
                    if v in updatedset2:
                        # Updated
                        updateditems.append(v)
            removeitems = lastresult
            self._updatedset2.clear()
            # Save current result for next difference
            lastresult = set(currentresult)
            if not additems and not removeitems and not updateditems:
                await flowupdate
                continue
            await self.updateflow(self._connection, set(additems), removeitems, set(updateditems))

Response: Coroutine calling updateflow for all flow updates.


Instruction:
Create a Python 3 function for
clearing a queue and call processor on all events in it

Response:
def syscall_clearqueue(queue):
    '''
    Clear a queue
    '''
    def _syscall(scheduler, processor):
        qes, qees = queue.clear()
        events = scheduler.queue.unblockqueue(queue)
        for e in events:
            scheduler.eventtree.remove(e)
        for e in qes:
            processor(e)
        for e in qees:
            processor(e)
    return _syscall


Instruction:
Implement a function in Python 3 to
remove subqueue from queue.

Response:
def syscall_removequeue(queue, index):
    '''
    Remove subqueue `queue[index]` from queue.
    '''
    def _syscall(scheduler, processor):
        events = scheduler.queue.unblockqueue(queue[index])
        for e in events:
            scheduler.eventtree.remove(e)
        qes, qees = queue.removeSubQueue(index)
        for e in qes:
            processor(e)
        for e in qees:
            processor(e)
    return _syscall


Instruction:
Explain what the following Python 3 code does
def syscall_clearremovequeue(queue, index):
    '''
    Clear the subqueue `queue[index]` and remove it from queue.
    '''
    def _syscall(scheduler, processor):
        qes, qees = queue[index].clear()
        events = scheduler.queue.unblockqueue(queue[index])
        for e in events:
            scheduler.eventtree.remove(e)
        qes2, qees2 = queue.removeSubQueue(index)
        for e in qes:
            processor(e)
        for e in qes2:
            processor(e)
        for e in qees:
            processor(e)
        for e in qees2:
            processor(e)
    return _syscall

Response: Clear the subqueue queue [ index and remove it from queue.


Instruction:
Here you have a function in Python 3, explain what it does
def register(self, matchers, runnable):
        '''
        Register an iterator(runnable) to scheduler and wait for events

        :param matchers: sequence of EventMatchers

        :param runnable: an iterator that accept send method

        :param daemon: if True, the runnable will be registered as a daemon.
        '''
        if getattr(self, 'syscallfunc', None) is not None and getattr(self, 'syscallrunnable', None) is None:
            # Inject this register
            self.syscallrunnable = runnable
        else:
            for m in matchers:
                self.matchtree.insert(m, runnable)
                events = self.eventtree.findAndRemove(m)
                for e in events:
                    self.queue.unblock(e)
                if m.indices[0] == PollEvent._classname0 and len(m.indices) >= 2:
                    self.polling.onmatch(m.indices[1], None if len(m.indices) <= 2 else m.indices[2], True)
            self.registerIndex.setdefault(runnable, set()).update(matchers)

Response: Register an iterator that accepts send method


Instruction:
How would you explain what the following Python 3 function does
def unregister(self, matchers, runnable):
        '''
        Unregister an iterator(runnable) and stop waiting for events

        :param matchers: sequence of EventMatchers

        :param runnable: an iterator that accept send method
        '''
        for m in matchers:
            self.matchtree.remove(m, runnable)
            if m.indices[0] == PollEvent._classname0 and len(m.indices) >= 2:
                self.polling.onmatch(m.indices[1], None if len(m.indices) <= 2 else m.indices[2], False)
        self.registerIndex.setdefault(runnable, set()).difference_update(matchers)

Response:
Unregisters an iterator that accepts send method
 yizi


Instruction:
Make a summary of the following Python 3 code
def unregisterall(self, runnable):
        '''
        Unregister all matches and detach the runnable. Automatically called when runnable returns StopIteration.
        '''
        if runnable in self.registerIndex:
            for m in self.registerIndex[runnable]:
                self.matchtree.remove(m, runnable)
                if m.indices[0] == PollEvent._classname0 and len(m.indices) >= 2:
                    self.polling.onmatch(m.indices[1], None if len(m.indices) <= 2 else m.indices[2], False)
            del self.registerIndex[runnable]
            self.daemons.discard(runnable)

Response: Unregisters all matches and detach the runnable. Automatically called when runnable returns StopIteration.


Instruction:
Write a Python 3 script to
unblock and ignore the matched events

Response:
def ignore(self, matcher):
        '''
        Unblock and ignore the matched events, if any.
        '''
        events  = self.eventtree.findAndRemove(matcher)
        for e in events:
            self.queue.unblock(e)
            e.canignore = True


Instruction:
Can you generate the documentation for the following Python 3 function
def quit(self, daemononly = False):
        '''
        Send quit event to quit the main loop
        '''
        if not self.quitting:
            self.quitting = True
            self.queue.append(SystemControlEvent(SystemControlEvent.QUIT, daemononly = daemononly), True)

Response: Send quit event to quit the main loop


Instruction:
Implement a function in Python 3 to
generate a TimerEvent on the specified time.

Response:
def setTimer(self, start, interval = None):
        '''
        Generate a TimerEvent on specified time

        :param start: offset for time from now (seconds), or datetime for a fixed time

        :param interval: if not None, the timer is regenerated by interval seconds.

        :returns: a timer handle to wait or cancel the timer
        '''
        if isinstance(start, datetime):
            timestamp = (start - datetime.fromtimestamp(0)).total_seconds()
        else:
            timestamp = self.current_time + start
        if interval is not None:
            if not (interval > 0):
                raise ValueError('interval must be positive.')
        th = self.TimerHandle(timestamp, interval)
        self.timers.push(th, timestamp)
        return th


Instruction:
Make a summary of the following Python 3 code
def registerPolling(self, fd, options = POLLING_IN|POLLING_OUT, daemon = False):
        '''
        register a polling file descriptor

        :param fd: file descriptor or socket object

        :param options: bit mask flags. Polling object should ignore the incompatible flag.
        '''
        self.polling.register(fd, options, daemon)

Response: register a polling file descriptor fd


Instruction:
How would you explain what the following Python 3 function does
def setDaemon(self, runnable, isdaemon, noregister = False):
        '''
        If a runnable is a daemon, it will not keep the main loop running. The main loop will end when all alived runnables are daemons.
        '''
        if not noregister and runnable not in self.registerIndex:
            self.register((), runnable)
        if isdaemon:
            self.daemons.add(runnable)
        else:
            self.daemons.discard(runnable)

Response: Sets the runnable to be a daemon.


Instruction:
Here you have a function in Python 3, explain what it does
def syscall(self, func):
        '''
        Call the func in core context (main loop).

        func should like::

            def syscall_sample(scheduler, processor):
                something...

        where processor is a function which accept an event. When calling processor, scheduler directly process this event without
        sending it to queue.

        An event matcher is returned to the caller, and the caller should wait for the event immediately to get the return value from the system call.
        The SyscallReturnEvent will have 'retvalue' as the return value, or 'exception' as the exception thrown: (type, value, traceback)

        :param func: syscall function

        :returns: an event matcher to wait for the SyscallReturnEvent. If None is returned, a syscall is already scheduled;
                  return to core context at first.

        '''
        if getattr(self, 'syscallfunc', None) is not None:
            return None
        self.syscallfunc = func
        self.syscallmatcher = SyscallReturnEvent.createMatcher()
        return self.syscallmatcher

Response: Call the function in core context.


Instruction:
Write a Python 3 function that can
return current servers of vhost if vhost is None return all servers.

Response:
def getservers(self, vhost = None):
        '''
        Return current servers

        :param vhost: return only servers of vhost if specified. '' to return only default servers.
                      None for all servers.
        '''
        if vhost is not None:
            return [s for s in self.connections if s.protocol.vhost == vhost]
        else:
            return list(self.connections)


Instruction:
Given the following Python 3 function, write the documentation
async def stoplisten(self, vhost = None):
        '''
        Stop listen on current servers

        :param vhost: return only servers of vhost if specified. '' to return only default servers.
                      None for all servers.
        '''
        servers = self.getservers(vhost)
        for s in servers:
            await s.stoplisten()
        return len(servers)

Response:
Stop listen on current servers

COOKIE


Instruction:
Can you generate the documentation for the following Python 3 function
async def startlisten(self, vhost = None):
        '''
        Start listen on current servers

        :param vhost: return only servers of vhost if specified. '' to return only default servers.
                      None for all servers.
        '''
        servers = self.getservers(vhost)
        for s in servers:
            await s.startlisten()
        return len(servers)

Response: Start listening on all servers in vhost.


Instruction:
Can you generate the documentation for the following Python 3 function
async def updateconfig(self):
        "Reload configurations, remove non-exist servers, add new servers, and leave others unchanged"
        exists = {}
        for s in self.connections:
            exists[(s.protocol.vhost, s.rawurl)] = s
        self._createServers(self, '', exists = exists)
        for _,v in exists.items():
            await v.shutdown()
            self.connections.remove(v)

Response: Reload configurations remove non - exist servers add new servers and leave others unchanged


Instruction:
Can you tell what is the following Python 3 function doing
def getconnections(self, vhost = None):
        "Return accepted connections, optionally filtered by vhost"
        if vhost is None:
            return list(self.managed_connections)
        else:
            return [c for c in self.managed_connections if c.protocol.vhost == vhost]

Response: Return accepted connections optionally filtered by vhost


Instruction:
Implement a function in Python 3 to
handle DHCP packets from OpenFlow.

Response:
async def _dhcp_handler(self):
        """
        Mini DHCP server, respond DHCP packets from OpenFlow
        """
        conn = self._connection
        ofdef = self._connection.openflowdef
        l3 = self._parent._gettableindex('l3input', self._connection.protocol.vhost)
        dhcp_packet_matcher = OpenflowAsyncMessageEvent.createMatcher(ofdef.OFPT_PACKET_IN, None, None, l3, 1,
                                                              self._connection, self._connection.connmark)
        # These tags are important options. They are sent first to make sure the client
        # correctly receive these options.
        required_tags = [d.OPTION_MESSAGE_TYPE, d.OPTION_SERVER_IDENTIFIER,
                         d.OPTION_NETMASK, d.OPTION_ROUTER,
                         d.OPTION_DNSSERVER, d.OPTION_BROADCAST, d.OPTION_MTU,
                         d.OPTION_LEASE_TIME, d.OPTION_T1, d.OPTION_T2]
        server_mac = mac_addr(self._parent.servermac)
        # IP fragment identifier
        trans_id = uint16.create(os.urandom(2))
        def set_options(payload, option_dict, provide_options, message_type, remove_lease = False):
            """
            Set DHCP options to output payload regarding the incoming request

            :param payload: output DHCP payload

            :param option_dict: incoming DHCP options in request

            :param provide_options: all DHCP options that are ready to sent to the client

            :param message_type: output DHCP message type

            :param remove_lease: remove all leases options. DHCPINFORM cannot contain
                                 leases options. See https://tools.ietf.org/html/rfc2131#section-3.4
            """
            message_type_opt = d.dhcp_option_message_type(value = message_type)
            if d.OPTION_REQUESTED_OPTIONS in option_dict:
                # First requested options, then required options, then others
                reqs = set(option_dict[d.OPTION_REQUESTED_OPTIONS].value)
                send_tags = [t for t in option_dict[d.OPTION_REQUESTED_OPTIONS].value
                             if t == d.OPTION_MESSAGE_TYPE or t in provide_options] \
                            + [t for t in required_tags
                               if (t in provide_options or t == d.OPTION_MESSAGE_TYPE) and t not in reqs] \
                            + [t for t in provide_options
                               if t not in reqs and t not in required_tags]
            else:
                # Required options, then others
                send_tags = [t for t in required_tags
                             if t in provide_options or t == d.OPTION_MESSAGE_TYPE] \
                            + [t for t in set(provide_options.keys()).difference(required_tags)]
            # If the client has sent an option for max message size, use it; or use the RFC required 576 bytes
            not_finished = d.build_options(
                                payload,
                                [message_type_opt if t == d.OPTION_MESSAGE_TYPE
                                  else provide_options[t] for t in send_tags
                                  if not remove_lease or (t != d.OPTION_LEASE_TIME and t != d.OPTION_T1 and t != OPTION_T2)],
                                max(min(option_dict[d.OPTION_MAX_MESSAGE_SIZE].value, 1400), 576)
                                if d.OPTION_MAX_MESSAGE_SIZE in option_dict
                                else 576)
        async def send_packet(pid, packet):
            """
            Send DHCP packet to specified port
            """
            await self.execute_commands(conn,
                            [ofdef.ofp_packet_out(
                                    buffer_id = ofdef.OFP_NO_BUFFER,
                                    in_port = ofdef.OFPP_CONTROLLER,
                                    actions = [ofdef.ofp_action_output(port = pid,
                                                                       max_len = ofdef.OFPCML_NO_BUFFER)],
                                    data = packet._tobytes()
                                )])
        while True:
            ev = await dhcp_packet_matcher
            msg = ev.message
            try:
                in_port = ofdef.ofp_port_no.create(ofdef.get_oxm(msg.match.oxm_fields, ofdef.OXM_OF_IN_PORT))
                if in_port not in self._dhcpentries:
                    # Not a DHCP-enabled port
                    continue
                port_mac, port_ip, server_ip, provide_options = self._dhcpentries[in_port]
                # Fragmented DHCP packets are not supported - this is allowed according
                # to RFC: server and clients are only needed to support at least 576 bytes
                # DHCP messages.
                l7_packet = ethernet_l7.create(msg.data)
                dhcp_packet = d.dhcp_payload.create(l7_packet.data)
                # We only process a DHCP request directly sent from the logical port
                if (dhcp_packet.op != d.BOOTREQUEST                            # A DHCP server packet
                        or dhcp_packet.hlen != 6 or dhcp_packet.htype != 1     # Hardware address not ethernet (48-bit)
                        or dhcp_packet.magic_cookie != d.BOOTP_MAGIC_COOKIE    # Magic cookie not match
                        or dhcp_packet.giaddr != 0):                           # A relayed DHCP message
                    raise ValueError('Unsupported DHCP packet')
                # Reassemble DHCP options
                options = d.reassemble_options(dhcp_packet)
                option_dict = dict((o.tag, o) for o in options)
                if d.OPTION_MESSAGE_TYPE not in option_dict:
                    raise ValueError('Message type not found')
                message_type = option_dict[d.OPTION_MESSAGE_TYPE].value
                is_nak = False
                if message_type == d.DHCPDISCOVER:
                    # A DHCPDISCOVER should get a DHCPOFFER response
                    if dhcp_packet.chaddr[:6].ljust(6, b'\x00') != mac_addr.tobytes(port_mac):
                        # MAC address not match, ignore this packet
                        continue
                    dhcp_reply = d.dhcp_payload(op = d.BOOTREPLY,
                                                htype = 1,
                                                hlen = 6,
                                                hops = 0,
                                                xid = dhcp_packet.xid,
                                                secs = 0,
                                                flags = dhcp_packet.flags,
                                                ciaddr = 0,
                                                yiaddr = port_ip,
                                                siaddr = 0,
                                                giaddr = dhcp_packet.giaddr,
                                                chaddr = dhcp_packet.chaddr,
                                                magic_cookie = d.BOOTP_MAGIC_COOKIE
                                                )
                    set_options(dhcp_reply, option_dict, provide_options, d.DHCPOFFER)
                elif message_type == d.DHCPREQUEST:
                    # A DHCPREQUEST should get a DHCPACK reply
                    if d.OPTION_SERVER_IDENTIFIER in option_dict and option_dict[d.OPTION_SERVER_IDENTIFIER].value != server_ip:
                        # Ignore packets to wrong address
                        continue
                    if dhcp_packet.chaddr[:6].ljust(6, b'\x00') != mac_addr.tobytes(port_mac) \
                            or (d.OPTION_REQUESTED_IP in option_dict and option_dict[d.OPTION_REQUESTED_IP].value != port_ip) \
                            or (dhcp_packet.ciaddr != 0 and dhcp_packet.ciaddr != port_ip):
                        # Requested MAC or IP not matched, send a NACK
                        dhcp_reply = d.dhcp_payload(op = d.BOOTREPLY,
                                                    htype = 1,
                                                    hlen = 6,
                                                    hops = 0,
                                                    xid = dhcp_packet.xid,
                                                    secs = 0,
                                                    flags = dhcp_packet.flags,
                                                    ciaddr = 0,
                                                    yiaddr = 0,
                                                    siaddr = 0,
                                                    giaddr = dhcp_packet.giaddr,
                                                    chaddr = dhcp_packet.chaddr,
                                                    magic_cookie = d.BOOTP_MAGIC_COOKIE
                                                    )
                        # Do not send more options in NACK
                        d.build_options(dhcp_reply, [d.dhcp_option_message_type(value = d.DHCPNAK),
                                                     d.dhcp_option_address(tag = d.OPTION_SERVER_IDENTIFIER,
                                                                           value = server_ip)], 576, 0)
                        is_nak = True
                    else:
                        dhcp_reply = d.dhcp_payload(op = d.BOOTREPLY,
                                                    htype = 1,
                                                    hlen = 6,
                                                    hops = 0,
                                                    xid = dhcp_packet.xid,
                                                    secs = 0,
                                                    flags = dhcp_packet.flags,
                                                    ciaddr = dhcp_packet.ciaddr,
                                                    yiaddr = port_ip,
                                                    siaddr = 0,
                                                    giaddr = dhcp_packet.giaddr,
                                                    chaddr = dhcp_packet.chaddr,
                                                    magic_cookie = d.BOOTP_MAGIC_COOKIE
                                                    )
                        set_options(dhcp_reply, option_dict, provide_options, d.DHCPACK)
                elif message_type == d.DHCPDECLINE:
                    # Address already in use?
                    self._logger.warning('DHCP client reports DHCPDECLINE, there should be some problems.'\
                                         ' Connection = %r(%016x), port = %d.',
                                         self._connection, self._connection.openflow_datapathid)
                elif message_type == d.DHCPRELEASE:
                    # Safe to ignore, we do not use a dynamic IP address pool
                    continue
                elif message_type == d.DHCPINFORM:
                    # Client setup IP addresses itself, but requesting more information
                    # DHCPINFORM reply cannot have lease options, and yiaddr = 0
                    dhcp_reply = d.dhcp_payload(op = d.BOOTREPLY,
                                                htype = 1,
                                                hlen = 6,
                                                hops = 0,
                                                xid = dhcp_packet.xid,
                                                secs = 0,
                                                flags = dhcp_packet.flags,
                                                ciaddr = dhcp_packet.ciaddr,
                                                yiaddr = 0,
                                                siaddr = 0,
                                                giaddr = dhcp_packet.giaddr,
                                                chaddr = dhcp_packet.chaddr,
                                                magic_cookie = d.BOOTP_MAGIC_COOKIE
                                                )
                    set_options(dhcp_reply, option_dict, provide_options, d.DHCPACK, True)
                trans_id = (trans_id + 1) & 0xffff
                if (dhcp_packet.flags & d.DHCPFLAG_BROADCAST) or is_nak:
                    # client request broadcast, or DHCPNAK
                    # RFC requires that DHCPNAK uses broadcast
                    dl_dst = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
                    ip_dst = 0xffffffff
                else:
                    dl_dst = l7_packet.dl_src
                    ip_dst = port_ip
                reply_packet = ip4_packet_l7((ip4_payload, ip4_udp_payload),
                                           dl_src = server_mac,
                                           dl_dst = dl_dst,
                                           identifier = trans_id,
                                           ttl = 128,
                                           ip_src = server_ip,
                                           ip_dst = ip_dst,
                                           sport = 67,
                                           dport = 68,
                                           data = dhcp_reply._tobytes()
                                           )
                # Send packet to the incoming port
                self.subroutine(send_packet(in_port, reply_packet), True)
            except Exception:
                self._logger.info('Invalid DHCP packet received: %r', msg.data, exc_info = True)


Instruction:
Can you generate the documentation for the following Python 3 function
def return_self_updater(func):
    '''
    Run func, but still return v. Useful for using knowledge.update with operates like append, extend, etc.
    e.g. return_self(lambda k,v: v.append('newobj'))
    '''
    @functools.wraps(func)
    def decorator(k,v):
        func(k,v)
        return v
    return decorator

Response: Decorator for returning self updater.


Instruction:
Here you have a function in Python 3, explain what it does
def date_time_string(timestamp=None):
    global _last_date_time_string
    _last_timestamp, _last_str = _last_date_time_string
    if timestamp is None:
        timestamp = time.time()
    _curr_timestamp = int(timestamp)
    if _curr_timestamp == _last_timestamp:
        return _last_str
    else:
        year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
        s = b"%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
                weekdayname[wd],
                day, monthname[month], year,
                hh, mm, ss)
        _last_date_time_string = (_curr_timestamp, s)
        return s

Response: Return the current date and time formatted for a message header.


Instruction:
Can you tell what is the following Python 3 function doing
def escape_b(s, quote=True):
    '''Replace special characters "&", "<" and ">" to HTML-safe sequences.
    If the optional flag quote is true, the quotation mark character (")
    is also translated.'''
    s = s.replace(b"&", b"&") # Must be done first!
    s = s.replace(b"<", b"<")
    s = s.replace(b">", b">")
    if quote:
        s = s.replace(b'"', b""")
    return s

Response: Replace special characters &lt and > to HTML - safe sequences.


Instruction:
Can you write a function in Python 3 where it
deprecates - use request_context for most use cases

Response:
def watch_context(keys, result, reqid, container, module = 'objectdb'):
    """
    DEPRECATED - use request_context for most use cases
    """
    try:
        keys = [k for k,r in zip(keys, result) if r is not None]
        yield result
    finally:
        if keys:
            async def clearup():
                try:
                    await send_api(container, module, 'munwatch', {'keys': keys, 'requestid': reqid})
                except QuitException:
                    pass
            container.subroutine(clearup(), False)


Instruction:
Given the following Python 3 function, write the documentation
def updater(f):
    "Decorate a function with named arguments into updater for transact"
    @functools.wraps(f)
    def wrapped_updater(keys, values):
        result = f(*values)
        return (keys[:len(result)], result)
    return wrapped_updater

Response: Decorate a function with named arguments into updater for transact


Instruction:
Create a Python 3 function to
decorate a function with named lists into updater for transact. :params \*args: parameter list sizes. -1 means all other items. None means a single item instead of a list. only one -1 is allowed.

Response:
def list_updater(*args):
    """
    Decorate a function with named lists into updater for transact.

    :params \*args: parameter list sizes. -1 means all other items. None means a single item instead of a list.
                    only one -1 is allowed.
    """
    neg_index = [i for v,i in izip(args, itertools.count()) if v is not None and v < 0]
    if len(neg_index) > 1:
        raise ValueError("Cannot use negative values more than once")
    if not neg_index:
        slice_list = []
        size = 0
        for arg in args:
            if arg is None:
                slice_list.append(size)
                size += 1
            else:
                slice_list.append(slice(size, size + arg))
                size += arg
    else:
        sep = neg_index[0]
        slice_list = []
        size = 0
        for arg in args[:sep]:
            if arg is None:
                slice_list.append(size)
                size += 1
            else:
                slice_list.append(slice(size, size + arg))
                size += arg
        rslice_list = []
        rsize = 0
        for arg in args[:sep:-1]:
            if arg is None:
                rslice_list.append(-1-rsize)
                rsize += 1
            else:
                rslice_list.append(slice(None if not rsize else -rsize, -(rsize + arg)))
                rsize += arg
        slice_list.append(slice(size, rsize))
        slice_list.extend(reversed(rslice_list))
    def inner_wrapper(f):
        @functools.wraps(f)
        def wrapped_updater(keys, values):
            result = f(*[values[s] for s in slice_list])
            return (keys[:len(result)], result)
        return wrapped_updater
    return inner_wrapper


Instruction:
Make a summary of the following Python 3 code
def create_new(cls, oldvalue, *args):
    "Raise  if the old value already exists"
    if oldvalue is not None:
        raise AlreadyExistsException('%r already exists' % (oldvalue,))
    return cls.create_instance(*args)

Response: Raise if the old value already exists


Instruction:
Make a summary of the following Python 3 code
def create_from_key(cls, oldvalue, key):
    "Raise  if the old value already exists"
    if oldvalue is not None:
        raise AlreadyExistsException('%r already exists' % (oldvalue,))
    return cls.create_from_key(key)

Response: Raise if the old value already exists


Instruction:
Implement a Python 3 function for
showing full value of a data object

Response:
def dump(obj, attributes = True, _refset = None):
    "Show full value of a data object"
    if _refset is None:
        _refset = set()
    if obj is None:
        return None
    elif isinstance(obj, DataObject):
        if id(obj) in _refset:
            attributes = False
        else:
            _refset.add(id(obj))
        cls = type(obj)
        clsname = getattr(cls, '__module__', '<unknown>') + '.' + getattr(cls, '__name__', '<unknown>')
        baseresult = {'_type': clsname, '_key': obj.getkey()}
        if not attributes:
            return baseresult
        else:
            baseresult.update((k,dump(v, attributes, _refset)) for k,v in vars(obj).items() if k[:1] != '_')
            _refset.remove(id(obj))
        return baseresult
    elif isinstance(obj, ReferenceObject):
        if obj._ref is not None:
            return dump(obj._ref, attributes, _refset)
        else:
            return {'_ref':obj.getkey()}
    elif isinstance(obj, WeakReferenceObject):
        return {'_weakref':obj.getkey()}
    elif isinstance(obj, DataObjectSet):
        return dump(list(obj.dataset()))
    elif isinstance(obj, dict):
        return dict((k, dump(v, attributes, _refset)) for k,v in obj.items())
    elif isinstance(obj, list) or isinstance(obj, tuple) or isinstance(obj, set):
        return [dump(v, attributes, _refset) for v in obj]
    else:
        return obj


Instruction:
Implement a function in Python 3 to
call vtep controller in sequence, merge mutiple calls if possible When a bind relationship is updated, we always send all logical ports to a logicalswitch, to make sure it recovers from some failed updates (so called idempotency). When multiple calls are pending, we only need to send the last of them.

Response:
async def action_handler(self):
        """
        Call vtep controller in sequence, merge mutiple calls if possible

        When a bind relationship is updated, we always send all logical ports to a logicalswitch,
        to make sure it recovers from some failed updates (so called idempotency). When multiple
        calls are pending, we only need to send the last of them.
        """
        bind_event = VtepControllerCall.createMatcher(self._conn)
        event_queue = []
        timeout_flag = [False]

        async def handle_action():
            while event_queue or timeout_flag[0]:
                events = event_queue[:]
                del event_queue[:]

                for e in events:
                    # every event must have physname , phyiname
                    # physname: physical switch name - must be same with OVSDB-VTEP switch
                    # phyiname: physical port name - must be same with the corresponding port
                    physname = e.physname
                    phyiname = e.phyiname
                    if e.type == VtepControllerCall.UNBINDALL:
                        # clear all other event info
                        self._store_event[(physname,phyiname)] = {"all":e}
                    elif e.type == VtepControllerCall.BIND:
                        # bind will combine bind event before
                        vlanid = e.vlanid
                        if (physname,phyiname) in self._store_event:
                            v = self._store_event[(physname,phyiname)]

                            if vlanid in v:
                                logicalports = e.logicalports
                                v.update({vlanid:(e.type,e.logicalnetworkid,e.vni,logicalports)})
                                self._store_event[(physname,phyiname)] = v
                            else:
                                # new bind info , no combind event
                                v.update({vlanid:(e.type,e.logicalnetworkid,e.vni,e.logicalports)})
                                self._store_event[(physname,phyiname)] = v
                        else:
                            self._store_event[(physname,phyiname)] = {vlanid:(e.type,e.logicalnetworkid,
                                                                              e.vni,e.logicalports)}

                    elif e.type == VtepControllerCall.UNBIND:

                        vlanid = e.vlanid

                        if (physname,phyiname) in self._store_event:
                            v = self._store_event[(physname,phyiname)]
                            v.update({vlanid:(e.type,e.logicalnetworkid)})
                            self._store_event[(physname,phyiname)] = v
                        else:
                            self._store_event[(physname,phyiname)] = {vlanid:(e.type,e.logicalnetworkid)}

                    else:
                        self._parent._logger.warning("catch error type event %r , ignore it", exc_info=True)
                        continue

                call = []
                target_name = "vtepcontroller"
                for k,v in self._store_event.items():
                    if "all" in v:
                        # send unbindall
                        call.append(self.api(self,target_name,"unbindphysicalport",
                                             {"physicalswitch": k[0], "physicalport": k[1]},
                                             timeout=10))
                        # unbindall , del it whatever
                        del v["all"]

                try:
                    await self.execute_all(call)
                except Exception:
                    self._parent._logger.warning("unbindall remove call failed", exc_info=True)

                for k,v in self._store_event.items():
                    for vlanid , e in dict(v).items():
                        if vlanid != "all":
                            if e[0] == VtepControllerCall.BIND:

                                params = {"physicalswitch": k[0],
                                            "physicalport": k[1],
                                            "vlanid": vlanid,
                                            "logicalnetwork": e[1],
                                            "vni":e[2],
                                            "logicalports": e[3]}

                                try:
                                    await self.api(self,target_name,"updatelogicalswitch",
                                                  params,timeout=10)
                                except Exception:
                                    self._parent._logger.warning("update logical switch error,try next %r",params, exc_info=True)
                                else:
                                    del self._store_event[k][vlanid]

                            elif e[0] == VtepControllerCall.UNBIND:

                                params = {"logicalnetwork":e[1],
                                                "physicalswitch":k[0],
                                                "physicalport":k[1],
                                                  "vlanid":vlanid}

                                try:
                                    await self.api(self,target_name,"unbindlogicalswitch",
                                                      params,timeout=10)
                                except Exception:
                                    self._parent._logger.warning("unbind logical switch error,try next %r",params, exc_info=True)
                                else:
                                    del self._store_event[k][vlanid]

                self._store_event = dict((k,v) for k,v in self._store_event.items() if v)

                if timeout_flag[0]:
                    timeout_flag[0] = False

        def append_event(event, matcher):
            event_queue.append(event)

        while True:
            timeout, ev, m = await self.wait_with_timeout(10, bind_event)

            if not timeout:
                event_queue.append(ev)
            else:
                timeout_flag[0] = True

            await self.with_callback(handle_action(), append_event, bind_event)


Instruction:
Here you have a function in Python 3, explain what it does
async def update_ports(self, ports, ovsdb_ports):
        new_port_names = dict((p['name'], _to32bitport(p['ofport'])) for p in ovsdb_ports)
        new_port_ids = dict((p['id'], _to32bitport(p['ofport'])) for p in ovsdb_ports if p['id'])
        if new_port_names == self._portnames and new_port_ids == self._portids:
            return
        self._portnames.clear()
        self._portnames.update(new_port_names)
        self._portids.clear()
        self._portids.update(new_port_ids)

        logicalportkeys = [LogicalPort.default_key(id) for id in self._portids]

        self._original_initialkeys = logicalportkeys + [PhysicalPortSet.default_key()]
        self._initialkeys = tuple(itertools.chain(self._original_initialkeys, self._append_initialkeys))
        phy_walker = partial(self._physicalport_walker, _portnames=new_port_names)
        log_walker = partial(self._logicalport_walker, _portids=new_port_ids)
        self._walkerdict = dict(itertools.chain(
            ((PhysicalPortSet.default_key(),phy_walker),),
            ((lgportkey,log_walker) for lgportkey in logicalportkeys)
        ))
        self._portnames = new_port_names
        self._portids = new_port_ids
        await self.restart_walk()

Response: Update the internal port information.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def list_proxy(root_package = 'vlcp'):
    '''
    Walk through all the sub modules, find subclasses of vlcp.server.module._ProxyModule,
    list their default values
    '''
    proxy_dict = OrderedDict()
    pkg = __import__(root_package, fromlist=['_'])
    for imp, module, _ in walk_packages(pkg.__path__, root_package + '.'):
        m = __import__(module, fromlist = ['_'])
        for _, v in vars(m).items():
            if v is not None and isinstance(v, type) and issubclass(v, _ProxyModule) \
                    and v is not _ProxyModule \
                    and v.__module__ == module \
                    and hasattr(v, '_default'):
                name = v.__name__.lower()
                if name not in proxy_dict:
                    proxy_dict[name] = {'defaultmodule': v._default.__name__.lower(),
                                        'class': repr(v._default.__module__ + '.' + v._default.__name__)}
    return proxy_dict

Response: List all the proxy modules in the specified root_package.


Instruction:
Here you have a function in Python 3, explain what it does
def list_modules(root_package = 'vlcp'):
    '''
    Walk through all the sub modules, find subclasses of vlcp.server.module.Module,
    list their apis through apidefs
    '''
    pkg = __import__(root_package, fromlist=['_'])
    module_dict = OrderedDict()
    _server = Server()
    for imp, module, _ in walk_packages(pkg.__path__, root_package + '.'):
        m = __import__(module, fromlist = ['_'])
        for name, v in vars(m).items():
            if v is not None and isinstance(v, type) and issubclass(v, Module) \
                    and v is not Module \
                    and not isinstance(v, _ProxyModule) \
                    and hasattr(v, '__dict__') and 'configkey' in v.__dict__ \
                    and v.__module__ == module:
                module_name = v.__name__.lower()
                if module_name not in module_dict:
                    _inst = v(_server)
                    module_info = OrderedDict((('class', v.__module__ + '.' + v.__name__),
                                                ('dependencies', [d.__name__.lower()
                                                                  for d in v.depends]),
                                                ('classdescription', getdoc(v)),
                                                ('apis', [])))
                    if hasattr(_inst, 'apiHandler'):
                        apidefs = _inst.apiHandler.apidefs
                        module_info['apis'] = [(d[0], d[3])
                                               for d in apidefs
                                               if len(d) > 3 and \
                                               not d[0].startswith('public/')]
                    module_dict[module_name] = module_info
    return module_dict

Response: List all the modules in the specified root_package.


Instruction:
Can you implement a function in Python 3 that
appends an event to the queue.

Response:
def append(self, event, force = False):
        '''
        Append an event to queue. The events are classified and appended to sub-queues

        :param event: input event

        :param force: if True, the event is appended even if the queue is full

        :returns: None if appended successfully, or a matcher to match a QueueCanWriteEvent otherwise
        '''
        if self.tree is None:
            if self.parent is None:
                raise IndexError('The queue is removed')
            else:
                return self.parent.parent.append(event, force)
        q = self.tree.matchfirst(event)
        return q.append(event, force)


Instruction:
Can you write a function in Python 3 where it
blocks the specified event and all subsequent events until unblock.

Response:
def block(self, event, emptyEvents = ()):
        '''
        Return a recently popped event to queue, and block all later events until unblock.

        Only the sub-queue directly containing the event is blocked, so events in other queues may still be processed.
        It is illegal to call block and unblock in different queues with a same event.

        :param event: the returned event. When the queue is unblocked later, this event will be popped again.

        :param emptyEvents: reactivate the QueueIsEmptyEvents
        '''
        q = self.tree.matchfirst(event)
        q.block(event)
        self.blockEvents[event] = q
        for ee in emptyEvents:
            ee.queue.waitForEmpty()


Instruction:
Here you have a function in Python 3, explain what it does
def unblock(self, event):
        '''
        Remove a block
        '''
        if event not in self.blockEvents:
            return
        self.blockEvents[event].unblock(event)
        del self.blockEvents[event]

Response:
Remove a block
        from the event list.


Instruction:
Create a Python 3 function for
removing blocked events from the queue and all subqueues. Usually used after queue clear or unblockall.

Response:
def unblockqueue(self, queue):
        '''
        Remove blocked events from the queue and all subqueues. Usually used after queue clear/unblockall to prevent leak.

        :returns: the cleared events
        '''
        subqueues = set()
        def allSubqueues(q):
            subqueues.add(q)
            subqueues.add(q.defaultQueue)
            for v in q.queueindex.values():
                if len(v) == 3:
                    allSubqueues(v[1])
        allSubqueues(queue)
        events = [k for k,v in self.blockEvents.items() if v in subqueues]
        for e in events:
            del self.blockEvents[e]
        return events


Instruction:
Can you tell what is the following Python 3 function doing
def unblockall(self):
        '''
        Remove all blocks from the queue and all sub-queues
        '''
        for q in self.queues.values():
            q.unblockall()
        self.blockEvents.clear()

Response: Remove all blocks from the queue and all sub - queues.


Instruction:
How would you explain what the following Python 3 function does
def notifyPop(self, queue, length = 1):
        '''
        Internal notify for sub-queues been poped

        :returns: List of any events generated by this pop
        '''
        self.totalSize = self.totalSize - length
        ret1 = []
        ret2 = []
        if self.isWaited and self.canAppend():
            self.isWaited = False
            ret1.append(QueueCanWriteEvent(self))
        if self.isWaitEmpty and not self:
            self.isWaitEmpty = False
            ret2.append(QueueIsEmptyEvent(self))
        if self.parent is not None:
            pr = self.parent.notifyPop(self, length)
            ret1 += pr[0]
            ret2 += pr[1]
        newblocked =  not self.canPop()
        if newblocked != self.blocked:
            self.blocked = newblocked
            if self.parent is not None:
                self.parent.notifyBlock(self, newblocked)
        return (ret1, ret2)

Response:
Internal notify for sub - queues been popped
           .


Instruction:
Implement a Python 3 function for
popping an event from the queue.

Response:
def pop(self):
        '''
        Pop an event from the queue. The event in the queue with higher priority is popped before ones in lower priority.
        If there are multiple queues with the same priority, events are taken in turn from each queue.
        May return some queueEvents indicating that some of the queues can be written into.

        :returns: `(obj, (queueEvents,...), (queueEmptyEvents,...))` where obj is the popped event, queueEvents are QueueCanWriteEvents generated by this pop
                  and queueEmptyEvents are QueueIsEmptyEvents generated by this pop
        '''
        ret = self._pop()
        if self.parent is not None:
            pr = self.parent.notifyPop(self)
            ret[1].extend(pr[0])
            ret[2].extend(pr[1])
        return ret


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _pop(self):
        '''
        Actual pop
        '''
        if not self.canPop():
            raise IndexError('pop from an empty or blocked queue')
        priority = self.prioritySet[-1]
        ret = self.queues[priority]._pop()
        self.outputStat = self.outputStat + 1
        self.totalSize = self.totalSize - 1
        if self.isWaited and self.canAppend():
            self.isWaited = False
            ret[1].append(QueueCanWriteEvent(self))
        if self.isWaitEmpty and not self:
            self.isWaitEmpty = False
            ret[2].append(QueueIsEmptyEvent(self))
        return ret

Response:
Actual pop
            from the queue.


Instruction:
How would you implement a function in Python 3 that
clears all the events in this queue including any sub - queues.

Response:
def clear(self):
        '''
        Clear all the events in this queue, including any sub-queues.

        :returns: ((queueEvents,...), (queueEmptyEvents,...)) where queueEvents are QueueCanWriteEvents generated by clearing.
        '''
        l = len(self)
        ret = self._clear()
        if self.parent is not None:
            pr = self.parent.notifyPop(self, l)
            ret[0].extend(pr[0])
            ret[1].extend(pr[1])
        return ret


Instruction:
Can you tell what is the following Python 3 function doing
def _clear(self):
        '''
        Actual clear
        '''
        ret = ([],[])
        for q in self.queues.values():
            pr = q._clear()
            ret[0].extend(pr[0])
            ret[1].extend(pr[1])
        self.totalSize = 0
        del self.prioritySet[:]
        if self.isWaited and self.canAppend():
            self.isWaited = False
            ret[0].append(QueueCanWriteEvent(self))
        if self.isWaitEmpty and not self:
            self.isWaitEmpty = False
            ret[1].append(QueueIsEmptyEvent(self))
        self.blockEvents.clear()
        return ret

Response: Internal method to clear the internal state of the internal state.


Instruction:
How would you explain what the following Python 3 function does
def setPriority(self, queue, priority):
        '''
        Set priority of a sub-queue
        '''
        q = self.queueindex[queue]
        self.queues[q[0]].removeSubQueue(q[1])
        newPriority = self.queues.setdefault(priority, CBQueue.MultiQueue(self, priority))
        q[0] = priority
        newPriority.addSubQueue(q[1])

Response: Set priority of a sub - queue.


Instruction:
Can you create a Python 3 function that
adds a sub - queue to current queue with a priority and a matcher

Response:
def addSubQueue(self, priority, matcher, name = None, maxdefault = None, maxtotal = None, defaultQueueClass = FifoQueue):
        '''
        add a sub queue to current queue, with a priority and a matcher

        :param priority: priority of this queue. Larger is higher, 0 is lowest.

        :param matcher: an event matcher to catch events. Every event match the criteria will be stored in this queue.

        :param name: a unique name to identify the sub-queue. If none, the queue is anonymous. It can be any hashable value.

        :param maxdefault: max length for default queue.

        :param maxtotal: max length for sub-queue total, including sub-queues of sub-queue
        '''
        if name is not None and name in self.queueindex:
            raise IndexError("Duplicated sub-queue name '" + str(name) + "'")
        subtree = self.tree.subtree(matcher, True)
        newPriority = self.queues.setdefault(priority, CBQueue.MultiQueue(self, priority))
        newQueue = CBQueue(subtree, newPriority, maxdefault, maxtotal, defaultQueueClass)
        newPriority.addSubQueue(newQueue)
        qi = [priority, newQueue, name]
        if name is not None:
            self.queueindex[name] = qi
        self.queueindex[newQueue] = qi
        return newQueue


Instruction:
Implement a Python 3 function for
removing a sub - queue from current queue.

Response:
def removeSubQueue(self, queue):
        '''
        remove a sub queue from current queue.

        This unblock the sub-queue, retrieve all events from the queue and put them back to the parent.

        Call clear on the sub-queue first if the events are not needed any more.

        :param queue: the name or queue object to remove

        :returns: ((queueevents,...), (queueEmptyEvents,...)) Possible queue events from removing sub-queues
        '''
        q = self.queueindex[queue]
        q[1].unblockall()
        q[1]._removeFromTree()
        ret = ([],[])
        while q[1].canPop():
            r = q[1].pop()
            self.append(r[0], True)
            ret[0].extend(r[1])
            ret[1].extend(r[2])
        self.queues[q[0]].removeSubQueue(q[1])
        # Remove from index
        if q[2] is not None:
            del self.queueindex[q[2]]
        del self.queueindex[q[1]]
        newblocked =  not self.canPop()
        if newblocked != self.blocked:
            self.blocked = newblocked
            if self.parent is not None:
                self.parent.notifyBlock(self, newblocked)
        return ret


Instruction:
Here you have a function in Python 3, explain what it does
def ensure_keys(walk, *keys):
    all_retrieved = True
    for k in keys:
        try:
            walk(k)
        except WalkKeyNotRetrieved:
            all_retrieved = False
    return all_retrieved

Response: Check if all keys are available in a tree.


Instruction:
Explain what the following Python 3 code does
def list_config(root_package = 'vlcp'):
    '''
    Walk through all the sub modules, find subclasses of vlcp.config.Configurable,
    list their available configurations through _default_ prefix
    '''
    pkg = __import__(root_package, fromlist=['_'])
    return_dict = OrderedDict()
    for imp, module, _ in walk_packages(pkg.__path__, root_package + '.'):
        m = __import__(module, fromlist = ['_'])
        for name, v in vars(m).items():
            if v is not None and isinstance(v, type) and issubclass(v, Configurable) \
                    and v is not Configurable \
                    and hasattr(v, '__dict__') and 'configkey' in v.__dict__ \
                    and v.__module__ == module:
                configkey = v.__dict__['configkey']
                if configkey not in return_dict:
                    configs = OrderedDict()
                    v2 = v
                    parents = [v2]
                    while True:
                        parent = None
                        for c in v2.__bases__:
                            if issubclass(c, Configurable):
                                parent = c
                        if parent is None or parent is Configurable:
                            break
                        if hasattr(parent, '__dict__') and 'configkey' not in parent.__dict__:
                            parents.append(parent)
                            v2 = parent
                        else:
                            break
                    for v2 in reversed(parents):
                        tmp_configs = {}
                        for k, default_value in v2.__dict__.items():
                            if k.startswith('_default_'):
                                config_attr = k[len('_default_'):]
                                if config_attr in v.__dict__:
                                    continue
                                configname = configkey + '.' + config_attr
                                tmp_configs.setdefault(configname, OrderedDict())['default'] = \
                                    pformat(default_value, width=10)
                        # Inspect the source lines to find remarks for these configurations
                        lines, _ = getsourcelines(v2)
                        last_remark = []
                        for l in lines:
                            l = l.strip()
                            if not l:
                                continue
                            if l.startswith('#'):
                                last_remark.append(l[1:])
                            else:
                                if l.startswith('_default_'):
                                    key, sep, _ = l.partition('=')
                                    if sep and key.startswith('_default_'):
                                        configname = configkey + '.' +  key[len('_default_'):].strip()
                                        if configname in tmp_configs and configname not in configs:
                                            configs[configname] = tmp_configs.pop(configname)
                                        if configname in configs and last_remark:
                                            configs[configname]['description'] = cleandoc('\n' + '\n'.join(last_remark))
                                del last_remark[:]
                        for key in tmp_configs:
                            if key not in configs:
                                configs[key] = tmp_configs[key]
                    if configs:
                        return_dict[configkey] = OrderedDict((('class', v.__module__ + '.' + name),
                                                             ('classdescription', getdoc(v)),
                                                             ('configs', configs)))
    return return_dict

Response: List all available configurations in a VLCP package.


Instruction:
Create a Python 3 function for
wrapping a WSGI - style class method to a HTTPRequest event handler

Response:
def http(container = None):
    "wrap a WSGI-style class method to a HTTPRequest event handler"
    def decorator(func):
        @functools.wraps(func)
        def handler(self, event):
            return _handler(self if container is None else container, event, lambda env: func(self, env))
        return handler
    return decorator


Instruction:
Create a Python 3 function for
wrapping a WSGI - style function to a HTTPRequest event handler

Response:
def statichttp(container = None):
    "wrap a WSGI-style function to a HTTPRequest event handler"
    def decorator(func):
        @functools.wraps(func)
        def handler(event):
            return _handler(container, event, func)
        if hasattr(func, '__self__'):
            handler.__self__ = func.__self__
        return handler
    return decorator


Instruction:
Implement a Python 3 function for
starting to send response

Response:
def start_response(self, status = 200, headers = [], clearheaders = True, disabletransferencoding = False):
        "Start to send response"
        if self._sendHeaders:
            raise HttpProtocolException('Cannot modify response, headers already sent')
        self.status = status
        self.disabledeflate = disabletransferencoding
        if clearheaders:
            self.sent_headers = headers[:]
        else:
            self.sent_headers.extend(headers)


Instruction:
Can you write a function in Python 3 where it
sends a new header

Response:
def header(self, key, value, replace = True):
        "Send a new header"
        if hasattr(key, 'encode'):
            key = key.encode('ascii')
        if hasattr(value, 'encode'):
            value = value.encode(self.encoding)
        if replace:
            self.sent_headers = [(k,v) for k,v in self.sent_headers if k.lower() != key.lower()]
        self.sent_headers.append((key, value))


Instruction:
Can you tell what is the following Python 3 function doing
def rawheader(self, kv, replace = True):
        if hasattr(kv, 'encode'):
            kv = kv.encode(self.encoding)
        k,v = kv.split(b':', 1)
        self.header(k, v.strip(), replace)

Response: Add a header with "<Header>: Value


Instruction:
Here you have a function in Python 3, explain what it does
def setcookie(self, key, value, max_age=None, expires=None, path='/', domain=None, secure=None, httponly=False):
        newcookie = Morsel()
        newcookie.key = key
        newcookie.value = value
        newcookie.coded_value = value
        if max_age is not None:
            newcookie['max-age'] = max_age
        if expires is not None:
            newcookie['expires'] = expires
        if path is not None:
            newcookie['path'] = path
        if domain is not None:
            newcookie['domain'] = domain
        if secure:
            newcookie['secure'] = secure
        if httponly:
            newcookie['httponly'] = httponly
        self.sent_cookies = [c for c in self.sent_cookies if c.key != key]
        self.sent_cookies.append(newcookie)

Response: Add a new cookie to the list of sent cookies.


Instruction:
Can you create a Python 3 function that
buffers the whole output until EOF or flushed.

Response:
def bufferoutput(self):
        """
        Buffer the whole output until write EOF or flushed.
        """
        new_stream = Stream(writebufferlimit=None)
        if self._sendHeaders:
            # An extra copy
            self.container.subroutine(new_stream.copy_to(self.outputstream, self.container, buffering=False))
        self.outputstream = Stream(writebufferlimit=None)


Instruction:
Here you have a function in Python 3, explain what it does
async def rewrite(self, path, method = None, keepresponse = True):
        "Rewrite this request to another processor. Must be called before header sent"
        if self._sendHeaders:
            raise HttpProtocolException('Cannot modify response, headers already sent')
        if getattr(self.event, 'rewritedepth', 0) >= getattr(self.protocol, 'rewritedepthlimit', 32):
            raise HttpRewriteLoopException
        newpath = urljoin(quote_from_bytes(self.path).encode('ascii'), path)
        if newpath == self.fullpath or newpath == self.originalpath:
            raise HttpRewriteLoopException
        extraparams = {}
        if keepresponse:
            if hasattr(self, 'status'):
                extraparams['status'] = self.status
            extraparams['sent_headers'] = self.sent_headers
            extraparams['sent_cookies'] = self.sent_cookies
        r = HttpRequestEvent(self.host,
                               newpath,
                               self.method if method is None else method,
                               self.connection,
                               self.connmark,
                               self.xid,
                               self.protocol,
                               headers = self.headers,
                               headerdict = self.headerdict,
                               setcookies = self.setcookies,
                               stream = self.inputstream,
                               rewritefrom = self.fullpath,
                               originalpath = self.originalpath,
                               rewritedepth = getattr(self.event, 'rewritedepth', 0) + 1,
                               **extraparams
                               )
        await self.connection.wait_for_send(r)
        self._sendHeaders = True
        self.outputstream = None

Response: Rewrite this request to another processor. Must be called before header sent


Instruction:
Write a Python 3 function that can
redirect this request with 3xx status

Response:
async def redirect(self, path, status = 302):
        """
        Redirect this request with 3xx status
        """
        location = urljoin(urlunsplit((b'https' if self.https else b'http',
                                                                     self.host,
                                                                     quote_from_bytes(self.path).encode('ascii'),
                                                                     '',
                                                                     ''
                                                                     )), path)
        self.start_response(status, [(b'Location', location)])
        await self.write(b'' + self.escape(location) + b'')
        await self.flush(True)


Instruction:
Here you have a function in Python 3, explain what it does
def escape(self, text, quote = True):
        if isinstance(text, bytes):
            return escape_b(text, quote)
        else:
            return escape(text, quote)

Response: Escape special characters in HTML.


Instruction:
Create a Python 3 function for
writing an error response.

Response:
async def error(self, status=500, allowredirect = True, close = True, showerror = None, headers = []):
        """
        Show default error response
        """
        if showerror is None:
            showerror = self.showerrorinfo
        if self._sendHeaders:
            if showerror:
                typ, exc, tb = sys.exc_info()
                if exc:
                    await self.write('\n', buffering = False)
                    await self.writelines((self.nl2br(self.escape(v)) for v in traceback.format_exception(typ, exc, tb)), buffering = False)
                    await self.write('\n', close, False)
        elif allowredirect and status in self.protocol.errorrewrite:
            await self.rewrite(self.protocol.errorrewrite[status], b'GET')
        elif allowredirect and status in self.protocol.errorredirect:
            await self.redirect(self.protocol.errorredirect[status])
        else:
            self.start_response(status, headers)
            typ, exc, tb = sys.exc_info()
            if showerror and exc:
                await self.write('\n', buffering = False)
                await self.writelines((self.nl2br(self.escape(v)) for v in traceback.format_exception(typ, exc, tb)), buffering = False)
                await self.write('\n', close, False)
            else:
                await self.write(b'
' + _createstatus(status) + b'
', close, False)


Instruction:
Implement a Python 3 function for
writing data to the current output stream.

Response:
async def write(self, data, eof = False, buffering = True):
        """
        Write output to current output stream
        """
        if not self.outputstream:
            self.outputstream = Stream()
            self._startResponse()
        elif (not buffering or eof) and not self._sendHeaders:
            self._startResponse()
        if not isinstance(data, bytes):
            data = data.encode(self.encoding)
        await self.outputstream.write(data, self.connection, eof, False, buffering)