Instruction:
Can you write a function in Python 3 where it
creates an Amazon SageMaker hyperparameter tuning job Args: job_name (str): Name of the tuning job being created. strategy (str): Strategy to be used for hyperparameter estimations. objective_type (str): The type of the objective metric for evaluating training jobs. This value can be either 'Minimize' or 'Maximize'. objective_metric_name (str): Name of the metric for evaluating training jobs. max_jobs (int): Maximum total number of training jobs to start for the hyperparameter tuning job. max_parallel_jobs (int): Maximum number of parallel training jobs to start. parameter_ranges (dict): Dictionary of parameter ranges. These parameter ranges can be one of three types: Continuous, Integer, or Categorical. static_hyperparameters (dict): Hyperparameters for model training. These hyperparameters remain unchanged across all of the training jobs for the hyperparameter tuning job. The hyperparameters are made accessible as a dictionary for the training code on SageMaker. image (str): Docker image containing training code. input_mode (str): The input mode that the algorithm supports. Valid modes: * 'File' - Amazon SageMaker copies the training dataset from the S3 location to a directory in the Docker container. * 'Pipe' - Amazon SageMaker streams data directly from S3 to the container via a Unix-named pipe. metric_definitions (list[dict]): A list of dictionaries that defines the metric(s) used to evaluate the training jobs. Each dictionary contains two keys: 'Name' for the name of the metric, and 'Regex' for the regular expression used to extract the metric from the logs. This should be defined only for jobs that don't use an Amazon algorithm. role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. You must grant sufficient permissions to this role. input_config (list): A list of Channel objects. Each channel is a named input source. Please refer to the format details described: https://botocore.readthedocs.io/en/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job output_config (dict): The S3 URI where you want to store the training results and optional KMS key ID. resource_config (dict): Contains values for ResourceConfig: * instance_count (int): Number of EC2 instances to use for training. The key in resource_config is 'InstanceCount'. * instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'. The key in resource_config is 'InstanceType'. stop_condition (dict): When training should finish, e.g. ``MaxRuntimeInSeconds``. tags (list[dict]): List of tags for labeling the tuning job. For more, see https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html. warm_start_config (dict): Configuration defining the type of warm start and other required configurations. early_stopping_type (str): Specifies whether early stopping is enabled for the job. Can be either 'Auto' or 'Off'. If set to 'Off', early stopping will not be attempted. If set to 'Auto', early stopping of some training jobs may happen, but is not guaranteed to. encrypt_inter_container_traffic (bool): Specifies whether traffic between training containers is encrypted for the training jobs started for this hyperparameter tuning job (default: ``False``). vpc_config (dict): Contains values for VpcConfig (default: None): * subnets (list[str]): List of subnet ids. The key in vpc_config is 'Subnets'. * security_group_ids (list[str]): List of security group ids. The key in vpc_config is 'SecurityGroupIds'.

Response:
def tune(self, job_name, strategy, objective_type, objective_metric_name,
             max_jobs, max_parallel_jobs, parameter_ranges,
             static_hyperparameters, input_mode, metric_definitions,
             role, input_config, output_config, resource_config, stop_condition, tags,
             warm_start_config, enable_network_isolation=False, image=None, algorithm_arn=None,
             early_stopping_type='Off', encrypt_inter_container_traffic=False, vpc_config=None):
        """Create an Amazon SageMaker hyperparameter tuning job

        Args:
            job_name (str): Name of the tuning job being created.
            strategy (str): Strategy to be used for hyperparameter estimations.
            objective_type (str): The type of the objective metric for evaluating training jobs. This value can be
                either 'Minimize' or 'Maximize'.
            objective_metric_name (str): Name of the metric for evaluating training jobs.
            max_jobs (int): Maximum total number of training jobs to start for the hyperparameter tuning job.
            max_parallel_jobs (int): Maximum number of parallel training jobs to start.
            parameter_ranges (dict): Dictionary of parameter ranges. These parameter ranges can be one of three types:
                 Continuous, Integer, or Categorical.
            static_hyperparameters (dict): Hyperparameters for model training. These hyperparameters remain
                unchanged across all of the training jobs for the hyperparameter tuning job. The hyperparameters are
                made accessible as a dictionary for the training code on SageMaker.
            image (str): Docker image containing training code.
            input_mode (str): The input mode that the algorithm supports. Valid modes:

                * 'File' - Amazon SageMaker copies the training dataset from the S3 location to
                    a directory in the Docker container.
                * 'Pipe' - Amazon SageMaker streams data directly from S3 to the container via a Unix-named pipe.

            metric_definitions (list[dict]): A list of dictionaries that defines the metric(s) used to evaluate the
                training jobs. Each dictionary contains two keys: 'Name' for the name of the metric, and 'Regex' for
                the regular expression used to extract the metric from the logs. This should be defined only for
                jobs that don't use an Amazon algorithm.
            role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs
                that create Amazon SageMaker endpoints use this role to access training data and model artifacts.
                You must grant sufficient permissions to this role.
            input_config (list): A list of Channel objects. Each channel is a named input source. Please refer to
                 the format details described:
                 https://botocore.readthedocs.io/en/latest/reference/services/sagemaker.html#SageMaker.Client.create_training_job
            output_config (dict): The S3 URI where you want to store the training results and optional KMS key ID.
            resource_config (dict): Contains values for ResourceConfig:

                * instance_count (int): Number of EC2 instances to use for training.
                    The key in resource_config is 'InstanceCount'.
                * instance_type (str): Type of EC2 instance to use for training, for example, 'ml.c4.xlarge'.
                    The key in resource_config is 'InstanceType'.

            stop_condition (dict): When training should finish, e.g. ``MaxRuntimeInSeconds``.
            tags (list[dict]): List of tags for labeling the tuning job. For more, see
                https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
            warm_start_config (dict): Configuration defining the type of warm start and
                other required configurations.
            early_stopping_type (str): Specifies whether early stopping is enabled for the job.
                Can be either 'Auto' or 'Off'. If set to 'Off', early stopping will not be attempted.
                If set to 'Auto', early stopping of some training jobs may happen, but is not guaranteed to.
            encrypt_inter_container_traffic (bool): Specifies whether traffic between training containers
                is encrypted for the training jobs started for this hyperparameter tuning job (default: ``False``).
            vpc_config (dict): Contains values for VpcConfig (default: None):

                * subnets (list[str]): List of subnet ids.
                    The key in vpc_config is 'Subnets'.
                * security_group_ids (list[str]): List of security group ids.
                    The key in vpc_config is 'SecurityGroupIds'.

        """
        tune_request = {
            'HyperParameterTuningJobName': job_name,
            'HyperParameterTuningJobConfig': {
                'Strategy': strategy,
                'HyperParameterTuningJobObjective': {
                    'Type': objective_type,
                    'MetricName': objective_metric_name,
                },
                'ResourceLimits': {
                    'MaxNumberOfTrainingJobs': max_jobs,
                    'MaxParallelTrainingJobs': max_parallel_jobs,
                },
                'ParameterRanges': parameter_ranges,
                'TrainingJobEarlyStoppingType': early_stopping_type,
            },
            'TrainingJobDefinition': {
                'StaticHyperParameters': static_hyperparameters,
                'RoleArn': role,
                'OutputDataConfig': output_config,
                'ResourceConfig': resource_config,
                'StoppingCondition': stop_condition,
            }
        }

        algorithm_spec = {
            'TrainingInputMode': input_mode
        }
        if algorithm_arn:
            algorithm_spec['AlgorithmName'] = algorithm_arn
        else:
            algorithm_spec['TrainingImage'] = image

        tune_request['TrainingJobDefinition']['AlgorithmSpecification'] = algorithm_spec

        if input_config is not None:
            tune_request['TrainingJobDefinition']['InputDataConfig'] = input_config

        if warm_start_config:
            tune_request['WarmStartConfig'] = warm_start_config

        if metric_definitions is not None:
            tune_request['TrainingJobDefinition']['AlgorithmSpecification']['MetricDefinitions'] = metric_definitions

        if tags is not None:
            tune_request['Tags'] = tags

        if vpc_config is not None:
            tune_request['TrainingJobDefinition']['VpcConfig'] = vpc_config

        if enable_network_isolation:
            tune_request['TrainingJobDefinition']['EnableNetworkIsolation'] = True

        if encrypt_inter_container_traffic:
            tune_request['TrainingJobDefinition']['EnableInterContainerTrafficEncryption'] = True

        LOGGER.info('Creating hyperparameter tuning job with name: {}'.format(job_name))
        LOGGER.debug('tune request: {}'.format(json.dumps(tune_request, indent=4)))
        self.sagemaker_client.create_hyper_parameter_tuning_job(**tune_request)


Instruction:
Here you have a function in Python 3, explain what it does
def stop_tuning_job(self, name):
        try:
            LOGGER.info('Stopping tuning job: {}'.format(name))
            self.sagemaker_client.stop_hyper_parameter_tuning_job(HyperParameterTuningJobName=name)
        except ClientError as e:
            error_code = e.response['Error']['Code']
            # allow to pass if the job already stopped
            if error_code == 'ValidationException':
                LOGGER.info('Tuning job: {} is already stopped or not running.'.format(name))
            else:
                LOGGER.error('Error occurred while attempting to stop tuning job: {}. Please try again.'.format(name))
                raise

Response: Stop the Amazon SageMaker hyperparameter tuning job with the specified name.


Instruction:
Implement a Python 3 function for
creating an Amazon SageMaker transform job.

Response:
def transform(self, job_name, model_name, strategy, max_concurrent_transforms, max_payload, env,
                  input_config, output_config, resource_config, tags):
        """Create an Amazon SageMaker transform job.

        Args:
            job_name (str): Name of the transform job being created.
            model_name (str): Name of the SageMaker model being used for the transform job.
            strategy (str): The strategy used to decide how to batch records in a single request.
                Possible values are 'MULTI_RECORD' and 'SINGLE_RECORD'.
            max_concurrent_transforms (int): The maximum number of HTTP requests to be made to
                each individual transform container at one time.
            max_payload (int): Maximum size of the payload in a single HTTP request to the container in MB.
            env (dict): Environment variables to be set for use during the transform job.
            input_config (dict): A dictionary describing the input data (and its location) for the job.
            output_config (dict): A dictionary describing the output location for the job.
            resource_config (dict): A dictionary describing the resources to complete the job.
            tags (list[dict]): List of tags for labeling a training job. For more, see
                https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
        """
        transform_request = {
            'TransformJobName': job_name,
            'ModelName': model_name,
            'TransformInput': input_config,
            'TransformOutput': output_config,
            'TransformResources': resource_config,
        }

        if strategy is not None:
            transform_request['BatchStrategy'] = strategy

        if max_concurrent_transforms is not None:
            transform_request['MaxConcurrentTransforms'] = max_concurrent_transforms

        if max_payload is not None:
            transform_request['MaxPayloadInMB'] = max_payload

        if env is not None:
            transform_request['Environment'] = env

        if tags is not None:
            transform_request['Tags'] = tags

        LOGGER.info('Creating transform job with name: {}'.format(job_name))
        LOGGER.debug('Transform request: {}'.format(json.dumps(transform_request, indent=4)))
        self.sagemaker_client.create_transform_job(**transform_request)


Instruction:
Can you write a function in Python 3 where it
creates an Amazon SageMaker Model.

Response:
def create_model(self, name, role, container_defs, vpc_config=None,
                     enable_network_isolation=False, primary_container=None,
                     tags=None):
        """Create an Amazon SageMaker ``Model``.
        Specify the S3 location of the model artifacts and Docker image containing
        the inference code. Amazon SageMaker uses this information to deploy the
        model in Amazon SageMaker. This method can also be used to create a Model for an Inference Pipeline
        if you pass the list of container definitions through the containers parameter.

        Args:
            name (str): Name of the Amazon SageMaker ``Model`` to create.
            role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs
                that create Amazon SageMaker endpoints use this role to access training data and model artifacts.
                You must grant sufficient permissions to this role.
            container_defs (list[dict[str, str]] or [dict[str, str]]): A single container definition or a list of
                container definitions which will be invoked sequentially while performing the prediction. If the list
                contains only one container, then it'll be passed to SageMaker Hosting as the ``PrimaryContainer`` and
                otherwise, it'll be passed as ``Containers``.You can also specify the  return value of
                ``sagemaker.get_container_def()`` or ``sagemaker.pipeline_container_def()``, which will used to
                create more advanced container configurations ,including model containers which need artifacts from S3.
            vpc_config (dict[str, list[str]]): The VpcConfig set on the model (default: None)
                * 'Subnets' (list[str]): List of subnet ids.
                * 'SecurityGroupIds' (list[str]): List of security group ids.
            enable_network_isolation (bool): Wether the model requires network isolation or not.
            primary_container (str or dict[str, str]): Docker image which defines the inference code.
                You can also specify the return value of ``sagemaker.container_def()``, which is used to create
                more advanced container configurations, including model containers which need artifacts from S3. This
                field is deprecated, please use container_defs instead.
            tags(List[dict[str, str]]): Optional. The list of tags to add to the model. Example:
                    >>> tags = [{'Key': 'tagname', 'Value': 'tagvalue'}]
                    For more information about tags, see https://boto3.amazonaws.com/v1/documentation\
                    /api/latest/reference/services/sagemaker.html#SageMaker.Client.add_tags

        Returns:
            str: Name of the Amazon SageMaker ``Model`` created.
        """
        if container_defs and primary_container:
            raise ValueError('Both container_defs and primary_container can not be passed as input')

        if primary_container:
            msg = 'primary_container is going to be deprecated in a future release. Please use container_defs instead.'
            warnings.warn(msg, DeprecationWarning)
            container_defs = primary_container

        role = self.expand_role(role)

        if isinstance(container_defs, list):
            container_definition = container_defs
        else:
            container_definition = _expand_container_def(container_defs)

        create_model_request = _create_model_request(name=name,
                                                     role=role,
                                                     container_def=container_definition,
                                                     tags=tags)

        if vpc_config:
            create_model_request['VpcConfig'] = vpc_config

        if enable_network_isolation:
            create_model_request['EnableNetworkIsolation'] = True

        LOGGER.info('Creating model with name: {}'.format(name))
        LOGGER.debug('CreateModel request: {}'.format(json.dumps(create_model_request, indent=4)))

        try:
            self.sagemaker_client.create_model(**create_model_request)
        except ClientError as e:
            error_code = e.response['Error']['Code']
            message = e.response['Error']['Message']

            if error_code == 'ValidationException' and 'Cannot create already existing model' in message:
                LOGGER.warning('Using already existing model: {}'.format(name))
            else:
                raise

        return name


Instruction:
How would you code a function in Python 3 to
create an Amazon SageMaker Model from a SageMaker Training Job.

Response:
def create_model_from_job(self, training_job_name, name=None, role=None, primary_container_image=None,
                              model_data_url=None, env=None, vpc_config_override=vpc_utils.VPC_CONFIG_DEFAULT):
        """Create an Amazon SageMaker ``Model`` from a SageMaker Training Job.

        Args:
            training_job_name (str): The Amazon SageMaker Training Job name.
            name (str): The name of the SageMaker ``Model`` to create (default: None).
                If not specified, the training job name is used.
            role (str): The ``ExecutionRoleArn`` IAM Role ARN for the ``Model``, specified either by an IAM role name or
                role ARN. If None, the ``RoleArn`` from the SageMaker Training Job will be used.
            primary_container_image (str): The Docker image reference (default: None). If None, it defaults to
                the Training Image in ``training_job_name``.
            model_data_url (str): S3 location of the model data (default: None). If None, defaults to
                the ``ModelS3Artifacts`` of ``training_job_name``.
            env (dict[string,string]): Model environment variables (default: {}).
            vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
                Default: use VpcConfig from training job.
                * 'Subnets' (list[str]): List of subnet ids.
                * 'SecurityGroupIds' (list[str]): List of security group ids.

        Returns:
            str: The name of the created ``Model``.
        """
        training_job = self.sagemaker_client.describe_training_job(TrainingJobName=training_job_name)
        name = name or training_job_name
        role = role or training_job['RoleArn']
        env = env or {}
        primary_container = container_def(
            primary_container_image or training_job['AlgorithmSpecification']['TrainingImage'],
            model_data_url=model_data_url or training_job['ModelArtifacts']['S3ModelArtifacts'],
            env=env)
        vpc_config = _vpc_config_from_training_job(training_job, vpc_config_override)
        return self.create_model(name, role, primary_container, vpc_config=vpc_config)


Instruction:
How would you implement a function in Python 3 that
creates a SageMaker Model Package from the results of training with an Algorithm Package

Response:
def create_model_package_from_algorithm(self, name, description, algorithm_arn, model_data):
        """Create a SageMaker Model Package from the results of training with an Algorithm Package

        Args:
            name (str): ModelPackage name
            description (str): Model Package description
            algorithm_arn (str): arn or name of the algorithm used for training.
            model_data (str): s3 URI to the model artifacts produced by training
        """
        request = {
            'ModelPackageName': name,
            'ModelPackageDescription': description,
            'SourceAlgorithmSpecification': {
                'SourceAlgorithms': [
                    {
                        'AlgorithmName': algorithm_arn,
                        'ModelDataUrl': model_data
                    }
                ]
            }
        }
        try:
            LOGGER.info('Creating model package with name: {}'.format(name))
            self.sagemaker_client.create_model_package(**request)
        except ClientError as e:
            error_code = e.response['Error']['Code']
            message = e.response['Error']['Message']

            if (
                    error_code == 'ValidationException'
                    and 'ModelPackage already exists' in message
            ):
                LOGGER.warning('Using already existing model package: {}'.format(name))
            else:
                raise


Instruction:
Write a Python 3 function for
waiting for an Amazon SageMaker model package to complete.

Response:
def wait_for_model_package(self, model_package_name, poll=5):
        """Wait for an Amazon SageMaker endpoint deployment to complete.

        Args:
            endpoint (str): Name of the ``Endpoint`` to wait for.
            poll (int): Polling interval in seconds (default: 5).

        Returns:
            dict: Return value from the ``DescribeEndpoint`` API.
        """
        desc = _wait_until(lambda: _create_model_package_status(self.sagemaker_client, model_package_name),
                           poll)
        status = desc['ModelPackageStatus']

        if status != 'Completed':
            reason = desc.get('FailureReason', None)
            raise ValueError('Error creating model package {}: {} Reason: {}'.format(
                model_package_name, status, reason))
        return desc


Instruction:
How would you explain what the following Python 3 function does
def create_endpoint_config(self, name, model_name, initial_instance_count, instance_type,
                               accelerator_type=None, tags=None, kms_key=None):
        """Create an Amazon SageMaker endpoint configuration.

        The endpoint configuration identifies the Amazon SageMaker model (created using the
        ``CreateModel`` API) and the hardware configuration on which to deploy the model. Provide this
        endpoint configuration to the ``CreateEndpoint`` API, which then launches the hardware and deploys the model.

        Args:
            name (str): Name of the Amazon SageMaker endpoint configuration to create.
            model_name (str): Name of the Amazon SageMaker ``Model``.
            initial_instance_count (int): Minimum number of EC2 instances to launch. The actual number of
                active instances for an endpoint at any given time varies due to autoscaling.
            instance_type (str): Type of EC2 instance to launch, for example, 'ml.c4.xlarge'.
            accelerator_type (str): Type of Elastic Inference accelerator to attach to the instance. For example,
                'ml.eia1.medium'. For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
            tags(List[dict[str, str]]): Optional. The list of tags to add to the endpoint config. Example:
                    >>> tags = [{'Key': 'tagname', 'Value': 'tagvalue'}]
                    For more information about tags, see https://boto3.amazonaws.com/v1/documentation\
                    /api/latest/reference/services/sagemaker.html#SageMaker.Client.add_tags

        Returns:
            str: Name of the endpoint point configuration created.
        """
        LOGGER.info('Creating endpoint-config with name {}'.format(name))

        tags = tags or []

        request = {
            'EndpointConfigName': name,
            'ProductionVariants': [
                production_variant(model_name, instance_type, initial_instance_count,
                                   accelerator_type=accelerator_type)
            ],
        }

        if tags is not None:
            request['Tags'] = tags

        if kms_key is not None:
            request['KmsKeyId'] = kms_key

        self.sagemaker_client.create_endpoint_config(**request)
        return name

Response: Create an Amazon SageMaker endpoint - point configuration.


Instruction:
Can you implement a function in Python 3 that
creates an Amazon SageMaker Endpoint according to the endpoint configuration specified in the request.

Response:
def create_endpoint(self, endpoint_name, config_name, tags=None, wait=True):
        """Create an Amazon SageMaker ``Endpoint`` according to the endpoint configuration specified in the request.

        Once the ``Endpoint`` is created, client applications can send requests to obtain inferences.
        The endpoint configuration is created using the ``CreateEndpointConfig`` API.

        Args:
            endpoint_name (str): Name of the Amazon SageMaker ``Endpoint`` being created.
            config_name (str): Name of the Amazon SageMaker endpoint configuration to deploy.
            wait (bool): Whether to wait for the endpoint deployment to complete before returning (default: True).

        Returns:
            str: Name of the Amazon SageMaker ``Endpoint`` created.
        """
        LOGGER.info('Creating endpoint with name {}'.format(endpoint_name))

        tags = tags or []

        self.sagemaker_client.create_endpoint(EndpointName=endpoint_name, EndpointConfigName=config_name, Tags=tags)
        if wait:
            self.wait_for_endpoint(endpoint_name)
        return endpoint_name


Instruction:
Explain what the following Python 3 code does
def update_endpoint(self, endpoint_name, endpoint_config_name):
        if not _deployment_entity_exists(lambda: self.sagemaker_client.describe_endpoint(EndpointName=endpoint_name)):
            raise ValueError('Endpoint with name "{}" does not exist; please use an existing endpoint name'
                             .format(endpoint_name))

        self.sagemaker_client.update_endpoint(EndpointName=endpoint_name,
                                              EndpointConfigName=endpoint_config_name)
        return endpoint_name

Response: Update an Amazon SageMaker endpoint according to the endpoint configuration specified in the request.


Instruction:
How would you implement a function in Python 3 that
deletes an Amazon SageMaker Endpoint.

Response:
def delete_endpoint(self, endpoint_name):
        """Delete an Amazon SageMaker ``Endpoint``.

        Args:
            endpoint_name (str): Name of the Amazon SageMaker ``Endpoint`` to delete.
        """
        LOGGER.info('Deleting endpoint with name: {}'.format(endpoint_name))
        self.sagemaker_client.delete_endpoint(EndpointName=endpoint_name)


Instruction:
Can you write a function in Python 3 where it
deletes an Amazon SageMaker endpoint configuration.

Response:
def delete_endpoint_config(self, endpoint_config_name):
        """Delete an Amazon SageMaker endpoint configuration.

        Args:
            endpoint_config_name (str): Name of the Amazon SageMaker endpoint configuration to delete.
        """
        LOGGER.info('Deleting endpoint configuration with name: {}'.format(endpoint_config_name))
        self.sagemaker_client.delete_endpoint_config(EndpointConfigName=endpoint_config_name)


Instruction:
Can you create a Python 3 function that
deletes an Amazon SageMaker Model.

Response:
def delete_model(self, model_name):
        """Delete an Amazon SageMaker Model.

        Args:
            model_name (str): Name of the Amazon SageMaker model to delete.

        """
        LOGGER.info('Deleting model with name: {}'.format(model_name))
        self.sagemaker_client.delete_model(ModelName=model_name)


Instruction:
How would you explain what the following Python 3 function does
def wait_for_job(self, job, poll=5):
        desc = _wait_until_training_done(lambda last_desc: _train_done(self.sagemaker_client, job, last_desc),
                                         None, poll)
        self._check_job_status(job, desc, 'TrainingJobStatus')
        return desc

Response: Wait for an Amazon SageMaker training job to complete.


Instruction:
Can you write a function in Python 3 where it
waits for an Amazon SageMaker Neo compilation job to complete.

Response:
def wait_for_compilation_job(self, job, poll=5):
        """Wait for an Amazon SageMaker Neo compilation job to complete.

        Args:
            job (str): Name of the compilation job to wait for.
            poll (int): Polling interval in seconds (default: 5).

        Returns:
            (dict): Return value from the ``DescribeCompilationJob`` API.

        Raises:
            ValueError: If the compilation job fails.
        """
        desc = _wait_until(lambda: _compilation_job_status(self.sagemaker_client, job), poll)
        self._check_job_status(job, desc, 'CompilationJobStatus')
        return desc


Instruction:
Here you have a function in Python 3, explain what it does
def wait_for_tuning_job(self, job, poll=5):
        desc = _wait_until(lambda: _tuning_job_status(self.sagemaker_client, job), poll)
        self._check_job_status(job, desc, 'HyperParameterTuningJobStatus')
        return desc

Response: Wait for an Amazon SageMaker hyperparameter tuning job to complete.


Instruction:
Write a Python 3 script to
wait for an Amazon SageMaker transform job to complete.

Response:
def wait_for_transform_job(self, job, poll=5):
        """Wait for an Amazon SageMaker transform job to complete.

        Args:
            job (str): Name of the transform job to wait for.
            poll (int): Polling interval in seconds (default: 5).

        Returns:
            (dict): Return value from the ``DescribeTransformJob`` API.

        Raises:
            ValueError: If the transform job fails.
        """
        desc = _wait_until(lambda: _transform_job_status(self.sagemaker_client, job), poll)
        self._check_job_status(job, desc, 'TransformJobStatus')
        return desc


Instruction:
Here you have a function in Python 3, explain what it does
def _check_job_status(self, job, desc, status_key_name):
        status = desc[status_key_name]
        # If the status is capital case, then convert it to Camel case
        status = _STATUS_CODE_TABLE.get(status, status)

        if status != 'Completed' and status != 'Stopped':
            reason = desc.get('FailureReason', '(No reason provided)')
            job_type = status_key_name.replace('JobStatus', ' job')
            raise ValueError('Error for {} {}: {} Reason: {}'.format(job_type, job, status, reason))

Response:
Check to see if the job completed successfully and if not construct and
            class instance.


Instruction:
Write a Python 3 function for
waiting for an Amazon SageMaker endpoint deployment to complete.

Response:
def wait_for_endpoint(self, endpoint, poll=5):
        """Wait for an Amazon SageMaker endpoint deployment to complete.

        Args:
            endpoint (str): Name of the ``Endpoint`` to wait for.
            poll (int): Polling interval in seconds (default: 5).

        Returns:
            dict: Return value from the ``DescribeEndpoint`` API.
        """
        desc = _wait_until(lambda: _deploy_done(self.sagemaker_client, endpoint), poll)
        status = desc['EndpointStatus']

        if status != 'InService':
            reason = desc.get('FailureReason', None)
            raise ValueError('Error hosting endpoint {}: {} Reason: {}'.format(endpoint, status, reason))
        return desc


Instruction:
Write a Python 3 script to
create an endpoint from the results of a successful training job.

Response:
def endpoint_from_job(self, job_name, initial_instance_count, instance_type,
                          deployment_image=None, name=None, role=None, wait=True,
                          model_environment_vars=None, vpc_config_override=vpc_utils.VPC_CONFIG_DEFAULT,
                          accelerator_type=None):
        """Create an ``Endpoint`` using the results of a successful training job.

        Specify the job name, Docker image containing the inference code, and hardware configuration to deploy
        the model. Internally the API, creates an Amazon SageMaker model (that describes the model artifacts and
        the Docker image containing inference code), endpoint configuration (describing the hardware to deploy
        for hosting the model), and creates an ``Endpoint`` (launches the EC2 instances and deploys the model on them).
        In response, the API returns the endpoint name to which you can send requests for inferences.

        Args:
            job_name (str): Name of the training job to deploy the results of.
            initial_instance_count (int): Minimum number of EC2 instances to launch. The actual number of
                active instances for an endpoint at any given time varies due to autoscaling.
            instance_type (str): Type of EC2 instance to deploy to an endpoint for prediction,
                for example, 'ml.c4.xlarge'.
            deployment_image (str): The Docker image which defines the inference code to be used as the entry point for
                accepting prediction requests. If not specified, uses the image used for the training job.
            name (str): Name of the ``Endpoint`` to create. If not specified, uses the training job name.
            role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs
                that create Amazon SageMaker endpoints use this role to access training data and model artifacts.
                You must grant sufficient permissions to this role.
            wait (bool): Whether to wait for the endpoint deployment to complete before returning (default: True).
            model_environment_vars (dict[str, str]): Environment variables to set on the model container
                (default: None).
            vpc_config_override (dict[str, list[str]]): Overrides VpcConfig set on the model.
                Default: use VpcConfig from training job.
                * 'Subnets' (list[str]): List of subnet ids.
                * 'SecurityGroupIds' (list[str]): List of security group ids.
            accelerator_type (str): Type of Elastic Inference accelerator to attach to the instance. For example,
                'ml.eia1.medium'. For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html

        Returns:
            str: Name of the ``Endpoint`` that is created.
        """
        job_desc = self.sagemaker_client.describe_training_job(TrainingJobName=job_name)
        output_url = job_desc['ModelArtifacts']['S3ModelArtifacts']
        deployment_image = deployment_image or job_desc['AlgorithmSpecification']['TrainingImage']
        role = role or job_desc['RoleArn']
        name = name or job_name
        vpc_config_override = _vpc_config_from_training_job(job_desc, vpc_config_override)

        return self.endpoint_from_model_data(model_s3_location=output_url, deployment_image=deployment_image,
                                             initial_instance_count=initial_instance_count, instance_type=instance_type,
                                             name=name, role=role, wait=wait,
                                             model_environment_vars=model_environment_vars,
                                             model_vpc_config=vpc_config_override, accelerator_type=accelerator_type)


Instruction:
Can you create a Python 3 function that
creates and deploy an endpoint using existing model data stored in S3.

Response:
def endpoint_from_model_data(self, model_s3_location, deployment_image, initial_instance_count, instance_type,
                                 name=None, role=None, wait=True, model_environment_vars=None, model_vpc_config=None,
                                 accelerator_type=None):
        """Create and deploy to an ``Endpoint`` using existing model data stored in S3.

        Args:
            model_s3_location (str): S3 URI of the model artifacts to use for the endpoint.
            deployment_image (str): The Docker image which defines the runtime code to be used as
                the entry point for accepting prediction requests.
            initial_instance_count (int): Minimum number of EC2 instances to launch. The actual number of
                active instances for an endpoint at any given time varies due to autoscaling.
            instance_type (str): Type of EC2 instance to deploy to an endpoint for prediction, e.g. 'ml.c4.xlarge'.
            name (str): Name of the ``Endpoint`` to create. If not specified, uses a name generated by
                combining the image name with a timestamp.
            role (str): An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs
                that create Amazon SageMaker endpoints use this role to access training data and model artifacts.
                You must grant sufficient permissions to this role.
            wait (bool): Whether to wait for the endpoint deployment to complete before returning (default: True).
            model_environment_vars (dict[str, str]): Environment variables to set on the model container
                (default: None).
            model_vpc_config (dict[str, list[str]]): The VpcConfig set on the model (default: None)
                * 'Subnets' (list[str]): List of subnet ids.
                * 'SecurityGroupIds' (list[str]): List of security group ids.
            accelerator_type (str): Type of Elastic Inference accelerator to attach to the instance. For example,
                'ml.eia1.medium'. For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html

        Returns:
            str: Name of the ``Endpoint`` that is created.
        """

        model_environment_vars = model_environment_vars or {}
        name = name or name_from_image(deployment_image)
        model_vpc_config = vpc_utils.sanitize(model_vpc_config)

        if _deployment_entity_exists(lambda: self.sagemaker_client.describe_endpoint(EndpointName=name)):
            raise ValueError('Endpoint with name "{}" already exists; please pick a different name.'.format(name))

        if not _deployment_entity_exists(lambda: self.sagemaker_client.describe_model(ModelName=name)):
            primary_container = container_def(image=deployment_image,
                                              model_data_url=model_s3_location,
                                              env=model_environment_vars)
            self.create_model(name=name,
                              role=role,
                              container_defs=primary_container,
                              vpc_config=model_vpc_config)

        if not _deployment_entity_exists(
                lambda: self.sagemaker_client.describe_endpoint_config(EndpointConfigName=name)):
            self.create_endpoint_config(name=name,
                                        model_name=name,
                                        initial_instance_count=initial_instance_count,
                                        instance_type=instance_type,
                                        accelerator_type=accelerator_type)

        self.create_endpoint(endpoint_name=name, config_name=name, wait=wait)
        return name


Instruction:
Can you generate a brief explanation for the following Python 3 code
def endpoint_from_production_variants(self, name, production_variants, tags=None, kms_key=None, wait=True):

        if not _deployment_entity_exists(
                lambda: self.sagemaker_client.describe_endpoint_config(EndpointConfigName=name)):
            config_options = {'EndpointConfigName': name, 'ProductionVariants': production_variants}
            if tags:
                config_options['Tags'] = tags
            if kms_key:
                config_options['KmsKeyId'] = kms_key

            self.sagemaker_client.create_endpoint_config(**config_options)
        return self.create_endpoint(endpoint_name=name, config_name=name, tags=tags, wait=wait)

Response: Create an SageMaker Endpoint from a list of production variants.


Instruction:
Can you implement a function in Python 3 that
expands an IAM role name into an ARN.

Response:
def expand_role(self, role):
        """Expand an IAM role name into an ARN.

        If the role is already in the form of an ARN, then the role is simply returned. Otherwise we retrieve the full
        ARN and return it.

        Args:
            role (str): An AWS IAM role (either name or full ARN).

        Returns:
            str: The corresponding AWS IAM role ARN.
        """
        if '/' in role:
            return role
        else:
            return self.boto_session.resource('iam').Role(role).arn


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_caller_identity_arn(self):
        assumed_role = self.boto_session.client('sts').get_caller_identity()['Arn']

        if 'AmazonSageMaker-ExecutionRole' in assumed_role:
            role = re.sub(r'^(.+)sts::(\d+):assumed-role/(.+?)/.*$', r'\1iam::\2:role/service-role/\3', assumed_role)
            return role

        role = re.sub(r'^(.+)sts::(\d+):assumed-role/(.+?)/.*$', r'\1iam::\2:role/\3', assumed_role)

        # Call IAM to get the role's path
        role_name = role[role.rfind('/') + 1:]
        try:
            role = self.boto_session.client('iam').get_role(RoleName=role_name)['Role']['Arn']
        except ClientError:
            LOGGER.warning("Couldn't call 'get_role' to get Role ARN from role name {} to get Role path."
                           .format(role_name))

        return role

Response: Returns the ARN user or role whose credentials are used to call the API.


Instruction:
Write a Python 3 script for
displaying the logs for a given training job.

Response:
def logs_for_job(self, job_name, wait=False, poll=10):  # noqa: C901 - suppress complexity warning for this method
        """Display the logs for a given training job, optionally tailing them until the
        job is complete. If the output is a tty or a Jupyter cell, it will be color-coded
        based on which instance the log entry is from.

        Args:
            job_name (str): Name of the training job to display the logs for.
            wait (bool): Whether to keep looking for new log entries until the job completes (default: False).
            poll (int): The interval in seconds between polling for new log entries and job completion (default: 5).

        Raises:
            ValueError: If waiting and the training job fails.
        """

        description = self.sagemaker_client.describe_training_job(TrainingJobName=job_name)
        print(secondary_training_status_message(description, None), end='')
        instance_count = description['ResourceConfig']['InstanceCount']
        status = description['TrainingJobStatus']

        stream_names = []  # The list of log streams
        positions = {}     # The current position in each stream, map of stream name -> position

        # Increase retries allowed (from default of 4), as we don't want waiting for a training job
        # to be interrupted by a transient exception.
        config = botocore.config.Config(retries={'max_attempts': 15})
        client = self.boto_session.client('logs', config=config)
        log_group = '/aws/sagemaker/TrainingJobs'

        job_already_completed = True if status == 'Completed' or status == 'Failed' or status == 'Stopped' else False

        state = LogState.TAILING if wait and not job_already_completed else LogState.COMPLETE
        dot = False

        color_wrap = sagemaker.logs.ColorWrap()

        # The loop below implements a state machine that alternates between checking the job status and
        # reading whatever is available in the logs at this point. Note, that if we were called with
        # wait == False, we never check the job status.
        #
        # If wait == TRUE and job is not completed, the initial state is TAILING
        # If wait == FALSE, the initial state is COMPLETE (doesn't matter if the job really is complete).
        #
        # The state table:
        #
        # STATE               ACTIONS                        CONDITION             NEW STATE
        # ----------------    ----------------               -----------------     ----------------
        # TAILING             Read logs, Pause, Get status   Job complete          JOB_COMPLETE
        #                                                    Else                  TAILING
        # JOB_COMPLETE        Read logs, Pause               Any                   COMPLETE
        # COMPLETE            Read logs, Exit                                      N/A
        #
        # Notes:
        # - The JOB_COMPLETE state forces us to do an extra pause and read any items that got to Cloudwatch after
        #   the job was marked complete.
        last_describe_job_call = time.time()
        last_description = description
        while True:
            if len(stream_names) < instance_count:
                # Log streams are created whenever a container starts writing to stdout/err, so this list
                # may be dynamic until we have a stream for every instance.
                try:
                    streams = client.describe_log_streams(logGroupName=log_group, logStreamNamePrefix=job_name + '/',
                                                          orderBy='LogStreamName', limit=instance_count)
                    stream_names = [s['logStreamName'] for s in streams['logStreams']]
                    positions.update([(s, sagemaker.logs.Position(timestamp=0, skip=0))
                                      for s in stream_names if s not in positions])
                except ClientError as e:
                    # On the very first training job run on an account, there's no log group until
                    # the container starts logging, so ignore any errors thrown about that
                    err = e.response.get('Error', {})
                    if err.get('Code', None) != 'ResourceNotFoundException':
                        raise

            if len(stream_names) > 0:
                if dot:
                    print('')
                    dot = False
                for idx, event in sagemaker.logs.multi_stream_iter(client, log_group, stream_names, positions):
                    color_wrap(idx, event['message'])
                    ts, count = positions[stream_names[idx]]
                    if event['timestamp'] == ts:
                        positions[stream_names[idx]] = sagemaker.logs.Position(timestamp=ts, skip=count + 1)
                    else:
                        positions[stream_names[idx]] = sagemaker.logs.Position(timestamp=event['timestamp'], skip=1)
            else:
                dot = True
                print('.', end='')
                sys.stdout.flush()
            if state == LogState.COMPLETE:
                break

            time.sleep(poll)

            if state == LogState.JOB_COMPLETE:
                state = LogState.COMPLETE
            elif time.time() - last_describe_job_call >= 30:
                description = self.sagemaker_client.describe_training_job(TrainingJobName=job_name)
                last_describe_job_call = time.time()

                if secondary_training_status_changed(description, last_description):
                    print()
                    print(secondary_training_status_message(description, last_description), end='')
                    last_description = description

                status = description['TrainingJobStatus']

                if status == 'Completed' or status == 'Failed' or status == 'Stopped':
                    print()
                    state = LogState.JOB_COMPLETE

        if wait:
            self._check_job_status(job_name, description, 'TrainingJobStatus')
            if dot:
                print()
            # Customers are not billed for hardware provisioning, so billable time is less than total time
            billable_time = (description['TrainingEndTime'] - description['TrainingStartTime']) * instance_count
            print('Billable seconds:', int(billable_time.total_seconds()) + 1)


Instruction:
Can you generate the documentation for the following Python 3 function
def registry(region_name, framework=None):
    try:
        account_id = image_registry_map[region_name][framework]
        return get_ecr_image_uri_prefix(account_id, region_name)
    except KeyError:
        logging.error("The specific image or region does not exist")
        raise

Response: Returns the docker registry for the given AWS region.


Instruction:
How would you code a function in Python 3 to
create a KNNModel referencing the latest KNN .

Response:
def create_model(self, vpc_config_override=VPC_CONFIG_DEFAULT):
        """Return a :class:`~sagemaker.amazon.KNNModel` referencing the latest
        s3 model data produced by this Estimator.

        Args:
            vpc_config_override (dict[str, list[str]]): Optional override for VpcConfig set on the model.
                Default: use subnets and security groups from this Estimator.
                * 'Subnets' (list[str]): List of subnet ids.
                * 'SecurityGroupIds' (list[str]): List of security group ids.
        """
        return KNNModel(self.model_data, self.role, sagemaker_session=self.sagemaker_session,
                        vpc_config=self.get_vpc_config(vpc_config_override))


Instruction:
Given the following Python 3 function, write the documentation
def copy_directory_structure(destination_directory, relative_path):
    full_path = os.path.join(destination_directory, relative_path)
    if os.path.exists(full_path):
        return

    os.makedirs(destination_directory, relative_path)

Response: Create all the intermediate directories required for relative_path to exist within destination_directory.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def move_to_destination(source, destination, job_name, sagemaker_session):
    parsed_uri = urlparse(destination)
    if parsed_uri.scheme == 'file':
        recursive_copy(source, parsed_uri.path)
        final_uri = destination
    elif parsed_uri.scheme == 's3':
        bucket = parsed_uri.netloc
        path = "%s%s" % (parsed_uri.path.lstrip('/'), job_name)
        final_uri = 's3://%s/%s' % (bucket, path)
        sagemaker_session.upload_data(source, bucket, path)
    else:
        raise ValueError('Invalid destination URI, must be s3:// or file://, got: %s' % destination)

    shutil.rmtree(source)
    return final_uri

Response: Move source to destination. Can handle uploading to S3 or file


Instruction:
Explain what the following Python 3 code does
def recursive_copy(source, destination):
    if os.path.isdir(source):
        copy_tree(source, destination)

Response:
A wrapper around distutils. dir_util. copy_tree but won t throw any exception when the source
    directory does not exist.


Instruction:
Write a Python 3 function for
converting the job description to init params that can be handled by the class constructor.

Response:
def _prepare_init_params_from_job_description(cls, job_details, model_channel_name=None):
        """Convert the job description to init params that can be handled by the class constructor

        Args:
            job_details: the returned job details from a describe_training_job API call.
            model_channel_name (str): Name of the channel where pre-trained model data will be downloaded.

        Returns:
            dictionary: The transformed init_params

        """
        init_params = super(MXNet, cls)._prepare_init_params_from_job_description(job_details, model_channel_name)
        image_name = init_params.pop('image')
        framework, py_version, tag, _ = framework_name_from_image(image_name)

        if not framework:
            # If we were unable to parse the framework name from the image it is not one of our
            # officially supported images, in this case just add the image to the init params.
            init_params['image_name'] = image_name
            return init_params

        init_params['py_version'] = py_version

        # We switched image tagging scheme from regular image version (e.g. '1.0') to more expressive
        # containing framework version, device type and python version (e.g. '0.12-gpu-py2').
        # For backward compatibility map deprecated image tag '1.0' to a '0.12' framework version
        # otherwise extract framework version from the tag itself.
        init_params['framework_version'] = '0.12' if tag == '1.0' else framework_version_from_tag(tag)

        training_job_name = init_params['base_job_name']

        if framework != cls.__framework_name__:
            raise ValueError("Training job: {} didn't use image for requested framework".format(training_job_name))

        return init_params


Instruction:
How would you code a function in Python 3 to
start a new transform job.

Response:
def transform(self, data, data_type='S3Prefix', content_type=None, compression_type=None, split_type=None,
                  job_name=None):
        """Start a new transform job.

        Args:
            data (str): Input data location in S3.
            data_type (str): What the S3 location defines (default: 'S3Prefix'). Valid values:

                * 'S3Prefix' - the S3 URI defines a key name prefix. All objects with this prefix will be used as
                    inputs for the transform job.
                * 'ManifestFile' - the S3 URI points to a single manifest file listing each S3 object to use as
                    an input for the transform job.

            content_type (str): MIME type of the input data (default: None).
            compression_type (str): Compression type of the input data, if compressed (default: None).
                Valid values: 'Gzip', None.
            split_type (str): The record delimiter for the input object (default: 'None').
                Valid values: 'None', 'Line', 'RecordIO', and 'TFRecord'.
            job_name (str): job name (default: None). If not specified, one will be generated.
        """
        local_mode = self.sagemaker_session.local_mode
        if not local_mode and not data.startswith('s3://'):
            raise ValueError('Invalid S3 URI: {}'.format(data))

        if job_name is not None:
            self._current_job_name = job_name
        else:
            base_name = self.base_transform_job_name or base_name_from_image(self._retrieve_image_name())
            self._current_job_name = name_from_base(base_name)

        if self.output_path is None:
            self.output_path = 's3://{}/{}'.format(self.sagemaker_session.default_bucket(), self._current_job_name)

        self.latest_transform_job = _TransformJob.start_new(self, data, data_type, content_type, compression_type,
                                                            split_type)


Instruction:
Implement a function in Python 3 to
attach an existing transform job to a new Transformer instance.

Response:
def attach(cls, transform_job_name, sagemaker_session=None):
        """Attach an existing transform job to a new Transformer instance

        Args:
            transform_job_name (str): Name for the transform job to be attached.
            sagemaker_session (sagemaker.session.Session): Session object which manages interactions with
                Amazon SageMaker APIs and any other AWS services needed. If not specified, one will be created
                using the default AWS configuration chain.

        Returns:
            sagemaker.transformer.Transformer: The Transformer instance with the specified transform job attached.

        """
        sagemaker_session = sagemaker_session or Session()

        job_details = sagemaker_session.sagemaker_client.describe_transform_job(TransformJobName=transform_job_name)
        init_params = cls._prepare_init_params_from_job_description(job_details)
        transformer = cls(sagemaker_session=sagemaker_session, **init_params)
        transformer.latest_transform_job = _TransformJob(sagemaker_session=sagemaker_session,
                                                         job_name=init_params['base_transform_job_name'])

        return transformer


Instruction:
Can you generate the documentation for the following Python 3 function
def _prepare_init_params_from_job_description(cls, job_details):
        init_params = dict()

        init_params['model_name'] = job_details['ModelName']
        init_params['instance_count'] = job_details['TransformResources']['InstanceCount']
        init_params['instance_type'] = job_details['TransformResources']['InstanceType']
        init_params['volume_kms_key'] = job_details['TransformResources'].get('VolumeKmsKeyId')
        init_params['strategy'] = job_details.get('BatchStrategy')
        init_params['assemble_with'] = job_details['TransformOutput'].get('AssembleWith')
        init_params['output_path'] = job_details['TransformOutput']['S3OutputPath']
        init_params['output_kms_key'] = job_details['TransformOutput'].get('KmsKeyId')
        init_params['accept'] = job_details['TransformOutput'].get('Accept')
        init_params['max_concurrent_transforms'] = job_details.get('MaxConcurrentTransforms')
        init_params['max_payload'] = job_details.get('MaxPayloadInMB')
        init_params['base_transform_job_name'] = job_details['TransformJobName']

        return init_params

Response: Convert the transform job description to init params that can be handled by the class constructor.


Instruction:
How would you explain what the following Python 3 function does
def piece_size(model_file=None, model_proto=None, name=None):

  return _gen_sentencepiece_processor_op.sentencepiece_get_piece_size(
      model_file=model_file, model_proto=model_proto, name=name)

Response: Returns the piece size.


Instruction:
Here you have a function in Python 3, explain what it does
def piece_to_id(input, model_file=None, model_proto=None, name=None):

  return _gen_sentencepiece_processor_op.sentencepiece_piece_to_id(
      input, model_file=model_file, model_proto=model_proto, name=name)

Response: Converts a piece into vocabulary id.


Instruction:
Can you generate the documentation for the following Python 3 function
def id_to_piece(input, model_file=None, model_proto=None, name=None):

  return _gen_sentencepiece_processor_op.sentencepiece_id_to_piece(
      input, model_file=model_file, model_proto=model_proto, name=name)

Response: Converts vocabulary id into piece.


Instruction:
Can you tell what is the following Python 3 function doing
def is_unknown(input, model_file=None, model_proto=None, name=None):

  return _gen_sentencepiece_processor_op.sentencepiece_get_piece_type(
      input, model_file=model_file, model_proto=model_proto, name=name,
      piece_type=0)

Response: Returns true if input id is unknown piece.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_control(input, model_file=None, model_proto=None, name=None):

  return _gen_sentencepiece_processor_op.sentencepiece_get_piece_type(
      input, model_file=model_file, model_proto=model_proto, name=name,
      piece_type=1)

Response: Returns true if input id is control piece.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def is_unused(input, model_file=None, model_proto=None, name=None):

  return _gen_sentencepiece_processor_op.sentencepiece_get_piece_type(
      input, model_file=model_file, model_proto=model_proto, name=name,
      piece_type=2)

Response: Returns true if input id is unused piece.


Instruction:
Here you have a function in Python 3, explain what it does
def encode_dense(input_sentences, nbest_size=0, alpha=1.0,
                 model_file=None, model_proto=None,
                 reverse=False, add_bos=False, add_eos=False,
                 out_type=tf.int32, name=None):
  """Encodes sentences into pieces in dense tensor format.

  Args:
    input_sentences: A 1D string tensor of arbitrary size holding the raw
                     text of input sentences.
    nbest_size: A scalar or 1D tensor for sampling.
                nbest_size = {0,1}: No sampling is performed.
                nbest_size > 1: samples from the nbest_size results.
                nbest_size < 0: assuming that nbest_size is infinite
                and samples from the all hypothesis (lattice) using
                forward-filtering-and-backward-sampling algorithm.
    alpha: A scalar or 1D tensor for a smoothing parameter.
           Inverse temperature for probability rescaling.
    model_file: The sentencepiece model file path.
    model_proto: The sentencepiece model serialized proto.
                 Either `model_file` or `model_proto` must be set.
    reverse: Reverses the tokenized sequence (Default = false)
    add_bos: Add <s> to the result (Default = false)
    add_eos: Add </s> to the result (Default = false)
             <s>/</s> is added after reversing (if enabled).
    out_type: output type. tf.int32 or tf.string (Default = tf.int32)
              Setting tf.int32 directly encodes the string into an id sequence.
    name: The name argument that is passed to the op function.
  Returns:
    pieces: A dense 2D tensor representing the tokenized sentences.
    sequence_length: A 1D tensor representing the length of pieces.
  """

  return _gen_sentencepiece_processor_op.sentencepiece_encode_dense(
      input_sentences, nbest_size=nbest_size, alpha=alpha,
      model_file=model_file, model_proto=model_proto,
      reverse=reverse, add_bos=add_bos, add_eos=add_eos,
      out_type=out_type, name=name)

Response: Encodes sentences into pieces in dense format.


Instruction:
Create a Python 3 function for
encoding sentences into pieces in sparse tensor format.

Response:
def encode_sparse(input_sentences, nbest_size=0, alpha=1.0,
                  model_file=None, model_proto=None,
                  reverse=False, add_bos=False, add_eos=False,
                  out_type=tf.int32, name=None):
  """Encodes sentences into pieces in sparse tensor format.

  Args:
    input_sentences: A 1D string tensor of arbitrary size holding the raw
                     text of input sentences.
    nbest_size: A scalar or 1D tensor for sampling.
                nbest_size = {0,1}: No sampling is performed.
                nbest_size > 1: samples from the nbest_size results.
                nbest_size < 0: assuming that nbest_size is infinite
                and samples from the all hypothesis (lattice) using
                forward-filtering-and-backward-sampling algorithm.
    alpha: A scalar or 1D tensor for a smoothing parameter.
           Inverse temperature for probability rescaling.
    model_file: The sentencepiece model file path.
    model_proto: The sentencepiece model serialized proto.
                 Either `model_file` or `model_proto` must be set.
    reverse: Reverses the tokenized sequence (Default = false)
    add_bos: Add <s> to the result (Default = false)
    add_eos: Add </s> to the result (Default = false)
             <s>/</s> is added after reversing (if enabled).
    out_type: output type. tf.int32 or tf.string (Default = tf.int32)
              Setting tf.int32 directly encodes the string into an id sequence.
    name: The name argument that is passed to the op function.

  Returns:
    pieces: A sparse 2D tensor representing the tokenized sentences.
  """

  indices, values, dense_shape = (
      _gen_sentencepiece_processor_op.sentencepiece_encode_sparse(
          input_sentences, nbest_size=nbest_size, alpha=alpha,
          model_file=model_file, model_proto=model_proto,
          reverse=reverse, add_bos=add_bos, add_eos=add_eos,
          out_type=out_type, name=name))
  return tf.SparseTensor(indices, values, dense_shape)


Instruction:
Implement a function in Python 3 to
decode pieces into postprocessed text.

Response:
def decode(pieces, sequence_length, model_file=None, model_proto=None,
           reverse=False, name=None):
  """Decode pieces into postprocessed text.

  Args:
    pieces: A 2D int32 or string tensor [batch_size x max_length] of
            encoded sequences.
    sequence_length: A 1D int32 tensor [batch_size] representing the
                   length of pieces.
    model_file: The sentencepiece model file path.
    model_proto: The sentencepiece model serialized proto.
                 Either `model_file` or `model_proto` must be set.
    reverse: Reverses the tokenized sequence (Default = false)
    name: The name argument that is passed to the op function.

  Returns:
    text: A 1D string tensor of decoded string.
  """

  return _gen_sentencepiece_processor_op.sentencepiece_decode(
      pieces, sequence_length, model_file=model_file,
      model_proto=model_proto, reverse=reverse, name=name)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def split_string(text, chars_per_string):
    return [text[i:i + chars_per_string] for i in range(0, len(text), chars_per_string)]

Response: Splits one string into multiple strings with a maximum amount of chars_per_string characters per string.


Instruction:
Make a summary of the following Python 3 code
def extract_arguments(text):
    regexp = re.compile("/\w*(@\w*)*\s*([\s\S]*)",re.IGNORECASE)
    result = regexp.match(text)
    return result.group(2) if is_command(text) else None

Response: Extracts the arguments after the command.


Instruction:
Make a summary of the following Python 3 code
def check_json(json_type):
        try:
            str_types = (str, unicode)
        except NameError:
            str_types = (str,)

        if type(json_type) == dict:
            return json_type
        elif type(json_type) in str_types:
            return json.loads(json_type)
        else:
            raise ValueError("json_type should be a json dict or string.")

Response: Checks whether json_type is a dict or a string.


Instruction:
Implement a function in Python 3 to
parse text and add entities to the html_text attribute of the current object.

Response:
def __html_text(self, text, entities):
        """
        Author: @sviat9440
        Message: "*Test* parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username"

        Example:
            message.html_text
            >> "<b>Test</b> parse <i>formatting</i>, <a href=\"https://example.com\">url</a>, <a href=\"tg://user?id=123456\">text_mention</a> and mention @username"

        Cusom subs:
            You can customize the substitutes. By default, there is no substitute for the entities: hashtag, bot_command, email. You can add or modify substitute an existing entity.
        Example:
            message.custom_subs = {"bold": "<strong class=\"example\">{text}</strong>", "italic": "<i class=\"example\">{text}</i>", "mention": "<a href={url}>{text}</a>"}
            message.html_text
            >> "<strong class=\"example\">Test</strong> parse <i class=\"example\">formatting</i>, <a href=\"https://example.com\">url</a> and <a href=\"tg://user?id=123456\">text_mention</a> and mention <a href=\"https://t.me/username\">@username</a>"
        """

        if not entities:
            return text
        _subs = {
            "bold"     : "<b>{text}</b>",
            "italic"   : "<i>{text}</i>",
            "pre"      : "<pre>{text}</pre>",
            "code"     : "<code>{text}</code>",
            "url"      : "<a href=\"{url}\">{text}</a>",
            "text_link": "<a href=\"{url}\">{text}</a>"
        }
        if hasattr(self, "custom_subs"):
            for type in self.custom_subs:
                _subs[type] = self.custom_subs[type]
        utf16_text = text.encode("utf-16-le")
        html_text = ""

        def func(text, type=None, url=None, user=None):
            text = text.decode("utf-16-le")
            if type == "text_mention":
                type = "url"
                url = "tg://user?id={0}".format(user.id)
            elif type == "mention":
                url = "https://t.me/{0}".format(text[1:])
            if not type or not _subs.get(type):
                return text
            subs = _subs.get(type)
            text = text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
            return subs.format(text=text, url=url)

        offset = 0
        for entity in entities:
            if entity.offset > offset:
                html_text += func(utf16_text[offset * 2 : entity.offset * 2])
                offset = entity.offset
            html_text += func(utf16_text[offset * 2 : (offset + entity.length) * 2], entity.type, entity.url, entity.user)
            offset += entity.length
        if offset * 2 < len(utf16_text):
            html_text += func(utf16_text[offset * 2:])
        return html_text


Instruction:
How would you explain what the following Python 3 function does
def to_json(self):
        json_dict = {'keyboard': self.keyboard}
        if self.one_time_keyboard:
            json_dict['one_time_keyboard'] = True

        if self.resize_keyboard:
            json_dict['resize_keyboard'] = True

        if self.selective:
            json_dict['selective'] = True

        return json.dumps(json_dict)

Response: Converts this object to its json representation following the Telegram API guidelines described here.


Instruction:
How would you explain what the following Python 3 function does
def add(self, *args):
        i = 1
        row = []
        for button in args:
            row.append(button.to_dic())
            if i % self.row_width == 0:
                self.keyboard.append(row)
                row = []
            i += 1
        if len(row) > 0:
            self.keyboard.append(row)

Response: This function adds strings to the keyboard while not exceeding row_width.


Instruction:
Create a Python 3 function to
add a list of KeyboardButton to the keyboard. This function does not consider row_width.

Response:
def row(self, *args):
        """
        Adds a list of KeyboardButton to the keyboard. This function does not consider row_width.
        ReplyKeyboardMarkup#row("A")#row("B", "C")#to_json() outputs '{keyboard: [["A"], ["B", "C"]]}'
        See https://core.telegram.org/bots/api#inlinekeyboardmarkup
        :param args: strings
        :return: self, to allow function chaining.
        """
        btn_array = []
        for button in args:
            btn_array.append(button.to_dic())
        self.keyboard.append(btn_array)
        return self


Instruction:
Make a summary of the following Python 3 code
def _make_request(token, method_name, method='get', params=None, files=None, base_url=API_URL):
    request_url = base_url.format(token, method_name)
    logger.debug("Request: method={0} url={1} params={2} files={3}".format(method, request_url, params, files))
    read_timeout = READ_TIMEOUT
    connect_timeout = CONNECT_TIMEOUT
    if files and format_header_param:
        fields.format_header_param = _no_encode(format_header_param)
    if params:
        if 'timeout' in params: read_timeout = params['timeout'] + 10
        if 'connect-timeout' in params: connect_timeout = params['connect-timeout'] + 10
    result = _get_req_session().request(method, request_url, params=params, files=files,
                                        timeout=(connect_timeout, read_timeout), proxies=proxy)
    logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
    return _check_result(method_name, result)['result']

Response: Makes a request to the Telegram API.


Instruction:
Write a Python 3 script to
check whether the result of a method call is a valid API response.

Response:
def _check_result(method_name, result):
    """
    Checks whether `result` is a valid API response.
    A result is considered invalid if:
        - The server returned an HTTP response code other than 200
        - The content of the result is invalid JSON.
        - The method call was unsuccessful (The JSON 'ok' field equals False)

    :raises ApiException: if one of the above listed cases is applicable
    :param method_name: The name of the method called
    :param result: The returned result of the method request
    :return: The result parsed to a JSON dictionary.
    """
    if result.status_code != 200:
        msg = 'The server returned HTTP {0} {1}. Response body:\n[{2}]' \
            .format(result.status_code, result.reason, result.text.encode('utf8'))
        raise ApiException(msg, method_name, result)

    try:
        result_json = result.json()
    except:
        msg = 'The server returned an invalid JSON response. Response body:\n[{0}]' \
            .format(result.text.encode('utf8'))
        raise ApiException(msg, method_name, result)

    if not result_json['ok']:
        msg = 'Error code: {0} Description: {1}' \
            .format(result_json['error_code'], result_json['description'])
        raise ApiException(msg, method_name, result)
    return result_json


Instruction:
Write a Python 3 function that can
use this method to send text messages.

Response:
def send_message(token, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
                 parse_mode=None, disable_notification=None):
    """
    Use this method to send text messages. On success, the sent Message is returned.
    :param token:
    :param chat_id:
    :param text:
    :param disable_web_page_preview:
    :param reply_to_message_id:
    :param reply_markup:
    :param parse_mode:
    :param disable_notification:
    :return:
    """
    method_url = r'sendMessage'
    payload = {'chat_id': str(chat_id), 'text': text}
    if disable_web_page_preview:
        payload['disable_web_page_preview'] = disable_web_page_preview
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if parse_mode:
        payload['parse_mode'] = parse_mode
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, method='post')


Instruction:
Write a Python 3 script to
use this method to set the score of a user in a game.

Response:
def set_game_score(token, user_id, score, force=None, disable_edit_message=None, chat_id=None, message_id=None,
                   inline_message_id=None):
    """
    Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns True. Returns an error, if the new score is not greater than the user's current score in the chat.
    :param token: Bot's token (you don't need to fill this)
    :param user_id: User identifier
    :param score: New score, must be non-negative
    :param force: (Optional) Pass True, if the high score is allowed to decrease. This can be useful when fixing mistakes or banning cheaters
    :param disable_edit_message: (Optional) Pass True, if the game message should not be automatically edited to include the current scoreboard
    :param chat_id: (Optional, required if inline_message_id is not specified) Unique identifier for the target chat (or username of the target channel in the format @channelusername)
    :param message_id: (Optional, required if inline_message_id is not specified) Unique identifier of the sent message
    :param inline_message_id: (Optional, required if chat_id and message_id are not specified) Identifier of the inline message
    :return:
    """
    method_url = r'setGameScore'
    payload = {'user_id': user_id, 'score': score}
    if force:
        payload['force'] = force
    if chat_id:
        payload['chat_id'] = chat_id
    if message_id:
        payload['message_id'] = message_id
    if inline_message_id:
        payload['inline_message_id'] = inline_message_id
    if disable_edit_message:
        payload['disable_edit_message'] = disable_edit_message
    return _make_request(token, method_url, params=payload)


Instruction:
Can you create a Python 3 function that
uses this method to get data for high score tables. Will return the score of the specified user and several of his neighbors in a game. On success, returns an Array of GameHighScore objects. This method will currently return scores for the target user, plus two of his closest neighbors on each side. Will also return the top three users if the user and his neighbors are not among them. Please note that this behavior is subject to change. :param token: Bot's token (you don't need to fill this) :param user_id: Target user id :param chat_id: (Optional, required if inline_message_id is not specified) Unique identifier for the target chat (or username of the target channel in the format @channelusername) :param message_id: (Optional, required if inline_message_id is not specified) Unique identifier of the sent message :param inline_message_id: (Optional, required if chat_id and message_id are not specified) Identifier of the inline message :return:

Response:
def get_game_high_scores(token, user_id, chat_id=None, message_id=None, inline_message_id=None):
    """
    Use this method to get data for high score tables. Will return the score of the specified user and several of his neighbors in a game. On success, returns an Array of GameHighScore objects.
    This method will currently return scores for the target user, plus two of his closest neighbors on each side. Will also return the top three users if the user and his neighbors are not among them. Please note that this behavior is subject to change.
    :param token: Bot's token (you don't need to fill this)
    :param user_id: Target user id
    :param chat_id: (Optional, required if inline_message_id is not specified) Unique identifier for the target chat (or username of the target channel in the format @channelusername)
    :param message_id: (Optional, required if inline_message_id is not specified) Unique identifier of the sent message
    :param inline_message_id: (Optional, required if chat_id and message_id are not specified) Identifier of the inline message
    :return:
    """
    method_url = r'getGameHighScores'
    payload = {'user_id': user_id}
    if chat_id:
        payload['chat_id'] = chat_id
    if message_id:
        payload['message_id'] = message_id
    if inline_message_id:
        payload['inline_message_id'] = inline_message_id
    return _make_request(token, method_url, params=payload)


Instruction:
Create a Python 3 function for
using this method to send invoices. On success, the sent Message is returned. :param token: Bot's token (you don't need to fill this) :param chat_id: Unique identifier for the target private chat :param title: Product name :param description: Product description :param invoice_payload: Bot-defined invoice payload, 1-128 bytes. This will not be displayed to the user, use for your internal processes. :param provider_token: Payments provider token, obtained via @Botfather :param currency: Three-letter ISO 4217 currency code, see https://core.telegram.org/bots/payments#supported-currencies :param prices: Price breakdown, a list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.) :param start_parameter: Unique deep-linking parameter that can be used to generate this invoice when used as a start parameter :param photo_url: URL of the product photo for the invoice. Can be a photo of the goods or a marketing image for a service. People like it better when they see what they are paying for. :param photo_size: Photo size :param photo_width: Photo width :param photo_height: Photo height :param need_name: Pass True, if you require the user's full name to complete the order :param need_phone_number: Pass True, if you require the user's phone number to complete the order :param need_email: Pass True, if you require the user's email to complete the order :param need_shipping_address: Pass True, if you require the user's shipping address to complete the order :param is_flexible: Pass True, if the final price depends on the shipping method :param disable_notification: Sends the message silently. Users will receive a notification with no sound. :param reply_to_message_id: If the message is a reply, ID of the original message :param reply_markup: A JSON-serialized object for an inline keyboard. If empty, one 'Pay total price' button will be shown. If not empty, the first button must be a Pay button :param provider_data: :return:

Response:
def send_invoice(token, chat_id, title, description, invoice_payload, provider_token, currency, prices,
                 start_parameter, photo_url=None, photo_size=None, photo_width=None, photo_height=None,
                 need_name=None, need_phone_number=None, need_email=None, need_shipping_address=None, is_flexible=None,
                 disable_notification=None, reply_to_message_id=None, reply_markup=None, provider_data=None):
    """
    Use this method to send invoices. On success, the sent Message is returned.
    :param token: Bot's token (you don't need to fill this)
    :param chat_id: Unique identifier for the target private chat
    :param title: Product name
    :param description: Product description
    :param invoice_payload: Bot-defined invoice payload, 1-128 bytes. This will not be displayed to the user, use for your internal processes.
    :param provider_token: Payments provider token, obtained via @Botfather
    :param currency: Three-letter ISO 4217 currency code, see https://core.telegram.org/bots/payments#supported-currencies
    :param prices: Price breakdown, a list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.)
    :param start_parameter: Unique deep-linking parameter that can be used to generate this invoice when used as a start parameter
    :param photo_url: URL of the product photo for the invoice. Can be a photo of the goods or a marketing image for a service. People like it better when they see what they are paying for.
    :param photo_size: Photo size
    :param photo_width: Photo width
    :param photo_height: Photo height
    :param need_name: Pass True, if you require the user's full name to complete the order
    :param need_phone_number: Pass True, if you require the user's phone number to complete the order
    :param need_email: Pass True, if you require the user's email to complete the order
    :param need_shipping_address: Pass True, if you require the user's shipping address to complete the order
    :param is_flexible: Pass True, if the final price depends on the shipping method
    :param disable_notification: Sends the message silently. Users will receive a notification with no sound.
    :param reply_to_message_id: If the message is a reply, ID of the original message
    :param reply_markup: A JSON-serialized object for an inline keyboard. If empty, one 'Pay total price' button will be shown. If not empty, the first button must be a Pay button
    :param provider_data:
    :return:
    """
    method_url = r'sendInvoice'
    payload = {'chat_id': chat_id, 'title': title, 'description': description, 'payload': invoice_payload,
               'provider_token': provider_token, 'start_parameter': start_parameter, 'currency': currency,
               'prices': _convert_list_json_serializable(prices)}
    if photo_url:
        payload['photo_url'] = photo_url
    if photo_size:
        payload['photo_size'] = photo_size
    if photo_width:
        payload['photo_width'] = photo_width
    if photo_height:
        payload['photo_height'] = photo_height
    if need_name:
        payload['need_name'] = need_name
    if need_phone_number:
        payload['need_phone_number'] = need_phone_number
    if need_email:
        payload['need_email'] = need_email
    if need_shipping_address:
        payload['need_shipping_address'] = need_shipping_address
    if is_flexible:
        payload['is_flexible'] = is_flexible
    if disable_notification:
        payload['disable_notification'] = disable_notification
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if provider_data:
        payload['provider_data'] = provider_data
    return _make_request(token, method_url, params=payload)


Instruction:
Can you tell what is the following Python 3 function doing
def answer_shipping_query(token, shipping_query_id, ok, shipping_options=None, error_message=None):
    method_url = 'answerShippingQuery'
    payload = {'shipping_query_id': shipping_query_id, 'ok': ok}
    if shipping_options:
        payload['shipping_options'] = _convert_list_json_serializable(shipping_options)
    if error_message:
        payload['error_message'] = error_message
    return _make_request(token, method_url, params=payload)

Response: This method sends an invoice request to a shipping address.


Instruction:
Create a Python 3 function for
using this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned. Alternatively, the user can be redirected to the specified Game URL. For this option to work, you must first create a game for your bot via BotFather and accept the terms. Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter. :param token: Bot's token (you don't need to fill this) :param callback_query_id: Unique identifier for the query to be answered :param text: (Optional) Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters :param show_alert: (Optional) If true, an alert will be shown by the client instead of a notification at the top of the chat screen. Defaults to false. :param url: (Optional) URL that will be opened by the user's client. If you have created a Game and accepted the conditions via @Botfather, specify the URL that opens your game – note that this will only work if the query comes from a callback_game button. Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter. :param cache_time: (Optional) The maximum amount of time in seconds that the result of the callback query may be cached client-side. Telegram apps will support caching starting in version 3.14. Defaults to 0. :return:

Response:
def answer_callback_query(token, callback_query_id, text=None, show_alert=None, url=None, cache_time=None):
    """
    Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
    Alternatively, the user can be redirected to the specified Game URL. For this option to work, you must first create a game for your bot via BotFather and accept the terms. Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter.
    :param token: Bot's token (you don't need to fill this)
    :param callback_query_id: Unique identifier for the query to be answered
    :param text: (Optional) Text of the notification. If not specified, nothing will be shown to the user, 0-200 characters
    :param show_alert: (Optional) If true, an alert will be shown by the client instead of a notification at the top of the chat screen. Defaults to false.
    :param url: (Optional) URL that will be opened by the user's client. If you have created a Game and accepted the conditions via @Botfather, specify the URL that opens your game – note that this will only work if the query comes from a callback_game button.
    Otherwise, you may use links like telegram.me/your_bot?start=XXXX that open your bot with a parameter.
    :param cache_time: (Optional) The maximum amount of time in seconds that the result of the callback query may be cached client-side. Telegram apps will support caching starting in version 3.14. Defaults to 0.
    :return:
    """
    method_url = 'answerCallbackQuery'
    payload = {'callback_query_id': callback_query_id}
    if text:
        payload['text'] = text
    if show_alert:
        payload['show_alert'] = show_alert
    if url:
        payload['url'] = url
    if cache_time is not None:
        payload['cache_time'] = cache_time
    return _make_request(token, method_url, params=payload, method='post')


Instruction:
Explain what the following Python 3 code does
def enable_save_next_step_handlers(self, delay=120, filename="./.handler-saves/step.save"):
        self.next_step_saver = Saver(self.next_step_handlers, filename, delay)

Response: Enable saving next step handlers


Instruction:
Explain what the following Python 3 code does
def enable_save_reply_handlers(self, delay=120, filename="./.handler-saves/reply.save"):
        self.reply_saver = Saver(self.reply_handlers, filename, delay)

Response: Enable saving reply handlers for the current node.


Instruction:
Can you write a function in Python 3 where it
loads next step handlers from save file

Response:
def load_next_step_handlers(self, filename="./.handler-saves/step.save", del_file_after_loading=True):
        """
        Load next step handlers from save file

        :param filename: Filename of the file where handlers was saved
        :param del_file_after_loading: Is passed True, after loading save file will be deleted
        """
        self.next_step_saver.load_handlers(filename, del_file_after_loading)


Instruction:
Write a Python 3 script to
use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned. :param allowed_updates: Array of string. List the types of updates you want your bot to receive. :param offset: Integer. Identifier of the first update to be returned. :param limit: Integer. Limits the number of updates to be retrieved. :param timeout: Integer. Timeout in seconds for long polling. :return: array of Updates

Response:
def get_updates(self, offset=None, limit=None, timeout=20, allowed_updates=None):
        """
        Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
        :param allowed_updates: Array of string. List the types of updates you want your bot to receive.
        :param offset: Integer. Identifier of the first update to be returned.
        :param limit: Integer. Limits the number of updates to be retrieved.
        :param timeout: Integer. Timeout in seconds for long polling.
        :return: array of Updates
        """
        json_updates = apihelper.get_updates(self.token, offset, limit, timeout, allowed_updates)
        ret = []
        for ju in json_updates:
            ret.append(types.Update.de_json(ju))
        return ret


Instruction:
How would you implement a function in Python 3 that
gets and discard all pending updates before first poll of the bot

Response:
def __skip_updates(self):
        """
        Get and discard all pending updates before first poll of the bot
        :return: total updates skipped
        """
        total = 0
        updates = self.get_updates(offset=self.last_update_id, timeout=1)
        while updates:
            total += len(updates)
            for update in updates:
                if update.update_id > self.last_update_id:
                    self.last_update_id = update.update_id
            updates = self.get_updates(offset=self.last_update_id + 1, timeout=1)
        return total


Instruction:
Given the following Python 3 function, write the documentation
def __retrieve_updates(self, timeout=20):
        if self.skip_pending:
            logger.debug('Skipped {0} pending messages'.format(self.__skip_updates()))
            self.skip_pending = False
        updates = self.get_updates(offset=(self.last_update_id + 1), timeout=timeout)
        self.process_new_updates(updates)

Response: Retrieve any updates from Telegram API.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_user_profile_photos(self, user_id, offset=None, limit=None):
        result = apihelper.get_user_profile_photos(self.token, user_id, offset, limit)
        return types.UserProfilePhotos.de_json(result)

Response: Get the user profile photos of the person with user_id.


Instruction:
Write a Python 3 function that can
use this method to get up to date information about a chat.

Response:
def get_chat(self, chat_id):
        """
        Use this method to get up to date information about the chat (current name of the user for one-on-one
        conversations, current username of a user, group or channel, etc.). Returns a Chat object on success.
        :param chat_id:
        :return:
        """
        result = apihelper.get_chat(self.token, chat_id)
        return types.Chat.de_json(result)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def leave_chat(self, chat_id):
        result = apihelper.leave_chat(self.token, chat_id)
        return result

Response: Use this method to leave a chat. Returns True on success.


Instruction:
Create a Python 3 function to
use this method to get a list of administrators in a chat. On success returns an Array of ChatMember objects containing information about all administrators except other bots.

Response:
def get_chat_administrators(self, chat_id):
        """
        Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects
        that contains information about all chat administrators except other bots.
        :param chat_id:
        :return:
        """
        result = apihelper.get_chat_administrators(self.token, chat_id)
        ret = []
        for r in result:
            ret.append(types.ChatMember.de_json(r))
        return ret


Instruction:
Here you have a function in Python 3, explain what it does
def get_chat_members_count(self, chat_id):
        result = apihelper.get_chat_members_count(self.token, chat_id)
        return result

Response: Use this method to get the number of members in a chat. Returns Int on success.


Instruction:
Write a Python 3 function for
using this method to set a new group sticker set for a supergroup chat.

Response:
def set_chat_sticker_set(self, chat_id, sticker_set_name):
        """
        Use this method to set a new group sticker set for a supergroup. The bot must be an administrator
        in the chat for this to work and must have the appropriate admin rights.
        Use the field can_set_sticker_set optionally returned in getChat requests to check
        if the bot can use this method. Returns True on success.
        :param chat_id: Unique identifier for the target chat or username of the target supergroup
        (in the format @supergroupusername)
        :param sticker_set_name: Name of the sticker set to be set as the group sticker set
        :return:
        """
        result = apihelper.set_chat_sticker_set(self.token, chat_id, sticker_set_name)
        return result


Instruction:
Can you generate the documentation for the following Python 3 function
def delete_chat_sticker_set(self, chat_id):
        result = apihelper.delete_chat_sticker_set(self.token, chat_id)
        return result

Response: Use this method to delete a group sticker set from a supergroup chat.


Instruction:
Implement a Python 3 function for
using this method to get information about a member of a chat. Returns a ChatMember object on success.

Response:
def get_chat_member(self, chat_id, user_id):
        """
        Use this method to get information about a member of a chat. Returns a ChatMember object on success.
        :param chat_id:
        :param user_id:
        :return:
        """
        result = apihelper.get_chat_member(self.token, chat_id, user_id)
        return types.ChatMember.de_json(result)


Instruction:
Can you generate the documentation for the following Python 3 function
def send_message(self, chat_id, text, disable_web_page_preview=None, reply_to_message_id=None, reply_markup=None,
                     parse_mode=None, disable_notification=None):
        """
        Use this method to send text messages.

        Warning: Do not send more than about 5000 characters each message, otherwise you'll risk an HTTP 414 error.
        If you must send more than 5000 characters, use the split_string function in apihelper.py.

        :param chat_id:
        :param text:
        :param disable_web_page_preview:
        :param reply_to_message_id:
        :param reply_markup:
        :param parse_mode:
        :param disable_notification: Boolean, Optional. Sends the message silently.
        :return: API reply.
        """
        return types.Message.de_json(
            apihelper.send_message(self.token, chat_id, text, disable_web_page_preview, reply_to_message_id,
                                   reply_markup, parse_mode, disable_notification))

Response: Use this method to send text messages. You can use this method to send text messages. You can use the split_string function in apihelper. py.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def forward_message(self, chat_id, from_chat_id, message_id, disable_notification=None):
        return types.Message.de_json(
            apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification))

Response: Use this method to forward a message from one chat to another.