Instruction:
Explain what the following Python 3 code does
def run(self, app, host=None, port=None, **kwargs):
        if host is None:
            host = '127.0.0.1'
        if port is None:
            server_name = app.config['SERVER_NAME']
            if server_name and ':' in server_name:
                port = int(server_name.rsplit(':', 1)[1])
            else:
                port = 5000

        debug = kwargs.pop('debug', app.debug)
        log_output = kwargs.pop('log_output', debug)
        use_reloader = kwargs.pop('use_reloader', debug)
        extra_files = kwargs.pop('extra_files', None)

        app.debug = debug
        if app.debug and self.server.eio.async_mode != 'threading':
            # put the debug middleware between the SocketIO middleware
            # and the Flask application instance
            #
            #    mw1   mw2   mw3   Flask app
            #     o ---- o ---- o ---- o
            #    /
            #   o Flask-SocketIO
            #    \  middleware
            #     o
            #  Flask-SocketIO WebSocket handler
            #
            # BECOMES
            #
            #  dbg-mw   mw1   mw2   mw3   Flask app
            #     o ---- o ---- o ---- o ---- o
            #    /
            #   o Flask-SocketIO
            #    \  middleware
            #     o
            #  Flask-SocketIO WebSocket handler
            #
            self.sockio_mw.wsgi_app = DebuggedApplication(self.sockio_mw.wsgi_app,
                                                          evalex=True)

        if self.server.eio.async_mode == 'threading':
            from werkzeug._internal import _log
            _log('warning', 'WebSocket transport not available. Install '
                            'eventlet or gevent and gevent-websocket for '
                            'improved performance.')
            app.run(host=host, port=port, threaded=True,
                    use_reloader=use_reloader, **kwargs)
        elif self.server.eio.async_mode == 'eventlet':
            def run_server():
                import eventlet
                import eventlet.wsgi
                import eventlet.green
                addresses = eventlet.green.socket.getaddrinfo(host, port)
                if not addresses:
                    raise RuntimeError('Could not resolve host to a valid address')
                eventlet_socket = eventlet.listen(addresses[0][4], addresses[0][0])

                # If provided an SSL argument, use an SSL socket
                ssl_args = ['keyfile', 'certfile', 'server_side', 'cert_reqs',
                            'ssl_version', 'ca_certs',
                            'do_handshake_on_connect', 'suppress_ragged_eofs',
                            'ciphers']
                ssl_params = {k: kwargs[k] for k in kwargs if k in ssl_args}
                if len(ssl_params) > 0:
                    for k in ssl_params:
                        kwargs.pop(k)
                    ssl_params['server_side'] = True  # Listening requires true
                    eventlet_socket = eventlet.wrap_ssl(eventlet_socket,
                                                        **ssl_params)

                eventlet.wsgi.server(eventlet_socket, app,
                                     log_output=log_output, **kwargs)

            if use_reloader:
                run_with_reloader(run_server, extra_files=extra_files)
            else:
                run_server()
        elif self.server.eio.async_mode == 'gevent':
            from gevent import pywsgi
            try:
                from geventwebsocket.handler import WebSocketHandler
                websocket = True
            except ImportError:
                websocket = False

            log = 'default'
            if not log_output:
                log = None
            if websocket:
                self.wsgi_server = pywsgi.WSGIServer(
                    (host, port), app, handler_class=WebSocketHandler,
                    log=log, **kwargs)
            else:
                self.wsgi_server = pywsgi.WSGIServer((host, port), app,
                                                     log=log, **kwargs)

            if use_reloader:
                # monkey patching is required by the reloader
                from gevent import monkey
                monkey.patch_all()

                def run_server():
                    self.wsgi_server.serve_forever()

                run_with_reloader(run_server, extra_files=extra_files)
            else:
                self.wsgi_server.serve_forever()

Response: This method runs the SocketIO web server.


Instruction:
Can you create a Python 3 function that
stops a running SocketIO web server.

Response:
def stop(self):
        """Stop a running SocketIO web server.

        This method must be called from a HTTP or SocketIO handler function.
        """
        if self.server.eio.async_mode == 'threading':
            func = flask.request.environ.get('werkzeug.server.shutdown')
            if func:
                func()
            else:
                raise RuntimeError('Cannot stop unknown web server')
        elif self.server.eio.async_mode == 'eventlet':
            raise SystemExit
        elif self.server.eio.async_mode == 'gevent':
            self.wsgi_server.stop()


Instruction:
Given the following Python 3 function, write the documentation
def start_background_task(self, target, *args, **kwargs):
        return self.server.start_background_task(target, *args, **kwargs)

Response: Start a background task using the appropriate async model.


Instruction:
Write a Python 3 function that can
dispatch an event to the proper handler method.

Response:
def trigger_event(self, event, *args):
        """Dispatch an event to the proper handler method.

        In the most common usage, this method is not overloaded by subclasses,
        as it performs the routing of events to methods. However, this
        method can be overriden if special dispatching rules are needed, or if
        having a single method that catches all events is desired.
        """
        handler_name = 'on_' + event
        if not hasattr(self, handler_name):
            # there is no handler for this event, so we ignore it
            return
        handler = getattr(self, handler_name)
        return self.socketio._handle_event(handler, event, self.namespace,
                                           *args)


Instruction:
Write a Python 3 script to
emit a custom event to one or more connected clients.

Response:
def emit(self, event, data=None, room=None, include_self=True,
             namespace=None, callback=None):
        """Emit a custom event to one or more connected clients."""
        return self.socketio.emit(event, data, room=room,
                                  include_self=include_self,
                                  namespace=namespace or self.namespace,
                                  callback=callback)


Instruction:
Explain what the following Python 3 code does
def send(self, data, room=None, include_self=True, namespace=None,
             callback=None):
        """Send a message to one or more connected clients."""
        return self.socketio.send(data, room=room, include_self=include_self,
                                  namespace=namespace or self.namespace,
                                  callback=callback)

Response: Send a message to one or more connected clients.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def background_thread():
    count = 0
    while True:
        socketio.sleep(10)
        count += 1
        socketio.emit('my_response',
                      {'data': 'Server generated event', 'count': count},
                      namespace='/test')

Response: Example of how to send server generated events to clients.


Instruction:
Write a Python 3 function for
updating query status and send email notification to a user

Response:
def on_failure(self, exc, task_id, args, kwargs, einfo):
        """Update query status and send email notification to a user"""
        super().on_failure(exc, task_id, args, kwargs, einfo)
        if isinstance(exc, OperationRunDoesNotExist):
            return
        self._operation_run.on_failure()


Instruction:
Can you generate the documentation for the following Python 3 function
def on_success(self, retval, task_id, args, kwargs):
        super().on_success(retval, task_id, args, kwargs)
        self._operation_run.on_success()

Response: Send email notification and a file if requested to do so by a user


Instruction:
Write a Python 3 function for
recording the event async.

Response:
def record_event(self, event: Event) -> None:
        """
        Record the event async.
        """
        from polyaxon.celery_api import celery_app
        from polyaxon.settings import EventsCeleryTasks

        if not event.ref_id:
            event.ref_id = self.get_ref_id()
        serialized_event = event.serialize(dumps=False,
                                           include_actor_name=True,
                                           include_instance_info=True)

        celery_app.send_task(EventsCeleryTasks.EVENTS_TRACK, kwargs={'event': serialized_event})
        celery_app.send_task(EventsCeleryTasks.EVENTS_LOG, kwargs={'event': serialized_event})
        celery_app.send_task(EventsCeleryTasks.EVENTS_NOTIFY, kwargs={'event': serialized_event})
        # We include the instance in the serialized event for executor
        serialized_event['instance'] = event.instance
        self.executor.record(event_type=event.event_type, event_data=serialized_event)


Instruction:
How would you code a function in Python 3 to
return the list of event types where use acted on an object.

Response:
def user_write_events(self) -> List[str]:
        """Return event types where use acted on an object.

        The write events are events with actions:
            * CREATED
            * UPDATED
            * DELETED
            * RESUMED
            * COPIED
            * CLONED
            * STOPPED
        """
        return [event_type for event_type, event in self.items if event.get_event_action()
                in event_actions.WRITE_ACTIONS]


Instruction:
Explain what the following Python 3 code does
def user_view_events(self) -> List[str]:
        return [event_type for event_type, event in self.items if event.get_event_action()
                == event_actions.VIEWED]

Response: Return event types where use viewed a main object.


Instruction:
Explain what the following Python 3 code does
def validate_new_email(email):
    email = normalize_email(email)
    User = get_user_model()  # noqa
    if User.objects.filter(email=email).exists():
        raise ValidationError('The given email address is already registered.')

Response: Validates a new email address by checking if it is already used by other users.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_project_pod_spec(volume_mounts,
                         volumes,
                         image,
                         command,
                         args,
                         ports,
                         env_vars=None,
                         env_from=None,
                         container_name=None,
                         resources=None,
                         node_selector=None,
                         affinity=None,
                         tolerations=None,
                         image_pull_policy=None,
                         restart_policy=None,
                         service_account_name=None):
    """Pod spec to be used to create pods for project: tensorboard, notebooks."""
    volume_mounts = to_list(volume_mounts, check_none=True)
    volumes = to_list(volumes, check_none=True)

    gpu_volume_mounts, gpu_volumes = get_gpu_volumes_def(resources)
    volume_mounts += gpu_volume_mounts
    volumes += gpu_volumes

    ports = [client.V1ContainerPort(container_port=port) for port in ports]

    pod_container = get_pod_container(
        volume_mounts=volume_mounts,
        image=image,
        command=command,
        args=args,
        ports=ports,
        env_vars=env_vars,
        env_from=env_from,
        container_name=container_name,
        resources=resources,
        image_pull_policy=image_pull_policy)
    containers = [pod_container]

    if service_account_name and not conf.get('K8S_RBAC_ENABLED'):
        service_account_name = None

    return client.V1PodSpec(restart_policy=restart_policy,
                            security_context=get_security_context(),
                            service_account_name=service_account_name,
                            containers=containers,
                            volumes=volumes,
                            node_selector=node_selector,
                            affinity=affinity,
                            tolerations=tolerations)

Response: Returns a pod spec for the project.


Instruction:
Can you tell what is the following Python 3 function doing
def get_object(self):
        if self._object:
            return self._object
        self._object = super().get_object()
        if not self.AUDITOR_EVENT_TYPES:
            return self._object
        method = self.request.method
        event_type = self.AUDITOR_EVENT_TYPES.get(method)
        if method == 'GET' and event_type and is_user(self.request.user):
            auditor.record(event_type=event_type,
                           instance=self._object,
                           actor_id=self.request.user.id,
                           actor_name=self.request.user.username)
        elif method == 'DELETE' and event_type and is_user(self.request.user):
            auditor.record(event_type=event_type,
                           instance=self._object,
                           actor_id=self.request.user.id,
                           actor_name=self.request.user.username)
        return self._object

Response: We memoize the access to this function in case a second call is made.


Instruction:
Explain what the following Python 3 code does
def initialize_context(self, request: HttpRequest, *args, **kwargs) -> None:
        for key in self.CONTEXT_KEYS:
            setattr(self, key, kwargs.get(key))
        for key in self.QUERY_CONTEXT_KEYS:
            setattr(self, key, request.query_params.get(key))
        self._initialize_context()
        self.validate_context()

Response: Initializes the endpoint with the passed context keys based on the passed query parameters and the passed request.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_shm_volumes():
    volumes, volume_mounts = [], []
    shm_volume = client.V1Volume(
        name=constants.SHM_VOLUME,
        empty_dir=client.V1EmptyDirVolumeSource(medium='Memory')
    )
    volumes.append(shm_volume)
    shm_volume_mount = client.V1VolumeMount(name=shm_volume.name, mount_path='/dev/shm')
    volume_mounts.append(shm_volume_mount)
    return volumes, volume_mounts

Response: Get the list of volumes and volumes_mounts for the current project.


Instruction:
Create a Python 3 function to
return a boolean indicating if the experiment has any running jobs

Response:
def has_running_jobs(self) -> bool:
        """"Return a boolean indicating if the experiment has any running jobs"""
        return self.jobs.exclude(status__status__in=ExperimentLifeCycle.DONE_STATUS).exists()


Instruction:
Can you write a function in Python 3 where it
gets the internal header of the request.

Response:
def get_internal_header(request: HttpRequest) -> str:
    """
    Return request's 'X_POLYAXON_INTERNAL:' header, as a bytestring.
    """
    return get_header(request=request, header_service=conf.get('HEADERS_INTERNAL'))


Instruction:
Implement a Python 3 function for
signaling handling for sidecars logs.

Response:
def logs_sidecars_experiments(experiment_name: str,
                              experiment_uuid: str,
                              job_uuid: str,
                              log_lines: Optional[Union[str, Iterable[str]]]) -> None:
    """Signal handling for sidecars logs."""
    handle_experiment_job_log(experiment_name=experiment_name,
                              experiment_uuid=experiment_uuid,
                              log_lines=log_lines)
    publisher.publish_experiment_job_log(
        log_lines=log_lines,
        experiment_uuid=experiment_uuid,
        experiment_name=experiment_name,
        job_uuid=job_uuid,
        send_task=False
    )


Instruction:
How would you implement a function in Python 3 that
signals handling for sidecars logs.

Response:
def logs_sidecars_jobs(job_uuid: str,
                       job_name: str,
                       log_lines: Optional[Union[str, Iterable[str]]]) -> None:
    """Signal handling for sidecars logs."""
    handle_job_logs(job_uuid=job_uuid,
                    job_name=job_name,
                    log_lines=log_lines)
    publisher.publish_job_log(
        log_lines=log_lines,
        job_uuid=job_uuid,
        job_name=job_name,
        send_task=False
    )


Instruction:
Write a Python 3 script to
signal handling for sidecars logs.

Response:
def logs_sidecars_builds(job_uuid: str,
                         job_name: str,
                         log_lines: Optional[Union[str, Iterable[str]]]) -> None:
    """Signal handling for sidecars logs."""
    handle_build_job_logs(job_uuid=job_uuid,
                          job_name=job_name,
                          log_lines=log_lines)
    publisher.publish_build_job_log(
        log_lines=log_lines,
        job_uuid=job_uuid,
        job_name=job_name,
        send_task=False
    )


Instruction:
Can you implement a function in Python 3 that
returns True if one task ran ( checked ).

Response:
def is_checked(self) -> bool:
        """One task ran (checked)."""
        if not self.redis_key_checked:
            return False

        value = self._red.get(self.redis_key_checked)
        if not value:
            return False

        return True


Instruction:
Write a Python 3 script to
return True if one task ran and one task has been delayed.

Response:
def is_delayed(self) -> bool:
        """One task ran (checked), and one task has been delayed."""
        if not self.redis_key_delayed:
            return False

        value = self._red.get(self.redis_key_delayed)
        if not value:
            return False

        return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def connect(self):
        _logger.info('Connecting to %s', self.AMQP_URL)
        while True:
            try:
                return adapters.AsyncioConnection(pika.URLParameters(self.AMQP_URL),
                                                  self.on_connection_open)
            except AMQPConnectionError:
                asyncio.sleep(1)

Response: This method connects to RabbitMQ returning the connection handle.


Instruction:
Can you tell what is the following Python 3 function doing
def setup_exchange(self, exchange_name):
        _logger.debug('Declaring exchange %s', exchange_name)
        self._channel.exchange_declare(self.on_exchange_declareok,
                                       exchange_name,
                                       self.EXCHANGE_TYPE,
                                       durable=True,
                                       passive=True)

Response:
Declare an exchange on RabbitMQ by invoking the Exchange. Declare RPC
        command.


Instruction:
How would you code a function in Python 3 to
declare a queue on RabbitMQ by invoking the Queue. Declare RPC command.

Response:
def setup_queue(self, queue_name):
        """Setup the queue on RabbitMQ by invoking the Queue.Declare RPC
        command. When it is complete, the on_queue_declareok method will
        be invoked by pika.

        :param str|unicode queue_name: The name of the queue to declare.
        """
        _logger.debug('Declaring queue %s', queue_name)
        self._channel.queue_declare(self.on_queue_declareok, queue_name)


Instruction:
Given the following Python 3 function, write the documentation
def on_queue_declareok(self, method_frame):
        _logger.info('Binding %s to %s with %s',
                     self.EXCHANGE, self._queue, self._routing_key)
        self._channel.queue_bind(self.on_bindok, self._queue,
                                 self.EXCHANGE, self._routing_key)

Response: Invoked by pika when the Queue. Declare RPC call made by pika to bind the queue to the exchange together with the routing key.


Instruction:
Implement a Python 3 function for
invoking by pika when a Basic. Cancel frame is received.

Response:
def on_consumer_cancelled(self, method_frame):
        """Invoked by pika when RabbitMQ sends a Basic.Cancel for a consumer
        receiving messages.

        :param pika.frame.Method method_frame: The Basic.Cancel frame
        """
        _logger.info('Consumer was cancelled remotely, shutting down: %r', method_frame)
        if self._channel:
            self._channel.close()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def acknowledge_message(self, delivery_tag):
        _logger.debug('Acknowledging message %s', delivery_tag)
        self._channel.basic_ack(delivery_tag)

Response: Acknowledge the delivery from RabbitMQ by sending Basic. Ack RPC method for the delivery tag.


Instruction:
Can you generate the documentation for the following Python 3 function
def on_message(self, unused_channel, basic_deliver, properties, body):
        _logger.debug('Received message # %s from %s: %s',
                      basic_deliver.delivery_tag, properties.app_id, body)
        if self.ws and body:
            self.messages.append(body)
        _logger.debug('out ws : %s', len(self.ws))
        _logger.debug('out messages : %s', len(self.messages))
        self.acknowledge_message(basic_deliver.delivery_tag)

Response: Invoked by pika when a message is delivered from RabbitMQ.


Instruction:
Can you create a Python 3 function that
tells RabbitMQ that you would like to stop consuming.

Response:
def stop_consuming(self):
        """Tell RabbitMQ that you would like to stop consuming by sending the
        Basic.Cancel RPC command.
        """
        if self._channel:
            _logger.debug('Sending a Basic.Cancel RPC command to RabbitMQ')
            self._channel.basic_cancel(self.on_cancelok, self._consumer_tag)


Instruction:
Create a Python 3 function for
opening a new channel with RabbitMQ by issuing the Channel. Open RPC command.

Response:
def open_channel(self):
        """Open a new channel with RabbitMQ by issuing the Channel.Open RPC
        command. When RabbitMQ responds that the channel is open, the
        on_channel_open callback will be invoked by pika.
        """
        _logger.info('Creating a new channel')
        self._connection.channel(on_open_callback=self.on_channel_open)


Instruction:
Explain what the following Python 3 code does
def run(self):
        self._connection = self.connect()
        self._connection.ioloop.start()

Response: Run the example consumer by connecting to RabbitMQ and then starting the IOLoop to block and allow the SelectConnection to operate.


Instruction:
Create a Python 3 function for
stopping the connection to RabbitMQ by stopping the consumer and closing the connection.

Response:
def stop(self):
        """Cleanly shutdown the connection to RabbitMQ by stopping the consumer
        with RabbitMQ. When RabbitMQ confirms the cancellation, on_cancelok
        will be invoked by pika, which will then closing the channel and
        connection. The IOLoop is started again because this method is invoked
        when CTRL-C is pressed raising a KeyboardInterrupt exception. This
        exception stops the IOLoop which needs to be running for pika to
        communicate with RabbitMQ. All of the commands issued prior to starting
        the IOLoop will be buffered but not processed.
        """
        _logger.debug('Stopping')
        self._closing = True
        self.stop_consuming()
        self._connection.ioloop.start()
        _logger.info('Stopped')


Instruction:
Write a Python 3 script for
rendering both the subject and body templates in the given context.

Response:
def render_mail_template(subject_template, body_template, context):
    """
    Renders both the subject and body templates in the given context.
    Returns a tuple (subject, body) of the result.
    """
    try:
        subject = strip_spaces(render_to_string(subject_template, context))
        body = render_to_string(body_template, context)
    finally:
        pass

    return subject, body


Instruction:
Here you have a function in Python 3, explain what it does
def send_mass_template_mail(subject_template, body_template, recipients, context=None):
    if context:
        subject, body = render_mail_template(subject_template, body_template, context)
    else:
        subject, body = subject_template, body_template

    message_tuples = [(subject, body, conf.get('DEFAULT_FROM_EMAIL'), [r]) for r in recipients]

    send_mass_mail(message_tuples)

Response: Sends an email using the given templates and context.


Instruction:
Write a Python 3 script for
creating a tar file based on the list of files passed

Response:
def create_tarfile(files: List[str], tar_path: str) -> None:
    """Create a tar file based on the list of files passed"""
    with tarfile.open(tar_path, "w:gz") as tar:
        for f in files:
            tar.add(f)


Instruction:
Write a Python 3 script to
open and close the broker channel.

Response:
def check() -> Result:
        """Open and close the broker channel."""
        try:
            # Context to release connection
            with Connection(conf.get('CELERY_BROKER_URL')) as conn:
                conn.connect()
        except ConnectionRefusedError:
            return Result(message='Service unable to connect, "Connection was refused".',
                          severity=Result.ERROR)

        except AccessRefused:
            return Result(message='Service unable to connect, "Authentication error".',
                          severity=Result.ERROR)

        except IOError:
            return Result(message='Service has an "IOError".', severity=Result.ERROR)

        except Exception as e:
            return Result(message='Service has an "{}" error.'.format(e), severity=Result.ERROR)
        else:
            return Result()


Instruction:
Here you have a function in Python 3, explain what it does
def get_suggestions(self, iteration_config=None):
        matrix = self.hptuning_config.matrix

        suggestions = []
        keys = list(matrix.keys())
        values = [v.to_numpy() for v in matrix.values()]
        for v in itertools.product(*values):
            suggestions.append(dict(zip(keys, v)))

        if self.hptuning_config.grid_search:
            n_suggestions = self.hptuning_config.grid_search.n_experiments
            if n_suggestions:
                return suggestions[:n_suggestions]
        return suggestions

Response: Returns a list of suggestions based on grid search.


Instruction:
Write a Python 3 function that can
find the argmax of the acquisition function.

Response:
def _maximize(self):
        """ Find argmax of the acquisition function."""
        if not self.space.is_observations_valid():
            return None
        y_max = self.space.y.max()
        self.utility_function.gaussian_process.fit(self.space.x, self.space.y)
        return self.utility_function.max_compute(y_max=y_max,
                                                 bounds=self.space.bounds,
                                                 n_warmup=self.n_warmup,
                                                 n_iter=self.n_iter)


Instruction:
Make a summary of the following Python 3 code
def dag(self) -> Tuple[Dict, Dict]:
        from pipelines import dags

        operations = self.operations.all().prefetch_related('downstream_operations')

        def get_downstream(op):
            return op.downstream_operations.values_list('id', flat=True)

        return dags.get_dag(operations, get_downstream)

Response: Construct the DAG of this pipeline based on the operations and their downstream.


Instruction:
Implement a Python 3 function for
calculating the countdown for a celery task retry.

Response:
def get_countdown(self, retries) -> int:
        """Calculate the countdown for a celery task retry."""
        retry_delay = self.retry_delay
        if self.retry_exponential_backoff:
            return min(
                max(2 ** retries, retry_delay),  # Exp. backoff
                self.max_retry_delay  # The countdown should be more the max allowed
            )
        return retry_delay


Instruction:
Implement a function in Python 3 to
return the params to run the celery task.

Response:
def get_run_params(self) -> Dict:
        """Return the params to run the celery task."""
        params = {}
        if self.celery_queue:
            params['queue'] = self.celery_queue

        if self.timeout:
            params['soft_time_limit'] = self.timeout
            # We set also a hard time limit that will send sig 9
            # This hard time limit should not happened, as it will set inconsistent state
            params['time_limit'] = self.timeout + settings.CELERY_HARD_TIME_LIMIT_DELAY

        if self.execute_at:
            params['eta'] = self.execute_at

        return params


Instruction:
Given the following Python 3 function, write the documentation
def can_transition(self, status_from: str, status_to: str) -> bool:
        if not self.STATUSES.can_transition(status_from=status_from, status_to=status_to):
            _logger.info(
                '`%s` tried to transition from status `%s` to non permitted status `%s`',
                str(self), status_from, status_to)
            return False

        return True

Response: Check if the current instance can be updated.


Instruction:
Can you write a function in Python 3 where it
constructs the DAG of this pipeline run based on the operation runs and their downstream runs.

Response:
def dag(self) -> Tuple[Dict, Dict]:
        """Construct the DAG of this pipeline run
        based on the its operation runs and their downstream.
        """
        from pipelines import dags

        operation_runs = self.operation_runs.all().prefetch_related('downstream_runs')

        def get_downstream(op_run):
            return op_run.downstream_runs.values_list('id', flat=True)

        return dags.get_dag(operation_runs, get_downstream)


Instruction:
Here you have a function in Python 3, explain what it does
def check_concurrency(self) -> bool:
        if not self.operation.concurrency:  # No concurrency set
            return True

        ops_count = self.operation.runs.filter(
            status__status__in=self.STATUSES.RUNNING_STATUS).count()
        return ops_count < self.operation.concurrency

Response: Checks the concurrency of the operation run.


Instruction:
Implement a function in Python 3 to
check the upstream and the trigger rule.

Response:
def check_upstream_trigger(self) -> bool:
        """Checks the upstream and the trigger rule."""
        if self.operation.trigger_policy == TriggerPolicy.ONE_DONE:
            return self.upstream_runs.filter(
                status__status__in=self.STATUSES.DONE_STATUS).exists()
        if self.operation.trigger_policy == TriggerPolicy.ONE_SUCCEEDED:
            return self.upstream_runs.filter(
                status__status=self.STATUSES.SUCCEEDED).exists()
        if self.operation.trigger_policy == TriggerPolicy.ONE_FAILED:
            return self.upstream_runs.filter(
                status__status=self.STATUSES.FAILED).exists()

        statuses = self.upstream_runs.values_list('status__status', flat=True)
        if self.operation.trigger_policy == TriggerPolicy.ALL_DONE:
            return not bool([True for status in statuses
                             if status not in self.STATUSES.DONE_STATUS])
        if self.operation.trigger_policy == TriggerPolicy.ALL_SUCCEEDED:
            return not bool([True for status in statuses
                             if status != self.STATUSES.SUCCEEDED])
        if self.operation.trigger_policy == TriggerPolicy.ALL_FAILED:
            return not bool([True for status in statuses
                             if status not in self.STATUSES.FAILED_STATUS])


Instruction:
Can you write a function in Python 3 where it
schedules the task: check first if the task can start: 1. we check that the task is still in the CREATED state. 2. we check that the upstream dependency is met. 3. we check that pipeline can start a new task; i.e. we check the concurrency of the pipeline. 4. we check that operation can start a new instance; i.e. we check the concurrency of the operation. -> If all checks pass we schedule the task start it. -> 1. If the operation is not in created status, nothing to do. -> 2. If the upstream dependency check is not met, two use cases need to be validated: * The upstream dependency is not met but could be met in the future, because some ops are still CREATED/SCHEDULED/RUNNING/... in this case nothing need to be done, every time an upstream operation finishes, it will notify all the downstream ops including this one. * The upstream dependency is not met and could not be met at all. In this case we need to mark the task with `UPSTREAM_FAILED`. -> 3. If the pipeline has reached it's concurrency limit, we just delay schedule based on the interval/time delay defined by the user. The pipeline scheduler will keep checking until the task can be scheduled or stopped. -> 4. If the operation has reached it's concurrency limit, Same as above we keep trying based on an interval defined by the user. Returns: boolean: Whether to try to schedule this operation run in the future or not.

Response:
def schedule_start(self) -> bool:
        """Schedule the task: check first if the task can start:
            1. we check that the task is still in the CREATED state.
            2. we check that the upstream dependency is met.
            3. we check that pipeline can start a new task;
              i.e. we check the concurrency of the pipeline.
            4. we check that operation can start a new instance;
              i.e. we check the concurrency of the operation.

        -> If all checks pass we schedule the task start it.

        -> 1. If the operation is not in created status, nothing to do.
        -> 2. If the upstream dependency check is not met, two use cases need to be validated:
            * The upstream dependency is not met but could be met in the future,
              because some ops are still CREATED/SCHEDULED/RUNNING/...
              in this case nothing need to be done, every time an upstream operation finishes,
              it will notify all the downstream ops including this one.
            * The upstream dependency is not met and could not be met at all.
              In this case we need to mark the task with `UPSTREAM_FAILED`.
        -> 3. If the pipeline has reached it's concurrency limit,
           we just delay schedule based on the interval/time delay defined by the user.
           The pipeline scheduler will keep checking until the task can be scheduled or stopped.
        -> 4. If the operation has reached it's concurrency limit,
           Same as above we keep trying based on an interval defined by the user.

        Returns:
            boolean: Whether to try to schedule this operation run in the future or not.
        """
        if self.last_status != self.STATUSES.CREATED:
            return False

        upstream_trigger_check = self.check_upstream_trigger()
        if not upstream_trigger_check and self.is_upstream_done:
            # This task cannot be scheduled anymore
            self.on_upstream_failed()
            return False

        if not self.pipeline_run.check_concurrency():
            return True

        if not self.check_concurrency():
            return True

        self.on_scheduled()
        self.start()
        return False


Instruction:
Can you implement a function in Python 3 that
starts the celery task of this operation.

Response:
def start(self) -> None:
        """Start the celery task of this operation."""
        kwargs = self.celery_task_context
        # Update we the operation run id
        kwargs['operation_run_id'] = self.id  # pylint:disable=unsupported-assignment-operation

        async_result = celery_app.send_task(
            self.operation.celery_task,
            kwargs=kwargs,
            **self.operation.get_run_params())
        self.celery_task_id = async_result.id
        self.save()


Instruction:
Create a Python 3 function for
returning the image name and image tag for an experiment

Response:
def get_experiment_image_info(experiment: 'Experiment') -> Tuple[str, str]:
    """Return the image name and image tag for an experiment"""
    project_name = experiment.project.name
    repo_name = project_name

    image_name = '{}/{}'.format(conf.get('REGISTRY_URI'), repo_name)
    image_tag = experiment.code_reference.commit
    return image_name, image_tag


Instruction:
Make a summary of the following Python 3 code
def get_job_image_info(project: 'Project', job: Any) -> Tuple[str, str]:
    project_name = project.name
    repo_name = project_name

    image_name = '{}/{}'.format(conf.get('REGISTRY_URI'), repo_name)
    try:
        last_commit = project.repo.last_commit
    except ValueError:
        raise ValueError('Repo was not found for project `{}`.'.format(project))
    return image_name, last_commit[0]

Response: Return the image name and image tag for a job


Instruction:
Here you have a function in Python 3, explain what it does
def get_notebook_image_info(project: 'Project', job: Any) -> Tuple[str, str]:
    image_name, _ = get_job_image_info(project, job)
    return image_name, LATEST_IMAGE_TAG

Response: Return the image name and image tag for a job


Instruction:
Implement a function in Python 3 to
create an iteration for the experiment group.

Response:
def create_iteration(self, num_suggestions):
        """Create an iteration for the experiment group."""
        from db.models.experiment_groups import ExperimentGroupIteration

        iteration_config = self.experiment_group.iteration_config

        if iteration_config is None:
            iteration = 0
            old_experiment_ids = None
            old_experiments_configs = None
            old_experiments_metrics = None
        else:
            iteration = iteration_config.iteration + 1
            old_experiment_ids = iteration_config.combined_experiment_ids
            old_experiments_configs = iteration_config.combined_experiments_configs
            old_experiments_metrics = iteration_config.combined_experiments_metrics

        # Create a new iteration config
        iteration_config = BOIterationConfig(
            iteration=iteration,
            num_suggestions=num_suggestions,
            old_experiment_ids=old_experiment_ids,
            old_experiments_configs=old_experiments_configs,
            old_experiments_metrics=old_experiments_metrics,
            experiment_ids=[],
            experiments_configs=[],
        )
        return ExperimentGroupIteration.objects.create(
            experiment_group=self.experiment_group,
            data=iteration_config.to_dict())


Instruction:
How would you explain what the following Python 3 function does
def update_iteration(self):
        iteration_config = self.get_iteration_config()
        if not iteration_config:
            return
        experiments_metrics = self.experiment_group.get_experiments_metrics(
            experiment_ids=iteration_config.experiment_ids,
            metric=self.get_metric_name()
        )
        experiments_configs = self.experiment_group.get_experiments_declarations(
            experiment_ids=iteration_config.experiment_ids
        )
        iteration_config.experiments_configs = list(experiments_configs)
        iteration_config.experiments_metrics = [m for m in experiments_metrics if m[1] is not None]
        self._update_config(iteration_config)

Response: Update the last experiment group s iteration with experiment performance.


Instruction:
Can you tell what is the following Python 3 function doing
def get_n_config_to_keep(self, n_suggestions, bracket_iteration):
        n_configs = n_suggestions * (self.eta ** -bracket_iteration)
        return int(n_configs / self.eta)

Response: Return the number of configs to keep and resume.


Instruction:
Explain what the following Python 3 code does
def get_n_config_to_keep_for_iteration(self, iteration, bracket_iteration):
        bracket = self.get_bracket(iteration=iteration)
        if bracket_iteration == bracket + 1:
            # End of loop `for bracket_iteration in range(bracket + 1):`
            return 0

        n_configs = self.get_n_configs(bracket=bracket)
        return self.get_n_config_to_keep(
            n_suggestions=n_configs, bracket_iteration=bracket_iteration)

Response: Return the number of configs to keep for an iteration and bracket.


Instruction:
Write a Python 3 script to
return the number of resources for this iteration

Response:
def get_n_resources_for_iteration(self, iteration, bracket_iteration):
        """Return the number of iterations to run for this barcket_i

        This is just util function around `get_n_resources`
        """
        bracket = self.get_bracket(iteration=iteration)
        n_resources = self.get_resources(bracket=bracket)
        return self.get_n_resources(n_resources=n_resources, bracket_iteration=bracket_iteration)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_suggestions(self, iteration_config=None):
        if not iteration_config or not isinstance(iteration_config, HyperbandIterationConfig):
            raise ValueError('Hyperband get suggestions requires an iteration.')
        bracket = self.get_bracket(iteration=iteration_config.iteration)
        n_configs = self.get_n_configs(bracket=bracket)
        n_resources = self.get_n_resources_for_iteration(
            iteration=iteration_config.iteration,
            bracket_iteration=iteration_config.bracket_iteration)
        n_resources = self.hptuning_config.hyperband.resource.cast_value(n_resources)
        suggestion_params = {
            self.hptuning_config.hyperband.resource.name: n_resources
        }
        return get_random_suggestions(matrix=self.hptuning_config.matrix,
                                      n_suggestions=n_configs,
                                      suggestion_params=suggestion_params,
                                      seed=self.hptuning_config.seed)

Response: Return a list of suggestions based on hyperband.


Instruction:
Given the following Python 3 function, write the documentation
def should_reschedule(self, iteration, bracket_iteration):
        bracket = self.get_bracket(iteration=iteration)
        if bracket_iteration < bracket:
            # The bracket is still processing
            return False

        # We can only reschedule if we can create a new bracket
        return self.get_bracket(iteration=iteration + 1) >= 0

Response: Return a boolean to indicate if we need to reschedule another iteration.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def should_reduce_configs(self, iteration, bracket_iteration):
        n_configs_to_keep = self.get_n_config_to_keep_for_iteration(
            iteration=iteration, bracket_iteration=bracket_iteration)
        return n_configs_to_keep > 0

Response: Return a boolean to indicate if we need to reschedule another bracket iteration.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_init_container(self,
                           init_command,
                           init_args,
                           env_vars,
                           context_mounts,
                           persistence_outputs,
                           persistence_data):
        """Pod init container for setting outputs path."""
        env_vars = to_list(env_vars, check_none=True)
        if self.original_name is not None and self.cloning_strategy == CloningStrategy.RESUME:
            return []
        if self.original_name is not None and self.cloning_strategy == CloningStrategy.COPY:
            command = InitCommands.COPY
            original_outputs_path = stores.get_experiment_outputs_path(
                persistence=persistence_outputs,
                experiment_name=self.original_name)
        else:
            command = InitCommands.CREATE
            original_outputs_path = None

        outputs_path = stores.get_experiment_outputs_path(
            persistence=persistence_outputs,
            experiment_name=self.experiment_name)
        _, outputs_volume_mount = get_pod_outputs_volume(persistence_outputs=persistence_outputs)
        volume_mounts = outputs_volume_mount + to_list(context_mounts, check_none=True)
        init_command = init_command or ["/bin/sh", "-c"]
        init_args = init_args or to_list(
            get_output_args(command=command,
                            outputs_path=outputs_path,
                            original_outputs_path=original_outputs_path))
        init_args += to_list(get_auth_context_args(entity='experiment',
                                                   entity_name=self.experiment_name))
        return [
            client.V1Container(
                name=self.init_container_name,
                image=self.init_docker_image,
                image_pull_policy=self.init_docker_image_pull_policy,
                command=init_command,
                args=[''.join(init_args)],
                env=env_vars,
                volume_mounts=volume_mounts)
        ]

Response: Get the init container for setting outputs path.


Instruction:
Can you generate the documentation for the following Python 3 function
def path(self) -> str:
        return os.path.join(self.project_path, self.project.name)

Response: The path of the git repository.


Instruction:
Make a summary of the following Python 3 code
def last_commit(self) -> Tuple:
        from libs.repos import git

        return git.get_last_commit(repo_path=self.path)

Response: Returns a tuple of hash and commit object


Instruction:
Write a Python 3 script to
validate that we can only authenticate to one S3 and one GCS.

Response:
def validate_stores_secrets_keys(stores_secrets):
        """Validates that we can only authenticate to one S3 and one GCS."""
        stores = set([])
        for store_secret in stores_secrets:
            if store_secret['store'] in stores:
                raise TensorboardValidation('Received an invalid store configuration.')
            elif store_secret['store'] not in {GCS, S3}:
                raise TensorboardValidation('Received an unsupported store configuration.')
            stores.add(store_secret['store'])


Instruction:
Can you create a Python 3 function that
handles the case of GCS and S3 and create a volume with secret file.

Response:
def get_stores_secrets_volumes(cls, stores_secrets):
        """Handles the case of GCS and S3 and create a volume with secret file."""
        volumes = []
        volume_mounts = []
        for store_secret in stores_secrets:
            store = store_secret['store']
            if store in {GCS, S3}:
                secrets_volumes, secrets_volume_mounts = get_volume_from_secret(
                    volume_name=cls.STORE_SECRET_VOLUME_NAME.format(store),
                    mount_path=cls.STORE_SECRET_KEY_MOUNT_PATH.format(store),
                    secret_name=store_secret['persistence_secret'],
                )
                volumes += secrets_volumes
                volume_mounts += secrets_volume_mounts

        return volumes, volume_mounts


Instruction:
Implement a function in Python 3 to
create an auth command for S3 and GCS.

Response:
def get_stores_secrets_command_args(cls, stores_secrets):
        """Create an auth command for S3 and GCS."""
        commands = []
        for store_secret in stores_secrets:
            store = store_secret['store']
            if store == GCS:
                commands.append('export GOOGLE_APPLICATION_CREDENTIALS={}'.format(
                    cls.STORE_SECRET_KEY_MOUNT_PATH.format(store) + '/' +
                    store_secret['persistence_secret_key']
                ))
            elif store == S3:
                commands.append(
                    "import json; data = json.loads(open('{}').read()); content = []; [content.append('export {}={}'.format(k, data[k])) for k in data]; output = open('{}', 'w'); output.write('\n'.join(content)); output.close()".format(  # noqa
                        cls.STORE_SECRET_KEY_MOUNT_PATH.format(store) + '/' +
                        store_secret['persistence_secret_key'],
                        cls.STORE_SECRET_KEY_MOUNT_PATH.format('envs3'),
                    ))
                commands.append("source {}".format(
                    cls.STORE_SECRET_KEY_MOUNT_PATH.format('envs3')))

        return commands


Instruction:
Can you create a Python 3 function that
returns a token to be used for a user account to access the resource.

Response:
def make_token(cls, ephemeral_token: 'RedisEphemeralTokens') -> str:
        """
        Returns a token to be used x number of times to allow a user account to access
        certain resource.
        """
        value = ephemeral_token.key
        if ephemeral_token.scope:
            value += ''.join(ephemeral_token.scope)

        return get_hmac(cls.KEY_SALT + ephemeral_token.salt, value)[::2]


Instruction:
Given the following Python 3 function, write the documentation
def check_token(self, token: 'RedisEphemeralTokens') -> bool:
        if self.get_state() is None:  # Token expired
            return False

        correct_token = self.make_token(self)
        self.clear()
        return constant_time_compare(correct_token, token)

Response: Check that a given token is correct for a given scope.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_ordering(self, request, queryset, view):
        params = request.query_params.get(self.ordering_param)
        if params:
            fields = strip_spaces(value=params, sep=',', join=False)
            ordering, annotations = self.remove_invalid_fields(queryset, fields, view, request)
            if ordering:
                return ordering, annotations

        # No ordering was included, or all the ordering fields were invalid
        return self.get_default_ordering(view), None

Response: Returns the ordering for the current locale.


Instruction:
Can you write a function in Python 3 where it
gets the init container for setting outputs path.

Response:
def get_init_container(self,
                           init_command,
                           init_args,
                           env_vars,
                           context_mounts,
                           persistence_outputs,
                           persistence_data):
        """Pod init container for setting outputs path."""
        env_vars = to_list(env_vars, check_none=True)
        volume_mounts = to_list(context_mounts, check_none=True)
        init_command = init_command or ["/bin/sh", "-c"]
        init_args = to_list(get_auth_context_args(entity='notebook', entity_name=self.job_name))
        return client.V1Container(
            name=self.init_container_name,
            image=self.init_docker_image,
            image_pull_policy=self.init_docker_image_pull_policy,
            command=init_command,
            args=init_args,
            env=env_vars,
            volume_mounts=volume_mounts)


Instruction:
Write a Python 3 function that can
generate the activation key which will be emailed to the user.

Response:
def get_activation_key(self, user):
        """
        Generate the activation key which will be emailed to the user.

        """
        return signing.dumps(
            obj=getattr(user, user.USERNAME_FIELD),
            salt=self.key_salt
        )


Instruction:
Can you tell what is the following Python 3 function doing
def send_activation_email(self, user):
        activation_key = self.get_activation_key(user)
        context = self.get_email_context(activation_key)
        context.update({
            'user': user
        })
        subject = render_to_string(self.email_subject_template,
                                   context)
        # Force subject to a single line to avoid header-injection
        # issues.
        subject = ''.join(subject.splitlines())
        message = render_to_string(self.email_body_template,
                                   context)
        user.email_user(subject, message, conf.get('DEFAULT_FROM_EMAIL'))

Response: Send the activation email to the user.


Instruction:
How would you implement a function in Python 3 that
creates the inactive user account and wait for validation from superuser

Response:
def create_inactive_user(self, form):
        """Create the inactive user account and wait for validation from superuser"""
        new_user = form.save(commit=False)
        new_user.is_active = False
        new_user.save()
        return new_user


Instruction:
Make a summary of the following Python 3 code
def validate_key(self, activation_key):
        try:
            username = signing.loads(
                activation_key,
                salt=self.key_salt,
                max_age=conf.get('ACCOUNT_ACTIVATION_DAYS') * 86400
            )
            return username
        # SignatureExpired is a subclass of BadSignature, so this will
        # catch either one.
        except signing.BadSignature:
            return None

Response: Verify that the activation key is valid and within the activation time window return the username if valid or None if not.


Instruction:
Write a Python 3 script to
look up and return the corresponding user account if it exists or None if it doesn t.

Response:
def get_user(self, username):
        """
        Given the verified username, look up and return the
        corresponding user account if it exists, or ``None`` if it
        doesn't.

        """
        User = get_user_model()  # noqa
        try:
            user = User.objects.get(**{
                User.USERNAME_FIELD: username,
                'is_active': False
            })
            return user
        except User.DoesNotExist:
            return None


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get(self, request, *args, **kwargs):
        activated_user = self.activate(*args, **kwargs)
        if activated_user:
            users_signals.user_activated.send(
                sender=self.__class__,
                user=activated_user,
                request=request
            )
            return redirect(self.success_url)
        return super().get(request, *args, **kwargs)

Response: The base get method that handles activation logic.


Instruction:
Write a Python 3 script to
get a pod container for the current task.

Response:
def get_pod_container(self,
                          volume_mounts,
                          persistence_outputs=None,
                          persistence_data=None,
                          outputs_refs_jobs=None,
                          outputs_refs_experiments=None,
                          secret_refs=None,
                          configmap_refs=None,
                          env_vars=None,
                          command=None,
                          args=None,
                          resources=None,
                          ports=None,
                          ephemeral_token=None):
        """Pod job container for task."""
        self._pod_container_checks()

        # Env vars preparations
        env_vars = to_list(env_vars, check_none=True)
        env_vars += self._get_container_pod_env_vars(
            persistence_outputs=persistence_outputs,
            persistence_data=persistence_data,
            outputs_refs_jobs=outputs_refs_jobs,
            outputs_refs_experiments=outputs_refs_experiments,
            ephemeral_token=ephemeral_token
        )
        env_vars += get_resources_env_vars(resources=resources)

        # Env from configmap and secret refs
        env_from = get_pod_env_from(secret_refs=secret_refs, configmap_refs=configmap_refs)

        def get_ports():
            _ports = to_list(ports) if ports else []
            return [client.V1ContainerPort(container_port=port) for port in _ports] or None

        return client.V1Container(name=self.job_container_name,
                                  image=self.job_docker_image,
                                  command=command,
                                  args=args,
                                  ports=get_ports(),
                                  env=env_vars,
                                  env_from=env_from,
                                  resources=get_resources(resources),
                                  volume_mounts=volume_mounts)


Instruction:
Create a Python 3 function for
getting the sidecar container for task logs.

Response:
def get_sidecar_container(self, volume_mounts):
        """Pod sidecar container for task logs."""
        return get_sidecar_container(
            job_container_name=self.job_container_name,
            sidecar_container_name=self.sidecar_container_name,
            sidecar_docker_image=self.sidecar_docker_image,
            sidecar_docker_image_pull_policy=self.sidecar_docker_image_pull_policy,
            namespace=self.namespace,
            sidecar_config=self.sidecar_config,
            sidecar_args=get_sidecar_args(container_id=self.job_container_name,
                                          app_label=self.app_label),
            internal_health_check_url=self.health_check_url,
            volume_mounts=volume_mounts)


Instruction:
How would you code a function in Python 3 to
return a pod spec for a task.

Response:
def get_task_pod_spec(self,
                          volume_mounts,
                          volumes,
                          resource_name,
                          persistence_outputs=None,
                          persistence_data=None,
                          outputs_refs_jobs=None,
                          outputs_refs_experiments=None,
                          env_vars=None,
                          command=None,
                          args=None,
                          init_command=None,
                          init_args=None,
                          init_env_vars=None,
                          resources=None,
                          ports=None,
                          secret_refs=None,
                          configmap_refs=None,
                          ephemeral_token=None,
                          node_selector=None,
                          affinity=None,
                          tolerations=None,
                          sidecar_context_mounts=None,
                          init_context_mounts=None,
                          restart_policy=None):
        """Pod spec to be used to create pods for tasks: master, worker, ps."""
        sidecar_context_mounts = to_list(sidecar_context_mounts, check_none=True)
        init_context_mounts = to_list(init_context_mounts, check_none=True)
        volume_mounts = to_list(volume_mounts, check_none=True)
        volumes = to_list(volumes, check_none=True)

        gpu_volume_mounts, gpu_volumes = get_gpu_volumes_def(resources)
        volume_mounts += gpu_volume_mounts
        volumes += gpu_volumes

        pod_container = self.get_pod_container(volume_mounts=volume_mounts,
                                               persistence_outputs=persistence_outputs,
                                               persistence_data=persistence_data,
                                               outputs_refs_jobs=outputs_refs_jobs,
                                               outputs_refs_experiments=outputs_refs_experiments,
                                               secret_refs=secret_refs,
                                               configmap_refs=configmap_refs,
                                               resources=resources,
                                               env_vars=env_vars,
                                               command=command,
                                               args=args,
                                               ports=ports,
                                               ephemeral_token=ephemeral_token)

        containers = [pod_container]
        if self.use_sidecar:
            sidecar_volume_mounts = self.get_sidecar_volume_mounts(
                persistence_outputs=persistence_outputs,
                persistence_data=persistence_data,
                context_mounts=sidecar_context_mounts)
            sidecar_container = self.get_sidecar_container(volume_mounts=sidecar_volume_mounts)
            containers.append(sidecar_container)

        init_container = self.get_init_container(init_command=init_command,
                                                 init_args=init_args,
                                                 env_vars=init_env_vars,
                                                 context_mounts=init_context_mounts,
                                                 persistence_outputs=persistence_outputs,
                                                 persistence_data=persistence_data)
        init_containers = to_list(init_container, check_none=True)

        node_selector = self._get_node_selector(node_selector=node_selector)
        affinity = self._get_affinity(affinity=affinity)
        tolerations = self._get_tolerations(tolerations=tolerations)
        service_account_name = self._get_service_account_name()
        return client.V1PodSpec(
            security_context=get_security_context() if self.use_security_context else None,
            restart_policy=restart_policy,
            service_account_name=service_account_name,
            init_containers=init_containers,
            containers=containers,
            volumes=volumes,
            node_selector=node_selector,
            tolerations=tolerations,
            affinity=affinity)


Instruction:
Given the following Python 3 function, write the documentation
def _prepare(cls, context: Dict) -> Dict:
        if not context and cls.raise_empty_context:
            raise PolyaxonActionException('{} received invalid payload context.'.format(cls.name))

        return context

Response: This method is used to prepare the payload context.


Instruction:
Given the following Python 3 function, write the documentation
def copy_experiment(experiment):
    try:
        publisher.publish_experiment_job_log(
            log_lines='Copying outputs from experiment `{}` into experiment `{}`'.format(
                experiment.original_experiment.unique_name, experiment.unique_name
            ),
            experiment_uuid=experiment.uuid.hex,
            experiment_name=experiment.unique_name,
            job_uuid='all',
        )
        stores.copy_experiment_outputs(
            persistence_outputs_from=experiment.original_experiment.persistence_outputs,
            persistence_outputs_to=experiment.persistence_outputs,
            experiment_name_from=experiment.original_experiment.unique_name,
            experiment_name_to=experiment.unique_name)

    except OSError:
        publisher.publish_experiment_job_log(
            log_lines='Could not copy the outputs of experiment `{}` into experiment `{}`'.format(
                experiment.original_experiment.unique_name, experiment.unique_name
            ),
            experiment_uuid=experiment.uuid.hex,
            experiment_name=experiment.unique_name,
            job_uuid='all',
        )
        _logger.warning(
            'Could not copy the outputs of experiment `%s` into experiment `%s`',
            experiment.original_experiment.unique_name, experiment.unique_name)

Response: Copy the outputs of the given experiment into the given experiment.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def record(self,
               event_type: str,
               event_data: Mapping = None,
               instance: Any = None,
               **kwargs) -> 'Event':
        """ Validate and record an event.

        >>> record('event.action', object_instance)
        """
        if not self.is_setup:
            return
        if not self.can_handle(event_type=event_type):
            return

        event = self.get_event(event_type=event_type,
                               event_data=event_data,
                               instance=instance,
                               **kwargs)
        self.record_event(event)
        return event

Response: Validate and record an event.


Instruction:
Write a Python 3 function for
generating a new image and log it to the job.

Response:
def generate(job,
             build_path: str,
             from_image: str,
             build_steps: Optional[List[str]] = None,
             env_vars: Optional[List[Tuple[str, str]]] = None,
             nvidia_bin: str = None,
             set_lang_env: bool = True,
             uid: int = None,
             gid: int = None) -> bool:
    """Build necessary code for a job to run"""
    rendered_dockerfile = dockerizer_generate(repo_path=build_path,
                                              from_image=from_image,
                                              build_steps=build_steps,
                                              env_vars=env_vars,
                                              nvidia_bin=nvidia_bin,
                                              set_lang_env=set_lang_env,
                                              uid=uid,
                                              gid=gid)

    if rendered_dockerfile:
        job.log_dockerfile(dockerfile=rendered_dockerfile)
    return True


Instruction:
Given the following Python 3 function, write the documentation
def get_suggestions(self, iteration_config=None):
        matrix = self.hptuning_config.matrix
        n_suggestions = self.hptuning_config.random_search.n_experiments
        seed = self.hptuning_config.seed
        return get_random_suggestions(matrix=matrix, n_suggestions=n_suggestions, seed=seed)

Response: Return a list of suggestions based on random search.


Instruction:
Implement a Python 3 function for
setting serializer to many if data is passed

Response:
def get_serializer(self, *args, **kwargs):
        """ if an array is passed, set serializer to many """
        if isinstance(kwargs.get('data', {}), list):
            kwargs['many'] = True
        return super().get_serializer(*args, **kwargs)


Instruction:
Can you create a Python 3 function that
tasks handling for sidecars logs.

Response:
def logs_handle_experiment_job(experiment_name: str,
                               experiment_uuid: str,
                               log_lines: Optional[Union[str, Iterable[str]]],
                               temp: bool = True) -> None:
    """Task handling for sidecars logs."""
    handle_experiment_job_log(experiment_name=experiment_name,
                              experiment_uuid=experiment_uuid,
                              log_lines=log_lines,
                              temp=temp)


Instruction:
How would you code a function in Python 3 to
task handling for sidecars logs.

Response:
def logs_handle_job(job_uuid: str,
                    job_name: str,
                    log_lines: Optional[Union[str, Iterable[str]]],
                    temp: bool = True) -> None:
    """Task handling for sidecars logs."""
    handle_job_logs(job_uuid=job_uuid,
                    job_name=job_name,
                    log_lines=log_lines,
                    temp=temp)


Instruction:
Explain what the following Python 3 code does
def logs_handle_build_job(job_uuid: str,
                          job_name: str,
                          log_lines: Optional[Union[str, Iterable[str]]],
                          temp: bool = True) -> None:
    """Task handling for sidecars logs."""
    handle_build_job_logs(job_uuid=job_uuid,
                          job_name=job_name,
                          log_lines=log_lines,
                          temp=temp)

Response: Task handling for sidecars logs.


Instruction:
How would you explain what the following Python 3 function does
def create_iteration(self, num_suggestions=0):
        from db.models.experiment_groups import ExperimentGroupIteration

        search_manager = self.experiment_group.search_manager
        iteration_config = self.experiment_group.iteration_config

        if iteration_config is None:
            iteration = 0
            bracket_iteration = 0
        else:
            should_reschedule = search_manager.should_reschedule(
                iteration=iteration_config.iteration,
                bracket_iteration=iteration_config.bracket_iteration)
            should_reduce_configs = search_manager.should_reduce_configs(
                iteration=iteration_config.iteration,
                bracket_iteration=iteration_config.bracket_iteration)
            if should_reschedule:
                iteration = iteration_config.iteration + 1
                bracket_iteration = 0
            elif should_reduce_configs:
                iteration = iteration_config.iteration
                bracket_iteration = iteration_config.bracket_iteration + 1
            else:
                raise ValueError(
                    'Hyperband create iteration failed for `{}`, '
                    'could not reschedule ot reduce configs'.format(self.experiment_group.id))

        # Create a new iteration config
        iteration_config = HyperbandIterationConfig(
            iteration=iteration,
            num_suggestions=num_suggestions,
            bracket_iteration=bracket_iteration)
        iteration_config.experiment_ids = []
        return ExperimentGroupIteration.objects.create(
            experiment_group=self.experiment_group,
            data=iteration_config.to_dict())

Response: Create an iteration for the experiment group.


Instruction:
Implement a function in Python 3 to
reduce the experiments to restart.

Response:
def get_reduced_configs(self):
        """Reduce the experiments to restart."""
        iteration_config = self.experiment_group.iteration_config
        if iteration_config is None:
            logger.error(
                'Experiment group `%s` attempt to update iteration, but has no iteration',
                self.experiment_group.id,
                extra={'stack': True})
            return
        search_manager = self.experiment_group.search_manager

        # Get the number of experiments to keep
        n_configs_to_keep = search_manager.get_n_config_to_keep_for_iteration(
            iteration=iteration_config.iteration,
            bracket_iteration=iteration_config.bracket_iteration)

        # Get the last group's experiments metrics
        experiments_metrics = self.experiment_group.iteration_config.experiments_metrics
        if experiments_metrics is None:
            raise ExperimentGroupException()

        # Order the experiments
        reverse = Optimization.maximize(
            self.experiment_group.hptuning_config.hyperband.metric.optimization)
        experiments_metrics = sorted(experiments_metrics, key=lambda x: x[1], reverse=reverse)

        # Keep n experiments
        return [xp[0] for xp in experiments_metrics[:n_configs_to_keep]]


Instruction:
Write a Python 3 script for
reducing the experiments to restart.

Response:
def reduce_configs(self):
        """Reduce the experiments to restart."""
        experiment_ids = self.get_reduced_configs()
        experiments = self.experiment_group.experiments.filter(id__in=experiment_ids)
        self.create_iteration()
        iteration_config = self.experiment_group.iteration_config
        hptuning_config = self.experiment_group.hptuning_config
        n_resources = self.experiment_group.search_manager.get_resources_for_iteration(
            iteration=iteration_config.iteration)
        resource_value = self.experiment_group.search_manager.get_n_resources(
            n_resources=n_resources, bracket_iteration=iteration_config.bracket_iteration
        )
        resource_name = hptuning_config.hyperband.resource.name
        resource_value = hptuning_config.hyperband.resource.cast_value(resource_value)

        # Check if we need to resume or restart the experiments
        for experiment in experiments:
            declarations = experiment.declarations
            declarations[resource_name] = resource_value
            declarations_spec = {'declarations': declarations}
            specification = experiment.specification.patch(declarations_spec)
            status_message = 'Hyperband iteration: {}, bracket iteration: {}'.format(
                iteration_config.iteration,
                iteration_config.bracket_iteration)

            if hptuning_config.hyperband.resume:
                experiment.resume(
                    declarations=declarations,
                    config=specification.parsed_data,
                    message=status_message)
            else:
                experiment.restart(
                    experiment_group=self.experiment_group,
                    declarations=declarations,
                    config=specification.parsed_data)


Instruction:
Make a summary of the following Python 3 code
def all_experiment_groups(self):
        from db.models.experiment_groups import ExperimentGroup

        return ExperimentGroup.all.filter(project=self)

Response: Returns a QuerySet of all experiment groups that are associated with this project.


Instruction:
Given the following Python 3 function, write the documentation
def all_jobs(self):
        from db.models.jobs import Job

        return Job.all.filter(project=self)

Response: Returns a QuerySet of all jobs in this project.


Instruction:
Create a Python 3 function for
returning all build jobs for this project.

Response:
def all_build_jobs(self):
        """
        Similar to build_jobs,
        but uses the default manager to return archived experiments as well.
        """
        from db.models.build_jobs import BuildJob

        return BuildJob.all.filter(project=self)


Instruction:
Make a summary of the following Python 3 code
def all_notebook_jobs(self):
        from db.models.notebooks import NotebookJob

        return NotebookJob.all.filter(project=self)

Response: Returns a QuerySet of all NotebookJob objects for this project.


Instruction:
Explain what the following Python 3 code does
def all_tensorboard_jobs(self):
        from db.models.tensorboards import TensorboardJob

        return TensorboardJob.all.filter(project=self)

Response: Returns a QuerySet of all TensorboardJob objects for this project.


Instruction:
Can you generate the documentation for the following Python 3 function
def all_experiments(self):
        from db.models.experiments import Experiment

        return Experiment.all.filter(experiment_group=self)

Response: Returns a QuerySet of all experiments in this group.


Instruction:
Implement a function in Python 3 to
return all the group experiments.

Response:
def all_group_experiments(self):
        """
        Similar to group_experiments,
        but uses the default manager to return archived experiments as well.
        """
        from db.models.experiments import Experiment

        if self.is_selection:
            return Experiment.all.filter(selections=self)
        return Experiment.all.filter(experiment_group=self)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_dag(nodes, downstream_fn) -> Tuple[Dict, Dict]:
    dag = {}
    node_by_ids = {}
    for node in nodes:
        downstream_ops = downstream_fn(node)
        dag[node.id] = set(downstream_ops)
        node_by_ids[node.id] = node

    return dag, node_by_ids

Response: Return a dag representation of the nodes passed.


Instruction:
Implement a function in Python 3 to
get a list of all node in the graph with no dependencies.

Response:
def get_independent_nodes(dag):
    """Get a list of all node in the graph with no dependencies."""
    nodes = set(dag.keys())
    dependent_nodes = set([node for downstream_nodes in dag.values() for node in downstream_nodes])
    return set(nodes - dependent_nodes)


Instruction:
Write a Python 3 function for
getting orphan nodes for given dag.

Response:
def get_orphan_nodes(dag):
    """Get orphan nodes for given dag."""
    independent_nodes = get_independent_nodes(dag)
    return set([node for node in independent_nodes if not dag[node]])


Instruction:
Can you write a function in Python 3 where it
checks if the node has any dependencies.

Response:
def has_dependencies(node, dag):
    """Checks if the node has dependencies."""
    for downstream_nodes in dag.values():
        if node in downstream_nodes:
            return True
    return False


Instruction:
Write a Python 3 script to
sort the dag first topologically.

Response:
def sort_topologically(dag):
    """Sort the dag breath first topologically.

    Only the nodes inside the dag are returned, i.e. the nodes that are also keys.

    Returns:
         a topological ordering of the DAG.
    Raises:
         an error if this is not possible (graph is not valid).
    """
    dag = copy.deepcopy(dag)
    sorted_nodes = []
    independent_nodes = deque(get_independent_nodes(dag))
    while independent_nodes:
        node = independent_nodes.popleft()
        sorted_nodes.append(node)
        # this alters the dag so that we are sure we are visiting the nodes only once
        downstream_nodes = dag[node]
        while downstream_nodes:
            downstream_node = downstream_nodes.pop(0)
            if downstream_node not in dag:
                continue
            if not has_dependencies(downstream_node, dag):
                independent_nodes.append(downstream_node)

    if len(sorted_nodes) != len(dag.keys()):
        raise ValueError('graph is not acyclic')
    return sorted_nodes


Instruction:
Write a Python 3 function that can
parse the negation modifier in an operation.

Response:
def parse_negation_operation(operation: str) -> Tuple[bool, str]:
    """Parse the negation modifier in an operation."""
    _operation = operation.strip()
    if not _operation:
        raise QueryParserException('Operation is not valid: {}'.format(operation))
    negation = False
    if _operation[0] == '~':
        negation = True
        _operation = _operation[1:]

    return negation, _operation.strip()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def parse_comparison_operation(operation: str) -> Tuple[Optional[str], str]:
    _operation = operation.strip()
    if not _operation:
        raise QueryParserException('Operation is not valid: {}'.format(operation))
    # Check inclusion comparison
    if _operation[:2] in ('<=', '=<'):
        return '<=', _operation[2:].strip()

    if _operation[:2] in ('>=', '=>'):
        return '>=', _operation[2:].strip()

    # Non inclusive
    if _operation[:1] in ('>', '<'):
        return _operation[:1], _operation[1:].strip()

    return None, _operation

Response: Parse the comparision operator in an operation.


Instruction:
Write a Python 3 function for
parsing a datetime operation.

Response:
def parse_datetime_operation(operation: str) -> 'QueryOpSpec':
    """Parse datetime operations.

    A datetime operation can one of the following:

     * single value: start_date:2014-10-10, start_date:>2014-10-10, start_date:>=2014-10-10
     * negation single value: start_date:~2014-10-10
     * interval: start_date:2010-10-10 10:10 .. 2012-10-10
     * negation interval: start_date:~2010-10-10 10:10 .. 2012-10-10

    This parser does not allow `|`
    """
    _operation = operation.strip()
    if not _operation:
        raise QueryParserException('Operation is not valid: {}'.format(operation))
    # Check not allowed ops
    if '|' in _operation:
        raise QueryParserException('`|` is not allowed for datetime operations. '
                                   'Operation: {}'.format(operation))

    # Check negation
    negation, _operation = parse_negation_operation(_operation)

    # Check range operator
    if '..' in _operation:
        op = '..'
        params = _operation.split('..')
        params = [param.strip() for param in params if param]
        if len(params) != 2:
            raise QueryParserException('Expression is not valid, ranges requires only 2 params, '
                                       'Operation: {}'.format(operation))
        return QueryOpSpec(op, negation, params)

    # Check comparison operators
    op, _operation = parse_comparison_operation(_operation)
    if not op:
        # Now the operation must be an equality param param
        op = '='

    if not _operation:
        raise QueryParserException('Expression is not valid, it must be formatted as '
                                   'name:operation, '
                                   'Operation: {}'.format(operation))
    return QueryOpSpec(op, negation, _operation)


Instruction:
Make a summary of the following Python 3 code
def parse_scalar_operation(operation: str) -> 'QueryOpSpec':
    _operation = operation.strip()
    if not _operation:
        raise QueryParserException('Operation is not valid: {}'.format(operation))
    # Check not allowed ops
    if '|' in _operation:
        raise QueryParserException('`|` is not allowed for scalar operations. '
                                   'Operation: {}'.format(operation))
    if '..' in _operation:
        raise QueryParserException('`..` is not allowed for scalar operations. '
                                   'Operation: {}'.format(operation))

    # Check negation
    negation, _operation = parse_negation_operation(_operation)

    # Check comparison operators
    op, _operation = parse_comparison_operation(_operation)
    if not op:
        # Now the operation must be an equality param param
        op = '='

    # Check that params are scalar (int, float)
    try:
        _operation = int(_operation)
    except (ValueError, TypeError):
        try:
            _operation = float(_operation)
        except (ValueError, TypeError):
            raise QueryParserException('Scalar operation requires int or float params, '
                                       'receive {}.'.format(operation))
    return QueryOpSpec(op, negation, _operation)

Response: Parse a scalar operation.


Instruction:
How would you implement a function in Python 3 that
bases parsing for expressions.

Response:
def parse_expression(expression: str) -> Tuple[str, str]:
    """Base parsing for expressions.

    Every expression must follow a basic format:
        `name:[modifier|operator]operation[*[operator]operation]`

    So this parser just split the expression into: field name, operation.
    """
    try:
        _expression = expression.strip()
        name, operation = _expression.split(':')
        name = name.strip()
        operation = operation.strip()
        if not name or not operation:
            raise ValueError
    except (ValueError, AttributeError):
        raise QueryParserException('Expression is not valid, it must be formatted as '
                                   'name:operation, '
                                   'Expression: {}'.format(expression))
    return name, operation


Instruction:
Can you tell what is the following Python 3 function doing
def split_query(query: str) -> List[str]:
    try:
        _query = query.strip()
    except (ValueError, AttributeError):
        raise QueryParserException('query is not valid, received instead {}'.format(query))

    expressions = _query.split(',')
    expressions = [exp.strip() for exp in expressions if exp.strip()]
    if not expressions:
        raise QueryParserException('Query is not valid: {}'.format(query))

    return expressions

Response: Split a query into different expressions.


Instruction:
Can you tell what is the following Python 3 function doing
def tokenize_query(query: str) -> Dict[str, Iterable]:
    expressions = split_query(query)
    name_operation_tuples = [parse_expression(expression) for expression in expressions]
    operation_by_name = defaultdict(list)
    for name, operation in name_operation_tuples:
        operation_by_name[name].append(operation)
    return operation_by_name

Response: Tokenizes a standard search query in name and operations mapping.


Instruction:
Given the following Python 3 function, write the documentation
def parse_field(field: str) -> Tuple[str, Optional[str]]:
    _field = field.split('.')
    _field = [f.strip() for f in _field]
    if len(_field) == 1 and _field[0]:
        return _field[0], None
    elif len(_field) == 2 and _field[0] and _field[1]:
        return _field[0], _field[1]
    raise QueryParserException('Query field must be either a single value,'
                               'possibly with single underscores, '
                               'or a prefix double underscore field. '
                               'Received `{}`'.format(field))

Response: Parses a single field with underscores and return field and suffix.


Instruction:
Write a Python 3 function that can
set the upstream operations for an operation run.

Response:
def set_op_upstreams(op_run, op):
    """Set the upstream operations for operation run."""
    # We get a list of all upstream ops or the current op
    upstream_ops = op.upstream_operations.values_list('id', flat=True)
    # We get latest op runs for the upstream_ops
    latest_op_runs = OperationRun.objects.filter(id__in=upstream_ops)
    latest_op_runs = latest_op_runs.annotate(max_date=Max('created_at'))
    latest_op_runs = latest_op_runs.filter(date=F('max_date'))
    # Set the upstream ops
    op_run.set(latest_op_runs)


Instruction:
Can you tell what is the following Python 3 function doing
def set_topological_dag_upstreams(dag, ops, op_runs, runs_by_ops):
    sorted_ops = dags.sort_topologically(dag=dag)
    for op_id in sorted_ops:
        op_run_id = runs_by_ops[op_id]
        op_run = op_runs[op_run_id]
        set_op_upstreams(op_run=op_run, op=ops[op_id])

Response: Set the upstream runs for the operations in the dag following the topological sort.


Instruction:
Given the following Python 3 function, write the documentation
def create_pipeline_run(pipeline, context_by_op):
    pipeline_run = PipelineRun.objects.create(pipeline=pipeline)
    dag, ops = pipeline.dag
    # Go trough the operation and create operation runs and the upstreams
    op_runs = {}
    runs_by_ops = {}
    for op_id in dag.keys():
        op_run = OperationRun.objects.create(
            pipeline_run=pipeline_run,
            operation_id=op_id,
            celery_task_context=context_by_op.get(op_id))
        op_run_id = op_run.id
        op_runs[op_run_id] = op_run
        runs_by_ops[op_id] = op_run_id

    # Create operations upstreams
    # We set the upstream for the topologically sorted dag
    set_topological_dag_upstreams(dag=dag, ops=ops, op_runs=op_runs, runs_by_ops=runs_by_ops)

Response: Create a pipeline run and instance.


Instruction:
How would you explain what the following Python 3 function does
def k8s_events_handle_experiment_job_statuses(self: 'celery_app.task', payload: Dict) -> None:
    details = payload['details']
    job_uuid = details['labels']['job_uuid']
    logger.debug('handling events status for job_uuid: %s, status: %s',
                 job_uuid, payload['status'])

    try:
        job = ExperimentJob.objects.get(uuid=job_uuid)
    except ExperimentJob.DoesNotExist:
        logger.debug('Job uuid`%s` does not exist', job_uuid)
        return

    try:
        job.experiment
    except Experiment.DoesNotExist:
        logger.debug('Experiment for job `%s` does not exist anymore', job_uuid)
        return

    if job.last_status is None and self.request.retries < 2:
        self.retry(countdown=1)

    # Set the new status
    try:
        set_node_scheduling(job, details['node_name'])
        job.set_status(status=payload['status'],
                       message=payload['message'],
                       created_at=payload.get('created_at'),
                       traceback=payload.get('traceback'),
                       details=details)
        logger.debug('status %s is set for job %s %s', payload['status'], job_uuid, job.id)
    except IntegrityError:
        # Due to concurrency this could happen, we just retry it
        logger.info('Retry job status %s handling %s', payload['status'], job_uuid)
        self.retry(countdown=Intervals.EXPERIMENTS_SCHEDULER)

Response: Handle the status of an experiment job.


Instruction:
Can you generate the documentation for the following Python 3 function
def k8s_events_handle_job_statuses(self: 'celery_app.task', payload: Dict) -> None:
    details = payload['details']
    job_uuid = details['labels']['job_uuid']
    job_name = details['labels']['job_name']
    project_name = details['labels'].get('project_name')
    logger.debug('handling events status for job %s', job_name)

    try:
        job = Job.objects.get(uuid=job_uuid)
    except Job.DoesNotExist:
        logger.debug('Job `%s` does not exist', job_name)
        return

    try:
        job.project
    except Project.DoesNotExist:
        logger.debug('Project for job `%s` does not exist', project_name)
        return

    # Set the new status
    try:
        set_node_scheduling(job, details['node_name'])
        job.set_status(status=payload['status'],
                       message=payload['message'],
                       traceback=payload.get('traceback'),
                       details=details)
    except IntegrityError:
        # Due to concurrency this could happen, we just retry it
        self.retry(countdown=Intervals.EXPERIMENTS_SCHEDULER)

Response: Handle the status of a job.


Instruction:
Write a Python 3 script for
handling the status of a plugin job.

Response:
def k8s_events_handle_plugin_job_statuses(self: 'celery_app.task', payload: Dict) -> None:
    """Project Plugin jobs statuses"""
    details = payload['details']
    app = details['labels']['app']
    job_uuid = details['labels']['job_uuid']
    job_name = details['labels']['job_name']
    project_name = details['labels'].get('project_name')
    logger.debug('handling events status for job %s %s', job_name, app)

    try:
        if app == conf.get('APP_LABELS_TENSORBOARD'):
            job = TensorboardJob.objects.get(uuid=job_uuid)
        elif app == conf.get('APP_LABELS_NOTEBOOK'):
            job = NotebookJob.objects.get(uuid=job_uuid)
        else:
            logger.info('Plugin job `%s` does not exist', app)
            return
    except (NotebookJob.DoesNotExist, TensorboardJob.DoesNotExist):
        logger.debug('`%s - %s` does not exist', app, job_name)
        return

    try:
        job.project
    except Project.DoesNotExist:
        logger.debug('`%s` does not exist anymore', project_name)

    # Set the new status
    try:
        set_node_scheduling(job, details['node_name'])
        job.set_status(status=payload['status'],
                       message=payload['message'],
                       traceback=payload.get('traceback'),
                       details=details)
    except IntegrityError:
        # Due to concurrency this could happen, we just retry it
        self.retry(countdown=Intervals.EXPERIMENTS_SCHEDULER)


Instruction:
Write a Python 3 function for
handling the build job statuses

Response:
def k8s_events_handle_build_job_statuses(self: 'celery_app.task', payload: Dict) -> None:
    """Project Plugin jobs statuses"""
    details = payload['details']
    app = details['labels']['app']
    job_uuid = details['labels']['job_uuid']
    job_name = details['labels']['job_name']
    project_name = details['labels'].get('project_name')
    logger.debug('handling events status for build jon %s %s', job_name, app)

    try:
        build_job = BuildJob.objects.get(uuid=job_uuid)
    except BuildJob.DoesNotExist:
        logger.info('Build job `%s` does not exist', job_name)
        return

    try:
        build_job.project
    except Project.DoesNotExist:
        logger.debug('`%s` does not exist anymore', project_name)

    # Set the new status
    try:
        set_node_scheduling(build_job, details['node_name'])
        build_job.set_status(status=payload['status'],
                             message=payload['message'],
                             traceback=payload.get('traceback'),
                             details=details)
    except IntegrityError:
        # Due to concurrency this could happen, we just retry it
        self.retry(countdown=Intervals.EXPERIMENTS_SCHEDULER)


Instruction:
How would you implement a function in Python 3 that
returns a pod sidecar container.

Response:
def get_sidecar_container(job_container_name,
                          sidecar_container_name,
                          sidecar_docker_image,
                          sidecar_docker_image_pull_policy,
                          namespace,
                          sidecar_config,
                          sidecar_args,
                          internal_health_check_url,
                          volume_mounts,
                          env_vars=None):
    """Return a pod sidecar container."""
    env_vars = to_list(env_vars) if env_vars else []
    env_vars += get_sidecar_env_vars(namespace=namespace,
                                     job_container_name=job_container_name,
                                     internal_health_check_url=internal_health_check_url)
    for k, v in sidecar_config.items():
        env_vars.append(get_env_var(name=k, value=v))
    return client.V1Container(name=sidecar_container_name,
                              image=sidecar_docker_image,
                              image_pull_policy=sidecar_docker_image_pull_policy,
                              command=get_sidecar_command(),
                              env=env_vars,
                              volume_mounts=volume_mounts,
                              args=sidecar_args)


Instruction:
Write a Python 3 script to
use custom exception handler for errors.

Response:
def handle_exception(self, exc):
        """Use custom exception handler for errors."""
        if isinstance(
            exc, (rest_exceptions.NotAuthenticated,
                  rest_exceptions.AuthenticationFailed)) and self.HANDLE_UNAUTHENTICATED:
            return HttpResponseRedirect('{}?next={}'.format(
                reverse('users:login'),
                self.request.get_full_path()))

        if isinstance(exc, Http404):
            raise Http404()

        if isinstance(exc, rest_exceptions.PermissionDenied):
            raise django_exceptions.PermissionDenied()

        return super().handle_exception(exc)


Instruction:
Explain what the following Python 3 code does
def get_resources(resources):  # pylint:disable=too-many-branches
    limits = {}
    requests = {}
    if resources is None:
        return None
    if resources.cpu:
        if resources.cpu.limits:
            limits['cpu'] = resources.cpu.limits
        if resources.cpu.requests:
            requests['cpu'] = resources.cpu.requests

    if resources.memory:
        if resources.memory.limits:
            limits['memory'] = '{}Mi'.format(resources.memory.limits)
        if resources.memory.requests:
            requests['memory'] = '{}Mi'.format(resources.memory.requests)

    if resources.gpu:
        if resources.gpu.limits:
            limits[conf.get('K8S_GPU_RESOURCE_KEY')] = resources.gpu.limits
        if resources.gpu.requests:
            requests[conf.get('K8S_GPU_RESOURCE_KEY')] = resources.gpu.requests

    if resources.tpu:
        if resources.tpu.limits:
            limits[conf.get('K8S_TPU_RESOURCE_KEY')] = resources.tpu.limits
        if resources.tpu.requests:
            requests[conf.get('K8S_TPU_RESOURCE_KEY')] = resources.tpu.requests
    return client.V1ResourceRequirements(limits=limits or None, requests=requests or None)

Response:
Create resources requirements.

    Args:
        resources: `PodResourcesConfig`

    Return:
        `V1ResourceRequirements`


Instruction:
Can you tell what is the following Python 3 function doing
def has_project_permissions(user: 'User', project: 'Project', request_method: str) -> bool:
    # Superusers and the creator is allowed to do everything
    if user.is_staff or user.is_superuser or project.user == user:
        return True

    # Other user
    return request_method in permissions.SAFE_METHODS and project.is_public

Response: Returns True if the user has permission to do project.


Instruction:
Can you write a function in Python 3 where it
deletes all group outputs and logs.

Response:
def experiment_group_pre_delete(sender, **kwargs):
    """Delete all group outputs."""
    instance = kwargs['instance']

    if instance.is_selection:
        return

    # Delete outputs and logs
    celery_app.send_task(
        SchedulerCeleryTasks.STORES_SCHEDULE_OUTPUTS_DELETION,
        kwargs={
            'persistence': instance.persistence_outputs,
            'subpath': instance.subpath,
        },
        countdown=conf.get('GLOBAL_COUNTDOWN'))
    celery_app.send_task(
        SchedulerCeleryTasks.STORES_SCHEDULE_LOGS_DELETION,
        kwargs={
            'persistence': instance.persistence_logs,
            'subpath': instance.subpath,
        },
        countdown=conf.get('GLOBAL_COUNTDOWN'))


Instruction:
Write a Python 3 script for
deleting all group outputs.

Response:
def experiment_group_post_delete(sender, **kwargs):
    """Delete all group outputs."""
    instance = kwargs['instance']
    auditor.record(event_type=EXPERIMENT_GROUP_DELETED, instance=instance)
    remove_bookmarks(object_id=instance.id, content_type='experimentgroup')


Instruction:
Here you have a function in Python 3, explain what it does
def get_asset_url(module: str, path: str) -> str:
    return '{}/{}/{}'.format(
        settings.STATIC_URL.rstrip('/'),
        module,
        path.lstrip('/'),
    )

Response: Return a static asset URL located within Polyaxon s static files.