Instruction:
Here you have a function in Python 3, explain what it does
def get_build_request(self, build_type=None, inner_template=None,
                          outer_template=None, customize_conf=None,
                          arrangement_version=DEFAULT_ARRANGEMENT_VERSION):
        """
        return instance of BuildRequest or BuildRequestV2

        :param build_type: str, unused
        :param inner_template: str, name of inner template for BuildRequest
        :param outer_template: str, name of outer template for BuildRequest
        :param customize_conf: str, name of customization config for BuildRequest
        :param arrangement_version: int, value of the arrangement version

        :return: instance of BuildRequest or BuildRequestV2
        """
        if build_type is not None:
            warnings.warn("build types are deprecated, do not use the build_type argument")

        validate_arrangement_version(arrangement_version)

        if not arrangement_version or arrangement_version < REACTOR_CONFIG_ARRANGEMENT_VERSION:
            build_request = BuildRequest(
                build_json_store=self.os_conf.get_build_json_store(),
                inner_template=inner_template,
                outer_template=outer_template,
                customize_conf=customize_conf)
        else:
            build_request = BuildRequestV2(
                build_json_store=self.os_conf.get_build_json_store(),
                outer_template=outer_template,
                customize_conf=customize_conf)

        # Apply configured resource limits.
        cpu_limit = self.build_conf.get_cpu_limit()
        memory_limit = self.build_conf.get_memory_limit()
        storage_limit = self.build_conf.get_storage_limit()
        if (cpu_limit is not None or
                memory_limit is not None or
                storage_limit is not None):
            build_request.set_resource_limits(cpu=cpu_limit,
                                              memory=memory_limit,
                                              storage=storage_limit)

        return build_request

Response: Returns an instance of BuildRequest or BuildRequestV2 depending on the build_type and arrangement_version.


Instruction:
How would you explain what the following Python 3 function does
def create_build_from_buildrequest(self, build_request):
        build_request.set_openshift_required_version(self.os_conf.get_openshift_required_version())
        build = build_request.render()
        response = self.os.create_build(json.dumps(build))
        build_response = BuildResponse(response.json(), self)
        return build_response

Response: render provided build_request and submit build from it


Instruction:
Write a Python 3 script to
get an existing build config.

Response:
def _get_existing_build_config(self, build_config):
        """
        Uses the given build config to find an existing matching build config.
        Build configs are a match if:
        - metadata.labels.git-repo-name AND metadata.labels.git-branch AND
          metadata.labels.git-full-repo are equal
        OR
        - metadata.labels.git-repo-name AND metadata.labels.git-branch are equal AND
          metadata.spec.source.git.uri are equal
        OR
        - metadata.name are equal
        """

        bc_labels = build_config['metadata']['labels']
        git_labels = {
            "label_selectors": [(key, bc_labels[key]) for key in self._GIT_LABEL_KEYS]
        }
        old_labels_kwargs = {
            "label_selectors": [(key, bc_labels[key]) for key in self._OLD_LABEL_KEYS],
            "filter_key": FILTER_KEY,
            "filter_value": graceful_chain_get(build_config, *FILTER_KEY.split('.'))
        }
        name = {
            "build_config_id": build_config['metadata']['name']
        }

        queries = (
            (self.os.get_build_config_by_labels, git_labels),
            (self.os.get_build_config_by_labels_filtered, old_labels_kwargs),
            (self.os.get_build_config, name),
        )

        existing_bc = None
        for func, kwargs in queries:
            try:
                existing_bc = func(**kwargs)
                # build config found
                break
            except OsbsException as exc:
                # doesn't exist
                logger.info('Build config NOT found via %s: %s',
                            func.__name__, str(exc))
                continue

        return existing_bc


Instruction:
Here you have a function in Python 3, explain what it does
def _get_image_stream_info_for_build_request(self, build_request):
        image_stream = None
        image_stream_tag_name = None

        if build_request.has_ist_trigger():
            image_stream_tag_id = build_request.trigger_imagestreamtag
            image_stream_id, image_stream_tag_name = image_stream_tag_id.split(':')

            try:
                image_stream = self.get_image_stream(image_stream_id).json()
            except OsbsResponseException as x:
                if x.status_code != 404:
                    raise

            if image_stream:
                try:
                    self.get_image_stream_tag(image_stream_tag_id).json()
                except OsbsResponseException as x:
                    if x.status_code != 404:
                        raise

        return image_stream, image_stream_tag_name

Response: Return ImageStream and ImageStreamTag name for base_image of build_request.


Instruction:
Given the following Python 3 function, write the documentation
def create_prod_build(self, *args, **kwargs):
        logger.warning("prod (all-in-one) builds are deprecated, "
                       "please use create_orchestrator_build "
                       "(support will be removed in version 0.54)")
        return self._do_create_prod_build(*args, **kwargs)

Response: Create a production build for the current build.


Instruction:
Write a Python 3 script for
creating a worker build with the specified parameters

Response:
def create_worker_build(self, **kwargs):
        """
        Create a worker build

        Pass through method to create_prod_build with the following
        modifications:
            - platform param is required
            - release param is required
            - arrangement_version param is required, which is used to
              select which worker_inner:n.json template to use
            - inner template set to worker_inner:n.json if not set
            - outer template set to worker.json if not set
            - customize configuration set to worker_customize.json if not set

        :return: BuildResponse instance
        """
        missing = set()
        for required in ('platform', 'release', 'arrangement_version'):
            if not kwargs.get(required):
                missing.add(required)

        if missing:
            raise ValueError("Worker build missing required parameters: %s" %
                             missing)

        if kwargs.get('platforms'):
            raise ValueError("Worker build called with unwanted platforms param")

        arrangement_version = kwargs['arrangement_version']
        kwargs.setdefault('inner_template', WORKER_INNER_TEMPLATE.format(
            arrangement_version=arrangement_version))
        kwargs.setdefault('outer_template', WORKER_OUTER_TEMPLATE)
        kwargs.setdefault('customize_conf', WORKER_CUSTOMIZE_CONF)
        kwargs['build_type'] = BUILD_TYPE_WORKER
        try:
            return self._do_create_prod_build(**kwargs)
        except IOError as ex:
            if os.path.basename(ex.filename) == kwargs['inner_template']:
                raise OsbsValidationException("worker invalid arrangement_version %s" %
                                              arrangement_version)

            raise


Instruction:
Make a summary of the following Python 3 code
def create_orchestrator_build(self, **kwargs):
        if not self.can_orchestrate():
            raise OsbsOrchestratorNotEnabled("can't create orchestrate build "
                                             "when can_orchestrate isn't enabled")
        extra = [x for x in ('platform',) if kwargs.get(x)]
        if extra:
            raise ValueError("Orchestrator build called with unwanted parameters: %s" %
                             extra)

        arrangement_version = kwargs.setdefault('arrangement_version',
                                                self.build_conf.get_arrangement_version())

        if arrangement_version < REACTOR_CONFIG_ARRANGEMENT_VERSION and not kwargs.get('platforms'):
            raise ValueError('Orchestrator build requires platforms param')

        kwargs.setdefault('inner_template', ORCHESTRATOR_INNER_TEMPLATE.format(
            arrangement_version=arrangement_version))
        kwargs.setdefault('outer_template', ORCHESTRATOR_OUTER_TEMPLATE)
        kwargs.setdefault('customize_conf', ORCHESTRATOR_CUSTOMIZE_CONF)
        kwargs['build_type'] = BUILD_TYPE_ORCHESTRATOR
        try:
            return self._do_create_prod_build(**kwargs)
        except IOError as ex:
            if os.path.basename(ex.filename) == kwargs['inner_template']:
                raise OsbsValidationException("orchestrator invalid arrangement_version %s" %
                                              arrangement_version)

            raise

Response: Create an orchestrator build for the current user.


Instruction:
How would you implement a function in Python 3 that
gets the logs from the build.

Response:
def get_build_logs(self, build_id, follow=False, build_json=None, wait_if_missing=False,
                       decode=False):
        """
        provide logs from build

        NOTE: Since atomic-reactor 1.6.25, logs are always in UTF-8, so if
        asked to decode, we assume that is the encoding in use. Otherwise, we
        return the bytes exactly as they came from the container.

        :param build_id: str
        :param follow: bool, fetch logs as they come?
        :param build_json: dict, to save one get-build query
        :param wait_if_missing: bool, if build doesn't exist, wait
        :param decode: bool, whether or not to decode logs as utf-8
        :return: None, bytes, or iterable of bytes
        """
        logs = self.os.logs(build_id, follow=follow, build_json=build_json,
                            wait_if_missing=wait_if_missing)

        if decode and isinstance(logs, GeneratorType):
            return self._decode_build_logs_generator(logs)

        # str or None returned from self.os.logs()
        if decode and logs is not None:
            logs = logs.decode("utf-8").rstrip()

        return logs


Instruction:
Write a Python 3 script for
getting orchestrator build logs

Response:
def get_orchestrator_build_logs(self, build_id, follow=False, wait_if_missing=False):
        """
        provide logs from orchestrator build

        :param build_id: str
        :param follow: bool, fetch logs as they come?
        :param wait_if_missing: bool, if build doesn't exist, wait
        :return: generator yielding objects with attributes 'platform' and 'line'
        """
        logs = self.get_build_logs(build_id=build_id, follow=follow,
                                   wait_if_missing=wait_if_missing, decode=True)

        if logs is None:
            return
        if isinstance(logs, GeneratorType):
            for entries in logs:
                for entry in entries.splitlines():
                    yield LogEntry(*self._parse_build_log_entry(entry))
        else:
            for entry in logs.splitlines():
                yield LogEntry(*self._parse_build_log_entry(entry))


Instruction:
Create a Python 3 function for
importing image tags from a Docker registry into an ImageStream

Response:
def import_image(self, name, tags=None):
        """
        Import image tags from a Docker registry into an ImageStream

        :return: bool, whether tags were imported
        """
        stream_import_file = os.path.join(self.os_conf.get_build_json_store(),
                                          'image_stream_import.json')
        with open(stream_import_file) as f:
            stream_import = json.load(f)
        return self.os.import_image(name, stream_import, tags=tags)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def import_image_tags(self, name, tags, repository, insecure=False):
        stream_import_file = os.path.join(self.os_conf.get_build_json_store(),
                                          'image_stream_import.json')
        with open(stream_import_file) as f:
            stream_import = json.load(f)
        return self.os.import_image_tags(name, stream_import, tags,
                                         repository, insecure)

Response: Imports image tags from specified container repository.


Instruction:
Write a Python 3 script to
ensure the tag is monitored in ImageStreamTag

Response:
def ensure_image_stream_tag(self, stream, tag_name, scheduled=False,
                                source_registry=None, organization=None, base_image=None):
        """Ensures the tag is monitored in ImageStream

        :param stream: dict, ImageStream object
        :param tag_name: str, name of tag to check, without name of
                              ImageStream as prefix
        :param scheduled: bool, if True, importPolicy.scheduled will be
                                set to True in ImageStreamTag
        :param source_registry: dict, info about source registry
        :param organization: str, oganization for registry
        :param base_image: str, base image
        :return: bool, whether or not modifications were performed
        """
        img_stream_tag_file = os.path.join(self.os_conf.get_build_json_store(),
                                           'image_stream_tag.json')
        with open(img_stream_tag_file) as f:
            tag_template = json.load(f)

        repository = None
        registry = None
        insecure = False

        if source_registry:
            registry = RegistryURI(source_registry['url']).docker_uri
            insecure = source_registry.get('insecure', False)

        if base_image and registry:
            repository = self._get_enclosed_repo_with_source_registry(base_image,
                                                                      registry, organization)

        return self.os.ensure_image_stream_tag(stream, tag_name, tag_template,
                                               scheduled, repository=repository,
                                               insecure=insecure)


Instruction:
Can you generate the documentation for the following Python 3 function
def create_image_stream(self, name, docker_image_repository,
                            insecure_registry=False):
        """
        Create an ImageStream object

        Raises exception on error

        :param name: str, name of ImageStream
        :param docker_image_repository: str, pull spec for docker image
               repository
        :param insecure_registry: bool, whether plain HTTP should be used
        :return: response
        """
        img_stream_file = os.path.join(self.os_conf.get_build_json_store(), 'image_stream.json')
        with open(img_stream_file) as f:
            stream = json.load(f)
        stream['metadata']['name'] = name
        stream['metadata'].setdefault('annotations', {})
        stream['metadata']['annotations'][ANNOTATION_SOURCE_REPO] = docker_image_repository
        if insecure_registry:
            stream['metadata']['annotations'][ANNOTATION_INSECURE_REPO] = 'true'

        return self.os.create_image_stream(json.dumps(stream))

Response: Create an ImageStream object with the given name and docker image repository.


Instruction:
Make a summary of the following Python 3 code
def get_compression_extension(self):

        build_request = BuildRequest(build_json_store=self.os_conf.get_build_json_store())
        inner = build_request.inner_template
        postbuild_plugins = inner.get('postbuild_plugins', [])
        for plugin in postbuild_plugins:
            if plugin.get('name') == 'compress':
                args = plugin.get('args', {})
                method = args.get('method', 'gzip')
                if method == 'gzip':
                    return '.gz'
                elif method == 'lzma':
                    return '.xz'
                raise OsbsValidationException("unknown compression method '%s'"
                                              % method)

        return None

Response:
Find the filename extension for the docker save output which
        may or may not be compressed.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_config_map(self, name, data):
        config_data_file = os.path.join(self.os_conf.get_build_json_store(), 'config_map.json')
        with open(config_data_file) as f:
            config_data = json.load(f)
        config_data['metadata']['name'] = name
        data_dict = {}
        for key, value in data.items():
            data_dict[key] = json.dumps(value)
        config_data['data'] = data_dict

        response = self.os.create_config_map(config_data)
        config_map_response = ConfigMapResponse(response.json())
        return config_map_response

Response: Create a ConfigMap object on the server


Instruction:
How would you code a function in Python 3 to
get a ConfigMap object from the server

Response:
def get_config_map(self, name):
        """
        Get a ConfigMap object from the server

        Raises exception on error

        :param name: str, name of configMap to get from the server
        :returns: ConfigMapResponse containing the ConfigMap with the requested name
        """
        response = self.os.get_config_map(name)
        config_map_response = ConfigMapResponse(response.json())
        return config_map_response


Instruction:
Implement a function in Python 3 to
wipe the bolt database.

Response:
def wipe(self):
        """
        Wipe the bolt database.

        Calling this after HoverPy has been instantiated is
        potentially dangerous. This function is mostly used
        internally for unit tests.
        """
        try:
            if os.isfile(self._dbpath):
                os.remove(self._dbpath)
        except OSError:
            pass


Instruction:
How would you code a function in Python 3 to
get the simulation data.

Response:
def simulation(self, data=None):
        """
        Gets / Sets the simulation data.

        If no data is passed in, then this method acts as a getter.
        if data is passed in, then this method acts as a setter.

        Keyword arguments:
        data -- the simulation data you wish to set (default None)
        """
        if data:
            return self._session.put(self.__v2() + "/simulation", data=data)
        else:
            return self._session.get(self.__v2() + "/simulation").json()


Instruction:
Can you generate the documentation for the following Python 3 function
def destination(self, name=""):
        if name:
            return self._session.put(
                self.__v2() + "/hoverfly/destination",
                data={"destination": name}).json()
        else:
            return self._session.get(
                self.__v2() + "/hoverfly/destination").json()

Response: Gets the destination data.


Instruction:
Implement a Python 3 function for
getting / Sets the mode. If no mode is provided, then this method acts as a getter. Keyword arguments: mode -- this should either be 'capture' or 'simulate' (default None)

Response:
def mode(self, mode=None):
        """
        Gets / Sets the mode.

        If no mode is provided, then this method acts as a getter.

        Keyword arguments:
        mode -- this should either be 'capture' or 'simulate' (default None)
        """
        if mode:
            logging.debug("SWITCHING TO %s" % mode)
            url = self.__v2() + "/hoverfly/mode"
            logging.debug(url)
            return self._session.put(
                url, data=json.dumps({"mode": mode})).json()["mode"]
        else:
            return self._session.get(
                self.__v2() + "/hoverfly/mode").json()["mode"]


Instruction:
Create a Python 3 function for
getting the metadata of the current user.

Response:
def metadata(self, delete=False):
        """
        Gets the metadata.
        """
        if delete:
            return self._session.delete(self.__v1() + "/metadata").json()
        else:
            return self._session.get(self.__v1() + "/metadata").json()


Instruction:
Given the following Python 3 function, write the documentation
def records(self, data=None):
        if data:
            return self._session.post(
                self.__v1() + "/records", data=data).json()
        else:
            return self._session.get(self.__v1() + "/records").json()

Response: Gets or sets records.


Instruction:
How would you explain what the following Python 3 function does
def delays(self, delays=[]):
        if delays:
            return self._session.put(
                self.__v1() + "/delays", data=json.dumps(delays)).json()
        else:
            return self._session.get(self.__v1() + "/delays").json()

Response: Gets or sets the delays.


Instruction:
Can you generate the documentation for the following Python 3 function
def addDelay(self, urlPattern="", delay=0, httpMethod=None):
        print("addDelay is deprecated please use delays instead")
        delay = {"urlPattern": urlPattern, "delay": delay}
        if httpMethod:
            delay["httpMethod"] = httpMethod
        return self.delays(delays={"data": [delay]})

Response: Adds a delay to the set of delays.


Instruction:
Can you write a function in Python 3 where it
enables the hoverfly as a proxy.

Response:
def __enableProxy(self):
        """
        Set the required environment variables to enable the use of hoverfly as a proxy.
        """
        os.environ[
            "HTTP_PROXY"] = self.httpProxy()
        os.environ[
            "HTTPS_PROXY"] = self.httpsProxy()

        os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(
            os.path.dirname(
                os.path.abspath(__file__)),
            "cert.pem")


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

Response:
def __writepid(self, pid):
        """
        HoverFly fails to launch if it's already running on
        the same ports. So we have to keep track of them using
        temp files with the proxy port and admin port, containing
        the processe's PID.
        """
        import tempfile
        d = tempfile.gettempdir()
        name = os.path.join(d, "hoverpy.%i.%i"%(self._proxyPort, self._adminPort))
        with open(name, 'w') as f:
            f.write(str(pid))
            logging.debug("writing to %s"%name)


Instruction:
How would you implement a function in Python 3 that
removes the PID file on shutdown unfortunately unfortunately this may not get called on shutdown

Response:
def __rmpid(self):
        """
        Remove the PID file on shutdown, unfortunately
        this may not get called if not given the time to
        shut down.
        """
        import tempfile
        d = tempfile.gettempdir()
        name = os.path.join(d, "hoverpy.%i.%i"%(self._proxyPort, self._adminPort))
        if os.path.exists(name):
            os.unlink(name)
            logging.debug("deleting %s"%name)


Instruction:
Write a Python 3 function for
killing HoverFly processes on these ports if they did not shut down correctly

Response:
def __kill_if_not_shut_properly(self):
        """
        If the HoverFly process on these given ports
        did not shut down correctly, then kill the pid
        before launching a new instance.
        todo: this will kill existing HoverFly processes
        on those ports indiscriminately
        """
        import tempfile
        d = tempfile.gettempdir()
        name = os.path.join(d, "hoverpy.%i.%i"%(self._proxyPort, self._adminPort))
        if os.path.exists(name):
            logging.debug("pid file exists.. killing it")
            f = open(name, "r")
            pid = int(f.read())
            try:
                import signal
                os.kill(pid, signal.SIGTERM)
                logging.debug("killing %i"%pid)
            except:
                logging.debug("nothing to clean up")
                pass
            finally:
                os.unlink(name)


Instruction:
Implement a Python 3 function for
starting the hoverfly process.

Response:
def __start(self):
        """
        Start the hoverfly process.

        This function waits until it can make contact
        with the hoverfly API before returning.
        """
        logging.debug("starting %i" % id(self))
        self.__kill_if_not_shut_properly()
        self.FNULL = open(os.devnull, 'w')
        flags = self.__flags()
        cmd = [hoverfly] + flags
        if self._showCmd:
            print(cmd)
        self._process = Popen(
            [hoverfly] +
            flags,
            stdin=self.FNULL,
            stdout=self.FNULL,
            stderr=subprocess.STDOUT)
        start = time.time()
        while time.time() - start < 1:
            try:
                url = "http://%s:%i/api/health" % (self._host, self._adminPort)
                r = self._session.get(url)
                j = r.json()
                up = "message" in j and "healthy" in j["message"]
                if up:
                    logging.debug("has pid %i" % self._process.pid)
                    self.__writepid(self._process.pid)
                    return self._process
                else:
                    time.sleep(1/100.0)
            except:
                # import traceback
                # traceback.print_exc()
                # wait 10 ms before trying again
                time.sleep(1/100.0)
                pass

        logging.error("Could not start hoverfly!")
        raise ValueError("Could not start hoverfly!")


Instruction:
Make a summary of the following Python 3 code
def __stop(self):
        if logging:
            logging.debug("stopping")
        self._process.terminate()
        # communicate means we wait until the process
        # was actually terminated, this removes some
        # warnings in python3
        self._process.communicate()
        self._process = None
        self.FNULL.close()
        self.FNULL = None
        self.__disableProxy()
        # del self._session
        # self._session = None
        self.__rmpid()

Response: Stop the hoverfly process.


Instruction:
Write a Python 3 script for
returning a list of flags for the current instance of the class.

Response:
def __flags(self):
        """
        Internal method. Turns arguments into flags.
        """
        flags = []
        if self._capture:
            flags.append("-capture")
        if self._spy:
            flags.append("-spy")
        if self._dbpath:
            flags += ["-db-path", self._dbpath]
            flags += ["-db", "boltdb"]
        else:
            flags += ["-db", "memory"]
        if self._synthesize:
            assert(self._middleware)
            flags += ["-synthesize"]
        if self._simulation:
            flags += ["-import", self._simulation]
        if self._proxyPort:
            flags += ["-pp", str(self._proxyPort)]
        if self._adminPort:
            flags += ["-ap", str(self._adminPort)]
        if self._modify:
            flags += ["-modify"]
        if self._verbose:
            flags += ["-v"]
        if self._dev:
            flags += ["-dev"]
        if self._metrics:
            flags += ["-metrics"]
        if self._auth:
            flags += ["-auth"]
        if self._middleware:
            flags += ["-middleware", self._middleware]
        if self._cert:
            flags += ["-cert", self._cert]
        if self._certName:
            flags += ["-cert-name", self._certName]
        if self._certOrg:
            flags += ["-cert-org", self._certOrg]
        if self._destination:
            flags += ["-destination", self._destination]
        if self._key:
            flags += ["-key", self._key]
        if self._dest:
            for i in range(len(self._dest)):
                flags += ["-dest", self._dest[i]]
        if self._generateCACert:
            flags += ["-generate-ca-cert"]
        if not self._tlsVerification:
            flags += ["-tls-verification", "false"]

        logging.debug("flags:" + str(flags))
        return flags


Instruction:
Write a Python 3 function that can
extract tabular data as |TableData| instances from a JSON file. |load_source_desc_file| This method can be loading four types of JSON formats: **(1)** Single table data in a file: .. code-block:: json :caption: Acceptable JSON Schema (1): single table { "type": "array", "items": { "type": "object", "additionalProperties": { "anyOf": [ {"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "null"} ] } } } .. code-block:: json :caption: Acceptable JSON example for the JSON schema (1) [ {"attr_b": 4, "attr_c": "a", "attr_a": 1}, {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2}, {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3} ] The example data will be loaded as the following tabular data: .. table:: +------+------+------+ |attr_a|attr_b|attr_c| +======+======+======+ | 1| 4.0|a | +------+------+------+ | 2| 2.1|bb | +------+------+------+ | 3| 120.9|ccc | +------+------+------+ **(2)** Single table data in a file: .. code-block:: json :caption: Acceptable JSON Schema (2): single table { "type": "object", "additionalProperties": { "type": "array", "items": { "anyOf": [ {"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "null"} ] } } } .. code-block:: json :caption: Acceptable JSON example for the JSON schema (2) { "attr_a": [1, 2, 3], "attr_b": [4, 2.1, 120.9], "attr_c": ["a", "bb", "ccc"] } The example data will be loaded as the following tabular data: .. table:: +------+------+------+ |attr_a|attr_b|attr_c| +======+======+======+ | 1| 4.0|a | +------+------+------+ | 2| 2.1|bb | +------+------+------+ | 3| 120.9|ccc | +------+------+------+ **(3)** Single table data in a file: .. code-block:: json :caption: Acceptable JSON Schema (3): single table { "type": "object", "additionalProperties": { "anyOf": [ {"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "null"} ] } } .. code-block:: json :caption: Acceptable JSON example for the JSON schema (3) { "num_ratings": 27, "support_threads": 1, "downloaded": 925716, "last_updated":"2017-12-01 6:22am GMT", "added":"2010-01-20", "num": 1.1, "hoge": null } The example data will be loaded as the following tabular data: .. table:: +---------------+---------------------+ | key | value | +===============+=====================+ |num_ratings | 27| +---------------+---------------------+ |support_threads| 1| +---------------+---------------------+ |downloaded | 925716| +---------------+---------------------+ |last_updated |2017-12-01 6:22am GMT| +---------------+---------------------+ |added |2010-01-20 | +---------------+---------------------+ |num | 1.1| +---------------+---------------------+ |hoge |None | +---------------+---------------------+ **(4)** Multiple table data in a file: .. code-block:: json :caption: Acceptable JSON Schema (4): multiple tables { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "object", "additionalProperties": { "anyOf": [ {"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "null"} ] } } } } .. code-block:: json :caption: Acceptable JSON example for the JSON schema (4) { "table_a" : [ {"attr_b": 4, "attr_c": "a", "attr_a": 1}, {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2}, {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3} ], "table_b" : [ {"a": 1, "b": 4}, {"a": 2 }, {"a": 3, "b": 120.9} ] } The example data will be loaded as the following tabular data: .. table:: table_a +------+------+------+ |attr_a|attr_b|attr_c| +======+======+======+ | 1| 4.0|a | +------+------+------+ | 2| 2.1|bb | +------+------+------+ | 3| 120.9|ccc | +------+------+------+ .. table:: table_b +-+-----+ |a| b | +=+=====+ |1| 4.0| +-+-----+ |2| None| +-+-----+ |3|120.9| +-+-----+ **(5)** Multiple table data in a file: .. code-block:: json :caption: Acceptable JSON Schema (5): multiple tables { "type": "object", "additionalProperties": { "type": "object", "additionalProperties": { "type": "array", "items": { "anyOf": [ {"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "null"} ] } } } } .. code-block:: json :caption: Acceptable JSON example for the JSON schema (5) { "table_a" : { "attr_a": [1, 2, 3], "attr_b": [4, 2.1, 120.9], "attr_c": ["a", "bb", "ccc"] }, "table_b" : { "a": [1, 3], "b": [4, 120.9] } } The example data will be loaded as the following tabular data: .. table:: table_a +------+------+------+ |attr_a|attr_b|attr_c| +======+======+======+ | 1| 4.0|a | +------+------+------+ | 2| 2.1|bb | +------+------+------+ | 3| 120.9|ccc | +------+------+------+ .. table:: table_b +-+-----+ |a| b | +=+=====+ |1| 4.0| +-+-----+ |3|120.9| +-+-----+ **(6)** Multiple table data in a file: .. code-block:: json :caption: Acceptable JSON Schema (6): multiple tables { "type": "object", "additionalProperties": { "type": "object", "additionalProperties": { "anyOf": [ {"type": "string"}, {"type": "number"}, {"type": "boolean"}, {"type": "null"} ] } } } .. code-block:: json :caption: Acceptable JSON example for the JSON schema (6) { "table_a": { "num_ratings": 27, "support_threads": 1, "downloaded": 925716, "last_updated":"2017-12-01 6:22am GMT", "added":"2010-01-20", "num": 1.1, "hoge": null }, "table_b": { "a": 4, "b": 120.9 } } The example data will be loaded as the following tabular data: .. table:: table_a +---------------+---------------------+ | key | value | +===============+=====================+ |num_ratings | 27| +---------------+---------------------+ |support_threads| 1| +---------------+---------------------+ |downloaded | 925716| +---------------+---------------------+ |last_updated |2017-12-01 6:22am GMT| +---------------+---------------------+ |added |2010-01-20 | +---------------+---------------------+ |num | 1.1| +---------------+---------------------+ |hoge |None | +---------------+---------------------+ .. table:: table_b +---+-----+ |key|value| +===+=====+ |a | 4.0| +---+-----+ |b |120.9| +---+-----+ :return: Loaded table data iterator. |load_table_name_desc| =================== ============================================== Format specifier Value after the replacement =================== ============================================== ``%(filename)s`` |filename_desc| ``%(key)s`` | This replaced the different value | for each single/multiple JSON tables: | [single JSON table] | ``%(format_name)s%(format_id)s`` | [multiple JSON table] Table data key. ``%(format_name)s`` ``"json"`` ``%(format_id)s`` |format_id_desc| ``%(global_id)s`` |global_id| =================== ============================================== :rtype: |TableData| iterator :raises pytablereader.DataError: If the data is invalid JSON. :raises pytablereader.error.ValidationError: If the data is not acceptable JSON format.

Response:
def load(self):
        """
        Extract tabular data as |TableData| instances from a JSON file.
        |load_source_desc_file|

        This method can be loading four types of JSON formats:

        **(1)** Single table data in a file:

            .. code-block:: json
                :caption: Acceptable JSON Schema (1): single table

                {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "additionalProperties": {
                            "anyOf": [
                                {"type": "string"},
                                {"type": "number"},
                                {"type": "boolean"},
                                {"type": "null"}
                            ]
                        }
                    }
                }

            .. code-block:: json
                :caption: Acceptable JSON example for the JSON schema (1)

                [
                    {"attr_b": 4, "attr_c": "a", "attr_a": 1},
                    {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
                    {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
                ]

            The example data will be loaded as the following tabular data:

                .. table::

                    +------+------+------+
                    |attr_a|attr_b|attr_c|
                    +======+======+======+
                    |     1|   4.0|a     |
                    +------+------+------+
                    |     2|   2.1|bb    |
                    +------+------+------+
                    |     3| 120.9|ccc   |
                    +------+------+------+

        **(2)** Single table data in a file:

            .. code-block:: json
                :caption: Acceptable JSON Schema (2): single table

                {
                    "type": "object",
                    "additionalProperties": {
                        "type": "array",
                        "items": {
                            "anyOf": [
                                {"type": "string"},
                                {"type": "number"},
                                {"type": "boolean"},
                                {"type": "null"}
                            ]
                        }
                    }
                }

            .. code-block:: json
                :caption: Acceptable JSON example for the JSON schema (2)

                {
                    "attr_a": [1, 2, 3],
                    "attr_b": [4, 2.1, 120.9],
                    "attr_c": ["a", "bb", "ccc"]
                }

            The example data will be loaded as the following tabular data:

                .. table::

                    +------+------+------+
                    |attr_a|attr_b|attr_c|
                    +======+======+======+
                    |     1|   4.0|a     |
                    +------+------+------+
                    |     2|   2.1|bb    |
                    +------+------+------+
                    |     3| 120.9|ccc   |
                    +------+------+------+

        **(3)** Single table data in a file:

            .. code-block:: json

                :caption: Acceptable JSON Schema (3): single table

                {
                    "type": "object",
                    "additionalProperties": {
                        "anyOf": [
                            {"type": "string"},
                            {"type": "number"},
                            {"type": "boolean"},
                            {"type": "null"}
                        ]
                    }
                }

            .. code-block:: json
                :caption: Acceptable JSON example for the JSON schema (3)

                {
                    "num_ratings": 27,
                    "support_threads": 1,
                    "downloaded": 925716,
                    "last_updated":"2017-12-01 6:22am GMT",
                    "added":"2010-01-20",
                    "num": 1.1,
                    "hoge": null
                }

            The example data will be loaded as the following tabular data:

                .. table::

                    +---------------+---------------------+
                    |      key      |        value        |
                    +===============+=====================+
                    |num_ratings    |                   27|
                    +---------------+---------------------+
                    |support_threads|                    1|
                    +---------------+---------------------+
                    |downloaded     |               925716|
                    +---------------+---------------------+
                    |last_updated   |2017-12-01 6:22am GMT|
                    +---------------+---------------------+
                    |added          |2010-01-20           |
                    +---------------+---------------------+
                    |num            |                  1.1|
                    +---------------+---------------------+
                    |hoge           |None                 |
                    +---------------+---------------------+

        **(4)** Multiple table data in a file:

            .. code-block:: json
                :caption: Acceptable JSON Schema (4): multiple tables

                {
                    "type": "object",
                    "additionalProperties": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "additionalProperties": {
                                "anyOf": [
                                    {"type": "string"},
                                    {"type": "number"},
                                    {"type": "boolean"},
                                    {"type": "null"}
                                ]
                            }
                        }
                    }
                }

            .. code-block:: json
                :caption: Acceptable JSON example for the JSON schema (4)

                {
                    "table_a" : [
                        {"attr_b": 4, "attr_c": "a", "attr_a": 1},
                        {"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
                        {"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
                    ],
                    "table_b" : [
                        {"a": 1, "b": 4},
                        {"a": 2 },
                        {"a": 3, "b": 120.9}
                    ]
                }

            The example data will be loaded as the following tabular data:

                .. table:: table_a

                    +------+------+------+
                    |attr_a|attr_b|attr_c|
                    +======+======+======+
                    |     1|   4.0|a     |
                    +------+------+------+
                    |     2|   2.1|bb    |
                    +------+------+------+
                    |     3| 120.9|ccc   |
                    +------+------+------+

                .. table:: table_b

                    +-+-----+
                    |a|  b  |
                    +=+=====+
                    |1|  4.0|
                    +-+-----+
                    |2| None|
                    +-+-----+
                    |3|120.9|
                    +-+-----+

        **(5)** Multiple table data in a file:

            .. code-block:: json
                :caption: Acceptable JSON Schema (5): multiple tables

                {
                    "type": "object",
                    "additionalProperties": {
                        "type": "object",
                        "additionalProperties": {
                            "type": "array",
                            "items": {
                                "anyOf": [
                                    {"type": "string"},
                                    {"type": "number"},
                                    {"type": "boolean"},
                                    {"type": "null"}
                                ]
                            }
                        }
                    }
                }

            .. code-block:: json
                :caption: Acceptable JSON example for the JSON schema (5)

                {
                    "table_a" : {
                        "attr_a": [1, 2, 3],
                        "attr_b": [4, 2.1, 120.9],
                        "attr_c": ["a", "bb", "ccc"]
                    },
                    "table_b" : {
                        "a": [1, 3],
                        "b": [4, 120.9]
                    }
                }

            The example data will be loaded as the following tabular data:

                .. table:: table_a

                    +------+------+------+
                    |attr_a|attr_b|attr_c|
                    +======+======+======+
                    |     1|   4.0|a     |
                    +------+------+------+
                    |     2|   2.1|bb    |
                    +------+------+------+
                    |     3| 120.9|ccc   |
                    +------+------+------+

                .. table:: table_b

                    +-+-----+
                    |a|  b  |
                    +=+=====+
                    |1|  4.0|
                    +-+-----+
                    |3|120.9|
                    +-+-----+

        **(6)** Multiple table data in a file:

            .. code-block:: json
                :caption: Acceptable JSON Schema (6): multiple tables

                {
                    "type": "object",
                    "additionalProperties": {
                        "type": "object",
                        "additionalProperties": {
                            "anyOf": [
                                {"type": "string"},
                                {"type": "number"},
                                {"type": "boolean"},
                                {"type": "null"}
                            ]
                        }
                    }
                }

            .. code-block:: json
                :caption: Acceptable JSON example for the JSON schema (6)

                {
                    "table_a": {
                        "num_ratings": 27,
                        "support_threads": 1,
                        "downloaded": 925716,
                        "last_updated":"2017-12-01 6:22am GMT",
                        "added":"2010-01-20",
                        "num": 1.1,
                        "hoge": null
                    },
                    "table_b": {
                        "a": 4,
                        "b": 120.9
                    }
                }

            The example data will be loaded as the following tabular data:

                .. table:: table_a

                    +---------------+---------------------+
                    |      key      |        value        |
                    +===============+=====================+
                    |num_ratings    |                   27|
                    +---------------+---------------------+
                    |support_threads|                    1|
                    +---------------+---------------------+
                    |downloaded     |               925716|
                    +---------------+---------------------+
                    |last_updated   |2017-12-01 6:22am GMT|
                    +---------------+---------------------+
                    |added          |2010-01-20           |
                    +---------------+---------------------+
                    |num            |                  1.1|
                    +---------------+---------------------+
                    |hoge           |None                 |
                    +---------------+---------------------+

                .. table:: table_b

                    +---+-----+
                    |key|value|
                    +===+=====+
                    |a  |  4.0|
                    +---+-----+
                    |b  |120.9|
                    +---+-----+

        :return:
            Loaded table data iterator.
            |load_table_name_desc|

            ===================  ==============================================
            Format specifier     Value after the replacement
            ===================  ==============================================
            ``%(filename)s``     |filename_desc|
            ``%(key)s``          | This replaced the different value
                                 | for each single/multiple JSON tables:
                                 | [single JSON table]
                                 | ``%(format_name)s%(format_id)s``
                                 | [multiple JSON table] Table data key.
            ``%(format_name)s``  ``"json"``
            ``%(format_id)s``    |format_id_desc|
            ``%(global_id)s``    |global_id|
            ===================  ==============================================
        :rtype: |TableData| iterator
        :raises pytablereader.DataError:
            If the data is invalid JSON.
        :raises pytablereader.error.ValidationError:
            If the data is not acceptable JSON format.
        """

        formatter = JsonTableFormatter(self.load_dict())
        formatter.accept(self)

        return formatter.to_table_data()


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

        self._validate()
        self._logger.logging_load()
        self.encoding = get_file_encoding(self.source, self.encoding)

        with io.open(self.source, "r", encoding=self.encoding) as fp:
            formatter = MediaWikiTableFormatter(fp.read())
        formatter.accept(self)

        return formatter.to_table_data()

Response: Loads the tabular data from a MediaWiki file.


Instruction:
Write a Python 3 script for
loading the tabular data from a MediaWiki text object.

Response:
def load(self):
        """
        Extract tabular data as |TableData| instances from a MediaWiki text
        object.
        |load_source_desc_text|

        :return:
            Loaded table data iterator.
            |load_table_name_desc|

            ===================  ==============================================
            Format specifier     Value after the replacement
            ===================  ==============================================
            ``%(filename)s``     ``""``
            ``%(key)s``          | This replaced to:
                                 | **(1)** ``caption`` mark of the table
                                 | **(2)** ``%(format_name)s%(format_id)s``
                                 | if ``caption`` mark not included
                                 | in the table.
            ``%(format_name)s``  ``"mediawiki"``
            ``%(format_id)s``    |format_id_desc|
            ``%(global_id)s``    |global_id|
            ===================  ==============================================
        :rtype: |TableData| iterator
        :raises pytablereader.DataError:
            If the MediaWiki data is invalid or empty.
        """

        self._validate()
        self._logger.logging_load()

        formatter = MediaWikiTableFormatter(self.source)
        formatter.accept(self)

        return formatter.to_table_data()


Instruction:
How would you code a function in Python 3 to
return dock json from existing build json

Response:
def get_dock_json(self):
        """ return dock json from existing build json """
        env_json = self.build_json['spec']['strategy']['customStrategy']['env']
        try:
            p = [env for env in env_json if env["name"] == "ATOMIC_REACTOR_PLUGINS"]
        except TypeError:
            raise RuntimeError("\"env\" is not iterable")
        if len(p) <= 0:
            raise RuntimeError("\"env\" misses key ATOMIC_REACTOR_PLUGINS")
        dock_json_str = p[0]['value']
        dock_json = json.loads(dock_json_str)
        return dock_json


Instruction:
Explain what the following Python 3 code does
def dock_json_get_plugin_conf(self, plugin_type, plugin_name):
        match = [x for x in self.dock_json[plugin_type] if x.get('name') == plugin_name]
        return match[0]

Response: Get the configuration for a plugin.


Instruction:
Here you have a function in Python 3, explain what it does
def remove_plugin(self, plugin_type, plugin_name):
        for p in self.dock_json[plugin_type]:
            if p.get('name') == plugin_name:
                self.dock_json[plugin_type].remove(p)
                break

Response: Remove a specific plugin from the dock. json file.


Instruction:
Create a Python 3 function to
add a new plugin to the dock_json.

Response:
def add_plugin(self, plugin_type, plugin_name, args_dict):
        """
        if config has plugin, override it, else add it
        """

        plugin_modified = False

        for plugin in self.dock_json[plugin_type]:
            if plugin['name'] == plugin_name:
                plugin['args'] = args_dict
                plugin_modified = True

        if not plugin_modified:
            self.dock_json[plugin_type].append({"name": plugin_name, "args": args_dict})


Instruction:
Implement a Python 3 function for
checking whether a plugin is configured.

Response:
def dock_json_has_plugin_conf(self, plugin_type, plugin_name):
        """
        Check whether a plugin is configured.
        """

        try:
            self.dock_json_get_plugin_conf(plugin_type, plugin_name)
            return True
        except (KeyError, IndexError):
            return False


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

        import requests

        url_path = urlparse(self.__url).path
        try:
            url_extension = get_extension(url_path.rstrip("/"))
        except InvalidFilePathError:
            raise UrlError("url must include path")

        logger.debug("TableUrlLoaderFactory: extension={}".format(url_extension))

        loader_class = self._get_loader_class(self._get_extension_loader_mapping(), url_extension)

        try:
            self._fetch_source(loader_class)
        except requests.exceptions.ProxyError as e:
            raise ProxyError(e)

        loader = self._create_from_extension(url_extension)

        logger.debug("TableUrlLoaderFactory: loader={}".format(loader.format_name))

        return loader

Response: Create a file loader from the file extension to loading file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def create_from_format_name(self, format_name):

        import requests

        logger.debug("TableUrlLoaderFactory: name={}".format(format_name))

        loader_class = self._get_loader_class(self._get_format_name_loader_mapping(), format_name)

        try:
            self._fetch_source(loader_class)
        except requests.exceptions.ProxyError as e:
            raise ProxyError(e)

        loader = self._create_from_format_name(format_name)

        logger.debug("TableUrlLoaderFactory: loader={}".format(loader.format_name))

        return loader

Response: Create a file loader from a format name.


Instruction:
Can you implement a function in Python 3 that
returns a mapping of format - extension and loader class.

Response:
def _get_extension_loader_mapping(self):
        """
        :return: Mappings of format-extension and loader class.
        :rtype: dict
        """

        loader_table = self._get_common_loader_mapping()
        loader_table.update(
            {
                "asp": HtmlTableTextLoader,
                "aspx": HtmlTableTextLoader,
                "htm": HtmlTableTextLoader,
                "md": MarkdownTableTextLoader,
                "sqlite3": SqliteFileLoader,
                "xls": ExcelTableFileLoader,
                "xlsx": ExcelTableFileLoader,
            }
        )

        return loader_table


Instruction:
Can you implement a function in Python 3 that
returns a mapping of format - name and loader class.

Response:
def _get_format_name_loader_mapping(self):
        """
        :return: Mappings of format-name and loader class.
        :rtype: dict
        """

        loader_table = self._get_common_loader_mapping()
        loader_table.update(
            {
                "excel": ExcelTableFileLoader,
                "json_lines": JsonLinesTableTextLoader,
                "markdown": MarkdownTableTextLoader,
                "mediawiki": MediaWikiTableTextLoader,
                "ssv": CsvTableFileLoader,
            }
        )

        return loader_table


Instruction:
How would you explain what the following Python 3 function does
def get_container_image_ids(self):

        statuses = graceful_chain_get(self.json, "status", "containerStatuses")
        if statuses is None:
            return {}

        def remove_prefix(image_id, prefix):
            if image_id.startswith(prefix):
                return image_id[len(prefix):]

            return image_id

        return {status['image']: remove_prefix(status['imageID'], 'docker://')
                for status in statuses}

Response: Find the image IDs the containers use.


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

        reason_key = 'reason'
        cid_key = 'containerID'
        exit_key = 'exitCode'

        pod_status = self.json.get('status', {})
        statuses = pod_status.get('containerStatuses', [])

        # Find the first non-zero exit code from a container
        # and return its 'message' or 'reason' value
        for status in statuses:
            try:
                terminated = status['state']['terminated']
                exit_code = terminated['exitCode']
                if exit_code != 0:
                    reason_dict = {
                        exit_key: exit_code,
                    }

                    if 'containerID' in terminated:
                        reason_dict[cid_key] = terminated['containerID']

                    for key in ['message', 'reason']:
                        try:
                            reason_dict[reason_key] = terminated[key]
                            break
                        except KeyError:
                            continue
                    else:
                        # Both 'message' and 'reason' are missing
                        reason_dict[reason_key] = 'Exit code {code}'.format(
                            code=exit_code
                        )

                    return reason_dict
            except KeyError:
                continue

        # Failing that, return the 'message' or 'reason' value for the
        # pod
        for key in ['message', 'reason']:
            try:
                return {reason_key: pod_status[key]}
            except KeyError:
                continue

        return {reason_key: pod_status['phase']}

Response: Find the reason a pod failed and return the message or reason value for the next non - zero exit code.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_error_message(self):
        error_reason = self.get_error_reason()
        if error_reason:
            error_message = error_reason.get('pod') or None
            if error_message:
                return "Error in pod: %s" % error_message
            plugin = error_reason.get('plugin')[0] or None
            error_message = error_reason.get('plugin')[1] or None
            if error_message:
                # Plugin has non-empty error description
                return "Error in plugin %s: %s" % (plugin, error_message)
            else:
                return "Error in plugin %s" % plugin

Response: Return an error message based on atomic - actor s metadata.


Instruction:
Can you create a Python 3 function that
creates a file loader from the file extension.

Response:
def create_from_path(self):
        """
        Create a file loader from the file extension to loading file.
        Supported file extensions are as follows:

            ==========================  =======================================
            Extension                   Loader
            ==========================  =======================================
            ``"csv"``                   :py:class:`~.CsvTableFileLoader`
            ``"xls"``/``"xlsx"``        :py:class:`~.ExcelTableFileLoader`
            ``"htm"``/``"html"``        :py:class:`~.HtmlTableFileLoader`
            ``"json"``                  :py:class:`~.JsonTableFileLoader`
            ``"jsonl"``                 :py:class:`~.JsonLinesTableFileLoader`
            ``"ldjson"``                :py:class:`~.JsonLinesTableFileLoader`
            ``"ltsv"``                  :py:class:`~.LtsvTableFileLoader`
            ``"md"``                    :py:class:`~.MarkdownTableFileLoader`
            ``"ndjson"``                :py:class:`~.JsonLinesTableFileLoader`
            ``"sqlite"``/``"sqlite3"``  :py:class:`~.SqliteFileLoader`
            ``"tsv"``                   :py:class:`~.TsvTableFileLoader`
            ==========================  =======================================

        :return:
            Loader that coincides with the file extension of the
            :py:attr:`.file_extension`.
        :raises pytablereader.LoaderNotFoundError:
            |LoaderNotFoundError_desc| loading the file.
        """

        loader = self._create_from_extension(self.file_extension)

        logger.debug(
            "TableFileLoaderFactory.create_from_path: extension={}, loader={}".format(
                self.file_extension, loader.format_name
            )
        )

        return loader


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

        loader = self._create_from_format_name(format_name)

        logger.debug(
            "TableFileLoaderFactory.create_from_format_name: name={}, loader={}".format(
                format_name, loader.format_name
            )
        )

        return loader

Response: Create a file loader from a format name.


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

        loader_table = self._get_common_loader_mapping()
        loader_table.update(
            {
                "htm": HtmlTableFileLoader,
                "md": MarkdownTableFileLoader,
                "sqlite3": SqliteFileLoader,
                "xlsx": ExcelTableFileLoader,
                "xls": ExcelTableFileLoader,
            }
        )

        return loader_table

Response: Returns a mapping of format extension and loader class.


Instruction:
Make a summary of the following Python 3 code
def _get_format_name_loader_mapping(self):

        loader_table = self._get_common_loader_mapping()
        loader_table.update(
            {
                "excel": ExcelTableFileLoader,
                "json_lines": JsonLinesTableFileLoader,
                "markdown": MarkdownTableFileLoader,
                "mediawiki": MediaWikiTableFileLoader,
                "ssv": CsvTableFileLoader,
            }
        )

        return loader_table

Response: Returns a mapping of format name and loader class.


Instruction:
Write a Python 3 script to
load the table data from an Excel file.

Response:
def load(self):
        """
        Extract tabular data as |TableData| instances from an Excel file.
        |spreadsheet_load_desc|

        :return:
            Loaded |TableData| iterator.
            |TableData| created for each sheet in the workbook.
            |load_table_name_desc|

            ===================  ====================================
            Format specifier     Value after the replacement
            ===================  ====================================
            ``%(filename)s``     Filename of the workbook
            ``%(sheet)s``        Name of the sheet
            ``%(format_name)s``  ``"spreadsheet"``
            ``%(format_id)s``    |format_id_desc|
            ``%(global_id)s``    |global_id|
            ===================  ====================================
        :rtype: |TableData| iterator
        :raises pytablereader.DataError:
            If the header row is not found.
        :raises pytablereader.error.OpenError:
            If failed to open the source file.
        """

        import xlrd

        self._validate()
        self._logger.logging_load()

        try:
            workbook = xlrd.open_workbook(self.source)
        except xlrd.biffh.XLRDError as e:
            raise OpenError(e)

        for worksheet in workbook.sheets():
            self._worksheet = worksheet

            if self._is_empty_sheet():
                continue

            self.__extract_not_empty_col_idx()

            try:
                start_row_idx = self._get_start_row_idx()
            except DataError:
                continue

            rows = [
                self.__get_row_values(row_idx)
                for row_idx in range(start_row_idx + 1, self._row_count)
            ]

            self.inc_table_count()
            headers = self.__get_row_values(start_row_idx)

            yield TableData(
                self._make_table_name(),
                headers,
                rows,
                dp_extractor=self.dp_extractor,
                type_hints=self._extract_type_hints(headers),
            )


Instruction:
Make a summary of the following Python 3 code
def load(self):

        self._validate()
        self._logger.logging_load()
        self.encoding = get_file_encoding(self.source, self.encoding)

        self._ltsv_input_stream = io.open(self.source, "r", encoding=self.encoding)

        for data_matrix in self._to_data_matrix():
            formatter = SingleJsonTableConverterA(data_matrix)
            formatter.accept(self)

            return formatter.to_table_data()

Response: Loads the tabular data from a LTSV file.


Instruction:
Explain what the following Python 3 code does
def load(self):

        self._validate()
        self._logger.logging_load()

        self._ltsv_input_stream = self.source.splitlines()

        for data_matrix in self._to_data_matrix():
            formatter = SingleJsonTableConverterA(data_matrix)
            formatter.accept(self)

            return formatter.to_table_data()

Response: Loads the tabular data from the LTSV text file.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def totals(self):
        def agg(d):
            keys = ['g','a','p','pm','pn','pim','s','ab','ms','ht','gv','tk','bs']
            res = { k: 0 for k in keys }
            res['fo'] = { 'won': 0, 'total': 0 }
            for _, v in d.items():
                for k in keys:
                    res[k] += v[k]
                for fi in res['fo'].keys():
                    res['fo'][fi] += v['fo'][fi]
            return res

        return self.__apply_to_both(agg)

Response: Computes and returns a dictionary containing home and away by player shots and face - off by player shots and face - off.


Instruction:
Implement a function in Python 3 to
return the subset home and away players that satisfy the provided filter function.

Response:
def filter_players(self, pl_filter):
        """
        Return the subset home and away players that satisfy the provided filter function.

        :param pl_filter: function that takes a by player dictionary and returns bool
        :returns: dict of the form ``{ 'home/away': { by_player_dict } }``. See :py:func:`home_players` and :py:func:`away_players`
        """
        def each(d):
            return {
                k: v
                for k, v in d.items()
                if pl_filter(k, v)
            }

        return self.__apply_to_both(each)


Instruction:
How would you code a function in Python 3 to
return all home and away by player info sorted by either the provided key or function.

Response:
def sort_players(self, sort_key=None, sort_func=None, reverse=False):
        """
        Return all home and away by player info sorted by either the provided key or function. Must provide
        at least one of the two parameters. Can sort either ascending or descending.

        :param sort_key: (def None) dict key to sort on
        :param sort_func: (def None) sorting function
        :param reverse: (optional, def False) if True, sort descending
        :returns: dict of the form ``{ 'home/away': { by_player_dict } }``. See :py:func:`home_players` and :py:func:`away_players`
        """
        def each(d):
            t = [ ]
            for num, v in d.items():
                ti = { vk: vv for vk, vv in v.items() }
                ti['num'] = num
                t.append(ti)

            if sort_key:
                return sorted(t, key=lambda k: k[sort_key], reverse=reverse)
            else:
                return sorted(t, key=sort_func, reverse=reverse)

        return self.__apply_to_both(each)


Instruction:
Given the following Python 3 function, write the documentation
def top_by_key(self, sort_key):
        res = self.sort_players(sort_key=sort_key, reverse=True)
        return {
            'home': res['home'][0],
            'away': res['away'][0]
        }

Response: Return home and away by player info for each team in each category.


Instruction:
Write a Python 3 script for
returning home and away by player info for each team who come in first according to the sort_func. Will perform ascending sort.

Response:
def top_by_func(self, sort_func):
        """
        Return home/away by player info for the players on each team who come in first according to the
        provided sorting function. Will perform ascending sort.

        :param sort_func: function that yields the sorting quantity
        :returns: dict of the form ``{ 'home/away': { by_player_dict } }``. See :py:func:`home_players` and :py:func:`away_players`
        """
        res = self.sort_players(sort_func=sort_func, reverse=True)
        return {
            'home': res['home'][0],
            'away': res['away'][0]
        }


Instruction:
Can you tell what is the following Python 3 function doing
def encode(obj):
    if hasattr(obj, 'json'):
        return obj.json
    if hasattr(obj, '__json__'):
        return obj.__json__()
    return dumps(obj)

Response: Encode one argument or object to json


Instruction:
How would you explain what the following Python 3 function does
def encode_args(args, extra=False):
    if not args:
        return ''

    methodargs = ', '.join([encode(a) for a in args])
    if extra:
        methodargs += ', '

    return methodargs

Response: Encode a list of arguments into a single string.


Instruction:
Here you have a function in Python 3, explain what it does
def _send(self, javascript):

        # Prepend JS to switch to the proper client context.
        message = """
            var _ctx = ctx_switch('%s'),
                browser = _ctx[0],
                ELEMENTS = _ctx[1];
            %s
        """ % (id(self), javascript)

        response = self.connection.send(message)

        return self._handle_response(response)

Response: Sends a javascript string to the zombie. js server and returns the response.


Instruction:
How would you explain what the following Python 3 function does
def wait(self, method, *args):
        methodargs = encode_args(args, extra=True)
        js = """
        %s(%s wait_callback);
        """ % (method, methodargs)
        self._send(js)

Response: Call a method on the zombie. js Browser instance and wait on a callback.


Instruction:
Explain what the following Python 3 code does
def create_element(self, method, args=None):
        if args is None:
            arguments = ''
        else:
            arguments = "(%s)" % encode_args(args)
        js = """
            create_element(ELEMENTS, %(method)s%(args)s);
        """ % {
            'method': method,
            'args': arguments
        }

        index = self.json(js)
        if index is None:
            return None

        return Element(index)

Response: Evaluate a browser method and CSS selector against the document DOMNode and return a single Element object.


Instruction:
Can you tell what is the following Python 3 function doing
def create_elements(self, method, args=[]):
        args = encode_args(args)

        js = """
            create_elements(ELEMENTS, %(method)s(%(args)s))
        """ % {
            'method': method,
            'args': args,
        }

        indexes = self.json(js)
        return map(Element, indexes)

Response: Execute a browser method that will return a list of elements.


Instruction:
Implement a Python 3 function for
filling a specified form field in the current document.

Response:
def fill(self, field, value):
        """
        Fill a specified form field in the current document.

        :param field: an instance of :class:`zombie.dom.DOMNode`
        :param value: any string value
        :return: self to allow function chaining.
        """
        self.client.nowait('browser.fill', (field, value))
        return self


Instruction:
Can you create a Python 3 function that
evaluates a CSS selector against the document and return a single DOMNode object.

Response:
def query(self, selector, context=None):
        """
        Evaluate a CSS selector against the document (or an optional context
        :class:`zombie.dom.DOMNode`) and return a single
        :class:`zombie.dom.DOMNode` object.

        :param selector: a string CSS selector
                        (http://zombie.labnotes.org/selectors)
        :param context: an (optional) instance of :class:`zombie.dom.DOMNode`
        """
        element = self.client.create_element(
            'browser.query', (selector, context))
        return DOMNode.factory(element, self)


Instruction:
Here you have a function in Python 3, explain what it does
def queryAll(self, selector, context=None):
        elements = self.client.create_elements(
            'browser.queryAll', (selector, context))
        return [DOMNode(e, self) for e in elements]

Response: Evaluate a CSS selector against the document and return a list of DOMNodes.


Instruction:
Create a Python 3 function to
find and returns a link element with the given CSS selector.

Response:
def link(self, selector):
        """
        Finds and returns a link ``<a>`` element (:class:`zombie.dom.DOMNode`).
        You can use a CSS selector or find a link by its text contents (case
        sensitive, but ignores leading/trailing spaces).

        :param selector: an optional string CSS selector
                        (http://zombie.labnotes.org/selectors) or inner text
        """
        element = self.client.create_element('browser.link', (selector,))
        return DOMNode(element, self)


Instruction:
How would you implement a function in Python 3 that
sets the value of the current field.

Response:
def value(self, value):
        """
        Used to set the ``value`` of form elements.
        """
        self.client.nowait(
            'set_field', (Literal('browser'), self.element, value))


Instruction:
Given the following Python 3 function, write the documentation
def fire(self, event):
        self.browser.fire(self.element, event)
        return self

Response: Fires a specified DOM event on the current node.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_all(self):
        try:
            self.toi.load_all()
            self.rosters.load_all()
            #self.summary.load_all()
            self.play_by_play.load_all()
            self.face_off_comp.load_all()
            return self
        except Exception as e:
            print(e)
            return None

Response: Loads all reports in the system.


Instruction:
Create a Python 3 function for
returning the game meta information displayed in report banners including team names away team date and location and attendance.

Response:
def matchup(self):
        """
        Return the game meta information displayed in report banners including team names,
        final score, game date, location, and attendance. Data format is

        .. code:: python

            {
                'home': home,
                'away': away,
                'final': final,
                'attendance': att,
                'date': date,
                'location': loc
            }

        :returns: matchup banner info
        :rtype: dict
        """
        if self.play_by_play.matchup:
            return self.play_by_play.matchup
        elif self.rosters.matchup:
            return self.rosters.matchup
        elif self.toi.matchup:
            return self.toi.matchup
        else:
            self.face_off_comp.matchup


Instruction:
Here you have a function in Python 3, explain what it does
def _utf8_encode(self, d):
        for k, v in d.items():
            if isinstance(v, str):
                d[k] = v.encode('utf8').lower()
            if isinstance(v, list):
                for index,item in enumerate(v):
                    item = item.encode('utf8').lower()
                    v[index] = item
            if isinstance(v, dict):
                d[k] = self._utf8_encode(v)

        return d

Response: Ensures all values are encoded in UTF - 8 and converts them to lowercase


Instruction:
Can you generate the documentation for the following Python 3 function
def _bool_encode(self, d):
        for k, v in d.items():
            if isinstance(v, bool):
                d[k] = str(v).lower()

        return d

Response: Converts bool values to lowercase strings


Instruction:
Can you create a Python 3 function that
formats search parameters and values for use with API

Response:
def _options(self, **kwargs):
        """
        Formats search parameters/values for use with API

        :param \*\*kwargs: search parameters/values

        """
        def _format_fq(d):
            for k,v in d.items():
                if isinstance(v, list):
                    d[k] = ' '.join(map(lambda x: '"' + x + '"', v))
                else:
                    d[k] = '"' + v + '"'
            values = []
            for k,v in d.items():
                value = '%s:(%s)' % (k,v)
                values.append(value)
            values = ' AND '.join(values)
            return values

        kwargs = self._utf8_encode(kwargs)
        kwargs = self._bool_encode(kwargs)

        values = ''

        for k, v in kwargs.items():
            if k is 'fq' and isinstance(v, dict):
                v = _format_fq(v)
            elif isinstance(v, list):
                v = ','.join(v)
            values += '%s=%s&' % (k, v)

        return values


Instruction:
How would you implement a function in Python 3 that
calls the API and returns a dictionary of the search results

Response:
def search(self,
                response_format = None,
                key = None,
                **kwargs):
        """
        Calls the API and returns a dictionary of the search results

        :param response_format: the format that the API uses for its response,
                                includes JSON (.json) and JSONP (.jsonp).
                                Defaults to '.json'.

        :param key: a developer key. Defaults to key given when the articleAPI class was initialized.

        """
        if response_format is None:
            response_format = self.response_format
        if key is None:
            key = self.key

        url = '%s%s?%sapi-key=%s' % (
            API_ROOT, response_format, self._options(**kwargs), key
        )

        r = requests.get(url)
        return r.json()


Instruction:
Make a summary of the following Python 3 code
def parse(self):

    r = super(GameSummRep, self).parse()
    try:
      self.parse_scoring_summary()
      return r and False
    except:
      return False

Response: Fully parses game summary report.


Instruction:
Implement a function in Python 3 to
create an event object corresponding to the given event type.

Response:
def Create(event_type):
        """
        Factory method creates objects derived from :py:class`.Event` with class name matching the :py:class`.EventType`.

        :param event_type: number for type of event
        :returns: constructed event corresponding to ``event_type``
        :rtype: :py:class:`.Event`
        """
        if event_type in EventType.Name:
            # unknown event type gets base class
            if EventType.Name[event_type] == Event.__name__:
                return Event()
            else:
                # instantiate Event subclass with same name as EventType name
                return [t for t in EventFactory.event_list if t.__name__ == EventType.Name[event_type]][0]()
        else:
            raise TypeError("EventFactory.Create: Invalid EventType")


Instruction:
Write a Python 3 function that can
return a shift summary object for the home team

Response:
def home_shift_summ(self):
        """
        :returns: :py:class:`.ShiftSummary` by player for the home team
        :rtype: dict ``{ player_num: shift_summary_obj }``
        """
        if not self.__wrapped_home:
            self.__wrapped_home = self.__wrap(self._home.by_player)

        return self.__wrapped_home


Instruction:
How would you code a function in Python 3 to
return a dictionary of shift summary objects for the away team

Response:
def away_shift_summ(self):
        """
        :returns: :py:class:`.ShiftSummary` by player for the away team
        :rtype: dict ``{ player_num: shift_summary_obj }``
        """
        if not self.__wrapped_away:
            self.__wrapped_away = self.__wrap(self._away.by_player)

        return self.__wrapped_away


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

        try:
            return super(RosterRep, self).parse() \
                .parse_rosters() \
                .parse_scratches() \
                .parse_coaches() \
                .parse_officials()
        except:
            return None

Response: Retreive and parse Play by Play data for the given game key.


Instruction:
Write a Python 3 script to
parse the home and away game rosters and return self on success None on failure

Response:
def parse_rosters(self):
        """
        Parse the home and away game rosters

        :returns: ``self`` on success, ``None`` otherwise
        """
        lx_doc = self.html_doc()

        if not self.__blocks:
            self.__pl_blocks(lx_doc)

        for t in ['home', 'away']:
            self.rosters[t] = self.__clean_pl_block(self.__blocks[t])

        return self if self.rosters else None


Instruction:
Create a Python 3 function to
parse the home and away healthy scratches

Response:
def parse_scratches(self):
        """
        Parse the home and away healthy scratches

        :returns: ``self`` on success, ``None`` otherwise
        """
        lx_doc = self.html_doc()
        if not self.__blocks:
            self.__pl_blocks(lx_doc)

        for t in ['aw_scr', 'h_scr']:
            ix = 'away' if t == 'aw_scr' else 'home'
            self.scratches[ix] = self.__clean_pl_block(self.__blocks[t])

        return self if self.scratches else None


Instruction:
Implement a function in Python 3 to
parse the home and away coaches.

Response:
def parse_coaches(self):
        """
        Parse the home and away coaches

        :returns: ``self`` on success, ``None`` otherwise
        """
        lx_doc = self.html_doc()
        tr = lx_doc.xpath('//tr[@id="HeadCoaches"]')[0]

        for i, td in enumerate(tr):
            txt = td.xpath('.//text()')
            txt = ex_junk(txt, ['\n','\r'])
            team = 'away' if i == 0 else 'home'
            self.coaches[team] = txt[0]

        return self if self.coaches else None


Instruction:
Explain what the following Python 3 code does
def parse_officials(self):
        # begin proper body of method
        lx_doc = self.html_doc()
        off_parser = opm(self.game_key.season)
        self.officials = off_parser(lx_doc)

        return self if self.officials else None

Response: Parse the officials tag of the current object.


Instruction:
Write a Python 3 script for
validating an email address.

Response:
def is_email(address, check_dns=False, diagnose=False):
    """Validate an email address.

    Keyword arguments:
    address   --- the email address as a string
    check_dns --- flag for whether to check the DNS status of the domain
    diagnose  --- flag for whether to return True/False or a Diagnosis

    """

    threshold = BaseDiagnosis.CATEGORIES["THRESHOLD"]
    d = ParserValidator().is_email(address, True)
    if check_dns is True and d < BaseDiagnosis.CATEGORIES["DNSWARN"]:
        threshold = BaseDiagnosis.CATEGORIES["VALID"]
        d = max(d, DNSValidator().is_valid(address.split("@")[1], True))

    return d if diagnose else d < threshold


Instruction:
Here you have a function in Python 3, explain what it does
def dispatch_loader(scraper, loader_name):
    l = '.'.join([scraper, loader_name])
    def wrapper(f):
        @wraps(f)
        def wrapped(self, *f_args, **f_kwargs):
            if not hasattr(self, '_loaded'):
                self._loaded = { }

            already_loaded = self._loaded.setdefault(l, False)
            if not already_loaded:
                attr = getattr(self, scraper)
                self._loaded[l] = getattr(attr, loader_name)() is not None
            return f(self, *f_args, **f_kwargs)
        return wrapped
    return wrapper

Response: Decorator that dispatches a report loader to the specified scraper.


Instruction:
Implement a Python 3 function for
parsing shifts from TOI report and return a dictionary of shift - > dict.

Response:
def parse_shifts(self):
        """
        Parse shifts from TOI report

        :returns: self if successfule else None
        """

        lx_doc = self.html_doc()
        pl_heads = lx_doc.xpath('//td[contains(@class, "playerHeading")]')
        for pl in pl_heads:
            sh_sum = { }

            pl_text = pl.xpath('text()')[0]
            num_name = pl_text.replace(',','').split(' ')
            sh_sum['player_num'] = int(num_name[0]) if num_name[0].isdigit() else -1
            sh_sum['player_name'] = { 'first': num_name[2], 'last': num_name[1] }

            first_shift = pl.xpath('../following-sibling::tr')[1]
            sh_sum['shifts'], last_shift = self.__player_shifts(first_shift)

            while ('Per' not in last_shift.xpath('.//text()')):
                last_shift = last_shift.xpath('following-sibling::tr')[0]

            per_summ = last_shift.xpath('.//tr')[0]
            sh_sum['by_period'], last_sum = self.__get_by_per_summ(per_summ)

            self.by_player[sh_sum['player_num']] = sh_sum

        return self if self.by_player else None


Instruction:
Write a Python 3 script to
check whether a domain is a valid MX or A record.

Response:
def is_valid(self, domain, diagnose=False):

        """Check whether a domain has a valid MX or A record.

        Keyword arguments:
        domain   --- the domain to check
        diagnose --- flag to report a diagnosis or a boolean (default False)

        """

        return_status = [ValidDiagnosis()]
        dns_checked = False

        # http://tools.ietf.org/html/rfc5321#section-2.3.5
        #   Names that can be resolved to MX RRs or address (i.e., A or AAAA)
        #   RRs (as discussed in Section 5) are permitted, as are CNAME RRs
        #   whose targets can be resolved, in turn, to MX or address RRs.
        #
        # http://tools.ietf.org/html/rfc5321#section-5.1
        #   The lookup first attempts to locate an MX record associated with
        #   the name.  If a CNAME record is found, the resulting name is
        #   processed as if it were the initial name. ... If an empty list of
        #   MXs is returned, the address is treated as if it was associated
        #   with an implicit MX RR, with a preference of 0, pointing to that
        #   host.
        #
        # is_email() author's note: We will regard the existence of a CNAME to
        # be sufficient evidence of the domain's existence. For performance
        # reasons we will not repeat the DNS lookup for the CNAME's target, but
        # we will raise a warning because we didn't immediately find an MX
        # record.
        try:
            dns.resolver.query(domain, 'MX')
            dns_checked = True
        except (dns.resolver.NXDOMAIN, dns.name.NameTooLong):
            # Domain can't be found in DNS
            return_status.append(DNSDiagnosis('NO_RECORD'))

            # Since dns.resolver gives more information than the PHP analog, we
            # can say that TLDs that throw an NXDOMAIN or NameTooLong error
            # have been checked
            if len(domain.split('.')) == 1:
                dns_checked = True
        except dns.resolver.NoAnswer:
            # MX-record for domain can't be found
            return_status.append(DNSDiagnosis('NO_MX_RECORD'))

            try:
                # TODO: See if we can/need to narrow to A / CNAME
                dns.resolver.query(domain)
            except dns.resolver.NoAnswer:
                # No usable records for the domain can be found
                return_status.append(DNSDiagnosis('NO_RECORD'))
        except dns.resolver.NoNameservers:
            return_status.append(DNSDiagnosis('NO_NAMESERVERS'))
        except (dns.exception.Timeout, dns.resolver.Timeout):
            return_status.append(DNSDiagnosis('DNS_TIMEDOUT'))

        # Check for TLD addresses
        # -----------------------
        # TLD addresses are specifically allowed in RFC 5321 but they are
        # unusual to say the least. We will allocate a separate
        # status to these addresses on the basis that they are more likely
        # to be typos than genuine addresses (unless we've already
        # established that the domain does have an MX record)
        #
        # http://tools.ietf.org/html/rfc5321#section-2.3.5
        #   In the case of a top-level domain used by itself in an address, a
        #   single string is used without any dots. This makes the requirement,
        #   described in more detail below, that only fully-qualified domain
        #   names appear in SMTP transactions on the public Internet,
        #   particularly important where top-level domains are involved.
        #
        # TLD format
        # ----------
        # The format of TLDs has changed a number of times. The standards
        # used by IANA have been largely ignored by ICANN, leading to
        # confusion over the standards being followed. These are not defined
        # anywhere, except as a general component of a DNS host name (a label).
        # However, this could potentially lead to 123.123.123.123 being a
        # valid DNS name (rather than an IP address) and thereby creating
        # an ambiguity. The most authoritative statement on TLD formats that
        # the author can find is in a (rejected!) erratum to RFC 1123
        # submitted by John Klensin, the author of RFC 5321:
        #
        # http://www.rfc-editor.org/errata_search.php?rfc=1123&eid=1353
        #   However, a valid host name can never have the dotted-decimal
        #   form #.#.#.#, since this change does not permit the highest-level
        #   component label to start with a digit even if it is not
        #   all-numeric.
        if not dns_checked:
            atom_list = domain.split(".")
            if len(atom_list) == 1:
                return_status.append(RFC5321Diagnosis('TLD'))

            try:
                float(atom_list[len(atom_list)-1][0])
                return_status.append(RFC5321Diagnosis('TLDNUMERIC'))
            except ValueError:
                pass

        final_status = max(return_status)

        return final_status if diagnose else final_status == ValidDiagnosis()


Instruction:
Explain what the following Python 3 code does
def html_doc(self):

        if self.__lx_doc is None:
            cn = NHLCn()

            if hasattr(cn, self.report_type):
                html = getattr(cn, self.report_type)(self.game_key)
            else:
                raise ValueError('Invalid report type: %s' % self.report_type)

            if cn.req_err is None:
                self.__lx_doc = fromstring(html)
            else:
                self.req_err = cn.req_err

        return self.__lx_doc

Response: returns the lxml processed html document


Instruction:
Create a Python 3 function for
parsing the banner matchup meta info for the game.

Response:
def parse_matchup(self):
        """
        Parse the banner matchup meta info for the game.

        :returns: ``self`` on success or ``None``
        """
        lx_doc = self.html_doc()
        try:
            if not self.matchup:
                self.matchup = self._fill_meta(lx_doc)
            return self
        except:
            return None


Instruction:
Can you implement a function in Python 3 that
generates and yield a stream of parsed plays. Useful for per play processing.

Response:
def parse_plays_stream(self):
        """Generate and yield a stream of parsed plays. Useful for per play processing."""

        lx_doc = self.html_doc()
        if lx_doc is not None:
            parser = PlayParser(self.game_key.season, self.game_key.game_type)
            plays = lx_doc.xpath('//tr[@class = "evenColor"]')
            for p in plays:
                p_obj = parser.build_play(p)
                self.plays.append(p_obj)

                yield p_obj


Instruction:
Can you generate the documentation for the following Python 3 function
def ColMap(season):
        if c.MIN_SEASON <= season <= c.MAX_SEASON:
            return {
                "play_num": 0,
                "per": 1,
                "str": 2,
                "time": 3,
                "event": 4,
                "desc": 5,
                "vis": 6,
                "home": 7
            }
        else:
            raise ValueError("RTSSCol.MAP(season): Invalid season " + str(season))

Response: Returns a dictionary mapping the type of information in the RTSS play row to the appropriate column number.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def build_play(self, pbp_row):
        d = pbp_row.findall('./td')
        c = PlayParser.ColMap(self.season)

        p = { }
        to_dig = lambda t: int(t) if t.isdigit() else 0
        p['play_num'] = to_int(d[c["play_num"]].text, 0)
        p['period'] = to_int(d[c["per"]].text, 0)

        p['strength'] = self.__strength(d[c["str"]].text)

        time = d[c["time"]].text.split(":")
        p['time'] = { "min": int(time[0]), "sec": int(time[1]) }

        skater_tab = d[c["vis"]].xpath("./table")
        p['vis_on_ice'] = self.__skaters(skater_tab[0][0]) if len(skater_tab) else { }

        skater_tab = d[c["home"]].xpath("./table")
        p['home_on_ice'] = self.__skaters(skater_tab[0][0]) if len(skater_tab) else { }

        p['event'] = event_type_mapper(
            d[c["event"]].text,
            period=p['period'],
            skater_ct=len(p['vis_on_ice']) + len(p['home_on_ice']),
            game_type=self.game_type
        )
        p['event'].desc = " ".join([t.encode('ascii', 'replace').decode('utf-8') for t in d[c["desc"]].xpath("text()")])

        parse_event_desc(p['event'], season=self.season)

        return p

Response: Parses the RTSS table row and returns a dict of the data.


Instruction:
Write a Python 3 script for
constructing a dictionary of players on the ice in the provided table at the time of play.

Response:
def __skaters(self, tab):
        """
        Constructs dictionary of players on the ice in the provided table at time of play.
        :param tab: RTSS table of the skaters and goalie on at the time of the play
        :rtype: dictionary, key = player number, value = [position, name]
        """

        res = { }
        for td in tab.iterchildren():
            if len(td):
                pl_data = td.xpath("./table/tr")
                pl = pl_data[0].xpath("./td/font")

                if pl[0].text.isdigit():
                    res[int(pl[0].text)] = [s.strip() for s in pl[0].get("title").split("-")][::-1]

                s = pl[0].get("title").split("-")
                pos = pl_data[1].getchildren()[0].text

        return res


Instruction:
Given the following Python 3 function, write the documentation
def exclude_from(l, containing = [], equal_to = []):

    cont = lambda li: any(c in li for c in containing)
    eq = lambda li: any(e == li for e in equal_to)
    return [li for li in l if not (cont(li) or eq(li))]

Response: Exclude elements in list l containing any elements from list l ex.


Instruction:
Here you have a function in Python 3, explain what it does
def calc_surfdist(surface, labels, annot, reg, origin, target):
  import nibabel as nib
  import numpy as np
  import os
  from surfdist import load, utils, surfdist
  import csv

  """ inputs:
  surface - surface file (e.g. lh.pial, with full path)
  labels - label file (e.g. lh.cortex.label, with full path)
  annot - annot file (e.g. lh.aparc.a2009s.annot, with full path)
  reg - registration file (lh.sphere.reg)
  origin - the label from which we calculate distances
  target - target surface (e.g. fsaverage4)
  """

  # Load stuff
  surf = nib.freesurfer.read_geometry(surface)
  cort = np.sort(nib.freesurfer.read_label(labels))
  src  = load.load_freesurfer_label(annot, origin, cort)

  # Calculate distances
  dist = surfdist.dist_calc(surf, cort, src)

  # Project distances to target
  trg = nib.freesurfer.read_geometry(target)[0]
  native = nib.freesurfer.read_geometry(reg)[0]
  idx_trg_to_native = utils.find_node_match(trg, native)[0]

  # Get indices in trg space
  distt = dist[idx_trg_to_native]

  # Write to file and return file handle
  filename = os.path.join(os.getcwd(),'distances.csv')
  distt.tofile(filename,sep=",")

  return filename

Response: Calculate distances between a surface and a registration.


Instruction:
Make a summary of the following Python 3 code
def stack_files(files, hemi, source, target):
  import csv
  import os
  import numpy as np

  fname = "sdist_%s_%s_%s.csv" % (hemi, source, target)
  filename = os.path.join(os.getcwd(),fname)

  alldist = []

  for dfile in files:
    alldist.append(np.genfromtxt(dfile, delimiter=','))

  alldist = np.array(alldist)
  alldist.tofile(filename,",")

  return filename

Response: This function takes a list of files as input and vstacks them together.


Instruction:
Can you create a Python 3 function that
gets short URL of blog post.

Response:
def get_short_url(self, obj):
        """
        Get short URL of blog post like '/blog/<slug>/' using ``get_absolute_url`` if available.
        Removes dependency on reverse URLs of Mezzanine views when deploying Mezzanine only as an API backend.
        """
        try:
            url = obj.get_absolute_url()
        except NoReverseMatch:
            url = '/blog/' + obj.slug
        return url


Instruction:
How would you explain what the following Python 3 function does
def head_to_head(self, home_num, away_num):
        if home_num in self.home_fo and away_num in self.home_fo[home_num]['opps']:
            h_fo = self.home_fo[home_num]['opps'][away_num]
            a_fo = self.away_fo[away_num]['opps'][home_num]
            return {
                'home': { k: h_fo[k] for k in self.__zones },
                'away': { k: a_fo[k] for k in self.__zones }
            }
        else:
            return { }

Response: Return the head - to - head face - off outcomes between two players.