Instruction:
Here you have a function in Python 3, explain what it does
def common_package_action_options(f):

    @click.option(
        "-s",
        "--skip-errors",
        default=False,
        is_flag=True,
        help="Skip/ignore errors when copying packages.",
    )
    @click.option(
        "-W",
        "--no-wait-for-sync",
        default=False,
        is_flag=True,
        help="Don't wait for package synchronisation to complete before " "exiting.",
    )
    @click.option(
        "-I",
        "--wait-interval",
        default=5.0,
        type=float,
        show_default=True,
        help="The time in seconds to wait between checking synchronisation.",
    )
    @click.option(
        "--sync-attempts",
        default=3,
        type=int,
        help="Number of times to attempt package synchronisation. If the "
        "package fails the first time, the client will attempt to "
        "automatically resynchronise it.",
    )
    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, *args, **kwargs):
        # pylint: disable=missing-docstring
        return ctx.invoke(f, *args, **kwargs)

    return wrapper

Response: Add common options for package actions.


Instruction:
Explain what the following Python 3 code does
def common_cli_config_options(f):

    @click.option(
        "-C",
        "--config-file",
        envvar="CLOUDSMITH_CONFIG_FILE",
        type=click.Path(dir_okay=True, exists=True, writable=False, resolve_path=True),
        help="The path to your config.ini file.",
    )
    @click.option(
        "--credentials-file",
        envvar="CLOUDSMITH_CREDENTIALS_FILE",
        type=click.Path(dir_okay=True, exists=True, writable=False, resolve_path=True),
        help="The path to your credentials.ini file.",
    )
    @click.option(
        "-P",
        "--profile",
        default=None,
        envvar="CLOUDSMITH_PROFILE",
        help="The name of the profile to use for configuration.",
    )
    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, *args, **kwargs):
        # pylint: disable=missing-docstring
        opts = config.get_or_create_options(ctx)
        profile = kwargs.pop("profile")
        config_file = kwargs.pop("config_file")
        creds_file = kwargs.pop("credentials_file")
        opts.load_config_file(path=config_file, profile=profile)
        opts.load_creds_file(path=creds_file, profile=profile)
        kwargs["opts"] = opts
        return ctx.invoke(f, *args, **kwargs)

    return wrapper

Response: Add common CLI config options to commands.


Instruction:
Can you create a Python 3 function that
adds common CLI output options to commands.

Response:
def common_cli_output_options(f):
    """Add common CLI output options to commands."""

    @click.option(
        "-d",
        "--debug",
        default=False,
        is_flag=True,
        help="Produce debug output during processing.",
    )
    @click.option(
        "-F",
        "--output-format",
        default="pretty",
        type=click.Choice(["pretty", "json", "pretty_json"]),
        help="Determines how output is formatted. This is only supported by a "
        "subset of the commands at the moment (e.g. list).",
    )
    @click.option(
        "-v",
        "--verbose",
        is_flag=True,
        default=False,
        help="Produce more output during processing.",
    )
    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, *args, **kwargs):
        # pylint: disable=missing-docstring
        opts = config.get_or_create_options(ctx)
        opts.debug = kwargs.pop("debug")
        opts.output = kwargs.pop("output_format")
        opts.verbose = kwargs.pop("verbose")
        kwargs["opts"] = opts
        return ctx.invoke(f, *args, **kwargs)

    return wrapper


Instruction:
Make a summary of the following Python 3 code
def common_cli_list_options(f):

    @click.option(
        "-p",
        "--page",
        default=1,
        type=int,
        help="The page to view for lists, where 1 is the first page",
        callback=validators.validate_page,
    )
    @click.option(
        "-l",
        "--page-size",
        default=30,
        type=int,
        help="The amount of items to view per page for lists.",
        callback=validators.validate_page_size,
    )
    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, *args, **kwargs):
        # pylint: disable=missing-docstring
        opts = config.get_or_create_options(ctx)
        kwargs["opts"] = opts
        return ctx.invoke(f, *args, **kwargs)

    return wrapper

Response: Add common list options to commands.


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

    @click.option(
        "-k",
        "--api-key",
        hide_input=True,
        envvar="CLOUDSMITH_API_KEY",
        help="The API key for authenticating with the API.",
    )
    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, *args, **kwargs):
        # pylint: disable=missing-docstring
        opts = config.get_or_create_options(ctx)
        opts.api_key = kwargs.pop("api_key")
        kwargs["opts"] = opts
        return ctx.invoke(f, *args, **kwargs)

    return wrapper

Response: Add common API authentication options to commands.


Instruction:
Can you generate the documentation for the following Python 3 function
def initialise_api(f):

    @click.option(
        "--api-host", envvar="CLOUDSMITH_API_HOST", help="The API host to connect to."
    )
    @click.option(
        "--api-proxy",
        envvar="CLOUDSMITH_API_PROXY",
        help="The API proxy to connect through.",
    )
    @click.option(
        "--api-user-agent",
        envvar="CLOUDSMITH_API_USER_AGENT",
        help="The user agent to use for requests.",
    )
    @click.option(
        "--api-headers",
        envvar="CLOUDSMITH_API_HEADERS",
        help="A CSV list of extra headers (key=value) to send to the API.",
    )
    @click.option(
        "-R",
        "--without-rate-limit",
        default=False,
        is_flag=True,
        help="Don't obey the suggested rate limit interval. The CLI will "
        "automatically sleep between commands to ensure that you do "
        "not hit the server-side rate limit.",
    )
    @click.option(
        "--rate-limit-warning",
        default=30,
        help="When rate limiting, display information that it is happening "
        "if wait interval is higher than this setting. By default no "
        "information will be printed. Set to zero to always see it.",
    )
    @click.option(
        "--error-retry-max",
        default=6,
        help="The maximum amount of times to retry on errors received from "
        "the API, as determined by the --error-retry-codes parameter.",
    )
    @click.option(
        "--error-retry-backoff",
        default=0.23,
        type=float,
        help="The backoff factor determines how long to wait in seconds "
        "between error retries. The backoff factor is multiplied by the "
        "amount of retries so far. So if 0.1, then the wait is 0.1s then "
        "0.2s, then 0.4s, and so forth.",
    )
    @click.option(
        "--error-retry-codes",
        default="500,502,503,504",
        help="The status codes that when received from the API will cause "
        "a retry (if --error-retry-max is > 0). By default this will be for "
        "500, 502, 503 and 504 error codes.",
    )
    @click.pass_context
    @functools.wraps(f)
    def wrapper(ctx, *args, **kwargs):
        # pylint: disable=missing-docstring
        opts = config.get_or_create_options(ctx)
        opts.api_host = kwargs.pop("api_host")
        opts.api_proxy = kwargs.pop("api_proxy")
        opts.api_user_agent = kwargs.pop("api_user_agent")
        opts.api_headers = kwargs.pop("api_headers")
        opts.rate_limit = not kwargs.pop("without_rate_limit")
        opts.rate_limit_warning = kwargs.pop("rate_limit_warning")
        opts.error_retry_max = kwargs.pop("error_retry_max")
        opts.error_retry_backoff = kwargs.pop("error_retry_backoff")
        opts.error_retry_codes = kwargs.pop("error_retry_codes")

        def call_print_rate_limit_info_with_opts(rate_info, atexit=False):
            utils.print_rate_limit_info(opts, rate_info, atexit=atexit)

        opts.api_config = _initialise_api(
            debug=opts.debug,
            host=opts.api_host,
            key=opts.api_key,
            proxy=opts.api_proxy,
            user_agent=opts.api_user_agent,
            headers=opts.api_headers,
            rate_limit=opts.rate_limit,
            rate_limit_callback=call_print_rate_limit_info_with_opts,
            error_retry_max=opts.error_retry_max,
            error_retry_backoff=opts.error_retry_backoff,
            error_retry_codes=opts.error_retry_codes,
        )

        kwargs["opts"] = opts
        return ctx.invoke(f, *args, **kwargs)

    return wrapper

Response: Initialise the Cloudsmith API for use.


Instruction:
Can you generate the documentation for the following Python 3 function
def list_entitlements(owner, repo, page, page_size, show_tokens):
    client = get_entitlements_api()

    with catch_raise_api_exception():
        data, _, headers = client.entitlements_list_with_http_info(
            owner=owner,
            repo=repo,
            page=page,
            page_size=page_size,
            show_tokens=show_tokens,
        )

    ratelimits.maybe_rate_limit(client, headers)
    page_info = PageInfo.from_headers(headers)
    entitlements = [ent.to_dict() for ent in data]  # pylint: disable=no-member
    return entitlements, page_info

Response: Get a list of entitlements on a repository.


Instruction:
Can you generate the documentation for the following Python 3 function
def create_entitlement(owner, repo, name, token, show_tokens):
    client = get_entitlements_api()

    data = {}
    if name is not None:
        data["name"] = name

    if token is not None:
        data["token"] = token

    with catch_raise_api_exception():
        data, _, headers = client.entitlements_create_with_http_info(
            owner=owner, repo=repo, data=data, show_tokens=show_tokens
        )

    ratelimits.maybe_rate_limit(client, headers)
    return data.to_dict()

Response: Create an entitlement in a repository.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delete_entitlement(owner, repo, identifier):
    client = get_entitlements_api()

    with catch_raise_api_exception():
        _, _, headers = client.entitlements_delete_with_http_info(
            owner=owner, repo=repo, identifier=identifier
        )

    ratelimits.maybe_rate_limit(client, headers)

Response: Delete an entitlement from a repository.


Instruction:
Make a summary of the following Python 3 code
def update_entitlement(owner, repo, identifier, name, token, show_tokens):
    client = get_entitlements_api()

    data = {}
    if name is not None:
        data["name"] = name

    if token is not None:
        data["token"] = token

    with catch_raise_api_exception():
        data, _, headers = client.entitlements_partial_update_with_http_info(
            owner=owner,
            repo=repo,
            identifier=identifier,
            data=data,
            show_tokens=show_tokens,
        )

    ratelimits.maybe_rate_limit(client, headers)
    return data.to_dict()

Response: Update an entitlement in a repository.


Instruction:
Can you write a function in Python 3 where it
refreshes an entitlement in a repository.

Response:
def refresh_entitlement(owner, repo, identifier, show_tokens):
    """Refresh an entitlement in a repository."""
    client = get_entitlements_api()

    with catch_raise_api_exception():
        data, _, headers = client.entitlements_refresh_with_http_info(
            owner=owner, repo=repo, identifier=identifier, show_tokens=show_tokens
        )

    ratelimits.maybe_rate_limit(client, headers)
    return data.to_dict()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_api_client(cls):
    config = cloudsmith_api.Configuration()
    client = cls()
    client.config = config
    client.api_client.rest_client = RestClient()

    user_agent = getattr(config, "user_agent", None)
    if user_agent:
        client.api_client.user_agent = user_agent

    headers = getattr(config, "headers", None)
    if headers:
        for k, v in six.iteritems(headers):
            client.api_client.set_default_header(k, v)

    return client

Response: Get an API client with configuration.


Instruction:
Write a Python 3 function that can
configure a new API key.

Response:
def set_api_key(config, key):
    """Configure a new API key."""
    if not key and "X-Api-Key" in config.api_key:
        del config.api_key["X-Api-Key"]
    else:
        config.api_key["X-Api-Key"] = key


Instruction:
Write a Python 3 script for
validating parameters for requesting a file upload.

Response:
def validate_request_file_upload(owner, repo, filepath, md5_checksum=None):
    """Validate parameters for requesting a file upload."""
    client = get_files_api()
    md5_checksum = md5_checksum or calculate_file_md5(filepath)

    with catch_raise_api_exception():
        _, _, headers = client.files_validate_with_http_info(
            owner=owner,
            repo=repo,
            data={"filename": os.path.basename(filepath), "md5_checksum": md5_checksum},
        )

    ratelimits.maybe_rate_limit(client, headers)
    return md5_checksum


Instruction:
Here you have a function in Python 3, explain what it does
def request_file_upload(owner, repo, filepath, md5_checksum=None):
    client = get_files_api()
    md5_checksum = md5_checksum or calculate_file_md5(filepath)

    with catch_raise_api_exception():
        data, _, headers = client.files_create_with_http_info(
            owner=owner,
            repo=repo,
            data={"filename": os.path.basename(filepath), "md5_checksum": md5_checksum},
        )

    # pylint: disable=no-member
    # Pylint detects the returned value as a tuple
    ratelimits.maybe_rate_limit(client, headers)
    return data.identifier, data.upload_url, data.upload_fields

Response: Request a new file upload.


Instruction:
Make a summary of the following Python 3 code
def upload_file(upload_url, upload_fields, filepath, callback=None):
    upload_fields = list(six.iteritems(upload_fields))
    upload_fields.append(
        ("file", (os.path.basename(filepath), click.open_file(filepath, "rb")))
    )
    encoder = MultipartEncoder(upload_fields)
    monitor = MultipartEncoderMonitor(encoder, callback=callback)

    config = cloudsmith_api.Configuration()
    if config.proxy:
        proxies = {"http": config.proxy, "https": config.proxy}
    else:
        proxies = None

    headers = {"content-type": monitor.content_type}

    client = get_files_api()
    headers["user-agent"] = client.api_client.user_agent

    session = create_requests_session()
    resp = session.post(upload_url, data=monitor, headers=headers, proxies=proxies)

    try:
        resp.raise_for_status()
    except requests.RequestException as exc:
        raise ApiException(
            resp.status_code, headers=exc.response.headers, body=exc.response.content
        )

Response: Upload a pre - signed file to Cloudsmith.


Instruction:
Given the following Python 3 function, write the documentation
def handle_api_exceptions(
    ctx, opts, context_msg=None, nl=False, exit_on_error=True, reraise_on_error=False
):
    """Context manager that handles API exceptions."""
    # flake8: ignore=C901
    # Use stderr for messages if the output is something else (e.g.  # JSON)
    use_stderr = opts.output != "pretty"

    try:
        yield
    except ApiException as exc:
        if nl:
            click.echo(err=use_stderr)
            click.secho("ERROR: ", fg="red", nl=False, err=use_stderr)
        else:
            click.secho("ERROR", fg="red", err=use_stderr)

        context_msg = context_msg or "Failed to perform operation!"
        click.secho(
            "%(context)s (status: %(code)s - %(code_text)s)"
            % {
                "context": context_msg,
                "code": exc.status,
                "code_text": exc.status_description,
            },
            fg="red",
            err=use_stderr,
        )

        detail, fields = get_details(exc)
        if detail or fields:
            click.echo(err=use_stderr)

            if detail:
                click.secho(
                    "Detail: %(detail)s"
                    % {"detail": click.style(detail, fg="red", bold=False)},
                    bold=True,
                    err=use_stderr,
                )

            if fields:
                for k, v in six.iteritems(fields):
                    field = "%s Field" % k.capitalize()
                    click.secho(
                        "%(field)s: %(message)s"
                        % {
                            "field": click.style(field, bold=True),
                            "message": click.style(v, fg="red"),
                        },
                        err=use_stderr,
                    )

        hint = get_error_hint(ctx, opts, exc)
        if hint:
            click.echo(
                "Hint: %(hint)s" % {"hint": click.style(hint, fg="yellow")},
                err=use_stderr,
            )

        if opts.verbose and not opts.debug:
            if exc.headers:
                click.echo(err=use_stderr)
                click.echo("Headers in Reply:", err=use_stderr)
                for k, v in six.iteritems(exc.headers):
                    click.echo(
                        "%(key)s = %(value)s" % {"key": k, "value": v}, err=use_stderr
                    )

        if reraise_on_error:
            six.reraise(*sys.exc_info())

        if exit_on_error:
            ctx.exit(exc.status or 1)

Response: Context manager that handles API exceptions.


Instruction:
Write a Python 3 function for
getting the details from the exception.

Response:
def get_details(exc):
    """Get the details from the exception."""
    detail = None
    fields = collections.OrderedDict()

    if exc.detail:
        detail = exc.detail

    if exc.fields:
        for k, v in six.iteritems(exc.fields):
            try:
                field_detail = v["detail"]
            except (TypeError, KeyError):
                field_detail = v

            if isinstance(field_detail, (list, tuple)):
                field_detail = " ".join(field_detail)

            if k == "non_field_errors":
                if detail:
                    detail += " " + field_detail
                else:
                    detail = field_detail
                continue

            fields[k] = field_detail

    return detail, fields


Instruction:
Can you tell what is the following Python 3 function doing
def get_error_hint(ctx, opts, exc):
    module = sys.modules[__name__]
    get_specific_error_hint = getattr(module, "get_%s_error_hint" % exc.status, None)
    if get_specific_error_hint:
        return get_specific_error_hint(ctx, opts, exc)
    return None

Response: Get a hint to show to the user if any.


Instruction:
Given the following Python 3 function, write the documentation
def make_env_key(app_name, key):
    key = key.replace('-', '_').replace(' ', '_')
    return str("_".join((x.upper() for x in (app_name, key))))

Response: Creates an environment key - equivalent for the given key


Instruction:
Create a Python 3 function for
setting the environment variable by prepending the app_name to key.

Response:
def set_env(self, key, value):
        """Sets environment variables by prepending the app_name to `key`. Also registers the
        environment variable with the instance object preventing an otherwise-required call to
        `reload()`.
        """
        os.environ[make_env_key(self.appname, key)] = str(value)  # must coerce to string
        self._registered_env_keys.add(key)
        self._clear_memoization()


Instruction:
Write a Python 3 function for
removing an environment variable using the prepended app_name convention with key.

Response:
def unset_env(self, key):
        """Removes an environment variable using the prepended app_name convention with `key`."""
        os.environ.pop(make_env_key(self.appname, key), None)
        self._registered_env_keys.discard(key)
        self._clear_memoization()


Instruction:
Here you have a function in Python 3, explain what it does
def _reload(self, force=False):
        self._config_map = dict()
        self._registered_env_keys = set()
        self.__reload_sources(force)
        self.__load_environment_keys()
        self.verify()
        self._clear_memoization()

Response:
Reloads the configuration from the file and environment variables. Useful if using
        os. environ instead of this class s set_env method.


Instruction:
Create a Python 3 function to
promote a package from one repository to another repository.

Response:
def move(
    ctx,
    opts,
    owner_repo_package,
    destination,
    skip_errors,
    wait_interval,
    no_wait_for_sync,
    sync_attempts,
    yes,
):
    """
    Move (promote) a package to another repo.

    This requires appropriate permissions for both the source
    repository/package and the destination repository.

    - OWNER/REPO/PACKAGE: Specify the OWNER namespace (i.e. user or org), the
    REPO name where the package is stored, and the PACKAGE name (slug) of the
    package itself. All separated by a slash.

      Example: 'your-org/awesome-repo/better-pkg'.

    - DEST: Specify the DEST (destination) repository to move the package to.
    This *must* be in the same namespace as the source repository.

      Example: 'other-repo'

    Full CLI example:

      $ cloudsmith mv your-org/awesome-repo/better-pkg other-repo
    """
    owner, source, slug = owner_repo_package

    move_args = {
        "slug": click.style(slug, bold=True),
        "source": click.style(source, bold=True),
        "dest": click.style(destination, bold=True),
    }

    prompt = "move the %(slug)s from %(source)s to %(dest)s" % move_args
    if not utils.confirm_operation(prompt, assume_yes=yes):
        return

    click.echo(
        "Moving %(slug)s package from %(source)s to %(dest)s ... " % move_args, nl=False
    )

    context_msg = "Failed to move package!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        with maybe_spinner(opts):
            _, new_slug = move_package(
                owner=owner, repo=source, identifier=slug, destination=destination
            )

    click.secho("OK", fg="green")

    if no_wait_for_sync:
        return

    wait_for_package_sync(
        ctx=ctx,
        opts=opts,
        owner=owner,
        repo=destination,
        slug=new_slug,
        wait_interval=wait_interval,
        skip_errors=skip_errors,
        attempts=sync_attempts,
    )


Instruction:
Create a Python 3 function for
creating a requests session that retries some errors.

Response:
def create_requests_session(
    retries=None,
    backoff_factor=None,
    status_forcelist=None,
    pools_size=4,
    maxsize=4,
    ssl_verify=None,
    ssl_cert=None,
    proxy=None,
    session=None,
):
    """Create a requests session that retries some errors."""
    # pylint: disable=too-many-branches
    config = Configuration()

    if retries is None:
        if config.error_retry_max is None:
            retries = 5
        else:
            retries = config.error_retry_max

    if backoff_factor is None:
        if config.error_retry_backoff is None:
            backoff_factor = 0.23
        else:
            backoff_factor = config.error_retry_backoff

    if status_forcelist is None:
        if config.error_retry_codes is None:
            status_forcelist = [500, 502, 503, 504]
        else:
            status_forcelist = config.error_retry_codes

    if ssl_verify is None:
        ssl_verify = config.verify_ssl

    if ssl_cert is None:
        if config.cert_file and config.key_file:
            ssl_cert = (config.cert_file, config.key_file)
        elif config.cert_file:
            ssl_cert = config.cert_file

    if proxy is None:
        proxy = Configuration().proxy

    session = session or requests.Session()
    session.verify = ssl_verify
    session.cert = ssl_cert

    if proxy:
        session.proxies = {"http": proxy, "https": proxy}

    retry = Retry(
        backoff_factor=backoff_factor,
        connect=retries,
        method_whitelist=False,
        read=retries,
        status_forcelist=tuple(status_forcelist),
        total=retries,
    )

    adapter = HTTPAdapter(
        max_retries=retry,
        pool_connections=pools_size,
        pool_maxsize=maxsize,
        pool_block=True,
    )

    session.mount("http://", adapter)
    session.mount("https://", adapter)

    return session


Instruction:
Make a summary of the following Python 3 code
def data(self):
        if self._data is None:
            self._data = self.response.content.decode("utf-8")
        return self._data

Response: Get the content for the response.


Instruction:
Write a Python 3 script for
getting a given response header.

Response:
def getheader(self, name, default=None):
        """
        Return a given response header.
        """
        return self.response.headers.get(name, default)


Instruction:
How would you explain what the following Python 3 function does
def request(
        self,
        method,
        url,
        query_params=None,
        headers=None,
        body=None,
        post_params=None,
        _preload_content=True,
        _request_timeout=None,
    ):
        """
        :param method: http request method
        :param url: http request url
        :param query_params: query parameters in the url
        :param headers: http request headers
        :param body: request json body, for `application/json`
        :param post_params: request post parameters,
                            `application/x-www-form-urlencoded`
                            and `multipart/form-data`
        :param _preload_content: if False, the response object will be returned without
                                 reading/decoding response data. Default is True.
        :param _request_timeout: timeout setting for this request. If one number provided, it will be total request
                                 timeout. It can also be a pair (tuple) of (connection, read) timeouts.
        """
        # Based on the RESTClientObject class generated by Swagger
        method = method.upper()
        assert method in ["GET", "HEAD", "DELETE", "POST", "PUT", "PATCH", "OPTIONS"]

        post_params = post_params or {}
        headers = headers or {}

        if "Content-Type" not in headers:
            headers["Content-Type"] = "application/json"

        request_kwargs = {}

        if query_params:
            url += "?" + urlencode(query_params)

        if method in ["POST", "PUT", "PATCH", "OPTIONS", "DELETE"]:
            if re.search("json", headers["Content-Type"], re.IGNORECASE):
                request_body = None
                if body:
                    request_body = json.dumps(body)
                request_kwargs["data"] = request_body
            elif headers["Content-Type"] == "application/x-www-form-urlencoded":
                request_kwargs["data"] = post_params
            elif headers["Content-Type"] == "multipart/form-data":
                del headers["Content-Type"]
                request_kwargs["data"] = post_params
            elif isinstance(body, str):
                request_kwargs["data"] = body
            else:
                # Cannot generate the request from given parameters
                msg = """Cannot prepare a request message for provided arguments.
                         Please check that your arguments match declared content type."""
                raise ApiException(status=0, reason=msg)

        try:
            resp = self.session.request(
                method,
                url,
                timeout=_request_timeout,
                stream=not _preload_content,
                headers=headers,
                **request_kwargs
            )
        except requests.exceptions.RequestException as exc:
            msg = "{0}\n{1}".format(type(exc).__name__, str(exc))
            raise ApiException(status=0, reason=msg)

        resp.encoding = resp.apparent_encoding or "utf-8"
        rest_resp = RestResponse(resp)

        if _preload_content:
            logger.debug("response body: %s", rest_resp.data)

        if not 200 <= rest_resp.status <= 299:
            raise ApiException(http_resp=rest_resp)

        return rest_resp

Response: This method is used to make a request to the API.


Instruction:
How would you explain what the following Python 3 function does
def get_root_path():
    return os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir))

Response: Get the root directory for the application.


Instruction:
Can you generate the documentation for the following Python 3 function
def read_file(*path):
    real_path = os.path.realpath(os.path.join(*path))
    with click.open_file(real_path, "r") as fp:
        return fp.read()

Response: Read the specific file into a string in its entirety.


Instruction:
Create a Python 3 function for
calculating an MD5 hash for a file.

Response:
def calculate_file_md5(filepath, blocksize=2 ** 20):
    """Calculate an MD5 hash for a file."""
    checksum = hashlib.md5()

    with click.open_file(filepath, "rb") as f:

        def update_chunk():
            """Add chunk to checksum."""
            buf = f.read(blocksize)
            if buf:
                checksum.update(buf)
            return bool(buf)

        while update_chunk():
            pass

    return checksum.hexdigest()


Instruction:
How would you implement a function in Python 3 that
constructs page and page size kwargs (if present ).

Response:
def get_page_kwargs(**kwargs):
    """Construct page and page size kwargs (if present)."""
    page_kwargs = {}

    page = kwargs.get("page")
    if page is not None and page > 0:
        page_kwargs["page"] = page

    page_size = kwargs.get("page_size")
    if page_size is not None and page_size > 0:
        page_kwargs["page_size"] = page_size

    return page_kwargs


Instruction:
Here you have a function in Python 3, explain what it does
def get_query_kwargs(**kwargs):
    query_kwargs = {}

    query = kwargs.pop("query")
    if query:
        query_kwargs["query"] = query

    return query_kwargs

Response: Construct page and page size kwargs.


Instruction:
How would you implement a function in Python 3 that
returns a PEP440 - compliant version derived from the git tag.

Response:
def _get_version_from_git_tag(path):
    """Return a PEP440-compliant version derived from the git status.
    If that fails for any reason, return the changeset hash.
    """
    m = GIT_DESCRIBE_REGEX.match(_git_describe_tags(path) or '')
    if m is None:
        return None
    version, post_commit, hash = m.groups()
    return version if post_commit == '0' else "{0}.post{1}+{2}".format(version, post_commit, hash)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_version(dunder_file):
    path = abspath(expanduser(dirname(dunder_file)))
    try:
        return _get_version_from_version_file(path) or _get_version_from_git_tag(path)
    except CalledProcessError as e:
        log.warn(repr(e))
        return None
    except Exception as e:
        log.exception(e)
        return None

Response:
Returns a version string for the current package derived
    from git or from a. version file.


Instruction:
Write a Python 3 function that can
count the time spent in some context

Response:
def ctime(message=None):
    "Counts the time spent in some context"
    global t_elapsed
    t_elapsed = 0.0
    print('\n')
    t = time.time()
    yield
    if message:
        print(message + ":  ", end='')
    t_elapsed = time.time() - t
    print(round(t_elapsed, 4), "sec")


Instruction:
Here you have a function in Python 3, explain what it does
def rot1(angle, form='c'):
    cos_a = np.cos(angle)
    sin_a = np.sin(angle)
    rot_mat = np.identity(3)

    if form=='c':
        rot_mat[1, 1] = cos_a
        rot_mat[1, 2] = -sin_a
        rot_mat[2, 1] = sin_a
        rot_mat[2, 2] = cos_a
    elif form=='r':
        rot_mat[1, 1] = cos_a
        rot_mat[1, 2] = sin_a
        rot_mat[2, 1] = -sin_a
        rot_mat[2, 2] = cos_a
    else:
        print("Unknown input. 'r' or 'c' for row/column notation.")
        return 1

    return rot_mat

Response: Euler rotation about first axis


Instruction:
Implement a Python 3 function for
converting DCM to body Euler 3 - 1 - 3 angles

Response:
def dcm2body313(dcm):
    """Convert DCM to body Euler 3-1-3 angles

    """
    theta = np.zeros(3)

    theta[0] = np.arctan2(dcm[2, 0], dcm[2, 1])
    theta[1] = np.arccos(dcm[2, 2])
    theta[2] = np.arctan2(dcm[0, 2], -dcm[1, 2])

    return theta


Instruction:
Can you generate the documentation for the following Python 3 function
def hat_map(vec):
    vec = np.squeeze(vec)
    skew = np.array([
                    [0, -vec[2], vec[1]],
                    [vec[2], 0, -vec[0]],
                    [-vec[1], vec[0], 0]])

    return skew

Response: Return that hat map of a vector


Instruction:
Make a summary of the following Python 3 code
def vee_map(skew):

    vec = 1/2 * np.array([skew[2,1] - skew[1,2],
                          skew[0,2] - skew[2,0],
                          skew[1,0] - skew[0,1]])

    return vec

Response: Return the vee map of a vector


Instruction:
Given the following Python 3 function, write the documentation
def dcmtoquat(dcm):
    quat = np.zeros(4)
    quat[-1] = 1/2*np.sqrt(np.trace(dcm)+1)
    quat[0:3] = 1/4/quat[-1]*vee_map(dcm-dcm.T)

    return quat

Response: Convert DCM to quaternion


Instruction:
Can you write a function in Python 3 where it
converts quaternion to DCM

Response:
def quattodcm(quat):
    """Convert quaternion to DCM

    This function will convert a quaternion to the equivalent rotation matrix,
    or direction cosine matrix.

    Parameters:
    ----------
    quat - (4,) numpy array
        Array defining a quaterion where the quaternion is defined in terms of
        a vector and a scalar part.  The vector is related to the eigen axis
        and equivalent in both reference frames [x y z w]

    Returns:
    --------
    dcm - (3,3) numpy array
        Numpy rotation matrix which defines a rotation from the b to a frame
    """

    dcm = (quat[-1]**2-np.inner(quat[0:3], quat[0:3]))*np.eye(3,3) + 2*np.outer(quat[0:3],quat[0:3]) + 2*quat[-1]*hat_map(quat[0:3])

    return dcm


Instruction:
Implement a function in Python 3 to
convert a rotation matrix to angular velocity w Omega

Response:
def dcmdottoang_vel(R,Rdot):
    """Convert a rotation matrix to angular velocity

        w - angular velocity in inertial frame
        Omega - angular velocity in body frame
    """
    w = vee_map(Rdot.dot(R.T))
    Omega = vee_map(R.T.dot(Rdot))

    return (w, Omega)


Instruction:
Write a Python 3 script for
computing kinematics for axis angle representation

Response:
def ang_veltoaxisangledot(angle, axis, Omega):
    """Compute kinematics for axis angle representation

    """
    angle_dot = axis.dot(Omega)
    axis_dot = 1/2*(hat_map(axis) - 1/np.tan(angle/2) * hat_map(axis).dot(hat_map(axis))).dot(Omega)
    return angle_dot, axis_dot


Instruction:
How would you code a function in Python 3 to
convert axis angle represetnation to angular velocity in body frame

Response:
def axisangledottoang_vel(angle,axis, angle_dot,axis_dot):
    """Convert axis angle represetnation to angular velocity in body frame

    """

    Omega = angle_dot*axis + np.sin(angle)*axis_dot - (1-np.cos(angle))*hat_map(axis).dot(axis_dot)
    return Omega


Instruction:
Explain what the following Python 3 code does
def normalize(num_in, lower=0, upper=360, b=False):
    if lower >= upper:
        ValueError("lower must be lesser than upper")
    if not b:
        if not ((lower + upper == 0) or (lower == 0)):
            raise ValueError('When b=False lower=0 or range must be symmetric about 0.')
    else:
        if not (lower + upper == 0):
            raise ValueError('When b=True range must be symmetric about 0.')

    # abs(num + upper) and abs(num - lower) are needed, instead of
    # abs(num), since the lower and upper limits need not be 0. We need
    # to add half size of the range, so that the final result is lower +
    # <value> or upper - <value>, respectively.
    if not hasattr(num_in, "__iter__"):
        num_in = np.asarray([num_in], dtype=np.float)

    res  = []
    for num in num_in:
        if not b:
            if num > upper or num == lower:
                num = lower + abs(num + upper) % (abs(lower) + abs(upper))
            if num < lower or num == upper:
                num = upper - abs(num - lower) % (abs(lower) + abs(upper))

            res.append(lower if num == upper else num)
        else:
            total_length = abs(lower) + abs(upper)
            if num < -total_length:
                num += ceil(num / (-2 * total_length)) * 2 * total_length
            if num > total_length:
                num -= floor(num / (2 * total_length)) * 2 * total_length
            if num > upper:
                num = total_length - num
            if num < lower:
                num = -total_length - num

            res.append(num)

    return np.asarray(res, dtype=np.float)

Response: Normalizes a number to range [ lower upper ) or [ lower upper ).


Instruction:
How would you explain what the following Python 3 function does
def list_repos(owner=None, **kwargs):
    client = get_repos_api()

    api_kwargs = {}
    api_kwargs.update(utils.get_page_kwargs(**kwargs))

    # pylint: disable=fixme
    # FIXME: Compatibility code until we work out how to conflate
    # the overlapping repos_list methods into one.
    repos_list = client.repos_list_with_http_info

    if owner is not None:
        api_kwargs["owner"] = owner
        if hasattr(client, "repos_list0_with_http_info"):
            # pylint: disable=no-member
            repos_list = client.repos_list0_with_http_info
    else:
        if hasattr(client, "repos_all_list_with_http_info"):
            # pylint: disable=no-member
            repos_list = client.repos_all_list_with_http_info

    with catch_raise_api_exception():
        res, _, headers = repos_list(**api_kwargs)

    ratelimits.maybe_rate_limit(client, headers)
    page_info = PageInfo.from_headers(headers)
    return [x.to_dict() for x in res], page_info

Response: List repositories in a namespace.


Instruction:
Implement a function in Python 3 to
copy a package from one repository to another.

Response:
def copy(
    ctx,
    opts,
    owner_repo_package,
    destination,
    skip_errors,
    wait_interval,
    no_wait_for_sync,
    sync_attempts,
):
    """
    Copy a package to another repository.

    This requires appropriate permissions for both the source
    repository/package and the destination repository.

    - OWNER/REPO/PACKAGE: Specify the OWNER namespace (i.e. user or org), the
    REPO name where the package is stored, and the PACKAGE name (slug) of the
    package itself. All separated by a slash.

      Example: 'your-org/awesome-repo/better-pkg'.

    - DEST: Specify the DEST (destination) repository to copy the package to.
    This *must* be in the same namespace as the source repository.

      Example: 'other-repo'

    Full CLI example:

      $ cloudsmith cp your-org/awesome-repo/better-pkg other-repo
    """
    owner, source, slug = owner_repo_package

    click.echo(
        "Copying %(slug)s package from %(source)s to %(dest)s ... "
        % {
            "slug": click.style(slug, bold=True),
            "source": click.style(source, bold=True),
            "dest": click.style(destination, bold=True),
        },
        nl=False,
    )

    context_msg = "Failed to copy package!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        with maybe_spinner(opts):
            _, new_slug = copy_package(
                owner=owner, repo=source, identifier=slug, destination=destination
            )

    click.secho("OK", fg="green")

    if no_wait_for_sync:
        return

    wait_for_package_sync(
        ctx=ctx,
        opts=opts,
        owner=owner,
        repo=destination,
        slug=new_slug,
        wait_interval=wait_interval,
        skip_errors=skip_errors,
        attempts=sync_attempts,
    )


Instruction:
Explain what the following Python 3 code does
def rm_file_or_dir(path, ignore_errors=True):
    if os.path.exists(path):
        if os.path.isdir(path):
            if os.path.islink(path):
                os.unlink(path)
            else:
                shutil.rmtree(path, ignore_errors=ignore_errors)
        else:
            if os.path.islink(path):
                os.unlink(path)
            else:
                os.remove(path)

Response: Helper function to remove a certain filepath from the system.


Instruction:
Write a Python 3 function for
checking whether the column has a factorization that exists and is not older than the source

Response:
def cache_valid(self, col):
        """
        Checks whether the column has a factorization that exists and is not older than the source

        :param col:
        :return:
        """
        cache_valid = False

        if self.rootdir:
            col_org_file_check = self[col].rootdir + '/__attrs__'
            col_values_file_check = self[col].rootdir + '.values/__attrs__'
            cache_valid = os.path.exists(col_org_file_check) and os.path.exists(col_values_file_check)

        return cache_valid


Instruction:
How would you explain what the following Python 3 function does
def group_cache_valid(self, col_list):
        cache_valid = False

        if self.rootdir:
            col_values_file_check = os.path.join(self.rootdir, self.create_group_base_name(col_list)) + \
                                    '.values/__attrs__'

            exists_group_index = os.path.exists(col_values_file_check)
            missing_col_check = [1 for col in col_list if not os.path.exists(self[col].rootdir + '/__attrs__')]
            cache_valid = (exists_group_index and not missing_col_check)

        return cache_valid

Response: Checks whether the group has a factorization that exists and is not older than the source ArcGIS group.


Instruction:
Create a Python 3 function for
existing todos here are: these should be hidden helper carrays As in: not normal columns that you would normally see as a user The factor (label index) carray is as long as the original carray (and the rest of the table therefore) But the (unique) values carray is not as long (as long as the number of unique values) :param col_list: :param refresh: :return:

Response:
def cache_factor(self, col_list, refresh=False):
        """
        Existing todos here are: these should be hidden helper carrays
        As in: not normal columns that you would normally see as a user

        The factor (label index) carray is as long as the original carray
        (and the rest of the table therefore)
        But the (unique) values carray is not as long (as long as the number
        of unique values)

        :param col_list:
        :param refresh:
        :return:
        """

        if not self.rootdir:
            raise TypeError('Only out-of-core ctables can have '
                            'factorization caching at the moment')

        if not isinstance(col_list, list):
            col_list = [col_list]

        if refresh:
            kill_list = [x for x in os.listdir(self.rootdir) if '.factor' in x or '.values' in x]
            for kill_dir in kill_list:
                rm_file_or_dir(os.path.join(self.rootdir, kill_dir))

        for col in col_list:

            # create cache if needed
            if refresh or not self.cache_valid(col):
                # todo: also add locking mechanism here

                # create directories
                col_rootdir = self[col].rootdir
                col_factor_rootdir = col_rootdir + '.factor'
                col_factor_rootdir_tmp = tempfile.mkdtemp(prefix='bcolz-')
                col_values_rootdir = col_rootdir + '.values'
                col_values_rootdir_tmp = tempfile.mkdtemp(prefix='bcolz-')

                # create factor
                carray_factor = \
                    bcolz.carray([], dtype='int64', expectedlen=self.size,
                                 rootdir=col_factor_rootdir_tmp, mode='w')
                _, values = \
                    ctable_ext.factorize(self[col], labels=carray_factor)
                carray_factor.flush()

                rm_file_or_dir(col_factor_rootdir, ignore_errors=True)
                shutil.move(col_factor_rootdir_tmp, col_factor_rootdir)

                # create values
                carray_values = \
                    bcolz.carray(np.fromiter(values.values(), dtype=self[col].dtype),
                                 rootdir=col_values_rootdir_tmp, mode='w')
                carray_values.flush()
                rm_file_or_dir(col_values_rootdir, ignore_errors=True)
                shutil.move(col_values_rootdir_tmp, col_values_rootdir)


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

        if isinstance(col_or_col_list, list):
            col_is_list = True
            col_list = col_or_col_list
        else:
            col_is_list = False
            col_list = [col_or_col_list]

        output = []

        for col in col_list:

            if self.auto_cache or self.cache_valid(col):
                # create factorization cache
                if not self.cache_valid(col):
                    self.cache_factor([col])

                # retrieve values from existing disk-based factorization
                col_values_rootdir = self[col].rootdir + '.values'
                carray_values = bcolz.carray(rootdir=col_values_rootdir, mode='r')
                values = list(carray_values)
            else:
                # factorize on-the-fly
                _, values = ctable_ext.factorize(self[col])
                values = values.values()

            output.append(values)

        if not col_is_list:
            output = output[0]

        return output

Response: Return a list of unique values of a column or a list of lists of column list


Instruction:
Make a summary of the following Python 3 code
def aggregate_groups(self, ct_agg, nr_groups, skip_key,
                         carray_factor, groupby_cols, agg_ops,
                         dtype_dict, bool_arr=None):
        '''Perform aggregation and place the result in the given ctable.

        Args:
            ct_agg (ctable): the table to hold the aggregation
            nr_groups (int): the number of groups (number of rows in output table)
            skip_key (int): index of the output row to remove from results (used for filtering)
            carray_factor: the carray for each row in the table a reference to the the unique group index
            groupby_cols: the list of 'dimension' columns that are used to perform the groupby over
            output_agg_ops (list): list of tuples of the form: (input_col, agg_op)
                    input_col (string): name of the column to act on
                    agg_op (int): aggregation operation to perform
            bool_arr: a boolean array containing the filter

        '''

        # this creates the groupby columns
        for col in groupby_cols:

            result_array = ctable_ext.groupby_value(self[col], carray_factor,
                                                    nr_groups, skip_key)

            if bool_arr is not None:
                result_array = np.delete(result_array, skip_key)

            ct_agg.addcol(result_array, name=col)
            del result_array

        # this creates the aggregation columns
        for input_col_name, output_col_name, agg_op in agg_ops:

            input_col = self[input_col_name]
            output_col_dtype = dtype_dict[output_col_name]

            input_buffer = np.empty(input_col.chunklen, dtype=input_col.dtype)
            output_buffer = np.zeros(nr_groups, dtype=output_col_dtype)

            if agg_op == 'sum':
                ctable_ext.aggregate_sum(input_col, carray_factor, nr_groups,
                                         skip_key, input_buffer, output_buffer)
            elif agg_op == 'mean':
                ctable_ext.aggregate_mean(input_col, carray_factor, nr_groups,
                                          skip_key, input_buffer, output_buffer)
            elif agg_op == 'std':
                ctable_ext.aggregate_std(input_col, carray_factor, nr_groups,
                                         skip_key, input_buffer, output_buffer)
            elif agg_op == 'count':
                ctable_ext.aggregate_count(input_col, carray_factor, nr_groups,
                                           skip_key, input_buffer, output_buffer)
            elif agg_op == 'count_distinct':
                ctable_ext.aggregate_count_distinct(input_col, carray_factor, nr_groups,
                                                    skip_key, input_buffer, output_buffer)
            elif agg_op == 'sorted_count_distinct':
                ctable_ext.aggregate_sorted_count_distinct(input_col, carray_factor, nr_groups,
                                                           skip_key, input_buffer, output_buffer)
            else:
                raise KeyError('Unknown aggregation operation ' + str(agg_op))

            if bool_arr is not None:
                output_buffer = np.delete(output_buffer, skip_key)

            ct_agg.addcol(output_buffer, name=output_col_name)
            del output_buffer

        ct_agg.delcol('tmp_col_bquery__')

Response: Perform aggregation and place the result in the given ctable.


Instruction:
Given the following Python 3 function, write the documentation
def factorize_groupby_cols(self, groupby_cols):
        # first check if the factorized arrays already exist
        # unless we need to refresh the cache
        factor_list = []
        values_list = []

        # factorize the groupby columns
        for col in groupby_cols:

            if self.auto_cache or self.cache_valid(col):
                # create factorization cache if needed
                if not self.cache_valid(col):
                    self.cache_factor([col])

                col_rootdir = self[col].rootdir
                col_factor_rootdir = col_rootdir + '.factor'
                col_values_rootdir = col_rootdir + '.values'
                col_carray_factor = \
                    bcolz.carray(rootdir=col_factor_rootdir, mode='r')
                col_carray_values = \
                    bcolz.carray(rootdir=col_values_rootdir, mode='r')
            else:
                col_carray_factor, values = ctable_ext.factorize(self[col])
                col_carray_values = \
                    bcolz.carray(np.fromiter(values.values(), dtype=self[col].dtype))

            factor_list.append(col_carray_factor)
            values_list.append(col_carray_values)

        return factor_list, values_list

Response: factorizes all columns that are used in the groupby


Instruction:
How would you explain what the following Python 3 function does
def create_group_column_factor(self, factor_list, groupby_cols, cache=False):
        if not self.rootdir:
            # in-memory scenario
            input_rootdir = None
            col_rootdir = None
            col_factor_rootdir = None
            col_values_rootdir = None
            col_factor_rootdir_tmp = None
            col_values_rootdir_tmp = None
        else:
            # temporary
            input_rootdir = tempfile.mkdtemp(prefix='bcolz-')
            col_factor_rootdir_tmp = tempfile.mkdtemp(prefix='bcolz-')
            col_values_rootdir_tmp = tempfile.mkdtemp(prefix='bcolz-')

        # create combination of groupby columns
        group_array = bcolz.zeros(0, dtype=np.int64, expectedlen=len(self), rootdir=input_rootdir, mode='w')
        factor_table = bcolz.ctable(factor_list, names=groupby_cols)
        ctable_iter = factor_table.iter(outcols=groupby_cols, out_flavor=tuple)
        ctable_ext.create_group_index(ctable_iter, len(groupby_cols), group_array)

        # now factorize the results
        carray_factor = \
            bcolz.carray([], dtype='int64', expectedlen=self.size, rootdir=col_factor_rootdir_tmp, mode='w')
        carray_factor, values = ctable_ext.factorize(group_array, labels=carray_factor)
        carray_factor.flush()

        carray_values = \
            bcolz.carray(np.fromiter(values.values(), dtype=np.int64), rootdir=col_values_rootdir_tmp, mode='w')
        carray_values.flush()

        del group_array
        if cache:
            # clean up the temporary file
            rm_file_or_dir(input_rootdir, ignore_errors=True)

        if cache:
            # official end destination
            col_rootdir = os.path.join(self.rootdir, self.create_group_base_name(groupby_cols))
            col_factor_rootdir = col_rootdir + '.factor'
            col_values_rootdir = col_rootdir + '.values'
            lock_file = col_rootdir + '.lock'

            # only works for linux
            if not os.path.exists(lock_file):
                uid = str(uuid.uuid4())
                try:
                    with open(lock_file, 'a+') as fn:
                        fn.write(uid + '\n')
                    with open(lock_file, 'r') as fn:
                        temp = fn.read().splitlines()
                    if temp[0] == uid:
                        lock = True
                    else:
                        lock = False
                    del temp
                except:
                    lock = False
            else:
                lock = False

            if lock:
                rm_file_or_dir(col_factor_rootdir, ignore_errors=False)
                shutil.move(col_factor_rootdir_tmp, col_factor_rootdir)
                carray_factor = bcolz.carray(rootdir=col_factor_rootdir, mode='r')

                rm_file_or_dir(col_values_rootdir, ignore_errors=False)
                shutil.move(col_values_rootdir_tmp, col_values_rootdir)
                carray_values = bcolz.carray(rootdir=col_values_rootdir, mode='r')
            else:
                # another process has a lock, we will work with our current files and clean up later
                self._dir_clean_list.append(col_factor_rootdir)
                self._dir_clean_list.append(col_values_rootdir)

        return carray_factor, carray_values

Response: Create a unique factorized column out of several individual columns.


Instruction:
Create a Python 3 function to
create unique groups for groupby loop

Response:
def make_group_index(self, groupby_cols, bool_arr):
        '''Create unique groups for groupby loop

            Args:
                factor_list:
                values_list:
                groupby_cols:
                bool_arr:

            Returns:
                carray: (carray_factor)
                int: (nr_groups) the number of resulting groups
                int: (skip_key)
        '''
        factor_list, values_list = self.factorize_groupby_cols(groupby_cols)

        # create unique groups for groupby loop
        if len(factor_list) == 0:
            # no columns to groupby over, so directly aggregate the measure
            # columns to 1 total
            tmp_rootdir = self.create_tmp_rootdir()
            carray_factor = bcolz.zeros(len(self), dtype='int64', rootdir=tmp_rootdir, mode='w')
            carray_values = ['Total']
        elif len(factor_list) == 1:
            # single column groupby, the groupby output column
            # here is 1:1 to the values
            carray_factor = factor_list[0]
            carray_values = values_list[0]
        else:
            # multi column groupby
            # first combine the factorized columns to single values
            if self.group_cache_valid(col_list=groupby_cols):
                # there is a group cache that we can use
                col_rootdir = os.path.join(self.rootdir, self.create_group_base_name(groupby_cols))
                col_factor_rootdir = col_rootdir + '.factor'
                carray_factor = bcolz.carray(rootdir=col_factor_rootdir)
                col_values_rootdir = col_rootdir + '.values'
                carray_values = bcolz.carray(rootdir=col_values_rootdir)
            else:
                # create a brand new groupby col combination
                carray_factor, carray_values = \
                    self.create_group_column_factor(factor_list, groupby_cols, cache=self.auto_cache)

        nr_groups = len(carray_values)
        skip_key = None

        if bool_arr is not None:
            # make all non relevant combinations -1
            tmp_rootdir = self.create_tmp_rootdir()
            carray_factor = bcolz.eval(
                '(factor + 1) * bool - 1',
                user_dict={'factor': carray_factor, 'bool': bool_arr}, rootdir=tmp_rootdir, mode='w')
            # now check how many unique values there are left
            tmp_rootdir = self.create_tmp_rootdir()
            labels = bcolz.carray([], dtype='int64', expectedlen=len(carray_factor), rootdir=tmp_rootdir, mode='w')
            carray_factor, values = ctable_ext.factorize(carray_factor, labels)
            # values might contain one value too much (-1) (no direct lookup
            # possible because values is a reversed dict)
            filter_check = \
                [key for key, value in values.items() if value == -1]
            if filter_check:
                skip_key = filter_check[0]
            # the new nr of groups depends on the outcome after filtering
            nr_groups = len(values)

        # using nr_groups as a total length might be one one off due to the skip_key
        # (skipping a row in aggregation)
        # but that is okay normally

        if skip_key is None:
            # if we shouldn't skip a row, set it at the first row after the total number of groups
            skip_key = nr_groups

        return carray_factor, nr_groups, skip_key


Instruction:
Write a Python 3 script to
create a temporary rootdir that we can destroy later again

Response:
def create_tmp_rootdir(self):
        """
        create a rootdir that we can destroy later again

        Returns
        -------

        """
        if self.rootdir:
            tmp_rootdir = tempfile.mkdtemp(prefix='bcolz-')
            self._dir_clean_list.append(tmp_rootdir)
        else:
            tmp_rootdir = None
        return tmp_rootdir


Instruction:
Write a Python 3 function that can
clean up all used temporary rootdirs

Response:
def clean_tmp_rootdir(self):
        """
        clean up all used temporary rootdirs

        Returns
        -------

        """
        for tmp_rootdir in list(self._dir_clean_list):
            rm_file_or_dir(tmp_rootdir)
            self._dir_clean_list.remove(tmp_rootdir)


Instruction:
Implement a Python 3 function for
creating a container for the output table a dictionary describing it s columns and a list of tuples describing aggregation operations to perform.

Response:
def create_agg_ctable(self, groupby_cols, agg_list, expectedlen, rootdir):
        '''Create a container for the output table, a dictionary describing it's
            columns and a list of tuples describing aggregation
            operations to perform.

        Args:
            groupby_cols (list): a list of columns to groupby over
            agg_list (list): the aggregation operations (see groupby for more info)
            expectedlen (int): expected length of output table
            rootdir (string): the directory to write the table to

        Returns:
            ctable: A table in the correct format for containing the output of
                    the specified aggregation operations.
            dict: (dtype_dict) dictionary describing columns to create
            list: (agg_ops) list of tuples of the form:
                    (input_col_name, output_col_name, agg_op)
                    input_col_name (string): name of the column to act on
                    output_col_name (string): name of the column to output to
                    agg_op (int): aggregation operation to perform
        '''
        dtype_dict = {}

        # include all the groupby columns
        for col in groupby_cols:
            dtype_dict[col] = self[col].dtype

        agg_ops_list = ['sum', 'count', 'count_distinct', 'sorted_count_distinct', 'mean', 'std']
        agg_ops = []

        for agg_info in agg_list:

            if not isinstance(agg_info, list):
                # example: ['m1', 'm2', ...]
                # default operation (sum) and default output column name (same is input)
                output_col_name = agg_info
                input_col_name = agg_info
                agg_op = 'sum'
            else:
                input_col_name = agg_info[0]
                agg_op = agg_info[1]

                if len(agg_info) == 2:
                    # example: [['m1', 'sum'], ['m2', 'mean], ...]
                    # default output column name
                    output_col_name = input_col_name
                else:
                    # example: [['m1', 'sum', 'mnew1'], ['m1, 'mean','mnew2'], ...]
                    # fully specified
                    output_col_name = agg_info[2]

            if agg_op not in agg_ops_list:
                raise NotImplementedError(
                    'Unknown Aggregation Type: ' + str(agg_op))

            # choose output column dtype based on aggregation operation and
            # input column dtype
            # TODO: check if the aggregation columns is numeric
            # NB: we could build a concatenation for strings like pandas, but I would really prefer to see that as a
            # separate operation
            if agg_op in ('count', 'count_distinct', 'sorted_count_distinct'):
                output_col_dtype = np.dtype(np.int64)
            elif agg_op in ('mean', 'std'):
                output_col_dtype = np.dtype(np.float64)
            else:
                output_col_dtype = self[input_col_name].dtype

            dtype_dict[output_col_name] = output_col_dtype

            # save output
            agg_ops.append((input_col_name, output_col_name, agg_op))

        # create aggregation table
        ct_agg = bcolz.ctable(
            np.zeros(expectedlen, [('tmp_col_bquery__', np.bool)]),
            expectedlen=expectedlen,
            rootdir=rootdir)

        return ct_agg, dtype_dict, agg_ops


Instruction:
Can you create a Python 3 function that
creates a boolean array where the terms in term_list are true.

Response:
def where_terms(self, term_list, cache=False):
        """
        Create a boolean array where `term_list` is true.
        A terms list has a [(col, operator, value), ..] construction.
        Eg. [('sales', '>', 2), ('state', 'in', ['IL', 'AR'])]

        :param term_list:
        :param outcols:
        :param limit:
        :param skip:
        :return: :raise ValueError:
        """

        if type(term_list) not in [list, set, tuple]:
            raise ValueError("Only term lists are supported")

        col_list = []
        op_list = []
        value_list = []

        for term in term_list:
            # get terms
            filter_col = term[0]
            filter_operator = term[1].lower().strip(' ')
            filter_value = term[2]

            # check values
            if filter_col not in self.cols:
                raise KeyError(unicode(filter_col) + ' not in table')

            if filter_operator in ['==', 'eq']:
                op_id = 1
            elif filter_operator in ['!=', 'neq']:
                op_id = 2
            elif filter_operator in ['in']:
                op_id = 3
            elif filter_operator in ['nin', 'not in']:
                op_id = 4
            elif filter_operator in ['>']:
                op_id = 5
            elif filter_operator in ['>=']:
                op_id = 6
            elif filter_operator in ['<']:
                op_id = 7
            elif filter_operator in ['<=']:
                op_id = 8
            else:
                raise KeyError(unicode(filter_operator) + ' is not an accepted operator for filtering')

            if op_id in [3, 4]:
                if type(filter_value) not in [list, set, tuple]:
                    raise ValueError("In selections need lists, sets or tuples")

                if len(filter_value) < 1:
                    raise ValueError("A value list needs to have values")

                # optimize lists of 1 value
                if len(filter_value) == 1:
                    if op_id == 3:
                        op_id = 1
                    else:
                        op_id = 2

                    filter_value = filter_value[0]
                else:
                    filter_value = set(filter_value)

            # prepare input for filter creation
            col_list.append(filter_col)
            op_list.append(op_id)
            value_list.append(filter_value)

        # rootdir
        if cache:
            # nb: this directory is not destroyed until the end of the groupby
            rootdir = self.create_tmp_rootdir()
        else:
            rootdir = None

        # create boolean array and fill it
        boolarr = bcolz.carray(np.ones(0, dtype=np.bool), expectedlen=self.len, rootdir=rootdir, mode='w')
        ctable_iter = self[col_list].iter(out_flavor='tuple')
        ctable_ext.apply_where_terms(ctable_iter, op_list, value_list, boolarr)

        return boolarr


Instruction:
Create a Python 3 function for
checking for where terms are applicable

Response:
def where_terms_factorization_check(self, term_list):
        """
        check for where terms if they are applicable
        Create a boolean array where `term_list` is true.
        A terms list has a [(col, operator, value), ..] construction.
        Eg. [('sales', '>', 2), ('state', 'in', ['IL', 'AR'])]

        :param term_list:
        :param outcols:
        :param limit:
        :param skip:
        :return: :raise ValueError:
        """

        if type(term_list) not in [list, set, tuple]:
            raise ValueError("Only term lists are supported")

        valid = True

        for term in term_list:
            # get terms
            filter_col = term[0]
            filter_operator = term[1].lower().strip(' ')
            filter_value = term[2]

            # check values
            if filter_col not in self.cols:
                raise KeyError(unicode(filter_col) + ' not in table')

            col_values_rootdir = os.path.join(self.rootdir, filter_col + '.values')

            if not os.path.exists(col_values_rootdir):
                # no factorization available
                break

            col_carray = bcolz.carray(rootdir=col_values_rootdir, mode='r')
            col_values = set(col_carray)

            if filter_operator in ['in', 'not in', 'nin']:
                if type(filter_value) not in [list, set, tuple]:
                    raise ValueError("In selections need lists, sets or tuples")
                if len(filter_value) < 1:
                    raise ValueError("A value list needs to have values")

                # optimize lists of 1 value
                if len(filter_value) == 1:
                    filter_value = filter_value[0]
                    if filter_operator == 'in':
                        filter_operator = '=='
                    else:
                        filter_operator = '!='
                else:
                    filter_value = set(filter_value)

            if filter_operator in ['==', 'eq']:
                valid = filter_value in col_values
            elif filter_operator in ['!=', 'neq']:
                valid = any(val for val in col_values if val != filter_value)
            elif filter_operator in ['in']:
                valid = any(val for val in filter_value if val in col_values)
            elif filter_operator in ['nin', 'not in']:
                valid = any(val for val in col_values if val not in filter_value)
            elif filter_operator in ['>']:
                valid = any(val for val in col_values if val > filter_value)
            elif filter_operator in ['>=']:
                valid = any(val for val in col_values if val >= filter_value)
            elif filter_operator in ['<']:
                valid = any(val for val in col_values if val < filter_value)
            elif filter_operator in ['<=']:
                valid = any(val for val in col_values if val >= filter_value)
            else:
                raise KeyError(str(filter_operator) + ' is not an accepted operator for filtering')

            # if one of the filters is blocking, we can stop
            if not valid:
                break

        return valid


Instruction:
Explain what the following Python 3 code does
def is_in_ordered_subgroups(self, basket_col=None, bool_arr=None,
                                _max_len_subgroup=1000):
        """
        Expands the filter using a specified column

        Parameters
        ----------
        basket_col
        bool_arr
        _max_len_subgroup

        Returns
        -------

        """
        assert basket_col is not None

        if bool_arr is None:
            return None

        if self.auto_cache and bool_arr.rootdir is not None:
            rootdir = self.create_tmp_rootdir()
        else:
            rootdir = None

        return \
            ctable_ext.is_in_ordered_subgroups(
                self[basket_col], bool_arr=bool_arr, rootdir=rootdir,
                _max_len_subgroup=_max_len_subgroup)

Response: Returns True if the entry is in the ordered subgroups of the entry.


Instruction:
Given the following Python 3 function, write the documentation
def with_metaclass(Type, skip_attrs=set(('__dict__', '__weakref__'))):

    def _clone_with_metaclass(Class):
        attrs = dict((key, value) for key, value in items(vars(Class))
                     if key not in skip_attrs)
        return Type(Class.__name__, Class.__bases__, attrs)

    return _clone_with_metaclass

Response: Class decorator to set metaclass for base classes.


Instruction:
Write a Python 3 script for
yielding length - sized tuples from the given collection.

Response:
def ensure_tuple(length, tuples):
	"""Yield `length`-sized tuples from the given collection.

	Will truncate longer tuples to the desired length, and pad using the leading element if shorter.
	"""

	for elem in tuples:
		# Handle non-tuples and non-lists as a single repeated element.
		if not isinstance(elem, (tuple, list)):
			yield (elem, ) * length
			continue

		l = len(elem)

		# If we have the correct length already, yield it.
		if l == length:
			yield elem

		# If we're too long, truncate.
		elif l > length:
			yield tuple(elem[:length])

		# If we're too short, pad the *leading* element out.
		elif l < length:
			yield (elem[0], ) * (length - l) + tuple(elem)


Instruction:
Implement a Python 3 function for
printing the environment versions.

Response:
def print_version():
    """Print the environment versions."""
    click.echo("Versions:")
    click.secho(
        "CLI Package Version: %(version)s"
        % {"version": click.style(get_cli_version(), bold=True)}
    )
    click.secho(
        "API Package Version: %(version)s"
        % {"version": click.style(get_api_version(), bold=True)}
    )


Instruction:
How would you code a function in Python 3 to
handle entrypoint to CLI.

Response:
def main(ctx, opts, version):
    """Handle entrypoint to CLI."""
    # pylint: disable=unused-argument
    if version:
        print_version()
    elif ctx.invoked_subcommand is None:
        click.echo(ctx.get_help())


Instruction:
Explain what the following Python 3 code does
def validate_upload_file(ctx, opts, owner, repo, filepath, skip_errors):
    filename = click.format_filename(filepath)
    basename = os.path.basename(filename)

    click.echo(
        "Checking %(filename)s file upload parameters ... "
        % {"filename": click.style(basename, bold=True)},
        nl=False,
    )

    context_msg = "Failed to validate upload parameters!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        with maybe_spinner(opts):
            md5_checksum = validate_request_file_upload(
                owner=owner, repo=repo, filepath=filename
            )

    click.secho("OK", fg="green")

    return md5_checksum

Response: Validate parameters for requesting a file upload.


Instruction:
Can you write a function in Python 3 where it
uploads a file via the API.

Response:
def upload_file(ctx, opts, owner, repo, filepath, skip_errors, md5_checksum):
    """Upload a package file via the API."""
    filename = click.format_filename(filepath)
    basename = os.path.basename(filename)

    click.echo(
        "Requesting file upload for %(filename)s ... "
        % {"filename": click.style(basename, bold=True)},
        nl=False,
    )

    context_msg = "Failed to request file upload!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        with maybe_spinner(opts):
            identifier, upload_url, upload_fields = request_file_upload(
                owner=owner, repo=repo, filepath=filename, md5_checksum=md5_checksum
            )

    click.secho("OK", fg="green")

    context_msg = "Failed to upload file!"
    with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
        filesize = utils.get_file_size(filepath=filename)

        label = "Uploading %(filename)s:" % {
            "filename": click.style(basename, bold=True)
        }

        with click.progressbar(
            length=filesize,
            label=label,
            fill_char=click.style("#", fg="green"),
            empty_char=click.style("-", fg="red"),
        ) as pb:

            def progress_callback(monitor):
                pb.update(monitor.bytes_read)

            api_upload_file(
                upload_url=upload_url,
                upload_fields=upload_fields,
                filepath=filename,
                callback=progress_callback,
            )

    return identifier


Instruction:
Here you have a function in Python 3, explain what it does
def validate_create_package(
    ctx, opts, owner, repo, package_type, skip_errors, **kwargs
):
    """Check new package parameters via the API."""
    click.echo(
        "Checking %(package_type)s package upload parameters ... "
        % {"package_type": click.style(package_type, bold=True)},
        nl=False,
    )

    context_msg = "Failed to validate upload parameters!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        with maybe_spinner(opts):
            api_validate_create_package(
                package_format=package_type, owner=owner, repo=repo, **kwargs
            )

    click.secho("OK", fg="green")
    return True

Response: Check new package parameters via the API.


Instruction:
Make a summary of the following Python 3 code
def create_package(ctx, opts, owner, repo, package_type, skip_errors, **kwargs):
    click.echo(
        "Creating a new %(package_type)s package ... "
        % {"package_type": click.style(package_type, bold=True)},
        nl=False,
    )

    context_msg = "Failed to create package!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        with maybe_spinner(opts):
            slug_perm, slug = api_create_package(
                package_format=package_type, owner=owner, repo=repo, **kwargs
            )

    click.secho("OK", fg="green")

    click.echo(
        "Created: %(owner)s/%(repo)s/%(slug)s (%(slug_perm)s)"
        % {
            "owner": click.style(owner, fg="magenta"),
            "repo": click.style(repo, fg="magenta"),
            "slug": click.style(slug, fg="green"),
            "slug_perm": click.style(slug_perm, bold=True),
        }
    )

    return slug_perm, slug

Response: Create a new package via the API.


Instruction:
Create a Python 3 function for
waiting for a package to be synced.

Response:
def wait_for_package_sync(
    ctx, opts, owner, repo, slug, wait_interval, skip_errors, attempts=3
):
    """Wait for a package to synchronise (or fail)."""
    # pylint: disable=too-many-locals
    attempts -= 1
    click.echo()
    label = "Synchronising %(package)s:" % {"package": click.style(slug, fg="green")}

    status_str = "Waiting"
    stage_str = None

    def display_status(current):
        """Display current sync status."""
        # pylint: disable=unused-argument
        if not stage_str:
            return status_str
        return click.style(
            "%(status)s / %(stage)s" % {"status": status_str, "stage": stage_str},
            fg="cyan",
        )

    context_msg = "Failed to synchronise file!"
    with handle_api_exceptions(
        ctx, opts=opts, context_msg=context_msg, reraise_on_error=skip_errors
    ):
        last_progress = 0
        with click.progressbar(
            length=100,
            label=label,
            fill_char=click.style("#", fg="green"),
            empty_char=click.style("-", fg="red"),
            item_show_func=display_status,
        ) as pb:
            while True:
                res = get_package_status(owner, repo, slug)
                ok, failed, progress, status_str, stage_str, reason = res
                delta = progress - last_progress
                if delta > 0:
                    last_progress = progress
                    pb.update(delta)
                if ok or failed:
                    break
                time.sleep(wait_interval)

    if ok:
        click.secho("Package synchronised successfully!", fg="green")
        return

    click.secho(
        "Package failed to synchronise during stage: %(stage)s"
        % {"stage": click.style(stage_str or "Unknown", fg="yellow")},
        fg="red",
    )

    if reason:
        click.secho(
            "Reason given: %(reason)s" % {"reason": click.style(reason, fg="yellow")},
            fg="red",
        )

        # pylint: disable=fixme
        # FIXME: The API should communicate "no retry" fails
        if "package should be deleted" in reason and attempts > 1:
            click.secho(
                "This is not recoverable, so stopping further attempts!", fg="red"
            )
            click.echo()
            attempts = 0

    if attempts + 1 > 0:
        # Show attempts upto and including zero attempts left
        click.secho(
            "Attempts left: %(left)s (%(action)s)"
            % {
                "left": click.style(six.text_type(attempts), bold=True),
                "action": "trying again" if attempts > 0 else "giving up",
            }
        )
        click.echo()

    if attempts > 0:
        from .resync import resync_package

        resync_package(
            ctx=ctx,
            opts=opts,
            owner=owner,
            repo=repo,
            slug=slug,
            skip_errors=skip_errors,
        )

        wait_for_package_sync(
            ctx=ctx,
            opts=opts,
            owner=owner,
            repo=repo,
            slug=slug,
            wait_interval=wait_interval,
            skip_errors=skip_errors,
            attempts=attempts,
        )
    else:
        ctx.exit(1)


Instruction:
How would you explain what the following Python 3 function does
def upload_files_and_create_package(
    ctx,
    opts,
    package_type,
    owner_repo,
    dry_run,
    no_wait_for_sync,
    wait_interval,
    skip_errors,
    sync_attempts,
    **kwargs
):
    """Upload package files and create a new package."""
    # pylint: disable=unused-argument
    owner, repo = owner_repo

    # 1. Validate package create parameters
    validate_create_package(
        ctx=ctx,
        opts=opts,
        owner=owner,
        repo=repo,
        package_type=package_type,
        skip_errors=skip_errors,
        **kwargs
    )

    # 2. Validate file upload parameters
    md5_checksums = {}
    for k, v in kwargs.items():
        if not v or not k.endswith("_file"):
            continue

        md5_checksums[k] = validate_upload_file(
            ctx=ctx,
            opts=opts,
            owner=owner,
            repo=repo,
            filepath=v,
            skip_errors=skip_errors,
        )

    if dry_run:
        click.echo()
        click.secho("You requested a dry run so skipping upload.", fg="yellow")
        return

    # 3. Upload any arguments that look like files
    for k, v in kwargs.items():
        if not v or not k.endswith("_file"):
            continue

        kwargs[k] = upload_file(
            ctx=ctx,
            opts=opts,
            owner=owner,
            repo=repo,
            filepath=v,
            skip_errors=skip_errors,
            md5_checksum=md5_checksums[k],
        )

    # 4. Create the package with package files and additional arguments
    _, slug = create_package(
        ctx=ctx,
        opts=opts,
        owner=owner,
        repo=repo,
        package_type=package_type,
        skip_errors=skip_errors,
        **kwargs
    )

    if no_wait_for_sync:
        return

    # 5. (optionally) Wait for the package to synchronise
    wait_for_package_sync(
        ctx=ctx,
        opts=opts,
        owner=owner,
        repo=repo,
        slug=slug,
        wait_interval=wait_interval,
        skip_errors=skip_errors,
        attempts=sync_attempts,
    )

Response: Upload files and create a new package.


Instruction:
Here you have a function in Python 3, explain what it does
def create_push_handlers():
    # pylint: disable=fixme
    # HACK: hacky territory - Dynamically generate a handler for each of the
    # package formats, until we have slightly more clever 'guess type'
    # handling. :-)
    handlers = create_push_handlers.handlers = {}
    context = create_push_handlers.context = get_package_formats()

    for key, parameters in six.iteritems(context):
        kwargs = parameters.copy()

        # Remove standard arguments
        kwargs.pop("package_file")
        if "distribution" in parameters:
            has_distribution_param = True
            kwargs.pop("distribution")
        else:
            has_distribution_param = False

        has_additional_params = len(kwargs) > 0

        help_text = """
            Push/upload a new %(type)s package upstream.
            """ % {
            "type": key.capitalize()
        }

        if has_additional_params:
            help_text += """

            PACKAGE_FILE: The main file to create the package from.
            """
        else:
            help_text += """

            PACKAGE_FILE: Any number of files to create packages from. Each
            file will result in a separate package.
            """

        if has_distribution_param:
            target_metavar = "OWNER/REPO/DISTRO/RELEASE"
            target_callback = validators.validate_owner_repo_distro
            help_text += """

            OWNER/REPO/DISTRO/RELEASE: Specify the OWNER namespace (i.e.
            user or org), the REPO name where the package file will be uploaded
            to, and the DISTRO and RELEASE the package is for. All separated by
            a slash.

            Example: 'your-org/awesome-repo/ubuntu/xenial'.
            """
        else:
            target_metavar = "OWNER/REPO"
            target_callback = validators.validate_owner_repo
            help_text += """

            OWNER/REPO: Specify the OWNER namespace (i.e. user or org), and the
            REPO name where the package file will be uploaded to. All separated
            by a slash.

            Example: 'your-org/awesome-repo'.
            """

        @push.command(name=key, help=help_text)
        @decorators.common_api_auth_options
        @decorators.common_cli_config_options
        @decorators.common_cli_output_options
        @decorators.common_package_action_options
        @decorators.initialise_api
        @click.argument("owner_repo", metavar=target_metavar, callback=target_callback)
        @click.argument(
            "package_file",
            nargs=1 if has_additional_params else -1,
            type=ExpandPath(
                dir_okay=False, exists=True, writable=False, resolve_path=True
            ),
        )
        @click.option(
            "-n",
            "--dry-run",
            default=False,
            is_flag=True,
            help="Execute in dry run mode (don't upload anything.)",
        )
        @click.option(
            "-n",
            "--dry-run",
            default=False,
            is_flag=True,
            help="Execute in dry run mode (don't upload anything.)",
        )
        @click.pass_context
        def push_handler(ctx, *args, **kwargs):
            """Handle upload for a specific package format."""
            parameters = context.get(ctx.info_name)
            kwargs["package_type"] = ctx.info_name

            owner_repo = kwargs.pop("owner_repo")
            if "distribution" in parameters:
                kwargs["distribution"] = "/".join(owner_repo[2:])
                owner_repo = owner_repo[0:2]
            kwargs["owner_repo"] = owner_repo

            package_files = kwargs.pop("package_file")
            if not isinstance(package_files, tuple):
                package_files = (package_files,)

            for package_file in package_files:
                kwargs["package_file"] = package_file

                try:
                    click.echo()
                    upload_files_and_create_package(ctx, *args, **kwargs)
                except ApiException:
                    click.secho("Skipping error and moving on.", fg="yellow")

                click.echo()

        # Add any additional arguments
        for k, info in six.iteritems(kwargs):
            option_kwargs = {}
            option_name_fmt = "--%(key)s"

            if k.endswith("_file"):
                # Treat parameters that end with _file as uploadable filepaths.
                option_kwargs["type"] = ExpandPath(
                    dir_okay=False, exists=True, writable=False, resolve_path=True
                )
            elif info["type"] == "bool":
                option_name_fmt = "--%(key)s/--no-%(key)s"
                option_kwargs["is_flag"] = True
            else:
                option_kwargs["type"] = str

            option_name = option_name_fmt % {"key": k.replace("_", "-")}
            decorator = click.option(
                option_name,
                required=info["required"],
                help=info["help"],
                **option_kwargs
            )
            push_handler = decorator(push_handler)

        handlers[key] = push_handler

Response: Create a handler for uploading a new package from each of the package formats.


Instruction:
Given the following Python 3 function, write the documentation
def memoize(func):
    func._result_cache = {}  # pylint: disable-msg=W0212

    @wraps(func)
    def _memoized_func(*args, **kwargs):
        key = (args, tuple(sorted(kwargs.items())))
        if key in func._result_cache:  # pylint: disable-msg=W0212
            return func._result_cache[key]  # pylint: disable-msg=W0212
        else:
            result = func(*args, **kwargs)
            if isinstance(result, GeneratorType) or not isinstance(result, Hashable):
                raise TypeError("Can't memoize a generator or non-hashable object!")
            func._result_cache[key] = result  # pylint: disable-msg=W0212
            return result

    return _memoized_func

Response: Memoize a function to return the cached result on subsequent calls.


Instruction:
Create a Python 3 function to
clear the memoized method or memoized property results for the given method names from the given object.

Response:
def clear_memoized_methods(obj, *method_names):
    """
    Clear the memoized method or @memoizedproperty results for the given
    method names from the given object.

    >>> v = [0]
    >>> def inc():
    ...     v[0] += 1
    ...     return v[0]
    ...
    >>> class Foo(object):
    ...    @memoizemethod
    ...    def foo(self):
    ...        return inc()
    ...    @memoizedproperty
    ...    def g(self):
    ...       return inc()
    ...
    >>> f = Foo()
    >>> f.foo(), f.foo()
    (1, 1)
    >>> clear_memoized_methods(f, 'foo')
    >>> (f.foo(), f.foo(), f.g, f.g)
    (2, 2, 3, 3)
    >>> (f.foo(), f.foo(), f.g, f.g)
    (2, 2, 3, 3)
    >>> clear_memoized_methods(f, 'g', 'no_problem_if_undefined')
    >>> f.g, f.foo(), f.g
    (4, 2, 4)
    >>> f.foo()
    2
    """
    for key in list(getattr(obj, '_memoized_results', {}).keys()):
        # key[0] is the method name
        if key[0] in method_names:
            del obj._memoized_results[key]

    try:
        property_dict = obj._cache_
    except AttributeError:
        property_dict = obj._cache_ = {}

    for prop in method_names:
        inner_attname = '__%s' % prop
        if inner_attname in property_dict:
            del property_dict[inner_attname]


Instruction:
Make a summary of the following Python 3 code
def aes_encrypt(base64_encryption_key, data):
    if isinstance(data, text_type):
        data = data.encode("UTF-8")
    aes_key_bytes, hmac_key_bytes = _extract_keys(base64_encryption_key)
    data = _pad(data)
    iv_bytes = os.urandom(AES_BLOCK_SIZE)
    cipher = AES.new(aes_key_bytes, mode=AES.MODE_CBC, IV=iv_bytes)
    data = iv_bytes + cipher.encrypt(data)  # prepend init vector
    hmac_signature = hmac.new(hmac_key_bytes, data, hashlib.sha256).digest()
    return as_base64(data + hmac_signature)

Response: Encrypt data with AES - CBC and sign it with HMAC - SHA256


Instruction:
Explain what the following Python 3 code does
def aes_decrypt(base64_encryption_key, base64_data):
    data = from_base64(base64_data)
    aes_key_bytes, hmac_key_bytes = _extract_keys(base64_encryption_key)
    data, hmac_signature = data[:-HMAC_SIG_SIZE], data[-HMAC_SIG_SIZE:]
    if hmac.new(hmac_key_bytes, data, hashlib.sha256).digest() != hmac_signature:
        raise AuthenticationError("HMAC authentication failed")
    iv_bytes, data = data[:AES_BLOCK_SIZE], data[AES_BLOCK_SIZE:]
    cipher = AES.new(aes_key_bytes, AES.MODE_CBC, iv_bytes)
    data = cipher.decrypt(data)
    return _unpad(data)

Response: Verify HMAC - SHA256 signature and decrypt data with AES - CBC


Instruction:
Given the following Python 3 function, write the documentation
def whoami(ctx, opts):
    click.echo("Retrieving your authentication status from the API ... ", nl=False)

    context_msg = "Failed to retrieve your authentication status!"
    with handle_api_exceptions(ctx, opts=opts, context_msg=context_msg):
        with maybe_spinner(opts):
            is_auth, username, email, name = get_user_brief()

    click.secho("OK", fg="green")
    click.echo("You are authenticated as:")
    if not is_auth:
        click.secho("Nobody (i.e. anonymous user)", fg="yellow")
    else:
        click.secho(
            "%(name)s (slug: %(username)s, email: %(email)s)"
            % {
                "name": click.style(name, fg="cyan"),
                "username": click.style(username, fg="magenta"),
                "email": click.style(email, fg="green"),
            }
        )

Response: Retrieve your current authentication status.


Instruction:
Can you write a function in Python 3 where it
performs a standardized pipline of operations across an iterable.

Response:
def _clean(self, value):
		"""Perform a standardized pipline of operations across an iterable."""

		value = (str(v) for v in value)

		if self.strip:
			value = (v.strip() for v in value)

		if not self.empty:
			value = (v for v in value if v)

		return value


Instruction:
Can you write a function in Python 3 where it
converts the given string into a list of substrings.

Response:
def native(self, value, context=None):
		"""Convert the given string into a list of substrings."""

		separator = self.separator.strip() if self.strip and hasattr(self.separator, 'strip') else self.separator
		value = super().native(value, context)

		if value is None:
			return self.cast()

		if hasattr(value, 'split'):
			value = value.split(separator)

		value = self._clean(value)

		try:
			return self.cast(value) if self.cast else value
		except Exception as e:
			raise Concern("{0} caught, failed to perform array transform: {1}", e.__class__.__name__, str(e))


Instruction:
Can you create a Python 3 function that
constructs a string - like representation for an iterable of string - like objects.

Response:
def foreign(self, value, context=None):
		"""Construct a string-like representation for an iterable of string-like objects."""

		if self.separator is None:
			separator = ' '
		else:
			separator = self.separator.strip() if self.strip and hasattr(self.separator, 'strip') else self.separator

		value = self._clean(value)

		try:
			value = separator.join(value)
		except Exception as e:
			raise Concern("{0} caught, failed to convert to string: {1}", e.__class__.__name__, str(e))

		return super().foreign(value)


Instruction:
Can you tell what is the following Python 3 function doing
def tob32(val):
    ret = bytearray(4)
    ret[0] = (val>>24)&M8
    ret[1] = (val>>16)&M8
    ret[2] = (val>>8)&M8
    ret[3] = val&M8
    return ret

Response: Return provided 32 bit value as a string of four bytes.


Instruction:
Explain what the following Python 3 code does
def bintoihex(buf, spos=0x0000):
    c = 0
    olen = len(buf)
    ret = ""
    # 16 byte lines
    while (c+0x10) <= olen:
        adr = c + spos
        l = ':10{0:04X}00'.format(adr)
        sum = 0x10+((adr>>8)&M8)+(adr&M8)
        for j in range(0,0x10):
            nb = buf[c+j]
            l += '{0:02X}'.format(nb)
            sum = (sum + nb)&M8
        l += '{0:02X}'.format((~sum+1)&M8)
        ret += l + '\n'
        c += 0x10
    # remainder
    if c < olen:
        rem = olen-c
        sum = rem
        adr = c + spos
        l = ':{0:02X}{1:04X}00'.format(rem,adr)   # rem < 0x10
        sum += ((adr>>8)&M8)+(adr&M8)
        for j in range(0,rem):
            nb = buf[c+j]
            l += '{0:02X}'.format(nb)
            sum = (sum + nb)&M8
        l += '{0:02X}'.format((~sum+1)&M8)
        ret += l + '\n'
    ret += ':00000001FF\n'        # EOF
    return ret

Response: Convert binary buffer to ihex and return as string.


Instruction:
How would you explain what the following Python 3 function does
def op_gen(mcode):
    gen = op_tbl[mcode[0]]
    ret = gen[0]	# opcode
    nargs = len(gen)
    i = 1
    while i < nargs:
        if i < len(mcode):	# or assume they are same len
            ret |= (mcode[i]&gen[i][0]) << gen[i][1]
        i += 1
    return ret

Response: Generate a machine instruction using the op gen table.


Instruction:
How would you implement a function in Python 3 that
emits scan error and abort assembly.

Response:
def scanerror(self, msg):
        """Emit scan error and abort assembly."""
        error('scan error: ' + msg + ' on line {}'.format(self.sline))
        sys.exit(-1)


Instruction:
Write a Python 3 function for
emitting parse error and abort assembly.

Response:
def parseerror(self, msg, line=None):
        """Emit parse error and abort assembly."""
        if line is None:
            line = self.sline
        error('parse error: ' + msg + ' on line {}'.format(line))
        sys.exit(-2)


Instruction:
Can you implement a function in Python 3 that
creates payload for upload or check - upload operations.

Response:
def make_create_payload(**kwargs):
    """Create payload for upload/check-upload operations."""
    payload = {}
    # Add non-empty arguments
    for k, v in six.iteritems(kwargs):
        if v is not None:
            payload[k] = v

    return payload


Instruction:
Write a Python 3 function for
creating a new package in a repository.

Response:
def create_package(package_format, owner, repo, **kwargs):
    """Create a new package in a repository."""
    client = get_packages_api()

    with catch_raise_api_exception():
        upload = getattr(client, "packages_upload_%s_with_http_info" % package_format)

        data, _, headers = upload(
            owner=owner, repo=repo, data=make_create_payload(**kwargs)
        )

    ratelimits.maybe_rate_limit(client, headers)
    return data.slug_perm, data.slug


Instruction:
Explain what the following Python 3 code does
def validate_create_package(package_format, owner, repo, **kwargs):
    client = get_packages_api()

    with catch_raise_api_exception():
        check = getattr(
            client, "packages_validate_upload_%s_with_http_info" % package_format
        )

        _, _, headers = check(
            owner=owner, repo=repo, data=make_create_payload(**kwargs)
        )

    ratelimits.maybe_rate_limit(client, headers)
    return True

Response: Validate parameters for creating a package.


Instruction:
Can you tell what is the following Python 3 function doing
def copy_package(owner, repo, identifier, destination):
    client = get_packages_api()

    with catch_raise_api_exception():
        data, _, headers = client.packages_copy_with_http_info(
            owner=owner,
            repo=repo,
            identifier=identifier,
            data={"destination": destination},
        )

    ratelimits.maybe_rate_limit(client, headers)
    return data.slug_perm, data.slug

Response: Copy a package to another repository.


Instruction:
Can you implement a function in Python 3 that
moves a package to another repository.

Response:
def move_package(owner, repo, identifier, destination):
    """Move a package to another repository."""
    client = get_packages_api()

    with catch_raise_api_exception():
        data, _, headers = client.packages_move_with_http_info(
            owner=owner,
            repo=repo,
            identifier=identifier,
            data={"destination": destination},
        )

    ratelimits.maybe_rate_limit(client, headers)
    return data.slug_perm, data.slug


Instruction:
Write a Python 3 script to
delete a package in a repository.

Response:
def delete_package(owner, repo, identifier):
    """Delete a package in a repository."""
    client = get_packages_api()

    with catch_raise_api_exception():
        _, _, headers = client.packages_delete_with_http_info(
            owner=owner, repo=repo, identifier=identifier
        )

    ratelimits.maybe_rate_limit(client, headers)
    return True