Instruction:
Here you have a function in Python 3, explain what it does
def interactions_iter(self, nbunch=None, t=None):
        seen = {}  # helper dict to keep track of multiply stored interactions
        if nbunch is None:
            nodes_nbrs_succ = self._succ.items()
        else:
            nodes_nbrs_succ = [(n, self._succ[n]) for n in self.nbunch_iter(nbunch)]

        for n, nbrs in nodes_nbrs_succ:
            for nbr in nbrs:
                if t is not None:
                    if nbr not in seen and self.__presence_test(n, nbr, t):
                        yield (n, nbr, {"t": [t]})
                else:
                    if nbr not in seen:
                        yield (nbr, n, self._succ[n][nbr])
                seen[n] = 1

        del seen

Response: Return an iterator over the interaction present in a given snapshot.


Instruction:
Write a Python 3 function that can
return an iterator over the in - interactions present in a given snapshot.

Response:
def in_interactions_iter(self, nbunch=None, t=None):
        """Return an iterator over the in interactions present in a given snapshot.

        Edges are returned as tuples in the order (node, neighbor).

        Parameters
        ----------
        nbunch : iterable container, optional (default= all nodes)
            A container of nodes.  The container will be iterated
            through once.
        t : snapshot id (default=None)
            If None the the method returns an iterator over the edges of the flattened graph.

        Returns
        -------
        edge_iter : iterator
            An iterator of (u,v) tuples of interaction.

        Notes
        -----
        Nodes in nbunch that are not in the graph will be (quietly) ignored.
        For directed graphs this returns the out-interaction.

        Examples
        --------
        >>> G = dn.DynDiGraph()
        >>> G.add_interaction(0,1, 0)
        >>> G.add_interaction(1,2, 0)
        >>> G.add_interaction(2,3,1)
        >>> [e for e in G.in_interactions_iter(t=0)]
        [(0, 1), (1, 2)]
        >>> list(G.in_interactions_iter())
        [(0, 1), (1, 2), (2, 3)]
        """
        if nbunch is None:
            nodes_nbrs_pred = self._pred.items()
        else:
            nodes_nbrs_pred = [(n, self._pred[n]) for n in self.nbunch_iter(nbunch)]

        for n, nbrs in nodes_nbrs_pred:

            for nbr in nbrs:
                if t is not None:
                    if self.__presence_test(nbr, n, t):
                        yield (nbr, n, {"t": [t]})
                else:
                    if nbr in self._pred[n]:
                        yield (nbr, n, self._pred[n][nbr])


Instruction:
Create a Python 3 function to
return an iterator over the out - interactions present in a given snapshot.

Response:
def out_interactions_iter(self, nbunch=None, t=None):
        """Return an iterator over the out interactions present in a given snapshot.

        Edges are returned as tuples
        in the order (node, neighbor).

        Parameters
        ----------
        nbunch : iterable container, optional (default= all nodes)
            A container of nodes.  The container will be iterated
            through once.
        t : snapshot id (default=None)
            If None the the method returns an iterator over the edges of the flattened graph.

        Returns
        -------
        edge_iter : iterator
            An iterator of (u,v) tuples of interaction.

        Notes
        -----
        Nodes in nbunch that are not in the graph will be (quietly) ignored.
        For directed graphs this returns the out-interaction.

        Examples
        --------
        >>> G = dn.DynDiGraph()
        >>> G.add_interaction(0,1, 0)
        >>> G.add_interaction(1,2, 0)
        >>> G.add_interaction(2,3,1)
        >>> [e for e in G.out_interactions_iter(t=0)]
        [(0, 1), (1, 2)]
        >>> list(G.out_interactions_iter())
        [(0, 1), (1, 2), (2, 3)]
        """
        if nbunch is None:
            nodes_nbrs_succ = self._succ.items()
        else:
            nodes_nbrs_succ = [(n, self._succ[n]) for n in self.nbunch_iter(nbunch)]

        for n, nbrs in nodes_nbrs_succ:
            for nbr in nbrs:
                if t is not None:
                    if self.__presence_test(n, nbr, t):
                        yield (n, nbr, {"t": [t]})
                else:
                    if nbr in self._succ[n]:
                        yield (n, nbr, self._succ[n][nbr])


Instruction:
How would you code a function in Python 3 to
return the number of interaction between two nodes at time t.

Response:
def number_of_interactions(self, u=None, v=None, t=None):
        """Return the number of interaction between two nodes at time t.

        Parameters
        ----------
        u, v : nodes, optional (default=all interaction)
            If u and v are specified, return the number of interaction between
            u and v. Otherwise return the total number of all interaction.
        t : snapshot id (default=None)
            If None will be returned the number of edges on the flattened graph.

        Returns
        -------
        nedges : int
            The number of interaction in the graph.  If nodes u and v are specified
            return the number of interaction between those nodes. If a single node is specified return None.

        See Also
        --------
        size

        Examples
        --------
        >>> G = dn.DynDiGraph()
        >>> G.add_path([0,1,2,3], t=0)
        >>> G.number_of_interactions()
        3
        >>> G.number_of_interactions(0,1, t=0)
        1
        >>> G.add_edge(3, 4, t=1)
        >>> G.number_of_interactions()
        4
        """
        if t is None:
            if u is None:
                return int(self.size())
            elif u is not None and v is not None:
                if v in self._succ[u]:
                    return 1
                else:
                    return 0
        else:
            if u is None:
                return int(self.size(t))
            elif u is not None and v is not None:
                if v in self._succ[u]:
                    if self.__presence_test(u, v, t):
                        return 1
                    else:
                        return 0


Instruction:
How would you implement a function in Python 3 that
returns True if the interaction u v is in the graph at time t.

Response:
def has_interaction(self, u, v, t=None):
        """Return True if the interaction (u,v) is in the graph at time t.

        Parameters
        ----------
        u, v : nodes
            Nodes can be, for example, strings or numbers.
            Nodes must be hashable (and not None) Python objects.
        t : snapshot id (default=None)
            If None will be returned the presence of the interaction on the flattened graph.

        Returns
        -------
        edge_ind : bool
            True if interaction is in the graph, False otherwise.

        Examples
        --------
        Can be called either using two nodes u,v or interaction tuple (u,v)

        >>> G = dn.DynDiGraph()
        >>> G.add_interaction(0,1, t=0)
        >>> G.add_interaction(1,2, t=0)
        >>> G.add_interaction(2,3, t=0)
        >>> G.has_interaction(0,1, t=0)
        True
        >>> G.has_interaction(0,1, t=1)
        False
        """
        try:
            if t is None:
                return v in self._succ[u]
            else:
                return v in self._succ[u] and self.__presence_test(u, v, t)
        except KeyError:
            return False


Instruction:
Explain what the following Python 3 code does
def has_successor(self, u, v, t=None):
        return self.has_interaction(u, v, t)

Response: Return True if node u has successor v at time t.


Instruction:
Given the following Python 3 function, write the documentation
def has_predecessor(self, u, v, t=None):
        return self.has_interaction(v, u, t)

Response: Return True if node u has predecessor v at time t.


Instruction:
Make a summary of the following Python 3 code
def successors_iter(self, n, t=None):
        try:
            if t is None:
                return iter(self._succ[n])
            else:
                return iter([i for i in self._succ[n] if self.__presence_test(n, i, t)])
        except KeyError:
            raise nx.NetworkXError("The node %s is not in the graph." % (n,))

Response: Return an iterator over the successors of n at time t.


Instruction:
Given the following Python 3 function, write the documentation
def predecessors_iter(self, n, t=None):
        try:
            if t is None:
                return iter(self._pred[n])
            else:
                return iter([i for i in self._pred[n] if self.__presence_test(i, n, t)])
        except KeyError:
            raise nx.NetworkXError("The node %s is not in the graph." % (n,))

Response: Return an iterator over predecessors nodes of n at time t.


Instruction:
How would you implement a function in Python 3 that
returns the in degree of a node or nodes at time t.

Response:
def in_degree(self, nbunch=None, t=None):
        """Return the in degree of a node or nodes at time t.

        The node in degree is the number of incoming interaction to that node in a given time frame.

        Parameters
        ----------
        nbunch : iterable container, optional (default=all nodes)
            A container of nodes.  The container will be iterated
            through once.

        t : snapshot id (default=None)
            If None will be returned the degree of nodes on the flattened graph.

        Returns
        -------
        nd : dictionary, or number
            A dictionary with nodes as keys and degree as values or
            a number if a single node is specified.

        Examples
        --------
        >>> G = dn.DynDiGraph()
        >>> G.add_interaction(0,1, t=0)
        >>> G.add_interaction(1,2, t=0)
        >>> G.add_interaction(2,3, t=0)
        >>> G.in_degree(0, t=0)
        1
        >>> G.in_degree([0,1], t=1)
        {0: 0, 1: 0}
        >>> list(G.in_degree([0,1], t=0).values())
        [1, 2]
        """
        if nbunch in self:  # return a single node
            return next(self.in_degree_iter(nbunch, t))[1]
        else:  # return a dict
            return dict(self.in_degree_iter(nbunch, t))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def in_degree_iter(self, nbunch=None, t=None):
        if nbunch is None:
            nodes_nbrs = self._pred.items()
        else:
            nodes_nbrs = ((n, self._pred[n]) for n in self.nbunch_iter(nbunch))

        if t is None:
            for n, nbrs in nodes_nbrs:
                deg = len(self._pred[n])
                yield (n, deg)
        else:
            for n, nbrs in nodes_nbrs:
                edges_t = len([v for v in nbrs.keys() if self.__presence_test(v, n, t)])
                if edges_t > 0:
                    yield (n, edges_t)
                else:
                    yield (n, 0)

Response: Return an iterator over the in degree of the node at time t.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def out_degree(self, nbunch=None, t=None):
        if nbunch in self:  # return a single node
            return next(self.out_degree_iter(nbunch, t))[1]
        else:  # return a dict
            return dict(self.out_degree_iter(nbunch, t))

Response: Return the out degree of a node or nodes at time t.


Instruction:
Write a Python 3 function for
returning an iterator over the out degree of a node in a given timeframe.

Response:
def out_degree_iter(self, nbunch=None, t=None):
        """Return an iterator for (node, out_degree) at time t.

        The node out degree is the number of interactions outgoing from the node in a given timeframe.

        Parameters
        ----------
        nbunch : iterable container, optional (default=all nodes)
            A container of nodes.  The container will be iterated
            through once.

        t : snapshot id (default=None)
            If None will be returned an iterator over the degree of nodes on the flattened graph.

        Returns
        -------
        nd_iter : an iterator
            The iterator returns two-tuples of (node, degree).

        See Also
        --------
        degree

        Examples
        --------
        >>> G = dn.DynDiGraph()
        >>> G.add_interaction(0, 1, t=0)
        >>> list(G.out_degree_iter(0, t=0))
        [(0, 1)]
        >>> list(G.out_degree_iter([0,1], t=0))
        [(0, 1)]
        """
        if nbunch is None:
            nodes_nbrs = self._succ.items()
        else:
            nodes_nbrs = ((n, self._succ[n]) for n in self.nbunch_iter(nbunch))

        if t is None:
            for n, nbrs in nodes_nbrs:
                deg = len(self._succ[n])
                yield (n, deg)
        else:
            for n, nbrs in nodes_nbrs:
                edges_t = len([v for v in nbrs.keys() if self.__presence_test(n, v, t)])
                if edges_t > 0:
                    yield (n, edges_t)
                else:
                    yield (n, 0)


Instruction:
Given the following Python 3 function, write the documentation
def to_undirected(self, reciprocal=False):
        from .dyngraph import DynGraph
        H = DynGraph()
        H.name = self.name
        H.add_nodes_from(self)

        if reciprocal is True:
            for u in self._node:
                for v in self._node:
                    if u >= v:
                        try:
                            outc = self._succ[u][v]['t']
                            intc = self._pred[u][v]['t']
                            for o in outc:
                                r = set(range(o[0], o[1]+1))
                                for i in intc:
                                    r2 = set(range(i[0], i[1]+1))
                                    inter = list(r & r2)
                                    if len(inter) == 1:
                                        H.add_interaction(u, v, t=inter[0])
                                    elif len(inter) > 1:
                                        H.add_interaction(u, v, t=inter[0], e=inter[-1])

                        except:
                            pass

        else:
            for it in self.interactions_iter():
                for t in it[2]['t']:
                    H.add_interaction(it[0], it[1], t=t[0], e=t[1])

        H.graph = deepcopy(self.graph)
        H._node = deepcopy(self._node)
        return H

Response: Return an undirected representation of the dynamic graph.


Instruction:
Can you generate the documentation for the following Python 3 function
def write_interactions(G, path, delimiter=' ',  encoding='utf-8'):
    for line in generate_interactions(G, delimiter):
        line += '\n'
        path.write(line.encode(encoding))

Response: Write a DyNetx graph in interaction list format.


Instruction:
Can you create a Python 3 function that
reads a DyNetx file containing a list of interaction lines.

Response:
def read_interactions(path, comments="#", directed=False, delimiter=None,
                  nodetype=None, timestamptype=None, encoding='utf-8', keys=False):
    """Read a DyNetx graph from interaction list format.

        Parameters
        ----------

        path : basestring
            The desired output filename

        delimiter : character
            Column delimiter
    """
    ids = None
    lines = (line.decode(encoding) for line in path)
    if keys:
        ids = read_ids(path.name, delimiter=delimiter, timestamptype=timestamptype)

    return parse_interactions(lines, comments=comments, directed=directed, delimiter=delimiter, nodetype=nodetype,
                             timestamptype=timestamptype, keys=ids)


Instruction:
Write a Python 3 script to
write a DyNetx graph in snapshot graph list format.

Response:
def write_snapshots(G, path, delimiter=' ', encoding='utf-8'):
    """Write a DyNetx graph in snapshot graph list format.

        Parameters
        ----------

        G : graph
            A DyNetx graph.

        path : basestring
            The desired output filename

        delimiter : character
            Column delimiter
        """
    for line in generate_snapshots(G, delimiter):
        line += '\n'
        path.write(line.encode(encoding))


Instruction:
Can you write a function in Python 3 where it
reads a DyNetx file from snapshot graph list format.

Response:
def read_snapshots(path, comments="#", directed=False, delimiter=None,
                   nodetype=None, timestamptype=None, encoding='utf-8', keys=False):
    """Read a DyNetx graph from snapshot graph list format.

        Parameters
        ----------

        path : basestring
            The desired output filename

        delimiter : character
            Column delimiter
    """
    ids = None
    lines = (line.decode(encoding) for line in path)
    if keys:
        ids = read_ids(path.name, delimiter=delimiter, timestamptype=timestamptype)

    return parse_snapshots(lines, comments=comments, directed=directed, delimiter=delimiter,  nodetype=nodetype,
                           timestamptype=timestamptype, keys=ids)


Instruction:
Implement a function in Python 3 to
function to ensure clean opening and closing of files.

Response:
def open_file(path_arg, mode='r'):
    """Decorator to ensure clean opening and closing of files.

    Parameters
    ----------
    path_arg : int
        Location of the path argument in args.  Even if the argument is a
        named positional argument (with a default value), you must specify its
        index as a positional argument.
    mode : str
        String for opening mode.

    Returns
    -------
    _open_file : function
        Function which cleanly executes the io.

    Examples
    --------
    Decorate functions like this::

       @open_file(0,'r')
       def read_function(pathname):
           pass

       @open_file(1,'w')
       def write_function(G,pathname):
           pass

       @open_file(1,'w')
       def write_function(G, pathname='graph.dot')
           pass

       @open_file('path', 'w+')
       def another_function(arg, **kwargs):
           path = kwargs['path']
           pass
    """
    # Note that this decorator solves the problem when a path argument is
    # specified as a string, but it does not handle the situation when the
    # function wants to accept a default of None (and then handle it).
    # Here is an example:
    #
    # @open_file('path')
    # def some_function(arg1, arg2, path=None):
    #    if path is None:
    #        fobj = tempfile.NamedTemporaryFile(delete=False)
    #        close_fobj = True
    #    else:
    #        # `path` could have been a string or file object or something
    #        # similar. In any event, the decorator has given us a file object
    #        # and it will close it for us, if it should.
    #        fobj = path
    #        close_fobj = False
    #
    #    try:
    #        fobj.write('blah')
    #    finally:
    #        if close_fobj:
    #            fobj.close()
    #
    # Normally, we'd want to use "with" to ensure that fobj gets closed.
    # However, recall that the decorator will make `path` a file object for
    # us, and using "with" would undesirably close that file object. Instead,
    # you use a try block, as shown above. When we exit the function, fobj will
    # be closed, if it should be, by the decorator.

    @decorator
    def _open_file(func, *args, **kwargs):

        # Note that since we have used @decorator, *args, and **kwargs have
        # already been resolved to match the function signature of func. This
        # means default values have been propagated. For example,  the function
        # func(x, y, a=1, b=2, **kwargs) if called as func(0,1,b=5,c=10) would
        # have args=(0,1,1,5) and kwargs={'c':10}.

        # First we parse the arguments of the decorator. The path_arg could
        # be an positional argument or a keyword argument.  Even if it is
        try:
            # path_arg is a required positional argument
            # This works precisely because we are using @decorator
            path = args[path_arg]
        except TypeError:
            # path_arg is a keyword argument. It is "required" in the sense
            # that it must exist, according to the decorator specification,
            # It can exist in `kwargs` by a developer specified default value
            # or it could have been explicitly set by the user.
            try:
                path = kwargs[path_arg]
            except KeyError:
                # Could not find the keyword. Thus, no default was specified
                # in the function signature and the user did not provide it.
                msg = 'Missing required keyword argument: {0}'
                raise nx.NetworkXError(msg.format(path_arg))
            else:
                is_kwarg = True
        except IndexError:
            # A "required" argument was missing. This can only happen if
            # the decorator of the function was incorrectly specified.
            # So this probably is not a user error, but a developer error.
            msg = "path_arg of open_file decorator is incorrect"
            raise nx.NetworkXError(msg)
        else:
            is_kwarg = False

        # Now we have the path_arg. There are two types of input to consider:
        #   1) string representing a path that should be opened
        #   2) an already opened file object
        if is_string_like(path):
            ext = splitext(path)[1]
            fobj = _dispatch_dict[ext](path, mode=mode)
            close_fobj = True
        elif hasattr(path, 'read'):
            # path is already a file-like object
            fobj = path
            close_fobj = False
        else:
            # could be None, in which case the algorithm will deal with it
            fobj = path
            close_fobj = False

        # Insert file object into args or kwargs.
        if is_kwarg:
            new_args = args
            kwargs[path_arg] = fobj
        else:
            # args is a tuple, so we must convert to list before modifying it.
            new_args = list(args)
            new_args[path_arg] = fobj

        # Finally, we call the original function, making sure to close the fobj.
        try:
            result = func(*new_args, **kwargs)
        finally:
            if close_fobj:
                fobj.close()

        return result

    return _open_file


Instruction:
How would you implement a function in Python 3 that
returns the number of edges between two nodes at time t.

Response:
def number_of_interactions(G, u=None, v=None, t=None):
    """Return the number of edges between two nodes at time t.

        Parameters
        ----------
        u, v : nodes, optional (default=all edges)
            If u and v are specified, return the number of edges between
            u and v. Otherwise return the total number of all edges.
        t : snapshot id (default=None)
            If None will be returned the number of edges on the flattened graph.

        Returns
        -------
        nedges : int
            The number of edges in the graph.  If nodes u and v are specified
            return the number of edges between those nodes.

        Examples
        --------
            >>> G = dn.DynGraph()
            >>> G.add_path([0,1,2,3], t=0)
            >>> dn.number_of_interactions(G, t=0)
            """
    return G.number_of_interactions(u, v, t)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def density(G, t=None):
    n = number_of_nodes(G, t)
    m = number_of_interactions(G, t)
    if m == 0 or m is None or n <= 1:
        return 0

    d = m / (n * (n - 1))
    if not G.is_directed():
        d *= 2
    return d

Response: r Return the density of a graph at timestamp t.


Instruction:
Make a summary of the following Python 3 code
def degree_histogram(G, t=None):
    counts = Counter(d for n, d in G.degree(t=t).items())
    return [counts.get(i, 0) for i in range(max(counts) + 1)]

Response: Return a list of the frequency of each degree value in a DyNetx graph.


Instruction:
Create a Python 3 function for
modifying a networkx graph to prevent further change by adding or removing nodes or edges.

Response:
def freeze(G):
    """Modify graph to prevent further change by adding or removing nodes or edges.

        Node and edge data can still be modified.

        Parameters
        ----------

            G : graph
                A NetworkX graph

        Notes
        -----
        To "unfreeze" a graph you must make a copy by creating a new graph object.

        See Also
        --------
        is_frozen
        """
    G.add_node = frozen
    G.add_nodes_from = frozen
    G.remove_node = frozen
    G.remove_nodes_from = frozen
    G.add_edge = frozen
    G.add_edges_from = frozen
    G.remove_edge = frozen
    G.remove_edges_from = frozen
    G.clear = frozen
    G.frozen = True
    return G


Instruction:
Can you generate the documentation for the following Python 3 function
def add_star(G, nodes, t, **attr):
    nlist = iter(nodes)
    v = next(nlist)
    edges = ((v, n) for n in nlist)
    G.add_interactions_from(edges, t, **attr)

Response: Adds a star at time t to the DyNetx graph G.


Instruction:
Given the following Python 3 function, write the documentation
def add_path(G, nodes, t, **attr):
    nlist = list(nodes)
    edges = zip(nlist[:-1], nlist[1:])
    G.add_interactions_from(edges, t, **attr)

Response: Adds a path at time t to the DyNetx graph G at time t.


Instruction:
How would you explain what the following Python 3 function does
def create_empty_copy(G, with_data=True):
    H = G.__class__()
    H.add_nodes_from(G.nodes(data=with_data))
    if with_data:
        H.graph.update(G.graph)
    return H

Response: Create an empty copy of the graph G with all of the edges removed.


Instruction:
Make a summary of the following Python 3 code
def set_node_attributes(G, values, name=None):
    # Set node attributes based on type of `values`
    if name is not None:  # `values` must not be a dict of dict
        try:  # `values` is a dict
            for n, v in values.items():
                try:
                    G.node[n][name] = values[n]
                except KeyError:
                    pass
        except AttributeError:  # `values` is a constant
            for n in G:
                G.node[n][name] = values
    else:  # `values` must be dict of dict
        for n, d in values.items():
            try:
                G.node[n].update(d)
            except KeyError:
                pass

Response: Set node attributes from dictionary of nodes and values keyed by node.


Instruction:
Implement a Python 3 function for
getting node attributes from graph

Response:
def get_node_attributes(G, name):
    """Get node attributes from graph

        Parameters
        ----------
        G : DyNetx Graph

        name : string
           Attribute name

        Returns
        -------
        Dictionary of attributes keyed by node.
    """
    return {n: d[name] for n, d in G.node.items() if name in d}


Instruction:
Create a Python 3 function to
return all of the neighbors of a node in the graph at time t.

Response:
def all_neighbors(graph, node, t=None):
    """ Returns all of the neighbors of a node in the graph at time t.

        If the graph is directed returns predecessors as well as successors.

        Parameters
        ----------

        graph : DyNetx graph
            Graph to find neighbors.

        node : node
            The node whose neighbors will be returned.

        t : snapshot id (default=None)
            If None the neighbors are identified on the flattened graph.

        Returns
        -------

        neighbors : iterator
            Iterator of neighbors
    """
    if graph.is_directed():
        values = chain(graph.predecessors(node, t=t), graph.successors(node, t=t))
    else:
        values = graph.neighbors(node, t=t)
    return values


Instruction:
Can you generate a brief explanation for the following Python 3 code
def non_neighbors(graph, node, t=None):
    if graph.is_directed():
        values = chain(graph.predecessors(node, t=t), graph.successors(node, t=t))
    else:
        values = graph.neighbors(node, t=t)

    nbors = set(values) | {node}
    return (nnode for nnode in graph if nnode not in nbors)

Response: Returns the non - neighbors of the node at time t.


Instruction:
Can you generate the documentation for the following Python 3 function
def non_interactions(graph, t=None):
    # if graph.is_directed():
    #    for u in graph:
    #        for v in non_neighbors(graph, u, t):
    #            yield (u, v)
    #else:
    nodes = set(graph)
    while nodes:
        u = nodes.pop()
        for v in nodes - set(graph[u]):
            yield (u, v)

Response: Returns the non - existent edges in the graph at time t.


Instruction:
Here you have a function in Python 3, explain what it does
def getMsg(self):
        '''
            getMsg - Generate a default message based on parameters to FunctionTimedOut exception'

            @return <str> - Message
        '''
        return 'Function %s (args=%s) (kwargs=%s) timed out after %f seconds.\n' %(self.timedOutFunction.__name__, repr(self.timedOutArgs), repr(self.timedOutKwargs), self.timedOutAfter)

Response: Generate a default message based on parameters to FunctionTimedOut exception


Instruction:
Create a Python 3 function for
retrying the timed - out function with same arguments.

Response:
def retry(self, timeout=RETRY_SAME_TIMEOUT):
        '''
            retry - Retry the timed-out function with same arguments.

            @param timeout <float/RETRY_SAME_TIMEOUT/None> Default RETRY_SAME_TIMEOUT

                If RETRY_SAME_TIMEOUT : Will retry the function same args, same timeout
                If a float/int : Will retry the function same args with provided timeout
                If None : Will retry function same args no timeout

            @return - Returnval from function
        '''
        if timeout is None:
            return self.timedOutFunction(*(self.timedOutArgs), **self.timedOutKwargs)

        from .dafunc import func_timeout

        if timeout == RETRY_SAME_TIMEOUT:
            timeout = self.timedOutAfter

        return func_timeout(timeout, self.timedOutFunction, args=self.timedOutArgs, kwargs=self.timedOutKwargs)


Instruction:
Can you implement a function in Python 3 that
runs a function for up to timeout seconds. Returns the result of the function.

Response:
def func_timeout(timeout, func, args=(), kwargs=None):
    '''
        func_timeout - Runs the given function for up to #timeout# seconds.

        Raises any exceptions #func# would raise, returns what #func# would return (unless timeout is exceeded), in which case it raises FunctionTimedOut

        @param timeout <float> - Maximum number of seconds to run #func# before terminating

        @param func <function> - The function to call

        @param args    <tuple> - Any ordered arguments to pass to the function

        @param kwargs  <dict/None> - Keyword arguments to pass to the function.

        @raises - FunctionTimedOut if #timeout# is exceeded, otherwise anything #func# could raise will be raised

        If the timeout is exceeded, FunctionTimedOut will be raised within the context of the called function every two seconds until it terminates,
        but will not block the calling thread (a new thread will be created to perform the join). If possible, you should try/except FunctionTimedOut
        to return cleanly, but in most cases it will 'just work'.

        @return - The return value that #func# gives
    '''

    if not kwargs:
        kwargs = {}
    if not args:
        args = ()

    ret = []
    exception = []
    isStopped = False

    def funcwrap(args2, kwargs2):
        try:
            ret.append( func(*args2, **kwargs2) )
        except FunctionTimedOut:
            # Don't print traceback to stderr if we time out
            pass
        except Exception as e:
            exc_info = sys.exc_info()
            if isStopped is False:
                # Assemble the alternate traceback, excluding this function
                #  from the trace (by going to next frame)
                # Pytohn3 reads native from __traceback__,
                # python2 has a different form for "raise"
                e.__traceback__ = exc_info[2].tb_next
                exception.append( e )

    thread = StoppableThread(target=funcwrap, args=(args, kwargs))
    thread.daemon = True

    thread.start()
    thread.join(timeout)

    stopException = None
    if thread.isAlive():
        isStopped = True

        class FunctionTimedOutTempType(FunctionTimedOut):
            def __init__(self):
                return FunctionTimedOut.__init__(self, '', timeout, func, args, kwargs)

        FunctionTimedOutTemp = type('FunctionTimedOut' + str( hash( "%d_%d_%d_%d" %(id(timeout), id(func), id(args), id(kwargs))) ), FunctionTimedOutTempType.__bases__, dict(FunctionTimedOutTempType.__dict__))

        stopException = FunctionTimedOutTemp
        thread._stopThread(stopException)
        thread.join(min(.1, timeout / 50.0))
        raise FunctionTimedOut('', timeout, func, args, kwargs)
    else:
        # We can still cleanup the thread here..
        # Still give a timeout... just... cuz..
        thread.join(.5)

    if exception:
        raise_exception(exception)

    if ret:
        return ret[0]


Instruction:
Can you implement a function in Python 3 that
creates a new path integral and train it and return the ground - state energy E0 and psi ** 2 for potential V.

Response:
def analyze_theory(V, x0list=[], plot=False):
    """ Extract ground-state energy E0 and psi**2 for potential V. """
    # initialize path integral
    T = 4.
    ndT = 8.         # use larger ndT to reduce discretization error (goes like 1/ndT**2)
    neval = 3e5   # should probably use more evaluations (10x?)
    nitn = 6
    alpha = 0.1     # damp adaptation

    # create integrator and train it (no x0list)
    integrand = PathIntegrand(V=V, T=T, ndT=ndT)
    integ = vegas.Integrator(integrand.region, alpha=alpha)
    integ(integrand, neval=neval, nitn=nitn / 2, alpha=2 * alpha)

    # evaluate path integral with trained integrator and x0list
    integrand = PathIntegrand(V=V, x0list=x0list, T=T, ndT=ndT)
    results = integ(integrand, neval=neval, nitn=nitn, alpha=alpha)
    print(results.summary())
    E0 = -np.log(results['exp(-E0*T)']) / T
    print('Ground-state energy = %s    Q = %.2f\n' % (E0, results.Q))

    if len(x0list) <= 0:
        return E0
    psi2 = results['exp(-E0*T) * psi(x0)**2'] / results['exp(-E0*T)']
    print('%5s  %-12s %-10s' % ('x', 'psi**2', 'sho-exact'))
    print(27 * '-')
    for i, (x0i, psi2i) in enumerate(zip(x0list, psi2)):
        exact = np.exp(- x0i ** 2) / np.sqrt(np.pi) #* np.exp(-T / 2.)
        print(
            "%5.1f  %-12s %-10.5f"
            % (x0i, psi2i, exact)
            )
    if plot:
        plot_results(E0, x0list, psi2, T)
    return E0


Instruction:
How would you implement a function in Python 3 that
stops the thread and return True if it is still alive.

Response:
def _stopThread(self, exception, raiseEvery=2.0):
        '''
            _stopThread - @see StoppableThread.stop
        '''
        if self.isAlive() is False:
            return True

        self._stderr = open(os.devnull, 'w')

        # Create "joining" thread which will raise the provided exception
        #  on a repeat, until the thread stops.
        joinThread = JoinThread(self, exception, repeatEvery=raiseEvery)

        # Try to prevent spurrious prints
        joinThread._stderr = self._stderr
        joinThread.start()
        joinThread._stderr = self._stderr


Instruction:
Make a summary of the following Python 3 code
def run(self):
        '''
            run - The thread main. Will attempt to stop and join the attached thread.
        '''

        # Try to silence default exception printing.
        self.otherThread._Thread__stderr = self._stderr
        if hasattr(self.otherThread, '_Thread__stop'):
            # If py2, call this first to start thread termination cleanly.
            #   Python3 does not need such ( nor does it provide.. )
            self.otherThread._Thread__stop()
        while self.otherThread.isAlive():
            # We loop raising exception incase it's caught hopefully this breaks us far out.
            ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.otherThread.ident), ctypes.py_object(self.exception))
            self.otherThread.join(self.repeatEvery)

        try:
            self._stderr.close()
        except:
            pass

Response: The main thread. Will attempt to stop the attached thread and join the attached thread.


Instruction:
Can you tell what is the following Python 3 function doing
def run(self):
        with open('src/vegas/__init__.py', 'a') as vfile:
            vfile.write("\n__version__ = '%s'\n" % VEGAS_VERSION)
        _build_py.run(self)

Response: Append version number to vegas. py


Instruction:
Can you generate the documentation for the following Python 3 function
def _make_map(self, limit):
        ny = 2000
        y = numpy.random.uniform(0., 1., (ny,1))
        limit = numpy.arctan(limit)
        m = AdaptiveMap([[-limit, limit]], ninc=100)
        theta = numpy.empty(y.shape, float)
        jac = numpy.empty(y.shape[0], float)
        for itn in range(10):
            m.map(y, theta, jac)
            tan_theta = numpy.tan(theta[:, 0])
            x = self.scale * tan_theta
            fx = (tan_theta ** 2 + 1) * numpy.exp(-(x ** 2) / 2.)
            m.add_training_data(y, (jac * fx) ** 2)
            m.adapt(alpha=1.5)
        return numpy.array(m.grid[0])

Response: Make a map that is adapted to the pdf.


Instruction:
Implement a Python 3 function for
returning integrand using the tan mapping.

Response:
def _expval(self, f, nopdf):
        """ Return integrand using the tan mapping. """
        def ff(theta, nopdf=nopdf):
            tan_theta = numpy.tan(theta)
            x = self.scale * tan_theta
            jac = self.scale * (tan_theta ** 2 + 1.)
            if nopdf:
                pdf = jac * self.pdf.pjac[None, :]
            else:
                pdf = jac * numpy.exp(-(x ** 2) / 2.) / numpy.sqrt(2 * numpy.pi)
            dp = self.pdf.x2dpflat(x)
            parg = None
            ans = None
            fparg_is_dict = False
            # iterate through the batch
            for i, (dpi, pdfi) in enumerate(zip(dp, pdf)):
                p = self.pdf.meanflat + dpi
                if parg is None:
                    # first time only
                    if self.pdf.shape is None:
                        parg = _gvar.BufferDict(self.pdf.g, buf=p)
                    else:
                        parg = p.reshape(self.pdf.shape)
                else:
                    if parg.shape is None:
                        parg.buf = p
                    else:
                        parg.flat[:] = p
                fparg = 1. if f is None else f(parg)
                if ans is None:
                    # first time only
                    if hasattr(fparg, 'keys'):
                        fparg_is_dict = True
                        if not isinstance(fparg, _gvar.BufferDict):
                            fparg = _gvar.BufferDict(fparg)
                        ans = _gvar.BufferDict()
                        for k in fparg:
                            ans[k] = numpy.empty(
                                (len(pdf),) + fparg.slice_shape(k)[1], float
                                )
                    else:
                        if numpy.shape(fparg) == ():
                            ans = numpy.empty(len(pdf), float)
                        else:
                            ans = numpy.empty(
                                (len(pdf),) + numpy.shape(fparg), float
                                )
                if fparg_is_dict:
                    prod_pdfi = numpy.prod(pdfi)
                    for k in ans:
                        ans[k][i] = fparg[k]
                        ans[k][i] *= prod_pdfi
                else:
                    if not isinstance(fparg, numpy.ndarray):
                        fparg = numpy.asarray(fparg)
                    ans[i] = fparg * numpy.prod(pdfi)
            return ans
        return ff


Instruction:
Given the following Python 3 function, write the documentation
def get_starting_chunk(filename, length=1024):
    # Ensure we open the file in binary mode
    try:
        with open(filename, 'rb') as f:
            chunk = f.read(length)
            return chunk
    except IOError as e:
        print(e)

Response: Reads the first little chunk of a file.


Instruction:
Given the following Python 3 function, write the documentation
def dump(self, function_name):

        assert isinstance(function_name, str)
        self.stdout.write(function_name)
        self.stdout.write("\n")
        self.stdout.write("-" * len(function_name))
        self.stdout.write("\n\n")

        byte_code = self.interpreter.compiled_functions[function_name]
        self.stdout.write(byte_code.dump())
        self.stdout.write("\n")

Response: Pretty - dump the bytecode for the given function name.


Instruction:
Can you create a Python 3 function that
calls startapp to generate app with custom user model.

Response:
def handle(self, **options):
        """Call "startapp" to generate app with custom user model."""
        template = os.path.dirname(os.path.abspath(__file__)) + "/app_template"
        name = options.pop("name")
        call_command("startapp", name, template=template, **options)


Instruction:
Create a Python 3 function to
create and save a User with the given email and password.

Response:
def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError("The given email must be set")
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user


Instruction:
Can you tell what is the following Python 3 function doing
def nwise(iterable, n):
    iters = itertools.tee(iterable, n)
    iters = (itertools.islice(it, i, None) for i, it in enumerate(iters))
    return itertools.izip(*iters)

Response: Iterate through a sequence with a defined length windown


Instruction:
Given the following Python 3 function, write the documentation
def coordinates(g, nodes):
    c = [g.node[n]['coordinate'] for n in nodes]
    return c

Response: Returns a list of ( lon lat ) pairs from nodes in an osmgraph created with osmgraph.


Instruction:
How would you code a function in Python 3 to
step along a path through a directed graph unless there is an intersection Example graph: Note that edge (1, 2) and (2, 3) are bidirectional, i.e., (2, 1) and (3, 2) are also edges 1 -- 2 -- 3 -->-- 5 -->-- 7 | | ^ v | | 4 6 >>> step(g, 1, 2) 3 >>> step(g, 3, 5) None >>> step(g, 2, 3) 5 >>> step(g, 2, 3, inbound=True) None >>> step(g, 7, 5, 3, backward=True) 3 >>> def f(g, n1, n2, backward): if n2 == 5: return 7 return None >>> step(g, 3, 5, continue_fn=f) 7 Parameters ---------- g : networkx DiGraph n1 : node id in g n2 : node id in g (n1, n2) must be an edge in g inbound : bool (default False) whether incoming edges should be considered backward : bool (default False) whether edges are in reverse order (i.e., point from n2 to n1) continue_fn : callable (optional) if at an intersection, continue_fn is called to indicate how to proceed continue_fn takes the form: f(g, n1, n2, backward) where all arguments are as passed into step. f should return a node id such that f(g, n1, n2, backward) is a successors of n2. f should return None if no way forward. Returns ------- The next node in the path from n1 to n2. Returns None if there are no edges from n2 or multiple edges from n2

Response:
def step(g, n1, n2, inbound=False, backward=False, continue_fn=None):
    """
    Step along a path through a directed graph unless there is an intersection

    Example graph:
    Note that edge (1, 2) and (2, 3) are bidirectional, i.e., (2, 1) and
    (3, 2) are also edges

    1 -- 2 -- 3 -->-- 5 -->-- 7
              |       |
              ^       v
              |       |
              4       6

    >>> step(g, 1, 2)
    3
    >>> step(g, 3, 5)
    None

    >>> step(g, 2, 3)
    5
    >>> step(g, 2, 3, inbound=True)
    None

    >>> step(g, 7, 5, 3, backward=True)
    3

    >>> def f(g, n1, n2, backward):
            if n2 == 5:
                return 7
            return None
    >>> step(g, 3, 5, continue_fn=f)
    7

    Parameters
    ----------
    g : networkx DiGraph
    n1 : node id in g
    n2 : node id in g
        (n1, n2) must be an edge in g
    inbound : bool (default False)
        whether incoming edges should be considered
    backward : bool (default False)
        whether edges are in reverse order (i.e., point from n2 to n1)
    continue_fn : callable (optional)
        if at an intersection, continue_fn is called to indicate how to
        proceed

        continue_fn takes the form:
        f(g, n1, n2, backward) where all arguments are as passed into step.
        f should return a node id such that f(g, n1, n2, backward) is a
        successors of n2. f should return None if no way forward.

    Returns
    -------
    The next node in the path from n1 to n2. Returns None if there
    are no edges from n2 or multiple edges from n2

    """
    forw = g.successors
    back = g.predecessors
    if backward:
        back, forw = forw, back

    nodes = forw(n2)
    if inbound:
        nodes = set(nodes + back(n2))
    candidates = [n for n in nodes if n != n1]

    if len(candidates) == 1:
        result = candidates[0]
    elif continue_fn:
        result = continue_fn(g, n1, n2, backward)
    else:
        result = None
    return result


Instruction:
Write a Python 3 script for
moving a node from n1 to n2 in a graph.

Response:
def move(g, n1, n2, **kwargs):
    """
    Step along a graph until it ends or reach an intersection

    Example graph:
    Note that edge (1, 2) and (2, 3) are bidirectional, i.e., (2, 1) and
    (3, 2) are also edges

    1 -- 2 -- 3 -->-- 5 -->-- 7
              |       |
              ^       v
              |       |
              4       6

    >>> list(move(g, 1, 2))
    [1, 2, 3, 5]  # Stops at 5 because you can get to both 6 and 7 from 3

    >>> step(g, 1, 2, inbound=True)
    [1, 2, 3]

    Parameters
    ----------
    Same as step()

    Yields
    ------
    Node IDs until either there is no path forward or the path reaches
    an intersection

    """
    prev = n1
    curr = n2
    _next = step(g, prev, curr, **kwargs)

    yield prev
    yield curr

    visited_nodes = set([prev, curr])
    while _next:
        yield _next
        if _next in visited_nodes:
            return
        visited_nodes.add(_next)
        prev = curr
        curr = _next
        _next = step(g, prev, curr, **kwargs)


Instruction:
Can you write a function in Python 3 where it
returns True if a node is an intersection of the graph g with n.

Response:
def is_intersection(g, n):
    """
    Determine if a node is an intersection

    graph: 1 -->-- 2 -->-- 3

    >>> is_intersection(g, 2)
    False

    graph:
     1 -- 2 -- 3
          |
          4

    >>> is_intersection(g, 2)
    True

    Parameters
    ----------
    g : networkx DiGraph
    n : node id

    Returns
    -------
    bool

    """
    return len(set(g.predecessors(n) + g.successors(n))) > 2


Instruction:
Can you generate the documentation for the following Python 3 function
def as_dict(self):
        if not self._is_valid:
            self.validate()

        from .converters import to_dict
        return to_dict(self)

Response: Returns the model as a dict


Instruction:
Make a summary of the following Python 3 code
def nodes_callback(self, data):
        for node_id, tags, coords in data:
            # Discard the coords because they go into add_coords
            self.nodes[node_id] = tags

Response: Callback for nodes with tags


Instruction:
Can you tell what is the following Python 3 function doing
def ways_callback(self, data):
        for way_id, tags, nodes in data:
            # Imposm passes all ways through regardless of whether the tags
            # have been filtered or not. It needs to do this in order to
            # handle relations, but we don't care about relations at the
            # moment.
            if tags:
                self.ways[way_id] = (tags, nodes)

Response: Callback for all ways


Instruction:
Make a summary of the following Python 3 code
def get_graph(self, parse_direction=False):
        g = nx.DiGraph()

        for way_id, (tags, nodes) in self.ways.items():
            # If oneway is '-1', reverse the way and treat as a normal oneway
            if tags.get('oneway') == '-1':
                nodes = reversed(nodes)
                tags['oneway'] = 'yes'
            oneway = tags.get('oneway') == 'yes'

            for n0, n1 in tools.pairwise(nodes):
                g.add_edge(n0, n1, attr_dict=tags)
                if parse_direction:
                    g[n0][n1]['_direction'] = 'forward'
                if not oneway:
                    g.add_edge(n1, n0, attr_dict=tags)
                    if parse_direction:
                        g[n1][n0]['_direction'] = 'backward'

                g.node[n0].update(self._node_properties(n0))
            g.node[n1].update(self._node_properties(n1))

        return g

Response: Returns a networkx directed graph of the received data.


Instruction:
Write a Python 3 script for
parsing an OSM file into a networkx graph.

Response:
def parse_file(filename, parse_direction=False, **kwargs):
    """
    Return an OSM networkx graph from the input OSM file

    Only works with OSM xml, xml.bz2 and pbf files. This function cannot take
    OSM QA tile files. Use parse_qa_tile() for QA tiles.

    >>> graph = parse_file(filename)

    """
    importer, parser = make_importer_parser(OSMParser, **kwargs)
    parser.parse(filename)

    return importer.get_graph(parse_direction=parse_direction)


Instruction:
Can you create a Python 3 function that
parses OSM networkx data into a networkx graph.

Response:
def parse_data(data, type, **kwargs):
    """
    Return an OSM networkx graph from the input OSM data

    Parameters
    ----------
    data : string
    type : string ('xml' or 'pbf')

    >>> graph = parse_data(data, 'xml')

    """
    suffixes = {
        'xml': '.osm',
        'pbf': '.pbf',
    }
    try:
        suffix = suffixes[type]
    except KeyError:
        raise ValueError('Unknown data type "%s"' % type)

    fd, filename = tempfile.mkstemp(suffix=suffix)
    try:
        os.write(fd, data)
        os.close(fd)
        return parse_file(filename, **kwargs)
    finally:
        os.remove(filename)


Instruction:
Here you have a function in Python 3, explain what it does
def parse_qa_tile(x, y, zoom, data, parse_direction=False, **kwargs):
    import osmqa
    importer, parser = make_importer_parser(osmqa.QATileParser, **kwargs)
    parser.parse_data(x, y, zoom, data)
    return importer.get_graph(parse_direction=parse_direction)

Response: Parse an OSM networkx graph from the input OSM QA tile data


Instruction:
Can you write a function in Python 3 where it
returns file name without path.

Response:
def _basename(fname):
    """Return file name without path."""
    if not isinstance(fname, Path):
        fname = Path(fname)
    path, name, ext = fname.parent, fname.stem, fname.suffix
    return path, name, ext


Instruction:
How would you code a function in Python 3 to
read Seabird bottle - trip file

Response:
def from_bl(fname):
    """Read Seabird bottle-trip (bl) file

    Example
    -------
    >>> from pathlib import Path
    >>> import ctd
    >>> data_path = Path(__file__).parents[1].joinpath("tests", "data")
    >>> df = ctd.from_bl(str(data_path.joinpath('bl', 'bottletest.bl')))
    >>> df._metadata["time_of_reset"]
    datetime.datetime(2018, 6, 25, 20, 8, 55)

    """
    df = pd.read_csv(
        fname,
        skiprows=2,
        parse_dates=[1],
        index_col=0,
        names=["bottle_number", "time", "startscan", "endscan"],
    )
    df._metadata = {
        "time_of_reset": pd.to_datetime(
            linecache.getline(str(fname), 2)[6:-1]
        ).to_pydatetime()
    }
    return df


Instruction:
Make a summary of the following Python 3 code
def from_btl(fname):
    f = _read_file(fname)
    metadata = _parse_seabird(f.readlines(), ftype="btl")

    f.seek(0)

    df = pd.read_fwf(
        f,
        header=None,
        index_col=False,
        names=metadata["names"],
        parse_dates=False,
        skiprows=metadata["skiprows"],
    )
    f.close()

    # At this point the data frame is not correctly lined up (multiple rows
    # for avg, std, min, max or just avg, std, etc).
    # Also needs date,time,and bottle number to be converted to one per line.

    # Get row types, see what you have: avg, std, min, max or just avg, std.
    rowtypes = df[df.columns[-1]].unique()
    # Get times and dates which occur on second line of each bottle.
    dates = df.iloc[:: len(rowtypes), 1].reset_index(drop=True)
    times = df.iloc[1 :: len(rowtypes), 1].reset_index(drop=True)
    datetimes = dates + " " + times

    # Fill the Date column with datetimes.
    df.loc[:: len(rowtypes), "Date"] = datetimes.values
    df.loc[1 :: len(rowtypes), "Date"] = datetimes.values

    # Fill missing rows.
    df["Bottle"] = df["Bottle"].fillna(method="ffill")
    df["Date"] = df["Date"].fillna(method="ffill")

    df["Statistic"] = df["Statistic"].str.replace(r"\(|\)", "")  # (avg) to avg

    name = _basename(fname)[1]

    dtypes = {
        "bpos": int,
        "pumps": bool,
        "flag": bool,
        "Bottle": int,
        "Scan": int,
        "Statistic": str,
        "Date": str,
    }
    for column in df.columns:
        if column in dtypes:
            df[column] = df[column].astype(dtypes[column])
        else:
            try:
                df[column] = df[column].astype(float)
            except ValueError:
                warnings.warn("Could not convert %s to float." % column)

    df["Date"] = pd.to_datetime(df["Date"])
    metadata["name"] = str(name)
    setattr(df, "_metadata", metadata)
    return df

Response: Reads a Seabird CTD BTL - ASCII format file and returns a DataFrame with the data.


Instruction:
Make a summary of the following Python 3 code
def from_edf(fname):
    f = _read_file(fname)
    header, names = [], []
    for k, line in enumerate(f.readlines()):
        line = line.strip()
        if line.startswith("Serial Number"):
            serial = line.strip().split(":")[1].strip()
        elif line.startswith("Latitude"):
            try:
                hemisphere = line[-1]
                lat = line.strip(hemisphere).split(":")[1].strip()
                lat = np.float_(lat.split())
                if hemisphere == "S":
                    lat = -(lat[0] + lat[1] / 60.0)
                elif hemisphere == "N":
                    lat = lat[0] + lat[1] / 60.0
            except (IndexError, ValueError):
                lat = None
        elif line.startswith("Longitude"):
            try:
                hemisphere = line[-1]
                lon = line.strip(hemisphere).split(":")[1].strip()
                lon = np.float_(lon.split())
                if hemisphere == "W":
                    lon = -(lon[0] + lon[1] / 60.0)
                elif hemisphere == "E":
                    lon = lon[0] + lon[1] / 60.0
            except (IndexError, ValueError):
                lon = None
        else:
            header.append(line)
            if line.startswith("Field"):
                col, unit = [l.strip().lower() for l in line.split(":")]
                names.append(unit.split()[0])
        if line == "// Data":
            skiprows = k + 1
            break

    f.seek(0)
    df = pd.read_csv(
        f,
        header=None,
        index_col=None,
        names=names,
        skiprows=skiprows,
        delim_whitespace=True,
    )
    f.close()

    df.set_index("depth", drop=True, inplace=True)
    df.index.name = "Depth [m]"
    name = _basename(fname)[1]

    metadata = {
        "lon": lon,
        "lat": lat,
        "name": str(name),
        "header": "\n".join(header),
        "serial": serial,
    }
    setattr(df, "_metadata", metadata)
    return df

Response: This function is used to create a ctd object from an EDF file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def from_cnv(fname):
    f = _read_file(fname)
    metadata = _parse_seabird(f.readlines(), ftype="cnv")

    f.seek(0)
    df = pd.read_fwf(
        f,
        header=None,
        index_col=None,
        names=metadata["names"],
        skiprows=metadata["skiprows"],
        delim_whitespace=True,
        widths=[11] * len(metadata["names"]),
    )
    f.close()

    key_set = False
    prkeys = ["prDM", "prdM", "pr"]
    for prkey in prkeys:
        try:
            df.set_index(prkey, drop=True, inplace=True)
            key_set = True
        except KeyError:
            continue
    if not key_set:
        raise KeyError(
            f"Could not find pressure field (supported names are {prkeys})."
        )
    df.index.name = "Pressure [dbar]"

    name = _basename(fname)[1]

    dtypes = {"bpos": int, "pumps": bool, "flag": bool}
    for column in df.columns:
        if column in dtypes:
            df[column] = df[column].astype(dtypes[column])
        else:
            try:
                df[column] = df[column].astype(float)
            except ValueError:
                warnings.warn("Could not convert %s to float." % column)

    metadata["name"] = str(name)
    setattr(df, "_metadata", metadata)
    return df

Response: Reads Seabird CTD CNV - ASCII format and returns a DataFrame with the data in the Seabird CTD structure.


Instruction:
Explain what the following Python 3 code does
def rosette_summary(fname):
    ros = from_cnv(fname)
    ros["pressure"] = ros.index.values.astype(float)
    ros["nbf"] = ros["nbf"].astype(int)
    ros.set_index("nbf", drop=True, inplace=True, verify_integrity=False)
    return ros

Response:
This function creates a SBE
    from a CNV file.


Instruction:
Write a Python 3 function for
extrapolating data to zones where the shallow stations are shadowed by the deep stations.

Response:
def extrap_sec(data, dist, depth, w1=1.0, w2=0):
    """
    Extrapolates `data` to zones where the shallow stations are shadowed by
    the deep stations.  The shadow region usually cannot be extrapolates via
    linear interpolation.

    The extrapolation is applied using the gradients of the `data` at a certain
    level.

    Parameters
    ----------
    data : array_like
          Data to be extrapolated
    dist : array_like
           Stations distance
    fd : float
         Decay factor [0-1]

    Returns
    -------
    Sec_extrap : array_like
                 Extrapolated variable

    """
    from scipy.interpolate import interp1d

    new_data1 = []
    for row in data:
        mask = ~np.isnan(row)
        if mask.any():
            y = row[mask]
            if y.size == 1:
                row = np.repeat(y, len(mask))
            else:
                x = dist[mask]
                f_i = interp1d(x, y)
                f_x = _extrap1d(f_i)
                row = f_x(dist)
        new_data1.append(row)

    new_data2 = []
    for col in data.T:
        mask = ~np.isnan(col)
        if mask.any():
            y = col[mask]
            if y.size == 1:
                col = np.repeat(y, len(mask))
            else:
                z = depth[mask]
                f_i = interp1d(z, y)
                f_z = _extrap1d(f_i)
                col = f_z(depth)
        new_data2.append(col)

    new_data = np.array(new_data1) * w1 + np.array(new_data2).T * w2
    return new_data


Instruction:
Can you tell what is the following Python 3 function doing
def gen_topomask(h, lon, lat, dx=1.0, kind="linear", plot=False):

    import gsw
    from scipy.interpolate import interp1d

    h, lon, lat = list(map(np.asanyarray, (h, lon, lat)))
    # Distance in km.
    x = np.append(0, np.cumsum(gsw.distance(lon, lat)[0] / 1e3))
    h = -gsw.z_from_p(h, lat.mean())
    Ih = interp1d(x, h, kind=kind, bounds_error=False, fill_value=h[-1])
    xm = np.arange(0, x.max() + dx, dx)
    hm = Ih(xm)

    return xm, hm

Response: Generate a topography mask from an oceanographic transect taking the deepest CTD scan as the depth of each station.


Instruction:
Make a summary of the following Python 3 code
def cell_thermal_mass(temperature, conductivity):

    alpha = 0.03  # Thermal anomaly amplitude.
    beta = 1.0 / 7  # Thermal anomaly time constant (1/beta).

    sample_interval = 1 / 15.0
    a = 2 * alpha / (sample_interval * beta + 2)
    b = 1 - (2 * a / alpha)
    dCodT = 0.1 * (1 + 0.006 * [temperature - 20])
    dT = np.diff(temperature)
    ctm = -1.0 * b * conductivity + a * (dCodT) * dT  # [S/m]
    return ctm

Response: Calculates the thermal mass of a cell.


Instruction:
Can you write a function in Python 3 where it
computes the thickness of water separating the mixed surface layer from the thermocline.

Response:
def barrier_layer_thickness(SA, CT):
    """
    Compute the thickness of water separating the mixed surface layer from the
    thermocline.  A more precise definition would be the difference between
    mixed layer depth (MLD) calculated from temperature minus the mixed layer
    depth calculated using density.

    """
    import gsw

    sigma_theta = gsw.sigma0(SA, CT)
    mask = mixed_layer_depth(CT)
    mld = np.where(mask)[0][-1]
    sig_surface = sigma_theta[0]
    sig_bottom_mld = gsw.sigma0(SA[0], CT[mld])
    d_sig_t = sig_surface - sig_bottom_mld
    d_sig = sigma_theta - sig_bottom_mld
    mask = d_sig < d_sig_t  # Barrier layer.
    return Series(mask, index=SA.index, name="BLT")


Instruction:
Can you tell what is the following Python 3 function doing
def plot_cast(df, secondary_y=False, label=None, *args, **kwargs):

    ax = kwargs.pop("ax", None)
    fignums = plt.get_fignums()
    if ax is None and not fignums:
        ax = plt.axes()
        fig = ax.get_figure()
        fig.set_size_inches((5.25, 6.75))
    else:
        ax = plt.gca()
        fig = plt.gcf()

    figsize = kwargs.pop("figsize", fig.get_size_inches())
    fig.set_size_inches(figsize)

    y_inverted = False
    if not getattr(ax, "y_inverted", False):
        setattr(ax, "y_inverted", True)
        y_inverted = True

    if secondary_y:
        ax = ax.twiny()

    xlabel = getattr(df, "name", None)
    ylabel = getattr(df.index, "name", None)

    if isinstance(df, pd.DataFrame):
        labels = label if label else df.columns
        for k, (col, series) in enumerate(df.iteritems()):
            ax.plot(series, series.index, label=labels[k])
    elif isinstance(df, pd.Series):
        label = label if label else str(df.name)
        ax.plot(df.values, df.index, label=label, *args, **kwargs)

    ax.set_ylabel(ylabel)
    ax.set_xlabel(xlabel)
    if y_inverted and not secondary_y:
        ax.invert_yaxis()
    return ax

Response: Plot a CTD variable with the index in the y - axis instead of x - axis.


Instruction:
Make a summary of the following Python 3 code
def split(df):
    idx = df.index.argmax() + 1
    down = df.iloc[:idx]
    # Reverse index to orient it as a CTD cast.
    up = df.iloc[idx:][::-1]
    return down, up

Response: Returns a tuple with down and up - cast.


Instruction:
How would you explain what the following Python 3 function does
def lp_filter(df, sample_rate=24.0, time_constant=0.15):

    from scipy import signal

    # Butter is closer to what SBE is doing with their cosine filter.
    Wn = (1.0 / time_constant) / (sample_rate * 2.0)
    b, a = signal.butter(2, Wn, "low")
    new_df = df.copy()
    new_df.index = signal.filtfilt(b, a, df.index.values)
    return new_df

Response: Filter a series with time constant and for


Instruction:
Can you generate the documentation for the following Python 3 function
def press_check(df):
    new_df = df.copy()
    press = new_df.copy().index.values

    ref = press[0]
    inversions = np.diff(np.r_[press, press[-1]]) < 0
    mask = np.zeros_like(inversions)
    for k, p in enumerate(inversions):
        if p:
            ref = press[k]
            cut = press[k + 1 :] < ref
            mask[k + 1 :][cut] = True
    new_df[mask] = np.NaN
    return new_df

Response: Check that pressure reversals in the index are indeed in the correct order.


Instruction:
How would you explain what the following Python 3 function does
def bindata(df, delta=1.0, method="average"):
    start = np.floor(df.index[0])
    stop = np.ceil(df.index[-1])
    new_index = np.arange(start, stop, delta)
    binned = pd.cut(df.index, bins=new_index)
    if method == "average":
        new_df = df.groupby(binned).mean()
        new_df.index = new_index[:-1]
    elif method == "interpolate":
        raise NotImplementedError(
            "Bin-average via interpolation method is not Implemented yet."
        )
    else:
        raise ValueError(
            f"Expected method `average` or `interpolate`, but got {method}."
        )
    return new_df

Response: Bin average the index to a given interval.


Instruction:
How would you explain what the following Python 3 function does
def _despike(series, n1, n2, block, keep):

    data = series.values.astype(float).copy()
    roll = _rolling_window(data, block)
    roll = ma.masked_invalid(roll)
    std = n1 * roll.std(axis=1)
    mean = roll.mean(axis=1)
    # Use the last value to fill-up.
    std = np.r_[std, np.tile(std[-1], block - 1)]
    mean = np.r_[mean, np.tile(mean[-1], block - 1)]
    mask = np.abs(data - mean.filled(fill_value=np.NaN)) > std.filled(
        fill_value=np.NaN
    )
    data[mask] = np.NaN

    # Pass two recompute the mean and std without the flagged values from pass
    # one and removed the flagged data.
    roll = _rolling_window(data, block)
    roll = ma.masked_invalid(roll)
    std = n2 * roll.std(axis=1)
    mean = roll.mean(axis=1)
    # Use the last value to fill-up.
    std = np.r_[std, np.tile(std[-1], block - 1)]
    mean = np.r_[mean, np.tile(mean[-1], block - 1)]
    values = series.values.astype(float)
    mask = np.abs(values - mean.filled(fill_value=np.NaN)) > std.filled(
        fill_value=np.NaN
    )

    clean = series.astype(float).copy()
    clean[mask] = np.NaN
    return clean

Response:
Passes with Standard deviation
    n1 and n2 with window size block.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def despike(df, n1=2, n2=20, block=100, keep=0):
    if isinstance(df, pd.Series):
        new_df = _despike(df, n1=n1, n2=n2, block=block, keep=keep)
    else:
        new_df = df.apply(_despike, n1=n1, n2=n2, block=block, keep=keep)
    return new_df

Response:
Wild Edit Seabird - like function. Passes with Standard deviation
    n1 and n2 with window size block.


Instruction:
How would you explain what the following Python 3 function does
def _smooth(series, window_len, window):

    windows = {
        "flat": np.ones,
        "hanning": np.hanning,
        "hamming": np.hamming,
        "bartlett": np.bartlett,
        "blackman": np.blackman,
    }
    data = series.values.copy()

    if window_len < 3:
        return pd.Series(data, index=series.index, name=series.name)

    if window not in list(windows.keys()):
        raise ValueError(
            """window must be one of 'flat', 'hanning',
                         'hamming', 'bartlett', 'blackman'"""
        )

    s = np.r_[
        2 * data[0] - data[window_len:1:-1],
        data,
        2 * data[-1] - data[-1:-window_len:-1],
    ]

    w = windows[window](window_len)

    data = np.convolve(w / w.sum(), s, mode="same")
    data = data[window_len - 1 : -window_len + 1]
    return pd.Series(data, index=series.index, name=series.name)

Response: Smooth the data using a window with requested size.


Instruction:
How would you explain what the following Python 3 function does
def smooth(df, window_len=11, window="hanning"):
    if isinstance(df, pd.Series):
        new_df = _smooth(df, window_len=window_len, window=window)
    else:
        new_df = df.apply(_smooth, window_len=window_len, window=window)
    return new_df

Response: Smooth the data using a window with requested size.


Instruction:
Write a Python 3 script for
inserting a new job into the currently used tube and returns the ID of the new job.

Response:
def put(self,
            body: Body,
            priority: int = DEFAULT_PRIORITY,
            delay: int = DEFAULT_DELAY,
            ttr: int = DEFAULT_TTR) -> int:
        """Inserts a job into the currently used tube and returns the job ID.

        :param body: The data representing the job.
        :param priority: An integer between 0 and 4,294,967,295 where 0 is the
                         most urgent.
        :param delay: The number of seconds to delay the job for.
        :param ttr: The maximum number of seconds the job can be reserved for
                    before timing out.
        """
        if isinstance(body, str):
            if self.encoding is None:
                raise TypeError("Unable to encode string with no encoding set")
            body = body.encode(self.encoding)
        cmd = b'put %d %d %d %d\r\n%b' % (priority, delay, ttr, len(body), body)
        return self._int_cmd(cmd, b'INSERTED')


Instruction:
How would you code a function in Python 3 to
change the currently used tube.

Response:
def use(self, tube: str) -> None:
        """Changes the currently used tube.

        :param tube: The tube to use.
        """
        self._send_cmd(b'use %b' % tube.encode('ascii'), b'USING')


Instruction:
Create a Python 3 function to
reserve a job from a tube on the watch list giving this client exclusive access to it. Returns the reserved job.

Response:
def reserve(self, timeout: Optional[int] = None) -> Job:
        """Reserves a job from a tube on the watch list, giving this client
        exclusive access to it for the TTR. Returns the reserved job.

        This blocks until a job is reserved unless a ``timeout`` is given,
        which will raise a :class:`TimedOutError <greenstalk.TimedOutError>` if
        a job cannot be reserved within that time.

        :param timeout: The maximum number of seconds to wait.
        """
        if timeout is None:
            cmd = b'reserve'
        else:
            cmd = b'reserve-with-timeout %d' % timeout
        return self._job_cmd(cmd, b'RESERVED')


Instruction:
Can you tell what is the following Python 3 function doing
def delete(self, job: JobOrID) -> None:
        self._send_cmd(b'delete %d' % _to_id(job), b'DELETED')

Response:
Deletes a job.

        :param job: The job or job ID to delete.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def release(self,
                job: Job,
                priority: int = DEFAULT_PRIORITY,
                delay: int = DEFAULT_DELAY) -> None:
        """Releases a reserved job.

        :param job: The job to release.
        :param priority: An integer between 0 and 4,294,967,295 where 0 is the
                         most urgent.
        :param delay: The number of seconds to delay the job for.
        """
        self._send_cmd(b'release %d %d %d' % (job.id, priority, delay), b'RELEASED')

Response: Releases a reserved job.


Instruction:
Create a Python 3 function to
bury a reserved job.

Response:
def bury(self, job: Job, priority: int = DEFAULT_PRIORITY) -> None:
        """Buries a reserved job.

        :param job: The job to bury.
        :param priority: An integer between 0 and 4,294,967,295 where 0 is the
                         most urgent.
        """
        self._send_cmd(b'bury %d %d' % (job.id, priority), b'BURIED')


Instruction:
Write a Python 3 script to
refresh the TTR of a reserved job.

Response:
def touch(self, job: Job) -> None:
        """Refreshes the TTR of a reserved job.

        :param job: The job to touch.
        """
        self._send_cmd(b'touch %d' % job.id, b'TOUCHED')


Instruction:
How would you explain what the following Python 3 function does
def watch(self, tube: str) -> int:
        return self._int_cmd(b'watch %b' % tube.encode('ascii'), b'WATCHING')

Response: Adds a tube to the watch list. Returns the number of tubes this client is watching.


Instruction:
Make a summary of the following Python 3 code
def ignore(self, tube: str) -> int:
        return self._int_cmd(b'ignore %b' % tube.encode('ascii'), b'WATCHING')

Response: Removes a tube from the watch list. Returns the number of tubes this client is watching.


Instruction:
Here you have a function in Python 3, explain what it does
def kick(self, bound: int) -> int:
        return self._int_cmd(b'kick %d' % bound, b'KICKED')

Response: Moves delayed and buried jobs into the ready queue and returns the number of jobs effected.


Instruction:
Write a Python 3 script to
move a delayed or buried job into the ready queue.

Response:
def kick_job(self, job: JobOrID) -> None:
        """Moves a delayed or buried job into the ready queue.

        :param job: The job or job ID to kick.
        """
        self._send_cmd(b'kick-job %d' % _to_id(job), b'KICKED')


Instruction:
Make a summary of the following Python 3 code
def stats_job(self, job: JobOrID) -> Stats:
        return self._stats_cmd(b'stats-job %d' % _to_id(job))

Response: Returns the job statistics for the given job.


Instruction:
Create a Python 3 function for
returning the statistics for the given tube.

Response:
def stats_tube(self, tube: str) -> Stats:
        """Returns tube statistics.

        :param tube: The tube to return statistics for.
        """
        return self._stats_cmd(b'stats-tube %b' % tube.encode('ascii'))


Instruction:
Create a Python 3 function to
prevent jobs from being reserved from a tube for a period of time.

Response:
def pause_tube(self, tube: str, delay: int) -> None:
        """Prevents jobs from being reserved from a tube for a period of time.

        :param tube: The tube to pause.
        :param delay: The number of seconds to pause the tube for.
        """
        self._send_cmd(b'pause-tube %b %d' % (tube.encode('ascii'), delay), b'PAUSED')


Instruction:
Can you implement a function in Python 3 that
consumes messages from an AMQP queue using a Python callback.

Response:
def consume(exchange, queue_name, routing_key, callback, app_name):
    """Consume messages from an AMQP queue using a Python callback."""
    # The configuration validates these are not null and contain all required keys
    # when it is loaded.

    bindings = config.conf["bindings"]
    queues = config.conf["queues"]

    # The CLI and config.DEFAULTS have different defaults for the queue
    # settings at the moment.  We should select a universal default in the
    # future and remove this. Unfortunately that will break backwards compatibility.
    if queues == config.DEFAULTS["queues"]:
        queues[config._default_queue_name]["durable"] = True
        queues[config._default_queue_name]["auto_delete"] = False

    if queue_name:
        queues = {queue_name: config.conf["queues"][config._default_queue_name]}
        for binding in bindings:
            binding["queue"] = queue_name

    if exchange:
        for binding in bindings:
            binding["exchange"] = exchange

    if routing_key:
        for binding in bindings:
            binding["routing_keys"] = routing_key

    callback_path = callback or config.conf["callback"]
    if not callback_path:
        raise click.ClickException(
            "A Python path to a callable object that accepts the message must be provided"
            ' with the "--callback" command line option or in the configuration file'
        )
    try:
        module, cls = callback_path.strip().split(":")
    except ValueError:
        raise click.ClickException(
            "Unable to parse the callback path ({}); the "
            'expected format is "my_package.module:'
            'callable_object"'.format(callback_path)
        )
    try:
        module = importlib.import_module(module)
    except ImportError as e:
        provider = "--callback argument" if callback else "configuration file"
        raise click.ClickException(
            "Failed to import the callback module ({}) provided in the {}".format(
                str(e), provider
            )
        )

    try:
        callback = getattr(module, cls)
    except AttributeError as e:
        raise click.ClickException(
            "Unable to import {} ({}); is the package installed? The python path should "
            'be in the format "my_package.module:callable_object"'.format(
                callback_path, str(e)
            )
        )

    if app_name:
        config.conf["client_properties"]["app"] = app_name

    _log.info("Starting consumer with %s callback", callback_path)
    try:
        deferred_consumers = api.twisted_consume(
            callback, bindings=bindings, queues=queues
        )
        deferred_consumers.addCallback(_consume_callback)
        deferred_consumers.addErrback(_consume_errback)
    except ValueError as e:
        click_version = pkg_resources.get_distribution("click").parsed_version
        if click_version < pkg_resources.parse_version("7.0"):
            raise click.exceptions.BadOptionUsage(str(e))
        else:
            raise click.exceptions.BadOptionUsage("callback", str(e))

    reactor.run()
    sys.exit(_exit_code)


Instruction:
Can you implement a function in Python 3 that
handles any errors that occur during consumer registration.

Response:
def _consume_errback(failure):
    """Handle any errors that occur during consumer registration."""
    global _exit_code
    if failure.check(exceptions.BadDeclaration):
        _log.error(
            "Unable to declare the %s object on the AMQP broker. The "
            "broker responded with %s. Check permissions for your user.",
            failure.value.obj_type,
            failure.value.reason,
        )
        _exit_code = 10
    elif failure.check(exceptions.PermissionException):
        _exit_code = 15
        _log.error(
            "The consumer could not proceed because of a permissions problem: %s",
            str(failure.value),
        )
    elif failure.check(exceptions.ConnectionException):
        _exit_code = 14
        _log.error(failure.value.reason)
    else:
        _exit_code = 11
        _log.exception(
            "An unexpected error (%r) occurred while registering the "
            "consumer, please report this bug.",
            failure.value,
        )
    try:
        reactor.stop()
    except error.ReactorNotRunning:
        pass


Instruction:
How would you explain what the following Python 3 function does
def _consume_callback(consumers):
    for consumer in consumers:

        def errback(failure):
            global _exit_code
            if failure.check(exceptions.HaltConsumer):
                _exit_code = failure.value.exit_code
                if _exit_code:
                    _log.error(
                        "Consumer halted with non-zero exit code (%d): %s",
                        _exit_code,
                        str(failure.value.reason),
                    )
            elif failure.check(exceptions.ConsumerCanceled):
                _exit_code = 12
                _log.error(
                    "The consumer was canceled server-side, check with system administrators."
                )
            elif failure.check(exceptions.PermissionException):
                _exit_code = 15
                _log.error(
                    "The consumer could not proceed because of a permissions problem: %s",
                    str(failure.value),
                )
            else:
                _exit_code = 13
                _log.error(
                    "Unexpected error occurred in consumer %r: %r", consumer, failure
                )
            try:
                reactor.stop()
            except error.ReactorNotRunning:
                pass

        def callback(consumer):
            _log.info("The %r consumer halted.", consumer)
            if all([c.result.called for c in consumers]):
                _log.info("All consumers have stopped; shutting down.")
                try:
                    # Last consumer out shuts off the lights
                    reactor.stop()
                except error.ReactorNotRunning:
                    pass

        consumer.result.addCallbacks(callback, errback)

Response: Callback when consumers are successfully registered.


Instruction:
Make a summary of the following Python 3 code
def _add_timeout(deferred, timeout):
    try:
        deferred.addTimeout(timeout, reactor)
    except AttributeError:
        # Twisted 12.2 (in EL7) does not have the addTimeout API, so make do with
        # the slightly more annoying approach of scheduling a call to cancel which
        # is then canceled if the deferred succeeds before the timeout is up.
        delayed_cancel = reactor.callLater(timeout, deferred.cancel)

        def cancel_cancel_call(result):
            """Halt the delayed call to cancel if the deferred fires before the timeout."""
            if not delayed_cancel.called:
                delayed_cancel.cancel()
            return result

        deferred.addBoth(cancel_cancel_call)

Response: Add a timeout to the given deferred.


Instruction:
Can you write a function in Python 3 where it
allocates a new AMQP channel.

Response:
def _allocate_channel(self):
        """
        Allocate a new AMQP channel.

        Raises:
            NoFreeChannels: If this connection has reached its maximum number of channels.
        """
        try:
            channel = yield self.channel()
        except pika.exceptions.NoFreeChannels:
            raise NoFreeChannels()
        _std_log.debug("Created AMQP channel id %d", channel.channel_number)
        if self._confirms:
            yield channel.confirm_delivery()
        defer.returnValue(channel)


Instruction:
Implement a Python 3 function for
publishing a message to an AMQP exchange.

Response:
def publish(self, message, exchange):
        """
        Publish a :class:`fedora_messaging.message.Message` to an `exchange`_
        on the message broker.

        Args:
            message (message.Message): The message to publish.
            exchange (str): The name of the AMQP exchange to publish to

        Raises:
            NoFreeChannels: If there are no available channels on this connection.
                If this occurs, you can either reduce the number of consumers on this
                connection or create an additional connection.
            PublishReturned: If the broker rejected the message. This can happen if
                there are resource limits that have been reached (full disk, for example)
                or if the message will be routed to 0 queues and the exchange is set to
                reject such messages.

        .. _exchange: https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchanges
        """
        message.validate()
        try:
            yield self._channel.basic_publish(
                exchange=exchange,
                routing_key=message._encoded_routing_key,
                body=message._encoded_body,
                properties=message._properties,
            )
        except (pika.exceptions.NackError, pika.exceptions.UnroutableError) as e:
            _std_log.error("Message was rejected by the broker (%s)", str(e))
            raise PublishReturned(reason=e)
        except pika.exceptions.ChannelClosed:
            self._channel = yield self._allocate_channel()
            yield self.publish(message, exchange)
        except pika.exceptions.ConnectionClosed as e:
            raise ConnectionException(reason=e)


Instruction:
Write a Python 3 script for
registering a message consumer that executes the provided callback when a message is received.

Response:
def consume(self, callback, queue, previous_consumer=None):
        """
        Register a message consumer that executes the provided callback when
        messages are received.

        The queue must exist prior to calling this method. If a consumer
        already exists for the given queue, the callback is simply updated and
        any new messages for that consumer use the new callback.

        Args:
            callback (callable): The callback to invoke when a message is received.
            queue (str): The name of the queue to consume from.
            previous_consumer (ConsumerV2): If this is the resumption of a prior
                consumer, you can provide the previous consumer so its result
                deferred can be re-used.

        Returns:
            Deferred: A Deferred that fires when the consumer is successfully
                registered with the message broker. The callback receives a
                :class:`.ConsumerV2` object that represents the AMQP consumer.
                The Deferred may error back with a :class:`PermissionException`
                if the user cannot read from the queue, a
                :class:`NoFreeChannels` if this connection has hit its channel
                limit, or a :class:`ConnectionException` if the connection dies
                before the consumer is successfully registered.

        NoFreeChannels: If there are no available channels on this connection.
            If this occurs, you can either reduce the number of consumers on this
            connection or create an additional connection.
        """
        if queue in self._consumers:
            self._consumers[queue].callback = callback
            defer.returnValue(self._consumers[queue])

        if previous_consumer is not None:
            consumer = previous_consumer
        else:
            consumer = ConsumerV2(queue=queue, callback=callback)
        consumer._protocol = self
        consumer._channel = yield self._allocate_channel()
        try:
            queue_object, _ = yield consumer._channel.basic_consume(
                queue=consumer.queue, consumer_tag=consumer._tag
            )
        except pika.exceptions.ChannelClosed as exc:
            if exc.args[0] == 403:
                raise PermissionException(
                    obj_type="queue", description=queue, reason=exc.args[1]
                )
            else:
                raise ConnectionException(reason=exc)

        def on_cancel_callback(frame):
            """
            Called when the consumer is canceled server-side.

            This can happen, for example, when the queue is deleted.
            To handle this, we do the necessary book-keeping to remove the consumer
            and then fire the errback on the consumer so the caller of
            :func:`fedora_messaging.api.consume` can decide what to do.

            Args:
                frame (pika.frame.Method): The cancel method from the server,
                    unused here because we already know what consumer is being
                    canceled.
            """
            _std_log.error("%r was canceled by the AMQP broker!", consumer)

            # If client and server are racing to cancel it might already be gone which
            # is why both are marked as no cover.
            try:
                del self._consumers[consumer.queue]
            except KeyError:  # pragma: no cover
                pass
            try:
                del self.factory._consumers[consumer.queue]
            except KeyError:  # pragma: no cover
                pass
            consumer._running = False
            consumer.result.errback(fail=ConsumerCanceled())

        try:
            consumer._channel.add_on_cancel_callback(on_cancel_callback)
        except AttributeError:
            pass  # pika 1.0.0+

        def read_loop_errback(failure):
            """
            Handle errors coming out of the read loop.

            There are two basic categories of errors: ones where the ``consumer.result``
            Deferred needs to be fired because the error is not recoverable, ones
            where we can recover from by letting the connection restart, and ones
            which are fatal for this consumer only (the queue was deleted by an
            administrator).

            Args:
                failure (twisted.python.failure.Failure): The exception raised by
                    the read loop encapsulated in a Failure.
            """
            exc = failure.value
            if failure.check(pika.exceptions.ConsumerCancelled):
                # Pika 1.0.0+ raises this exception. To support previous versions
                # we register a callback (called below) ourselves with the channel.
                on_cancel_callback(None)
            elif failure.check(pika.exceptions.ChannelClosed):
                if exc.args[0] == 403:
                    # This is a mis-configuration, the consumer can register itself,
                    # but it doesn't have permissions to read from the queue,
                    # so no amount of restarting will help.
                    e = PermissionException(
                        obj_type="queue",
                        description=queue,
                        reason=failure.value.args[1],
                    )
                    consumer.result.errback(Failure(e, PermissionException))
                    consumer.cancel()
                else:
                    _std_log.exception(
                        "Consumer halted (%r) unexpectedly; "
                        "the connection should restart.",
                        failure,
                    )
            elif failure.check(error.ConnectionDone, error.ConnectionLost):
                _std_log.warning(
                    "The connection to the broker was lost (%r), consumer halted; "
                    "the connection should restart and consuming will resume.",
                    exc,
                )
            elif failure.check(pika.exceptions.AMQPError):
                _std_log.exception(
                    "An unexpected AMQP error occurred; the connection should "
                    "restart, but please report this as a bug."
                )
            else:
                consumer.result.errback(failure)
                consumer.cancel()

        consumer._read_loop = self._read(queue_object, consumer)
        consumer._read_loop.addErrback(read_loop_errback)

        self._consumers[queue] = consumer
        _std_log.info("Successfully registered AMQP consumer %r", consumer)
        defer.returnValue(consumer)


Instruction:
Can you tell what is the following Python 3 function doing
def declare_exchanges(self, exchanges):
        channel = yield self._allocate_channel()
        try:
            for exchange in exchanges:
                args = exchange.copy()
                args.setdefault("passive", config.conf["passive_declares"])
                try:
                    yield channel.exchange_declare(**args)
                except pika.exceptions.ChannelClosed as e:
                    raise BadDeclaration("exchange", args, e)
        finally:
            try:
                channel.close()
            except pika.exceptions.AMQPError:
                pass

Response: A method that declares a number of exchanges at once.


Instruction:
Implement a Python 3 function for
declaring a list of queues.

Response:
def declare_queues(self, queues):
        """
        Declare a list of queues.

        Args:
            queues (list of dict): A list of dictionaries, where each dictionary
                represents an exchange. Each dictionary can have the following keys:

                  * queue (str): The name of the queue
                  * passive (bool): If true, this will just assert that the queue exists,
                    but won't create it if it doesn't. Defaults to the configuration value
                    :ref:`conf-passive-declares`
                  * durable (bool): Whether or not the queue is durable
                  * exclusive (bool): Whether or not the queue is exclusive to this connection.
                  * auto_delete (bool): Whether or not the queue should be automatically
                    deleted once this connection ends.
                  * arguments (dict): Additional arguments for the creation of the queue.
        Raises:
            NoFreeChannels: If there are no available channels on this connection.
                If this occurs, you can either reduce the number of consumers on this
                connection or create an additional connection.
            BadDeclaration: If a queue could not be declared. This can occur
                if the queue already exists, but does its type does not match
                (e.g. it is declared as a durable queue, but exists as a non-durable
                queue). It can also occur if it does not exist, but the current
                user does not have permissions to create the object.
        """
        channel = yield self._allocate_channel()
        try:
            for queue in queues:
                args = queue.copy()
                args.setdefault("passive", config.conf["passive_declares"])
                try:
                    yield channel.queue_declare(**args)
                except pika.exceptions.ChannelClosed as e:
                    raise BadDeclaration("queue", args, e)
        finally:
            try:
                channel.close()
            except pika.exceptions.AMQPError:
                pass


Instruction:
Write a Python 3 script to
declare a set of bindings between queues and exchanges.

Response:
def bind_queues(self, bindings):
        """
        Declare a set of bindings between queues and exchanges.

        Args:
            bindings (list of dict): A list of binding definitions. Each dictionary
                must contain the "queue" key whose value is the name of the queue
                to create the binding on, as well as the "exchange" key whose value
                should be the name of the exchange to bind to. Additional acceptable
                keys are any keyword arguments accepted by
                :meth:`pika.channel.Channel.queue_bind`.

        Raises:
            NoFreeChannels: If there are no available channels on this connection.
                If this occurs, you can either reduce the number of consumers on this
                connection or create an additional connection.
            BadDeclaration: If a binding could not be declared. This can occur if the
                queue or exchange don't exist, or if they do, but the current user does
                not have permissions to create bindings.
        """
        channel = yield self._allocate_channel()
        try:
            for binding in bindings:
                try:
                    yield channel.queue_bind(**binding)
                except pika.exceptions.ChannelClosed as e:
                    raise BadDeclaration("binding", binding, e)
        finally:
            try:
                channel.close()
            except pika.exceptions.AMQPError:
                pass


Instruction:
Write a Python 3 script for
signaling to consumers they should stop after finishing any messages currently being processed then close the connection.

Response:
def halt(self):
        """
        Signal to consumers they should stop after finishing any messages
        currently being processed, then close the connection.

        Returns:
            defer.Deferred: fired when all consumers have successfully stopped
            and the connection is closed.
        """
        if self.is_closed:
            # We were asked to stop because the connection is already gone.
            # There's no graceful way to stop because we can't acknowledge
            # messages in the middle of being processed.
            _std_log.info("Disconnect requested, but AMQP connection already gone")
            self._channel = None
            return

        _std_log.info(
            "Waiting for %d consumer(s) to finish processing before halting",
            len(self._consumers),
        )
        pending_cancels = []
        for c in list(self._consumers.values()):
            pending_cancels.append(c.cancel())
        yield defer.gatherResults(pending_cancels)
        _std_log.info("Finished canceling %d consumers", len(self._consumers))

        try:
            yield self.close()
        except pika.exceptions.ConnectionWrongStateError:
            pass  # Already closing, not a problem since that's what we want.
        self._consumers = {}
        self._channel = None