Instruction:
How would you implement a function in Python 3 that
searches within the resources shared to the application.

Response:
def search(
        self,
        token: dict = None,
        query: str = "",
        bbox: list = None,
        poly: str = None,
        georel: str = None,
        order_by: str = "_created",
        order_dir: str = "desc",
        page_size: int = 100,
        offset: int = 0,
        share: str = None,
        specific_md: list = [],
        include: list = [],
        whole_share: bool = True,
        check: bool = True,
        augment: bool = False,
        tags_as_dicts: bool = False,
        prot: str = "https",
    ) -> dict:
        """Search within the resources shared to the application.

        It's the main method to use.

        :param str token: API auth token - DEPRECATED: token is now automatically included
        :param str query: search terms and semantic filters. Equivalent of
         **q** parameter in Isogeo API. It could be a simple
         string like *oil* or a tag like *keyword:isogeo:formations*
         or *keyword:inspire-theme:landcover*. The *AND* operator
         is applied when various tags are passed.
        :param list bbox: Bounding box to limit the search.
         Must be a 4 list of coordinates in WGS84 (EPSG 4326).
         Could be associated with *georel*.
        :param str poly: Geographic criteria for the search, in WKT format.
         Could be associated with *georel*.
        :param str georel: geometric operator to apply to the bbox or poly
         parameters.

         Available values (see: *isogeo.GEORELATIONS*):

          * 'contains',
          * 'disjoint',
          * 'equals',
          * 'intersects' - [APPLIED BY API if NOT SPECIFIED]
          * 'overlaps',
          * 'within'.

        :param str order_by: sorting results.

         Available values:

          * '_created': metadata creation date [DEFAULT if relevance is null]
          * '_modified': metadata last update
          * 'title': metadata title
          * 'created': data creation date (possibly None)
          * 'modified': data last update date
          * 'relevance': relevance score calculated by API [DEFAULT].

        :param str order_dir: sorting direction.

         Available values:
          * 'desc': descending
          * 'asc': ascending

        :param int page_size: limits the number of results.
         Useful to paginate results display. Default value: 100.
        :param int offset: offset to start page size
         from a specific results index
        :param str share: share UUID to filter on
        :param list specific_md: list of metadata UUIDs to filter on
        :param list include: subresources that should be returned.
         Must be a list of strings. Available values: *isogeo.SUBRESOURCES*
        :param bool whole_share: option to return all results or only the
         page size. *True* by DEFAULT.
        :param bool check: option to check query parameters and avoid erros.
         *True* by DEFAULT.
        :param bool augment: option to improve API response by adding
         some tags on the fly (like shares_id)
        :param bool tags_as_dicts: option to store tags as key/values by filter.
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # specific resources specific parsing
        specific_md = checker._check_filter_specific_md(specific_md)

        # sub resources specific parsing
        include = checker._check_filter_includes(include)

        # handling request parameters
        payload = {
            "_id": specific_md,
            "_include": include,
            "_lang": self.lang,
            "_limit": page_size,
            "_offset": offset,
            "box": bbox,
            "geo": poly,
            "rel": georel,
            "ob": order_by,
            "od": order_dir,
            "q": query,
            "s": share,
        }

        if check:
            checker.check_request_parameters(payload)
        else:
            pass

        # search request
        search_url = "{}://v1.{}.isogeo.com/resources/search".format(prot, self.api_url)
        try:
            search_req = self.get(
                search_url,
                headers=self.header,
                params=payload,
                proxies=self.proxies,
                verify=self.ssl,
            )
        except Exception as e:
            logging.error(e)
            raise Exception

        # fast response check
        checker.check_api_response(search_req)

        # serializing result into dict and storing resources in variables
        search_rez = search_req.json()
        resources_count = search_rez.get("total")  # total of metadatas shared

        # handling Isogeo API pagination
        # see: http://help.isogeo.com/api/fr/methods/pagination.html
        if resources_count > page_size and whole_share:
            # if API returned more than one page of results, let's get the rest!
            metadatas = []  # a recipient list
            payload["_limit"] = 100  # now it'll get pages of 100 resources
            # let's parse pages
            for idx in range(0, int(ceil(resources_count / 100)) + 1):
                payload["_offset"] = idx * 100
                search_req = self.get(
                    search_url,
                    headers=self.header,
                    params=payload,
                    proxies=self.proxies,
                    verify=self.ssl,
                )
                # storing results by addition
                metadatas.extend(search_req.json().get("results"))
            search_rez["results"] = metadatas
        else:
            pass

        # add shares to tags and query
        if augment:
            self.add_tags_shares(search_rez.get("tags"))
            if share:
                search_rez.get("query")["_shares"] = [share]
            else:
                search_rez.get("query")["_shares"] = []
        else:
            pass

        # store tags in dicts
        if tags_as_dicts:
            new_tags = utils.tags_to_dict(
                tags=search_rez.get("tags"), prev_query=search_rez.get("query")
            )
            # clear
            search_rez.get("tags").clear()
            search_rez.get("query").clear()
            # update
            search_rez.get("tags").update(new_tags[0])
            search_rez.get("query").update(new_tags[1])
        else:
            pass
        # end of method
        return search_rez


Instruction:
How would you code a function in Python 3 to
get complete or partial metadata about one specific resource.

Response:
def resource(
        self,
        token: dict = None,
        id_resource: str = None,
        subresource=None,
        include: list = [],
        prot: str = "https",
    ) -> dict:
        """Get complete or partial metadata about one specific resource.

        :param str token: API auth token
        :param str id_resource: metadata UUID to get
        :param list include: subresources that should be included.
         Must be a list of strings. Available values: 'isogeo.SUBRESOURCES'
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # if subresource route
        if isinstance(subresource, str):
            subresource = "/{}".format(checker._check_subresource(subresource))
        else:
            subresource = ""
            # _includes specific parsing
            include = checker._check_filter_includes(include)

        # handling request parameters
        payload = {"id": id_resource, "_include": include}
        # resource search
        md_url = "{}://v1.{}.isogeo.com/resources/{}{}".format(
            prot, self.api_url, id_resource, subresource
        )
        resource_req = self.get(
            md_url,
            headers=self.header,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )
        checker.check_api_response(resource_req)

        # end of method
        return resource_req.json()


Instruction:
Can you create a Python 3 function that
gets information about the application s shares.

Response:
def shares(self, token: dict = None, prot: str = "https") -> dict:
        """Get information about shares which feed the application.

        :param str token: API auth token
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # passing auth parameter
        shares_url = "{}://v1.{}.isogeo.com/shares/".format(prot, self.api_url)
        shares_req = self.get(
            shares_url, headers=self.header, proxies=self.proxies, verify=self.ssl
        )

        # checking response
        checker.check_api_response(shares_req)

        # end of method
        return shares_req.json()


Instruction:
Create a Python 3 function to
get information about a specific share and its applications.

Response:
def share(
        self,
        share_id: str,
        token: dict = None,
        augment: bool = False,
        prot: str = "https",
    ) -> dict:
        """Get information about a specific share and its applications.

        :param str token: API auth token
        :param str share_id: share UUID
        :param bool augment: option to improve API response by adding
         some tags on the fly.
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # passing auth parameter
        share_url = "{}://v1.{}.isogeo.com/shares/{}".format(
            prot, self.api_url, share_id
        )
        share_req = self.get(
            share_url, headers=self.header, proxies=self.proxies, verify=self.ssl
        )

        # checking response
        checker.check_api_response(share_req)

        # enhance share model
        share = share_req.json()
        if augment:
            share = utils.share_extender(
                share, self.search(whole_share=1, share=share_id).get("results")
            )
        else:
            pass

        # end of method
        return share


Instruction:
Can you tell what is the following Python 3 function doing
def licenses(
        self, token: dict = None, owner_id: str = None, prot: str = "https"
    ) -> dict:
        """Get information about licenses owned by a specific workgroup.

        :param str token: API auth token
        :param str owner_id: workgroup UUID
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # handling request parameters
        payload = {"gid": owner_id}

        # search request
        licenses_url = "{}://v1.{}.isogeo.com/groups/{}/licenses".format(
            prot, self.api_url, owner_id
        )
        licenses_req = self.get(
            licenses_url,
            headers=self.header,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )

        # checking response
        req_check = checker.check_api_response(licenses_req)
        if isinstance(req_check, tuple):
            return req_check

        # end of method
        return licenses_req.json()

Response: Get information about licenses owned by a specific workgroup.


Instruction:
Can you generate the documentation for the following Python 3 function
def license(self, license_id: str, token: dict = None, prot: str = "https") -> dict:
        # handling request parameters
        payload = {"lid": license_id}

        # search request
        license_url = "{}://v1.{}.isogeo.com/licenses/{}".format(
            prot, self.api_url, license_id
        )
        license_req = self.get(
            license_url,
            headers=self.header,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )

        # checking response
        checker.check_api_response(license_req)

        # end of method
        return license_req.json()

Response: Get details about a specific license.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def thesauri(self, token: dict = None, prot: str = "https") -> dict:
        # passing auth parameter
        thez_url = "{}://v1.{}.isogeo.com/thesauri".format(prot, self.api_url)
        thez_req = self.get(
            thez_url, headers=self.header, proxies=self.proxies, verify=self.ssl
        )

        # checking response
        checker.check_api_response(thez_req)

        # end of method
        return thez_req.json()

Response: Get list of available thesauri.


Instruction:
Can you create a Python 3 function that
gets a thesaurus. :param str token: API auth token :param str thez_id: thesaurus UUID :param str prot: https [DEFAULT] or http (use it only for dev and tracking needs).

Response:
def thesaurus(
        self,
        token: dict = None,
        thez_id: str = "1616597fbc4348c8b11ef9d59cf594c8",
        prot: str = "https",
    ) -> dict:
        """Get a thesaurus.

        :param str token: API auth token
        :param str thez_id: thesaurus UUID
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # handling request parameters
        payload = {"tid": thez_id}

        # passing auth parameter
        thez_url = "{}://v1.{}.isogeo.com/thesauri/{}".format(
            prot, self.api_url, thez_id
        )
        thez_req = self.get(
            thez_url,
            headers=self.header,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )

        # checking response
        checker.check_api_response(thez_req)

        # end of method
        return thez_req.json()


Instruction:
Can you create a Python 3 function that
searches for keywords within a specific thesaurus.

Response:
def keywords(
        self,
        token: dict = None,
        thez_id: str = "1616597fbc4348c8b11ef9d59cf594c8",
        query: str = "",
        offset: int = 0,
        order_by: str = "text",
        order_dir: str = "desc",
        page_size: int = 20,
        specific_md: list = [],
        specific_tag: list = [],
        include: list = [],
        prot: str = "https",
    ) -> dict:
        """Search for keywords within a specific thesaurus.

        :param str token: API auth token
        :param str thez_id: thesaurus UUID
        :param str query: search terms
        :param int offset: pagination start
        :param str order_by: sort criteria. Available values :

            - count.group,
            - count.isogeo,
            - text

        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # specific resources specific parsing
        specific_md = checker._check_filter_specific_md(specific_md)
        # sub resources specific parsing
        include = checker._check_filter_includes(include, "keyword")
        # specific tag specific parsing
        specific_tag = checker._check_filter_specific_tag(specific_tag)

        # handling request parameters
        payload = {
            "_id": specific_md,
            "_include": include,
            "_limit": page_size,
            "_offset": offset,
            "_tag": specific_tag,
            "tid": thez_id,
            "ob": order_by,
            "od": order_dir,
            "q": query,
        }

        # search request
        keywords_url = "{}://v1.{}.isogeo.com/thesauri/{}/keywords/search".format(
            prot, self.api_url, thez_id
        )

        kwds_req = self.get(
            keywords_url,
            headers=self.header,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )

        # checking response
        checker.check_api_response(kwds_req)

        # end of method
        return kwds_req.json()


Instruction:
Implement a function in Python 3 to
download hosted resource. :param str token: API auth token :param dict resource_link: link dictionary :param bool encode_clean: option to ensure a clean filename and avoid OS errors :param str proxy_url: proxy to use to download :param str prot: https [DEFAULT] or http (use it only for dev and tracking needs). Example of resource_link dict: .. code-block:: json { "_id": "g8h9i0j11k12l13m14n15o16p17Q18rS", "type": "hosted", "title": "label_of_hosted_file.zip", "url": "/resources/1a2b3c4d5e6f7g8h9i0j11k12l13m14n/links/g8h9i0j11k12l13m14n15o16p17Q18rS.bin", "kind": "data", "actions": ["download", ], "size": "2253029", }

Response:
def dl_hosted(
        self,
        token: dict = None,
        resource_link: dict = None,
        encode_clean: bool = 1,
        proxy_url: str = None,
        prot: str = "https",
    ) -> tuple:
        """Download hosted resource.

        :param str token: API auth token
        :param dict resource_link: link dictionary
        :param bool encode_clean: option to ensure a clean filename and avoid OS errors
        :param str proxy_url: proxy to use to download
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).

        Example of resource_link dict:

        .. code-block:: json

            {
            "_id": "g8h9i0j11k12l13m14n15o16p17Q18rS",
            "type": "hosted",
            "title": "label_of_hosted_file.zip",
            "url": "/resources/1a2b3c4d5e6f7g8h9i0j11k12l13m14n/links/g8h9i0j11k12l13m14n15o16p17Q18rS.bin",
            "kind": "data",
            "actions": ["download", ],
            "size": "2253029",
            }

        """
        # check resource link parameter type
        if not isinstance(resource_link, dict):
            raise TypeError("Resource link expects a dictionary.")
        else:
            pass
        # check resource link type
        if not resource_link.get("type") == "hosted":
            raise ValueError(
                "Resource link passed is not a hosted one: {}".format(
                    resource_link.get("type")
                )
            )
        else:
            pass

        # handling request parameters
        payload = {"proxyUrl": proxy_url}

        # prepare URL request
        hosted_url = "{}://v1.{}.isogeo.com/{}".format(
            prot, self.api_url, resource_link.get("url")
        )

        # send stream request
        hosted_req = self.get(
            hosted_url,
            headers=self.header,
            stream=True,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )
        # quick check
        req_check = checker.check_api_response(hosted_req)
        if not req_check:
            raise ConnectionError(req_check[1])
        else:
            pass

        # get filename from header
        content_disposition = hosted_req.headers.get("Content-Disposition")
        if content_disposition:
            filename = re.findall("filename=(.+)", content_disposition)[0]
        else:
            filename = resource_link.get("title")

        # remove special characters
        if encode_clean:
            filename = utils.encoded_words_to_text(filename)
            filename = re.sub(r"[^\w\-_\. ]", "", filename)

        # well-formed size
        in_size = resource_link.get("size")
        for size_cat in ("octets", "Ko", "Mo", "Go"):
            if in_size < 1024.0:
                out_size = "%3.1f %s" % (in_size, size_cat)
            in_size /= 1024.0

        out_size = "%3.1f %s" % (in_size, " To")

        # end of method
        return (hosted_req, filename, out_size)


Instruction:
Implement a function in Python 3 to
get resource exported into XML ISO 19139.

Response:
def xml19139(
        self,
        token: dict = None,
        id_resource: str = None,
        proxy_url=None,
        prot: str = "https",
    ):
        """Get resource exported into XML ISO 19139.

        :param str token: API auth token
        :param str id_resource: metadata UUID to export
        :param str proxy_url: proxy to use to download
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # check metadata UUID
        if not checker.check_is_uuid(id_resource):
            raise ValueError("Metadata ID is not a correct UUID.")
        else:
            pass

        # handling request parameters
        payload = {"proxyUrl": proxy_url, "id": id_resource}

        # resource search
        md_url = "{}://v1.{}.isogeo.com/resources/{}.xml".format(
            prot, self.api_url, id_resource
        )
        xml_req = self.get(
            md_url,
            headers=self.header,
            stream=True,
            params=payload,
            proxies=self.proxies,
            verify=self.ssl,
        )

        # end of method
        return xml_req


Instruction:
Write a Python 3 function for
adding shares list to the tags dictionary in search results.

Response:
def add_tags_shares(self, tags: dict = dict()):
        """Add shares list to the tags attributes in search results.

        :param dict tags: tags dictionary from a search request
        """
        # check if shares_id have already been retrieved or not
        if not hasattr(self, "shares_id"):
            shares = self.shares()
            self.shares_id = {
                "share:{}".format(i.get("_id")): i.get("name") for i in shares
            }
        else:
            pass
        # update query tags
        tags.update(self.shares_id)


Instruction:
Can you write a function in Python 3 where it
gets information about the application declared on Isogeo.

Response:
def get_app_properties(self, token: dict = None, prot: str = "https"):
        """Get information about the application declared on Isogeo.

        :param str token: API auth token
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # check if app properties have already been retrieved or not
        if not hasattr(self, "app_properties"):
            first_app = self.shares()[0].get("applications")[0]
            app = {
                "admin_url": "{}/applications/{}".format(
                    self.mng_url, first_app.get("_id")
                ),
                "creation_date": first_app.get("_created"),
                "last_update": first_app.get("_modified"),
                "name": first_app.get("name"),
                "type": first_app.get("type"),
                "kind": first_app.get("kind"),
                "url": first_app.get("url"),
            }
            self.app_properties = app
        else:
            pass


Instruction:
Write a Python 3 script for
getting environment directives which represent INSPIRE limitations.

Response:
def get_directives(self, token: dict = None, prot: str = "https") -> dict:
        """Get environment directives which represent INSPIRE limitations.

        :param str token: API auth token
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # search request
        req_url = "{}://v1.{}.isogeo.com/directives".format(prot, self.api_url)

        req = self.get(
            req_url, headers=self.header, proxies=self.proxies, verify=self.ssl
        )

        # checking response
        checker.check_api_response(req)

        # end of method
        return req.json()


Instruction:
Here you have a function in Python 3, explain what it does
def get_coordinate_systems(
        self, token: dict = None, srs_code: str = None, prot: str = "https"
    ) -> dict:
        """Get available coordinate systems in Isogeo API.

        :param str token: API auth token
        :param str srs_code: code of a specific coordinate system
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # if specific format
        if isinstance(srs_code, str):
            specific_srs = "/{}".format(srs_code)
        else:
            specific_srs = ""

        # search request
        req_url = "{}://v1.{}.isogeo.com/coordinate-systems{}".format(
            prot, self.api_url, specific_srs
        )

        req = self.get(
            req_url, headers=self.header, proxies=self.proxies, verify=self.ssl
        )

        # checking response
        checker.check_api_response(req)

        # end of method
        return req.json()

Response: Get available coordinate systems in Isogeo API.


Instruction:
Create a Python 3 function for
getting the formats for a specific language.

Response:
def get_formats(
        self, token: dict = None, format_code: str = None, prot: str = "https"
    ) -> dict:
        """Get formats.

        :param str token: API auth token
        :param str format_code: code of a specific format
        :param str prot: https [DEFAULT] or http
         (use it only for dev and tracking needs).
        """
        # if specific format
        if isinstance(format_code, str):
            specific_format = "/{}".format(format_code)
        else:
            specific_format = ""

        # search request
        req_url = "{}://v1.{}.isogeo.com/formats{}".format(
            prot, self.api_url, specific_format
        )

        req = self.get(
            req_url, headers=self.header, proxies=self.proxies, verify=self.ssl
        )

        # checking response
        checker.check_api_response(req)

        # end of method
        return req.json()


Instruction:
Create a Python 3 function for
parsing a boolean value from the input string and returns the corresponding value.

Response:
def parse_bool(v, default=None, exceptions: bool=True) -> bool:
    """
    Parses boolean value
    :param v: Input string
    :param default: Default value if exceptions=False
    :param exceptions: Raise exception on error or not
    :return: bool
    """
    if isinstance(v, bool):
        return v
    s = str(v).lower()
    if s in TRUE_VALUES:
        return True
    elif s in FALSE_VALUES:
        return False
    else:
        if exceptions:
            raise ValidationError('Failed to parse boolean from "{}"'.format(v))
        return default


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_datetime(v, default=None, tz=None, exceptions: bool=True) -> datetime:
    try:
        t = dateutil_parse(v, default=datetime(2000, 1, 1))
        if tz is None:
            tz = pytz.utc
        return t if t.tzinfo else tz.localize(t)
    except Exception:
        if exceptions:
            raise ValidationError('Failed to parse datetime from "{}"'.format(v))
        return default

Response: Parses a datetime from a string into a datetime object.


Instruction:
Given the following Python 3 function, write the documentation
def convert_boolean(string_value):

    lean_string_value = string_value.strip().lower()
    if lean_string_value in ['yes', 'true', 'on', '1']:
        return True
    elif lean_string_value in ['no', 'false', 'off', '0']:
        return False

    # Not recognised boolean if we get here
    raise ValueError('Unrecognised boolean ({})'.format(lean_string_value))

Response: Converts a string to a boolean.


Instruction:
Explain what the following Python 3 code does
def _handle_hdr(self, hdr):

        column_number = 1
        for cell in hdr:
            cell_parts = cell.split(self._type_sep)
            if len(cell_parts) not in [1, 2]:
                raise ContentError(column_number, self._c_reader.line_num,
                                   cell, 'Expected name and type (up to 2 items)')
            name = cell_parts[0].strip()
            if len(name) == 0:
                raise ContentError(column_number, self._c_reader.line_num,
                                   cell, 'Column name is empty')
            if name in self._column_names:
                raise ContentError(column_number, self._c_reader.line_num,
                                   name, 'Duplicate column name')

            if len(cell_parts) == 2:
                column_type = cell_parts[1].strip().lower()
                if column_type not in CONVERTERS:
                    raise UnknownTypeError(column_number, column_type)
            else:
                # Unspecified - assume built-in 'string'
                column_type = 'string'
            self._converters.append([name, CONVERTERS[column_type]])
            self._column_names.append(name)
            column_number += 1

Response: Handles the header line.


Instruction:
Write a Python 3 script for
searching datasets within uData portal.

Response:
def search_datasets(
        self,
        license=None,
        format=None,
        query=None,
        featured=None,
        owner=None,
        organization=None,
        badge=None,
        reuses=None,
        page_size=20,
        x_fields=None,
    ):
        """Search datasets within uData portal."""
        # handling request parameters
        payload = {"badge": badge, "size": page_size, "X-Fields": x_fields}

        # search request
        # head = {"X-API-KEY": self.api_key}
        search_url = "{}/datasets".format(
            self.base_url,
            # org_id,
            # page_size
        )

        search_req = requests.get(
            search_url,
            # headers=head,
            params=payload,
        )

        # serializing result into dict and storing resources in variables
        logger.debug(search_req.url)
        return search_req.json()


Instruction:
Here you have a function in Python 3, explain what it does
def get_filters_values(self):
        # DATASETS --
        # badges
        self._DST_BADGES = requests.get(self.base_url + "datasets/badges/").json()
        # licences
        self._DST_LICENSES = {
            l.get("id"): l.get("title")
            for l in requests.get(self.base_url + "datasets/licenses").json()
        }
        # frequencies
        self._DST_FREQUENCIES = {
            f.get("id"): f.get("label")
            for f in requests.get(self.base_url + "datasets/frequencies").json()
        }
        # ORGANIZATIONS --
        # badges
        self._ORG_BADGES = requests.get(self.base_url + "organizations/badges/").json()
        # # licences
        # self._DST_LICENSES = {l.get("id"): l.get("title")
        #                   for l in requests.get(self.base_url + "datasets/licenses").json()}
        # # frequencies
        # self._DST_FREQUENCIES = {f.get("id"): f.get("label")
        #                      for f in requests.get(self.base_url + "datasets/frequencies").json()}
        # SPATIAL --
        # granularities
        self._GRANULARITIES = {
            g.get("id"): g.get("name")
            for g in requests.get(self.base_url + "spatial/granularities").json()
        }
        # levels
        self._LEVELS = {
            g.get("id"): g.get("name")
            for g in requests.get(self.base_url + "spatial/levels").json()
        }
        # MISC --
        # facets
        self._FACETS = (
            "all",
            "badge",
            "featured",
            "format",
            "geozone",
            "granularity",
            "license",
            "owner",
            "organization",
            "reuses",
            "tag",
            "temporal_coverage",
        )
        # reuses
        self._REUSES = ("none", "few", "quite", "many")

Response: Get different filters values as dicts.


Instruction:
Can you write a function in Python 3 where it
deploys the app to AppEngine.

Response:
def deploy(app_id, version, promote, quiet):
    # type: (str, str, bool, bool) -> None
    """ Deploy the app to AppEngine.

    Args:
        app_id (str):
            AppEngine App ID. Overrides config value app_id if given.
        version (str):
            AppEngine project version. Overrides config values if given.
        promote (bool):
            If set to **True** promote the current remote app version to the one
            that's being deployed.
        quiet (bool):
            If set to **True** this will pass the ``--quiet`` flag to gcloud
            command.
    """
    gae_app = GaeApp.for_branch(git.current_branch().name)

    if gae_app is None and None in (app_id,  version):
        msg = (
            "Can't find an AppEngine app setup for branch <35>{}<32> and"
            "--project and --version were not given."
        )
        log.err(msg, git.current_branch().name)
        sys.exit(1)

    if version is not None:
        gae_app.version = version

    if app_id is not None:
        gae_app.app_id = app_id

    gae_app.deploy(promote, quiet)


Instruction:
Given the following Python 3 function, write the documentation
def devserver(port, admin_port, clear):
    # type: (int, int, bool) -> None
    """ Run devserver.

    Args:
        port (int):
            Port on which the app will be served.
        admin_port (int):
            Port on which the admin interface is served.
        clear (bool):
            If set to **True**, clear the datastore on startup.
    """
    admin_port = admin_port or (port + 1)

    args = [
        '--port={}'.format(port),
        '--admin_port={}'.format(admin_port)
    ]

    if clear:
        args += ['--clear_datastore=yes']

    with conf.within_proj_dir():
        shell.run('dev_appserver.py . {args}'.format(args=' '.join(args)))

Response: Run devserver on startup.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def setup_ci():
    # type: () -> None
    """ Setup AppEngine SDK on CircleCI """
    gcloud_path = shell.run('which gcloud', capture=True).stdout.strip()
    sdk_path = normpath(join(gcloud_path, '../../platform/google_appengine'))
    gcloud_cmd = gcloud_path + ' --quiet'

    if not exists(sdk_path):
        log.info("Installing AppEngine SDK")
        shell.run('sudo {} components install app-engine-python'.format(
            gcloud_cmd
        ))
    else:
        # Only initialise once. To reinitialise, just build without cache.
        log.info("AppEngine SDK already initialised")

    log.info("Using service account authentication")
    shell.run('{} auth activate-service-account --key-file {}'.format(
        gcloud_cmd,
        conf.proj_path('ops/client_secret.json')
    ))

Response: Setup AppEngine SDK on CircleCI


Instruction:
Implement a function in Python 3 to
mark function as experimental.

Response:
def mark_experimental(fn):
    # type: (FunctionType) -> FunctionType
    """ Mark function as experimental.

    Args:
        fn (FunctionType):
            The command function to decorate.
    """
    @wraps(fn)
    def wrapper(*args, **kw):   # pylint: disable=missing-docstring
        from peltak.core import shell

        if shell.is_tty:
            warnings.warn("This command is has experimental status. The "
                          "interface is not yet stable and might change "
                          "without notice within with a patch version update. "
                          "Use at your own risk")
        return fn(*args, **kw)

    return wrapper


Instruction:
Write a Python 3 function for
marking a function as deprecated.

Response:
def mark_deprecated(replaced_by):
    # type: (Text) -> FunctionType
    """ Mark command as deprecated.

    Args:
        replaced_by (str):
            The command that deprecated this command and should be used instead.
    """
    def decorator(fn):   # pylint: disable=missing-docstring
        @wraps(fn)
        def wrapper(*args, **kw):   # pylint: disable=missing-docstring
            from peltak.core import shell

            if shell.is_tty:
                warnings.warn("This command is has been deprecated. Please use "
                              "{new} instead.".format(new=replaced_by))

            return fn(*args, **kw)

        return wrapper

    return decorator


Instruction:
Create a Python 3 function to
split the given iterable into batches of size batch_size.

Response:
def in_batches(iterable, batch_size):
    # type: (Iterable[Any]) -> Generator[List[Any]]
    """ Split the given iterable into batches.

    Args:
        iterable (Iterable[Any]):
            The iterable you want to split into batches.
        batch_size (int):
            The size of each bach. The last batch will be probably smaller (if
            the number of elements cannot be equally divided.

    Returns:
        Generator[list[Any]]: Will yield all items in batches of **batch_size**
            size.

    Example:

        >>> from peltak.core import util
        >>>
        >>> batches = util.in_batches([1, 2, 3, 4, 5, 6, 7], 3)
        >>> batches = list(batches)     # so we can query for lenght
        >>> len(batches)
        3
        >>> batches
        [[1, 2, 3], [4, 5, 6], [7]]

    """
    items = list(iterable)
    size = len(items)

    for i in range(0, size, batch_size):
        yield items[i:min(i + batch_size, size)]


Instruction:
Make a summary of the following Python 3 code
def yaml_dump(data, stream=None):
    # type: (YamlData, Optional[TextIO]) -> Text
    """ Dump data to a YAML string/file.

    Args:
        data (YamlData):
            The data to serialize as YAML.
        stream (TextIO):
            The file-like object to save to. If given, this function will write
            the resulting YAML to that stream.

    Returns:
        str: The YAML string.
    """
    return yaml.dump(
        data,
        stream=stream,
        Dumper=Dumper,
        default_flow_style=False
    )

Response: Dump data to a YAML string or file - like object.


Instruction:
Explain what the following Python 3 code does
def clear(cls, fn):
        # type: (FunctionType) -> None
        """ Clear result cache on the given function.

        If the function has no cached result, this call will do nothing.

        Args:
            fn (FunctionType):
                The function whose cache should be cleared.
        """
        if hasattr(fn, cls.CACHE_VAR):
            delattr(fn, cls.CACHE_VAR)

Response: Clears the result cache on the given function.


Instruction:
Write a Python 3 script to
start working on a new feature by branching off develop.

Response:
def start(name):
    # type: (str) -> None
    """ Start working on a new feature by branching off develop.

    This will create a new branch off develop called feature/<name>.

    Args:
        name (str):
            The name of the new feature.
    """
    branch = git.current_branch(refresh=True)
    task_branch = 'task/' + common.to_branch_name(name)

    if branch.type not in ('feature', 'hotfix'):
        log.err("Task branches can only branch off <33>feature<32> or "
                "<33>hotfix<32> branches")
        sys.exit(1)

    common.git_checkout(task_branch, create=True)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update():
    # type: () -> None
    """ Update the feature with updates committed to develop.

    This will merge current develop into the current branch.
    """
    branch = git.current_branch(refresh=True)
    base_branch = common.get_base_branch()

    common.assert_branch_type('task')
    common.git_checkout(base_branch)
    common.git_pull(base_branch)
    common.git_checkout(branch.name)
    common.git_merge(branch.name, base_branch)

Response: Update the feature with updates committed to develop.


Instruction:
Explain what the following Python 3 code does
def finish():
    # type: () -> None
    """ Merge current feature branch into develop. """
    pretend = context.get('pretend', False)

    if not pretend and (git.staged() or git.unstaged()):
        log.err(
            "You have uncommitted changes in your repo!\n"
            "You need to stash them before you merge the hotfix branch"
        )
        sys.exit(1)

    branch = git.current_branch(refresh=True)
    base = common.get_base_branch()

    prompt = "<32>Merge <33>{}<32> into <33>{}<0>?".format(branch.name, base)
    if not click.confirm(shell.fmt(prompt)):
        log.info("Cancelled")
        return

    common.assert_branch_type('task')

    # Merge task into it's base feature branch
    common.git_checkout(base)
    common.git_pull(base)
    common.git_merge(base, branch.name)

    # Cleanup
    common.git_branch_delete(branch.name)
    common.git_prune()

    common.git_checkout(base)

Response: Merge current feature branch into develop.


Instruction:
Explain what the following Python 3 code does
def merged():
    # type: () -> None
    """ Cleanup a remotely merged branch. """
    base_branch = common.get_base_branch()
    branch = git.current_branch(refresh=True)

    common.assert_branch_type('task')

    # Pull feature branch with the merged task
    common.git_checkout(base_branch)
    common.git_pull(base_branch)

    # Cleanup
    common.git_branch_delete(branch.name)
    common.git_prune()

    common.git_checkout(base_branch)

Response: Cleanup a remotely merged task.


Instruction:
Write a Python 3 script for
calling a Mutagen function with appropriate error handling.

Response:
def mutagen_call(action, path, func, *args, **kwargs):
    """Call a Mutagen function with appropriate error handling.

    `action` is a string describing what the function is trying to do,
    and `path` is the relevant filename. The rest of the arguments
    describe the callable to invoke.

    We require at least Mutagen 1.33, where `IOError` is *never* used,
    neither for internal parsing errors *nor* for ordinary IO error
    conditions such as a bad filename. Mutagen-specific parsing errors and IO
    errors are reraised as `UnreadableFileError`. Other exceptions
    raised inside Mutagen---i.e., bugs---are reraised as `MutagenError`.
    """
    try:
        return func(*args, **kwargs)
    except mutagen.MutagenError as exc:
        log.debug(u'%s failed: %s', action, six.text_type(exc))
        raise UnreadableFileError(path, six.text_type(exc))
    except Exception as exc:
        # Isolate bugs in Mutagen.
        log.debug(u'%s', traceback.format_exc())
        log.error(u'uncaught Mutagen exception in %s: %s', action, exc)
        raise MutagenError(path, exc)


Instruction:
Given the following Python 3 function, write the documentation
def _safe_cast(out_type, val):
    if val is None:
        return None

    if out_type == int:
        if isinstance(val, int) or isinstance(val, float):
            # Just a number.
            return int(val)
        else:
            # Process any other type as a string.
            if isinstance(val, bytes):
                val = val.decode('utf-8', 'ignore')
            elif not isinstance(val, six.string_types):
                val = six.text_type(val)
            # Get a number from the front of the string.
            match = re.match(r'[\+-]?[0-9]+', val.strip())
            return int(match.group(0)) if match else 0

    elif out_type == bool:
        try:
            # Should work for strings, bools, ints:
            return bool(int(val))
        except ValueError:
            return False

    elif out_type == six.text_type:
        if isinstance(val, bytes):
            return val.decode('utf-8', 'ignore')
        elif isinstance(val, six.text_type):
            return val
        else:
            return six.text_type(val)

    elif out_type == float:
        if isinstance(val, int) or isinstance(val, float):
            return float(val)
        else:
            if isinstance(val, bytes):
                val = val.decode('utf-8', 'ignore')
            else:
                val = six.text_type(val)
            match = re.match(r'[\+-]?([0-9]+\.?[0-9]*|[0-9]*\.[0-9]+)',
                             val.strip())
            if match:
                val = match.group(0)
                if val:
                    return float(val)
            return 0.0

    else:
        return val

Response: Try to covert val to out_type but never raise an exception.


Instruction:
Create a Python 3 function for
unpacking image data from a WM / Picture tag. Return a tuple containing the MIME type the raw image data a type indicator and the image s description.

Response:
def _unpack_asf_image(data):
    """Unpack image data from a WM/Picture tag. Return a tuple
    containing the MIME type, the raw image data, a type indicator, and
    the image's description.

    This function is treated as "untrusted" and could throw all manner
    of exceptions (out-of-bounds, etc.). We should clean this up
    sometime so that the failure modes are well-defined.
    """
    type, size = struct.unpack_from('<bi', data)
    pos = 5
    mime = b''
    while data[pos:pos + 2] != b'\x00\x00':
        mime += data[pos:pos + 2]
        pos += 2
    pos += 2
    description = b''
    while data[pos:pos + 2] != b'\x00\x00':
        description += data[pos:pos + 2]
        pos += 2
    pos += 2
    image_data = data[pos:pos + size]
    return (mime.decode("utf-16-le"), image_data, type,
            description.decode("utf-16-le"))


Instruction:
Can you write a function in Python 3 where it
packs image data for a WM / Picture tag.

Response:
def _pack_asf_image(mime, data, type=3, description=""):
    """Pack image data for a WM/Picture tag.
    """
    tag_data = struct.pack('<bi', type, len(data))
    tag_data += mime.encode("utf-16-le") + b'\x00\x00'
    tag_data += description.encode("utf-16-le") + b'\x00\x00'
    tag_data += data
    return tag_data


Instruction:
Can you create a Python 3 function that
converts a Sound Check bytestring value to a tuple of gain and peak.

Response:
def _sc_decode(soundcheck):
    """Convert a Sound Check bytestring value to a (gain, peak) tuple as
    used by ReplayGain.
    """
    # We decode binary data. If one of the formats gives us a text
    # string, interpret it as UTF-8.
    if isinstance(soundcheck, six.text_type):
        soundcheck = soundcheck.encode('utf-8')

    # SoundCheck tags consist of 10 numbers, each represented by 8
    # characters of ASCII hex preceded by a space.
    try:
        soundcheck = codecs.decode(soundcheck.replace(b' ', b''), 'hex')
        soundcheck = struct.unpack('!iiiiiiiiii', soundcheck)
    except (struct.error, TypeError, binascii.Error):
        # SoundCheck isn't in the format we expect, so return default
        # values.
        return 0.0, 0.0

    # SoundCheck stores absolute calculated/measured RMS value in an
    # unknown unit. We need to find the ratio of this measurement
    # compared to a reference value of 1000 to get our gain in dB. We
    # play it safe by using the larger of the two values (i.e., the most
    # attenuation).
    maxgain = max(soundcheck[:2])
    if maxgain > 0:
        gain = math.log10(maxgain / 1000.0) * -10
    else:
        # Invalid gain value found.
        gain = 0.0

    # SoundCheck stores peak values as the actual value of the sample,
    # and again separately for the left and right channels. We need to
    # convert this to a percentage of full scale, which is 32768 for a
    # 16 bit sample. Once again, we play it safe by using the larger of
    # the two values.
    peak = max(soundcheck[6:8]) / 32768.0

    return round(gain, 2), round(peak, 6)


Instruction:
Create a Python 3 function to
encode the given gain and peak values as a Sound Check string.

Response:
def _sc_encode(gain, peak):
    """Encode ReplayGain gain/peak values as a Sound Check string.
    """
    # SoundCheck stores the peak value as the actual value of the
    # sample, rather than the percentage of full scale that RG uses, so
    # we do a simple conversion assuming 16 bit samples.
    peak *= 32768.0

    # SoundCheck stores absolute RMS values in some unknown units rather
    # than the dB values RG uses. We can calculate these absolute values
    # from the gain ratio using a reference value of 1000 units. We also
    # enforce the maximum value here, which is equivalent to about
    # -18.2dB.
    g1 = int(min(round((10 ** (gain / -10)) * 1000), 65534))
    # Same as above, except our reference level is 2500 units.
    g2 = int(min(round((10 ** (gain / -10)) * 2500), 65534))

    # The purpose of these values are unknown, but they also seem to be
    # unused so we just use zero.
    uk = 0
    values = (g1, g1, g2, g2, uk, uk, int(peak), int(peak), uk, uk)
    return (u' %08X' * 10) % values


Instruction:
Implement a Python 3 function for
returning the MIME type of the image data.

Response:
def image_mime_type(data):
    """Return the MIME type of the image data (a bytestring).
    """
    # This checks for a jpeg file with only the magic bytes (unrecognized by
    # imghdr.what). imghdr.what returns none for that type of file, so
    # _wider_test_jpeg is run in that case. It still returns None if it didn't
    # match such a jpeg file.
    kind = _imghdr_what_wrapper(data)
    if kind in ['gif', 'jpeg', 'png', 'tiff', 'bmp']:
        return 'image/{0}'.format(kind)
    elif kind == 'pgm':
        return 'image/x-portable-graymap'
    elif kind == 'pbm':
        return 'image/x-portable-bitmap'
    elif kind == 'ppm':
        return 'image/x-portable-pixmap'
    elif kind == 'xbm':
        return 'image/x-xbitmap'
    else:
        return 'image/x-{0}'.format(kind)


Instruction:
How would you implement a function in Python 3 that
gives a raw value stored on a Mutagen object decode and return the represented value.

Response:
def deserialize(self, mutagen_value):
        """Given a raw value stored on a Mutagen object, decode and
        return the represented value.
        """
        if self.suffix and isinstance(mutagen_value, six.text_type) \
           and mutagen_value.endswith(self.suffix):
            return mutagen_value[:-len(self.suffix)]
        else:
            return mutagen_value


Instruction:
Make a summary of the following Python 3 code
def set(self, mutagen_file, value):
        self.store(mutagen_file, self.serialize(value))

Response: Assign the value for the field using this style.


Instruction:
Explain what the following Python 3 code does
def serialize(self, value):
        if isinstance(value, float) and self.as_type is six.text_type:
            value = u'{0:.{1}f}'.format(value, self.float_places)
            value = self.as_type(value)
        elif self.as_type is six.text_type:
            if isinstance(value, bool):
                # Store bools as 1/0 instead of True/False.
                value = six.text_type(int(bool(value)))
            elif isinstance(value, bytes):
                value = value.decode('utf-8', 'ignore')
            else:
                value = six.text_type(value)
        else:
            value = self.as_type(value)

        if self.suffix:
            value += self.suffix

        return value

Response: Convert the external Python value to a type that is suitable for storing in a Mutagen file object.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_list(self, mutagen_file):
        return [self.deserialize(item) for item in self.fetch(mutagen_file)]

Response: Get a list of all values for the field using this style.


Instruction:
Make a summary of the following Python 3 code
def set_list(self, mutagen_file, values):
        self.store(mutagen_file, [self.serialize(value) for value in values])

Response: Set all values for the field using this style. values should be an iterable.


Instruction:
Write a Python 3 function for
converting APIC frame into Image.

Response:
def deserialize(self, apic_frame):
        """Convert APIC frame into Image."""
        return Image(data=apic_frame.data, desc=apic_frame.desc,
                     type=apic_frame.type)


Instruction:
How would you code a function in Python 3 to
return an APIC frame populated with data from image.

Response:
def serialize(self, image):
        """Return an APIC frame populated with data from ``image``.
        """
        assert isinstance(image, Image)
        frame = mutagen.id3.Frames[self.key]()
        frame.data = image.data
        frame.mime = image.mime_type
        frame.desc = image.desc or u''

        # For compatibility with OS X/iTunes prefer latin-1 if possible.
        # See issue #899
        try:
            frame.desc.encode("latin-1")
        except UnicodeEncodeError:
            frame.encoding = mutagen.id3.Encoding.UTF16
        else:
            frame.encoding = mutagen.id3.Encoding.LATIN1

        frame.type = image.type_index
        return frame


Instruction:
How would you code a function in Python 3 to
turn a Image into a base64 encoded FLAC picture block.

Response:
def serialize(self, image):
        """Turn a Image into a base64 encoded FLAC picture block.
        """
        pic = mutagen.flac.Picture()
        pic.data = image.data
        pic.type = image.type_index
        pic.mime = image.mime_type
        pic.desc = image.desc or u''

        # Encoding with base64 returns bytes on both Python 2 and 3.
        # Mutagen requires the data to be a Unicode string, so we decode
        # it before passing it along.
        return base64.b64encode(pic.write()).decode('ascii')


Instruction:
Implement a Python 3 function for
storing the given pictures in the given mutagen file.

Response:
def store(self, mutagen_file, pictures):
        """``pictures`` is a list of mutagen.flac.Picture instances.
        """
        mutagen_file.clear_pictures()
        for pic in pictures:
            mutagen_file.add_picture(pic)


Instruction:
Implement a Python 3 function for
turning a Image into a mutagen. flac. Picture.

Response:
def serialize(self, image):
        """Turn a Image into a mutagen.flac.Picture.
        """
        pic = mutagen.flac.Picture()
        pic.data = image.data
        pic.type = image.type_index
        pic.mime = image.mime_type
        pic.desc = image.desc or u''
        return pic


Instruction:
Given the following Python 3 function, write the documentation
def delete(self, mutagen_file):
        for cover_tag in self.TAG_NAMES.values():
            try:
                del mutagen_file[cover_tag]
            except KeyError:
                pass

Response: Delete all images from the file.


Instruction:
Can you create a Python 3 function that
yields the list of storage styles that can handle the MediaFile s format.

Response:
def styles(self, mutagen_file):
        """Yields the list of storage styles of this field that can
        handle the MediaFile's format.
        """
        for style in self._styles:
            if mutagen_file.__class__.__name__ in style.formats:
                yield style


Instruction:
Given the following Python 3 function, write the documentation
def _none_value(self):
        if self.out_type == int:
            return 0
        elif self.out_type == float:
            return 0.0
        elif self.out_type == bool:
            return False
        elif self.out_type == six.text_type:
            return u''

Response: Get an appropriate null value for this field s type.


Instruction:
Create a Python 3 function for
getting a 3 - item sequence representing the date consisting of year month and day number. Each number is either an integer or None.

Response:
def _get_date_tuple(self, mediafile):
        """Get a 3-item sequence representing the date consisting of a
        year, month, and day number. Each number is either an integer or
        None.
        """
        # Get the underlying data and split on hyphens and slashes.
        datestring = super(DateField, self).__get__(mediafile, None)
        if isinstance(datestring, six.string_types):
            datestring = re.sub(r'[Tt ].*$', '', six.text_type(datestring))
            items = re.split('[-/]', six.text_type(datestring))
        else:
            items = []

        # Ensure that we have exactly 3 components, possibly by
        # truncating or padding.
        items = items[:3]
        if len(items) < 3:
            items += [None] * (3 - len(items))

        # Use year field if year is missing.
        if not items[0] and hasattr(self, '_year_field'):
            items[0] = self._year_field.__get__(mediafile)

        # Convert each component to an integer if possible.
        items_ = []
        for item in items:
            try:
                items_.append(int(item))
            except (TypeError, ValueError):
                items_.append(None)
        return items_


Instruction:
Write a Python 3 function for
setting the value of the field given a year month and day number.

Response:
def _set_date_tuple(self, mediafile, year, month=None, day=None):
        """Set the value of the field given a year, month, and day
        number. Each number can be an integer or None to indicate an
        unset component.
        """
        if year is None:
            self.__delete__(mediafile)
            return

        date = [u'{0:04d}'.format(int(year))]
        if month:
            date.append(u'{0:02d}'.format(int(month)))
        if month and day:
            date.append(u'{0:02d}'.format(int(day)))
        date = map(six.text_type, date)
        super(DateField, self).__set__(mediafile, u'-'.join(date))

        if hasattr(self, '_year_field'):
            self._year_field.__set__(mediafile, year)


Instruction:
Write a Python 3 function for
getting the names of all writable properties that reflect metadata tags.

Response:
def fields(cls):
        """Get the names of all writable properties that reflect
        metadata tags (i.e., those that are instances of
        :class:`MediaField`).
        """
        for property, descriptor in cls.__dict__.items():
            if isinstance(descriptor, MediaField):
                if isinstance(property, bytes):
                    # On Python 2, class field names are bytes. This method
                    # produces text strings.
                    yield property.decode('utf8', 'ignore')
                else:
                    yield property


Instruction:
Can you write a function in Python 3 where it
gets a sort key for a field name that determines the order fields should be written in.

Response:
def _field_sort_name(cls, name):
        """Get a sort key for a field name that determines the order
        fields should be written in.

        Fields names are kept unchanged, unless they are instances of
        :class:`DateItemField`, in which case `year`, `month`, and `day`
        are replaced by `date0`, `date1`, and `date2`, respectively, to
        make them appear in that order.
        """
        if isinstance(cls.__dict__[name], DateItemField):
            name = re.sub('year',  'date0', name)
            name = re.sub('month', 'date1', name)
            name = re.sub('day',   'date2', name)
        return name


Instruction:
How would you code a function in Python 3 to
get the names of all writable metadata fields sorted in the order that they should be written.

Response:
def sorted_fields(cls):
        """Get the names of all writable metadata fields, sorted in the
        order that they should be written.

        This is a lexicographic order, except for instances of
        :class:`DateItemField`, which are sorted in year-month-day
        order.
        """
        for property in sorted(cls.fields(), key=cls._field_sort_name):
            yield property


Instruction:
Can you write a function in Python 3 where it
adds a field to store custom tags.

Response:
def add_field(cls, name, descriptor):
        """Add a field to store custom tags.

        :param name: the name of the property the field is accessed
                     through. It must not already exist on this class.

        :param descriptor: an instance of :class:`MediaField`.
        """
        if not isinstance(descriptor, MediaField):
            raise ValueError(
                u'{0} must be an instance of MediaField'.format(descriptor))
        if name in cls.__dict__:
            raise ValueError(
                u'property "{0}" already exists on MediaField'.format(name))
        setattr(cls, name, descriptor)


Instruction:
Explain what the following Python 3 code does
def update(self, dict):
        for field in self.sorted_fields():
            if field in dict:
                if dict[field] is None:
                    delattr(self, field)
                else:
                    setattr(self, field, dict[field])

Response: Updates all the fields of the MediaFile with the values from a dictionary.


Instruction:
Can you create a Python 3 function that
returns the number of channels in the audio file.

Response:
def channels(self):
        """The number of channels in the audio (an int)."""
        if hasattr(self.mgfile.info, 'channels'):
            return self.mgfile.info.channels
        return 0


Instruction:
How would you explain what the following Python 3 function does
def bitrate(self):
        if hasattr(self.mgfile.info, 'bitrate') and self.mgfile.info.bitrate:
            # Many formats provide it explicitly.
            return self.mgfile.info.bitrate
        else:
            # Otherwise, we calculate bitrate from the file size. (This
            # is the case for all of the lossless formats.)
            if not self.length:
                # Avoid division by zero if length is not available.
                return 0
            size = os.path.getsize(self.path)
            return int(size * 8 / self.length)

Response: The bitrate of the audio in the file.


Instruction:
Here you have a function in Python 3, explain what it does
def fromfits(infilename, hdu = 0, verbose = True):

    pixelarray, hdr = pyfits.getdata(infilename, hdu, header=True)
    pixelarray = np.asarray(pixelarray).transpose()

    pixelarrayshape = pixelarray.shape
    if verbose :
        print "FITS import shape : (%i, %i)" % (pixelarrayshape[0], pixelarrayshape[1])
        print "FITS file BITPIX : %s" % (hdr["BITPIX"])
        print "Internal array type :", pixelarray.dtype.name

    return pixelarray, hdr

Response: Reads a FITS file and returns a 2D numpy array of the data.


Instruction:
Create a Python 3 function to
write a 2D numpy array to a FITS file.

Response:
def tofits(outfilename, pixelarray, hdr = None, verbose = True):
    """
    Takes a 2D numpy array and write it into a FITS file.
    If you specify a header (pyfits format, as returned by fromfits()) it will be used for the image.
    You can give me boolean numpy arrays, I will convert them into 8 bit integers.
    """
    pixelarrayshape = pixelarray.shape
    if verbose :
        print "FITS export shape : (%i, %i)" % (pixelarrayshape[0], pixelarrayshape[1])

    if pixelarray.dtype.name == "bool":
        pixelarray = np.cast["uint8"](pixelarray)

    if os.path.isfile(outfilename):
        os.remove(outfilename)

    if hdr == None: # then a minimal header will be created
        hdu = pyfits.PrimaryHDU(pixelarray.transpose())
    else: # this if else is probably not needed but anyway ...
        hdu = pyfits.PrimaryHDU(pixelarray.transpose(), hdr)

    hdu.writeto(outfilename)

    if verbose :
        print "Wrote %s" % outfilename


Instruction:
Given the following Python 3 function, write the documentation
def subsample(a): # this is more a generic function then a method ...
    """
    # Ouuwww this is slow ...
    outarray = np.zeros((a.shape[0]*2, a.shape[1]*2), dtype=np.float64)
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
            outarray[2*i,2*j] = a[i,j]
            outarray[2*i+1,2*j] = a[i,j]
            outarray[2*i,2*j+1] = a[i,j]
            outarray[2*i+1,2*j+1] = a[i,j]
    return outarray
    """
    # much better :
    newshape = (2*a.shape[0], 2*a.shape[1])
    slices = [slice(0,old, float(old)/new) for old,new in zip(a.shape,newshape) ]
    coordinates = np.mgrid[slices]
    indices = coordinates.astype('i')   #choose the biggest smaller integer index
    return a[tuple(indices)]

Response: Subsample a array by 2x2


Instruction:
Create a Python 3 function for
wrapping around rebin that actually rebins 2 by 2

Response:
def rebin2x2(a):
    """
    Wrapper around rebin that actually rebins 2 by 2
    """
    inshape = np.array(a.shape)
    if not (inshape % 2 == np.zeros(2)).all(): # Modulo check to see if size is even
        raise RuntimeError, "I want even image shapes !"

    return rebin(a, inshape/2)


Instruction:
How would you code a function in Python 3 to
label the cosmicislands and return a list of dicts containing their positions.

Response:
def labelmask(self, verbose = None):
        """
        Finds and labels the cosmic "islands" and returns a list of dicts containing their positions.
        This is made on purpose for visualizations a la f2n.drawstarslist, but could be useful anyway.
        """
        if verbose == None:
            verbose = self.verbose
        if verbose:
            print "Labeling mask pixels ..."
        # We morphologicaly dilate the mask to generously connect "sparse" cosmics :
        #dilstruct = np.ones((5,5))
        dilmask = ndimage.morphology.binary_dilation(self.mask, structure=dilstruct, iterations=1, mask=None, output=None, border_value=0, origin=0, brute_force=False)
        # origin = 0 means center
        (labels, n) = ndimage.measurements.label(dilmask)
        #print "Number of cosmic ray hits : %i" % n
        #tofits(labels, "labels.fits", verbose = False)
        slicecouplelist = ndimage.measurements.find_objects(labels)
        # Now we have a huge list of couples of numpy slice objects giving a frame around each object
        # For plotting purposes, we want to transform this into the center of each object.
        if len(slicecouplelist) != n:
            # This never happened, but you never know ...
            raise RuntimeError, "Mega error in labelmask !"
        centers = [[(tup[0].start + tup[0].stop)/2.0, (tup[1].start + tup[1].stop)/2.0] for tup in slicecouplelist]
        # We also want to know how many pixels where affected by each cosmic ray.
        # Why ? Dunno... it's fun and available in scipy :-)
        sizes = ndimage.measurements.sum(self.mask.ravel(), labels.ravel(), np.arange(1,n+1,1))
        retdictlist = [{"name":"%i" % size, "x":center[0], "y":center[1]} for (size, center) in zip(sizes, centers)]

        if verbose:
            print "Labeling done"

        return retdictlist


Instruction:
Write a Python 3 script to
give the mask, we replace the actual problematic pixels with the masked 5x5 median value. This mimics what is done in L.A.Cosmic, but it's a bit harder to do in python, as there is no readymade masked median. So for now we do a loop... Saturated stars, if calculated, are also masked : they are not "cleaned", but their pixels are not used for the interpolation. We will directly change self.cleanimage. Instead of using the self.mask, you can supply your own mask as argument. This might be useful to apply this cleaning function iteratively. But for the true L.A.Cosmic, we don't use this, i.e. we use the full mask at each iteration.

Response:
def clean(self, mask = None, verbose = None):
        """
        Given the mask, we replace the actual problematic pixels with the masked 5x5 median value.
        This mimics what is done in L.A.Cosmic, but it's a bit harder to do in python, as there is no
        readymade masked median. So for now we do a loop...
        Saturated stars, if calculated, are also masked : they are not "cleaned", but their pixels are not
        used for the interpolation.

        We will directly change self.cleanimage. Instead of using the self.mask, you can supply your
        own mask as argument. This might be useful to apply this cleaning function iteratively.
        But for the true L.A.Cosmic, we don't use this, i.e. we use the full mask at each iteration.

        """
        if verbose == None:
            verbose = self.verbose
        if mask == None:
            mask = self.mask

        if verbose:
            print "Cleaning cosmic affected pixels ..."

        # So... mask is a 2D array containing False and True, where True means "here is a cosmic"
        # We want to loop through these cosmics one by one.
        cosmicindices = np.argwhere(mask)
        # This is a list of the indices of cosmic affected pixels.
        #print cosmicindices

        # We put cosmic ray pixels to np.Inf to flag them :
        self.cleanarray[mask] = np.Inf

        # Now we want to have a 2 pixel frame of Inf padding around our image.
        w = self.cleanarray.shape[0]
        h = self.cleanarray.shape[1]
        padarray = np.zeros((w+4,h+4))+np.Inf
        padarray[2:w+2,2:h+2] = self.cleanarray.copy() # that copy is important, we need 2 independent arrays

        # The medians will be evaluated in this padarray, skipping the np.Inf.
        # Now in this copy called padarray, we also put the saturated stars to np.Inf, if available :
        if self.satstars is not None:
            padarray[2:w+2,2:h+2][self.satstars] = np.Inf
            # Viva python, I tested this one, it works...

        # A loop through every cosmic pixel :
        for cosmicpos in cosmicindices:
            x = cosmicpos[0]
            y = cosmicpos[1]
            cutout = padarray[x:x+5, y:y+5].ravel() # remember the shift due to the padding !
            #print cutout
            # Now we have our 25 pixels, some of them are np.Inf, and we want to take the median
            goodcutout = cutout[cutout != np.Inf]
            #print np.alen(goodcutout)

            if np.alen(goodcutout) >= 25 :
                # This never happened, but you never know ...
                raise RuntimeError, "Mega error in clean !"
            elif np.alen(goodcutout) > 0 :
                replacementvalue = np.median(goodcutout)
            else :
                # i.e. no good pixels : Shit, a huge cosmic, we will have to improvise ...
                print "OH NO, I HAVE A HUUUUUUUGE COSMIC !!!!!"
                replacementvalue = self.guessbackgroundlevel()

            # We update the cleanarray,
            # but measure the medians in the padarray, so to not mix things up...
            self.cleanarray[x, y] = replacementvalue

        # That's it.
        if verbose:
            print "Cleaning done"

        # FYI, that's how the LACosmic cleaning looks in iraf :
        """
        imarith(outmask,"+",finalsel,outmask)
        imreplace(outmask,1,lower=1,upper=INDEF) # ok so outmask = 1 are the cosmics
        imcalc(outmask,inputmask,"(1.-10000.*im1)",verb-)
        imarith(oldoutput,"*",inputmask,inputmask)
        median(inputmask,med5,5,5,zloreject=-9999,zhi=INDEF,verb-)
        imarith(outmask,"*",med5,med5)
        if (i>1) imdel(output)
        imcalc(oldoutput//","//outmask//","//med5,output,"(1.-im2)*im1+im3",verb-)

        # =

        merging to full mask
        inputmask = 1.0 - 10000.0 * finalsel # So this is 1.0, but cosmics are very negative
        inputmask = oldoutput * inputmask # orig image, with very negative cosmics
        med5 = median of inputmask, but rejecting these negative cosmics
        # i dunno how to do this in python -> had to do the loop
        med5 = finalsel * med5 # we keep only the cosmics of this median
        # actual replacement :
        output = (1.0 - outmask)*oldoutput + med5 # ok
        """


Instruction:
Can you implement a function in Python 3 that
finds saturated stars in the image and store them in self. satstars.

Response:
def findsatstars(self, verbose = None):
        """
        Uses the satlevel to find saturated stars (not cosmics !), and puts the result as a mask in self.satstars.
        This can then be used to avoid these regions in cosmic detection and cleaning procedures.
        Slow ...
        """
        if verbose == None:
            verbose = self.verbose
        if verbose:
                print "Detecting saturated stars ..."
        # DETECTION

        satpixels = self.rawarray > self.satlevel # the candidate pixels

        # We build a smoothed version of the image to look for large stars and their support :
        m5 = ndimage.filters.median_filter(self.rawarray, size=5, mode='mirror')
        # We look where this is above half the satlevel
        largestruct = m5 > (self.satlevel/2.0)
        # The rough locations of saturated stars are now :
        satstarscenters = np.logical_and(largestruct, satpixels)

        if verbose:
                print "Building mask of saturated stars ..."

        # BUILDING THE MASK
        # The subtility is that we want to include all saturated pixels connected to these saturated stars...
        # I haven't found a better solution then the double loop

        # We dilate the satpixels alone, to ensure connectivity in glitchy regions and to add a safety margin around them.
        #dilstruct = np.array([[0,1,0], [1,1,1], [0,1,0]])

        dilsatpixels = ndimage.morphology.binary_dilation(satpixels, structure=dilstruct, iterations=2, mask=None, output=None, border_value=0, origin=0, brute_force=False)
        # It turns out it's better to think large and do 2 iterations...

        # We label these :
        (dilsatlabels, nsat) = ndimage.measurements.label(dilsatpixels)
        #tofits(dilsatlabels, "test.fits")

        if verbose:
                print "We have %i saturated stars." % nsat

        # The ouput, False for now :
        outmask = np.zeros(self.rawarray.shape)

        for i in range(1,nsat+1): # we go through the islands of saturated pixels
            thisisland = dilsatlabels == i # gives us a boolean array
            # Does this intersect with satstarscenters ?
            overlap = np.logical_and(thisisland, satstarscenters)
            if np.sum(overlap) > 0:
                outmask = np.logical_or(outmask, thisisland) # we add thisisland to the mask

        self.satstars = np.cast['bool'](outmask)

        if verbose:
                print "Mask of saturated stars done"


Instruction:
Explain what the following Python 3 code does
def getsatstars(self, verbose = None):
        if verbose == None:
            verbose = self.verbose
        if not self.satlevel > 0:
            raise RuntimeError, "Cannot determine satstars : you gave satlevel <= 0 !"
        if self.satstars == None:
            self.findsatstars(verbose = verbose)
        return self.satstars

Response: Returns the saturated stars for this instance.


Instruction:
Can you generate the documentation for the following Python 3 function
def guessbackgroundlevel(self):
        if self.backgroundlevel == None:
            self.backgroundlevel = np.median(self.rawarray.ravel())
        return self.backgroundlevel

Response: Guesses the background level of the current instance of the class.


Instruction:
Explain what the following Python 3 code does
def lacosmiciteration(self, verbose = None):

        if verbose == None:
            verbose = self.verbose

        if verbose:
            print "Convolving image with Laplacian kernel ..."

        # We subsample, convolve, clip negative values, and rebin to original size
        subsam = subsample(self.cleanarray)
        conved = signal.convolve2d(subsam, laplkernel, mode="same", boundary="symm")
        cliped = conved.clip(min=0.0)
        #cliped = np.abs(conved) # unfortunately this does not work to find holes as well ...
        lplus = rebin2x2(cliped)

        if verbose:
            print "Creating noise model ..."

        # We build a custom noise map, so to compare the laplacian to
        m5 = ndimage.filters.median_filter(self.cleanarray, size=5, mode='mirror')
        # We keep this m5, as I will use it later for the interpolation.
        m5clipped = m5.clip(min=0.00001) # As we will take the sqrt
        noise = (1.0/self.gain) * np.sqrt(self.gain*m5clipped + self.readnoise*self.readnoise)

        if verbose:
           print "Calculating Laplacian signal to noise ratio ..."

        # Laplacian signal to noise ratio :
        s = lplus / (2.0 * noise) # the 2.0 is from the 2x2 subsampling
        # This s is called sigmap in the original lacosmic.cl

        # We remove the large structures (s prime) :
        sp = s - ndimage.filters.median_filter(s, size=5, mode='mirror')

        if verbose:
            print "Selecting candidate cosmic rays ..."

         # Candidate cosmic rays (this will include stars + HII regions)
        candidates = sp > self.sigclip
        nbcandidates = np.sum(candidates)

        if verbose:
            print "  %5i candidate pixels" % nbcandidates

         # At this stage we use the saturated stars to mask the candidates, if available :
        if self.satstars is not None:
            if verbose:
                 print "Masking saturated stars ..."
                 candidates = np.logical_and(np.logical_not(self.satstars), candidates)
                 nbcandidates = np.sum(candidates)

            if verbose:
                print "  %5i candidate pixels not part of saturated stars" % nbcandidates

        if verbose:
            print "Building fine structure image ..."

         # We build the fine structure image :
        m3 = ndimage.filters.median_filter(self.cleanarray, size=3, mode='mirror')
        m37 = ndimage.filters.median_filter(m3, size=7, mode='mirror')
        f = m3 - m37
        # In the article that's it, but in lacosmic.cl f is divided by the noise...
        # Ok I understand why, it depends on if you use sp/f or L+/f as criterion.
        # There are some differences between the article and the iraf implementation.
        # So I will stick to the iraf implementation.
        f = f / noise
        f = f.clip(min=0.01) # as we will divide by f. like in the iraf version.

        if verbose:
            print "Removing suspected compact bright objects ..."

        # Now we have our better selection of cosmics :
        cosmics = np.logical_and(candidates, sp/f > self.objlim)
        # Note the sp/f and not lplus/f ... due to the f = f/noise above.

        nbcosmics = np.sum(cosmics)

        if verbose:
            print "  %5i remaining candidate pixels" % nbcosmics

        # What follows is a special treatment for neighbors, with more relaxed constains.

        if verbose:
            print "Finding neighboring pixels affected by cosmic rays ..."

        # We grow these cosmics a first time to determine the immediate neighborhod  :
        growcosmics = np.cast['bool'](signal.convolve2d(np.cast['float32'](cosmics), growkernel, mode="same", boundary="symm"))

        # From this grown set, we keep those that have sp > sigmalim
        # so obviously not requiring sp/f > objlim, otherwise it would be pointless
        growcosmics = np.logical_and(sp > self.sigclip, growcosmics)

        # Now we repeat this procedure, but lower the detection limit to sigmalimlow :

        finalsel = np.cast['bool'](signal.convolve2d(np.cast['float32'](growcosmics), growkernel, mode="same", boundary="symm"))
        finalsel = np.logical_and(sp > self.sigcliplow, finalsel)

        # Again, we have to kick out pixels on saturated stars :
        if self.satstars is not None:
            if verbose:
                print "Masking saturated stars ..."
            finalsel = np.logical_and(np.logical_not(self.satstars), finalsel)

        nbfinal = np.sum(finalsel)

        if verbose:
            print "  %5i pixels detected as cosmics" % nbfinal

        # Now the replacement of the cosmics...
        # we outsource this to the function clean(), as for some purposes the cleaning might not even be needed.
        # Easy way without masking would be :
        #self.cleanarray[finalsel] = m5[finalsel]

        # We find how many cosmics are not yet known :
        newmask = np.logical_and(np.logical_not(self.mask), finalsel)
        nbnew = np.sum(newmask)

        # We update the mask with the cosmics we have found :
        self.mask = np.logical_or(self.mask, finalsel)

        # We return
        # (used by function lacosmic)

        return {"niter":nbfinal, "nnew":nbnew, "itermask":finalsel, "newmask":newmask}

Response: This function performs one iteration of the L. A. Cosmic algorithm.


Instruction:
Write a Python 3 script to
run the cosmic algorithm.

Response:
def run(self, maxiter = 4, verbose = False):
        """
        Full artillery :-)
            - Find saturated stars
            - Run maxiter L.A.Cosmic iterations (stops if no more cosmics are found)

        Stops if no cosmics are found or if maxiter is reached.
        """

        if self.satlevel > 0 and self.satstars == None:
            self.findsatstars(verbose=True)

        print "Starting %i L.A.Cosmic iterations ..." % maxiter
        for i in range(1, maxiter+1):
            print "Iteration %i" % i

            iterres = self.lacosmiciteration(verbose=verbose)
            print "%i cosmic pixels (%i new)" % (iterres["niter"], iterres["nnew"])

            #self.clean(mask = iterres["mask"]) # No, we want clean to operate on really clean pixels only !
            # Thus we always apply it on the full mask, as lacosmic does :
            self.clean(verbose=verbose)
            # But note that for huge cosmics, one might want to revise this.
            # Thats why I added a feature to skip saturated stars !

            if iterres["niter"] == 0:
                break


Instruction:
Write a Python 3 function that can
search your Django project root for Miragefile. py and Miragefile. py files.

Response:
def search_project_root():
        """
        Search your Django project root.

        returns:
            - path:string  Django project root path
        """

        while True:

            current = os.getcwd()

            if pathlib.Path("Miragefile.py").is_file() or pathlib.Path("Miragefile").is_file():
                return current
            elif os.getcwd() == "/":
                raise FileNotFoundError
            else:
                os.chdir("../")


Instruction:
Write a Python 3 function that can
search your Django application root for a specific locale.

Response:
def search_app_root():
        """
        Search your Django application root

        returns:
            - (String) Django application root path
        """
        while True:

            current = os.getcwd()

            if pathlib.Path("apps.py").is_file():
                return current
            elif pathlib.Path.cwd() == "/":
                raise FileNotFoundError
            else:
                os.chdir("../")


Instruction:
Write a Python 3 function for
judging where current working directory is in Django application or not.

Response:
def in_app() -> bool:
        """
        Judge where current working directory is in Django application or not.

        returns:
            - (Bool) cwd is in app dir returns True
        """
        try:
            MirageEnvironment.set_import_root()
            import apps
            if os.path.isfile("apps.py"):
                return True
            else:
                return False
        except ImportError:
            return False
        except:
            return False


Instruction:
Create a Python 3 function for
calling update_cached_fields() for each object passed in as argument.

Response:
def update_cached_fields(*args):
    """
    Calls update_cached_fields() for each object passed in as argument.
    Supports also iterable objects by checking __iter__ attribute.
    :param args: List of objects
    :return: None
    """
    for a in args:
        if a is not None:
            if hasattr(a, '__iter__'):
                for e in a:
                    e.update_cached_fields()
            else:
                a.update_cached_fields()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def update_cached_fields_pre_save(self, update_fields: list):
        if self.id and update_fields is None:
            self.update_cached_fields(commit=False, exceptions=False)

Response: Call on pre_save signal for objects to automatically refresh on save.


Instruction:
Can you generate the documentation for the following Python 3 function
def start(name):
    # type: (str) -> None
    """ Start working on a new hotfix.

    This will create a new branch off master called hotfix/<name>.

    Args:
        name (str):
            The name of the new feature.
    """
    hotfix_branch = 'hotfix/' + common.to_branch_name(name)
    master = conf.get('git.master_branch', 'master')

    common.assert_on_branch(master)
    common.git_checkout(hotfix_branch, create=True)

Response: Start working on a new hotfix.


Instruction:
How would you implement a function in Python 3 that
merges current feature into develop.

Response:
def finish():
    # type: () -> None
    """ Merge current feature into develop. """
    pretend = context.get('pretend', False)

    if not pretend and (git.staged() or git.unstaged()):
        log.err(
            "You have uncommitted changes in your repo!\n"
            "You need to stash them before you merge the hotfix branch"
        )
        sys.exit(1)

    develop = conf.get('git.devel_branch', 'develop')
    master = conf.get('git.master_branch', 'master')
    branch = git.current_branch(refresh=True)

    common.assert_branch_type('hotfix')

    # Merge hotfix into master
    common.git_checkout(master)
    common.git_pull(master)
    common.git_merge(master, branch.name)

    # Merge hotfix into develop
    common.git_checkout(develop)
    common.git_pull(develop)
    common.git_merge(develop, branch.name)

    # Cleanup
    common.git_branch_delete(branch.name)
    common.git_prune()

    common.git_checkout(master)


Instruction:
Create a Python 3 function to
run the shell command

Response:
def run(self):
        """
        Run the shell command

        Returns:
            ShellCommand: return this ShellCommand instance for chaining
        """
        if not self.block:
            self.output = []
            self.error = []
            self.thread = threading.Thread(target=self.run_non_blocking)
            self.thread.start()
        else:
            self.__create_process()
            self.process.wait()
            if self._stdout is not None:
                self.output = self.process.stdout.read().decode("utf-8")
            if self._stderr is not None:
                self.error = self.process.stderr.read().decode("utf-8")
            self.return_code = self.process.returncode

        return self


Instruction:
Write a Python 3 function for
sending text to stdin. Can only be used on non blocking commands

Response:
def send(self, value):
        """
        Send text to stdin. Can only be used on non blocking commands

        Args:
            value (str): the text to write on stdin
        Raises:
            TypeError: If command is blocking
        Returns:
            ShellCommand: return this ShellCommand instance for chaining
        """
        if not self.block and self._stdin is not None:
            self.writer.write("{}\n".format(value))
            return self
        else:
            raise TypeError(NON_BLOCKING_ERROR_MESSAGE)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def poll_output(self):
        if self.block:
            return self.output

        new_list = self.output[self.old_output_size:]
        self.old_output_size += len(new_list)
        return new_list

Response: Append lines from stdout to self. output.


Instruction:
Create a Python 3 function for
appending lines from stderr to self. error.

Response:
def poll_error(self):
        """
        Append lines from stderr to self.errors.

        Returns:
            list: The lines added since last call
        """
        if self.block:
            return self.error

        new_list = self.error[self.old_error_size:]
        self.old_error_size += len(new_list)
        return new_list


Instruction:
How would you code a function in Python 3 to
kill the current non blocking command

Response:
def kill(self):
        """
        Kill the current non blocking command

        Raises:
            TypeError: If command is blocking
        """
        if self.block:
            raise TypeError(NON_BLOCKING_ERROR_MESSAGE)

        try:
            self.process.kill()
        except ProcessLookupError as exc:
            self.logger.debug(exc)


Instruction:
Given the following Python 3 function, write the documentation
def wait_for(self, pattern, timeout=None):
        should_continue = True

        if self.block:
            raise TypeError(NON_BLOCKING_ERROR_MESSAGE)

        def stop(signum, frame):  # pylint: disable=W0613
            nonlocal should_continue
            if should_continue:
                raise TimeoutError()

        if timeout:
            signal.signal(signal.SIGALRM, stop)
            signal.alarm(timeout)

        while should_continue:
            output = self.poll_output() + self.poll_error()
            filtered = [line for line in output if re.match(pattern, line)]
            if filtered:
                should_continue = False

Response: Block until a pattern is found in stdout and stderr.


Instruction:
Can you implement a function in Python 3 that
checks if the command is currently running

Response:
def is_running(self):
        """
        Check if the command is currently running

        Returns:
            bool: True if running, else False
        """
        if self.block:
            return False

        return self.thread.is_alive() or self.process.poll() is None


Instruction:
Write a Python 3 script to
block and print the output of the command

Response:
def print_live_output(self):
        '''
        Block and print the output of the command

        Raises:
            TypeError: If command is blocking
        '''
        if self.block:
            raise TypeError(NON_BLOCKING_ERROR_MESSAGE)
        else:
            while self.thread.is_alive() or self.old_output_size < len(self.output) or self.old_error_size < len(self.error):
                if self._stdout is not None and len(self.output) > self.old_output_size:
                    while self.old_output_size < len(self.output):
                        self.logger.info(self.output[self.old_output_size])
                        self.old_output_size += 1

                if self._stderr is not None and len(self.error) > self.old_error_size:
                    while self.old_error_size < len(self.error):
                        self.logger.error(self.error[self.old_error_size])
                        self.old_error_size += 1


Instruction:
Can you create a Python 3 function that
runs a command in the current working directory.

Response:
def run(self, command, block=True, cwd=None, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE):
        """
        Create an instance of :class:`~ShellCommand` and run it

        Args:
            command (str): :class:`~ShellCommand`
            block (bool): See :class:`~ShellCommand`
            cwd (str): Override the runner cwd. Useb by the :class:`~ShellCommand` instance
        """
        if cwd is None:
            cwd = self.cwd

        return ShellCommand(command=command, logger=self.logger, block=block, cwd=cwd, stdin=stdin, stdout=stdout, stderr=stderr).run()