Instruction:
Can you write a function in Python 3 where it
starts listening for incoming connections.

Response:
def listen(self, port=None):
        """Start listening for incoming connections.

        A request handler must have already been specified with
        ``TChannel.host``.

        :param port:
            An explicit port to listen on. This is unnecessary when advertising
            on Hyperbahn.

        :returns:
            Returns immediately.

        :raises AlreadyListeningError:
            If listen was already called.
        """

        if self.is_listening():
            raise AlreadyListeningError(
                "listen has already been called"
            )

        if port:
            assert not self._port, "Port has already been set."
            self._port = int(port)

        assert self._handler, "Call .host with a RequestHandler first"
        server = TChannelServer(self)

        bind_sockets_kwargs = {
            'port': self._port,
            # ipv6 causes random address already in use (socket.error w errno
            # == 98) when getaddrinfo() returns multiple values
            # @see https://github.com/uber/tchannel-python/issues/256
            'family': socket.AF_INET,
        }
        if self._reuse_port is True:
            # allow multiple processes to share the same port,
            # this is really useful in a world where services launch N
            # processes per container/os-space, where N is
            # the amount of cpus for example
            bind_sockets_kwargs['reuse_port'] = True

        sockets = bind_sockets(**bind_sockets_kwargs)
        assert sockets, "No sockets bound for port %d" % self._port

        # If port was 0, the OS probably assigned something better.
        self._port = sockets[0].getsockname()[1]

        server.add_sockets(sockets)

        # assign server so we don't listen twice
        self._server = server


Instruction:
Write a Python 3 function that can
register a simple endpoint with this TChannel.

Response:
def _register_simple(self, endpoint, scheme, f):
        """Register a simple endpoint with this TChannel.

        :param endpoint:
            Name of the endpoint being registered.
        :param scheme:
            Name of the arg scheme under which the endpoint will be
            registered.
        :param f:
            Callable handler for the endpoint.
        """
        assert scheme in DEFAULT_NAMES, ("Unsupported arg scheme %s" % scheme)
        if scheme == JSON:
            req_serializer = JsonSerializer()
            resp_serializer = JsonSerializer()
        else:
            req_serializer = RawSerializer()
            resp_serializer = RawSerializer()
        self._handler.register(endpoint, f, req_serializer, resp_serializer)
        return f


Instruction:
Write a Python 3 function for
registering a Thrift endpoint on this TChannel.

Response:
def _register_thrift(self, service_module, handler, **kwargs):
        """Register a Thrift endpoint on this TChannel.

        :param service_module:
            Reference to the Thrift-generated module for the service being
            registered.
        :param handler:
            Handler for the endpoint
        :param method:
            Name of the Thrift method being registered. If omitted, ``f``'s
            name is assumed to be the method name.
        :param service:
            Name of the Thrift service. By default this is determined
            automatically from the module name.
        """
        import tchannel.thrift as thrift
        # Imported inside the function so that we don't have a hard dependency
        # on the Thrift library. This function is usable only if the Thrift
        # library is installed.
        thrift.register(self._handler, service_module, handler, **kwargs)
        return handler


Instruction:
Make a summary of the following Python 3 code
def register(self, endpoint, scheme=None, handler=None, **kwargs):
        assert endpoint is not None, "endpoint is required"

        if endpoint is TChannel.FALLBACK:
            decorator = partial(self._handler.register, TChannel.FALLBACK)
            if handler is not None:
                return decorator(handler)
            else:
                return decorator

        if not scheme:
            # scheme defaults to raw, unless the endpoint is a service module.
            if inspect.ismodule(endpoint):
                scheme = "thrift"
            else:
                scheme = "raw"
        scheme = scheme.lower()
        if scheme == 'thrift':
            decorator = partial(self._register_thrift, endpoint, **kwargs)
        else:
            decorator = partial(
                self._register_simple, endpoint, scheme, **kwargs
            )

        if handler is not None:
            return decorator(handler)
        else:
            return decorator

Response: Register a handler with this TChannel.


Instruction:
Can you tell what is the following Python 3 function doing
def lt(self, i, j):
        if self.peers[i].rank == self.peers[j].rank:
            return self.peers[i].order < self.peers[j].order

        return self.peers[i].rank < self.peers[j].rank

Response: Compare the priority of two peers.


Instruction:
How would you code a function in Python 3 to
push a new peer into the heap

Response:
def push_peer(self, peer):
        """Push a new peer into the heap"""

        self.order += 1
        peer.order = self.order + random.randint(0, self.size())
        heap.push(self, peer)


Instruction:
How would you explain what the following Python 3 function does
def add_and_shuffle(self, peer):
        self.push_peer(peer)

        r = random.randint(0, self.size() - 1)
        self.swap_order(peer.index, r)

Response: Push a new peer into the heap and shuffle the heap


Instruction:
Write a Python 3 script for
removing the peer from the heap.

Response:
def remove_peer(self, peer):
        """Remove the peer from the heap.

        Return: removed peer if peer exists. If peer's index is out of range,
        raise IndexError.
        """
        if peer.index < 0 or peer.index >= self.size():
            raise IndexError('Peer index is out of range')

        assert peer is self.peers[peer.index], "peer is not in the heap"

        return heap.remove(self, peer.index)


Instruction:
Here you have a function in Python 3, explain what it does
def token_request(self, authorization_code):
        if not self._client.token_endpoint:
            return None

        request = {
            'grant_type': 'authorization_code',
            'code': authorization_code,
            'redirect_uri': self._redirect_uri
        }

        logger.debug('making token request: %s', request)
        client_auth_method = self._client.registration_response.get('token_endpoint_auth_method', 'client_secret_basic')
        auth_header = _ClientAuthentication(self._client.client_id, self._client.client_secret)(client_auth_method,
                                                                                request)
        resp = self._provider_configuration.requests_session \
            .post(self._client.token_endpoint,
                  data=request,
                  headers=auth_header) \
            .json()
        logger.debug('received token response: %s', json.dumps(resp))

        if 'error' in resp:
            token_resp = TokenErrorResponse(**resp)
        else:
            token_resp = AccessTokenResponse(**resp)
            token_resp.verify(keyjar=self._client.keyjar)
            if 'id_token' in resp:
                token_resp['id_token_jwt'] = resp['id_token']

        return token_resp

Response: Makes a token request.


Instruction:
Make a summary of the following Python 3 code
def userinfo_request(self, access_token):
        http_method = self._provider_configuration.userinfo_endpoint_method
        if http_method is None or not self._client.userinfo_endpoint:
            return None

        logger.debug('making userinfo request')
        userinfo_response = self._client.do_user_info_request(method=http_method, token=access_token)
        logger.debug('received userinfo response: %s', userinfo_response.to_json())

        return userinfo_response

Response: Makes a userinfo request to the user info endpoint.


Instruction:
Can you generate the documentation for the following Python 3 function
def update(self, access_token=None, id_token=None, id_token_jwt=None, userinfo=None):

        def set_if_defined(session_key, value):
            if value:
                self._session_storage[session_key] = value

        auth_time = int(time.time())
        if id_token:
            auth_time = id_token.get('auth_time', auth_time)

        self._session_storage['last_authenticated'] = auth_time
        set_if_defined('access_token', access_token)
        set_if_defined('id_token', id_token)
        set_if_defined('id_token_jwt', id_token_jwt)
        set_if_defined('userinfo', userinfo)

Response: Updates the internal state of the session.


Instruction:
Can you tell what is the following Python 3 function doing
def log_background_messages(self, name=None):
        with self.lock:
            if name:
                self._log_messages_by_thread(name)
            else:
                self._log_all_messages()

Response: Forwards all messages logged on background to Robot Framework log.


Instruction:
Can you create a Python 3 function that
decorates a function using a caller.

Response:
def decorate(func, caller):
    """
    decorate(func, caller) decorates a function using a caller.
    """
    evaldict = func.__globals__.copy()
    evaldict['_call_'] = caller
    evaldict['_func_'] = func
    fun = FunctionMaker.create(
        func, "return _call_(_func_, %(shortsignature)s)",
        evaldict, __wrapped__=func)
    if hasattr(func, '__qualname__'):
        fun.__qualname__ = func.__qualname__
    return fun


Instruction:
Can you create a Python 3 function that
sets a handler function for the type of message template currently loaded.

Response:
def set_client_handler(self, handler_func, name=None, header_filter=None, interval=0.5):
        """Sets an automatic handler for the type of message template currently loaded.

        This feature allows users to set a python handler function which is called
        automatically by the Rammbock message queue when message matches the expected
        template. The optional name argument defines the client node to which the
        handler will be bound. Otherwise the default client will be used.

        The header_filter defines which header field will be used to identify the
        message defined in template. (Otherwise all incoming messages will match!)

        The interval defines the interval in seconds on which the handler will
        be called on background. By default the incoming messages are checked
        every 0.5 seconds.

        The handler function will be called with two arguments: the rammbock library
        instance and the received message.

        Example:
        | Load template      | SomeMessage |
        | Set client handler | my_module.respond_to_sample |

        my_module.py:
        | def respond_to_sample(rammbock, msg):
        |     rammbock.save_template("__backup_template", unlocked=True)
        |     try:
        |         rammbock.load_template("sample response")
        |         rammbock.client_sends_message()
        |     finally:
        |         rammbock.load_template("__backup_template")
        """
        msg_template = self._get_message_template()
        client, client_name = self._clients.get_with_name(name)
        client.set_handler(msg_template, handler_func, header_filter=header_filter, interval=interval)


Instruction:
Write a Python 3 script for
setting a handler function for the type of message template currently loaded.

Response:
def set_server_handler(self, handler_func, name=None, header_filter=None, alias=None, interval=0.5):
        """Sets an automatic handler for the type of message template currently loaded.

        This feature allows users to set a python handler function which is called
        automatically by the Rammbock message queue when message matches the expected
        template. The optional name argument defines the server node to which the
        handler will be bound. Otherwise the default server will be used.

        The header_filter defines which header field will be used to identify the
        message defined in template. (Otherwise all incoming messages will match!)

        The interval defines the interval in seconds on which the handler will
        be called on background. By default the incoming messages are checked
        every 0.5 seconds.

        The alias is the alias for the connection. By default the current active
        connection will be used.

        The handler function will be called with two arguments: the rammbock library
        instance and the received message.

        Example:
        | Load template      | SomeMessage |
        | Set server handler | my_module.respond_to_sample | messageType |

        my_module.py:
        | def respond_to_sample(rammbock, msg):
        |     rammbock.save_template("__backup_template", unlocked=True)
        |     try:
        |         rammbock.load_template("sample response")
        |         rammbock.server_sends_message()
        |     finally:
        |         rammbock.load_template("__backup_template")
        """
        msg_template = self._get_message_template()
        server, server_name = self._servers.get_with_name(name)
        server.set_handler(msg_template, handler_func, header_filter=header_filter, alias=alias, interval=interval)


Instruction:
Can you generate the documentation for the following Python 3 function
def reset_rammbock(self):
        for client in self._clients:
            client.close()
        for server in self._servers:
            server.close()
        self._init_caches()

Response: Closes all connections deletes all servers clients and protocols and sets the internal cache to empty.


Instruction:
How would you code a function in Python 3 to
reset streams and sockets of incoming messages.

Response:
def clear_message_streams(self):
        """ Resets streams and sockets of incoming messages.

        You can use this method to reuse the same connections for several
        consecutive test cases.
        """
        for client in self._clients:
            client.empty()
        for server in self._servers:
            server.empty()


Instruction:
Write a Python 3 script for
starting defining a new protocol template.

Response:
def new_protocol(self, protocol_name):
        """Start defining a new protocol template.

        All messages sent and received from a connection that uses a protocol
        have to conform to this protocol template.
        """
        if self._protocol_in_progress:
            raise Exception('Can not start a new protocol definition in middle of old.')
        if protocol_name in self._protocols:
            raise Exception('Protocol %s already defined' % protocol_name)
        self._init_new_message_stack(Protocol(protocol_name, library=self))
        self._protocol_in_progress = True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def start_udp_server(self, ip, port, name=None, timeout=None, protocol=None, family='ipv4'):
        self._start_server(UDPServer, ip, port, name, timeout, protocol, family)

Response: Starts a new UDP server to given ip and port.


Instruction:
Create a Python 3 function for
starting a new TCP server to given ip and port.

Response:
def start_tcp_server(self, ip, port, name=None, timeout=None, protocol=None, family='ipv4'):
        """Starts a new TCP server to given `ip` and `port`.

        Server can be given a `name`, default `timeout` and a `protocol`.
        `family` can be either ipv4 (default) or ipv6. Notice that you have to
        use `Accept Connection` keyword for server to receive connections.

        Examples:
        | Start TCP server | 10.10.10.2 | 53 |
        | Start TCP server | 10.10.10.2 | 53 | Server1 |
        | Start TCP server | 10.10.10.2 | 53 | name=Server1 | protocol=GTPV2 |
        | Start TCP server | 10.10.10.2 | 53 | timeout=5 |
        | Start TCP server | 0:0:0:0:0:0:0:1 | 53 | family=ipv6 |
        """
        self._start_server(TCPServer, ip, port, name, timeout, protocol, family)


Instruction:
How would you code a function in Python 3 to
start a new SCTP server to given ip and port.

Response:
def start_sctp_server(self, ip, port, name=None, timeout=None, protocol=None, family='ipv4'):
        """Starts a new STCP server to given `ip` and `port`.

        `family` can be either ipv4 (default) or ipv6.

        pysctp (https://github.com/philpraxis/pysctp) need to be installed your system.
        Server can be given a `name`, default `timeout` and a `protocol`.
        Notice that you have to use `Accept Connection` keyword for server to
        receive connections.

        Examples:
        | Start STCP server | 10.10.10.2 | 53 |
        | Start STCP server | 10.10.10.2 | 53 | Server1 |
        | Start STCP server | 10.10.10.2 | 53 | name=Server1 | protocol=GTPV2 |
        | Start STCP server | 10.10.10.2 | 53 | timeout=5 |
        """
        self._start_server(SCTPServer, ip, port, name, timeout, protocol, family)


Instruction:
Explain what the following Python 3 code does
def start_udp_client(self, ip=None, port=None, name=None, timeout=None, protocol=None, family='ipv4'):
        self._start_client(UDPClient, ip, port, name, timeout, protocol, family)

Response: Starts a new UDP client.


Instruction:
Create a Python 3 function to
start a new TCP client.

Response:
def start_tcp_client(self, ip=None, port=None, name=None, timeout=None, protocol=None, family='ipv4'):
        """Starts a new TCP client.

        Client can be optionally given `ip` and `port` to bind to, as well as
        `name`, default `timeout` and a `protocol`. `family` can be either
        ipv4 (default) or ipv6.

        You should use `Connect` keyword to connect client to a host.

        Examples:
        | Start TCP client |
        | Start TCP client | name=Client1 | protocol=GTPV2 |
        | Start TCP client | 10.10.10.2 | 53 | name=Server1 | protocol=GTPV2 |
        | Start TCP client | timeout=5 |
        | Start TCP client | 0:0:0:0:0:0:0:1 | 53 | family=ipv6 |
        """
        self._start_client(TCPClient, ip, port, name, timeout, protocol, family)


Instruction:
Create a Python 3 function for
starting a new SCTP client.

Response:
def start_sctp_client(self, ip=None, port=None, name=None, timeout=None, protocol=None, family='ipv4'):
        """Starts a new SCTP client.

        Client can be optionally given `ip` and `port` to bind to, as well as
        `name`, default `timeout` and a `protocol`.  `family` can be either
        ipv4 (default) or ipv6.

        You should use `Connect` keyword to connect client to a host.

        Examples:
        | Start TCP client |
        | Start TCP client | name=Client1 | protocol=GTPV2 |
        | Start TCP client | 10.10.10.2 | 53 | name=Server1 | protocol=GTPV2 |
        | Start TCP client | timeout=5 |
        """
        self._start_client(SCTPClient, ip, port, name, timeout, protocol, family)


Instruction:
Write a Python 3 script to
accept a connection to server identified by name or the latest server.

Response:
def accept_connection(self, name=None, alias=None, timeout=0):
        """Accepts a connection to server identified by `name` or the latest
        server if `name` is empty.

        If given an `alias`, the connection is named and can be later referenced
        with that name.

        If `timeout` is > 0, the connection times out after the time specified.
        `timeout` defaults to 0 which will wait indefinitely.
        Empty value or None will use socket default timeout.

        Examples:
        | Accept connection |
        | Accept connection | Server1 | my_connection |
        | Accept connection | Server1 | my_connection | timeout=5 |
        """
        server = self._servers.get(name)
        server.accept_connection(alias, timeout)


Instruction:
Can you write a function in Python 3 where it
connects a client to given host and port.

Response:
def connect(self, host, port, name=None):
        """Connects a client to given `host` and `port`. If client `name` is not
        given then connects the latest client.

        Examples:
        | Connect | 127.0.0.1 | 8080 |
        | Connect | 127.0.0.1 | 8080 | Client1 |
        """
        client = self._clients.get(name)
        client.connect_to(host, port)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def client_sends_binary(self, message, name=None, label=None):
        client, name = self._clients.get_with_name(name)
        client.send(message)
        self._register_send(client, label, name)

Response: Send raw binary message.


Instruction:
Implement a function in Python 3 to
send raw binary message to server.

Response:
def server_sends_binary(self, message, name=None, connection=None, label=None):
        """Send raw binary `message`.

        If server `name` is not given, uses the latest server. Optional message
        `label` is shown on logs.

        Examples:
        | Server sends binary | Hello! |
        | Server sends binary | ${some binary} | Server1 | label=DebugMessage |
        | Server sends binary | ${some binary} | connection=my_connection |
        """
        server, name = self._servers.get_with_name(name)
        server.send(message, alias=connection)
        self._register_send(server, label, name, connection=connection)


Instruction:
How would you code a function in Python 3 to
receive raw binary message.

Response:
def client_receives_binary(self, name=None, timeout=None, label=None):
        """Receive raw binary message.

        If client `name` is not given, uses the latest client. Optional message
        `label` is shown on logs.

        Examples:
        | ${binary} = | Client receives binary |
        | ${binary} = | Client receives binary | Client1 | timeout=5 |
        """
        client, name = self._clients.get_with_name(name)
        msg = client.receive(timeout=timeout)
        self._register_receive(client, label, name)
        return msg


Instruction:
Can you create a Python 3 function that
receives raw binary message.

Response:
def server_receives_binary(self, name=None, timeout=None, connection=None, label=None):
        """Receive raw binary message.

        If server `name` is not given, uses the latest server. Optional message
        `label` is shown on logs.

        Examples:
        | ${binary} = | Server receives binary |
        | ${binary} = | Server receives binary | Server1 | connection=my_connection | timeout=5 |
        """
        return self.server_receives_binary_from(name, timeout, connection=connection, label=label)[0]


Instruction:
Create a Python 3 function for
receiving raw binary message from server.

Response:
def server_receives_binary_from(self, name=None, timeout=None, connection=None, label=None):
        """Receive raw binary message. Returns message, ip, and port.

        If server `name` is not given, uses the latest server. Optional message
        `label` is shown on logs.

        Examples:
        | ${binary} | ${ip} | ${port} = | Server receives binary from |
        | ${binary} | ${ip} | ${port} = | Server receives binary from | Server1 | connection=my_connection | timeout=5 |
        """
        server, name = self._servers.get_with_name(name)
        msg, ip, port = server.receive_from(timeout=timeout, alias=connection)
        self._register_receive(server, label, name, connection=connection)
        return msg, ip, port


Instruction:
Create a Python 3 function for
defining a new message template with message_name.

Response:
def new_message(self, message_name, protocol=None, *parameters):
        """Define a new message template with `message_name`.

        `protocol` has to be defined earlier with `Start Protocol Description`.
        Optional parameters are default values for message header separated with
        colon.

        Examples:
        | New message | MyMessage | MyProtocol | header_field:value |
        """
        proto = self._get_protocol(protocol)
        if not proto:
            raise Exception("Protocol not defined! Please define a protocol before creating a message!")
        if self._protocol_in_progress:
            raise Exception("Protocol definition in progress. Please finish it before starting to define a message.")
        configs, fields, header_fields = self._parse_parameters(parameters)
        self._raise_error_if_configs_or_fields(configs, fields, 'New message')
        self._init_new_message_stack(MessageTemplate(message_name, proto, header_fields))


Instruction:
Here you have a function in Python 3, explain what it does
def save_template(self, name, unlocked=False):
        if isinstance(unlocked, basestring):
            unlocked = unlocked.lower() != 'false'
        template = self._get_message_template()
        if not unlocked:
            template.set_as_saved()
        self._message_templates[name] = (template, self._field_values)

Response: Save a message template for later use with Load template.


Instruction:
Write a Python 3 function for
loading a message template saved with Save template.

Response:
def load_template(self, name, *parameters):
        """Load a message template saved with `Save template`.
        Optional parameters are default values for message header separated with
        colon.

        Examples:
        | Load Template | MyMessage | header_field:value |
        """
        template, fields, header_fields = self._set_templates_fields_and_header_fields(name, parameters)
        self._init_new_message_stack(template, fields, header_fields)


Instruction:
Can you tell what is the following Python 3 function doing
def load_copy_of_template(self, name, *parameters):
        template, fields, header_fields = self._set_templates_fields_and_header_fields(name, parameters)
        copy_of_template = copy.deepcopy(template)
        copy_of_fields = copy.deepcopy(fields)
        self._init_new_message_stack(copy_of_template, copy_of_fields, header_fields)

Response:
Load a copy of message template saved with Save template when originally saved values need to be preserved
        from test to test.


Instruction:
Given the following Python 3 function, write the documentation
def get_message(self, *parameters):
        _, message_fields, header_fields = self._get_parameters_with_defaults(parameters)
        return self._encode_message(message_fields, header_fields)

Response:
Get encoded message.

        * Send Message -keywords are convenience methods, that will call this to
        get the message object and then send it. Optional parameters are message
        field values separated with colon.

        Examples:
        | ${msg} = | Get message |
        | ${msg} = | Get message | field_name:value |


Instruction:
Write a Python 3 script to
receive a message from the buffer and validate it.

Response:
def client_receives_message(self, *parameters):
        """Receive a message with template defined using `New Message` and
        validate field values.

        Message template has to be defined with `New Message` before calling
        this.

        Optional parameters:
        - `name` the client name (default is the latest used) example: `name=Client 1`
        - `timeout` for receiving message. example: `timeout=0.1`
        - `latest` if set to True, get latest message from buffer instead first. Default is False. Example: `latest=True`
        -  message field values for validation separated with colon. example: `some_field:0xaf05`

        Examples:
        | ${msg} = | Client receives message |
        | ${msg} = | Client receives message | name=Client1 | timeout=5 |
        | ${msg} = | Client receives message | message_field:(0|1) |
        """
        with self._receive(self._clients, *parameters) as (msg, message_fields, header_fields):
            self._validate_message(msg, message_fields, header_fields)
            return msg


Instruction:
How would you code a function in Python 3 to
receive a message with template defined using New Message.

Response:
def client_receives_without_validation(self, *parameters):
        """Receive a message with template defined using `New Message`.

        Message template has to be defined with `New Message` before calling
        this.

        Optional parameters:
        - `name` the client name (default is the latest used) example: `name=Client 1`
        - `timeout` for receiving message. example: `timeout=0.1`
        - `latest` if set to True, get latest message from buffer instead first. Default is False. Example: `latest=True`

        Examples:
        | ${msg} = | Client receives without validation |
        | ${msg} = | Client receives without validation | name=Client1 | timeout=5 |
        """
        with self._receive(self._clients, *parameters) as (msg, _, _):
            return msg


Instruction:
Write a Python 3 script for
receiving a message from the server and validate it.

Response:
def server_receives_message(self, *parameters):
        """Receive a message with template defined using `New Message` and
        validate field values.

        Message template has to be defined with `New Message` before calling
        this.

        Optional parameters:
        - `name` the client name (default is the latest used) example: `name=Client 1`
        - `connection` alias. example: `connection=connection 1`
        - `timeout` for receiving message. example: `timeout=0.1`
        - `latest` if set to True, get latest message from buffer instead first. Default is False. Example: `latest=True`
        -  message field values for validation separated with colon. example: `some_field:0xaf05`

        Optional parameters are server `name`, `connection` alias and
        possible `timeout` separated with equals and message field values for
        validation separated with colon.

        Examples:
        | ${msg} = | Server receives message |
        | ${msg} = | Server receives message | name=Server1 | alias=my_connection | timeout=5 |
        | ${msg} = | Server receives message | message_field:(0|1) |
        """
        with self._receive(self._servers, *parameters) as (msg, message_fields, header_fields):
            self._validate_message(msg, message_fields, header_fields)
            return msg


Instruction:
How would you explain what the following Python 3 function does
def server_receives_without_validation(self, *parameters):
        with self._receive(self._servers, *parameters) as (msg, _, _):
            return msg

Response: Receive a message with template defined using New Message.


Instruction:
How would you explain what the following Python 3 function does
def validate_message(self, msg, *parameters):
        _, message_fields, header_fields = self._get_parameters_with_defaults(parameters)
        self._validate_message(msg, message_fields, header_fields)

Response:
Validates given message using template defined with New Message and
        field values given as optional arguments.


Instruction:
How would you code a function in Python 3 to
add an unsigned integer to template.

Response:
def uint(self, length, name, value=None, align=None):
        """Add an unsigned integer to template.

        `length` is given in bytes and `value` is optional. `align` can be used
        to align the field to longer byte length.

        Examples:
        | uint | 2 | foo |
        | uint | 2 | foo | 42 |
        | uint | 2 | fourByteFoo | 42 | align=4 |
        """
        self._add_field(UInt(length, name, value, align=align))


Instruction:
Can you tell what is the following Python 3 function doing
def int(self, length, name, value=None, align=None):
        self._add_field(Int(length, name, value, align=align))

Response: Add an integer field to template.


Instruction:
How would you explain what the following Python 3 function does
def chars(self, length, name, value=None, terminator=None):

        self._add_field(Char(length, name, value, terminator))

Response: Add a char array to the current entry in the log.


Instruction:
Create a Python 3 function for
defining a new struct to template.

Response:
def new_struct(self, type, name, *parameters):
        """Defines a new struct to template.

        You must call `End Struct` to end struct definition. `type` is the name
        for generic type and `name` is the field name in containing structure.
        Possible parameters are values for struct fields separated with colon
        and optional struct length defined with `length=`. Length can be used in
        receiveing to validate that struct matches predfeined length. When
        sending, the struct length can refer to other message field which will
        then be set dynamically.

        Examples:
        | New struct | Pair | myPair |
        | u8     | first |
        | u8     | second |
        | End Struct |
        """
        configs, parameters, _ = self._get_parameters_with_defaults(parameters)
        self._add_struct_name_to_params(name, parameters)
        self._message_stack.append(StructTemplate(type, name, self._current_container, parameters, length=configs.get('length'), align=configs.get('align')))


Instruction:
Write a Python 3 script for
defining a new list to template of size and with name.

Response:
def _new_list(self, size, name):
        """Defines a new list to template of `size` and with `name`.

        List type must be given after this keyword by defining one field. Then
        the list definition has to be closed using `End List`.

        Special value '*' in size means that list will decode values as long as
        data is available. This free length value is not supported on encoding.

        Examples:
        | New list | 5 | myIntList |
        | u16 |
        | End List |

        | u8 | listLength |
        | New list | listLength | myIntList |
        | u16 |
        | End List |

        | New list | * | myIntList |
        | u16 |
        | End List |
        """
        self._message_stack.append(ListTemplate(size, name, self._current_container))


Instruction:
Can you write a function in Python 3 where it
defines a new binary container to template.

Response:
def new_binary_container(self, name):
        """Defines a new binary container to template.

        Binary container can only contain binary fields defined with `Bin`
        keyword.

        Examples:
        | New binary container | flags |
        | bin | 2 | foo |
        | bin | 6 | bar |
        | End binary container |
        """
        self._message_stack.append(BinaryContainerTemplate(name, self._current_container))


Instruction:
Write a Python 3 script for
ending a binary container.

Response:
def end_binary_container(self):
        """End binary container. See `New Binary Container`.
        """
        binary_container = self._message_stack.pop()
        binary_container.verify()
        self._add_field(binary_container)


Instruction:
Can you implement a function in Python 3 that
adds a new binary field to template. This keyword has to be called within a binary container.

Response:
def bin(self, size, name, value=None):
        """Add new binary field to template.

        This keyword has to be called within a binary container. See `New Binary
        Container`.
        """
        self._add_field(Binary(size, name, value))


Instruction:
Create a Python 3 function to
define a new union to template of type and name.

Response:
def new_union(self, type, name):
        """Defines a new union to template of `type` and `name`.

        Fields inside the union are alternatives and the length of the union is
        the length of its longest field.

        Example:
        | New union | IntOrAddress | foo |
        | Chars | 16 | ipAddress |
        | u32   | int |
        | End union |
        """
        self._message_stack.append(UnionTemplate(type, name, self._current_container))


Instruction:
Explain what the following Python 3 code does
def start_bag(self, name):
        self._message_stack.append(BagTemplate(name, self._current_container))

Response: Starts bag templates for the given name.


Instruction:
Can you tell what is the following Python 3 function doing
def value(self, name, value):
        if isinstance(value, _StructuredElement):
            self._struct_fields_as_values(name, value)
        elif name.startswith('header:'):
            self._header_values[name.partition(':')[-1]] = value
        else:
            self._field_values[name] = value

Response: Defines a default value for a template field identified by name.


Instruction:
Can you create a Python 3 function that
defines a conditional element of name exists if condition is true.

Response:
def conditional(self, condition, name):
        """Defines a 'condition' when conditional element of 'name' exists if `condition` is true.

        `condition` can contain multiple conditions combined together using Logical Expressions(&&,||).

        Example:
        | Conditional | mycondition == 1 | foo |
        | u8   | myelement | 42 |
        | End conditional |

        | Conditional | condition1 == 1 && condition2 != 2 | bar |
        | u8   | myelement | 8 |
        | End condtional |
        """
        self._message_stack.append(ConditionalTemplate(condition, name, self._current_container))


Instruction:
Explain what the following Python 3 code does
def get_client_unread_messages_count(self, client_name=None):
        client = self._clients.get_with_name(client_name)[0]
        return client.get_messages_count_in_buffer()

Response: Gets the count of unread messages from client


Instruction:
Here you have a function in Python 3, explain what it does
def get_server_unread_messages_count(self, server_name=None):
        server = self._servers.get(server_name)
        return server.get_messages_count_in_buffer()

Response: Gets the count of unread messages from server


Instruction:
Write a Python 3 script to
compute the 2 s compliment of int value val

Response:
def to_twos_comp(val, bits):
    """compute the 2's compliment of int value val"""
    if not val.startswith('-'):
        return to_int(val)
    value = _invert(to_bin_str_from_int_string(bits, bin(to_int(val[1:]))))
    return int(value, 2) + 1


Instruction:
Can you implement a function in Python 3 that
adds an unsigned 1 byte integer field to template. This is an convenience method that simply calls uint and does not call it.

Response:
def u8(self, name, value=None, align=None):
        """Add an unsigned 1 byte integer field to template.

        This is an convenience method that simply calls `Uint` keyword with predefined length."""
        self.uint(1, name, value, align)


Instruction:
Make a summary of the following Python 3 code
def u16(self, name, value=None, align=None):
        self.uint(2, name, value, align)

Response: Add an unsigned 2 byte integer field to template.


Instruction:
Given the following Python 3 function, write the documentation
def u24(self, name, value=None, align=None):
        self.uint(3, name, value, align)

Response:
Add an unsigned 3 byte integer field to template.
        This is an convenience method that simply calls uint and also uses the predefined length.


Instruction:
Can you write a function in Python 3 where it
adds an unsigned 4 byte integer field to template.

Response:
def u32(self, name, value=None, align=None):
        """Add an unsigned 4 byte integer field to template.

        This is an convenience method that simply calls `Uint` keyword with predefined length."""
        self.uint(4, name, value, align)


Instruction:
How would you implement a function in Python 3 that
adds an unsigned 5 byte integer field to template. This is an convenience method that simply calls uint and then calls self. uint.

Response:
def u40(self, name, value=None, align=None):
        """Add an unsigned 5 byte integer field to template.

        This is an convenience method that simply calls `Uint` keyword with predefined length."""
        self.uint(5, name, value, align)


Instruction:
Write a Python 3 function that can
add an unsigned 8 byte integer field to template. This is an convenience method that simply calls uint and does not call it.

Response:
def u64(self, name, value=None, align=None):
        """Add an unsigned 8 byte integer field to template.

        This is an convenience method that simply calls `Uint` keyword with predefined length."""
        self.uint(8, name, value, align)


Instruction:
Can you generate the documentation for the following Python 3 function
def u128(self, name, value=None, align=None):
        self.uint(16, name, value, align)

Response: Add an unsigned 16 byte integer field to template.


Instruction:
How would you code a function in Python 3 to
add an 1 byte integer field to template. This is an convenience method that simply calls Int.

Response:
def i8(self, name, value=None, align=None):
        """Add an 1 byte integer field to template.

        This is an convenience method that simply calls `Int` keyword with predefined length."""
        self.int(1, name, value, align)


Instruction:
Can you generate the documentation for the following Python 3 function
def i32(self, name, value=None, align=None):
        self.int(4, name, value, align)

Response:
Add an 32 byte integer field to template.
        This is a convenience method that simply calls Int.


Instruction:
Can you create a Python 3 function that
defines a new array of given size and containing fields of type containing parameters of type name if the name of this array element is provided as the contents of the keyword parameters.

Response:
def array(self, size, type, name, *parameters):
        """Define a new array of given `size` and containing fields of type `type`.

        `name` if the name of this array element. The `type` is the name of keyword that is executed as the contents of
        the array and optional extra parameters are passed as arguments to this keyword.

        Examples:
        | Array | 8 | u16 | myArray |

        | u32 | length |
        | Array | length | someStruct | myArray | <argument for someStruct> |
        """
        self._new_list(size, name)
        BuiltIn().run_keyword(type, '', *parameters)
        self._end_list()


Instruction:
Can you tell what is the following Python 3 function doing
def container(self, name, length, type, *parameters):
        self.new_struct('Container', name, 'length=%s' % length)
        BuiltIn().run_keyword(type, *parameters)
        self.end_struct()

Response: Define a container with given length.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def embed_seqdiag_sequence(self):
        test_name = BuiltIn().replace_variables('${TEST NAME}')
        outputdir = BuiltIn().replace_variables('${OUTPUTDIR}')
        path = os.path.join(outputdir, test_name + '.seqdiag')
        SeqdiagGenerator().compile(path, self._message_sequence)

Response: Create a message sequence diagram png file to output folder and embed the image to log file


Instruction:
Can you generate the documentation for the following Python 3 function
def local_error(self, originalValue, calculatedValue):
        originalValue = originalValue[0]
        calculatedValue = calculatedValue[0]

        # error is zero
        if not originalValue and not calculatedValue:
            return 0.0

        return abs(calculatedValue - originalValue)/ ((abs(originalValue) + abs(calculatedValue))/2) * 100

Response: Calculates the error between two values.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def tostring(self, inject):
        injected_parts = ''
        for part in self.parts:
            injected = part.tostring(inject)
            tei_tag = next(
                (attribute for attribute in part.attributes if attribute.key == "tei-tag"), None)
            if tei_tag and tei_tag.text == "w" and injected_parts:
                # make sure words can be tokenized correctly
                if injected_parts and injected_parts[-1] != ' ':
                    injected_parts += ' '
                injected_parts += injected.strip() + ' '
            else:
                injected_parts += injected

        return inject(self, injected_parts)

Response: Convert an element to a single string and allow the passed inject method to place content before any ArcGIS element.


Instruction:
Can you write a function in Python 3 where it
creates a parser hooking up the command methods below to be run when chosen.

Response:
def _create_parser(self):
        """
        Create a parser hooking up the command methods below to be run when chosen.
        :return: CommandParser parser with commands attached.
        """
        parser = CommandParser(get_internal_version_str())
        parser.register_list_command(self._setup_run_command(ListCommand))
        parser.register_upload_command(self._setup_run_command(UploadCommand))
        parser.register_add_user_command(self._setup_run_command(AddUserCommand))
        parser.register_remove_user_command(self._setup_run_command(RemoveUserCommand))
        parser.register_download_command(self._setup_run_command(DownloadCommand))
        parser.register_share_command(self._setup_run_command(ShareCommand))
        parser.register_deliver_command(self._setup_run_command(DeliverCommand))
        parser.register_delete_command(self._setup_run_command(DeleteCommand))
        parser.register_list_auth_roles_command(self._setup_run_command(ListAuthRolesCommand))
        return parser


Instruction:
Implement a function in Python 3 to
check the version of the pypi and print a error to stderr and pause.

Response:
def _check_pypi_version(self):
        """
        When the version is out of date or we have trouble retrieving it print a error to stderr and pause.
        """
        try:
            check_version()
        except VersionException as err:
            print(str(err), file=sys.stderr)
            time.sleep(TWO_SECONDS)


Instruction:
Create a Python 3 function to
run command_constructor and call run on the resulting object

Response:
def _run_command(self, command_constructor, args):
        """
        Run command_constructor and call run(args) on the resulting object
        :param command_constructor: class of an object that implements run(args)
        :param args: object arguments for specific command created by CommandParser
        """
        verify_terminal_encoding(sys.stdout.encoding)
        self._check_pypi_version()
        config = create_config(allow_insecure_config_file=args.allow_insecure_config_file)
        self.show_error_stack_trace = config.debug_mode
        command = command_constructor(config)
        command.run(args)


Instruction:
Here you have a function in Python 3, explain what it does
def make_user_list(self, emails, usernames):
        to_users = []
        remaining_emails = [] if not emails else list(emails)
        remaining_usernames = [] if not usernames else list(usernames)
        for user in self.remote_store.fetch_users():
            if user.email in remaining_emails:
                to_users.append(user)
                remaining_emails.remove(user.email)
            elif user.username in remaining_usernames:
                to_users.append(user)
                remaining_usernames.remove(user.username)
        if remaining_emails or remaining_usernames:
            unable_to_find_users = ','.join(remaining_emails + remaining_usernames)
            msg = "Unable to find users for the following email/usernames: {}".format(unable_to_find_users)
            raise ValueError(msg)
        return to_users

Response: Given a list of emails and usernames fetch DukeDS user info.


Instruction:
Write a Python 3 function for
uploading contents of folders to a project with project_name on remote store.

Response:
def run(self, args):
        """
        Upload contents of folders to a project with project_name on remote store.
        If follow_symlinks we will traverse symlinked directories.
        If content is already on remote site it will not be sent.
        :param args: Namespace arguments parsed from the command line.
        """
        project_name_or_id = self.create_project_name_or_id_from_args(args)
        folders = args.folders                  # list of local files/folders to upload into the project
        follow_symlinks = args.follow_symlinks  # should we follow symlinks when traversing folders
        dry_run = args.dry_run                  # do not upload anything, instead print out what you would upload

        project_upload = ProjectUpload(self.config, project_name_or_id, folders, follow_symlinks=follow_symlinks)
        if dry_run:
            print(project_upload.dry_run_report())
        else:
            print(project_upload.get_differences_summary())
            if project_upload.needs_to_upload():
                project_upload.run()
                print('\n')
                print(project_upload.get_upload_report())
                print('\n')
            print(project_upload.get_url_msg())


Instruction:
How would you explain what the following Python 3 function does
def run(self, args):
        project_name_or_id = self.create_project_name_or_id_from_args(args)
        folder = args.folder                # path to a folder to download data into
        # Default to project name with spaces replaced with '_' if not specified
        if not folder:
            folder = replace_invalid_path_chars(project_name_or_id.value.replace(' ', '_'))
        destination_path = format_destination_path(folder)
        path_filter = PathFilter(args.include_paths, args.exclude_paths)
        project = self.fetch_project(args, must_exist=True)
        project_download = ProjectDownload(self.remote_store, project, destination_path, path_filter)
        project_download.run()

Response: Download a project based on passed in args.


Instruction:
Implement a function in Python 3 to
give the user with user_full_name the auth_role permissions on the remote project with project_name.

Response:
def run(self, args):
        """
        Give the user with user_full_name the auth_role permissions on the remote project with project_name.
        :param args Namespace arguments parsed from the command line
        """
        email = args.email                  # email of person to give permissions, will be None if username is specified
        username = args.username            # username of person to give permissions, will be None if email is specified
        auth_role = args.auth_role          # type of permission(project_admin)
        project = self.fetch_project(args, must_exist=True, include_children=False)
        user = self.remote_store.lookup_or_register_user_by_email_or_username(email, username)
        self.remote_store.set_user_project_permission(project, user, auth_role)
        print(u'Gave user {} {} permissions for project {}.'.format(user.full_name, auth_role, project.name))


Instruction:
Can you tell what is the following Python 3 function doing
def run(self, args):
        email = args.email                # email of person to remove permissions from (None if username specified)
        username = args.username          # username of person to remove permissions from (None if email is specified)
        project = self.fetch_project(args, must_exist=True, include_children=False)
        user = self.remote_store.lookup_or_register_user_by_email_or_username(email, username)
        self.remote_store.revoke_user_project_permission(project, user)
        print(u'Removed permissions from user {} for project {}.'.format(user.full_name, project.name))

Response: Remove permissions from the user with user_full_name or email on the remote project with project_name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def run(self, args):
        email = args.email                  # email of person to send email to
        username = args.username            # username of person to send email to, will be None if email is specified
        force_send = args.resend            # is this a resend so we should force sending
        auth_role = args.auth_role          # authorization role(project permissions) to give to the user
        msg_file = args.msg_file            # message file who's contents will be sent with the share
        message = read_argument_file_contents(msg_file)
        print("Sharing project.")
        to_user = self.remote_store.lookup_or_register_user_by_email_or_username(email, username)
        try:
            project = self.fetch_project(args, must_exist=True, include_children=False)
            dest_email = self.service.share(project, to_user, force_send, auth_role, message)
            print("Share email message sent to " + dest_email)
        except D4S2Error as ex:
            if ex.warning:
                print(ex.message)
            else:
                raise

Response: Share a project with the remote store.


Instruction:
How would you implement a function in Python 3 that
begins process that will transfer the project to another user. Send delivery message to D4S2 service specifying a project and a user. When user accepts delivery they receive access and we lose admin privileges. :param args Namespace arguments parsed from the command line

Response:
def run(self, args):
        """
        Begins process that will transfer the project to another user.
        Send delivery message to D4S2 service specifying a project and a user.
        When user accepts delivery they receive access and we lose admin privileges.
        :param args Namespace arguments parsed from the command line
        """
        email = args.email                  # email of person to deliver to, will be None if username is specified
        username = args.username            # username of person to deliver to, will be None if email is specified
        copy_project = args.copy_project    # should we deliver a copy of the project
        force_send = args.resend            # is this a resend so we should force sending
        msg_file = args.msg_file            # message file who's contents will be sent with the delivery
        share_usernames = args.share_usernames  # usernames who will have this project shared once it is accepted
        share_emails = args.share_emails    # emails of users who will have this project shared once it is accepted
        message = read_argument_file_contents(msg_file)
        project = self.fetch_project(args, must_exist=True, include_children=False)
        share_users = self.make_user_list(share_emails, share_usernames)
        print("Delivering project.")
        new_project_name = None
        if copy_project:
            new_project_name = self.get_new_project_name(project.name)
        to_user = self.remote_store.lookup_or_register_user_by_email_or_username(email, username)
        try:
            path_filter = PathFilter(args.include_paths, args.exclude_paths)
            dest_email = self.service.deliver(project, new_project_name, to_user, share_users,
                                              force_send, path_filter, message)
            print("Delivery email message sent to " + dest_email)
        except D4S2Error as ex:
            if ex.warning:
                print(ex.message)
            else:
                raise


Instruction:
How would you implement a function in Python 3 that
returns a unique project name for the copy.

Response:
def get_new_project_name(self, project_name):
        """
        Return a unique project name for the copy.
        :param project_name: str: name of project we will copy
        :return: str
        """
        timestamp_str = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M')
        return "{} {}".format(project_name, timestamp_str)


Instruction:
How would you implement a function in Python 3 that
lists project names. :param args Namespace arguments parsed from the command line

Response:
def run(self, args):
        """
        Lists project names.
        :param args Namespace arguments parsed from the command line
        """
        long_format = args.long_format
        # project_name and auth_role args are mutually exclusive
        if args.project_name or args.project_id:
            project = self.fetch_project(args, must_exist=True, include_children=True)
            self.print_project_details(project, long_format)
        else:
            self.print_project_list_details(args.auth_role, long_format)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def print_project_list_details(self, filter_auth_role, long_format):
        if filter_auth_role:
            projects_details = self.remote_store.get_projects_with_auth_role(auth_role=filter_auth_role)
        else:
            projects_details = self.remote_store.get_projects_details()
        if projects_details:
            for projects_detail in projects_details:
                print(self.get_project_info_line(projects_detail, long_format))
        else:
            print(NO_PROJECTS_FOUND_MESSAGE)

Response: Prints the list of projects and their details to stdout.


Instruction:
Explain what the following Python 3 code does
def run(self, args):
        project = self.fetch_project(args, must_exist=True, include_children=False)
        if not args.force:
            delete_prompt = "Are you sure you wish to delete {} (y/n)?".format(project.name)
            if not boolean_input_prompt(delete_prompt):
                return
        self.remote_store.delete_project(self.create_project_name_or_id_from_args(args))

Response: Delete a single project specified by project_name or id in args.


Instruction:
Write a Python 3 script to
print out non deprecated project - type auth roles.

Response:
def run(self, args):
        """
        Prints out non deprecated project-type auth roles.
        :param args Namespace arguments parsed from the command line
        """
        auth_roles = self.remote_store.get_active_auth_roles(RemoteAuthRole.PROJECT_CONTEXT)
        if auth_roles:
            for auth_role in auth_roles:
                print(auth_role.id, "-", auth_role.description)
        else:
            print("No authorization roles found.")


Instruction:
Can you implement a function in Python 3 that
retrieves a list of CA certificates at either the TXAWS_CERTS_PATH or the TXAWS_CERTS_PATH environment variable.

Response:
def get_ca_certs(environ=os.environ):
    """
    Retrieve a list of CAs at either the DEFAULT_CERTS_PATH or the env
    override, TXAWS_CERTS_PATH.

    In order to find .pem files, this function checks first for presence of the
    TXAWS_CERTS_PATH environment variable that should point to a directory
    containing cert files. In the absense of this variable, the module-level
    DEFAULT_CERTS_PATH will be used instead.

    Note that both of these variables have have multiple paths in them, just
    like the familiar PATH environment variable (separated by colons).
    """
    cert_paths = environ.get("TXAWS_CERTS_PATH", DEFAULT_CERTS_PATH).split(":")
    certificate_authority_map = {}
    for path in cert_paths:
        if not path:
            continue
        for cert_file_name in glob(os.path.join(path, "*.pem")):
            # There might be some dead symlinks in there, so let's make sure
            # it's real.
            if not os.path.exists(cert_file_name):
                continue
            cert_file = open(cert_file_name)
            data = cert_file.read()
            cert_file.close()
            x509 = load_certificate(FILETYPE_PEM, data)
            digest = x509.digest("sha1")
            # Now, de-duplicate in case the same cert has multiple names.
            certificate_authority_map[digest] = x509
    values = certificate_authority_map.values()
    if len(values) == 0:
        raise exception.CertsNotFoundError("Could not find any .pem files.")
    return values


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _calculate(self, startingPercentage, endPercentage, startDate, endDate):
        # get the defined subset of error values
        errorValues = self._get_error_values(startingPercentage, endPercentage, startDate, endDate)
        errorValues = filter(lambda item: item is None, errorValues)

        if errorValues[0] is None:
            return 1.0

        share = 1.0 / float(len(errorValues))
        product = 1.0

        for errorValue in errorValues:
            # never multiply with zero!
            if 0 == errorValue:
                continue

            product *= errorValue**share

        return product

Response: This method is used to calculate the error of the current language.


Instruction:
Can you tell what is the following Python 3 function doing
def iso8601time(time_tuple):
    if time_tuple:
        return time.strftime("%Y-%m-%dT%H:%M:%SZ", time_tuple)
    else:
        return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

Response: Format time_tuple as an ISO8601 time string.


Instruction:
Can you generate the documentation for the following Python 3 function
def parse(url, defaultPort=True):
    url = url.strip()
    parsed = urlparse(url)
    scheme = parsed[0]
    path = urlunparse(("", "") + parsed[2:])
    host = parsed[1]

    if ":" in host:
        host, port = host.split(":")
        try:
            port = int(port)
        except ValueError:
            # A non-numeric port was given, it will be replaced with
            # an appropriate default value if defaultPort is True
            port = None
    else:
            port = None

    if port is None and defaultPort:
        if scheme == "https":
            port = 443
        else:
            port = 80

    if path == "":
        path = "/"
    return (str(scheme), str(host), port, str(path))

Response: Parses the given URL into scheme host port and path.


Instruction:
Implement a Python 3 function for
getting the entire text content as str

Response:
def tostring(self, inject):
        """Get the entire text content as str"""
        return inject(self, '\n'.join(document.tostring(inject) for document in self.documents))


Instruction:
Make a summary of the following Python 3 code
def retry_when_service_down(func):
    def retry_function(*args, **kwds):
        showed_status_msg = False
        status_watcher = args[0]
        while True:
            try:
                result = func(*args, **kwds)
                if showed_status_msg:
                    status_watcher.set_status_message('')
                return result
            except DataServiceError as dse:
                if dse.status_code == 503:
                    if not showed_status_msg:
                        message = SERVICE_DOWN_MESSAGE.format(datetime.datetime.utcnow())
                        status_watcher.set_status_message(message)
                        showed_status_msg = True
                    time.sleep(SERVICE_DOWN_RETRY_SECONDS)
                else:
                    raise
    return retry_function

Response:
Decorator that will retry a function while it fails with status 503
Insights


Instruction:
Create a Python 3 function for
running func until the resource is consistent.

Response:
def retry_until_resource_is_consistent(func, monitor):
    """
    Runs func, if func raises DSResourceNotConsistentError will retry func indefinitely.
    Notifies monitor if we have to wait(only happens if DukeDS API raises DSResourceNotConsistentError.
    :param func: func(): function to run
    :param monitor: object: has start_waiting() and done_waiting() methods when waiting for non-consistent resource
    :return: whatever func returns
    """
    waiting = False
    while True:
        try:
            resp = func()
            if waiting and monitor:
                monitor.done_waiting()
            return resp
        except DSResourceNotConsistentError:
            if not waiting and monitor:
                monitor.start_waiting()
                waiting = True
            time.sleep(RESOURCE_NOT_CONSISTENT_RETRY_SECONDS)


Instruction:
Make a summary of the following Python 3 code
def get_auth(self):
        if self.legacy_auth():
            return self._auth
        if not self.auth_expired():
            return self._auth
        self.claim_new_token()
        return self._auth

Response: Gets an active authentication token refreshing it if necessary.


Instruction:
Can you create a Python 3 function that
updates internal state to have a new token using a no authorization data service.

Response:
def claim_new_token(self):
        """
        Update internal state to have a new token using a no authorization data service.
        """
        # Intentionally doing this manually so we don't have a chicken and egg problem with DataServiceApi.
        headers = {
            'Content-Type': ContentType.json,
            'User-Agent': self.user_agent_str,
        }
        data = {
            "agent_key": self.config.agent_key,
            "user_key": self.config.user_key,
        }
        url_suffix = "/software_agents/api_token"
        url = self.config.url + url_suffix
        response = requests.post(url, headers=headers, data=json.dumps(data))
        if response.status_code == 404:
            if not self.config.agent_key:
                raise MissingInitialSetupError()
            else:
                raise SoftwareAgentNotFoundError()
        elif response.status_code == 503:
            raise DataServiceError(response, url_suffix, data)
        elif response.status_code != 201:
            raise AuthTokenCreationError(response)
        resp_json = response.json()
        self._auth = resp_json['api_token']
        self._expires = resp_json['expires_on']


Instruction:
Can you tell what is the following Python 3 function doing
def auth_expired(self):
        if self._auth and self._expires:
            now_with_skew = time.time() + AUTH_TOKEN_CLOCK_SKEW_MAX
            return now_with_skew > self._expires
        return True

Response: Returns True if the current token has expired.


Instruction:
Explain what the following Python 3 code does
def _url_parts(self, url_suffix, data, content_type):
        url = self.base_url + url_suffix
        send_data = data
        if content_type == ContentType.json:
            send_data = json.dumps(data)
        headers = {
            'Content-Type': content_type,
            'User-Agent': self.user_agent_str,
        }
        if self.auth:
            headers['Authorization'] = self.auth.get_auth()
        return url, send_data, headers

Response: Format the url data based on config_type.


Instruction:
Can you tell what is the following Python 3 function doing
def _post(self, url_suffix, data, content_type=ContentType.json):
        (url, data_str, headers) = self._url_parts(url_suffix, data, content_type=content_type)
        resp = self.http.post(url, data_str, headers=headers)
        return self._check_err(resp, url_suffix, data, allow_pagination=False)

Response: Send a POST request to API at url_suffix with post_data. Raises error if x - total - pages is contained in the response.


Instruction:
Implement a Python 3 function for
sending PUT request to API at url_suffix with post_data.

Response:
def _put(self, url_suffix, data, content_type=ContentType.json):
        """
        Send PUT request to API at url_suffix with post_data.
        Raises error if x-total-pages is contained in the response.
        :param url_suffix: str URL path we are sending a PUT to
        :param data: object data we are sending
        :param content_type: str from ContentType that determines how we format the data
        :return: requests.Response containing the result
        """
        (url, data_str, headers) = self._url_parts(url_suffix, data, content_type=content_type)
        resp = self.http.put(url, data_str, headers=headers)
        return self._check_err(resp, url_suffix, data, allow_pagination=False)


Instruction:
Explain what the following Python 3 code does
def _get_single_item(self, url_suffix, data, content_type=ContentType.json):
        (url, data_str, headers) = self._url_parts(url_suffix, data, content_type=content_type)
        resp = self.http.get(url, headers=headers, params=data_str)
        return self._check_err(resp, url_suffix, data, allow_pagination=False)

Response: Send a GET request to API at url_suffix with post_data.


Instruction:
How would you explain what the following Python 3 function does
def _get_single_page(self, url_suffix, data, page_num):
        data_with_per_page = dict(data)
        data_with_per_page['page'] = page_num
        data_with_per_page['per_page'] = self._get_page_size()
        (url, data_str, headers) = self._url_parts(url_suffix, data_with_per_page,
                                                   content_type=ContentType.form)
        resp = self.http.get(url, headers=headers, params=data_str)
        return self._check_err(resp, url_suffix, data, allow_pagination=True)

Response: Send a GET request to API at url_suffix with post_data adding page and per_page parameters to retrieve a single page.


Instruction:
Given the following Python 3 function, write the documentation
def _get_collection(self, url_suffix, data):
        response = self._get_single_page(url_suffix, data, page_num=1)
        total_pages_str = response.headers.get('x-total-pages')
        if total_pages_str:
            total_pages = int(total_pages_str)
            if total_pages > 1:
                multi_response = MultiJSONResponse(base_response=response, merge_array_field_name="results")
                for page in range(2, total_pages + 1):
                    additional_response = self._get_single_page(url_suffix, data, page_num=page)
                    multi_response.add_response(additional_response)
                return multi_response
        return response

Response: Performs a GET for all pages based on x - total - pages in first response headers.


Instruction:
Given the following Python 3 function, write the documentation
def _check_err(resp, url_suffix, data, allow_pagination):
        total_pages = resp.headers.get('x-total-pages')
        if not allow_pagination and total_pages:
            raise UnexpectedPagingReceivedError()
        if 200 <= resp.status_code < 300:
            return resp
        if resp.status_code == 404:
            if resp.json().get("code") == "resource_not_consistent":
                raise DSResourceNotConsistentError(resp, url_suffix, data)
        raise DataServiceError(resp, url_suffix, data)

Response: Raise DataServiceError if the response was not successful.


Instruction:
Create a Python 3 function to
create a new project with the specified name and description.

Response:
def create_project(self, project_name, desc):
        """
        Send POST to /projects creating a new project with the specified name and desc.
        Raises DataServiceError on error.
        :param project_name: str name of the project
        :param desc: str description of the project
        :return: requests.Response containing the successful result
        """
        data = {
            "name": project_name,
            "description": desc
        }
        return self._post("/projects", data)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_folder(self, folder_name, parent_kind_str, parent_uuid):
        data = {
            'name': folder_name,
            'parent': {
                'kind': parent_kind_str,
                'id': parent_uuid
            }
        }
        return self._post("/folders", data)

Response: Create a new folder with specified name and parent.


Instruction:
Make a summary of the following Python 3 code
def get_project_children(self, project_id, name_contains, exclude_response_fields=None):
        return self._get_children('projects', project_id, name_contains, exclude_response_fields)

Response: Get the children of a project


Instruction:
Make a summary of the following Python 3 code
def _get_children(self, parent_name, parent_id, name_contains, exclude_response_fields=None):
        data = {}
        if name_contains is not None:
            data['name_contains'] = name_contains
        if exclude_response_fields:
            data['exclude_response_fields'] = ' '.join(exclude_response_fields)
        url_prefix = "/{}/{}/children".format(parent_name, parent_id)
        return self._get_collection(url_prefix, data)

Response: Send GET message to / projects or folders or files or folders to fetch info about children


Instruction:
Explain what the following Python 3 code does
def create_upload(self, project_id, filename, content_type, size,
                      hash_value, hash_alg, storage_provider_id=None, chunked=True):
        """
        Post to /projects/{project_id}/uploads to create a uuid for uploading chunks.
        NOTE: The optional hash_value and hash_alg parameters are being removed from the DukeDS API.
        :param project_id: str uuid of the project we are uploading data for.
        :param filename: str name of the file we want to upload
        :param content_type: str mime type of the file
        :param size: int size of the file in bytes
        :param hash_value: str hash value of the entire file
        :param hash_alg: str algorithm used to create hash_value
        :param storage_provider_id: str optional storage provider id
        :param chunked: is the uploaded file made up of multiple chunks. When False a single upload url is returned.
            For more see https://github.com/Duke-Translational-Bioinformatics/duke-data-service/blob/develop/api_design/DDS-1182-nonchucked_upload_api_design.md
        :return: requests.Response containing the successful result
        """
        data = {
            "name": filename,
            "content_type": content_type,
            "size": size,
            "hash": {
                "value": hash_value,
                "algorithm": hash_alg
            },
            "chunked": chunked,
        }
        if storage_provider_id:
            data['storage_provider'] = {'id': storage_provider_id}
        return self._post("/projects/" + project_id + "/uploads", data)

Response: Create a new upload for a specific file in the project.


Instruction:
Given the following Python 3 function, write the documentation
def create_upload_url(self, upload_id, number, size, hash_value, hash_alg):
        if number < 1:
            raise ValueError("Chunk number must be > 0")
        data = {
            "number": number,
            "size": size,
            "hash": {
                "value": hash_value,
                "algorithm": hash_alg
            }
        }
        return self._put("/uploads/" + upload_id + "/chunks", data)

Response: Given an upload created by create_upload retrieve a url where we can upload a chunk.


Instruction:
Write a Python 3 function for
marking the upload we created in create_upload complete.

Response:
def complete_upload(self, upload_id, hash_value, hash_alg):
        """
        Mark the upload we created in create_upload complete.
        :param upload_id: str uuid of the upload to complete.
        :param hash_value: str hash value of chunk
        :param hash_alg: str algorithm used to create hash
        :return: requests.Response containing the successful result
        """
        data = {
            "hash[value]": hash_value,
            "hash[algorithm]": hash_alg
        }
        return self._put("/uploads/" + upload_id + "/complete", data, content_type=ContentType.form)


Instruction:
Write a Python 3 script for
creating a new file after completing an upload.

Response:
def create_file(self, parent_kind, parent_id, upload_id):
        """
        Create a new file after completing an upload.
        :param parent_kind: str kind of parent(dds-folder,dds-project)
        :param parent_id: str uuid of parent
        :param upload_id: str uuid of complete upload
        :return: requests.Response containing the successful result
        """
        data = {
            "parent": {
                "kind": parent_kind,
                "id": parent_id
            },
            "upload": {
                "id": upload_id
            }
        }
        return self._post("/files/", data)


Instruction:
Given the following Python 3 function, write the documentation
def update_file(self, file_id, upload_id):
        put_data = {
            "upload[id]": upload_id,
        }
        return self._put("/files/" + file_id, put_data, content_type=ContentType.form)

Response: Send PUT request to the files endpoint to update the contents of the file to upload_id and sets a label.


Instruction:
Explain what the following Python 3 code does
def send_external(self, http_verb, host, url, http_headers, chunk):
        if http_verb == 'PUT':
            return self.http.put(host + url, data=chunk, headers=http_headers)
        elif http_verb == 'POST':
            return self.http.post(host + url, data=chunk, headers=http_headers)
        else:
            raise ValueError("Unsupported http_verb:" + http_verb)

Response: Send a chunk to an external object store.


Instruction:
Can you implement a function in Python 3 that
retrieves a streaming request for a file.

Response:
def receive_external(self, http_verb, host, url, http_headers):
        """
        Retrieve a streaming request for a file.
        :param http_verb: str GET is only supported right now
        :param host: str host we are requesting the file from
        :param url: str url to ask the host for
        :param http_headers: object headers to send with the request
        :return: requests.Response containing the successful result
        """
        if http_verb == 'GET':
            return self.http.get(host + url, headers=http_headers, stream=True)
        else:
            raise ValueError("Unsupported http_verb:" + http_verb)


Instruction:
Create a Python 3 function for
sending GET request to / users with optional full_name email and username filtering.

Response:
def get_users(self, full_name=None, email=None, username=None):
        """
        Send GET request to /users for users with optional full_name, email, and/or username filtering.
        :param full_name: str name of the user we are searching for
        :param email: str: optional email to filter by
        :param username: str: optional username to filter by
        :return: requests.Response containing the successful result
        """
        data = {}
        if full_name:
            data['full_name_contains'] = full_name
        if email:
            data['email'] = email
        if username:
            data['username'] = username
        return self._get_collection('/users', data)


Instruction:
Explain what the following Python 3 code does
def set_user_project_permission(self, project_id, user_id, auth_role):
        put_data = {
            "auth_role[id]": auth_role
        }
        return self._put("/projects/" + project_id + "/permissions/" + user_id, put_data,
                         content_type=ContentType.form)

Response: Set the permission for a user in a project.


Instruction:
Make a summary of the following Python 3 code
def get_api_token(self, agent_key, user_key):
        data = {
            "agent_key": agent_key,
            "user_key": user_key,
        }
        return self._post("/software_agents/api_token", data)

Response: Send POST request to get an auth token.


Instruction:
Can you create a Python 3 function that
sends POST request to initiate transfer of a project to the specified user ids

Response:
def create_project_transfer(self, project_id, to_user_ids):
        """
        Send POST request to initiate transfer of a project to the specified user ids
        :param project_id: str uuid of the project
        :param to_users: list of user uuids to receive the project
        :return: requests.Response containing the successful result
        """
        data = {
            "to_users[][id]": to_user_ids,
        }
        return self._post("/projects/" + project_id + "/transfers", data,
                          content_type=ContentType.form)


Instruction:
Write a Python 3 script to
send PUT request to one of the project transfer endpoints

Response:
def _process_project_transfer(self, action, transfer_id, status_comment):
        """
        Send PUT request to one of the project transfer action endpoints
        :param action: str name of the action (reject/accept/cancel)
        :param transfer_id: str uuid of the project_transfer
        :param status_comment: str comment about the action, optional
        :return: requests.Response containing the successful result
        """
        data = {}
        if status_comment:
            data["status_comment"] = status_comment
        path = "/project_transfers/{}/{}".format(transfer_id, action)
        return self._put(path, data, content_type=ContentType.form)


Instruction:
Can you write a function in Python 3 where it
creates a new activity with the specified name and description.

Response:
def create_activity(self, activity_name, desc=None, started_on=None, ended_on=None):
        """
        Send POST to /activities creating a new activity with the specified name and desc.
        Raises DataServiceError on error.
        :param activity_name: str name of the activity
        :param desc: str description of the activity (optional)
        :param started_on: str datetime when the activity started (optional)
        :param ended_on: str datetime when the activity ended (optional)
        :return: requests.Response containing the successful result
        """
        data = {
            "name": activity_name,
            "description": desc,
            "started_on": started_on,
            "ended_on": ended_on
        }
        return self._post("/activities", data)


Instruction:
Make a summary of the following Python 3 code
def update_activity(self, activity_id, activity_name=None, desc=None,
                        started_on=None, ended_on=None):
        """
        Send PUT request to /activities/{activity_id} to update the activity metadata.
        Raises ValueError if at least one field is not updated.
        :param activity_id: str uuid of activity
        :param activity_name: str new name of the activity (optional)
        :param desc: str description of the activity (optional)
        :param started_on: str date the updated activity began on (optional)
        :param ended_on: str date the updated activity ended on (optional)
        :return: requests.Response containing the successful result
        """
        put_data = {
            "name": activity_name,
            "description": desc,
            "started_on": started_on,
            "ended_on": ended_on
        }
        return self._put("/activities/" + activity_id, put_data)

Response: Update the metadata of an activity.


Instruction:
Implement a function in Python 3 to
create a was used by relationship between an activity and an entity.

Response:
def create_used_relation(self, activity_id, entity_kind, entity_id):
        """
        Create a was used by relationship between an activity and a entity(file).
        :param activity_id: str: uuid of the activity
        :param entity_kind: str: kind of entity('dds-file')
        :param entity_id: str: uuid of the entity
        :return: requests.Response containing the successful result
        """
        return self._create_activity_relation(activity_id, entity_kind, entity_id, ActivityRelationTypes.USED)


Instruction:
Can you create a Python 3 function that
creates a was generated by relationship between an activity and an entity.

Response:
def create_was_generated_by_relation(self, activity_id, entity_kind, entity_id):
        """
        Create a was generated by relationship between an activity and a entity(file).
        :param activity_id: str: uuid of the activity
        :param entity_kind: str: kind of entity('dds-file')
        :param entity_id: str: uuid of the entity
        :return: requests.Response containing the successful result
        """
        return self._create_activity_relation(activity_id, entity_kind, entity_id, ActivityRelationTypes.WAS_GENERATED_BY)


Instruction:
Make a summary of the following Python 3 code
def create_was_invalidated_by_relation(self, activity_id, entity_kind, entity_id):
        return self._create_activity_relation(activity_id, entity_kind, entity_id, ActivityRelationTypes.WAS_INVALIDATED_BY)

Response: Create a was invalidated by relationship between an activity and an entity.


Instruction:
Implement a Python 3 function for
creating a was derived from relation.

Response:
def create_was_derived_from_relation(self, used_entity_id, used_entity_kind,
                                         generated_entity_id, generated_entity_kind):
        """
        Create a was derived from relation.
        :param used_entity_id: str: uuid of the used entity (file_version_id)
        :param used_entity_kind: str: kind of entity ('dds-file')
        :param generated_entity_id: uuid of the generated entity (file_version_id)
        :param generated_entity_kind: str: kind of entity ('dds-file')
        :return: requests.Response containing the successful result
        """
        data = {
            "used_entity": {
                "id": used_entity_id,
                "kind": used_entity_kind
            },
            "generated_entity": {
                "id": generated_entity_id,
                "kind": generated_entity_kind
            }
        }
        return self._post("/relations/was_derived_from", data)


Instruction:
Make a summary of the following Python 3 code
def get_auth_provider_affiliates(self, auth_provider_id, full_name_contains=None, email=None, username=None):
        data = {}
        if full_name_contains:
            data['full_name_contains'] = full_name_contains
        if email:
            data['email'] = email
        if username:
            data['username'] = username
        return self._get_collection("/auth_providers/{}/affiliates/".format(auth_provider_id), data)

Response: Get the affiliates for a specific auth provider.


Instruction:
Explain what the following Python 3 code does
def auth_provider_add_user(self, auth_provider_id, username):
        url = "/auth_providers/{}/affiliates/{}/dds_user/".format(auth_provider_id, username)
        return self._post(url, {})

Response: Add a user to the set of user identities for the given auth provider.


Instruction:
Can you generate the documentation for the following Python 3 function
def add_response(self, response):
        key = self.merge_array_field_name
        response_json = response.json()
        value = self.combined_json[key]
        self.combined_json[self.merge_array_field_name] = value + response_json[key]

Response: Add data from json to data returned by json()


Instruction:
Make a summary of the following Python 3 code
def get_method(self, call, *args, **kwargs):
        method_class = self.registry.get(call.action, call.version)
        method = method_class(*args, **kwargs)
        if not method.is_available():
            raise APIError(400, "InvalidAction", "The action %s is not "
                           "valid for this web service." % call.action)
        else:
            return method

Response: Returns the method instance to invoke for the given L { Call.


Instruction:
How would you explain what the following Python 3 function does
def handle(self, request):
        request.id = str(uuid4())
        deferred = maybeDeferred(self._validate, request)
        deferred.addCallback(self.execute)

        def write_response(response):
            request.setHeader("Content-Length", str(len(response)))
            request.setHeader("Content-Type", self.content_type)
            # Prevent browsers from trying to guess a different content type.
            request.setHeader("X-Content-Type-Options", "nosniff")
            request.write(response)
            request.finish()
            return response

        def write_error(failure):
            if failure.check(APIError):
                status = failure.value.status

                # Don't log the stack traces for 4xx responses.
                if status < 400 or status >= 500:
                    log.err(failure)
                else:
                    log.msg("status: %s message: %s" % (
                        status, safe_str(failure.value)))

                body = failure.value.response
                if body is None:
                    body = self.dump_error(failure.value, request)
            else:
                # If the error is a generic one (not an APIError), log the
                # message , but don't send it back to the client, as it could
                # contain sensitive information. Send a generic server error
                # message instead.
                log.err(failure)
                body = "Server error"
                status = 500
            request.setResponseCode(status)
            write_response(body)

        deferred.addCallback(write_response)
        deferred.addErrback(write_error)
        return deferred

Response: Handle an HTTP request for executing an API call.


Instruction:
Write a Python 3 function for
executing an API L { Call.

Response:
def execute(self, call):
        """Execute an API L{Call}.

        At this point the request has been authenticated and C{call.principal}
        is set with the L{Principal} for the L{User} requesting the call.

        @return: The response to write in the request for the given L{Call}.
        @raises: An L{APIError} in case the execution fails, sporting an error
            message the HTTP status code to return.
        """
        method = self.get_method(call)
        deferred = maybeDeferred(self.authorize, method, call)
        deferred.addCallback(lambda _: method.invoke(call))
        return deferred.addCallback(self.dump_result)


Instruction:
Can you tell what is the following Python 3 function doing
def get_call_arguments(self, request):
        params = dict((k, v[-1]) for k, v in request.args.iteritems())
        args, rest = self.schema.extract(params)
        # Get rid of Signature so it doesn't mess with signature verification
        params.pop("Signature")
        result = {
            "transport_args": {
                "action": args.Action,
                "access_key_id": args.AWSAccessKeyId,
                "timestamp": args.Timestamp,
                "expires": args.Expires,
                "version": args.Version,
                "signature_method": args.SignatureMethod,
                "signature": args.Signature,
                "signature_version": args.SignatureVersion},
            "handler_args": rest,
            "raw_args": params
        }
        return result

Response: Get the arguments for the specified request.


Instruction:
Can you tell what is the following Python 3 function doing
def _validate(self, request):
        call_arguments = self.get_call_arguments(request)
        args = call_arguments["transport_args"]
        rest = call_arguments["handler_args"]
        params = call_arguments["raw_args"]

        self._validate_generic_parameters(args)

        def create_call(principal):
            self._validate_principal(principal, args)
            self._validate_signature(request, principal, args, params)
            return Call(raw_params=rest,
                        principal=principal,
                        action=args["action"],
                        version=args["version"],
                        id=request.id)

        deferred = maybeDeferred(self.get_principal, args["access_key_id"])
        deferred.addCallback(create_call)
        return deferred

Response: Validate an L{HTTPRequest before executing it.


Instruction:
Create a Python 3 function to
validate the generic request parameters.

Response:
def _validate_generic_parameters(self, args):
        """Validate the generic request parameters.

        @param args: Parsed schema arguments.
        @raises APIError: In the following cases:
            - Action is not included in C{self.actions}
            - SignatureVersion is not included in C{self.signature_versions}
            - Expires and Timestamp are present
            - Expires is before the current time
            - Timestamp is older than 15 minutes.
        """
        utc_now = self.get_utc_time()

        if getattr(self, "actions", None) is not None:
            # Check the deprecated 'actions' attribute
            if not args["action"] in self.actions:
                raise APIError(400, "InvalidAction", "The action %s is not "
                               "valid for this web service." % args["action"])
        else:
            self.registry.check(args["action"], args["version"])

        if not args["signature_version"] in self.signature_versions:
            raise APIError(403, "InvalidSignature", "SignatureVersion '%s' "
                           "not supported" % args["signature_version"])

        if args["expires"] and args["timestamp"]:
            raise APIError(400, "InvalidParameterCombination",
                           "The parameter Timestamp cannot be used with "
                           "the parameter Expires")
        if args["expires"] and args["expires"] < utc_now:
            raise APIError(400,
                           "RequestExpired",
                           "Request has expired. Expires date is %s" % (
                                args["expires"].strftime(self.time_format)))
        if (args["timestamp"]
            and args["timestamp"] + timedelta(minutes=15) < utc_now):
            raise APIError(400,
                           "RequestExpired",
                           "Request has expired. Timestamp date is %s" % (
                               args["timestamp"].strftime(self.time_format)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _validate_signature(self, request, principal, args, params):
        creds = AWSCredentials(principal.access_key, principal.secret_key)
        endpoint = AWSServiceEndpoint()
        endpoint.set_method(request.method)
        endpoint.set_canonical_host(request.getHeader("Host"))
        path = request.path
        if self.path is not None:
            path = "%s/%s" % (self.path.rstrip("/"), path.lstrip("/"))
        endpoint.set_path(path)
        signature = Signature(creds, endpoint, params,
                              signature_method=args["signature_method"],
                              signature_version=args["signature_version"]
                              )
        if signature.compute() != args["signature"]:
            raise APIError(403, "SignatureDoesNotMatch",
                           "The request signature we calculated does not "
                           "match the signature you provided. Check your "
                           "key and signing method.")

Response: Validate the signature of the request.


Instruction:
Make a summary of the following Python 3 code
def render_GET(self, request):
        if not request.args:
            request.setHeader("Content-Type", "text/plain")
            return self.get_status_text()
        else:
            self.handle(request)
            return NOT_DONE_YET

Response: Handle a GET request.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def getSignatureKey(key, dateStamp, regionName, serviceName):
    kDate = sign((b'AWS4' + key), dateStamp)
    kRegion = sign(kDate, regionName)
    kService = sign(kRegion, serviceName)
    kSigning = sign(kService, b'aws4_request')
    return kSigning

Response: Generate the signing key for AWS V4 requests.


Instruction:
Given the following Python 3 function, write the documentation
def _make_canonical_uri(parsed):
    path = urllib.quote(parsed.path)
    canonical_parsed = parsed._replace(path=path,
                                       params='', query='', fragment='')
    return urlparse.urlunparse(canonical_parsed)

Response: Returns the canonical URI for a parsed URL.


Instruction:
Can you generate the documentation for the following Python 3 function
def _make_canonical_query_string(parsed):
    query_params = urlparse.parse_qs(parsed.query, keep_blank_values=True)
    sorted_query_params = sorted((k, v)
                                 for k, vs in query_params.items()
                                 for v in vs)
    return urllib.urlencode(sorted_query_params)

Response: Returns the canonical query string for a parsed URL.


Instruction:
Can you tell what is the following Python 3 function doing
def _make_canonical_headers(headers, headers_to_sign):
    pairs = []
    for name in headers_to_sign:
        if name not in headers:
            continue
        values = headers[name]
        if not isinstance(values, (list, tuple)):
            values = [values]
        comma_values = b','.join(' '.join(line.strip().split())
                                 for value in values
                                 for line in value.splitlines())
        pairs.append((name.lower(), comma_values))

    sorted_pairs = sorted(b'%s:%s' % (name, value)
                          for name, value in sorted(pairs))
    return b'\n'.join(sorted_pairs) + b'\n'

Response: Return the canonicalized headers.


Instruction:
Here you have a function in Python 3, explain what it does
def _make_signed_headers(headers, headers_to_sign):
    return b";".join(header.lower() for header in sorted(headers_to_sign)
                     if header in headers)

Response: Return a semicolon - delimited list of headers to sign.