Instruction:
Here you have a function in Python 3, explain what it does
def valid_date(key, config_date):
    key_date = "/".join(key.split("/")[4:7])
    return parse_date(key_date) > parse_date(config_date)

Response: Checks that the key is not longer than the config date


Instruction:
Can you tell what is the following Python 3 function doing
def index(
        config, date=None, directory=None, concurrency=5, accounts=None,
        tag=None, verbose=False):
    """index traildbs directly from s3 for multiple accounts.

    context: assumes a daily traildb file in s3 with dated key path
    """

    logging.basicConfig(level=(verbose and logging.DEBUG or logging.INFO))
    logging.getLogger('botocore').setLevel(logging.WARNING)
    logging.getLogger('elasticsearch').setLevel(logging.WARNING)
    logging.getLogger('urllib3').setLevel(logging.WARNING)
    logging.getLogger('requests').setLevel(logging.WARNING)
    logging.getLogger('c7n.worker').setLevel(logging.INFO)

    with open(config) as fh:
        config = yaml.safe_load(fh.read())
    jsonschema.validate(config, CONFIG_SCHEMA)

    date = get_date_path(date, delta=24)
    directory = directory or "/tmp"

    with ProcessPoolExecutor(max_workers=concurrency) as w:
        futures = {}
        jobs = []

        for account in config.get('accounts'):
            if accounts and account['name'] not in accounts:
                continue
            if tag:
                found = False
                for t in account['tags'].values():
                    if tag == t:
                        found = True
                        break
                if not found:
                    continue
            for region in account.get('regions'):
                p = (config, account, region, date, directory)
                jobs.append(p)

        for j in jobs:
            log.debug("submit account:{} region:{} date:{}".format(
                j[1]['name'], j[2], j[3]))
            futures[w.submit(index_account_trails, *j)] = j

        # Process completed
        for f in as_completed(futures):
            config, account, region, date, directory = futures[f]
            if f.exception():
                log.warning("error account:{} region:{} error:{}".format(
                    account['name'], region, f.exception()))
                continue
            log.info("complete account:{} region:{}".format(
                account['name'], region))

Response: index traildbs directly from s3 for multiple accounts


Instruction:
Can you tell what is the following Python 3 function doing
def _initialize_session(self):

        # Only run once
        if self.credentials is not None:
            return

        tenant_auth_variables = [
            constants.ENV_TENANT_ID, constants.ENV_SUB_ID,
            constants.ENV_CLIENT_ID, constants.ENV_CLIENT_SECRET
        ]

        token_auth_variables = [
            constants.ENV_ACCESS_TOKEN, constants.ENV_SUB_ID
        ]

        msi_auth_variables = [
            constants.ENV_USE_MSI, constants.ENV_SUB_ID
        ]

        if self.authorization_file:
            self.credentials, self.subscription_id = self.load_auth_file(self.authorization_file)
            self.log.info("Creating session with authorization file")

        elif all(k in os.environ for k in token_auth_variables):
            # Token authentication
            self.credentials = BasicTokenAuthentication(
                token={
                    'access_token': os.environ[constants.ENV_ACCESS_TOKEN]
                })
            self.subscription_id = os.environ[constants.ENV_SUB_ID]
            self.log.info("Creating session with Token Authentication")
            self._is_token_auth = True

        elif all(k in os.environ for k in tenant_auth_variables):
            # Tenant (service principal) authentication
            self.credentials = ServicePrincipalCredentials(
                client_id=os.environ[constants.ENV_CLIENT_ID],
                secret=os.environ[constants.ENV_CLIENT_SECRET],
                tenant=os.environ[constants.ENV_TENANT_ID],
                resource=self.resource_namespace)
            self.subscription_id = os.environ[constants.ENV_SUB_ID]
            self.tenant_id = os.environ[constants.ENV_TENANT_ID]
            self.log.info("Creating session with Service Principal Authentication")

        elif all(k in os.environ for k in msi_auth_variables):
            # MSI authentication
            if constants.ENV_CLIENT_ID in os.environ:
                self.credentials = MSIAuthentication(
                    client_id=os.environ[constants.ENV_CLIENT_ID],
                    resource=self.resource_namespace)
            else:
                self.credentials = MSIAuthentication(
                    resource=self.resource_namespace)

            self.subscription_id = os.environ[constants.ENV_SUB_ID]
            self.log.info("Creating session with MSI Authentication")
        else:
            # Azure CLI authentication
            self._is_cli_auth = True
            (self.credentials,
             self.subscription_id,
             self.tenant_id) = Profile().get_login_credentials(
                resource=self.resource_namespace)
            self.log.info("Creating session with Azure CLI Authentication")

        # Let provided id parameter override everything else
        if self.subscription_id_override is not None:
            self.subscription_id = self.subscription_id_override

        self.log.info("Session using Subscription ID: %s" % self.subscription_id)

        if self.credentials is None:
            self.log.error('Unable to locate credentials for Azure session.')

Response: Initializes the session with the available authentication type.


Instruction:
Implement a function in Python 3 to
get the latest non - preview api version for a resource.

Response:
def resource_api_version(self, resource_id):
        """ latest non-preview api version for resource """

        namespace = ResourceIdParser.get_namespace(resource_id)
        resource_type = ResourceIdParser.get_resource_type(resource_id)

        cache_id = namespace + resource_type

        if cache_id in self._provider_cache:
            return self._provider_cache[cache_id]

        resource_client = self.client('azure.mgmt.resource.ResourceManagementClient')
        provider = resource_client.providers.get(namespace)

        rt = next((t for t in provider.resource_types
            if StringUtils.equal(t.resource_type, resource_type)), None)

        if rt and rt.api_versions:
            versions = [v for v in rt.api_versions if 'preview' not in v.lower()]
            api_version = versions[0] if versions else rt.api_versions[0]
            self._provider_cache[cache_id] = api_version
            return api_version


Instruction:
Create a Python 3 function for
building auth json string for deploying Azure Functions.

Response:
def get_functions_auth_string(self, target_subscription_id):
        """
        Build auth json string for deploying
        Azure Functions.  Look for dedicated
        Functions environment variables or
        fall back to normal Service Principal
        variables.

        """

        self._initialize_session()

        function_auth_variables = [
            constants.ENV_FUNCTION_TENANT_ID,
            constants.ENV_FUNCTION_CLIENT_ID,
            constants.ENV_FUNCTION_CLIENT_SECRET
        ]

        # Use dedicated function env vars if available
        if all(k in os.environ for k in function_auth_variables):
            auth = {
                'credentials':
                    {
                        'client_id': os.environ[constants.ENV_FUNCTION_CLIENT_ID],
                        'secret': os.environ[constants.ENV_FUNCTION_CLIENT_SECRET],
                        'tenant': os.environ[constants.ENV_FUNCTION_TENANT_ID]
                    },
                'subscription': target_subscription_id
            }

        elif type(self.credentials) is ServicePrincipalCredentials:
            auth = {
                'credentials':
                    {
                        'client_id': os.environ[constants.ENV_CLIENT_ID],
                        'secret': os.environ[constants.ENV_CLIENT_SECRET],
                        'tenant': os.environ[constants.ENV_TENANT_ID]
                    },
                'subscription': target_subscription_id
            }

        else:
            raise NotImplementedError(
                "Service Principal credentials are the only "
                "supported auth mechanism for deploying functions.")

        return json.dumps(auth, indent=2)


Instruction:
Can you implement a function in Python 3 that
queries a set of resources.

Response:
def filter(self, resource_manager, **params):
        """Query a set of resources."""
        m = self.resolve(resource_manager.resource_type)
        client = local_session(self.session_factory).client(
            m.service, resource_manager.config.region)
        enum_op, path, extra_args = m.enum_spec
        if extra_args:
            params.update(extra_args)
        return self._invoke_client_enum(
            client, enum_op, params, path,
            getattr(resource_manager, 'retry', None)) or []


Instruction:
Explain what the following Python 3 code does
def get(self, resource_manager, identities):
        m = self.resolve(resource_manager.resource_type)
        params = {}
        client_filter = False

        # Try to formulate server side query
        if m.filter_name:
            if m.filter_type == 'list':
                params[m.filter_name] = identities
            elif m.filter_type == 'scalar':
                assert len(identities) == 1, "Scalar server side filter"
                params[m.filter_name] = identities[0]
        else:
            client_filter = True

        resources = self.filter(resource_manager, **params)
        if client_filter:
            # This logic was added to prevent the issue from:
            # https://github.com/capitalone/cloud-custodian/issues/1398
            if all(map(lambda r: isinstance(r, six.string_types), resources)):
                resources = [r for r in resources if r in identities]
            else:
                resources = [r for r in resources if r[m.id] in identities]

        return resources

Response: Get resources by identities


Instruction:
How would you explain what the following Python 3 function does
def filter(self, resource_manager, **params):
        m = self.resolve(resource_manager.resource_type)
        client = local_session(self.session_factory).client(m.service)

        enum_op, path, extra_args = m.enum_spec
        if extra_args:
            params.update(extra_args)

        parent_type, parent_key, annotate_parent = m.parent_spec
        parents = self.manager.get_resource_manager(parent_type)
        parent_ids = [p[parents.resource_type.id] for p in parents.resources()]

        # Bail out with no parent ids...
        existing_param = parent_key in params
        if not existing_param and len(parent_ids) == 0:
            return []

        # Handle a query with parent id
        if existing_param:
            return self._invoke_client_enum(client, enum_op, params, path)

        # Have to query separately for each parent's children.
        results = []
        for parent_id in parent_ids:
            merged_params = self.get_parent_parameters(params, parent_id, parent_key)
            subset = self._invoke_client_enum(
                client, enum_op, merged_params, path, retry=self.manager.retry)
            if annotate_parent:
                for r in subset:
                    r[self.parent_key] = parent_id
            if subset and self.capture_parent_id:
                results.extend([(parent_id, s) for s in subset])
            elif subset:
                results.extend(subset)
        return results

Response: Query a set of resources.


Instruction:
Write a Python 3 function for
creating an api gw response from a wsgi app and environ.

Response:
def create_gw_response(app, wsgi_env):
    """Create an api gw response from a wsgi app and environ.
    """
    response = {}
    buf = []
    result = []

    def start_response(status, headers, exc_info=None):
        result[:] = [status, headers]
        return buf.append

    appr = app(wsgi_env, start_response)
    close_func = getattr(appr, 'close', None)
    try:
        buf.extend(list(appr))
    finally:
        close_func and close_func()

    response['body'] = ''.join(buf)
    response['statusCode'] = result[0].split(' ', 1)[0]
    response['headers'] = {}

    for k, v in result[1]:
        response['headers'][k] = v
    if 'Content-Length' not in response['headers']:
        response['headers']['Content-Length'] = str(len(response['body']))
    if 'Content-Type' not in response['headers']:
        response['headers']['Content-Type'] = 'text/plain'
    return response


Instruction:
Can you tell what is the following Python 3 function doing
def create_wsgi_request(event, server_name='apigw'):
    path = urllib.url2pathname(event['path'])
    script_name = (
        event['headers']['Host'].endswith('.amazonaws.com') and
        event['requestContext']['stage'] or '').encode('utf8')
    query = event['queryStringParameters']
    query_string = query and urllib.urlencode(query) or ""
    body = event['body'] and event['body'].encode('utf8') or ''

    environ = {
        'HTTPS': 'on',
        'PATH_INFO': path.encode('utf8'),
        'QUERY_STRING': query_string.encode('utf8'),
        'REMOTE_ADDR': event[
            'requestContext']['identity']['sourceIp'].encode('utf8'),
        'REQUEST_METHOD': event['httpMethod'].encode('utf8'),
        'SCRIPT_NAME': script_name,
        'SERVER_NAME': server_name.encode('utf8'),
        'SERVER_PORT': '80'.encode('utf8'),
        'SERVER_PROTOCOL': u'HTTP/1.1'.encode('utf8'),

        'wsgi.errors': sys.stderr,
        'wsgi.input': StringIO(body),
        'wsgi.multiprocess': False,
        'wsgi.multithread': False,
        'wsgi.run_once': False,
        'wsgi.url_scheme': u'https'.encode('utf8'),
        'wsgi.version': (1, 0),
    }

    headers = event['headers']
    # Input processing
    if event['httpMethod'] in ("POST", "PUT", "PATCH"):
        if 'Content-Type' in headers:
            environ['CONTENT_TYPE'] = headers['Content-Type']
        environ['CONTENT_LENGTH'] = str(len(body))

    for header in list(event['headers'].keys()):
        wsgi_name = "HTTP_" + header.upper().replace('-', '_')
        environ[wsgi_name] = headers[header].encode('utf8')

    if script_name:
        path_info = environ['PATH_INFO']
        if script_name in path_info:
            environ['PATH_INFO'].replace(script_name, '')

    # Extract remote user from event
    remote_user = None
    if event['requestContext'].get('authorizer'):
        remote_user = event[
            'requestContext']['authorizer'].get('principalId')
    elif event['requestContext'].get('identity'):
        remote_user = event['requestContext']['identity'].get('userArn')
    if remote_user:
        environ['REMOTE_USER'] = remote_user

    # apigw aware integrations
    environ['apigw.request'] = event['requestContext']
    environ['apigw.stagevars'] = event['stageVariables']

    return environ

Response: Create a wsgi environment from an apigw request.


Instruction:
Write a Python 3 function that can
create a LogRetriever from a client and lambda arn.

Response:
def create_from_lambda_arn(cls, client, lambda_arn):
        # type: (TypedAWSClient, str) -> LogRetriever
        """Create a LogRetriever from a client and lambda arn.

        :type client: botocore.client.Logs
        :param client: A ``logs`` client.

        :type lambda_arn: str
        :param lambda_arn: The ARN of the lambda function.

        :return: An instance of ``LogRetriever``.

        """
        lambda_name = lambda_arn.split(':')[6]
        log_group_name = '/aws/lambda/%s' % lambda_name
        return cls(client, log_group_name)


Instruction:
Write a Python 3 script to
retrieve the log entries from the log group.

Response:
def retrieve_logs(self, include_lambda_messages=True, max_entries=None):
        # type: (bool, Optional[int]) -> Iterator[Dict[str, Any]]
        """Retrieve logs from a log group.

        :type include_lambda_messages: boolean
        :param include_lambda_messages: Include logs generated by the AWS
            Lambda service.  If this value is False, only chalice logs will be
            included.

        :type max_entries: int
        :param max_entries: Maximum number of log messages to include.

        :rtype: iterator
        :return: An iterator that yields event dicts.  Each event
            dict has these keys:

            * logStreamName -> (string) The name of the log stream.
            * timestamp -> (datetime.datetime) - The timestamp for the msg.
            * message -> (string) The data contained in the log event.
            * ingestionTime -> (datetime.datetime) Ingestion time of event.
            * eventId -> (string) A unique identifier for this event.
            * logShortId -> (string) Short identifier for logStreamName.

        """
        # TODO: Add support for startTime/endTime.
        shown = 0
        for event in self._client.iter_log_events(self._log_group_name,
                                                  interleaved=True):
            if not include_lambda_messages and \
                    self._is_lambda_message(event):
                continue
            # logStreamName is: '2016/07/05/[id]hash'
            # We want to extract the hash portion and
            # provide a short identifier.
            identifier = event['logStreamName']
            if ']' in identifier:
                index = identifier.find(']')
                identifier = identifier[index + 1:index + 7]
            event['logShortId'] = identifier
            yield event
            shown += 1
            if max_entries is not None and shown >= max_entries:
                return


Instruction:
Write a Python 3 script for
validating app configuration. The purpose of this method is to provide a fail fast mechanism for anything we know is going to fail deployment. We can detect common error cases and provide the user with helpful error messages.

Response:
def validate_configuration(config):
    # type: (Config) -> None
    """Validate app configuration.

    The purpose of this method is to provide a fail fast mechanism
    for anything we know is going to fail deployment.
    We can detect common error cases and provide the user with helpful
    error messages.

    """
    routes = config.chalice_app.routes
    validate_routes(routes)
    validate_route_content_types(routes, config.chalice_app.api.binary_types)
    _validate_manage_iam_role(config)
    validate_python_version(config)
    validate_unique_function_names(config)
    validate_feature_flags(config.chalice_app)


Instruction:
Create a Python 3 function for
validating that the given configuration matches a specific python version.

Response:
def validate_python_version(config, actual_py_version=None):
    # type: (Config, Optional[str]) -> None
    """Validate configuration matches a specific python version.

    If the ``actual_py_version`` is not provided, it will default
    to the major/minor version of the currently running python
    interpreter.

    :param actual_py_version: The major/minor python version in
        the form "pythonX.Y", e.g "python2.7", "python3.6".

    """
    lambda_version = config.lambda_python_version
    if actual_py_version is None:
        actual_py_version = 'python%s.%s' % sys.version_info[:2]
    if actual_py_version != lambda_version:
        # We're not making this a hard error for now, but we may
        # turn this into a hard fail.
        warnings.warn("You are currently running %s, but the closest "
                      "supported version on AWS Lambda is %s\n"
                      "Please use %s, otherwise you may run into "
                      "deployment issues. " %
                      (actual_py_version, lambda_version, lambda_version),
                      stacklevel=2)


Instruction:
Given the following Python 3 function, write the documentation
def _execute(self,
                 command,        # type: str
                 args,           # type: List[str]
                 env_vars=None,  # type: EnvVars
                 shim=None       # type: OptStr
                 ):
        # type: (...) -> Tuple[int, bytes, bytes]
        """Execute a pip command with the given arguments."""
        main_args = [command] + args
        logger.debug("calling pip %s", ' '.join(main_args))
        rc, out, err = self._wrapped_pip.main(main_args, env_vars=env_vars,
                                              shim=shim)
        return rc, out, err

Response: Execute a pip command with the given arguments.


Instruction:
Write a Python 3 function for
building an sdist into a wheel file.

Response:
def build_wheel(self, wheel, directory, compile_c=True):
        # type: (str, str, bool) -> None
        """Build an sdist into a wheel file."""
        arguments = ['--no-deps', '--wheel-dir', directory, wheel]
        env_vars = self._osutils.environ()
        shim = ''
        if not compile_c:
            env_vars.update(pip_no_compile_c_env_vars)
            shim = pip_no_compile_c_shim
        # Ignore rc and stderr from this command since building the wheels
        # may fail and we will find out when we categorize the files that were
        # generated.
        self._execute('wheel', arguments,
                      env_vars=env_vars, shim=shim)


Instruction:
Given the following Python 3 function, write the documentation
def download_all_dependencies(self, requirements_filename, directory):
        # type: (str, str) -> None
        """Download all dependencies as sdist or wheel."""
        arguments = ['-r', requirements_filename, '--dest', directory]
        rc, out, err = self._execute('download', arguments)
        # When downloading all dependencies we expect to get an rc of 0 back
        # since we are casting a wide net here letting pip have options about
        # what to download. If a package is not found it is likely because it
        # does not exist and was mispelled. In this case we raise an error with
        # the package name. Otherwise a nonzero rc results in a generic
        # download error where we pass along the stderr.
        if rc != 0:
            if err is None:
                err = b'Unknown error'
            error = err.decode()
            match = re.search(("Could not find a version that satisfies the "
                               "requirement (.+?) "), error)
            if match:
                package_name = match.group(1)
                raise NoSuchPackageError(str(package_name))
            raise PackageDownloadError(error)
        stdout = out.decode()
        matches = re.finditer(self._LINK_IS_DIR_PATTERN, stdout)
        for match in matches:
            wheel_package_path = str(match.group(1))
            # Looks odd we do not check on the error status of building the
            # wheel here. We can assume this is a valid package path since
            # we already passed the pip download stage. This stage would have
            # thrown a PackageDownloadError if any of the listed packages were
            # not valid.
            # If it fails the actual build step, it will have the same behavior
            # as any other package we fail to build a valid wheel for, and
            # complain at deployment time.
            self.build_wheel(wheel_package_path, directory)

Response: Download all dependencies as sdist or wheel.


Instruction:
Implement a Python 3 function for
downloading wheel files for manylinux for all the given packages.

Response:
def download_manylinux_wheels(self, abi, packages, directory):
        # type: (str, List[str], str) -> None
        """Download wheel files for manylinux for all the given packages."""
        # If any one of these dependencies fails pip will bail out. Since we
        # are only interested in all the ones we can download, we need to feed
        # each package to pip individually. The return code of pip doesn't
        # matter here since we will inspect the working directory to see which
        # wheels were downloaded. We are only interested in wheel files
        # compatible with lambda, which means manylinux1_x86_64 platform and
        # cpython implementation. The compatible abi depends on the python
        # version and is checked later.
        for package in packages:
            arguments = ['--only-binary=:all:', '--no-deps', '--platform',
                         'manylinux1_x86_64', '--implementation', 'cp',
                         '--abi', abi, '--dest', directory, package]
            self._execute('download', arguments)


Instruction:
Make a summary of the following Python 3 code
def to_cfn_resource_name(name):
    # type: (str) -> str
    """Transform a name to a valid cfn name.

    This will convert the provided name to a CamelCase name.
    It's possible that the conversion to a CFN resource name
    can result in name collisions.  It's up to the caller
    to handle name collisions appropriately.

    """
    if not name:
        raise ValueError("Invalid name: %r" % name)
    word_separators = ['-', '_']
    for word_separator in word_separators:
        word_parts = [p for p in name.split(word_separator) if p]
        name = ''.join([w[0].upper() + w[1:] for w in word_parts])
    return re.sub(r'[^A-Za-z0-9]+', '', name)

Response: Transform a name to a valid CFN resource name.


Instruction:
Can you write a function in Python 3 where it
deletes a top level key from the deployed JSON file.

Response:
def remove_stage_from_deployed_values(key, filename):
    # type: (str, str) -> None
    """Delete a top level key from the deployed JSON file."""
    final_values = {}  # type: Dict[str, Any]
    try:
        with open(filename, 'r') as f:
            final_values = json.load(f)
    except IOError:
        # If there is no file to delete from, then this funciton is a noop.
        return

    try:
        del final_values[key]
        with open(filename, 'wb') as f:
            data = serialize_to_json(final_values)
            f.write(data.encode('utf-8'))
    except KeyError:
        # If they key didn't exist then there is nothing to remove.
        pass


Instruction:
Create a Python 3 function to
record deployed values to a JSON file.

Response:
def record_deployed_values(deployed_values, filename):
    # type: (Dict[str, Any], str) -> None
    """Record deployed values to a JSON file.

    This allows subsequent deploys to lookup previously deployed values.

    """
    final_values = {}  # type: Dict[str, Any]
    if os.path.isfile(filename):
        with open(filename, 'r') as f:
            final_values = json.load(f)
    final_values.update(deployed_values)
    with open(filename, 'wb') as f:
        data = serialize_to_json(final_values)
        f.write(data.encode('utf-8'))


Instruction:
Can you tell what is the following Python 3 function doing
def create_zip_file(source_dir, outfile):
    # type: (str, str) -> None
    """Create a zip file from a source input directory.

    This function is intended to be an equivalent to
    `zip -r`.  You give it a source directory, `source_dir`,
    and it will recursively zip up the files into a zipfile
    specified by the `outfile` argument.

    """
    with zipfile.ZipFile(outfile, 'w',
                         compression=zipfile.ZIP_DEFLATED) as z:
        for root, _, filenames in os.walk(source_dir):
            for filename in filenames:
                full_name = os.path.join(root, filename)
                archive_name = os.path.relpath(full_name, source_dir)
                z.write(full_name, archive_name)

Response: Create a zip file from a source directory.


Instruction:
Can you tell what is the following Python 3 function doing
def update_function(self,
                        function_name,               # type: str
                        zip_contents,                # type: str
                        environment_variables=None,  # type: StrMap
                        runtime=None,                # type: OptStr
                        tags=None,                   # type: StrMap
                        timeout=None,                # type: OptInt
                        memory_size=None,            # type: OptInt
                        role_arn=None,               # type: OptStr
                        subnet_ids=None,             # type: OptStrList
                        security_group_ids=None,     # type: OptStrList
                        layers=None,                 # type: OptStrList
                        ):
        # type: (...) -> Dict[str, Any]
        """Update a Lambda function's code and configuration.

        This method only updates the values provided to it. If a parameter
        is not provided, no changes will be made for that that parameter on
        the targeted lambda function.
        """
        return_value = self._update_function_code(function_name=function_name,
                                                  zip_contents=zip_contents)
        self._update_function_config(
            environment_variables=environment_variables,
            runtime=runtime,
            timeout=timeout,
            memory_size=memory_size,
            role_arn=role_arn,
            subnet_ids=subnet_ids,
            security_group_ids=security_group_ids,
            function_name=function_name,
            layers=layers
        )
        if tags is not None:
            self._update_function_tags(return_value['FunctionArn'], tags)
        return return_value

Response: Updates a Lambda function s code and configuration.


Instruction:
Can you tell what is the following Python 3 function doing
def delete_role(self, name):
        # type: (str) -> None
        """Delete a role by first deleting all inline policies."""
        client = self._client('iam')
        inline_policies = client.list_role_policies(
            RoleName=name
        )['PolicyNames']
        for policy_name in inline_policies:
            self.delete_role_policy(name, policy_name)
        client.delete_role(RoleName=name)

Response: Delete a role by first deleting all inline policies.


Instruction:
Can you create a Python 3 function that
gets the rest api id associated with an API name.

Response:
def get_rest_api_id(self, name):
        # type: (str) -> Optional[str]
        """Get rest api id associated with an API name.

        :type name: str
        :param name: The name of the rest api.

        :rtype: str
        :return: If the rest api exists, then the restApiId
            is returned, otherwise None.

        """
        rest_apis = self._client('apigateway').get_rest_apis()['items']
        for api in rest_apis:
            if api['name'] == name:
                return api['id']
        return None


Instruction:
Create a Python 3 function to
check if an API Gateway REST API exists.

Response:
def rest_api_exists(self, rest_api_id):
        # type: (str) -> bool
        """Check if an an API Gateway REST API exists."""
        client = self._client('apigateway')
        try:
            client.get_rest_api(restApiId=rest_api_id)
            return True
        except client.exceptions.NotFoundException:
            return False


Instruction:
Can you create a Python 3 function that
authorizes API gateway to invoke a lambda function.

Response:
def add_permission_for_apigateway(self, function_name,
                                      region_name, account_id,
                                      rest_api_id, random_id=None):
        # type: (str, str, str, str, Optional[str]) -> None
        """Authorize API gateway to invoke a lambda function is needed.

        This method will first check if API gateway has permission to call
        the lambda function, and only if necessary will it invoke
        ``self.add_permission_for_apigateway(...).

        """
        source_arn = self._build_source_arn_str(region_name, account_id,
                                                rest_api_id)
        self._add_lambda_permission_if_needed(
            source_arn=source_arn,
            function_arn=function_name,
            service_name='apigateway',
        )


Instruction:
How would you implement a function in Python 3 that
returns the function policy for a lambda function.

Response:
def get_function_policy(self, function_name):
        # type: (str) -> Dict[str, Any]
        """Return the function policy for a lambda function.

        This function will extract the policy string as a json document
        and return the json.loads(...) version of the policy.

        """
        client = self._client('lambda')
        try:
            policy = client.get_policy(FunctionName=function_name)
            return json.loads(policy['Policy'])
        except client.exceptions.ResourceNotFoundException:
            return {'Statement': []}


Instruction:
Can you generate a brief explanation for the following Python 3 code
def download_sdk(self, rest_api_id, output_dir,
                     api_gateway_stage=DEFAULT_STAGE_NAME,
                     sdk_type='javascript'):
        # type: (str, str, str, str) -> None
        """Download an SDK to a directory.

        This will generate an SDK and download it to the provided
        ``output_dir``.  If you're using ``get_sdk_download_stream()``,
        you have to handle downloading the stream and unzipping the
        contents yourself.  This method handles that for you.

        """
        zip_stream = self.get_sdk_download_stream(
            rest_api_id, api_gateway_stage=api_gateway_stage,
            sdk_type=sdk_type)
        tmpdir = tempfile.mkdtemp()
        with open(os.path.join(tmpdir, 'sdk.zip'), 'wb') as f:
            f.write(zip_stream.read())
        tmp_extract = os.path.join(tmpdir, 'extracted')
        with zipfile.ZipFile(os.path.join(tmpdir, 'sdk.zip')) as z:
            z.extractall(tmp_extract)
        # The extract zip dir will have a single directory:
        #  ['apiGateway-js-sdk']
        dirnames = os.listdir(tmp_extract)
        if len(dirnames) == 1:
            full_dirname = os.path.join(tmp_extract, dirnames[0])
            if os.path.isdir(full_dirname):
                final_dirname = 'chalice-%s-sdk' % sdk_type
                full_renamed_name = os.path.join(tmp_extract, final_dirname)
                os.rename(full_dirname, full_renamed_name)
                shutil.move(full_renamed_name, output_dir)
                return
        raise RuntimeError(
            "The downloaded SDK had an unexpected directory structure: %s" %
            (', '.join(dirnames)))

Response: Download an SDK to a directory.


Instruction:
Can you create a Python 3 function that
generates an SDK for a given API Gateway stage and type and return a file like object that streams the contents of the generated SDK.

Response:
def get_sdk_download_stream(self, rest_api_id,
                                api_gateway_stage=DEFAULT_STAGE_NAME,
                                sdk_type='javascript'):
        # type: (str, str, str) -> file
        """Generate an SDK for a given SDK.

        Returns a file like object that streams a zip contents for the
        generated SDK.

        """
        response = self._client('apigateway').get_sdk(
            restApiId=rest_api_id, stageName=api_gateway_stage,
            sdkType=sdk_type)
        return response['body']


Instruction:
Write a Python 3 script for
verifying that the topic and function name of a topic is the current topic.

Response:
def verify_sns_subscription_current(self, subscription_arn, topic_name,
                                        function_arn):
        # type: (str, str, str) -> bool
        """Verify a subscription arn matches the topic and function name.

        Given a subscription arn, verify that the associated topic name
        and function arn match up to the parameters passed in.

        """
        sns_client = self._client('sns')
        try:
            attributes = sns_client.get_subscription_attributes(
                SubscriptionArn=subscription_arn)['Attributes']
            return (
                # Splitting on ':' is safe because topic names can't have
                # a ':' char.
                attributes['TopicArn'].rsplit(':', 1)[1] == topic_name and
                attributes['Endpoint'] == function_arn
            )
        except sns_client.exceptions.NotFoundException:
            return False


Instruction:
Write a Python 3 function that can
connect an S3 bucket to a Lambda function.

Response:
def connect_s3_bucket_to_lambda(self, bucket, function_arn, events,
                                    prefix=None, suffix=None):
        # type: (str, str, List[str], OptStr, OptStr) -> None
        """Configure S3 bucket to invoke a lambda function.

        The S3 bucket must already have permission to invoke the
        lambda function before you call this function, otherwise
        the service will return an error.  You can add permissions
        by using the ``add_permission_for_s3_event`` below.  The
        ``events`` param matches the event strings supported by the
        service.

        This method also only supports a single prefix/suffix for now,
        which is what's offered in the Lambda console.

        """
        s3 = self._client('s3')
        existing_config = s3.get_bucket_notification_configuration(
            Bucket=bucket)
        # Because we're going to PUT this config back to S3, we need
        # to remove `ResponseMetadata` because that's added in botocore
        # and isn't a param of the put_bucket_notification_configuration.
        existing_config.pop('ResponseMetadata', None)
        existing_lambda_config = existing_config.get(
            'LambdaFunctionConfigurations', [])
        single_config = {
            'LambdaFunctionArn': function_arn, 'Events': events
        }  # type: Dict[str, Any]
        filter_rules = []
        if prefix is not None:
            filter_rules.append({'Name': 'Prefix', 'Value': prefix})
        if suffix is not None:
            filter_rules.append({'Name': 'Suffix', 'Value': suffix})
        if filter_rules:
            single_config['Filter'] = {'Key': {'FilterRules': filter_rules}}
        new_config = self._merge_s3_notification_config(existing_lambda_config,
                                                        single_config)
        existing_config['LambdaFunctionConfigurations'] = new_config
        s3.put_bucket_notification_configuration(
            Bucket=bucket,
            NotificationConfiguration=existing_config,
        )


Instruction:
How would you implement a function in Python 3 that
verifies that the uuid matches the resource and function arn provided.

Response:
def verify_event_source_current(self, event_uuid, resource_name,
                                    service_name, function_arn):
        # type: (str, str, str, str) -> bool
        """Check if the uuid matches the resource and function arn provided.

        Given a uuid representing an event source mapping for a lambda
        function, verify that the associated source arn
        and function arn match up to the parameters passed in.

        Instead of providing the event source arn, the resource name
        is provided along with the service name.  For example, if we're
        checking an SQS queue event source, the resource name would be
        the queue name (e.g. ``myqueue``) and the service would be ``sqs``.

        """
        client = self._client('lambda')
        try:
            attributes = client.get_event_source_mapping(UUID=event_uuid)
            actual_arn = attributes['EventSourceArn']
            arn_start, actual_name = actual_arn.rsplit(':', 1)
            return (
                actual_name == resource_name and
                arn_start.startswith('arn:aws:%s' % service_name) and
                attributes['FunctionArn'] == function_arn
            )
        except client.exceptions.ResourceNotFoundException:
            return False


Instruction:
Create a Python 3 function for
loading the chalice config file from the project directory.

Response:
def load_project_config(self):
        # type: () -> Dict[str, Any]
        """Load the chalice config file from the project directory.

        :raise: OSError/IOError if unable to load the config file.

        """
        config_file = os.path.join(self.project_dir, '.chalice', 'config.json')
        with open(config_file) as f:
            return json.loads(f.read())


Instruction:
Write a Python 3 script to
invoke the deployed lambda function NAME.

Response:
def invoke(ctx, name, profile, stage):
    # type: (click.Context, str, str, str) -> None
    """Invoke the deployed lambda function NAME."""
    factory = ctx.obj['factory']  # type: CLIFactory
    factory.profile = profile

    try:
        invoke_handler = factory.create_lambda_invoke_handler(name, stage)
        payload = factory.create_stdin_reader().read()
        invoke_handler.invoke(payload)
    except NoSuchFunctionError as e:
        err = click.ClickException(
            "could not find a lambda function named %s." % e.name)
        err.exit_code = 2
        raise err
    except botocore.exceptions.ClientError as e:
        error = e.response['Error']
        err = click.ClickException(
            "got '%s' exception back from Lambda\n%s"
            % (error['Code'], error['Message']))
        err.exit_code = 1
        raise err
    except UnhandledLambdaError:
        err = click.ClickException(
            "Unhandled exception in Lambda function, details above.")
        err.exit_code = 1
        raise err
    except ReadTimeout as e:
        err = click.ClickException(e.message)
        err.exit_code = 1
        raise err


Instruction:
Implement a Python 3 function for
generating a CodePipeline for a given codebuild image and source.

Response:
def generate_pipeline(ctx, codebuild_image, source, buildspec_file, filename):
    # type: (click.Context, str, str, str, str) -> None
    """Generate a cloudformation template for a starter CD pipeline.

    This command will write a starter cloudformation template to
    the filename you provide.  It contains a CodeCommit repo,
    a CodeBuild stage for packaging your chalice app, and a
    CodePipeline stage to deploy your application using cloudformation.

    You can use any AWS SDK or the AWS CLI to deploy this stack.
    Here's an example using the AWS CLI:

        \b
        $ chalice generate-pipeline pipeline.json
        $ aws cloudformation deploy --stack-name mystack \b
            --template-file pipeline.json --capabilities CAPABILITY_IAM
    """
    from chalice import pipeline
    factory = ctx.obj['factory']  # type: CLIFactory
    config = factory.create_config_obj()
    p = pipeline.CreatePipelineTemplate()
    params = pipeline.PipelineParameters(
        app_name=config.app_name,
        lambda_python_version=config.lambda_python_version,
        codebuild_image=codebuild_image,
        code_source=source,
    )
    output = p.create_template(params)
    if buildspec_file:
        extractor = pipeline.BuildSpecExtractor()
        buildspec_contents = extractor.extract_buildspec(output)
        with open(buildspec_file, 'w') as f:
            f.write(buildspec_contents)
    with open(filename, 'w') as f:
        f.write(serialize_to_json(output))


Instruction:
How would you implement a function in Python 3 that
returns a list of deployed resources associated with a given stage.

Response:
def deployed_resources(self, chalice_stage_name):
        # type: (str) -> DeployedResources
        """Return resources associated with a given stage.

        If a deployment to a given stage has never happened,
        this method will return a value of None.

        """
        # This is arguably the wrong level of abstraction.
        # We might be able to move this elsewhere.
        deployed_file = os.path.join(
            self.project_dir, '.chalice', 'deployed',
            '%s.json' % chalice_stage_name)
        data = self._load_json_file(deployed_file)
        if data is not None:
            schema_version = data.get('schema_version', '1.0')
            if schema_version != '2.0':
                raise ValueError("Unsupported schema version (%s) in file: %s"
                                 % (schema_version, deployed_file))
            return DeployedResources(data)
        return self._try_old_deployer_values(chalice_stage_name)


Instruction:
Make a summary of the following Python 3 code
def generate_policy(self, config):
        # type: (Config) -> Dict[str, Any]
        """Auto generate policy for an application."""
        # Admittedly, this is pretty bare bones logic for the time
        # being.  All it really does it work out, given a Config instance,
        # which files need to analyzed and then delegates to the
        # appropriately analyzer functions to do the real work.
        # This may change in the future.
        app_py = os.path.join(config.project_dir, 'app.py')
        assert self._osutils.file_exists(app_py)
        app_source = self._osutils.get_file_contents(app_py, binary=False)
        app_policy = policy_from_source_code(app_source)
        app_policy['Statement'].append(CLOUDWATCH_LOGS)
        if config.subnet_ids and config.security_group_ids:
            app_policy['Statement'].append(VPC_ATTACH_POLICY)
        return app_policy

Response: Auto generate policy for an application.


Instruction:
Create a Python 3 function to
return all clients calls made in provided source code.

Response:
def get_client_calls(source_code):
    # type: (str) -> APICallT
    """Return all clients calls made in provided source code.

    :returns: A dict of service_name -> set([client calls]).
        Example: {"s3": set(["list_objects", "create_bucket"]),
                  "dynamodb": set(["describe_table"])}
    """
    parsed = parse_code(source_code)
    t = SymbolTableTypeInfer(parsed)
    binder = t.bind_types()
    collector = APICallCollector(binder)
    api_calls = collector.collect_api_calls(parsed.parsed_ast)
    return api_calls


Instruction:
Implement a function in Python 3 to
return client calls for a chalice app.

Response:
def get_client_calls_for_app(source_code):
    # type: (str) -> APICallT
    """Return client calls for a chalice app.

    This is similar to ``get_client_calls`` except it will
    automatically traverse into chalice views with the assumption
    that they will be called.

    """
    parsed = parse_code(source_code)
    parsed.parsed_ast = AppViewTransformer().visit(parsed.parsed_ast)
    ast.fix_missing_locations(parsed.parsed_ast)
    t = SymbolTableTypeInfer(parsed)
    binder = t.bind_types()
    collector = APICallCollector(binder)
    api_calls = collector.collect_api_calls(parsed.parsed_ast)
    return api_calls


Instruction:
Create a Python 3 function to
match the url against a set of known routes.

Response:
def match_route(self, url):
        # type: (str) -> MatchResult
        """Match the url against known routes.

        This method takes a concrete route "/foo/bar", and
        matches it against a set of routes.  These routes can
        use param substitution corresponding to API gateway patterns.
        For example::

            match_route('/foo/bar') -> '/foo/{name}'

        """
        # Otherwise we need to check for param substitution
        parsed_url = urlparse(url)
        parsed_qs = parse_qs(parsed_url.query, keep_blank_values=True)
        query_params = {k: v[-1] for k, v in parsed_qs.items()}
        path = parsed_url.path
        # API Gateway removes the trailing slash if the route is not the root
        # path. We do the same here so our route matching works the same way.
        if path != '/' and path.endswith('/'):
            path = path[:-1]
        parts = path.split('/')
        captured = {}
        for route_url in self.route_urls:
            url_parts = route_url.split('/')
            if len(parts) == len(url_parts):
                for i, j in zip(parts, url_parts):
                    if j.startswith('{') and j.endswith('}'):
                        captured[j[1:-1]] = i
                        continue
                    if i != j:
                        break
                else:
                    return MatchResult(route_url, captured, query_params)
        raise ValueError("No matching route found for: %s" % url)


Instruction:
Can you tell what is the following Python 3 function doing
def _prepare_authorizer_event(self, arn, lambda_event, lambda_context):
        # type: (str, EventType, LambdaContext) -> EventType
        """Translate event for an authorizer input."""
        authorizer_event = lambda_event.copy()
        authorizer_event['type'] = 'TOKEN'
        try:
            authorizer_event['authorizationToken'] = authorizer_event.get(
                'headers', {})['authorization']
        except KeyError:
            raise NotAuthorizedError(
                {'x-amzn-RequestId': lambda_context.aws_request_id,
                 'x-amzn-ErrorType': 'UnauthorizedException'},
                b'{"message":"Unauthorized"}')
        authorizer_event['methodArn'] = arn
        return authorizer_event

Response: Translate an authorizer input into a dictionary of fields that can be used to send to the authorizer.


Instruction:
Write a Python 3 function that can
set initial parameters for default modulator if it was not edited by user previously.

Response:
def bootstrap_modulator(self, protocol: ProtocolAnalyzer):
        """
        Set initial parameters for default modulator if it was not edited by user previously
        :return:
        """
        if len(self.modulators) != 1 or len(self.table_model.protocol.messages) == 0:
            return

        modulator = self.modulators[0]
        modulator.samples_per_bit = protocol.messages[0].bit_len

        if protocol.signal:
            modulator.sample_rate = protocol.signal.sample_rate
            modulator.modulation_type = protocol.signal.modulation_type
            auto_freq = modulator.estimate_carrier_frequency(protocol.signal, protocol)
            if auto_freq is not None and auto_freq != 0:
                modulator.carrier_freq_hz = auto_freq

        self.show_modulation_info()


Instruction:
Write a Python 3 function that can
modulate the data of the current state of the modulated message.

Response:
def modulate_data(self, buffer: np.ndarray) -> np.ndarray:
        """

        :param buffer: Buffer in which the modulated data shall be written, initialized with zeros
        :return:
        """
        self.ui.prBarGeneration.show()
        self.ui.prBarGeneration.setValue(0)
        self.ui.prBarGeneration.setMaximum(self.table_model.row_count)
        self.modulation_msg_indices.clear()

        pos = 0
        for i in range(0, self.table_model.row_count):
            message = self.table_model.protocol.messages[i]
            modulator = self.__get_modulator_of_message(message)
            # We do not need to modulate the pause extra, as result is already initialized with zeros
            modulated = modulator.modulate(start=0, data=message.encoded_bits, pause=0)
            buffer[pos:pos + len(modulated)] = modulated
            pos += len(modulated) + message.pause
            self.modulation_msg_indices.append(pos)
            self.ui.prBarGeneration.setValue(i + 1)
            QApplication.instance().processEvents()

        self.ui.prBarGeneration.hide()
        return buffer


Instruction:
Can you tell what is the following Python 3 function doing
def refresh_existing_encodings(self, encodings_from_file):
        update = False

        for msg in self.table_model.protocol.messages:
            i = next((i for i, d in enumerate(encodings_from_file) if d.name == msg.decoder.name), 0)
            if msg.decoder != encodings_from_file[i]:
                update = True
                msg.decoder = encodings_from_file[i]
                msg.clear_decoded_bits()
                msg.clear_encoded_bits()

        if update:
            self.refresh_table()
            self.refresh_estimated_time()

Response: Refresh existing encodings for messages when encoding was changed by user in dialog.


Instruction:
Given the following Python 3 function, write the documentation
def protocols(self):
        if self.__protocols is None:
            self.__protocols = self.proto_tree_model.protocols
        return self.__protocols

Response: returns a list of ProtocolAnalyzer objects for the current node


Instruction:
How would you code a function in Python 3 to
return a list of visible protocols in this hierarchy.

Response:
def protocol_list(self):
        """
        :return: visible protocols
        :rtype: list of ProtocolAnalyzer
        """
        result = []
        for group in self.groups:
            result.extend(group.protocols)
        return result


Instruction:
Can you tell what is the following Python 3 function doing
def full_protocol_list(self):
        result = []
        for group in self.groups:
            result.extend(group.all_protocols)
        return result

Response: returns a list of all protocols including not shown ones


Instruction:
Here you have a function in Python 3, explain what it does
def refresh_existing_encodings(self):
        update = False

        for msg in self.proto_analyzer.messages:
            i = next((i for i, d in enumerate(self.decodings) if d.name == msg.decoder.name), 0)
            if msg.decoder != self.decodings[i]:
                update = True
                msg.decoder = self.decodings[i]
                msg.clear_decoded_bits()
                msg.clear_encoded_bits()

        if update:
            self.protocol_model.update()
            self.label_value_model.update()

Response: Refresh existing encodings for messages when encoding was changed by user in dialog.


Instruction:
Here you have a function in Python 3, explain what it does
def set_show_only_status(self):
        if self.ui.chkBoxShowOnlyDiffs.isChecked() and not self.ui.cbShowDiffs.isChecked():
            self.ui.cbShowDiffs.setChecked(True)
            self.show_differences(True)

        if self.ui.chkBoxOnlyShowLabelsInProtocol.isChecked() and self.ui.chkBoxShowOnlyDiffs.isChecked():
            self.show_only_diffs_and_labels()
        elif self.ui.chkBoxOnlyShowLabelsInProtocol.isChecked() and not self.ui.chkBoxShowOnlyDiffs.isChecked():
            self.show_only_labels()
        elif not self.ui.chkBoxOnlyShowLabelsInProtocol.isChecked() and self.ui.chkBoxShowOnlyDiffs.isChecked():
            self.show_only_diffs()
        else:
            self.restore_visibility()

        self.ui.tblViewProtocol.resize_columns()

Response: Sets the show only status of the current locale.


Instruction:
Create a Python 3 function for
wrapping method selecting the backend to assign the protocol field to the given messages.

Response:
def find_field(self, messages):
        """
        Wrapper method selecting the backend to assign the protocol field.
        Various strategies are possible e.g.:
        1) Heuristics e.g. for Preamble
        2) Scoring based e.g. for Length
        3) Fulltext search for addresses based on participant subgroups

        :param messages: messages a field shall be searched for
        :type messages: list of Message
        """
        try:
            if self.backend == self.Backend.python:
                self._py_find_field(messages)
            elif self.backend == self.Backend.cython:
                self._cy_find_field(messages)
            elif self.backend == self.Backend.plainc:
                self._c_find_field(messages)
            else:
                raise ValueError("Unsupported backend {}".format(self.backend))
        except NotImplementedError:
            logger.info("Skipped {} because not implemented yet".format(self.__class__.__name__))


Instruction:
Write a Python 3 function that can
assign message types based on the clusters.

Response:
def assign_messagetypes(self, messages, clusters):
        """
        Assign message types based on the clusters. Following rules:
        1) Messages from different clusters will get different message types
        2) Messages from same clusters will get same message type
        3) The new message type will copy over the existing labels
        4) No new message type will be set for messages, that already have a custom message type assigned

        For messages with clustername "default" no new message type will be created

        :param messages: Messages, that messagetype needs to be clustered
        :param clusters: clusters for the messages
        :type messages: list[Message]
        :type clusters: dict[str, set[int]]
        :return:
        """
        for clustername, clustercontent in clusters.items():
            if clustername == "default":
                # Do not force the default message type
                continue

            for msg_i in clustercontent:
                msg = messages[msg_i]
                if msg.message_type == self.messagetypes[0]:
                    # Message has default message type
                    # Copy the existing labels and create a new message type
                    # if it was not already done
                    try:
                        msg_type = next(mtype for mtype in self.messagetypes if mtype.name == clustername)
                    except StopIteration:
                        msg_type = MessageType(name=clustername, iterable=msg.message_type)
                        msg_type.assigned_by_logic_analyzer = True
                        self.messagetypes.append(msg_type)
                    msg.message_type = msg_type


Instruction:
How would you code a function in Python 3 to
perform the short - time Fourier transform to get the spectrogram for the given samples

Response:
def stft(self, samples: np.ndarray):
        """
        Perform Short-time Fourier transform to get the spectrogram for the given samples
        :return: short-time Fourier transform of the given signal
        """
        window = self.window_function(self.window_size)
        hop_size = self.hop_size

        if len(samples) < self.window_size:
            samples = np.append(samples, np.zeros(self.window_size - len(samples)))

        num_frames = max(1, (len(samples) - self.window_size) // hop_size + 1)

        # Get frames as numpy view with stride_tricks to save RAM
        # Same as: frames = [padded_samples[i*hop_size:i*hop_size+self.window_size] for i in range(num_frames)]
        shape = (num_frames, self.window_size)
        strides = (hop_size * samples.strides[-1], samples.strides[-1])
        frames = np.lib.stride_tricks.as_strided(samples, shape=shape, strides=strides)

        result = np.fft.fft(frames * window, self.window_size) / np.atleast_1d(self.window_size)
        return result


Instruction:
How would you explain what the following Python 3 function does
def export_to_fta(self, sample_rate, filename: str, include_amplitude=False):
        spectrogram = self.__calculate_spectrogram(self.samples)
        spectrogram = np.flipud(spectrogram.T)
        if include_amplitude:
            result = np.empty((spectrogram.shape[0], spectrogram.shape[1], 3),
                              dtype=[('f', np.float64), ('t', np.uint32), ('a', np.float32)])
        else:
            result = np.empty((spectrogram.shape[0], spectrogram.shape[1], 2),
                              dtype=[('f', np.float64), ('t', np.uint32)])

        fft_freqs = np.fft.fftshift(np.fft.fftfreq(spectrogram.shape[0], 1/sample_rate))
        time_width = 1e9 * ((len(self.samples) / sample_rate) / spectrogram.shape[1])

        for i in range(spectrogram.shape[0]):
            for j in range(spectrogram.shape[1]):
                if include_amplitude:
                    result[i, j] = (fft_freqs[i], int(j*time_width), spectrogram[i, j])
                else:
                    result[i, j] = (fft_freqs[i], int(j * time_width))

        result.tofile(filename)

Response: Export to Fourier Transform


Instruction:
Make a summary of the following Python 3 code
def create_image(data: np.ndarray, colormap, data_min=None, data_max=None, normalize=True) -> QImage:
        image_data = Spectrogram.apply_bgra_lookup(data, colormap, data_min, data_max, normalize)

        if not image_data.flags['C_CONTIGUOUS']:
            logger.debug("Array was not C_CONTIGUOUS. Converting it.")
            image_data = np.ascontiguousarray(image_data)

        try:
            # QImage constructor needs inverted row/column order
            image = QImage(image_data.ctypes.data, image_data.shape[1], image_data.shape[0], QImage.Format_ARGB32)
        except Exception as e:
            logger.error("could not create image " + str(e))
            return QImage()

        image.data = image_data
        return image

Response: Create QImage from ARGB array.


Instruction:
Write a Python 3 script to
set the modulation type of the modulation.

Response:
def modulation_type(self, value: int):
        """
        0 - "ASK", 1 - "FSK", 2 - "PSK", 3 - "APSK (QAM)"

        :param value:
        :return:
        """
        if self.__modulation_type != value:
            self.__modulation_type = value
            self._qad = None

            self.modulation_type_changed.emit(self.__modulation_type)
            if not self.block_protocol_update:
                self.protocol_needs_update.emit()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def estimate_frequency(self, start: int, end: int, sample_rate: float):
        # ensure power of 2 for faster fft
        length = 2 ** int(math.log2(end - start))
        data = self.data[start:start + length]

        try:
            w = np.fft.fft(data)
            frequencies = np.fft.fftfreq(len(w))
            idx = np.argmax(np.abs(w))
            freq = frequencies[idx]
            freq_in_hertz = abs(freq * sample_rate)
        except ValueError:
            # No samples in window e.g. start == end, use a fallback
            freq_in_hertz = 100e3

        return freq_in_hertz

Response: Estimate the frequency of the baseband signal using FFT


Instruction:
Can you implement a function in Python 3 that
returns the range of the selected entries in the current tree.

Response:
def selection_range(self):
        """
        :rtype: int, int, int, int
        """
        selected = self.selectionModel().selection()
        """:type: QItemSelection """

        if selected.isEmpty():
            return -1, -1, -1, -1

        min_row = numpy.min([rng.top() for rng in selected])
        max_row = numpy.max([rng.bottom() for rng in selected])
        start = numpy.min([rng.left() for rng in selected])
        end = numpy.max([rng.right() for rng in selected]) + 1

        return min_row, max_row, start, end


Instruction:
Write a Python 3 function that can
fix stdout and stderr for frozen windows apps.

Response:
def fix_windows_stdout_stderr():
    """
    Processes can't write to stdout/stderr on frozen windows apps because they do not exist here
    if process tries it anyway we get a nasty dialog window popping up, so we redirect the streams to a dummy
    see https://github.com/jopohl/urh/issues/370
    """

    if hasattr(sys, "frozen") and sys.platform == "win32":
        try:
            sys.stdout.write("\n")
            sys.stdout.flush()
        except:
            class DummyStream(object):
                def __init__(self): pass

                def write(self, data): pass

                def read(self, data): pass

                def flush(self): pass

                def close(self): pass

            sys.stdout, sys.stderr, sys.stdin = DummyStream(), DummyStream(), DummyStream()
            sys.__stdout__, sys.__stderr__, sys.__stdin__ = DummyStream(), DummyStream(), DummyStream()


Instruction:
How would you code a function in Python 3 to
build the order of components based on their priority and predecessors.

Response:
def build_component_order(self):
        """
        Build the order of component based on their priority and predecessors

        :rtype: list of Component
        """
        present_components = [item for item in self.__dict__.values() if isinstance(item, Component) and item.enabled]
        result = [None] * len(present_components)
        used_prios = set()
        for component in present_components:
            index = component.priority % len(present_components)
            if index in used_prios:
                raise ValueError("Duplicate priority: {}".format(component.priority))
            used_prios.add(index)

            result[index] = component

        # Check if predecessors are valid
        for i, component in enumerate(result):
            if any(i < result.index(pre) for pre in component.predecessors):
                raise ValueError("Component {} comes before at least one of its predecessors".format(component))

        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def find_candidates(candidates):

        result = defaultdict(int)
        for i, c_i in enumerate(candidates):
            for j in range(i, len(candidates)):
                lcs = util.longest_common_substring(c_i.hex_value, candidates[j].hex_value)
                if lcs:
                    result[lcs] += 1

        return result

Response: Find candidate addresses using LCS algorithm.


Instruction:
How would you code a function in Python 3 to
choose a pair of address candidates ensuring they have the same length and starting with the highest scored ones.

Response:
def choose_candidate_pair(candidates):
        """
        Choose a pair of address candidates ensuring they have the same length and starting with the highest scored ones

        :type candidates: dict[str, int]
        :param candidates: Count how often the longest common substrings appeared in the messages
        :return:
        """
        highscored = sorted(candidates, key=candidates.get, reverse=True)
        for i, h_i in enumerate(highscored):
            for h_j in highscored[i+1:]:
                if len(h_i) == len(h_j):
                    yield (h_i, h_j)


Instruction:
How would you code a function in Python 3 to
extract each archive from the list of filenames.

Response:
def uncompress_archives(file_names, temp_dir):
    """
    Extract each archive from the list of filenames.
    Normal files stay untouched.
    Add all files to the Recent Files.
    :type file_names: list of str
    :type temp_dir: str
    :rtype: list of str
    """
    result = []
    for filename in file_names:
        if filename.endswith(".tar") or filename.endswith(".tar.gz") or filename.endswith(".tar.bz2"):
            obj = tarfile.open(filename, "r")
            extracted_file_names = []
            for j, member in enumerate(obj.getmembers()):
                obj.extract(member, temp_dir)
                extracted_filename = os.path.join(temp_dir, obj.getnames()[j])
                extracted_file_names.append(extracted_filename)
                archives[extracted_filename] = filename
            result.extend(extracted_file_names[:])
        elif filename.endswith(".zip"):
            obj = zipfile.ZipFile(filename)
            extracted_file_names = []
            for j, info in enumerate(obj.infolist()):
                obj.extract(info, path=temp_dir)
                extracted_filename = os.path.join(temp_dir, obj.namelist()[j])
                extracted_file_names.append(extracted_filename)
                archives[extracted_filename] = filename
            result.extend(extracted_file_names[:])
        else:
            result.append(filename)

    return result


Instruction:
Can you implement a function in Python 3 that
writes modulators to the project file.

Response:
def write_modulators_to_project_file(self, tree=None):
        """
        :type modulators: list of Modulator
        :return:
        """
        if self.project_file is None or not self.modulators:
            return

        if tree is None:
            tree = ET.parse(self.project_file)

        root = tree.getroot()
        root.append(Modulator.modulators_to_xml_tag(self.modulators))

        tree.write(self.project_file)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def cwt_haar(x: np.ndarray, scale=10):
    next_power_two = 2 ** int(np.log2(len(x)))

    x = x[0:next_power_two]
    num_data = len(x)

    # get FFT of x (eq. (3) in paper)
    x_hat = np.fft.fft(x)

    # Get omega (eq. (5) in paper)
    f = (2.0 * np.pi / num_data)
    omega = f * np.concatenate((np.arange(0, num_data // 2), np.arange(num_data // 2, num_data) * -1))

    # get psi hat (eq. (6) in paper)
    psi_hat = np.sqrt(2.0 * np.pi * scale) * normalized_haar_wavelet(scale * omega, scale)

    # get W (eq. (4) in paper)
    W = np.fft.ifft(x_hat * psi_hat)

    return W[2 * scale:-2 * scale]

Response:
continuous haar wavelet transform based on the paper
    continuous haar wavelet analysis by Christopher Torrence and Gilbert P Compo


Instruction:
Explain what the following Python 3 code does
def __find_sync_range(self, messages, preamble_end: int, search_end: int):

        possible_sync_pos = defaultdict(int)

        for i, msg in enumerate(messages):
            bits_i = msg.decoded_bits[preamble_end:search_end]
            for j in range(i, len(messages)):
                bits_j = messages[j].decoded_bits[preamble_end:search_end]
                first_diff = next((k for k, (bit_i, bit_j) in enumerate(zip(bits_i, bits_j)) if bit_i != bit_j), None)
                if first_diff is not None:
                    first_diff = preamble_end + 4 * (first_diff // 4)
                    if (first_diff - preamble_end) >= 4:
                        possible_sync_pos[(preamble_end, first_diff)] += 1
        try:
            sync_interval = max(possible_sync_pos, key=possible_sync_pos.__getitem__)
            return sync_interval
        except ValueError:
            return None

Response: Find the synchronization interval between two messages.


Instruction:
Can you generate the documentation for the following Python 3 function
def __pad_until_index(self, row: int, bit_pos: int):
        try:
            new_bits = array.array("B", [0] * max(0, bit_pos - len(self.protocol.messages[row])))
            if len(new_bits) == 0:
                return True

            self.protocol.messages[row].plain_bits = self.protocol.messages[row].plain_bits + new_bits
            msg = self.protocol.messages[row]
            self.display_data[
                row] = msg.plain_bits if self.proto_view == 0 else msg.plain_hex_array if self.proto_view == 1 else msg.plain_ascii_array
        except IndexError:
            return False

        return True

Response: Pads message in given row with zeros until given column so user can enter values behind end of message


Instruction:
Explain what the following Python 3 code does
def find_differences(self, refindex: int):
        differences = defaultdict(set)

        if refindex >= len(self.protocol.messages):
            return differences

        if self.proto_view == 0:
            proto = self.protocol.decoded_proto_bits_str
        elif self.proto_view == 1:
            proto = self.protocol.decoded_hex_str
        elif self.proto_view == 2:
            proto = self.protocol.decoded_ascii_str
        else:
            return differences

        ref_message = proto[refindex]
        ref_offset = self.get_alignment_offset_at(refindex)

        for i, message in enumerate(proto):
            if i == refindex:
                continue

            msg_offset = self.get_alignment_offset_at(i)
            short, long = sorted([len(ref_message) + ref_offset, len(message) + msg_offset])

            differences[i] = {
                j for j in range(max(msg_offset, ref_offset), long)
                if j >= short or message[j - msg_offset] != ref_message[j - ref_offset]
            }

        return differences

Response: Search all differences between protocol messages regarding a reference message.


Instruction:
Here you have a function in Python 3, explain what it does
def create_rectangle(proto_bits, pulse_len=100):
        ones = np.ones(pulse_len, dtype=np.float32) * 1
        zeros = np.ones(pulse_len, dtype=np.float32) * -1
        n = 0
        y = []
        for msg in proto_bits:
            for bit in msg:
                n += pulse_len
                if bit == "0":
                    y.extend(zeros)
                else:
                    y.extend(ones)
        x = np.arange(0, n).astype(np.int64)
        scene = ZoomableScene()
        scene.setSceneRect(0, -1, n, 2)
        scene.setBackgroundBrush(constants.BGCOLOR)
        scene.addLine(0, 0, n, 0, QPen(constants.AXISCOLOR, 0))
        if len(y) > 0:
            y = np.array(y)
        else:
            y = np.array(y).astype(np.float32)
        path = path_creator.array_to_QPath(x, y)
        scene.addPath(path, QPen(constants.LINECOLOR, 0))
        return scene, n

Response: Create a rectangle that contains the contents of the given list of message types.


Instruction:
Write a Python 3 script for
setting the parameters of the current spectrogram.

Response:
def set_parameters(self, samples: np.ndarray, window_size, data_min, data_max) -> bool:
        """
        Return true if redraw is needed
        """
        redraw_needed = False
        if self.samples_need_update:
            self.spectrogram.samples = samples
            redraw_needed = True
            self.samples_need_update = False

        if window_size != self.spectrogram.window_size:
            self.spectrogram.window_size = window_size
            redraw_needed = True

        if data_min != self.spectrogram.data_min:
            self.spectrogram.data_min = data_min
            redraw_needed = True

        if data_max != self.spectrogram.data_max:
            self.spectrogram.data_max = data_max
            redraw_needed = True

        return redraw_needed


Instruction:
Write a Python 3 function for
getting the next action from the selection.

Response:
def get_action(self, parent, undo_stack: QUndoStack, sel_range, protocol: ProtocolAnalyzer, view: int):
        """
        :type parent: QTableView
        :type undo_stack: QUndoStack
        :type protocol_analyzers: list of ProtocolAnalyzer
        """
        min_row, max_row, start, end = sel_range
        if min_row == -1 or max_row == -1 or start == -1 or end == -1:
            return None

        if max_row != min_row:
            return None

        end = protocol.convert_index(end, view, 0, True, message_indx=min_row)[0]
        # factor = 1 if view == 0 else 4 if view == 1 else 8

        self.command = MessageBreakAction(protocol, max_row, end)
        action = QAction(self.command.text(), parent)
        action.triggered.connect(self.action_triggered)
        self.undo_stack = undo_stack
        return action


Instruction:
Write a Python 3 function for
returning the length of this message in byte.

Response:
def get_byte_length(self, decoded=True) -> int:
        """
        Return the length of this message in byte.

        """
        end = len(self.decoded_bits) if decoded else len(self.__plain_bits)
        end = self.convert_index(end, 0, 2, decoded=decoded)[0]
        return int(end)


Instruction:
Write a Python 3 function that can
get the SRC address of a message from the data.

Response:
def get_src_address_from_data(self, decoded=True):
        """
        Return the SRC address of a message if SRC_ADDRESS label is present in message type of the message
        Return None otherwise

        :param decoded:
        :return:
        """
        src_address_label = next((lbl for lbl in self.message_type if lbl.field_type
                                  and lbl.field_type.function == FieldType.Function.SRC_ADDRESS), None)
        if src_address_label:
            start, end = self.get_label_range(src_address_label, view=1, decode=decoded)
            if decoded:
                src_address = self.decoded_hex_str[start:end]
            else:
                src_address = self.plain_hex_str[start:end]
        else:
            src_address = None

        return src_address


Instruction:
Here you have a function in Python 3, explain what it does
def split(self, decode=True):
        start = 0
        result = []
        message = self.decoded_bits if decode else self.plain_bits
        bit_alignments = set()
        if self.align_labels:
            for l in self.message_type:
                bit_alignments.add(l.start)
                bit_alignments.add(l.end)

        self.__bit_alignments = sorted(bit_alignments)

        for pos in self.__bit_alignments:
            result.append(message[start:pos])
            start = pos

        result.append(message[start:])
        return result

Response: Returns a list of the bit - alignment entries in the message.


Instruction:
How would you explain what the following Python 3 function does
def set_e(self):
        if self.ui.combobox_decodings.count() < 1:  # Empty list
            return

        self.e = copy.deepcopy(self.decodings[self.ui.combobox_decodings.currentIndex()])
        """:type: encoding """
        chain = self.e.get_chain()
        self.ui.decoderchain.clear()
        self.chainoptions.clear()
        last_i = ""
        for i in chain:
            if i in [constants.DECODING_INVERT, constants.DECODING_ENOCEAN, constants.DECODING_DIFFERENTIAL,
                     constants.DECODING_REDUNDANCY, constants.DECODING_CARRIER, constants.DECODING_BITORDER,
                     constants.DECODING_EDGE, constants.DECODING_DATAWHITENING, constants.DECODING_SUBSTITUTION,
                     constants.DECODING_EXTERNAL, constants.DECODING_CUT, constants.DECODING_MORSE,
                     constants.DECODING_DISABLED_PREFIX]:
                self.ui.decoderchain.addItem(i)
                self.decoderchainUpdate()
                last_i = self.ui.decoderchain.item(self.ui.decoderchain.count() - 1).text()
            else:
                if any(x in last_i for x in [constants.DECODING_REDUNDANCY, constants.DECODING_CARRIER,
                                             constants.DECODING_SUBSTITUTION, constants.DECODING_EXTERNAL,
                                             constants.DECODING_DATAWHITENING, constants.DECODING_CUT,
                                             constants.DECODING_MORSE]):
                    self.chainoptions[last_i] = i

        self.decoderchainUpdate()
        self.decoder_update()
        self.ui.saveas.setVisible(False)

Response: Sets the current language of the current language in the decoder chain.


Instruction:
Write a Python 3 script to
return a dictionary of all the protocols in the hierarchy

Response:
def protocols(self):
        """
        :rtype: dict[int, list of ProtocolAnalyzer]
        """
        result = {}
        for i, group in enumerate(self.rootItem.children):
            result[i] = [child.protocol for child in group.children]

        return result


Instruction:
Write a Python 3 script to
return a dictionary of all the protocol tree items in the tree.

Response:
def protocol_tree_items(self):
        """
        :rtype: dict[int, list of ProtocolTreeItem]
        """
        result = {}
        for i, group in enumerate(self.rootItem.children):
            result[i] = [child for child in group.children]

        return result


Instruction:
Write a Python 3 function for
moving the items to a new group.

Response:
def move_to_group(self, items, new_group_id: int):
        """
        :type items: list of ProtocolTreeItem
        """
        group = self.rootItem.child(new_group_id)
        for item in items:
            group.appendChild(item)
        self.controller.refresh()


Instruction:
Can you implement a function in Python 3 that
sets all protocols in copy mode.

Response:
def set_copy_mode(self, use_copy: bool):
        """
        Set all protocols in copy mode. They will return a copy of their protocol.
        This is used for writable mode in CFC.

        :param use_copy:
        :return:
        """
        for group in self.rootItem.children:
            for proto in group.children:
                proto.copy_data = use_copy


Instruction:
Create a Python 3 function for
getting a representation of the ring buffer for plotting. This is expensive so it is expensive so it is only used in frontend.

Response:
def view_data(self):
        """
        Get a representation of the ring buffer for plotting. This is expensive, so it should only be used in frontend
        :return:
        """
        left, right = self.left_index, self.left_index + len(self)
        if left > right:
            left, right = right, left

        data = np.frombuffer(self.__data.get_obj(), dtype=np.complex64)
        return np.concatenate((data[left:right], data[right:], data[:left]))


Instruction:
Can you generate the documentation for the following Python 3 function
def push(self, values: np.ndarray):
        n = len(values)
        if len(self) + n > self.size:
            raise ValueError("Too much data to push to RingBuffer")

        slide_1 = np.s_[self.right_index:min(self.right_index + n, self.size)]
        slide_2 = np.s_[:max(self.right_index + n - self.size, 0)]
        with self.__data.get_lock():
            data = np.frombuffer(self.__data.get_obj(), dtype=np.complex64)
            data[slide_1] = values[:slide_1.stop - slide_1.start]
            data[slide_2] = values[slide_1.stop - slide_1.start:]
            self.right_index += n

        self.__length.value += n

Response: Push values to buffer.


Instruction:
Here you have a function in Python 3, explain what it does
def pop(self, number: int, ensure_even_length=False):
        if ensure_even_length:
            number -= number % 2

        if len(self) == 0 or number == 0:
            return np.array([], dtype=np.complex64)

        if number < 0:
            # take everything
            number = len(self)
        else:
            number = min(number, len(self))

        with self.__data.get_lock():
            data = np.frombuffer(self.__data.get_obj(), dtype=np.complex64)

            result = np.empty(number, dtype=np.complex64)

            if self.left_index + number > len(data):
                end = len(data) - self.left_index
            else:
                end = number

            result[:end] = data[self.left_index:self.left_index + end]
            if end < number:
                result[end:] = data[:number-end]

        self.left_index += number
        self.__length.value -= number

        return result

Response: Pop a number of elements from the buffer.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def code_data_whitening(self, decoding, inpt):
        inpt_copy = array.array("B", inpt)
        return self.apply_data_whitening(decoding, inpt_copy)

Response:
XOR Data Whitening
        :param decoding:
        :param inpt:
        :return:


Instruction:
Write a Python 3 function for
getting the action for this table.

Response:
def get_action(self, parent, undo_stack: QUndoStack, sel_range, protocol, view: int):
        """
        :type parent: QTableView
        :type undo_stack: QUndoStack
        """
        self.command = ZeroHideAction(protocol, self.following_zeros, view, self.zero_hide_offsets)
        action = QAction(self.command.text(), parent)
        action.triggered.connect(self.action_triggered)
        self.undo_stack = undo_stack
        return action


Instruction:
Make a summary of the following Python 3 code
def scroll_mouse(self, mouse_x: int):
        scrollbar = self.horizontalScrollBar()

        if mouse_x - self.view_rect().x() > self.view_rect().width():
            scrollbar.setValue(scrollbar.value() + 5)

        elif mouse_x < self.view_rect().x():
            scrollbar.setValue(scrollbar.value() - 5)

Response: Scrolls the mouse if ROI Selection reaches corner of view


Instruction:
Given the following Python 3 function, write the documentation
def refresh_selection_area(self):
        self.__set_selection_area(x=self.selection_area.x, y=self.selection_area.y,
                                  w=self.selection_area.width, h=self.selection_area.height)

Response: Refresh the selection area in case scene was resized or scaled.


Instruction:
Given the following Python 3 function, write the documentation
def view_rect(self) -> QRectF:
        top_left = self.mapToScene(0, 0)
        bottom_right = self.mapToScene(self.viewport().width() - 1, self.viewport().height() - 1)
        return QRectF(top_left, bottom_right)

Response: Return the boundaries of the view in scene coordinates


Instruction:
How would you explain what the following Python 3 function does
def dropEvent(self, event: QDropEvent):
        items = [item for item in self.items(event.scenePos()) if isinstance(item, GraphicsItem) and item.acceptDrops()]
        item = None if len(items) == 0 else items[0]
        if len(event.mimeData().urls()) > 0:
            self.files_dropped.emit(event.mimeData().urls())

        indexes = list(event.mimeData().text().split("/")[:-1])

        group_nodes = []
        file_nodes = []
        for index in indexes:
            try:
                row, column, parent = map(int, index.split(","))
                if parent == -1:
                    parent = self.tree_root_item
                else:
                    parent = self.tree_root_item.child(parent)
                node = parent.child(row)
                if node.is_group:
                    group_nodes.append(node)
                else:
                    file_nodes.append(node)
            except ValueError:
                continue

        # Which Nodes to add?
        nodes_to_add = []
        """:type: list of ProtocolTreeItem """
        for group_node in group_nodes:
            nodes_to_add.extend(group_node.children)
        nodes_to_add.extend([file_node for file_node in file_nodes if file_node not in nodes_to_add])
        protocols_to_add = [node.protocol for node in nodes_to_add]

        ref_item = item
        position = None if ref_item is None else item.drop_indicator_position
        self.add_protocols(ref_item, position, protocols_to_add)
        super().dropEvent(event)

Response: Adds the protocol tree item to the tree.


Instruction:
Can you create a Python 3 function that
converts a bit array to a string.

Response:
def convert_bits_to_string(bits, output_view_type: int, pad_zeros=False, lsb=False, lsd=False, endianness="big"):
    """
    Convert bit array to string
    :param endianness: Endianness little or big
    :param bits: Bit array
    :param output_view_type: Output view type index
    0 = bit, 1=hex, 2=ascii, 3=decimal 4=binary coded decimal (bcd)
    :param pad_zeros:
    :param lsb: Least Significant Bit   -> Reverse bits first
    :param lsd: Least Significant Digit -> Reverse result at end
    :return:
    """
    bits_str = "".join(["1" if b else "0" for b in bits])

    if output_view_type == 4:
        # For BCD we need to enforce padding
        pad_zeros = True

    if pad_zeros and output_view_type in (1, 2, 4):
        n = 4 if output_view_type in (1, 4) else 8 if output_view_type == 2 else 1
        bits_str += "0" * ((n - (len(bits_str) % n)) % n)

    if lsb:
        # Reverse bit string
        bits_str = bits_str[::-1]

    if endianness == "little":
        # reverse byte wise
        bits_str = "".join(bits_str[max(i - 8, 0):i] for i in range(len(bits_str), 0, -8))

    if output_view_type == 0:  # bit
        result = bits_str

    elif output_view_type == 1:  # hex
        result = "".join(["{0:x}".format(int(bits_str[i:i + 4], 2)) for i in range(0, len(bits_str), 4)])

    elif output_view_type == 2:  # ascii
        result = "".join(map(chr,
                             [int("".join(bits_str[i:i + 8]), 2) for i in range(0, len(bits_str), 8)]))

    elif output_view_type == 3:  # decimal
        try:
            result = str(int(bits_str, 2))
        except ValueError:
            return None
    elif output_view_type == 4:  # bcd
        result = "".join([BCD_LUT[bits_str[i:i + 4]] for i in range(0, len(bits_str), 4)])
    else:
        raise ValueError("Unknown view type")

    if lsd:
        # reverse result
        return result[::-1]
    else:
        return result


Instruction:
Given the following Python 3 function, write the documentation
def __ensure_message_length_multiple(bit_data, bit_len: int, pauses, bit_sample_pos, divisor: int):
        for i in range(len(bit_data)):
            missing_bits = (divisor - (len(bit_data[i]) % divisor)) % divisor
            if missing_bits > 0 and pauses[i] >= bit_len * missing_bits:
                bit_data[i].extend([0] * missing_bits)
                pauses[i] = pauses[i] - missing_bits * bit_len

                try:
                    bit_sample_pos[i][-1] = bit_sample_pos[i][-2] + bit_len
                except IndexError as e:
                    logger.warning("Error padding message " + str(e))
                    continue

                bit_sample_pos[i].extend([bit_sample_pos[i][-1] + (k + 1) * bit_len for k in range(missing_bits - 1)])
                bit_sample_pos[i].append(bit_sample_pos[i][-1] + pauses[i])

Response: This method is used to ensure that the bit_len of the message is multiple of the bit_len of the message.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_samplepos_of_bitseq(self, start_message: int, start_index: int, end_message: int, end_index: int,
                                include_pause: bool):
        """
        Determine on which place (regarding samples) a bit sequence is
        :rtype: tuple[int,int]
        """
        try:
            if start_message > end_message:
                start_message, end_message = end_message, start_message

            if start_index >= len(self.messages[start_message].bit_sample_pos) - 1:
                start_index = len(self.messages[start_message].bit_sample_pos) - 1
                if not include_pause:
                    start_index -= 1

            if end_index >= len(self.messages[end_message].bit_sample_pos) - 1:
                end_index = len(self.messages[end_message].bit_sample_pos) - 1
                if not include_pause:
                    end_index -= 1

            start = self.messages[start_message].bit_sample_pos[start_index]
            num_samples = self.messages[end_message].bit_sample_pos[end_index] - start

            return start, num_samples
        except (KeyError, IndexError):
            return -1, -1

Response: Returns the position of the first bit sequence in the message.


Instruction:
Here you have a function in Python 3, explain what it does
def get_bitseq_from_selection(self, selection_start: int, selection_width: int):
        start_message, start_index, end_message, end_index = -1, -1, -1, -1
        if not self.messages or not self.messages[0].bit_sample_pos:
            return start_message, start_index, end_message, end_index

        if selection_start + selection_width < self.messages[0].bit_sample_pos[0]:
            return start_message, start_index, end_message, end_index

        for i, msg in enumerate(self.messages):
            msg_sample_pos = msg.bit_sample_pos
            if msg_sample_pos[-2] < selection_start:
                continue
            elif start_message == -1:
                start_message = i
                for j, sample_pos in enumerate(msg_sample_pos):
                    if sample_pos < selection_start:
                        continue
                    elif start_index == -1:
                        start_index = j
                        if msg_sample_pos[-1] - selection_start < selection_width:
                            break
                    elif sample_pos - selection_start > selection_width:
                        return start_message, start_index, i, j
            elif msg_sample_pos[-1] - selection_start < selection_width:
                continue
            else:
                for j, sample_pos in enumerate(msg_sample_pos):
                    if sample_pos - selection_start > selection_width:
                        return start_message, start_index, i, j

        last_message = len(self.messages) - 1
        last_index = len(self.messages[-1].plain_bits) + 1
        return start_message, start_index, last_message, last_index

Response: get start and end index of bit sequence from selected samples


Instruction:
Make a summary of the following Python 3 code
def convert_index(self, index: int, from_view: int, to_view: int, decoded: bool, message_indx=-1) -> tuple:
        if len(self.messages) == 0:
            return 0, 0

        if message_indx == -1:
            message_indx = self.messages.index(max(self.messages, key=len))  # Longest message

        if message_indx >= len(self.messages):
            message_indx = len(self.messages) - 1

        return self.messages[message_indx].convert_index(index, from_view, to_view, decoded)

Response: Convert an index into a new version of the current version.


Instruction:
How would you explain what the following Python 3 function does
def estimate_frequency_for_one(self, sample_rate: float, nbits=42) -> float:
        return self.__estimate_frequency_for_bit(True, sample_rate, nbits)

Response: Calculates the frequency of at most nbits logical ones and returns the mean of these frequencies.


Instruction:
Given the following Python 3 function, write the documentation
def estimate_frequency_for_zero(self, sample_rate: float, nbits=42) -> float:
        return self.__estimate_frequency_for_bit(False, sample_rate, nbits)

Response: Calculates the frequency of at most nbits logical zeros and returns the mean of these frequencies.


Instruction:
Here you have a function in Python 3, explain what it does
def auto_assign_decodings(self, decodings):
        nrz_decodings = [decoding for decoding in decodings if decoding.is_nrz or decoding.is_nrzi]
        fallback = nrz_decodings[0] if nrz_decodings else None
        candidate_decodings = [decoding for decoding in decodings
                               if decoding not in nrz_decodings and not decoding.contains_cut]

        for message in self.messages:
            decoder_found = False

            for decoder in candidate_decodings:
                if decoder.applies_for_message(message.plain_bits):
                    message.decoder = decoder
                    decoder_found = True
                    break

            if not decoder_found and fallback:
                message.decoder = fallback

Response: Auto - assigns the decodings to the message objects.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def selection_range(self):
        selected = self.selectionModel().selection()
        """:type: QItemSelection """

        if selected.isEmpty():
            return -1, -1

        min_row = min(rng.top() for rng in selected)
        max_row = max(rng.bottom() for rng in selected)

        return min_row, max_row

Response: Returns the selection range of the selected items.


Instruction:
Make a summary of the following Python 3 code
def cleanup():
    script_dir = os.path.dirname(__file__) if not os.path.islink(__file__) else os.path.dirname(os.readlink(__file__))
    script_dir = os.path.realpath(os.path.join(script_dir, ".."))
    shutil.rmtree(os.path.join(script_dir, "dist"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "tmp"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "urh.egg-info"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "src", "urh.egg-info"), ignore_errors=True)
    shutil.rmtree(os.path.join(script_dir, "src", "urh", "tmp"), ignore_errors=True)

Response: Remove all cache directories


Instruction:
Can you tell what is the following Python 3 function doing
def selection_range(self):
        selected = self.selectionModel().selection()  # type: QItemSelection
        if self.selection_is_empty:
            return -1, -1, -1, -1

        def range_to_tuple(rng):
            return rng.row(), rng.column()

        top_left = min(range_to_tuple(rng.topLeft()) for rng in selected)
        bottom_right = max(range_to_tuple(rng.bottomRight()) for rng in selected)

        return top_left[0], bottom_right[0], top_left[1], bottom_right[1] + 1

Response: Returns the range of the selected items.


Instruction:
Write a Python 3 script for
returning the most frequent value in list.

Response:
def get_most_frequent_value(values: list):
    """
    Return the most frequent value in list.
    If there is no unique one, return the maximum of the most frequent values

    :param values:
    :return:
    """
    if len(values) == 0:
        return None

    most_common = Counter(values).most_common()
    result, max_count = most_common[0]
    for value, count in most_common:
        if count < max_count:
            return result
        else:
            result = value

    return result


Instruction:
Create a Python 3 function to
get the list of start end indices of messages in the sequence of magnitudes.

Response:
def segment_messages_from_magnitudes(magnitudes: np.ndarray, noise_threshold: float):
    """
    Get the list of start, end indices of messages

    :param magnitudes: Magnitudes of samples
    :param noise_threshold: Threshold for noise
    :return:
    """
    return c_auto_interpretation.segment_messages_from_magnitudes(magnitudes, noise_threshold)


Instruction:
Here you have a function in Python 3, explain what it does
def round_plateau_lengths(plateau_lengths: list):
    # round to n_digits of most common value
    digit_counts = [len(str(p)) for p in plateau_lengths]
    n_digits = min(3, int(np.percentile(digit_counts, 50)))
    f = 10 ** (n_digits - 1)

    for i, plateau_len in enumerate(plateau_lengths):
        plateau_lengths[i] = int(round(plateau_len / f)) * f

Response: Round plateau lengths to next divisible number of digits e. g. 99 293 293.


Instruction:
Given the following Python 3 function, write the documentation
def on_files_dropped_on_group(self, files, group_id: int):
        self.__add_urls_to_group(files, group_id=group_id)

Response: Called when the files list is dropped from a group.