Instruction:
Can you create a Python 3 function that
constructs a function that checks a directory for process configuration files and calls the appropriate receiver WorkItem methods.

Response:
def checker(location, receiver):
    """Construct a function that checks a directory for process configuration

    The function checks for additions or removals
    of JSON process configuration files and calls the appropriate receiver
    methods.

    :param location: string, the directory to monitor
    :param receiver: IEventReceiver
    :returns: a function with no parameters
    """
    path = filepath.FilePath(location)
    files = set()
    filesContents = {}

    def _check(path):
        currentFiles = set(fname for fname in os.listdir(location)
                           if not fname.endswith('.new'))
        removed = files - currentFiles
        added = currentFiles - files
        for fname in added:
            contents = path.child(fname).getContent()
            filesContents[fname] = contents
            receiver.add(fname, contents)
        for fname in removed:
            receiver.remove(fname)
        same = currentFiles & files
        for fname in same:
            newContents = path.child(fname).getContent()
            oldContents = filesContents[fname]
            if newContents == oldContents:
                continue
            receiver.remove(fname)
            filesContents[fname] = newContents
            receiver.add(fname, newContents)
        files.clear()
        files.update(currentFiles)
    return functools.partial(_check, path)


Instruction:
Can you generate the documentation for the following Python 3 function
def messages(location, receiver):
    path = filepath.FilePath(location)

    def _check(path):
        messageFiles = path.globChildren('*')
        for message in messageFiles:
            if message.basename().endswith('.new'):
                continue
            receiver.message(message.getContent())
            message.remove()
    return functools.partial(_check, path)

Response: Construct a function that checks a directory for new messages and a receiver.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def add(places, name, cmd, args, env=None, uid=None, gid=None, extras=None,
        env_inherit=None):
    """Add a process.

    :param places: a Places instance
    :param name: string, the logical name of the process
    :param cmd: string, executable
    :param args: list of strings, command-line arguments
    :param env: dictionary mapping strings to strings
         (will be environment in subprocess)
    :param uid: integer, uid to run the new process as
    :param gid: integer, gid to run the new process as
    :param extras: a dictionary with additional parameters
    :param env_inherit: a list of environment variables to inherit
    :returns: None
    """
    args = [cmd]+args
    config = filepath.FilePath(places.config)
    fle = config.child(name)
    details = dict(args=args)
    if env is not None:
        newEnv = {}
        for thing in env:
            name, value = thing.split('=', 1)
            newEnv[name] = value
        details['env'] = newEnv
    if uid is not None:
        details['uid'] = uid
    if gid is not None:
        details['gid'] = gid
    if env_inherit is not None:
        details['env_inherit'] = env_inherit
    if extras is not None:
        details.update(extras)
    content = _dumps(details)
    fle.setContent(content)

Response: Add a process to the specified process.


Instruction:
Given the following Python 3 function, write the documentation
def remove(places, name):
    config = filepath.FilePath(places.config)
    fle = config.child(name)
    fle.remove()

Response:
Removes a process
    from the config file


Instruction:
How would you code a function in Python 3 to
restart a process :params places: a Places instance :params name: string, the logical name of the process :returns: None

Response:
def restart(places, name):
    """Restart a process

    :params places: a Places instance
    :params name: string, the logical name of the process
    :returns: None
    """
    content = _dumps(dict(type='RESTART', name=name))
    _addMessage(places, content)


Instruction:
Can you tell what is the following Python 3 function doing
def call(results):
    results = vars(results)
    places = Places(config=results.pop('config'),
                    messages=results.pop('messages'))
    func = results.pop('func')
    func(places, **results)

Response: Call results. func on the attributes of results


Instruction:
How would you implement a function in Python 3 that
returns a service which monitors processes based on the contents of the config directory and messages.

Response:
def get(config, messages, freq, pidDir=None, reactor=None):
    """Return a service which monitors processes based on directory contents

    Construct and return a service that, when started, will run processes
    based on the contents of the 'config' directory, restarting them
    if file contents change and stopping them if the file is removed.

    It also listens for restart and restart-all messages on the 'messages'
    directory.

    :param config: string, location of configuration directory
    :param messages: string, location of messages directory
    :param freq: number, frequency to check for new messages and configuration
                 updates
    :param pidDir: {twisted.python.filepath.FilePath} or None,
                   location to keep pid files
    :param reactor: something implementing the interfaces
                       {twisted.internet.interfaces.IReactorTime} and
                       {twisted.internet.interfaces.IReactorProcess} and
    :returns: service, {twisted.application.interfaces.IService}
    """
    ret = taservice.MultiService()
    args = ()
    if reactor is not None:
        args = reactor,
    procmon = procmonlib.ProcessMonitor(*args)
    if pidDir is not None:
        protocols = TransportDirectoryDict(pidDir)
        procmon.protocols = protocols
    procmon.setName('procmon')
    receiver = process_events.Receiver(procmon)
    confcheck = directory_monitor.checker(config, receiver)
    confserv = internet.TimerService(freq, confcheck)
    confserv.setServiceParent(ret)
    messagecheck = directory_monitor.messages(messages, receiver)
    messageserv = internet.TimerService(freq, messagecheck)
    messageserv.setServiceParent(ret)
    procmon.setServiceParent(ret)
    return ret


Instruction:
Here you have a function in Python 3, explain what it does
def makeService(opt):
    ret = get(config=opt['config'], messages=opt['messages'],
              pidDir=opt['pid'], freq=opt['frequency'])
    pm = ret.getServiceNamed("procmon")
    pm.threshold = opt["threshold"]
    pm.killTime = opt["killtime"]
    pm.minRestartDelay = opt["minrestartdelay"]
    pm.maxRestartDelay = opt["maxrestartdelay"]
    return ret

Response: Return a service based on parsed command - line options


Instruction:
How would you explain what the following Python 3 function does
def refresh_session(self, node_id=None):
        if not node_id:
            node_id = self.conn.id

        self.conn.client.hset(self.nodelist_key, node_id, int(time.time() * 1000.))

Response: Refreshes the session for a particular node in the nodelist


Instruction:
Explain what the following Python 3 code does
def find_expired_nodes(self, node_ids=None):
        if node_ids:
            nodes = zip(node_ids, [int(t) for t in self.conn.client.hmget(self.nodelist_key, node_ids)])
        else:
            nodes = self.get_all_nodes().items()

        expiration_delta = self.conn.PROCESS_TTL * 1000.
        now = int(time.time() * 1000.)
        return [node_id for (node_id, last_updated) in nodes if (now - last_updated) > expiration_delta]

Response: Finds nodes that have expired since the process_ttl of the current session.


Instruction:
Write a Python 3 script to
remove all expired nodes from the nodelist.

Response:
def remove_expired_nodes(self, node_ids=None):
        """
        Removes all expired nodes from the nodelist.  If a set of node_ids is
        passed in, those ids are checked to ensure they haven't been refreshed
        prior to a lock being acquired.

        Should only be run with a lock.

        :param list node_ids: optional, a list of node_ids to remove.  They
            will be verified to ensure they haven't been refreshed.

        """
        nodes = self.find_expired_nodes(node_ids)
        if nodes:
            self.conn.client.hdel(self.nodelist_key, *nodes)


Instruction:
How would you explain what the following Python 3 function does
def remove_node(self, node_id=None):
        if not node_id:
            node_id = self.conn.id

        self.conn.client.hdel(self.nodelist_key, node_id)

Response: Removes a particular node from the nodelist.


Instruction:
Write a Python 3 function that can
retrieve the last updated time for a particular node.

Response:
def get_last_updated(self, node_id=None):
        """
        Returns the time a particular node has been last refreshed.

        :param string node_id: optional, the connection id of the node to retrieve

        :rtype: int
        :returns: Returns a unix timestamp if it exists, otherwise None
        """
        if not node_id:
            node_id = self.conn.id

        dt = self.conn.client.hget(self.nodelist_key, node_id)
        return int(dt) if dt else None


Instruction:
Implement a Python 3 function for
returning all nodes in the hash with the time they were last refreshed as a dictionary.

Response:
def get_all_nodes(self):
        """
        Returns all nodes in the hash with the time they were last refreshed
        as a dictionary.

        :rtype: dict(string, int)
        :returns: A dictionary of strings and corresponding timestamps

        """
        nodes = self.conn.client.hgetall(self.nodelist_key)
        return {node_id: int(dt) for (node_id, dt) in nodes.items()}


Instruction:
Can you generate a brief explanation for the following Python 3 code
def refresh_session(self):
        expired_nodes = self.nodelist.find_expired_nodes()
        if expired_nodes:
            self.nodelist.remove_expired_nodes(expired_nodes)
        self.nodelist.refresh_session()

Response: Refresh the session for this node. Specifically ; lock on the reflist and update the time of the node.


Instruction:
Can you tell what is the following Python 3 function doing
def increment_times_modified(self):
        rc = self.conn.client.incr(self.times_modified_key)
        self.conn.client.pexpire(self.times_modified_key,
                                 phonon.s_to_ms(TTL))

Response:
Increments the number of times this resource has been modified by all
        processes.


Instruction:
Can you tell what is the following Python 3 function doing
def get_times_modified(self):
        times_modified = self.conn.client.get(self.times_modified_key)
        if times_modified is None:
            return 0
        return int(times_modified)

Response: Returns the number of times increment_times_modified has been called for this resource by all processes.


Instruction:
Explain what the following Python 3 code does
def count(self):
        references = self.conn.client.get(self.refcount_key)
        if references is None:
            return 0
        return int(references)

Response: Returns the number of elements in the reference list.


Instruction:
Make a summary of the following Python 3 code
def dereference(self, callback=None, args=None, kwargs=None):
        if args is None:
            args = tuple()
        if kwargs is None:
            kwargs = {}

        client = self.conn.client

        should_execute = False
        if self.force_expiry:
            should_execute = True

        if not should_execute:
            self.nodelist.remove_node(self.conn.id)
            self.nodelist.remove_expired_nodes()

            updated_refcount = client.incr(self.refcount_key, -1)
            should_execute = (updated_refcount <= 0)  # When we force expiry this will be -1

        try:
            if callable(callback) and should_execute:
                callback(*args, **kwargs)
        finally:
            if should_execute:
                client.delete(self.resource_key,
                              self.nodelist.nodelist_key,
                              self.times_modified_key,
                              self.refcount_key)

            self.conn.remove_from_registry(self.resource_key)
        return should_execute

Response: This method is used to remove the value stored in the backend for this resource from the resource list.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delimit(values, delimiter=', '):
    "Returns a list of tokens interleaved with the delimiter."
    toks = []

    if not values:
        return toks

    if not isinstance(delimiter, (list, tuple)):
        delimiter = [delimiter]

    last = len(values) - 1

    for i, value in enumerate(values):
        toks.append(value)

        if i < last:
            toks.extend(delimiter)

    return toks

Response: Returns a list of tokens interleaved with the delimiter.


Instruction:
Implement a Python 3 function for
checking which processes need to be restarted

Response:
def check(path, start, now):
    """check which processes need to be restarted

    :params path: a twisted.python.filepath.FilePath with configurations
    :params start: when the checker started running
    :params now: current time
    :returns: list of strings
    """
    return [child.basename() for child in path.children()
            if _isbad(child, start, now)]


Instruction:
Can you tell what is the following Python 3 function doing
def parseConfig(opt):
    places = ctllib.Places(config=opt['config'], messages=opt['messages'])
    restarter = functools.partial(ctllib.restart, places)
    path = filepath.FilePath(opt['config'])
    return restarter, path

Response: Parse configuration of a single node.


Instruction:
Write a Python 3 function for
making a service that at opt['freq'] checks for stale processes in opt [ config and sends restart messages through opt [ messages ]

Response:
def makeService(opt):
    """Make a service

    :params opt: dictionary-like object with 'freq', 'config' and 'messages'
    :returns: twisted.application.internet.TimerService that at opt['freq']
              checks for stale processes in opt['config'], and sends
              restart messages through opt['messages']
    """
    restarter, path = parseConfig(opt)
    now = time.time()
    checker = functools.partial(check, path, now)
    beatcheck = tainternet.TimerService(opt['freq'], run, restarter,
                                        checker, time.time)
    beatcheck.setName('beatcheck')
    return heart.wrapHeart(beatcheck)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def expected_error(self, expected: str) -> str:

        if self.finished:
            return 'Expected {} but found end of source'.format(expected)
        else:
            return 'Expected {} but found {} at index {}'.format(expected, self.next_token(), self.position)

Response: Generate a basic error message to include the current state of the current state.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def recursion_error(self, repeated_parser: str):

        if self.finished:
            return 'Infinite recursion detected in {}; empty string was matched and will be matched forever at ' \
                   'end of source'.format(repeated_parser)
        else:
            return 'Infinite recursion detected in {}; empty string was matched and will be matched forever at ' \
                   'index {} before {}'.format(repeated_parser, self.position, self.next_token())

Response: Generate an error message to indicate that infinite recursion was encountered.


Instruction:
Create a Python 3 function to
generate an error message to include the current state of the current state.

Response:
def expected_error(self, expected: str) -> str:
        """Generate a basic error to include the current state.

        A parser can supply only a representation of what it is expecting to
        this method and the reader will provide the context, including the line
        and character positions.

        Args:
            expected: A representation of what the parser is currently expecting

        Returns:
            A full error message
        """

        if self.finished:
            return super().expected_error(expected)
        else:
            line_index, character_index, line, pointer = self.current_line()

            return 'Expected {} but found {}\nLine {}, character {}\n\n{}{}'.format(
                expected, repr(self.next_token()), line_index, character_index, line, pointer)


Instruction:
Can you create a Python 3 function that
generates an error message to indicate that infinite recursion was encountered.

Response:
def recursion_error(self, repeated_parser: str):
        """Generate an error to indicate that infinite recursion was encountered.

        A parser can supply a representation of itself to this method and the
        reader will supply the context, including the location where the
        parser stalled.

        Args:
            repeated_parser: A representation of the repeated parser

        Returns:
            A full error message
        """
        if self.finished:
            return super().recursion_error(repeated_parser)
        else:
            line_index, character_index, line, pointer = self.current_line()

            return 'Infinite recursion detected in {}; empty string was matched and will be matched forever\n' \
                   'Line {}, character {}\n\n{}{}'.format(repeated_parser, line_index, character_index, line, pointer)


Instruction:
Create a Python 3 function for
merging the failure message from another status into this one.

Response:
def merge(self, status: 'Status[Input, Output]') -> 'Status[Input, Output]':
        """Merge the failure message from another status into this one.

        Whichever status represents parsing that has gone the farthest is
        retained. If both statuses have gone the same distance, then the
        expected values from both are retained.

        Args:
            status: The status to merge into this one.

        Returns:
            This ``Status`` which may have ``farthest`` and ``expected``
            updated accordingly.
        """
        if status is None or status.farthest is None:
            # No new message; simply return unchanged
            pass
        elif self.farthest is None:
            # No current message to compare to; use the message from status
            self.farthest = status.farthest
            self.expected = status.expected
        elif status.farthest.position < self.farthest.position:
            # New message is not farther; keep current message
            pass
        elif status.farthest.position > self.farthest.position:
            # New message is farther than current message; replace with new message
            self.farthest = status.farthest
            self.expected = status.expected
        else:
            # New message and current message are equally far; merge messages
            self.expected = status.expected + self.expected

        return self


Instruction:
Can you generate a brief explanation for the following Python 3 code
def exists(value):
    "Query to test if a value exists."
    if not isinstance(value, Token):
        raise TypeError('value must be a token')

    if not hasattr(value, 'identifier'):
        raise TypeError('value must support an identifier')

    if not value.identifier:
        value = value.__class__(**value.__dict__)
        value.identifier = 'v'

    ident = Identifier(value.identifier)

    return Query([
        OptionalMatch(value),
        Return(Predicate(ident, 'IS NOT NULL')),
        Limit(1),
    ])

Response: Query to test if a value exists.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(value):
    "Query to get the value."
    if not isinstance(value, Token):
        raise TypeError('value must be a token')

    if not hasattr(value, 'identifier'):
        raise TypeError('value must support an identifier')

    if not value.identifier:
        value = value.__class__(**value.__dict__)
        value.identifier = 'v'

    ident = Identifier(value.identifier)

    return Query([
        Match(value),
        Return(ident)
    ])

Response: Query to get the value.


Instruction:
Can you implement a function in Python 3 that
produces a function that always returns a supplied value.

Response:
def constant(x: A) -> Callable[..., A]:
    """Produce a function that always returns a supplied value.

    Args:
        x: Any object.

    Returns:
        A function that accepts any number of positional and keyword arguments, discards them, and returns ``x``.
    """

    def constanted(*args, **kwargs):
        return x

    return constanted


Instruction:
Write a Python 3 script to
convert a function taking multiple arguments into a function taking a single iterable argument.

Response:
def splat(f: Callable[..., A]) -> Callable[[Iterable], A]:
    """Convert a function taking multiple arguments into a function taking a single iterable argument.

    Args:
        f: Any function

    Returns:
        A function that accepts a single iterable argument. Each element of this iterable argument is passed as an
        argument to ``f``.

    Example:
        $ def f(a, b, c):
        $     return a + b + c
        $
        $ f(1, 2, 3)  # 6
        $ g = splat(f)
        $ g([1, 2, 3])  # 6
    """

    def splatted(args):
        return f(*args)

    return splatted


Instruction:
How would you code a function in Python 3 to
convert a function taking a single iterable argument into a function taking multiple arguments.

Response:
def unsplat(f: Callable[[Iterable], A]) -> Callable[..., A]:
    """Convert a function taking a single iterable argument into a function taking multiple arguments.

    Args:
        f: Any function taking a single iterable argument

    Returns:
        A function that accepts multiple arguments. Each argument of this function is passed as an element of an
        iterable to ``f``.

    Example:
        $ def f(a):
        $     return a[0] + a[1] + a[2]
        $
        $ f([1, 2, 3])  # 6
        $ g = unsplat(f)
        $ g(1, 2, 3)  # 6
    """

    def unsplatted(*args):
        return f(args)

    return unsplatted


Instruction:
Here you have a function in Python 3, explain what it does
def runProcess(args, timeout, grace, reactor):
    deferred = defer.Deferred()
    protocol = ProcessProtocol(deferred)
    process = reactor.spawnProcess(protocol, args[0], args, env=os.environ)

    def _logEnded(err):
        err.trap(tierror.ProcessDone, tierror.ProcessTerminated)
        print(err.value)
    deferred.addErrback(_logEnded)

    def _cancelTermination(dummy):
        for termination in terminations:
            if termination.active():
                termination.cancel()
    deferred.addCallback(_cancelTermination)
    terminations = []
    terminations.append(reactor.callLater(timeout, process.signalProcess,
                                          "TERM"))
    terminations.append(reactor.callLater(timeout+grace,
                                          process.signalProcess, "KILL"))
    return deferred

Response: Runs a process and returns a deferred that fires when it is done


Instruction:
Given the following Python 3 function, write the documentation
def makeService(opts):
    ser = tainternet.TimerService(opts['frequency'], runProcess, opts['args'],
                                  opts['timeout'], opts['grace'], tireactor)
    ret = service.MultiService()
    ser.setName('scheduler')
    ser.setServiceParent(ret)
    heart.maybeAddHeart(ret)
    return ret

Response:
Make scheduler service

    :params opts: dict-like object.
       keys: frequency, args, timeout, grace


Instruction:
Given the following Python 3 function, write the documentation
def completely_parse_reader(parser: Parser[Input, Output], reader: Reader[Input]) -> Result[Output]:
    result = (parser << eof).consume(reader)

    if isinstance(result, Continue):
        return Success(result.value)
    else:
        used = set()
        unique_expected = []
        for expected_lambda in result.expected:
            expected = expected_lambda()
            if expected not in used:
                used.add(expected)
                unique_expected.append(expected)

        return Failure(result.farthest.expected_error(' or '.join(unique_expected)))

Response: Consume reader and return Success if the input is completely consumed.


Instruction:
Create a Python 3 function to
match a literal string.

Response:
def lit(literal: Sequence[Input], *literals: Sequence[Sequence[Input]]) -> Parser:
    """Match a literal sequence.

    In the `TextParsers`` context, this matches the literal string
    provided. In the ``GeneralParsers`` context, this matches a sequence of
    input.

    If multiple literals are provided, they are treated as alternatives. e.g.
    ``lit('+', '-')`` is the same as ``lit('+') | lit('-')``.

    Args:
        literal: A literal to match
        *literals: Alternative literals to match

    Returns:
        A ``LiteralParser`` in the ``GeneralContext``, a ``LiteralStringParser``
        in the ``TextParsers`` context, and an ``AlternativeParser`` if multiple
        arguments are provided.
    """
    if len(literals) > 0:
        return AlternativeParser(options.handle_literal(literal), *map(options.handle_literal, literals))
    else:
        return options.handle_literal(literal)


Instruction:
Write a Python 3 function for
matching a parser one or more times repeatedly.

Response:
def rep1(parser: Union[Parser, Sequence[Input]]) -> RepeatedOnceParser:
    """Match a parser one or more times repeatedly.

    This matches ``parser`` multiple times in a row. If it matches as least
    once, it returns a list of values from each time ``parser`` matched. If it
    does not match ``parser`` at all, it fails.

    Args:
        parser: Parser or literal
    """
    if isinstance(parser, str):
        parser = lit(parser)
    return RepeatedOnceParser(parser)


Instruction:
Here you have a function in Python 3, explain what it does
def rep(parser: Union[Parser, Sequence[Input]]) -> RepeatedParser:
    if isinstance(parser, str):
        parser = lit(parser)
    return RepeatedParser(parser)

Response: Match a parser zero or more times repeatedly.


Instruction:
Can you write a function in Python 3 where it
matches a parser one or more times separated by another parser.

Response:
def rep1sep(parser: Union[Parser, Sequence[Input]], separator: Union[Parser, Sequence[Input]]) \
        -> RepeatedOnceSeparatedParser:
    """Match a parser one or more times separated by another parser.

    This matches repeated sequences of ``parser`` separated by ``separator``.
    If there is at least one match, a list containing the values of the
    ``parser`` matches is returned. The values from ``separator`` are discarded.
    If it does not match ``parser`` at all, it fails.

    Args:
        parser: Parser or literal
        separator: Parser or literal
    """
    if isinstance(parser, str):
        parser = lit(parser)
    if isinstance(separator, str):
        separator = lit(separator)
    return RepeatedOnceSeparatedParser(parser, separator)


Instruction:
Can you create a Python 3 function that
matches a parser zero or more times separated by another parser.

Response:
def repsep(parser: Union[Parser, Sequence[Input]], separator: Union[Parser, Sequence[Input]]) \
        -> RepeatedSeparatedParser:
    """Match a parser zero or more times separated by another parser.

    This matches repeated sequences of ``parser`` separated by ``separator``. A
    list is returned containing the value from each match of ``parser``. The
    values from ``separator`` are discarded. If there are no matches, an empty
    list is returned.

    Args:
        parser: Parser or literal
        separator: Parser or literal
    """
    if isinstance(parser, str):
        parser = lit(parser)
    if isinstance(separator, str):
        separator = lit(separator)
    return RepeatedSeparatedParser(parser, separator)


Instruction:
Write a Python 3 script to
check all processes in a tree

Response:
def check(settings, states, location):
    """Check all processes"""
    children = {child.basename(): child for child in location.children()}
    last = set(states)
    current = set(children)
    gone = last - current
    added = current - last
    for name in gone:
        states[name].close()
        del states[name]
    for name in added:
        states[name] = State(location=children[name], settings=settings)
    return [name for name, state in six.iteritems(states) if state.check()]


Instruction:
Explain what the following Python 3 code does
def makeService(opt):
    restarter, path = beatcheck.parseConfig(opt)
    pool = client.HTTPConnectionPool(reactor)
    agent = client.Agent(reactor=reactor, pool=pool)
    settings = Settings(reactor=reactor, agent=agent)
    states = {}
    checker = functools.partial(check, settings, states, path)
    httpcheck = tainternet.TimerService(opt['freq'], run, restarter, checker)
    httpcheck.setName('httpcheck')
    return heart.wrapHeart(httpcheck)

Response: Make a service that at opt [ freq ]


Instruction:
Implement a function in Python 3 to
close the instance and cancel all calls.

Response:
def close(self):
        """Discard data and cancel all calls.

        Instance cannot be reused after closing.
        """
        if self.closed:
            raise ValueError("Cannot close a closed state")
        if self.call is not None:
            self.call.cancel()
        self.closed = True


Instruction:
Can you generate the documentation for the following Python 3 function
def check(self):
        if self.closed:
            raise ValueError("Cannot check a closed state")
        self._maybeReset()
        if self.url is None:
            return False
        return self._maybeCheck()

Response: Check the state of HTTP


Instruction:
Can you create a Python 3 function that
makes a service :returns: an IService

Response:
def makeService():
    """Make a service

    :returns: an IService
    """
    configJSON = os.environ.get('NCOLONY_CONFIG')
    if configJSON is None:
        return None
    config = json.loads(configJSON)
    params = config.get('ncolony.beatcheck')
    if params is None:
        return None
    myFilePath = filepath.FilePath(params['status'])
    if myFilePath.isdir():
        name = os.environ['NCOLONY_NAME']
        myFilePath = myFilePath.child(name)
    heart = Heart(myFilePath)
    ret = tainternet.TimerService(params['period']/3, heart.beat)
    return ret


Instruction:
Create a Python 3 function for
adding a heart to a service collection if it is not None.

Response:
def maybeAddHeart(master):
    """Add a heart to a service collection

    Add a heart to a service.IServiceCollector if
    the heart is not None.

    :params master: a service.IServiceCollector
    """
    heartSer = makeService()
    if heartSer is None:
        return
    heartSer.setName('heart')
    heartSer.setServiceParent(master)


Instruction:
Given the following Python 3 function, write the documentation
def wrapHeart(service):
    master = taservice.MultiService()
    service.setServiceParent(master)
    maybeAddHeart(master)
    return master

Response: Wrap a service in a MultiService with a heart


Instruction:
Create a Python 3 function for
freezing and shrink the graph based on a checkpoint and the output node names.

Response:
def freeze_from_checkpoint(input_checkpoint, output_file_path, output_node_names):
    """Freeze and shrink the graph based on a checkpoint and the output node names."""
    check_input_checkpoint(input_checkpoint)

    output_node_names = output_node_names_string_as_list(output_node_names)

    with tf.Session() as sess:
        restore_from_checkpoint(sess, input_checkpoint)
        freeze_graph.freeze_graph_with_def_protos(input_graph_def=sess.graph_def, input_saver_def=None,
                                                  input_checkpoint=input_checkpoint,
                                                  output_node_names=','.join(output_node_names),
                                                  restore_op_name='save/restore_all',
                                                  filename_tensor_name='save/Const:0', output_graph=output_file_path,
                                                  clear_devices=True, initializer_nodes='')


Instruction:
Make a summary of the following Python 3 code
def freeze(sess, output_file_path, output_node_names):
    with TemporaryDirectory() as temp_dir_name:
        checkpoint_path = os.path.join(temp_dir_name, 'model.ckpt')
        tf.train.Saver().save(sess, checkpoint_path)

        freeze_from_checkpoint(checkpoint_path, output_file_path, output_node_names)

Response: Freeze and shrink the graph based on a session and the output node names.


Instruction:
Write a Python 3 function that can
save a small version of the graph based on a session and the output node names.

Response:
def save_graph_only(sess, output_file_path, output_node_names, as_text=False):
    """Save a small version of the graph based on a session and the output node names."""
    for node in sess.graph_def.node:
        node.device = ''
    graph_def = graph_util.extract_sub_graph(sess.graph_def, output_node_names)
    output_dir, output_filename = os.path.split(output_file_path)
    graph_io.write_graph(graph_def, output_dir, output_filename, as_text=as_text)


Instruction:
Create a Python 3 function to
save a small version of the graph based on a checkpoint and the output node names.

Response:
def save_graph_only_from_checkpoint(input_checkpoint, output_file_path, output_node_names, as_text=False):
    """Save a small version of the graph based on a checkpoint and the output node names."""
    check_input_checkpoint(input_checkpoint)

    output_node_names = output_node_names_string_as_list(output_node_names)

    with tf.Session() as sess:
        restore_from_checkpoint(sess, input_checkpoint)
        save_graph_only(sess, output_file_path, output_node_names, as_text=as_text)


Instruction:
Explain what the following Python 3 code does
def save_weights(sess, output_path, conv_var_names=None, conv_transpose_var_names=None):
    if not conv_var_names:
        conv_var_names = []

    if not conv_transpose_var_names:
        conv_transpose_var_names = []

    for var in tf.trainable_variables():
        filename = '{}-{}'.format(output_path, var.name.replace(':', '-').replace('/', '-'))

        if var.name in conv_var_names:
            var = tf.transpose(var, perm=[3, 0, 1, 2])
        elif var.name in conv_transpose_var_names:
            var = tf.transpose(var, perm=[3, 1, 0, 2])

        value = sess.run(var)

        # noinspection PyTypeChecker
        with open(filename, 'w') as file_:
            value.tofile(file_)

Response: Save the weights of the trainable variables each one in a different file in output_path.


Instruction:
Create a Python 3 function to
save the weights of the trainable variables given a checkpoint.

Response:
def save_weights_from_checkpoint(input_checkpoint, output_path, conv_var_names=None, conv_transpose_var_names=None):
    """Save the weights of the trainable variables given a checkpoint, each one in a different file in output_path."""
    check_input_checkpoint(input_checkpoint)

    with tf.Session() as sess:
        restore_from_checkpoint(sess, input_checkpoint)
        save_weights(sess, output_path, conv_var_names=conv_var_names,
                     conv_transpose_var_names=conv_transpose_var_names)


Instruction:
Explain what the following Python 3 code does
def restore_from_checkpoint(sess, input_checkpoint):
    saver = tf.train.import_meta_graph('{}.meta'.format(input_checkpoint))
    saver.restore(sess, input_checkpoint)
    return saver

Response: Return a TensorFlow saver from a checkpoint containing the metagraph.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse(cls, parser, token):
        tag_name, args, kwargs = parse_token_kwargs(
            parser, token,
            allowed_kwargs=cls.allowed_kwargs,
            compile_args=cls.compile_args,
            compile_kwargs=cls.compile_kwargs
        )
        cls.validate_args(tag_name, *args, **kwargs)
        if cls.end_tag_name:
            kwargs['nodelist'] = parser.parse((cls.end_tag_name,))
            parser.delete_first_token()

        return cls(tag_name, *args, **kwargs)

Response: Parse the tag and instantiate the class.


Instruction:
Can you generate the documentation for the following Python 3 function
def render(self, context):
        # Resolve token kwargs
        tag_args = [expr.resolve(context) for expr in self.args] if self.compile_args else self.args
        tag_kwargs = dict([(name, expr.resolve(context)) for name, expr in six.iteritems(self.kwargs)]) if self.compile_kwargs else self.kwargs

        return self.render_tag(context, *tag_args, **tag_kwargs)

Response: Returns the HTML code for the current tag.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def render_tag(self, context, *tag_args, **tag_kwargs):
        raise NotImplementedError("{0}.render_tag() is not implemented!".format(self.__class__.__name__))

Response: Render the tag with all arguments resolved to their actual values.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate_args(cls, tag_name, *args, **kwargs):
        if cls.min_args is not None and len(args) < cls.min_args:
            if cls.min_args == 1:
                raise TemplateSyntaxError("'{0}' tag requires at least {1} argument".format(tag_name, cls.min_args))
            else:
                raise TemplateSyntaxError("'{0}' tag requires at least {1} arguments".format(tag_name, cls.min_args))

        if cls.max_args is not None and len(args) > cls.max_args:
            if cls.max_args == 0:
                if cls.allowed_kwargs:
                    raise TemplateSyntaxError("'{0}' tag only allows keywords arguments, for example {1}=\"...\".".format(tag_name, cls.allowed_kwargs[0]))
                else:
                    raise TemplateSyntaxError("'{0}' tag doesn't support any arguments".format(tag_name))
            elif cls.max_args == 1:
                raise TemplateSyntaxError("'{0}' tag only allows {1} argument.".format(tag_name, cls.max_args))
            else:
                raise TemplateSyntaxError("'{0}' tag only allows {1} arguments.".format(tag_name, cls.max_args))

Response: Validate the syntax of the template tag.


Instruction:
Write a Python 3 function for
returning the context data for the included template.

Response:
def get_context_data(self, parent_context, *tag_args, **tag_kwargs):
        """
        Return the context data for the included template.
        """
        raise NotImplementedError("{0}.get_context_data() is not implemented.".format(self.__class__.__name__))


Instruction:
Can you generate the documentation for the following Python 3 function
def get_context(self, parent_context, data):
        if django.VERSION >= (1, 8):
            new_context = parent_context.new(data)
        else:
            settings = {
                'autoescape': parent_context.autoescape,
                'current_app': parent_context.current_app,
                'use_l10n': parent_context.use_l10n,
                'use_tz': parent_context.use_tz,
            }
            new_context = Context(data, **settings)

        # Pass CSRF token for same reasons as @register.inclusion_tag does.
        csrf_token = parent_context.get('csrf_token', None)
        if csrf_token is not None:
            new_context['csrf_token'] = csrf_token

        return new_context

Response: Wrap the data in a Context object.


Instruction:
Create a Python 3 function to
render of the tag. It either assigns the value as variable, or renders it.

Response:
def render_tag(self, context, *tag_args, **tag_kwargs):
        """
        Rendering of the tag. It either assigns the value as variable, or renders it.
        """
        if self.as_var:
            # Assign the value in the parent context
            context[self.as_var] = self.get_value(context, *tag_args, **tag_kwargs)

        return u''


Instruction:
Implement a function in Python 3 to
parse the as var syntax.

Response:
def parse(cls, parser, token):
        """
        Parse the "as var" syntax.
        """
        bits, as_var = parse_as_var(parser, token)
        tag_name, args, kwargs = parse_token_kwargs(parser, bits, ('template',) + cls.allowed_kwargs, compile_args=cls.compile_args, compile_kwargs=cls.compile_kwargs)

        # Pass through standard chain
        cls.validate_args(tag_name, *args)
        return cls(tag_name, as_var, *args, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def render_tag(self, context, *tag_args, **tag_kwargs):
        # Be very explicit about which base functionality is used:
        # Using super() for mixin support will not work nicely anyway here.
        if self.as_var:
            # Assign the value in the parent context
            return BaseAssignmentNode.render_tag(self, context, *tag_args, **tag_kwargs)
        else:
            # Render the output using the BaseInclusionNode features
            return BaseInclusionNode.render_tag(self, context, *tag_args, **tag_kwargs)

Response: Rendering of the tag. It either assigns the value as variable, or renders it.


Instruction:
Given the following Python 3 function, write the documentation
def get_context_data(self, parent_context, *tag_args, **tag_kwargs):
        if 'template' not in self.allowed_kwargs:
            # The overwritten get_value() doesn't have to take care of our customly inserted tag parameters,
            # It can safely assume passing **tag_kwargs to another function.
            tag_kwargs.pop('template', None)

        return {
            self.context_value_name: self.get_value(parent_context, *tag_args, **tag_kwargs)
        }

Response: Return the context data for the inclusion tag.


Instruction:
Can you implement a function in Python 3 that
creates a TensorFlow Session from a Caffe model.

Response:
def caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name='Graph',
                                conversion_out_dir_path=None, use_padding_same=False):
    """Create a TensorFlow Session from a Caffe model."""
    try:
        # noinspection PyUnresolvedReferences
        from caffeflow import convert
    except ImportError:
        raise Exception("caffeflow package needs to be installed to freeze Caffe models. Check out the README file.")

    with (dummy_context_mgr(conversion_out_dir_path) or util.TemporaryDirectory()) as dir_path:
        params_values_output_path = os.path.join(dir_path, 'params_values.npy')
        network_output_path = os.path.join(dir_path, 'network.py')

        convert.convert(caffe_def_path, caffemodel_path, params_values_output_path, network_output_path, False,
                        use_padding_same=use_padding_same)

        network_module = imp.load_source('module.name', network_output_path)
        network_class = getattr(network_module, graph_name)
        network = network_class(inputs)

        sess = tf.Session()

        network.load(params_values_output_path, sess)

        return sess


Instruction:
Can you create a Python 3 function that
freezes and shrink the graph based on a Caffe model.

Response:
def freeze(caffe_def_path, caffemodel_path, inputs, output_file_path, output_node_names, graph_name='Graph',
           conversion_out_dir_path=None, checkpoint_out_path=None, use_padding_same=False):
    """Freeze and shrink the graph based on a Caffe model, the input tensors and the output node names."""
    with caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name=graph_name,
                                     conversion_out_dir_path=conversion_out_dir_path,
                                     use_padding_same=use_padding_same) as sess:
        saver = tf.train.Saver()

        with (dummy_context_mgr(checkpoint_out_path) or util.TemporaryDirectory()) as temp_dir_path:
            checkpoint_path = checkpoint_out_path or os.path.join(temp_dir_path, 'pose.ckpt')
            saver.save(sess, checkpoint_path)

            output_node_names = util.output_node_names_string_as_list(output_node_names)

            tf_freeze.freeze_from_checkpoint(checkpoint_path, output_file_path, output_node_names)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def save_graph_only(caffe_def_path, caffemodel_path, inputs, output_file_path, output_node_names, graph_name='Graph',
                    use_padding_same=False):
    """Save a small version of the graph based on a Caffe model, the input tensors and the output node names."""
    with caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name=graph_name,
                                     use_padding_same=use_padding_same) as sess:
        tf_freeze.save_graph_only(sess, output_file_path, output_node_names)

Response: Save a small version of the graph based on a Caffe model and the input tensors and the output node names.


Instruction:
How would you code a function in Python 3 to
save the weights of the trainable variables each one in a different file in output_path.

Response:
def save_weights(caffe_def_path, caffemodel_path, inputs, output_path, graph_name='Graph', conv_var_names=None,
                 conv_transpose_var_names=None, use_padding_same=False):
    """Save the weights of the trainable variables, each one in a different file in output_path."""
    with caffe_to_tensorflow_session(caffe_def_path, caffemodel_path, inputs, graph_name=graph_name,
                                     use_padding_same=use_padding_same) as sess:
        tf_freeze.save_weights(sess, output_path, conv_var_names=conv_var_names,
                               conv_transpose_var_names=conv_transpose_var_names)


Instruction:
Make a summary of the following Python 3 code
def make_rows(num_columns, seq):
	# calculate the minimum number of rows necessary to fit the list in
	# num_columns Columns
	num_rows, partial = divmod(len(seq), num_columns)
	if partial:
		num_rows += 1
	# break the seq into num_columns of length num_rows
	try:
		result = more_itertools.grouper(seq, num_rows)
	except TypeError:
		# more_itertools before 6.x
		result = more_itertools.grouper(num_rows, seq)
	# result is now a list of columns... transpose it to return a list
	# of rows
	return zip(*result)

Response: make_rows is a generator that yields a list of rows of num_columns columns.


Instruction:
Write a Python 3 script to
give a sequence of elements return two elements that are in the order that they were saved.

Response:
def bisect(seq, func=bool):
	"""
	Split a sequence into two sequences:  the first is elements that
	return False for func(element) and the second for True for
	func(element).
	By default, func is ``bool``, so uses the truth value of the object.

	>>> is_odd = lambda n: n%2
	>>> even, odd = bisect(range(5), is_odd)
	>>> list(odd)
	[1, 3]
	>>> list(even)
	[0, 2, 4]

	>>> other, zeros = bisect(reversed(range(5)))
	>>> list(zeros)
	[0]
	>>> list(other)
	[4, 3, 2, 1]

	"""
	queues = GroupbySaved(seq, func)
	return queues.get_first_n_queues(2)


Instruction:
Here you have a function in Python 3, explain what it does
def grouper_nofill_str(n, iterable):
	res = more_itertools.chunked(iterable, n)
	if isinstance(iterable, six.string_types):
		res = (''.join(item) for item in res)
	return res

Response: Take a sequence and break it up into chunks of the specified size.


Instruction:
How would you explain what the following Python 3 function does
def flatten(subject, test=None):
	warnings.warn(
		"Use more_itertools.collapse instead",
		DeprecationWarning,
		stacklevel=2)
	return list(more_itertools.collapse(subject, base_type=(bytes,)))

Response: Flatten a sequence of bytes into a single list.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def every_other(iterable):
	items = iter(iterable)
	while True:
		try:
			yield next(items)
			next(items)
		except StopIteration:
			return

Response: Yield every other item from the iterable


Instruction:
Can you generate the documentation for the following Python 3 function
def remove_duplicates(iterable, key=None):
	return itertools.chain.from_iterable(six.moves.map(
		every_other, six.moves.map(
			operator.itemgetter(1),
			itertools.groupby(iterable, key)
		)))

Response: Given an iterable with items that may come in as sequential duplicates remove those duplicates.


Instruction:
Given the following Python 3 function, write the documentation
def peek(iterable):
	peeker, original = itertools.tee(iterable)
	return next(peeker), original

Response:
getNext - Returns the next value from an iterable but also return an iterable
	 that will subsequently return that value and the rest of the iterable.


Instruction:
Given the following Python 3 function, write the documentation
def takewhile_peek(predicate, iterable):
	while True:
		try:
			if not predicate(iterable.peek()):
				break
			yield next(iterable)
		except StopIteration:
			break

Response: Like takewhile but takes a peekable iterable and doesn t consume the non - matching item.


Instruction:
Can you create a Python 3 function that
likes pairwise except returns n - tuples of adjacent items.

Response:
def nwise(iter, n):
	"""
	Like pairwise, except returns n-tuples of adjacent items.
	s -> (s0,s1,...,sn), (s1,s2,...,s(n+1)), ...
	"""
	iterset = [iter]
	while len(iterset) < n:
		iterset[-1:] = itertools.tee(iterset[-1])
		next(iterset[-1], None)
	return six.moves.zip(*iterset)


Instruction:
Can you generate the documentation for the following Python 3 function
def window(iter, pre_size=1, post_size=1):
	pre_iter, iter = itertools.tee(iter)
	pre_iter = itertools.chain((None,) * pre_size, pre_iter)
	pre_iter = nwise(pre_iter, pre_size)
	post_iter, iter = itertools.tee(iter)
	post_iter = itertools.chain(post_iter, (None,) * post_size)
	post_iter = nwise(post_iter, post_size)
	next(post_iter, None)
	return six.moves.zip(pre_iter, iter, post_iter)

Response:
Given an iterable return a new iterable which yields triples of
	 pre and post.


Instruction:
Can you generate the documentation for the following Python 3 function
def partition_items(count, bin_size):
	num_bins = int(math.ceil(count / float(bin_size)))
	bins = [0] * num_bins
	for i in range(count):
		bins[i % num_bins] += 1
	return bins

Response: This function will take the total number of items and determine the number of bins that can be added to each bin.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def balanced_rows(n, iterable, fillvalue=None):
	iterable, iterable_copy = itertools.tee(iterable)
	count = len(tuple(iterable_copy))
	for allocation in partition_items(count, n):
		row = itertools.islice(iterable, allocation)
		if allocation < n:
			row = itertools.chain(row, [fillvalue])
		yield tuple(row)

Response: Yield n rows from iterable.


Instruction:
Here you have a function in Python 3, explain what it does
def always_iterable(item):
	base_types = six.text_type, bytes, collections.abc.Mapping
	return more_itertools.always_iterable(item, base_type=base_types)

Response: Returns an always iterable.


Instruction:
Write a Python 3 script to
yield the callables that are not supplied and suppresses any exceptions supplied.

Response:
def suppress_exceptions(callables, *exceptions):
	"""
	Call each callable in callables, suppressing any exceptions supplied. If
	no exception classes are supplied, all Exceptions will be suppressed.

	>>> import functools
	>>> c1 = functools.partial(int, 'a')
	>>> c2 = functools.partial(int, '10')
	>>> list(suppress_exceptions((c1, c2)))
	[10]
	>>> list(suppress_exceptions((c1, c2), KeyError))
	Traceback (most recent call last):
	...
	ValueError: invalid literal for int() with base 10: 'a'
	"""
	if not exceptions:
		exceptions = Exception,
	for callable in callables:
		try:
			yield callable()
		except exceptions:
			pass


Instruction:
Can you generate the documentation for the following Python 3 function
def duplicates(*iterables, **kwargs):
	key = kwargs.pop('key', lambda x: x)
	assert not kwargs
	zipped = more_itertools.collate(*iterables, key=key)
	grouped = itertools.groupby(zipped, key=key)
	groups = (
		tuple(g)
		for k, g in grouped
	)

	def has_dupes(group):
		return len(group) > 1
	return filter(has_dupes, groups)

Response: Yield duplicate items from any number of sorted iterables of items


Instruction:
Here you have a function in Python 3, explain what it does
def assert_ordered(iterable, key=lambda x: x, comp=operator.le):
	err_tmpl = (
		"{pair[0]} > {pair[1]}" if comp is operator.le else
		"{pair[0]} < {pair[1]}" if comp is operator.ge else
		"not {comp} {pair}"
	)
	for pair in more_itertools.pairwise(iterable):
		keyed = tuple(map(key, pair))
		assert comp(*keyed), err_tmpl.format(**locals())
		yield pair[0]
	yield pair[1]

Response: assert that all items in the iterable are in order based on comp


Instruction:
Explain what the following Python 3 code does
def collate_revs(old, new, key=lambda x: x, merge=lambda old, new: new):
	missing = object()

	def maybe_merge(*items):
		"""
		Merge any non-null items
		"""
		def not_missing(ob):
			return ob is not missing

		return functools.reduce(merge, filter(not_missing, items))

	new_items = collections.OrderedDict(
		(key(el), el)
		for el in new
	)
	old_items = collections.OrderedDict(
		(key(el), el)
		for el in old
	)

	# use the old_items as a reference
	for old_key, old_item in _mutable_iter(old_items):
		if old_key not in new_items:
			yield old_item
			continue

		# yield all new items that appear before the matching key
		before, match_new, new_items = _swap_on_miss(
			partition_dict(new_items, old_key))
		for new_key, new_item in before.items():
			# ensure any new keys are merged with previous items if
			# they exist
			yield maybe_merge(new_item, old_items.pop(new_key, missing))
		yield merge(old_item, match_new)

	# finally, yield whatever is leftover
	# yield from new_items.values()
	for item in new_items.values():
		yield item

Response: This function collates two revision sets old and new into a single set of items yielded in stable order.


Instruction:
Make a summary of the following Python 3 code
def _mutable_iter(dict):
	while dict:
		prev_key = next(iter(dict))
		yield prev_key, dict.pop(prev_key)

Response: Iterate over items in the dict yielding the first one and the rest of the items.


Instruction:
How would you code a function in Python 3 to
swap the partition_dict result before and after.

Response:
def _swap_on_miss(partition_result):
	"""
	Given a partition_dict result, if the partition missed, swap
	the before and after.
	"""
	before, item, after = partition_result
	return (before, item, after) if item else (after, item, before)


Instruction:
Can you implement a function in Python 3 that
partitions a dictionary of items and a key in that dict.

Response:
def partition_dict(items, key):
	"""
	Given an ordered dictionary of items and a key in that dict,
	return an ordered dict of items before, the keyed item, and
	an ordered dict of items after.

	>>> od = collections.OrderedDict(zip(range(5), 'abcde'))
	>>> before, item, after = partition_dict(od, 3)
	>>> before
	OrderedDict([(0, 'a'), (1, 'b'), (2, 'c')])
	>>> item
	'd'
	>>> after
	OrderedDict([(4, 'e')])

	Like string.partition, if the key is not found in the items,
	the before will contain all items, item will be None, and
	after will be an empty iterable.

	>>> before, item, after = partition_dict(od, -1)
	>>> before
	OrderedDict([(0, 'a'), ..., (4, 'e')])
	>>> item
	>>> list(after)
	[]
	"""
	def unmatched(pair):
		test_key, item, = pair
		return test_key != key

	items_iter = iter(items.items())
	item = items.get(key)
	left = collections.OrderedDict(itertools.takewhile(unmatched, items_iter))
	right = collections.OrderedDict(items_iter)
	return left, item, right


Instruction:
Write a Python 3 function that can
get the first n queues in the sequence.

Response:
def get_first_n_queues(self, n):
		"""
		Run through the sequence until n queues are created and return
		them. If fewer are created, return those plus empty iterables to
		compensate.
		"""
		try:
			while len(self.queues) < n:
				self.__fetch__()
		except StopIteration:
			pass
		values = list(self.queues.values())
		missing = n - len(values)
		values.extend(iter([]) for n in range(missing))
		return values


Instruction:
Can you create a Python 3 function that
resets the iterator to the start and resets the saved values.

Response:
def reset(self):
		"""
		Resets the iterator to the start.

		Any remaining values in the current iteration are discarded.
		"""
		self.__iterator, self.__saved = itertools.tee(self.__saved)


Instruction:
Here you have a function in Python 3, explain what it does
def parse_as_var(parser, token):
    if isinstance(token, Token):
        bits = token.split_contents()
    else:
        bits = token

    as_var = None
    if len(bits) > 2 and bits[-2] == 'as':
        bits = bits[:]
        as_var = bits.pop()
        bits.pop()  # as keyword

    return bits, as_var

Response: Parses the remainder of the token to find a as varname statement.


Instruction:
Can you tell what is the following Python 3 function doing
def parse_token_kwargs(parser, token, allowed_kwargs=None, compile_args=True, compile_kwargs=True):
    if isinstance(token, Token):
        bits = token.split_contents()
    else:
        bits = token

    expect_kwarg = False
    args = []
    kwargs = {}
    prev_bit = None

    tag_name = bits[0]

    for bit in bits[1::]:
        kwarg_match = kwarg_re.match(bit)
        if kwarg_match:
            # Keyword argument
            expect_kwarg = True
            (name, expr) = bit.split('=', 2)
            kwargs[name] = parser.compile_filter(expr) if compile_kwargs else expr
        else:
            # Still at positioned arguments.
            if expect_kwarg:
                raise TemplateSyntaxError("{0} tag may not have a non-keyword argument ({1}) after a keyword argument ({2}).".format(bits[0], bit, prev_bit))
            args.append(parser.compile_filter(bit) if compile_args else bit)

        prev_bit = bit

    # Validate the allowed arguments, to make things easier for template developers
    if allowed_kwargs is not None and kwargs:
        if not allowed_kwargs:
            raise TemplateSyntaxError("The option %s=... cannot be used in '%s'.\nNo keyword arguments are allowed.")

        for name in kwargs:
            if name not in allowed_kwargs:
                raise TemplateSyntaxError("The option %s=... cannot be used in '%s'.\nPossible options are: %s." % (name, bits[0], ", ".join(allowed_kwargs)))

    return tag_name, args, kwargs

Response: Parses the keyword arguments of a tag.


Instruction:
Can you create a Python 3 function that
serializes a tuple into a BIP32 format

Response:
def bip32_serialize(rawtuple):
    """
    Derived from code from pybitcointools (https://github.com/vbuterin/pybitcointools)
    by Vitalik Buterin
    """
    vbytes, depth, fingerprint, i, chaincode, key = rawtuple
    i = encode(i, 256, 4)
    chaincode = encode(hash_to_int(chaincode), 256, 32)
    keydata = b'\x00'  +key[:-1] if vbytes in PRIVATE else key
    bindata = vbytes + from_int_to_byte(depth % 256) + fingerprint + i + chaincode + keydata
    return changebase(bindata + bin_dbl_sha256(bindata)[:4], 256, 58)


Instruction:
Implement a Python 3 function for
deriving from code from pybitcointools (https://github.com/vbuterin/pybitcointools) by Vitalik Buterin

Response:
def bip32_deserialize(data):
    """
    Derived from code from pybitcointools (https://github.com/vbuterin/pybitcointools)
    by Vitalik Buterin
    """
    dbin = changebase(data, 58, 256)
    if bin_dbl_sha256(dbin[:-4])[:4] != dbin[-4:]:
        raise Exception("Invalid checksum")
    vbytes = dbin[0:4]
    depth = from_byte_to_int(dbin[4])
    fingerprint = dbin[5:9]
    i = decode(dbin[9:13], 256)
    chaincode = dbin[13:45]
    key = dbin[46:78]+b'\x01' if vbytes in PRIVATE else dbin[45:78]
    return (vbytes, depth, fingerprint, i, chaincode, key)


Instruction:
Can you generate the documentation for the following Python 3 function
def fetch_table_names(self, include_system_table=False):

        result = self.__cur.execute("SELECT name FROM sqlite_master WHERE TYPE='table'")
        if result is None:
            return []

        table_names = [record[0] for record in result.fetchall()]

        if include_system_table:
            return table_names

        return [table for table in table_names if table not in SQLITE_SYSTEM_TABLES]

Response: Returns a list of table names in the database.


Instruction:
Create a Python 3 function to
fetch sqlite_master table information as a list of dictionaries.

Response:
def fetch_sqlite_master(self):
        """
        Get sqlite_master table information as a list of dictionaries.

        :return: sqlite_master table information.
        :rtype: list

        :Sample Code:
            .. code:: python

                from sqliteschema import SQLiteSchemaExtractor

                print(json.dumps(SQLiteSchemaExtractor("sample.sqlite").fetch_sqlite_master(), indent=4))

        :Output:
            .. code-block:: json

                [
                    {
                        "tbl_name": "sample_table",
                        "sql": "CREATE TABLE 'sample_table' ('a' INTEGER, 'b' REAL, 'c' TEXT, 'd' REAL, 'e' TEXT)",
                        "type": "table",
                        "name": "sample_table",
                        "rootpage": 2
                    },
                    {
                        "tbl_name": "sample_table",
                        "sql": "CREATE INDEX sample_table_a_index ON sample_table('a')",
                        "type": "index",
                        "name": "sample_table_a_index",
                        "rootpage": 3
                    }
                ]
        """

        sqlite_master_record_list = []
        result = self.__cur.execute(
            "SELECT {:s} FROM sqlite_master".format(", ".join(self._SQLITE_MASTER_ATTR_NAME_LIST))
        )

        for record in result.fetchall():
            sqlite_master_record_list.append(
                dict(
                    [
                        [attr_name, item]
                        for attr_name, item in zip(self._SQLITE_MASTER_ATTR_NAME_LIST, record)
                    ]
                )
            )

        return sqlite_master_record_list


Instruction:
How would you implement a function in Python 3 that
yields each node of object graph in postorder.

Response:
def object_iter(obj, parent=None, parent_key=None, idx=None,
                siblings=None):
    """Yields each node of object graph in postorder."""

    obj_node = Node(value=obj, parent=parent, parent_key=parent_key,
                siblings=siblings, idx=idx)

    if isinstance(obj, list):
        _siblings = len(obj)
        for i, elem in enumerate(obj):
            for node in object_iter(elem, obj_node, None, i + 1, _siblings):
                yield node
    elif isinstance(obj, collections.Mapping):
        for key in obj:
            for node in object_iter(obj[key], obj_node, key):
                yield node
    yield obj_node


Instruction:
Explain what the following Python 3 code does
def select(selector, obj):

    parser = Parser(obj)
    try:
        return parser.parse(selector)
    except SelectorSyntaxError as e:
        log.exception(e)
        return False

Response: Appy selector to obj and return matching nodes.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse(self, selector):
        log.debug(self.obj)
        tokens = lex(selector)

        if self.peek(tokens, 'operator') == '*':
            self.match(tokens, 'operator')
            results = list(object_iter(self.obj))
        else:
            results = self.selector_production(tokens)

        results = [node.value for node in results]
        # single results should be returned as a primitive
        if len(results) == 1:
            return results[0]
        elif not len(results):
            return None
        return results

Response: Accept a list of tokens. Returns matched nodes of self. obj.


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

        validators = []
        # the following productions should return predicate functions.

        if self.peek(tokens, 'type'):
            type_ = self.match(tokens, 'type')
            validators.append(self.type_production(type_))

        if self.peek(tokens, 'identifier'):
            key = self.match(tokens, 'identifier')
            validators.append(self.key_production(key))

        if self.peek(tokens, 'pclass'):
            pclass = self.match(tokens, 'pclass')
            validators.append(self.pclass_production(pclass))

        if self.peek(tokens, 'nth_func'):
            nth_func = self.match(tokens, 'nth_func')
            validators.append(self.nth_child_production(nth_func, tokens))

        if self.peek(tokens, 'pclass_func'):
            pclass_func = self.match(tokens, 'pclass_func')
            validators.append(self.pclass_func_production(pclass_func, tokens))

        if not len(validators):
            raise SelectorSyntaxError('no selector recognized.')

        # apply validators from a selector expression to self.obj
        results = self._match_nodes(validators, self.obj)

        if self.peek(tokens, 'operator'):
            operator = self.match(tokens, 'operator')
            rvals = self.selector_production(tokens)
            if operator == ',':
                results.extend(rvals)
            elif operator == '>':
                results = self.parents(results, rvals)
            elif operator == '~':
                results = self.siblings(results, rvals)
            elif operator == ' ':
                results = self.ancestors(results, rvals)
            else:
                raise SelectorSyntaxError("unrecognized operator '%s'"
                                          % operator)
        else:
            if len(tokens):
                rvals = self.selector_production(tokens)
                results = self.ancestors(results, rvals)

        return results

Response: Production for a full selector.


Instruction:
Explain what the following Python 3 code does
def parents(self, lhs, rhs):

        return [node for node in rhs if node.parent in lhs]

Response: Find nodes in rhs which have parents in lhs.


Instruction:
How would you code a function in Python 3 to
return nodes from rhs which have ancestors in lhs.

Response:
def ancestors(self, lhs, rhs):
        """Return nodes from rhs which have ancestors in lhs."""

        def _search(node):
            if node in lhs:
                return True
            if not node.parent:
                return False
            return _search(node.parent)

        return [node for node in rhs if _search(node)]


Instruction:
Can you write a function in Python 3 where it
finds nodes in lhs having common parents in rhs.

Response:
def siblings(self, lhs, rhs):
        """Find nodes in rhs having common parents in lhs."""
        parents = [node.parent for node in lhs]

        return [node for node in rhs if node.parent in parents]


Instruction:
Implement a Python 3 function for
parsing nth - child production.

Response:
def nth_child_production(self, lexeme, tokens):
        """Parse args and pass them to pclass_func_validator."""

        args = self.match(tokens, 'expr')

        pat = self.nth_child_pat.match(args)

        if pat.group(5):
            a = 2
            b = 1 if pat.group(5) == 'odd' else 0
        elif pat.group(6):
            a = 0
            b = int(pat.group(6))
        else:
            sign = pat.group(1) if pat.group(1) else '+'
            coef = pat.group(2) if pat.group(2) else '1'
            a = eval(sign + coef)
            b = eval(pat.group(3) + pat.group(4)) if pat.group(3) else 0

        reverse = False
        if lexeme == 'nth-last-child':
            reverse = True

        def validate(node):
            """This crazy function taken from jsonselect.js:444."""

            if not node.siblings:
                return False

            idx = node.idx - 1
            tot = node.siblings

            if reverse:
                idx = tot - idx
            else:
                idx += 1

            if a == 0:
                m = b == idx
            else:
                mod = (idx - b) % a
                m = not mod and (idx * a + b) >= 0
            return m

        return validate


Instruction:
Explain what the following Python 3 code does
def _match_nodes(self, validators, obj):

        results = []
        for node in object_iter(obj):
            if all([validate(node) for validate in validators]):
                results.append(node)
        return results

Response:
Apply each validator in validators to each node in obj.

        Return each node in obj which matches all validators.


Instruction:
Make a summary of the following Python 3 code
def ping(dst, count, inter=0.2, maxwait=1000, size=64):
    def _then(result, p):
        p.stopListening()
        return result

    d = defer.Deferred()
    p = ICMPPort(0, ICMPPing(d, dst, count, inter, maxwait, size), "", 8192, reactor)
    p.startListening()

    return d.addCallback(_then, p)

Response: Sends ICMP echo requests to destination dst count times.


Instruction:
Can you implement a function in Python 3 that
makes an HTTP request and return the body

Response:
def getBody(self, url, method='GET', headers={}, data=None, socket=None):
        """Make an HTTP request and return the body
        """

        if not 'User-Agent' in headers:
            headers['User-Agent'] = ['Tensor HTTP checker']

        return self.request(url, method, headers, data, socket)


Instruction:
How would you code a function in Python 3 to
expire any items in the cache older than age seconds

Response:
def expire(self, age):
        """Expire any items in the cache older than `age` seconds"""
        now = time.time()
        cache = self._acquire_cache()

        expired = [k for k, v in cache.items() if (now - v[0]) > age]

        for k in expired:
            if k in cache:
                del cache[k]
            if k in self.store:
                del self.store[k]

        self._write_cache(cache)


Instruction:
Write a Python 3 function that can
set a key k to value v

Response:
def set(self, k, v):
        """Set a key `k` to value `v`"""
        self.store[k] = (time.time(), v)
        self._persist()


Instruction:
Write a Python 3 script to
return key contents and modify time

Response:
def get(self, k):
        """Returns key contents, and modify time"""
        if self._changed():
            self._read()

        if k in self.store:
            return tuple(self.store[k])
        else:
            return None


Instruction:
Can you tell what is the following Python 3 function doing
def contains(self, k):
        if self._changed():
            self._read()
        return k in self.store.keys()

Response: Return True if key k exists


Instruction:
Create a Python 3 function for
verifying the chain integrity.

Response:
def chain_check(cls, timestamp: int) -> bool:
        """
        Given a record timestamp, verify the chain integrity.

        :param timestamp: UNIX time / POSIX time / Epoch time
        :return: 'True' if the timestamp fits the chain. 'False' otherwise.
        """

        # Creation is messy.
        # You want genius, you get madness; two sides of the same coin.
        # ... I'm sure this can be cleaned up. However, let's test it first.

        record = cls.get_record(timestamp)

        if isinstance(record, NistBeaconValue) is False:
            # Don't you dare try to play me
            return False

        prev_record = cls.get_previous(record.timestamp)
        next_record = cls.get_next(record.timestamp)

        if prev_record is None and next_record is None:
            # Uh, how did you manage to do this?
            # I'm not even mad, that's amazing.
            return False

        if (
                isinstance(prev_record, NistBeaconValue) and
                isinstance(next_record, NistBeaconValue)
        ):
            # Majority case, somewhere in the middle of the chain
            # True if:
            #   - All three records have proper signatures
            #   - The requested record's previous output equals previous
            #   - The next possible record's previous output equals the record
            return (
                record.valid_signature and
                prev_record.valid_signature and
                next_record.valid_signature and
                record.previous_output_value == prev_record.output_value and
                next_record.previous_output_value == record.output_value
            )

        if (
                prev_record is None and
                isinstance(next_record, NistBeaconValue)
        ):
            # Edge case, this was potentially the first record of all time
            return (
                record.valid_signature and
                next_record.valid_signature and
                cls._INIT_RECORD == record and
                next_record.previous_output_value == record.output_value
            )

        if (
                isinstance(prev_record, NistBeaconValue) and
                next_record is None
        ):
            # Edge case, this was potentially the latest and greatest
            return (
                record.valid_signature and
                prev_record.valid_signature and
                record.previous_output_value == prev_record.output_value
            )


Instruction:
Explain what the following Python 3 code does
def get_first_record(
            cls,
            download: bool=True
    ) -> NistBeaconValue:
        """
        Get the first (oldest) record available. Since the first record
        IS a known value in the system we can load it from constants.

        :param download: 'True' will always reach out to NIST to get the
                         first record. 'False' returns a local copy.
        :return: The first beacon value. 'None' otherwise.
        """

        if download:
            return NistBeacon.get_record(cls._INIT_RECORD.timestamp)
        else:
            return NistBeaconValue.from_json(cls._INIT_RECORD.json)

Response: Get the first record available.


Instruction:
Implement a function in Python 3 to
convert a string of JSON which represents a NIST randomness beacon value into a Nist RandomnessBeaconValue object.

Response:
def from_json(cls, input_json: str) -> 'NistBeaconValue':
        """
        Convert a string of JSON which represents a NIST randomness beacon
        value into a 'NistBeaconValue' object.

        :param input_json: JSON to build a 'Nist RandomnessBeaconValue' from
        :return: A 'NistBeaconValue' object, 'None' otherwise
        """

        try:
            data_dict = json.loads(input_json)
        except ValueError:
            return None

        # Our required values are "must haves". This makes it simple
        # to verify we loaded everything out of JSON correctly.
        required_values = {
            cls._KEY_FREQUENCY: None,
            cls._KEY_OUTPUT_VALUE: None,
            cls._KEY_PREVIOUS_OUTPUT_VALUE: None,
            cls._KEY_SEED_VALUE: None,
            cls._KEY_SIGNATURE_VALUE: None,
            cls._KEY_STATUS_CODE: None,
            cls._KEY_TIMESTAMP: None,
            cls._KEY_VERSION: None,
        }

        for key in required_values:
            if key in data_dict:
                required_values[key] = data_dict[key]

        # Confirm that the required values are set, and not 'None'
        if None in required_values.values():
            return None

        # We have all the required values, return a node object
        return cls(
            version=required_values[cls._KEY_VERSION],
            frequency=int(required_values[cls._KEY_FREQUENCY]),
            timestamp=int(required_values[cls._KEY_TIMESTAMP]),
            seed_value=required_values[cls._KEY_SEED_VALUE],
            previous_output_value=required_values[
                cls._KEY_PREVIOUS_OUTPUT_VALUE
            ],
            signature_value=required_values[cls._KEY_SIGNATURE_VALUE],
            output_value=required_values[cls._KEY_OUTPUT_VALUE],
            status_code=required_values[cls._KEY_STATUS_CODE],
        )


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_xml(cls, input_xml: str) -> 'NistBeaconValue':

        invalid_result = None

        understood_namespaces = {
            'nist-0.1': 'http://beacon.nist.gov/record/0.1/',
        }

        # Our required values are "must haves". This makes it simple
        # to verify we loaded everything out of XML correctly.
        required_values = {
            cls._KEY_FREQUENCY: None,
            cls._KEY_OUTPUT_VALUE: None,
            cls._KEY_PREVIOUS_OUTPUT_VALUE: None,
            cls._KEY_SEED_VALUE: None,
            cls._KEY_SIGNATURE_VALUE: None,
            cls._KEY_STATUS_CODE: None,
            cls._KEY_TIMESTAMP: None,
            cls._KEY_VERSION: None,
        }

        # First attempt to load the xml, return 'None' on ParseError
        try:
            tree = ElementTree.ElementTree(ElementTree.fromstring(input_xml))
        except ElementTree.ParseError:
            return invalid_result

        # Using the required values, let's load the xml values in
        for key in required_values:
            discovered_element = tree.find(
                "{0}:{1}".format('nist-0.1', key),
                namespaces=understood_namespaces,
            )

            if not isinstance(discovered_element, ElementTree.Element):
                continue

            # Bad pylint message - https://github.com/PyCQA/pylint/issues/476
            # pylint: disable=no-member
            required_values[key] = discovered_element.text

        # Confirm that the required values are set, and not 'None'
        if None in required_values.values():
            return invalid_result

        # We have all the required values, return a node object
        return cls(
            version=required_values[cls._KEY_VERSION],
            frequency=int(required_values[cls._KEY_FREQUENCY]),
            timestamp=int(required_values[cls._KEY_TIMESTAMP]),
            seed_value=required_values[cls._KEY_SEED_VALUE],
            previous_output_value=required_values[
                cls._KEY_PREVIOUS_OUTPUT_VALUE
            ],
            signature_value=required_values[cls._KEY_SIGNATURE_VALUE],
            output_value=required_values[cls._KEY_OUTPUT_VALUE],
            status_code=required_values[cls._KEY_STATUS_CODE],
        )

Response:
Convert a string of XML which represents a NIST Randomness Beacon value
            into a NistBeaconValue object.


Instruction:
Create a Python 3 function to
return a minified version of the javascript content

Response:
def rendered_content(self):
        """Returns a 'minified' version of the javascript content"""
        template = self.resolve_template(self.template_name)
        if django.VERSION[1] < 8:
            if template.name.endswith('.min'):
                return super(MinifiedJsTemplateResponse, self).rendered_content
        else:
            if template.template.name.endswith('.min'):
                return super(MinifiedJsTemplateResponse, self).rendered_content
        # if no minified template exists, minify the response
        content = super(MinifiedJsTemplateResponse, self).rendered_content
        content = jsmin.jsmin(content)
        return content


Instruction:
Given the following Python 3 function, write the documentation
def get_fn(self, fn, max_lines=None):
        stat = os.stat(self.logfile)

        if (stat.st_ino == self.lastInode) and (stat.st_size == self.lastSize):
            # Nothing new
            return []

        # Handle rollover and rotations vaguely
        if (stat.st_ino != self.lastInode) or (stat.st_size < self.lastSize):
            self.lastSize = 0

        fi = open(self.logfile, 'rt')
        fi.seek(self.lastSize)

        self.lastInode = stat.st_ino

        lines = 0

        for i in fi:
            lines += 1
            if max_lines and (lines > max_lines):
                self.storeLast()
                fi.close()
                return

            if '\n' in i:
                self.lastSize += len(i)
                if self.parser:
                    line = self.parser(i.strip('\n'))
                else:
                    line = i.strip('\n')

                fn(line)

        self.storeLast()

        fi.close()

Response: Passes each parsed log line to fn


Instruction:
Write a Python 3 script to
return a big list of all log lines since the last run

Response:
def get(self, max_lines=None):
        """Returns a big list of all log lines since the last run
        """
        rows = []

        self.get_fn(lambda row: rows.append(row), max_lines=max_lines)

        return rows


Instruction:
Make a summary of the following Python 3 code
def create_token(self, obj_id, extra_data):
        return self.dumps(
            dict(
                id=obj_id,
                data=extra_data,
                rnd=binascii.hexlify(os.urandom(4)).decode('utf-8')
            )
        )

Response: Create a token referencing the object id with extra data.


Instruction:
Given the following Python 3 function, write the documentation
def validate_token(self, token, expected_data=None):
        try:
            # Load token and remove random data.
            data = self.load_token(token)

            # Compare expected data with data in token.
            if expected_data:
                for k in expected_data:
                    if expected_data[k] != data["data"].get(k):
                        return None
            return data
        except BadData:
            return None

Response: Validate secret link token.


Instruction:
Can you create a Python 3 function that
loads data in a token.

Response:
def load_token(self, token, force=False):
        """Load data in a token.

        :param token: Token to load.
        :param force: Load token data even if signature expired.
                      Default: False.
        """
        try:
            data = self.loads(token)
        except SignatureExpired as e:
            if not force:
                raise
            data = e.payload

        del data["rnd"]
        return data


Instruction:
Here you have a function in Python 3, explain what it does
def create_token(self, obj_id, extra_data):
        return self.engine.encrypt(
            super(EncryptedTokenMixIn, self).create_token(obj_id, extra_data)
        )

Response: Create a token referencing the object id with extra data.


Instruction:
Can you implement a function in Python 3 that
loads data in a token.

Response:
def load_token(self, token, force=False):
        """Load data in a token.

        :param token: Token to load.
        :param force: Load token data even if signature expired.
                      Default: False.
        """
        return super(EncryptedTokenMixIn, self).load_token(
            self.engine.decrypt(token), force=force
        )


Instruction:
Create a Python 3 function to
create the secret link token.

Response:
def create_token(cls, obj_id, data, expires_at=None):
        """Create the secret link token."""
        if expires_at:
            s = TimedSecretLinkSerializer(expires_at=expires_at)
        else:
            s = SecretLinkSerializer()

        return s.create_token(obj_id, data)


Instruction:
Make a summary of the following Python 3 code
def validate_token(cls, token, expected_data=None):
        for algorithm in SUPPORTED_DIGEST_ALGORITHMS:
            s = SecretLinkSerializer(algorithm_name=algorithm)
            st = TimedSecretLinkSerializer(algorithm_name=algorithm)

            try:
                for serializer in (s, st):
                    data = serializer.validate_token(
                        token, expected_data=expected_data)
                    if data:
                        return data
            except SignatureExpired:  # move to next algorithm
                raise
            except BadData:
                continue

Response: Validate a secret link token.


Instruction:
Can you write a function in Python 3 where it
validates a secret link token.

Response:
def load_token(cls, token, force=False):
        """Validate a secret link token (non-expiring + expiring)."""
        for algorithm in SUPPORTED_DIGEST_ALGORITHMS:
            s = SecretLinkSerializer(algorithm_name=algorithm)
            st = TimedSecretLinkSerializer(algorithm_name=algorithm)
            for serializer in (s, st):
                try:
                    data = serializer.load_token(token, force=force)
                    if data:
                        return data
                except SignatureExpired:
                    raise  # signature was parsed and is expired
                except BadData:
                    continue


Instruction:
Can you generate a brief explanation for the following Python 3 code
def Counter32(a, b, delta):
    if b < a:
        c = 4294967295 - a
        return (c + b) / float(delta)

    return (b - a) / float(delta)

Response: 32bit counter aggregator with wrapping


Instruction:
Can you generate a brief explanation for the following Python 3 code
def Counter(a, b, delta):
    if b < a:
        return None

    return (b - a) / float(delta)

Response: Calculate the ratio of two items in a counter.


Instruction:
How would you code a function in Python 3 to
set up sources from the given config

Response:
def setupSources(self, config):
        """Sets up source objects from the given config"""
        sources = config.get('sources', [])

        for source in sources:
            src = self.createSource(source)
            self.setupTriggers(source, src)

            self.sources.append(src)


Instruction:
Here you have a function in Python 3, explain what it does
def _parse_format(self, format):
        format = format.strip()
        format = re.sub('[ \t]+',' ',format)

        subpatterns = []

        findquotes = re.compile(r'^\\"')
        findreferreragent = re.compile('Referer|User-Agent')
        findpercent = re.compile('^%.*t$')
        lstripquotes = re.compile(r'^\\"')
        rstripquotes = re.compile(r'\\"$')
        header = re.compile(r'.*%\{([^\}]+)\}i')

        for element in format.split(' '):

            hasquotes = 0
            if findquotes.search(element): hasquotes = 1

            if hasquotes:
                element = lstripquotes.sub('', element)
                element = rstripquotes.sub('', element)

            head = header.match(element)
            if head:
                self._names.append(head.groups()[0].lower())
                self._types.append(str)
            else:
                self._names.append(self.alias(element))
                self._types.append(self.types.get(element, [None, str])[1])

            subpattern = '(\S*)'

            if hasquotes:
                if element == '%r' or findreferreragent.search(element):
                    subpattern = r'\"([^"\\]*(?:\\.[^"\\]*)*)\"'
                else:
                    subpattern = r'\"([^\"]*)\"'

            elif findpercent.search(element):
                subpattern = r'(\[[^\]]+\])'

            elif element == '%U':
                subpattern = '(.+?)'

            subpatterns.append(subpattern)

        self._pattern = '^' + ' '.join(subpatterns) + '$'
        try:
            self._regex = re.compile(self._pattern)
        except Exception as e:
            raise ApacheLogParserError(e)

Response: Converts the input format to a regular expression and returns the fields that are extracted from the internal state.


Instruction:
Write a Python 3 script for
parsing a single line from the log file and returns a dictionary of the contents of it s contents. Raises an exception if it couldn t parse the line.

Response:
def parse(self, line):
        """
        Parses a single line from the log file and returns
        a dictionary of it's contents.

        Raises and exception if it couldn't parse the line
        """
        line = line.strip()
        match = self._regex.match(line)

        if match:
            data = {}
            for i, e in enumerate(match.groups()):
                if e == "-":
                    k, v = self._names[i], None
                else:
                    k, v = self._names[i], self._types[i](e)
                data[k] = v
            return data

        raise ApacheLogParserError("Unable to parse: %s" % line)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate_expires_at(form, field):
    if form.accept.data:
        if not field.data or datetime.utcnow().date() >= field.data:
            raise validators.StopValidation(_(
                "Please provide a future date."
            ))
        if not field.data or \
                datetime.utcnow().date() + timedelta(days=365) < field.data:
            raise validators.StopValidation(_(
                "Please provide a date no more than 1 year into the future."
            ))

Response: Validate that the date is in the future.


Instruction:
How would you explain what the following Python 3 function does
def validate_accept(form, field):
        if field.data and form.reject.data:
            raise validators.ValidationError(
                _("Both reject and accept cannot be set at the same time.")
            )

Response: Validate that reject and accept are not set at the same time.


Instruction:
Here you have a function in Python 3, explain what it does
def validate_reject(form, field):
        if field.data and form.accept.data:
            raise validators.ValidationError(
                _("Both reject and accept cannot be set at the same time.")
            )

Response: Validate that reject and accept are not set at the same time.


Instruction:
Implement a function in Python 3 to
verify token and save in session if it s valid.

Response:
def verify_token():
    """Verify token and save in session if it's valid."""
    try:
        from .models import SecretLink
        token = request.args['token']
        # if the token is valid
        if token and SecretLink.validate_token(token, {}):
            # then save in session the token
            session['accessrequests-secret-token'] = token
    except KeyError:
        pass


Instruction:
Explain what the following Python 3 code does
def name(self):
        if (
            self.device_type and
            self.device_type.code in (DeviceType.MOBILE, DeviceType.TABLET)
        ):
            return self.device
        else:
            return self.browser

Response: Return a basic meaningful name based on device type and browser type.


Instruction:
Can you create a Python 3 function that
does not warn on external images.

Response:
def _warn_node(self, msg, *args, **kwargs):
    """Do not warn on external images."""
    if not msg.startswith('nonlocal image URI found:'):
        _warn_node_old(self, msg, *args, **kwargs)


Instruction:
Implement a function in Python 3 to
connect receivers to signals.

Response:
def connect_receivers():
    """Connect receivers to signals."""
    request_created.connect(send_email_validation)
    request_confirmed.connect(send_confirmed_notifications)
    request_rejected.connect(send_reject_notification)
    # Order is important:
    request_accepted.connect(create_secret_link)
    request_accepted.connect(send_accept_notification)


Instruction:
Explain what the following Python 3 code does
def create_secret_link(request, message=None, expires_at=None):
    pid, record = get_record(request.recid)
    if not record:
        raise RecordNotFound(request.recid)

    description = render_template(
        "zenodo_accessrequests/link_description.tpl",
        request=request,
        record=record,
        pid=pid,
        expires_at=expires_at,
        message=message,
    )

    request.create_secret_link(
        record["title"],
        description=description,
        expires_at=expires_at
    )

Response: Receiver for request - accepted signal.


Instruction:
Given the following Python 3 function, write the documentation
def send_confirmed_notifications(request):
    pid, record = get_record(request.recid)
    if record is None:
        current_app.logger.error("Cannot retrieve record %s. Emails not sent"
                                 % request.recid)
        return
    title = _("Access request: %(record)s", record=record["title"])

    _send_notification(
        request.receiver.email,
        title,
        "zenodo_accessrequests/emails/new_request.tpl",
        request=request,
        record=record,
        pid=pid,
    )

    _send_notification(
        request.sender_email,
        title,
        "zenodo_accessrequests/emails/confirmation.tpl",
        request=request,
        record=record,
        pid=pid,
    )

Response: Receiver for request - confirmed signal to send email notification.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def send_reject_notification(request, message=None):
    pid, record = get_record(request.recid)
    _send_notification(
        request.sender_email,
        _("Access request rejected"),
        "zenodo_accessrequests/emails/rejected.tpl",
        request=request,
        record=record,
        pid=pid,
        message=message,
    )

Response: Receiver for request - rejected signal to send email notification.


Instruction:
Write a Python 3 function that can
render a template and send as email.

Response:
def _send_notification(to, subject, template, **ctx):
    """Render a template and send as email."""
    msg = Message(
        subject,
        sender=current_app.config.get('SUPPORT_EMAIL'),
        recipients=[to]
    )
    msg.body = render_template(template, **ctx)

    send_email.delay(msg.__dict__)


Instruction:
Write a Python 3 function that can
create a new secret link.

Response:
def create(cls, title, owner, extra_data, description="", expires_at=None):
        """Create a new secret link."""
        if isinstance(expires_at, date):
            expires_at = datetime.combine(expires_at, datetime.min.time())

        with db.session.begin_nested():
            obj = cls(
                owner=owner,
                title=title,
                description=description,
                expires_at=expires_at,
                token='',
            )
            db.session.add(obj)

        with db.session.begin_nested():
            # Create token (dependent on obj.id and recid)
            obj.token = SecretLinkFactory.create_token(
                obj.id, extra_data, expires_at=expires_at
            ).decode('utf8')

        link_created.send(obj)
        return obj


Instruction:
Implement a function in Python 3 to
validate a secret link token.

Response:
def validate_token(cls, token, expected_data):
        """Validate a secret link token.

        Only queries the database if token is valid to determine that the token
        has not been revoked.
        """
        data = SecretLinkFactory.validate_token(
            token, expected_data=expected_data
        )

        if data:
            link = cls.query.get(data['id'])
            if link and link.is_valid():
                return True
        return False


Instruction:
Create a Python 3 function for
loading extra data stored in token.

Response:
def extra_data(self):
        """Load token data stored in token (ignores expiry date of tokens)."""
        if self.token:
            return SecretLinkFactory.load_token(self.token, force=True)["data"]
        return None


Instruction:
Can you write a function in Python 3 where it
gets absolute url for the given endpoint.

Response:
def get_absolute_url(self, endpoint):
        """Get absolute for secret link (using https scheme).

        The endpoint is passed to ``url_for`` with ``token`` and ``extra_data``
        as keyword arguments. E.g.::

            >>> link.extra_data
            dict(recid=1)
            >>> link.get_absolute_url('record.metadata')

        translates into::

            >>> url_for('record.metadata', token="...", recid=1, )
        """
        copy = deepcopy(self.extra_data)
        if 'recid' in copy:
            copy['pid_value'] = copy.pop('recid')
        return url_for(
            endpoint, token=self.token,
            _external=True, **(copy or {})
        )


Instruction:
Can you tell what is the following Python 3 function doing
def create(cls, recid=None, receiver=None, sender_full_name=None,
               sender_email=None, justification=None, sender=None):
        """Create a new access request.

        :param recid: Record id (required).
        :param receiver: User object of receiver (required).
        :param sender_full_name: Full name of sender (required).
        :param sender_email: Email address of sender (required).
        :param justification: Justification message (required).
        :param sender: User object of sender (optional).
        """
        sender_user_id = None if sender is None else sender.id

        assert recid
        assert receiver
        assert sender_full_name
        assert sender_email
        assert justification

        # Determine status
        status = RequestStatus.EMAIL_VALIDATION
        if sender and sender.confirmed_at:
            status = RequestStatus.PENDING

        with db.session.begin_nested():
            # Create object
            obj = cls(
                status=status,
                recid=recid,
                receiver_user_id=receiver.id,
                sender_user_id=sender_user_id,
                sender_full_name=sender_full_name,
                sender_email=sender_email,
                justification=justification
            )

            db.session.add(obj)

        # Send signal
        if obj.status == RequestStatus.EMAIL_VALIDATION:
            request_created.send(obj)
        else:
            request_confirmed.send(obj)
        return obj

Response: Create a new access request.