Instruction:
Make a summary of the following Python 3 code
def resolve_artifacts_by_builder_compat(
            self, package_names, builder_name, dependencies=False):
        """
        Yield the list of paths to the artifacts in the order of the
        dependency resolution

        Arguments:

        package_names
            The names of the packages to probe the dependency graph, to
            be provided as a list of strings.
        artifact_name
            The exact name of the artifact.
        dependencies
            Trace dependencies.  Default is off.

        Returns the path of where the artifact should be if it has been
        declared, otherwise None.
        """

        paths = self.compat_builders.get(builder_name)
        if not paths:
            # perhaps warn, but just return
            return

        resolver = (
            # traces dependencies for distribution.
            find_packages_requirements_dists
            if dependencies else
            # just get grabs the distribution.
            pkg_names_to_dists
        )
        for distribution in resolver(package_names):
            path = paths.get(distribution.project_name)
            if path:
                yield path

Response: Yields the list of paths to the artifacts that are needed for the given packages.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_artifact_metadata(self, package_name):

        filename = self.metadata.get(package_name)
        if not filename or not exists(filename):
            return {}
        with open(filename, encoding='utf8') as fd:
            contents = fd.read()

        try:
            is_json_compat(contents)
        except ValueError:
            logger.info("artifact metadata file '%s' is invalid", filename)
            return {}

        return json.loads(contents)

Response: Get the artifact metadata for the given package name.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_metadata_entry(self, entry_point, toolchain, spec):

        export_target = spec['export_target']
        toolchain_bases = trace_toolchain(toolchain)
        toolchain_bin_path = spec.get(TOOLCHAIN_BIN_PATH)
        toolchain_bin = ([
            basename(toolchain_bin_path),  # bin_name
            get_bin_version_str(toolchain_bin_path),  # bin_version
        ] if toolchain_bin_path else [])

        return {basename(export_target): {
            'toolchain_bases': toolchain_bases,
            'toolchain_bin': toolchain_bin,
            'builder': '%s:%s' % (
                entry_point.module_name, '.'.join(entry_point.attrs)),
        }}

Response: Generate the metadata entry for the entry_point.


Instruction:
Implement a Python 3 function for
iterating records for a specific package.

Response:
def iter_records_for(self, package_name):
        """
        Iterate records for a specific package.
        """

        entry_points = self.packages.get(package_name, NotImplemented)
        if entry_points is NotImplemented:
            logger.debug(
                "package '%s' has not declared any entry points for the '%s' "
                "registry for artifact construction",
                package_name, self.registry_name,
            )
            return iter([])

        logger.debug(
            "package '%s' has declared %d entry points for the '%s' "
            "registry for artifact construction",
            package_name, len(entry_points), self.registry_name,
        )
        return iter(entry_points.values())


Instruction:
Given the following Python 3 function, write the documentation
def generate_builder(self, entry_point, export_target):

        try:
            builder = entry_point.resolve()
        except ImportError:
            logger.error(
                "unable to import the target builder for the entry point "
                "'%s' from package '%s' to generate artifact '%s'",
                entry_point, entry_point.dist, export_target,
            )
            return

        if not self.verify_builder(builder):
            logger.error(
                "the builder referenced by the entry point '%s' "
                "from package '%s' has an incompatible signature",
                entry_point, entry_point.dist,
            )
            return

        # CLEANUP see deprecation notice below
        verifier = self.verify_export_target(export_target)
        if not verifier:
            logger.error(
                "the export target '%s' has been rejected", export_target)
            return

        toolchain, spec = self.extract_builder_result(builder(
            [entry_point.dist.project_name], export_target=export_target))
        if not toolchain:
            logger.error(
                "the builder referenced by the entry point '%s' "
                "from package '%s' failed to produce a valid "
                "toolchain",
                entry_point, entry_point.dist,
            )
            return

        if spec.get(EXPORT_TARGET) != export_target:
            logger.error(
                "the builder referenced by the entry point '%s' "
                "from package '%s' failed to produce a spec with the "
                "expected export_target",
                entry_point, entry_point.dist,
            )
            return

        if callable(verifier):
            warnings.warn(
                "%s:%s.verify_export_target returned a callable, which "
                "will no longer be passed to spec.advise by calmjs-4.0.0; "
                "please instead override 'setup_export_location' or "
                "'prepare_export_location' in that class" % (
                    self.__class__.__module__, self.__class__.__name__),
                DeprecationWarning
            )
            spec.advise(BEFORE_PREPARE, verifier, export_target)
        else:
            spec.advise(
                BEFORE_PREPARE,
                self.prepare_export_location, export_target)
        yield entry_point, toolchain, spec

Response: Yields exactly one builder for the provided entry point and export target.


Instruction:
Given the following Python 3 function, write the documentation
def execute_builder(self, entry_point, toolchain, spec):

        toolchain(spec)
        if not exists(spec['export_target']):
            logger.error(
                "the entry point '%s' from package '%s' failed to "
                "generate an artifact at '%s'",
                entry_point, entry_point.dist, spec['export_target']
            )
            return {}
        return self.generate_metadata_entry(entry_point, toolchain, spec)

Response: Executes the builder and returns the result.


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

        metadata = super(ArtifactRegistry, self).process_package(package_name)
        if metadata:
            self.update_artifact_metadata(package_name, metadata)

Response: Process the given package and update the artifact metadata.


Instruction:
Create a Python 3 function to
create a grammar for an alphanumeric variable size.

Response:
def alphanum_variable(min_size, max_size, name=None):
    """
    Creates the grammar for an alphanumeric code where the size ranges between
    two values.

    :param min_size: minimum size
    :param max_size: maximum size
    :param name: name for the field
    :return: grammar for an alphanumeric field of a variable size
    """

    if name is None:
        name = 'Alphanumeric Field'

    if min_size < 0:
        # Can't have negative min
        raise BaseException()
    if max_size < min_size:
        # Max can't be lower than min
        raise BaseException()

    field = pp.Word(pp.alphanums, min=min_size, max=max_size)

    # Parse action
    field.setParseAction(lambda s: s[0].strip())

    # White spaces are not removed
    field.leaveWhitespace()

    # Name
    field.setName(name)

    return field


Instruction:
Make a summary of the following Python 3 code
def year(columns, name=None):

    if columns < 0:
        # Can't have negative size
        raise BaseException()

    field = numeric(columns, name)

    # Parse action
    field.addParseAction(_to_year)

    return field

Response: Returns a grammar for a field containing a year.


Instruction:
How would you code a function in Python 3 to
check that the value is a JSON decodable string or a dict that can be serialized into a JSON.

Response:
def is_json_compat(value):
    """
    Check that the value is either a JSON decodable string or a dict
    that can be encoded into a JSON.

    Raises ValueError when validation fails.
    """

    try:
        value = json.loads(value)
    except ValueError as e:
        raise ValueError('JSON decoding error: ' + str(e))
    except TypeError:
        # Check that the value can be serialized back into json.
        try:
            json.dumps(value)
        except TypeError as e:
            raise ValueError(
                'must be a JSON serializable object: ' + str(e))

    if not isinstance(value, dict):
        raise ValueError(
            'must be specified as a JSON serializable dict or a '
            'JSON deserializable string'
        )

    return True


Instruction:
Here you have a function in Python 3, explain what it does
def validate_json_field(dist, attr, value):

    try:
        is_json_compat(value)
    except ValueError as e:
        raise DistutilsSetupError("%r %s" % (attr, e))

    return True

Response: Check if json field is valid.


Instruction:
Create a Python 3 function for
validating that the value is a list of valid identifiers

Response:
def validate_line_list(dist, attr, value):
    """
    Validate that the value is compatible
    """

    # does not work as reliably in Python 2.
    if isinstance(value, str):
        value = value.split()
    value = list(value)

    try:
        check = (' '.join(value)).split()
        if check == value:
            return True
    except Exception:
        pass
    raise DistutilsSetupError("%r must be a list of valid identifiers" % attr)


Instruction:
Explain what the following Python 3 code does
def write_json_file(argname, cmd, basename, filename):

    value = getattr(cmd.distribution, argname, None)

    if isinstance(value, dict):
        value = json.dumps(
            value, indent=4, sort_keys=True, separators=(',', ': '))

    cmd.write_or_delete_file(argname, filename, value, force=True)

Response: Writes a JSON file from the package s distribution directory using the specified filename.


Instruction:
Make a summary of the following Python 3 code
def write_line_list(argname, cmd, basename, filename):

    values = getattr(cmd.distribution, argname, None)
    if isinstance(values, list):
        values = '\n'.join(values)
    cmd.write_or_delete_file(argname, filename, values, force=True)

Response: Write out the retrieved value as list of lines.


Instruction:
Can you write a function in Python 3 where it
locates a package s distribution by its name.

Response:
def find_pkg_dist(pkg_name, working_set=None):
    """
    Locate a package's distribution by its name.
    """

    working_set = working_set or default_working_set
    req = Requirement.parse(pkg_name)
    return working_set.find(req)


Instruction:
Can you implement a function in Python 3 that
converts package names which can be a string of a number of package names or requirements separated by spaces.

Response:
def convert_package_names(package_names):
    """
    Convert package names, which can be a string of a number of package
    names or requirements separated by spaces.
    """

    results = []
    errors = []

    for name in (
            package_names.split()
            if hasattr(package_names, 'split') else package_names):
        try:
            Requirement.parse(name)
        except ValueError:
            errors.append(name)
        else:
            results.append(name)

    return results, errors


Instruction:
Given the following Python 3 function, write the documentation
def find_packages_requirements_dists(pkg_names, working_set=None):

    working_set = working_set or default_working_set
    requirements = [
        r for r in (Requirement.parse(req) for req in pkg_names)
        if working_set.find(r)
    ]
    return list(reversed(working_set.resolve(requirements)))

Response: Return the entire list of dependency requirements for the given packages.


Instruction:
Implement a function in Python 3 to
leverage the find_packages_requirements_dists but strip out the packages that are not in pkg_names.

Response:
def find_packages_parents_requirements_dists(pkg_names, working_set=None):
    """
    Leverages the `find_packages_requirements_dists` but strip out the
    distributions that matches pkg_names.
    """

    dists = []
    # opting for a naive implementation
    targets = set(pkg_names)
    for dist in find_packages_requirements_dists(pkg_names, working_set):
        if dist.project_name in targets:
            continue
        dists.append(dist)
    return dists


Instruction:
Explain what the following Python 3 code does
def read_dist_egginfo_json(dist, filename=DEFAULT_JSON):

    # use the given package's distribution to acquire the json file.
    if not dist.has_metadata(filename):
        logger.debug("no '%s' for '%s'", filename, dist)
        return

    try:
        result = dist.get_metadata(filename)
    except IOError:
        logger.error("I/O error on reading of '%s' for '%s'.", filename, dist)
        return

    try:
        obj = json.loads(result)
    except (TypeError, ValueError):
        logger.error(
            "the '%s' found in '%s' is not a valid json.", filename, dist)
        return

    logger.debug("found '%s' for '%s'.", filename, dist)
    return obj

Response: Read a json file within an egginfo distribution.


Instruction:
Can you implement a function in Python 3 that
reads json from egginfo of a package identified by pkg_name.

Response:
def read_egginfo_json(pkg_name, filename=DEFAULT_JSON, working_set=None):
    """
    Read json from egginfo of a package identified by `pkg_name` that's
    already installed within the current Python environment.
    """

    working_set = working_set or default_working_set
    dist = find_pkg_dist(pkg_name, working_set=working_set)
    return read_dist_egginfo_json(dist, filename)


Instruction:
Write a Python 3 function for
flattening a distribution s egginfo json with the depended keys to be flattened.

Response:
def flatten_dist_egginfo_json(
        source_dists, filename=DEFAULT_JSON, dep_keys=DEP_KEYS,
        working_set=None):
    """
    Flatten a distribution's egginfo json, with the depended keys to be
    flattened.

    Originally this was done for this:

    Resolve a distribution's (dev)dependencies through the working set
    and generate a flattened version package.json, returned as a dict,
    from the resolved distributions.

    Default working set is the one from pkg_resources.

    The generated package.json dict is done by grabbing all package.json
    metadata from all parent Python packages, starting from the highest
    level and down to the lowest.  The current distribution's
    dependencies will be layered on top along with its other package
    information.  This has the effect of child packages overriding
    node/npm dependencies which is by the design of this function.  If
    nested dependencies are desired, just rely on npm only for all
    dependency management.

    Flat is better than nested.
    """

    working_set = working_set or default_working_set
    obj = {}

    # TODO figure out the best way to explicitly report back to caller
    # how the keys came to be (from which dist).  Perhaps create a
    # detailed function based on this, retain this one to return the
    # distilled results.

    depends = {dep: {} for dep in dep_keys}

    # Go from the earliest package down to the latest one, as we will
    # flatten children's d(evD)ependencies on top of parent's.
    for dist in source_dists:
        obj = read_dist_egginfo_json(dist, filename)
        if not obj:
            continue

        logger.debug("merging '%s' for required '%s'", filename, dist)
        for dep in dep_keys:
            depends[dep].update(obj.get(dep, {}))

    if obj is None:
        # top level object does not have egg-info defined
        return depends

    for dep in dep_keys:
        # filtering out all the nulls.
        obj[dep] = {k: v for k, v in depends[dep].items() if v is not None}

    return obj


Instruction:
Can you generate the documentation for the following Python 3 function
def flatten_egginfo_json(
        pkg_names, filename=DEFAULT_JSON, dep_keys=DEP_KEYS, working_set=None):
    """
    A shorthand calling convention where the package name is supplied
    instead of a distribution.

    Originally written for this:

    Generate a flattened package.json with packages `pkg_names` that are
    already installed within the current Python environment (defaults
    to the current global working_set which should have been set up
    correctly by pkg_resources).
    """

    working_set = working_set or default_working_set
    # Ensure only grabbing packages that exists in working_set
    dists = find_packages_requirements_dists(
        pkg_names, working_set=working_set)
    return flatten_dist_egginfo_json(
        dists, filename=filename, dep_keys=dep_keys, working_set=working_set)

Response: Generates a flattened package. json with packages pkg_names and dep_keys.


Instruction:
Here you have a function in Python 3, explain what it does
def build_helpers_egginfo_json(
        json_field, json_key_registry, json_filename=None):
    """
    Return a tuple of functions that will provide the usage of the
    JSON egginfo based around the provided field.
    """

    json_filename = (
        json_field + '.json' if json_filename is None else json_filename)

    # Default calmjs core implementation specific functions, to be used by
    # integrators intended to use this as a distribution.

    def get_extras_json(pkg_names, working_set=None):
        """
        Only extract the extras_json information for the given packages
        'pkg_names'.
        """

        working_set = working_set or default_working_set
        dep_keys = set(get(json_key_registry).iter_records())
        dists = pkg_names_to_dists(pkg_names, working_set=working_set)
        return flatten_dist_egginfo_json(
            dists, filename=json_filename,
            dep_keys=dep_keys, working_set=working_set
        )

    def _flatten_extras_json(pkg_names, find_dists, working_set):
        # registry key must be explicit here as it was designed for this.
        dep_keys = set(get(json_key_registry).iter_records())
        dists = find_dists(pkg_names, working_set=working_set)
        return flatten_dist_egginfo_json(
            dists, filename=json_filename,
            dep_keys=dep_keys, working_set=working_set
        )

    def flatten_extras_json(pkg_names, working_set=None):
        """
        Traverses through the dependency graph of packages 'pkg_names'
        and flattens all the egg_info json information
        """

        working_set = working_set or default_working_set
        return _flatten_extras_json(
            pkg_names, find_packages_requirements_dists, working_set)

    def flatten_parents_extras_json(pkg_names, working_set=None):
        """
        Traverses through the dependency graph of packages 'pkg_names'
        and flattens all the egg_info json information for parents of
        the specified packages.
        """

        working_set = working_set or default_working_set
        return _flatten_extras_json(
            pkg_names, find_packages_parents_requirements_dists, working_set)

    write_extras_json = partial(write_json_file, json_field)

    return (
        get_extras_json,
        flatten_extras_json,
        flatten_parents_extras_json,
        write_extras_json,
    )

Response: Build a tuple of functions that will provide the usage of the JSON egginfo based around the provided field.


Instruction:
Write a Python 3 script to
return a tuple of functions that will provide the functions that will provide the relevant sets of module registry records based on the given packages.

Response:
def build_helpers_module_registry_dependencies(registry_name='calmjs.module'):
    """
    Return a tuple of funtions that will provide the functions that
    return the relevant sets of module registry records based on the
    dependencies defined for the provided packages.
    """

    def get_module_registry_dependencies(
            pkg_names, registry_name=registry_name, working_set=None):
        """
        Get dependencies for the given package names from module
        registry identified by registry name.

        For the given packages 'pkg_names' and the registry identified
        by 'registry_name', resolve the exported location for just the
        package.
        """

        working_set = working_set or default_working_set
        registry = get(registry_name)
        if not isinstance(registry, BaseModuleRegistry):
            return {}
        result = {}
        for pkg_name in pkg_names:
            result.update(registry.get_records_for_package(pkg_name))
        return result

    def _flatten_module_registry_dependencies(
            pkg_names, registry_name, find_dists, working_set):
        """
        Flatten dependencies for the given package names from module
        registry identified by registry name using the find_dists
        function on the given working_set.

        For the given packages 'pkg_names' and the registry identified
        by 'registry_name', resolve and flatten all the exported
        locations.
        """

        result = {}
        registry = get(registry_name)
        if not isinstance(registry, BaseModuleRegistry):
            return result

        dists = find_dists(pkg_names, working_set=working_set)
        for dist in dists:
            result.update(registry.get_records_for_package(dist.project_name))

        return result

    def flatten_module_registry_dependencies(
            pkg_names, registry_name=registry_name, working_set=None):
        """
        Flatten dependencies for the specified packages from the module
        registry identified by registry name.

        For the given packages 'pkg_names' and the registry identified
        by 'registry_name', resolve and flatten all the exported
        locations.
        """

        working_set = working_set or default_working_set
        return _flatten_module_registry_dependencies(
            pkg_names, registry_name, find_packages_requirements_dists,
            working_set)

    def flatten_parents_module_registry_dependencies(
            pkg_names, registry_name=registry_name, working_set=None):
        """
        Flatten dependencies for the parents of the specified packages
        from the module registry identified by registry name.

        For the given packages 'pkg_names' and the registry identified
        by 'registry_name', resolve and flatten all the exported
        locations.
        """

        working_set = working_set or default_working_set
        return _flatten_module_registry_dependencies(
            pkg_names, registry_name, find_packages_parents_requirements_dists,
            working_set)

    return (
        get_module_registry_dependencies,
        flatten_module_registry_dependencies,
        flatten_parents_module_registry_dependencies,
    )


Instruction:
Can you tell what is the following Python 3 function doing
def has_calmjs_artifact_declarations(cmd, registry_name='calmjs.artifacts'):

    return any(get(registry_name).iter_records_for(
        cmd.distribution.get_name()))

Response: Check that the artifact build step is there.


Instruction:
Can you tell what is the following Python 3 function doing
def build_calmjs_artifacts(dist, key, value, cmdclass=BuildCommand):

    if value is not True:
        return

    build_cmd = dist.get_command_obj('build')
    if not isinstance(build_cmd, cmdclass):
        logger.error(
            "'build' command in Distribution is not an instance of "
            "'%s:%s' (got %r instead)",
            cmdclass.__module__, cmdclass.__name__, build_cmd)
        return

    build_cmd.sub_commands.append((key, has_calmjs_artifact_declarations))

Response: Trigger the artifact build process through the setuptools.


Instruction:
Explain what the following Python 3 code does
def get_rule(self, field_id):

        if field_id in self._fields:
            # Field already exists
            field = self._fields[field_id]
        else:
            # Field does not exist
            # It is created
            field = self._create_field(field_id)

            # Field is saved
            self._fields[field_id] = field

        return field

Response: Returns the rule for the given field_id.


Instruction:
Can you generate the documentation for the following Python 3 function
def _create_field(self, field_id):
        # Field configuration info
        config = self._field_configs[field_id]

        adapter = self._adapters[config['type']]

        if 'name' in config:
            name = config['name']
        else:
            name = None

        if 'size' in config:
            columns = config['size']
        else:
            columns = None

        if 'values' in config:
            values = config['values']
        else:
            values = None

        field = adapter.get_field(name, columns, values)

        if 'results_name' in config:
            field = field.setResultsName(config['results_name'])
        else:
            field = field.setResultsName(field_id)

        return field

Response: Creates the basic rule for the specified field.


Instruction:
Here you have a function in Python 3, explain what it does
def read_csv_file(self, file_name):
        result = []
        with open(os.path.join(self.__path(), os.path.basename(file_name)),
                  'rt') as csvfile:
            headers_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
            for type_row in headers_reader:
                for t in type_row:
                    result.append(t)
        return result

Response: Reads a CSV file into a list of the contents.


Instruction:
How would you implement a function in Python 3 that
reads a YAML file into a matrix.

Response:
def read_yaml_file(self, file_name):
        """
        Parses a YAML file into a matrix.

        :param file_name: name of the YAML file
        :return: a matrix with the file's contents
        """
        with open(os.path.join(self.__path(), os.path.basename(file_name)),
                  'rt') as yamlfile:
            return yaml.load(yamlfile)


Instruction:
Create a Python 3 function for
reading the data from the table identified by the id.

Response:
def get_data(self, file_id):
        """
        Acquires the data from the table identified by the id.

        The file is read only once, consecutive calls to this method will
        return the sale collection.

        :param file_id: identifier for the table
        :return: all the values from the table
        """
        if file_id not in self._file_values:
            file_contents = 'cwr_%s.csv' % file_id
            self._file_values[file_id] = self._reader.read_csv_file(
                file_contents)

        return self._file_values[file_id]


Instruction:
Here you have a function in Python 3, explain what it does
def record_type(values):
    field = basic.lookup(values, name='Record Type (one of %s)' % values)

    return field.setResultsName('record_type')

Response: Returns a grammar for the record type field.


Instruction:
Can you generate the documentation for the following Python 3 function
def record_prefix(required_type, factory):
    field = record_type(required_type)
    field += factory.get_rule('transaction_sequence_n')
    field += factory.get_rule('record_sequence_n')

    # field.leaveWhitespace()

    return field

Response: Creates a record prefix for the specified record type.


Instruction:
How would you implement a function in Python 3 that
searches and return entity or sub entity that contain value of this field.

Response:
def expand_entity(self, entity):
        """
        Search and return entity or sub entity that contain value of this field.
        :param entity:
        :return: entity
        :raise KeyError
        """
        if self.name in entity:
            return entity
        for key, value in entity.items():
            if isinstance(value, dict):
                if self.name in value:
                    return value
        raise KeyError("The field %s (%s) not found in %s" % (self.name, self._rule['type'], entity))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def encode(self, entity):
        entity = self.expand_entity(entity)
        value = entity[self.name]
        result = self.format(value)
        return result

Response:
Encode this
        :param entity:
        :return: cwr string


Instruction:
Write a Python 3 script to
read a CWR grammar config file.

Response:
def read_config_file(self, file_name):
        """
        Reads a CWR grammar config file.

        :param file_name: name of the text file
        :return: the file's contents
        """
        with open(os.path.join(self.__path(), os.path.basename(file_name)),
                  'rt') as file_config:
            return self._parser.parseString(file_config.read())


Instruction:
Can you tell what is the following Python 3 function doing
def _load_cwr_defaults(self):
        if self._cwr_defaults is None:
            self._cwr_defaults = self._reader.read_yaml_file(
                self._file_defaults)

        return self._cwr_defaults

Response: Loads the CWR default values file and creates a matrix from it and then returns this data.


Instruction:
Can you implement a function in Python 3 that
loads the configuration fields file for the given id.

Response:
def load_field_config(self, file_id):
        """
        Loads the configuration fields file for the id.

        :param file_id: the id for the field
        :return: the fields configuration
        """
        if file_id not in self._field_configs:
            self._field_configs[file_id] = self._reader.read_yaml_file(
                'field_config_%s.yml' % file_id)

        return self._field_configs[file_id]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_group_config(self, file_id):
        if file_id not in self._group_configs:
            self._group_configs[file_id] = self._reader.read_config_file(
                'group_config_%s.cml' % file_id)

        return self._group_configs[file_id]

Response: Loads the configuration fields file for the group.


Instruction:
Create a Python 3 function to
load the configuration fields file for the record.

Response:
def load_record_config(self, file_id):
        """
        Loads the configuration fields file for the id.

        :param file_id: the id for the field
        :return: the fields configuration
        """
        if file_id not in self._record_configs:
            self._record_configs[file_id] = self._reader.read_config_file(
                'record_config_%s.cml' % file_id)

        return self._record_configs[file_id]


Instruction:
How would you code a function in Python 3 to
load the configuration fields file for the id.

Response:
def load_transaction_config(self, file_id):
        """
        Loads the configuration fields file for the id.

        :param file_id: the id for the field
        :return: the fields configuration
        """
        if file_id not in self._transaction_configs:
            self._transaction_configs[file_id] = self._reader.read_config_file(
                'transaction_config_%s.cml' % file_id)

        return self._transaction_configs[file_id]


Instruction:
Create a Python 3 function to
load the CWR acknowledge config file and returns the values matrix

Response:
def load_acknowledge_config(self, file_id):
        """
        Loads the CWR acknowledge config
        :return: the values matrix
        """
        if self._cwr_defaults is None:
            self._cwr_defaults = self._reader.read_yaml_file(
                'acknowledge_config_%s.yml' % file_id)

        return self._cwr_defaults


Instruction:
Write a Python 3 script to
print a soft error message to stderr.

Response:
def soft_error(self, message):
        """
        Same as error, without the dying in a fire part.
        """

        self.print_usage(sys.stderr)
        args = {'prog': self.prog, 'message': message}
        self._print_message(
            _('%(prog)s: error: %(message)s\n') % args, sys.stderr)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def default_filename_decoder():
    factory = default_filename_grammar_factory()

    grammar_old = factory.get_rule('filename_old')
    grammar_new = factory.get_rule('filename_new')

    return FileNameDecoder(grammar_old, grammar_new)

Response:
Creates a decoder which parses CWR filenames following the old or the new
    convention.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def decode(self, data):
        file_name = self._filename_decoder.decode(data['filename'])

        file_data = data['contents']
        i = 0
        max_size = len(file_data)
        while file_data[i:i + 1] != 'H' and i < max_size:
            i += 1
        if i > 0:
            data['contents'] = file_data[i:]

        transmission = self._file_decoder.decode(data['contents'])[0]

        return CWRFile(file_name, transmission)

Response: Parses the file and creates a CWRFile instance from it.


Instruction:
Write a Python 3 function that can
parse the filename and returns a FileTag instance.

Response:
def decode(self, file_name):
        """
        Parses the filename, creating a FileTag from it.

        It will try both the old and the new conventions, if the filename does
        not conform any of them, then an empty FileTag will be returned.

        :param file_name: filename to parse
        :return: a FileTag instance
        """
        try:
            file_tag = self._filename_decoder_new.decode(file_name)
        except:
            try:
                file_tag = self._filename_decoder_old.decode(file_name)
            except:
                file_tag = FileTag(0, 0, '', '', '')

        return file_tag


Instruction:
Explain what the following Python 3 code does
def finalize_env(env):

    keys = _PLATFORM_ENV_KEYS.get(sys.platform, [])
    if 'PATH' not in keys:
        # this MUST be available due to Node.js (and others really)
        # needing something to look for binary locations when it shells
        # out to other binaries.
        keys.append('PATH')
    results = {
        key: os.environ.get(key, '') for key in keys
    }
    results.update(env)
    return results

Response:
Finalize the environment for passing into subprocess. Popen
    family of external process calling methods.


Instruction:
Can you write a function in Python 3 where it
does a fork - exec through the subprocess. Popen abstraction in a way that takes a stdin and return stdout and stderr.

Response:
def fork_exec(args, stdin='', **kwargs):
    """
    Do a fork-exec through the subprocess.Popen abstraction in a way
    that takes a stdin and return stdout.
    """

    as_bytes = isinstance(stdin, bytes)
    source = stdin if as_bytes else stdin.encode(locale)
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, **kwargs)
    stdout, stderr = p.communicate(source)
    if as_bytes:
        return stdout, stderr
    return (stdout.decode(locale), stderr.decode(locale))


Instruction:
Can you generate the documentation for the following Python 3 function
def raise_os_error(_errno, path=None):

    msg = "%s: '%s'" % (strerror(_errno), path) if path else strerror(_errno)
    raise OSError(_errno, msg)

Response: Raises an exception under Python 2. 7.


Instruction:
Write a Python 3 function that can
give a command check where it is on PATH.

Response:
def which(cmd, mode=os.F_OK | os.X_OK, path=None):
    """
    Given cmd, check where it is on PATH.

    Loosely based on the version in python 3.3.
    """

    if os.path.dirname(cmd):
        if os.path.isfile(cmd) and os.access(cmd, mode):
            return cmd

    if path is None:
        path = os.environ.get('PATH', defpath)
    if not path:
        return None

    paths = path.split(pathsep)

    if sys.platform == 'win32':
        # oh boy
        if curdir not in paths:
            paths = [curdir] + paths

        # also need to check the fileexts...
        pathext = os.environ.get('PATHEXT', '').split(pathsep)

        if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
            files = [cmd]
        else:
            files = [cmd + ext for ext in pathext]
    else:
        # sanity
        files = [cmd]

    seen = set()
    for p in paths:
        normpath = normcase(p)
        if normpath in seen:
            continue
        seen.add(normpath)
        for f in files:
            fn = os.path.join(p, f)
            if os.path.isfile(fn) and os.access(fn, mode):
                return fn

    return None


Instruction:
Can you generate the documentation for the following Python 3 function
def _init(self):

        self._entry_points = {}
        for entry_point in self.raw_entry_points:
            if entry_point.dist.project_name != self.reserved.get(
                    entry_point.name, entry_point.dist.project_name):
                logger.error(
                    "registry '%s' for '%s' is reserved for package '%s'",
                    entry_point.name, self.registry_name,
                    self.reserved[entry_point.name],
                )
                continue

            if self.get_record(entry_point.name):
                logger.warning(
                    "registry '%s' for '%s' is already registered.",
                    entry_point.name, self.registry_name,
                )
                existing = self._entry_points[entry_point.name]
                logger.debug(
                    "registered '%s' from '%s'", existing, existing.dist)
                logger.debug(
                    "discarded '%s' from '%s'", entry_point, entry_point.dist)
                continue

            logger.debug(
                "recording '%s' from '%s'", entry_point, entry_point.dist)
            self._entry_points[entry_point.name] = entry_point

Response: Initialize the internal state of the internal state of the internal state of the internal state.


Instruction:
Here you have a function in Python 3, explain what it does
def dict_update_overwrite_check(base, fresh):

    result = [
        (key, base[key], fresh[key])
        for key in set(base.keys()) & set(fresh.keys())
        if base[key] != fresh[key]
    ]
    base.update(fresh)
    return result

Response: For updating a base dict with a fresh one returns a list of tuple containing the key previous value and the fresh value.


Instruction:
Can you implement a function in Python 3 that
updates the loaderplugin registry in a Spec object.

Response:
def spec_update_loaderplugin_registry(spec, default=None):
    """
    Resolve a BasePluginLoaderRegistry instance from spec, and update
    spec[CALMJS_LOADERPLUGIN_REGISTRY] with that value before returning
    it.
    """

    registry = spec.get(CALMJS_LOADERPLUGIN_REGISTRY)
    if isinstance(registry, BaseLoaderPluginRegistry):
        logger.debug(
            "loaderplugin registry '%s' already assigned to spec",
            registry.registry_name)
        return registry
    elif not registry:
        # resolving registry
        registry = get_registry(spec.get(CALMJS_LOADERPLUGIN_REGISTRY_NAME))
        if isinstance(registry, BaseLoaderPluginRegistry):
            logger.info(
                "using loaderplugin registry '%s'", registry.registry_name)
            spec[CALMJS_LOADERPLUGIN_REGISTRY] = registry
            return registry

    # acquire the real default instance, if possible.
    if not isinstance(default, BaseLoaderPluginRegistry):
        default = get_registry(default)
        if not isinstance(default, BaseLoaderPluginRegistry):
            logger.info(
                "provided default is not a valid loaderplugin registry")
            default = None

    if default is None:
        default = BaseLoaderPluginRegistry('<default_loaderplugins>')

    # TODO determine the best way to optionally warn about this for
    # toolchains that require this.
    if registry:
        logger.info(
            "object referenced in spec is not a valid loaderplugin registry; "
            "using default loaderplugin registry '%s'", default.registry_name)
    else:
        logger.info(
            "no loaderplugin registry referenced in spec; "
            "using default loaderplugin registry '%s'", default.registry_name)
    spec[CALMJS_LOADERPLUGIN_REGISTRY] = registry = default

    return registry


Instruction:
Implement a Python 3 function for
taking an existing spec and a sourcepath mapping (that could be produced via calmjs.dist.*_module_registry_dependencies functions) and split out the keys that does not contain loaderplugin syntax and assign it to the spec under sourcepath_key. For the parts with loader plugin syntax (i.e. modnames (keys) that contain a '!' character), they are instead stored under a different mapping under its own mapping identified by the plugin_name. The mapping under loaderplugin_sourcepath_map_key will contain all mappings of this type. The resolution for the handlers will be done through the loader plugin registry provided via spec[CALMJS_LOADERPLUGIN_REGISTRY] if available, otherwise the registry instance will be acquired through the main registry using spec[CALMJS_LOADERPLUGIN_REGISTRY_NAME]. For the example sourcepath_map input: sourcepath = { 'module': 'something', 'plugin!inner': 'inner', 'plugin!other': 'other', 'plugin?query!question': 'question', 'plugin!plugin2!target': 'target', } The following will be stored under the following keys in spec: spec[sourcepath_key] = { 'module': 'something', } spec[loaderplugin_sourcepath_map_key] = { 'plugin': { 'plugin!inner': 'inner', 'plugin!other': 'other', 'plugin?query!question': 'question', 'plugin!plugin2!target': 'target', }, } The goal of this function is to aid in processing each of the plugin types by batch, one level at a time. It is up to the handler itself to trigger further lookups as there are implementations of loader plugins that do not respect the chaining mechanism, thus a generic lookup done at once may not be suitable. Note that nested/chained loaderplugins are not immediately grouped as they must be individually handled given that the internal syntax are generally proprietary to the outer plugin. The handling will be dealt with at the Toolchain.compile_loaderplugin_entry method through the associated handler call method. Toolchain implementations may either invoke this directly as part of the prepare step on the required sourcepaths values stored in the spec, or implement this at a higher level before invocating the toolchain instance with the spec.

Response:
def spec_update_sourcepath_filter_loaderplugins(
        spec, sourcepath_map, sourcepath_map_key,
        loaderplugin_sourcepath_map_key=LOADERPLUGIN_SOURCEPATH_MAPS):
    """
    Take an existing spec and a sourcepath mapping (that could be
    produced via calmjs.dist.*_module_registry_dependencies functions)
    and split out the keys that does not contain loaderplugin syntax and
    assign it to the spec under sourcepath_key.

    For the parts with loader plugin syntax (i.e. modnames (keys) that
    contain a '!' character), they are instead stored under a different
    mapping under its own mapping identified by the plugin_name.  The
    mapping under loaderplugin_sourcepath_map_key will contain all
    mappings of this type.

    The resolution for the handlers will be done through the loader
    plugin registry provided via spec[CALMJS_LOADERPLUGIN_REGISTRY] if
    available, otherwise the registry instance will be acquired through
    the main registry using spec[CALMJS_LOADERPLUGIN_REGISTRY_NAME].

    For the example sourcepath_map input:

    sourcepath = {
        'module': 'something',
        'plugin!inner': 'inner',
        'plugin!other': 'other',
        'plugin?query!question': 'question',
        'plugin!plugin2!target': 'target',
    }

    The following will be stored under the following keys in spec:

    spec[sourcepath_key] = {
        'module': 'something',
    }

    spec[loaderplugin_sourcepath_map_key] = {
        'plugin': {
            'plugin!inner': 'inner',
            'plugin!other': 'other',
            'plugin?query!question': 'question',
            'plugin!plugin2!target': 'target',
        },
    }

    The goal of this function is to aid in processing each of the plugin
    types by batch, one level at a time.  It is up to the handler itself
    to trigger further lookups as there are implementations of loader
    plugins that do not respect the chaining mechanism, thus a generic
    lookup done at once may not be suitable.

    Note that nested/chained loaderplugins are not immediately grouped
    as they must be individually handled given that the internal syntax
    are generally proprietary to the outer plugin.  The handling will be
    dealt with at the Toolchain.compile_loaderplugin_entry method
    through the associated handler call method.

    Toolchain implementations may either invoke this directly as part
    of the prepare step on the required sourcepaths values stored in the
    spec, or implement this at a higher level before invocating the
    toolchain instance with the spec.
    """

    default = dict_setget_dict(spec, sourcepath_map_key)
    registry = spec_update_loaderplugin_registry(spec)

    # it is more loaderplugin_sourcepath_maps
    plugins = dict_setget_dict(spec, loaderplugin_sourcepath_map_key)

    for modname, sourcepath in sourcepath_map.items():
        parts = modname.split('!', 1)
        if len(parts) == 1:
            # default
            default[modname] = sourcepath
            continue

        # don't actually do any processing yet.
        plugin_name = registry.to_plugin_name(modname)
        plugin = dict_setget_dict(plugins, plugin_name)
        plugin[modname] = sourcepath


Instruction:
Can you tell what is the following Python 3 function doing
def toolchain_spec_prepare_loaderplugins(
        toolchain, spec,
        loaderplugin_read_key,
        handler_sourcepath_key,
        loaderplugin_sourcepath_map_key=LOADERPLUGIN_SOURCEPATH_MAPS):
    """
    A standard helper function for combining the filtered (e.g. using
    ``spec_update_sourcepath_filter_loaderplugins``) loaderplugin
    sourcepath mappings back into one that is usable with the standard
    ``toolchain_spec_compile_entries`` function.

    Arguments:

    toolchain
        The toolchain
    spec
        The spec

    loaderplugin_read_key
        The read_key associated with the loaderplugin process as set up
        for the Toolchain that implemented this.  If the toolchain has
        this in its compile_entries:

            ToolchainSpecCompileEntry('loaderplugin', 'plugsrc', 'plugsink')

        The loaderplugin_read_key it must use will be 'plugsrc'.

    handler_sourcepath_key
        All found handlers will have their handler_sourcepath method be
        invoked, and the combined results will be a dict stored in the
        spec under that key.

    loaderplugin_sourcepath_map_key
        It must be the same key to the value produced by
        ``spec_update_sourcepath_filter_loaderplugins``
    """

    # ensure the registry is applied to the spec
    registry = spec_update_loaderplugin_registry(
        spec, default=toolchain.loaderplugin_registry)

    # this one is named like so for the compile entry method
    plugin_sourcepath = dict_setget_dict(
        spec, loaderplugin_read_key + '_sourcepath')
    # the key is supplied by the toolchain that might make use of this
    if handler_sourcepath_key:
        handler_sourcepath = dict_setget_dict(spec, handler_sourcepath_key)
    else:
        # provide a null value for this.
        handler_sourcepath = {}

    for key, value in spec.get(loaderplugin_sourcepath_map_key, {}).items():
        handler = registry.get(key)
        if handler:
            # assume handler will do the job.
            logger.debug("found handler for '%s' loader plugin", key)
            plugin_sourcepath.update(value)
            logger.debug(
                "plugin_sourcepath updated with %d keys", len(value))
            # TODO figure out how to address the case where the actual
            # JavaScript module for the handling wasn't found.
            handler_sourcepath.update(
                handler.generate_handler_sourcepath(toolchain, spec, value))
        else:
            logger.warning(
                "loaderplugin handler for '%s' not found in loaderplugin "
                "registry '%s'; as arguments associated with loader plugins "
                "are specific, processing is disabled for this group; the "
                "sources referenced by the following names will not be "
                "compiled into the build target: %s",
                key, registry.registry_name, sorted(value.keys()),
            )

Response: This function is used to prepare the loaderplugins for a given set of sources.


Instruction:
Write a Python 3 script for
processing the compile entries.

Response:
def process_compile_entries(
        processor, spec, entries, modpath_logger=None, targetpath_logger=None):
    """
    The generalized raw spec entry process invocation loop.
    """

    # Contains a mapping of the module name to the compiled file's
    # relative path starting from the base build_dir.
    all_modpaths = {}
    all_targets = {}
    # List of exported module names, should be equal to all keys of
    # the compiled and bundled sources.
    all_export_module_names = []

    def update(base, fresh, logger):
        if callable(logger):
            for dupes in dict_update_overwrite_check(base, fresh):
                logger(*dupes)
        else:
            base.update(fresh)

    for entry in entries:
        modpaths, targetpaths, export_module_names = processor(spec, entry)
        update(all_modpaths, modpaths, modpath_logger)
        update(all_targets, targetpaths, targetpath_logger)
        all_export_module_names.extend(export_module_names)

    return all_modpaths, all_targets, all_export_module_names


Instruction:
Write a Python 3 script to
like update but a list of selected keys must be provided.

Response:
def update_selected(self, other, selected):
        """
        Like update, however a list of selected keys must be provided.
        """

        self.update({k: other[k] for k in selected})


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

        if frame is None:
            logger.debug(
                'currentframe() returned None; frame protection disabled')
            return

        f_back = frame.f_back
        while f_back:
            if f_back.f_code is self.handle.__code__:
                raise RuntimeError(
                    "indirect invocation of '%s' by 'handle' is forbidden" %
                    frame.f_code.co_name,
                )
            f_back = f_back.f_back

Response: This method is used to check if the current frame protection is allowed.


Instruction:
Write a Python 3 script for
adding an advice to the stack of the advice group.

Response:
def advise(self, name, f, *a, **kw):
        """
        Add an advice that will be handled later by the handle method.

        Arguments:

        name
            The name of the advice group
        f
            A callable method or function.

        The rest of the arguments will be passed as arguments and
        keyword arguments to f when it's invoked.
        """

        if name is None:
            return

        advice = (f, a, kw)
        debug = self.get(DEBUG)

        frame = currentframe()
        if frame is None:
            logger.debug('currentframe() failed to return frame')
        else:
            if name in self._called:
                self.__advice_stack_frame_protection(frame)
            if debug:
                logger.debug(
                    "advise '%s' invoked by %s:%d",
                    name,
                    frame.f_back.f_code.co_filename, frame.f_back.f_lineno,
                )
                if debug > 1:
                    # use the memory address of the tuple which should
                    # be stable
                    self._frames[id(advice)] = ''.join(
                        format_stack(frame.f_back))

        self._advices[name] = self._advices.get(name, [])
        self._advices[name].append(advice)


Instruction:
Create a Python 3 function to
call all advices at the provided name. This has an analogue in the join point in aspected oriented programming, but the analogy is a weak one as we don't have the proper metaobject protocol to support this. Implementation that make use of this system should make it clear that they will call this method with name associated with its group before and after its execution, or that the method at hand that want this invoked be called by this other conductor method. For the Toolchain standard steps (prepare, compile, assemble, link and finalize), this handle method will only be called by invoking the toolchain as a callable. Calling those methods piecemeal will not trigger the invocation, even though it probably should. Modules, classes and methods that desire to call their own handler should instead follow the convention where the handle be called before and after with the appropriate names. For instance: def test(self, spec): spec.handle(BEFORE_TEST) # do the things spec.handle(AFTER_TEST) This arrangement will need to be revisited when a proper system is written at the metaclass level. Arguments: name The name of the advices group. All the callables registered to this group will be invoked, last-in-first-out.

Response:
def handle(self, name):
        """
        Call all advices at the provided name.

        This has an analogue in the join point in aspected oriented
        programming, but the analogy is a weak one as we don't have the
        proper metaobject protocol to support this.  Implementation that
        make use of this system should make it clear that they will call
        this method with name associated with its group before and after
        its execution, or that the method at hand that want this invoked
        be called by this other conductor method.

        For the Toolchain standard steps (prepare, compile, assemble,
        link and finalize), this handle method will only be called by
        invoking the toolchain as a callable.  Calling those methods
        piecemeal will not trigger the invocation, even though it
        probably should.  Modules, classes and methods that desire to
        call their own handler should instead follow the convention
        where the handle be called before and after with the appropriate
        names.  For instance:

            def test(self, spec):
                spec.handle(BEFORE_TEST)
                # do the things
                spec.handle(AFTER_TEST)

        This arrangement will need to be revisited when a proper system
        is written at the metaclass level.

        Arguments:

        name
            The name of the advices group.  All the callables
            registered to this group will be invoked, last-in-first-out.
        """

        if name in self._called:
            logger.warning(
                "advice group '%s' has been called for this spec %r",
                name, self,
            )
            # only now ensure checking
            self.__advice_stack_frame_protection(currentframe())
        else:
            self._called.add(name)

        # Get a complete clone, so indirect manipulation done to the
        # reference that others have access to will not have an effect
        # within the scope of this execution.  Please refer to the
        # test_toolchain, test_spec_advice_no_infinite_pop test case.
        advices = []
        advices.extend(self._advices.get(name, []))

        if advices and self.get('debug'):
            logger.debug(
                "handling %d advices in group '%s' ", len(advices), name)

        while advices:
            try:
                # cleanup basically done lifo (last in first out)
                values = advices.pop()
                advice, a, kw = values
                if not ((callable(advice)) and
                        isinstance(a, tuple) and
                        isinstance(kw, dict)):
                    raise TypeError
            except ValueError:
                logger.info('Spec advice extraction error: got %s', values)
            except TypeError:
                logger.info('Spec advice malformed: got %s', values)
            else:
                try:
                    try:
                        advice(*a, **kw)
                    except Exception as e:
                        # get that back by the id.
                        frame = self._frames.get(id(values))
                        if frame:
                            logger.info('Spec advice exception: %r', e)
                            logger.info(
                                'Traceback for original advice:\n%s', frame)
                        # continue on for the normal exception
                        raise
                except AdviceCancel as e:
                    logger.info(
                        "advice %s in group '%s' signaled its cancellation "
                        "during its execution: %s", advice, name, e
                    )
                    if self.get(DEBUG):
                        logger.debug(
                            'showing traceback for cancellation', exc_info=1,
                        )
                except AdviceAbort as e:
                    # this is a signaled error with a planned abortion
                    logger.warning(
                        "advice %s in group '%s' encountered a known error "
                        "during its execution: %s; continuing with toolchain "
                        "execution", advice, name, e
                    )
                    if self.get(DEBUG):
                        logger.warning(
                            'showing traceback for error', exc_info=1,
                        )
                except ToolchainCancel:
                    # this is the safe cancel
                    raise
                except ToolchainAbort as e:
                    logger.critical(
                        "an advice in group '%s' triggered an abort: %s",
                        name, str(e)
                    )
                    raise
                except KeyboardInterrupt:
                    raise ToolchainCancel('interrupted')
                except Exception as e:
                    # a completely unplanned failure
                    logger.critical(
                        "advice %s in group '%s' terminated due to an "
                        "unexpected exception: %s", advice, name, e
                    )
                    if self.get(DEBUG):
                        logger.critical(
                            'showing traceback for error', exc_info=1,
                        )


Instruction:
Write a Python 3 script for
resolving and update the path key in the spec with its realpath based on the working directory.

Response:
def realpath(self, spec, key):
        """
        Resolve and update the path key in the spec with its realpath,
        based on the working directory.
        """

        if key not in spec:
            # do nothing for now
            return

        if not spec[key]:
            logger.warning(
                "cannot resolve realpath of '%s' as it is not defined", key)
            return

        check = realpath(join(spec.get(WORKING_DIR, ''), spec[key]))
        if check != spec[key]:
            spec[key] = check
            logger.warning(
                "realpath of '%s' resolved to '%s', spec is updated",
                key, check
            )
        return check


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

        self.compile_prefix = 'compile_'
        self.sourcepath_suffix = '_sourcepath'
        self.modpath_suffix = '_modpaths'
        self.targetpath_suffix = '_targetpaths'

Response:
Set up the compile prefix sourcepath and targetpath suffix attributes which are used to retrieve the values from the generator
        function.


Instruction:
How would you explain what the following Python 3 function does
def _validate_build_target(self, spec, target):

        if not realpath(target).startswith(spec[BUILD_DIR]):
            raise ValueError('build_target %s is outside build_dir' % target)

Response: Validate that the target is inside the build_dir.


Instruction:
Given the following Python 3 function, write the documentation
def transpile_modname_source_target(self, spec, modname, source, target):

        if not isinstance(self.transpiler, BaseUnparser):
            _deprecation_warning(
                'transpiler callable assigned to %r must be an instance of '
                'calmjs.parse.unparsers.base.BaseUnparser by calmjs-4.0.0; '
                'if the original transpile behavior is to be retained, the '
                'subclass may instead override this method to call '
                '`simple_transpile_modname_source_target` directly, as '
                'this fallback behavior will be removed by calmjs-4.0.0' % (
                    self,
                )
            )
            return self.simple_transpile_modname_source_target(
                spec, modname, source, target)

        # do the new thing here.
        return self._transpile_modname_source_target(
            spec, modname, source, target)

Response:
This method is called by compile_transpile_entry to process the JavaScript source file provided by some
        Python package through the transpiler instance.


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

        modname, source, target, modpath = entry
        handler = spec[CALMJS_LOADERPLUGIN_REGISTRY].get(modname)
        if handler:
            return handler(self, spec, modname, source, target, modpath)
        logger.warning(
            "no loaderplugin handler found for plugin entry '%s'", modname)
        return {}, {}, []

Response: Compile a loader plugin entry into a dictionary.


Instruction:
How would you code a function in Python 3 to
create a target file name from the input module name and its source file name. The result should be a path relative to the build_dir, and this is derived directly from the modname with NO implicit convers of path separators (i.e. '/' or any other) into a system or OS specific form (e.g. '\\'). The rationale for this choice is that there exists Node.js/JavaScript tools that handle this internally and/or these paths and values are directly exposed on the web and thus these separators must be preserved. If the specific implementation requires this to be done, implementations may override by wrapping the result of this using os.path.normpath. For the generation of transpile write targets, this will be done in _generate_transpile_target. Default is to append the module name with the filename_suffix assigned to this instance (setup by setup_filename_suffix), iff the provided source also end with this filename suffix. However, certain tools have issues dealing with loader plugin syntaxes showing up on the filesystem (and certain filesystems definitely do not like some of the characters), so the usage of the loaderplugin registry assigned to the spec may be used for lookup if available. Called by generator method `_gen_modname_source_target_modpath`.

Response:
def modname_source_to_target(self, spec, modname, source):
        """
        Create a target file name from the input module name and its
        source file name.  The result should be a path relative to the
        build_dir, and this is derived directly from the modname with NO
        implicit convers of path separators (i.e. '/' or any other) into
        a system or OS specific form (e.g. '\\').  The rationale for
        this choice is that there exists Node.js/JavaScript tools that
        handle this internally and/or these paths and values are
        directly exposed on the web and thus these separators must be
        preserved.

        If the specific implementation requires this to be done,
        implementations may override by wrapping the result of this
        using os.path.normpath.  For the generation of transpile write
        targets, this will be done in _generate_transpile_target.

        Default is to append the module name with the filename_suffix
        assigned to this instance (setup by setup_filename_suffix), iff
        the provided source also end with this filename suffix.

        However, certain tools have issues dealing with loader plugin
        syntaxes showing up on the filesystem (and certain filesystems
        definitely do not like some of the characters), so the usage of
        the loaderplugin registry assigned to the spec may be used for
        lookup if available.

        Called by generator method `_gen_modname_source_target_modpath`.
        """

        loaderplugin_registry = spec.get(CALMJS_LOADERPLUGIN_REGISTRY)
        if '!' in modname and loaderplugin_registry:
            handler = loaderplugin_registry.get(modname)
            if handler:
                return handler.modname_source_to_target(
                    self, spec, modname, source)

        if (source.endswith(self.filename_suffix) and
                not modname.endswith(self.filename_suffix)):
            return modname + self.filename_suffix
        else:
            # assume that modname IS the filename
            return modname


Instruction:
Given the following Python 3 function, write the documentation
def modname_source_target_modnamesource_to_modpath(
            self, spec, modname, source, target, modname_source):
        """
        Typical JavaScript tools will get confused if '.js' is added, so
        by default the same modname is returned as path rather than the
        target file for the module path to be written to the output file
        for linkage by tools.  Some other tools may desire the target to
        be returned instead, or construct some other string that is more
        suitable for the tool that will do the assemble and link step.

        The modname and source argument provided to aid pedantic tools,
        but really though this provides more consistency to method
        signatures.

        Same as `self.modname_source_target_to_modpath`, but includes
        the original raw key-value as a 2-tuple.

        Called by generator method `_gen_modname_source_target_modpath`.
        """

        return self.modname_source_target_to_modpath(
            spec, modname, source, target)

Response: Returns the path to the target file for the given module name.


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

        for modname_source in d.items():
            try:
                modname = self.modname_source_to_modname(spec, *modname_source)
                source = self.modname_source_to_source(spec, *modname_source)
                target = self.modname_source_to_target(spec, *modname_source)
                modpath = self.modname_source_target_modnamesource_to_modpath(
                    spec, modname, source, target, modname_source)
            except ValueError as e:
                # figure out which of the above 3 functions failed by
                # acquiring the name from one frame down.
                f_name = sys.exc_info()[2].tb_next.tb_frame.f_code.co_name

                if isinstance(e, ValueSkip):
                    # a purposely benign failure.
                    log = partial(
                        logger.info,
                        "toolchain purposely skipping on '%s', "
                        "reason: %s, where modname='%s', source='%s'",
                    )
                else:
                    log = partial(
                        logger.warning,
                        "toolchain failed to acquire name with '%s', "
                        "reason: %s, where modname='%s', source='%s'; "
                        "skipping",
                    )

                log(f_name, e, *modname_source)
                continue
            yield modname, source, target, modpath

Response: Private generator that generates the names of the source and target modules that are used by the toolchain.


Instruction:
Given the following Python 3 function, write the documentation
def compile(self, spec):

        spec[EXPORT_MODULE_NAMES] = export_module_names = spec.get(
            EXPORT_MODULE_NAMES, [])
        if not isinstance(export_module_names, list):
            raise TypeError(
                "spec provided a '%s' but it is not of type list "
                "(got %r instead)" % (EXPORT_MODULE_NAMES, export_module_names)
            )

        def compile_entry(method, read_key, store_key):
            spec_read_key = read_key + self.sourcepath_suffix
            spec_modpath_key = store_key + self.modpath_suffix
            spec_target_key = store_key + self.targetpath_suffix

            if _check_key_exists(spec, [spec_modpath_key, spec_target_key]):
                logger.error(
                    "aborting compile step %r due to existing key", entry,
                )
                return

            sourcepath_dict = spec.get(spec_read_key, {})
            entries = self._gen_modname_source_target_modpath(
                spec, sourcepath_dict)
            (spec[spec_modpath_key], spec[spec_target_key],
                new_module_names) = method(spec, entries)
            logger.debug(
                "entry %r "
                "wrote %d entries to spec[%r], "
                "wrote %d entries to spec[%r], "
                "added %d export_module_names",
                entry,
                len(spec[spec_modpath_key]), spec_modpath_key,
                len(spec[spec_target_key]), spec_target_key,
                len(new_module_names),
            )
            export_module_names.extend(new_module_names)

        for entry in self.compile_entries:
            if isinstance(entry, ToolchainSpecCompileEntry):
                log = partial(
                    logging.getLogger(entry.logger).log,
                    entry.log_level,
                    (
                        entry.store_key + "%s['%s'] is being rewritten from "
                        "'%s' to '%s'; configuration may now be invalid"
                    ),
                ) if entry.logger else None
                compile_entry(partial(
                    toolchain_spec_compile_entries, self,
                    process_name=entry.process_name,
                    overwrite_log=log,
                ), entry.read_key, entry.store_key)
                continue

            m, read_key, store_key = entry
            if callable(m):
                method = m
            else:
                method = getattr(self, self.compile_prefix + m, None)
                if not callable(method):
                    logger.error(
                        "'%s' not a callable attribute for %r from "
                        "compile_entries entry %r; skipping", m, self, entry
                    )
                    continue

            compile_entry(method, read_key, store_key)

Response: Generic step that compiles from a spec to build the build directory.


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

        if not isinstance(spec, Spec):
            raise TypeError('spec must be of type Spec')

        if not spec.get(BUILD_DIR):
            tempdir = realpath(mkdtemp())
            spec.advise(CLEANUP, shutil.rmtree, tempdir)
            build_dir = join(tempdir, 'build')
            mkdir(build_dir)
            spec[BUILD_DIR] = build_dir
        else:
            build_dir = self.realpath(spec, BUILD_DIR)
            if not isdir(build_dir):
                logger.error("build_dir '%s' is not a directory", build_dir)
                raise_os_error(errno.ENOTDIR, build_dir)

        self.realpath(spec, EXPORT_TARGET)

        # Finally, handle setup which may set up the deferred advices,
        # as all the toolchain (and its runtime and/or its parent
        # runtime and related toolchains) spec advises should have been
        # done.
        spec.handle(SETUP)

        try:
            process = ('prepare', 'compile', 'assemble', 'link', 'finalize')
            for p in process:
                spec.handle('before_' + p)
                getattr(self, p)(spec)
                spec.handle('after_' + p)
            spec.handle(SUCCESS)
        except ToolchainCancel:
            # quietly handle the issue and move on out of here.
            pass
        finally:
            spec.handle(CLEANUP)

Response: This function sets up the spec advises and returns the unique ID of the new one.


Instruction:
Can you tell what is the following Python 3 function doing
def transpile_modname_source_target(self, spec, modname, source, target):

        return self.simple_transpile_modname_source_target(
            spec, modname, source, target)

Response: Transpile a module name from one source to another.


Instruction:
How would you explain what the following Python 3 function does
def get_bin_version_str(bin_path, version_flag='-v', kw={}):

    try:
        prog = _get_exec_binary(bin_path, kw)
        version_str = version_expr.search(
            check_output([prog, version_flag], **kw).decode(locale)
        ).groups()[0]
    except OSError:
        logger.warning("failed to execute '%s'", bin_path)
        return None
    except Exception:
        logger.exception(
            "encountered unexpected error while trying to find version of "
            "'%s':", bin_path
        )
        return None
    logger.info("'%s' is version '%s'", bin_path, version_str)
    return version_str

Response: Get the version string through the binary.


Instruction:
How would you explain what the following Python 3 function does
def get_bin_version(bin_path, version_flag='-v', kw={}):

    version_str = get_bin_version_str(bin_path, version_flag, kw)
    if version_str:
        return tuple(int(i) for i in version_str.split('.'))

Response: Get the version string through the binary and return a tuple of integers.


Instruction:
Explain what the following Python 3 code does
def node(self, source, args=(), env={}):

        return self._exec(self.node_bin, source, args=args, env=env)

Response: Calls node with an inline source.


Instruction:
Given the following Python 3 function, write the documentation
def pkg_manager_view(
            self, package_names, stream=None, explicit=False, **kw):
        """
        Returns the manifest JSON for the Python package name.  Default
        npm implementation calls for package.json.

        If this class is initiated using standard procedures, this will
        mimic the functionality of ``npm view`` but mostly for showing
        the dependencies.  This is done as a default action.

        Arguments:

        package_names
            The names of the python packages with their requirements to
            source the package.json from.
        stream
            If specified, the generated package.json will be written to
            there.
        explicit
            If True, the package names specified are the explicit list
            to search for - no dependency resolution will then be done.

        Returns the manifest json as a dict.
        """

        # For looking up the pkg_name to dist converter for explicit
        to_dists = {
            False: find_packages_requirements_dists,
            True: pkg_names_to_dists,
        }

        # assuming string, and assume whitespaces are invalid.
        pkg_names, malformed = convert_package_names(package_names)
        if malformed:
            msg = 'malformed package name(s) specified: %s' % ', '.join(
                malformed)
            raise ValueError(msg)

        if len(pkg_names) == 1:
            logger.info(
                "generating a flattened '%s' for '%s'",
                self.pkgdef_filename, pkg_names[0],
            )
        else:
            logger.info(
                "generating a flattened '%s' for packages {%s}",
                self.pkgdef_filename, ', '.join(pkg_names),
            )

        # remember the filename is in the context of the distribution,
        # not the filesystem.
        dists = to_dists[explicit](pkg_names)
        pkgdef_json = flatten_dist_egginfo_json(
            dists, filename=self.pkgdef_filename,
            dep_keys=self.dep_keys,
        )

        if pkgdef_json.get(
                self.pkg_name_field, NotImplemented) is NotImplemented:
            # use the last item.
            pkg_name = Requirement.parse(pkg_names[-1]).project_name
            pkgdef_json[self.pkg_name_field] = pkg_name

        if stream:
            self.dump(pkgdef_json, stream)
            stream.write('\n')

        return pkgdef_json

Response: This function generates a manifest json for the Python package name.


Instruction:
Can you write a function in Python 3 where it
notes default implementation calls for npm and package.json, please note that it may not be the case for this instance of Driver. If this class is initiated using standard procedures, this will emulate the functionality of ``npm init`` for the generation of a working ``package.json``, but without asking users for input but instead uses information available through the distribution packages within ``setuptools``. Arguments: package_names The names of the python packages with their requirements to source the package.json from. overwrite Boolean flag; if set, overwrite package.json with the newly generated ``package.json``; merge Boolean flag; if set, implies overwrite, but does not ignore interactive setting. However this will keep details defined in existing ``package.json`` and only merge dependencies / devDependencies defined by the specified Python package. callback A callable. If this is passed, the value for overwrite will be derived from its result; it will be called with arguments (original_json, pkgdef_json, pkgdef_path, dumps=self.dumps). Typically the calmjs.ui.prompt_overwrite_json is passed into this argument; refer to its documentation on details. Returns generated definition file if successful; can be achieved by writing a new file or that the existing one matches with the expected version. Returns False otherwise.

Response:
def pkg_manager_init(
            self, package_names, overwrite=False, merge=False,
            callback=None, **kw):
        """
        Note: default implementation calls for npm and package.json,
        please note that it may not be the case for this instance of
        Driver.

        If this class is initiated using standard procedures, this will
        emulate the functionality of ``npm init`` for the generation of
        a working ``package.json``, but without asking users for input
        but instead uses information available through the distribution
        packages within ``setuptools``.

        Arguments:

        package_names
            The names of the python packages with their requirements to
            source the package.json from.

        overwrite
            Boolean flag; if set, overwrite package.json with the newly
            generated ``package.json``;

        merge
            Boolean flag; if set, implies overwrite, but does not ignore
            interactive setting.  However this will keep details defined
            in existing ``package.json`` and only merge dependencies /
            devDependencies defined by the specified Python package.

        callback
            A callable.  If this is passed, the value for overwrite will
            be derived from its result; it will be called with arguments
            (original_json, pkgdef_json, pkgdef_path, dumps=self.dumps).
            Typically the calmjs.ui.prompt_overwrite_json is passed into
            this argument; refer to its documentation on details.

        Returns generated definition file if successful; can be achieved
        by writing a new file or that the existing one matches with the
        expected version.  Returns False otherwise.
        """

        # this will be modified in place
        original_json = {}

        pkgdef_json = self.pkg_manager_view(package_names, **kw)

        # Now we figure out the actual file we want to work with.
        pkgdef_path = self.join_cwd(self.pkgdef_filename)
        existed = exists(pkgdef_path)

        if existed:
            try:
                with open(pkgdef_path, 'r') as fd:
                    original_json = json.load(fd)
            except ValueError:
                logger.warning(
                    "ignoring existing malformed '%s'", pkgdef_path)
            except (IOError, OSError):
                logger.error(
                    "reading of existing '%s' failed; "
                    "please confirm that it is a file and/or permissions to "
                    "read and write is permitted before retrying.",
                    pkgdef_path
                )
                # Cowardly giving up.
                raise

            if merge:
                # Merge the generated on top of the original.
                updates = generate_merge_dict(
                    self.dep_keys, original_json, pkgdef_json,
                )
                final = {}
                final.update(original_json)
                final.update(pkgdef_json)
                final.update(updates)
                pkgdef_json = final

            if original_json == pkgdef_json:
                # Well, if original existing one is identical with the
                # generated version, we have reached our target.
                return pkgdef_json

            if not overwrite and callable(callback):
                overwrite = callback(
                    original_json, pkgdef_json, pkgdef_path, dumps=self.dumps)
            else:
                # here the implied settings due to non-interactive mode
                # are finally set
                if merge:
                    overwrite = True

            if not overwrite:
                logger.warning("not overwriting existing '%s'", pkgdef_path)
                return False

        with open(pkgdef_path, 'w') as fd:
            self.dump(pkgdef_json, fd)
        logger.info("wrote '%s'", pkgdef_path)

        return pkgdef_json


Instruction:
Can you generate a brief explanation for the following Python 3 code
def pkg_manager_install(
            self, package_names=None,
            production=None, development=None,
            args=(), env={}, **kw):
        """
        This will install all dependencies into the current working
        directory for the specific Python package from the selected
        JavaScript package manager; this requires that this package
        manager's package definition file to be properly generated
        first, otherwise the process will be aborted.

        If the production argument is supplied, it will be passed to the
        underlying package manager binary as a true or false value with
        the --production flag, otherwise it will not be set.

        Likewise for development.  However, the production flag has
        priority.

        If the argument 'args' is supplied as a tuple, those will be
        passed through to the package manager install command as its
        arguments.  This will be very specific to the underlying
        program; use with care as misuse can result in an environment
        that is not expected by the other parts of the framework.

        If the argument 'env' is supplied, they will be additional
        environment variables that are not already defined by the
        framework, which are 'NODE_PATH' and 'PATH'.  Values set for
        those will have highest precedence, then the ones passed in
        through env, then finally whatever was already defined before
        the execution of this program.

        All other arguments to this method will be passed forward to the
        pkg_manager_init method, if the package_name is supplied for the
        Python package.

        If no package_name was supplied then just continue with the
        process anyway, to still enable the shorthand calling.

        If the package manager could not be invoked, it will simply not
        be.

        Arguments:

        package_names
            The names of the Python package to generate the manifest
            for.
        args
            The arguments to pass into the command line install.
        """

        if not package_names:
            logger.warning(
                "no package name supplied, not continuing with '%s %s'",
                self.pkg_manager_bin, self.install_cmd,
            )
            return

        result = self.pkg_manager_init(package_names, **kw)
        if result is False:
            logger.warning(
                "not continuing with '%s %s' as the generation of "
                "'%s' failed", self.pkg_manager_bin, self.install_cmd,
                self.pkgdef_filename
            )
            return

        call_kw = self._gen_call_kws(**env)
        logger.debug(
            "invoking '%s %s'", self.pkg_manager_bin, self.install_cmd)
        if self.env_path:
            logger.debug(
                "invoked with env_path '%s'", self.env_path)
        if self.working_dir:
            logger.debug(
                "invoked from working directory '%s'", self.working_dir)
        try:
            cmd = [self._get_exec_binary(call_kw), self.install_cmd]
            cmd.extend(self._prodev_flag(
                production, development, result.get(self.devkey)))
            cmd.extend(args)
            logger.info('invoking %s', ' '.join(cmd))
            call(cmd, **call_kw)
        except (IOError, OSError):
            logger.error(
                "invocation of the '%s' binary failed; please ensure it and "
                "its dependencies are installed and available.", self.binary
            )
            # Still raise the exception as this is a lower level API.
            raise

        return True

Response: This method installs all dependencies into the current working directory for the specified Python package.


Instruction:
Can you generate the documentation for the following Python 3 function
def run(self, args=(), env={}):

        # the following will call self._get_exec_binary
        return self._exec(self.binary, args=args, env=env)

Response: Calls the package manager with the arguments.


Instruction:
Given the following Python 3 function, write the documentation
def _get_exec_binary(binary, kw):

    binary = which(binary, path=kw.get('env', {}).get('PATH'))
    if binary is None:
        raise_os_error(errno.ENOENT)
    return binary

Response:
Get the executable path for the
   .


Instruction:
Can you implement a function in Python 3 that
initializes the entry points for the specified registry.

Response:
def _init_entry_points(self, entry_points):
        """
        Default initialization loop.
        """

        logger.debug(
            "registering %d entry points for registry '%s'",
            len(entry_points), self.registry_name,
        )
        for entry_point in entry_points:
            try:
                logger.debug(
                    "registering entry point '%s' from '%s'",
                    entry_point, entry_point.dist,
                )
                self._init_entry_point(entry_point)
            except ImportError:
                logger.warning(
                    'ImportError: %s not found; skipping registration',
                    entry_point.module_name)
            except Exception:
                logger.exception(
                    "registration of entry point '%s' from '%s' to registry "
                    "'%s' failed with the following exception",
                    entry_point, entry_point.dist, self.registry_name,
                )


Instruction:
Write a Python 3 function for
storing the records for the given entry_point in a way that permit lookup by package .

Response:
def store_records_for_package(self, entry_point, records):
        """
        Store the records in a way that permit lookup by package
        """

        # If provided records already exist in the module mapping list,
        # it likely means that a package declared multiple keys for the
        # same package namespace; while normally this does not happen,
        # this default implementation make no assumptions as to whether
        # or not this is permitted.
        pkg_module_records = self._dist_to_package_module_map(entry_point)
        pkg_module_records.extend(records)


Instruction:
Make a summary of the following Python 3 code
def register_entry_point(self, entry_point):

        module = _import_module(entry_point.module_name)
        self._register_entry_point_module(entry_point, module)

Response: Register a lone entry_point


Instruction:
Write a Python 3 function for
getting a record by name

Response:
def get_record(self, name):
        """
        Get a record by name
        """

        result = {}
        result.update(self.records.get(name, {}))
        return result


Instruction:
Can you create a Python 3 function that
gets all records identified by package.

Response:
def get_records_for_package(self, package_name):
        """
        Get all records identified by package.
        """

        names = self.package_module_map.get(package_name, [])
        result = {}
        for name in names:
            result.update(self.get_record(name))
        return result


Instruction:
Given the following Python 3 function, write the documentation
def resolve_parent_registry_name(self, registry_name, suffix):

        if not registry_name.endswith(suffix):
            raise ValueError(
                "child module registry name defined with invalid suffix "
                "('%s' does not end with '%s')" % (registry_name, suffix))
        return registry_name[:-len(suffix)]

Response: Resolve the parent module registry name.


Instruction:
Can you create a Python 3 function that
gets a record for the given name which will be a set of all records matching the desired module names.

Response:
def get_record(self, name):
        """
        Get a record for the registered name, which will be a set of
        matching desired "module names" for the given path.
        """

        return set().union(self.records.get(name, set()))


Instruction:
Can you write a function in Python 3 where it
gets all records identified by package.

Response:
def get_records_for_package(self, package_name):
        """
        Get all records identified by package.
        """

        result = []
        result.extend(self.package_module_map.get(package_name))
        return result


Instruction:
How would you code a function in Python 3 to
figure out which binary this will execute.

Response:
def which(self):
        """
        Figure out which binary this will execute.

        Returns None if the binary is not found.
        """

        if self.binary is None:
            return None

        return which(self.binary, path=self.env_path)


Instruction:
Write a Python 3 function for
finding all node_modules directories configured to be accessible through this driver instance.

Response:
def find_node_modules_basedir(self):
        """
        Find all node_modules directories configured to be accessible
        through this driver instance.

        This is typically used for adding the direct instance, and does
        not traverse the parent directories like what Node.js does.

        Returns a list of directories that contain a 'node_modules'
        directory.
        """

        paths = []

        # First do the working dir.
        local_node_path = self.join_cwd(NODE_MODULES)
        if isdir(local_node_path):
            paths.append(local_node_path)

        # do the NODE_PATH environment variable last, as Node.js seem to
        # have these resolving just before the global.
        if self.node_path:
            paths.extend(self.node_path.split(pathsep))

        return paths


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

        if self.binary is None:
            return None

        # first, log down the pedantic things...
        if isdir(self.join_cwd(NODE_MODULES)):
            logger.debug(
                "'%s' instance will attempt to locate '%s' binary from "
                "%s%s%s%s%s, located through the working directory",
                self.__class__.__name__, self.binary,
                self.join_cwd(), sep, NODE_MODULES, sep, NODE_MODULES_BIN,
            )
        if self.node_path:
            logger.debug(
                "'%s' instance will attempt to locate '%s' binary from "
                "its %s of %s",
                self.__class__.__name__, self.binary,
                NODE_PATH, self.node_path,
            )

        paths = self.find_node_modules_basedir()
        whichpaths = pathsep.join(join(p, NODE_MODULES_BIN) for p in paths)

        if paths:
            logger.debug(
                "'%s' instance located %d possible paths to the '%s' binary, "
                "which are %s",
                self.__class__.__name__, len(paths), self.binary, whichpaths,
            )

        return which(self.binary, path=whichpaths)

Response: Return the path to the binary which has node_path and node_modules.


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

        modcls_name = ':'.join((
            self.__class__.__module__, self.__class__.__name__))

        if self.binary is None:
            raise ValueError(
                "binary undefined for '%s' instance" % modcls_name)

        logger.debug(
            "locating '%s' node binary for %s instance...",
            self.binary, modcls_name,
        )

        default = self.which()
        if default is not None:
            logger.debug(
                "found '%s'; "
                "not modifying PATH environment variable in instance of '%s'.",
                realpath(default), modcls_name)
            return True

        target = self.which_with_node_modules()

        if target:
            # Only setting the path specific for the binary; side effect
            # will be whoever else borrowing the _exec in here might not
            # get the binary they want.  That's why it's private.
            self.env_path = dirname(target)
            logger.debug(
                "located '%s' binary at '%s'; setting PATH environment "
                "variable for '%s' instance.",
                self.binary, self.env_path, modcls_name
            )
            return True
        else:
            logger.debug(
                "Unable to locate '%s'; not modifying PATH environment "
                "variable for instance of '%s'.",
                self.binary, modcls_name
            )
            return False

Response: Attempts to locate and set the PATH environment variable for the instance with the node modules defined for this instance.


Instruction:
Can you tell what is the following Python 3 function doing
def _exec(self, binary, stdin='', args=(), env={}):

        call_kw = self._gen_call_kws(**env)
        call_args = [self._get_exec_binary(call_kw)]
        call_args.extend(args)
        return fork_exec(call_args, stdin, **call_kw)

Response:
Executes the given binary using stdin and args with environment
        variables.


Instruction:
How would you implement a function in Python 3 that
dumps the contents of the object to a file - like object stream.

Response:
def dump(self, blob, stream):
        """
        Call json.dump with the attributes of this instance as
        arguments.
        """

        json.dump(
            blob, stream, indent=self.indent, sort_keys=True,
            separators=self.separators,
        )


Instruction:
Can you generate the documentation for the following Python 3 function
def dumps(self, blob):

        return json.dumps(
            blob, indent=self.indent, sort_keys=True,
            separators=self.separators,
        )

Response: Dump the contents of the object as a JSON string.


Instruction:
Write a Python 3 script to
join the current working directory with the current working directory.

Response:
def join_cwd(self, path=None):
        """
        Join the path with the current working directory.  If it is
        specified for this instance of the object it will be used,
        otherwise rely on the global value.
        """

        if self.working_dir:
            logger.debug(
                "'%s' instance 'working_dir' set to '%s' for join_cwd",
                type(self).__name__, self.working_dir,
            )
            cwd = self.working_dir
        else:
            cwd = getcwd()
            logger.debug(
                "'%s' instance 'working_dir' unset; "
                "default to process '%s' for join_cwd",
                type(self).__name__, cwd,
            )

        if path:
            return join(cwd, path)
        return cwd


Instruction:
Make a summary of the following Python 3 code
def _unicode_handler(obj):
    try:
        result = obj.isoformat()
    except AttributeError:
        raise TypeError("Unserializable object {} of type {}".format(obj,
                                                                     type(obj)))

    return result

Response: Transform an unicode string into a UTF - 8 equivalent.


Instruction:
Implement a Python 3 function for
encoding the data into a JSON structure from an instance from the current domain model.

Response:
def encode(self, entity):
        """
        Encodes the data, creating a JSON structure from an instance from the
        domain model.

        :param entity: the instance to encode
        :return: a JSON structure created from the received data
        """
        encoded = self._dict_encoder.encode(entity)

        if sys.version_info[0] == 2:
            result = json.dumps(encoded, ensure_ascii=False,
                                default=_iso_handler, encoding='latin1')
        else:
            # For Python 3
            result = json.dumps(encoded, ensure_ascii=False,
                                default=_iso_handler)

        return result