Instruction:
How would you code a function in Python 3 to
convert a FHIR JSON file to RDF

Response:
def fhir_json_to_rdf(json_fname: str,
                     base_uri: str = "http://hl7.org/fhir/",
                     target_graph: Optional[Graph] = None,
                     add_ontology_header: bool = True,
                     do_continuations: bool = True,
                     replace_narrative_text: bool = False,
                     metavoc: Optional[Union[Graph, FHIRMetaVoc]] = None) -> Graph:
    """
    Convert a FHIR JSON resource image to RDF
    :param json_fname: Name or URI of the file to convert
    :param base_uri: Base URI to use for relative references.
    :param target_graph:  If supplied, add RDF to this graph. If not, start with an empty graph.
    :param add_ontology_header:  True means add owl:Ontology declaration to output
    :param do_continuations: True means follow continuation records on bundles and queries
    :param replace_narrative_text: True means replace any narrative text longer than 120 characters with
                '<div xmlns="http://www.w3.org/1999/xhtml">(removed)</div>'
    :param metavoc: FHIR Metadata Vocabulary (fhir.ttl) graph
    :return: resulting graph
    """

    def check_for_continuation(data_: JsonObj) -> Optional[str]:
        if do_continuations and 'link' in data_ and isinstance(data_.link, list):
            for link_e in data_.link:
                if 'relation' in link_e and link_e.relation == 'next':
                    return link_e.url
        return None

    if target_graph is None:
        target_graph = Graph()

    if metavoc is None:
        metavoc = FHIRMetaVoc().g
    elif isinstance(metavoc, FHIRMetaVoc):
        metavoc = metavoc.g

    page_fname = json_fname
    while page_fname:
        data = load(page_fname)
        if 'resourceType' in data and data.resourceType != 'Bundle':
            FHIRResource(metavoc, None, base_uri, data, target=target_graph, add_ontology_header=add_ontology_header,
                         replace_narrative_text=replace_narrative_text)
            page_fname = check_for_continuation(data)
        elif 'entry' in data and isinstance(data.entry, list) and 'resource' in data.entry[0]:
            FHIRCollection(metavoc, None, base_uri, data, target=target_graph,
                           add_ontology_header=add_ontology_header if 'resourceType' in data else False,
                           replace_narrative_text=replace_narrative_text)
            page_fname = check_for_continuation(data)
        else:
            page_fname = None
            target_graph = None
    return target_graph


Instruction:
Here you have a function in Python 3, explain what it does
def subj_pred_idx_to_uri(s: URIRef, p: URIRef, idx: Optional[int] = None) -> URIRef:
    return URIRef(str(s) + '.' + str(p).rsplit('/', 1)[1] + ("_{}".format(idx) if idx is not None else ''))

Response: Converts FHIR subject predicate and entry index into a URI.


Instruction:
Write a Python 3 script to
transform the BNode whose subject is s into its equivalent skolemized equivalent.

Response:
def map_node(s: Node, sk_s: URIRef, gin: Graph, gout: Graph) -> None:
    """
    Transform the BNode whose subject is s into its equivalent, replacing s with its 'skolemized' equivalent
    :param s: Actual subject
    :param sk_s: Equivalent URI of subject in output graph
    :param gin: Input graph
    :param gout: Output graph
    """
    for p, o in gin.predicate_objects(s):
        if not isinstance(o, BNode):
            gout.add((sk_s, p, o))
        else:
            sk_o = subj_pred_idx_to_uri(sk_s, p, gin.value(o, FHIR.index))
            gout.add((sk_s, p, sk_o))
            map_node(o, sk_o, gin, gout)


Instruction:
Explain what the following Python 3 code does
def skolemize(gin: Graph) -> Graph:
    gout = Graph()

    # Emit any unreferenced subject BNodes (boxes)
    anon_subjs = [s for s in gin.subjects() if isinstance(s, BNode) and len([gin.subject_predicates(s)]) == 0]
    if anon_subjs:
        idx = None if len(anon_subjs) == 1 else 0
        for s in anon_subjs:
            map_node(s, FHIR['treeRoot' + ('_{}'.format(idx) if idx is not None else '')], gin, gout)
            if idx is not None:
                idx += 1

    # Cover all other non-bnode entries
    for subj in set(s for s in gin.subjects() if isinstance(s, URIRef)):
        map_node(subj, subj, gin, gout)
    return gout

Response: Takes a Gin graph and returns a new graph containing all of the blank nodes in the graph.


Instruction:
Can you tell what is the following Python 3 function doing
def complete_definition(subj: Node,
                        source_graph: Graph,
                        target_graph: Optional[Graph]=None) -> PrettyGraph:
    """
    Return the transitive closure of subject.
    :param subj: URI or BNode for subject
    :param source_graph: Graph containing defininition
    :param target_graph: return graph (for recursion)
    :return: target_graph
    """
    if target_graph is None:
        target_graph = PrettyGraph()
    for p, o in source_graph.predicate_objects(subj):
        target_graph.add((subj, p, o))
        if isinstance(o, BNode):
            complete_definition(o, source_graph, target_graph)
    return target_graph

Response: Complete the transitive closure of subject.


Instruction:
Can you implement a function in Python 3 that
dumps a graph g in a sorted n3 format

Response:
def dump_nt_sorted(g: Graph) -> List[str]:
    """
    Dump graph g in a sorted n3 format
    :param g: graph to dump
    :return: stringified representation of g
    """
    return [l.decode('ascii') for l in sorted(g.serialize(format='nt').splitlines()) if l]


Instruction:
Explain what the following Python 3 code does
def rdf_compare(g1: Graph, g2: Graph, ignore_owl_version: bool=False, ignore_type_arcs: bool = False,
                compare_filter: Optional[Callable[[Graph, Graph, Graph], None]]=None) -> str:
    """
    Compare graph g1 and g2
    :param g1: first graph
    :param g2: second graph
    :param ignore_owl_version:
    :param ignore_type_arcs:
    :param compare_filter: Final adjustment for graph difference. Used, for example, to deal with FHIR decimal problems.
    :return: List of differences as printable lines or blank if everything matches
    """
    def graph_for_subject(g: Graph, subj: Node) -> Graph:
        subj_in_g = complete_definition(subj, g)
        if ignore_type_arcs:
            for ta_s, ta_o in subj_in_g.subject_objects(RDF.type):
                if isinstance(ta_s, BNode) and isinstance(ta_o, URIRef):
                    subj_in_g.remove((ta_s, RDF.type, ta_o))
        if ignore_owl_version:
            subj_in_g.remove((subj, OWL.versionIRI, subj_in_g.value(subj, OWL.versionIRI)))
        return subj_in_g

    def primary_subjects(g: Graph) -> Set[Node]:
        anon_subjs = set(anon_s for anon_s in g.subjects()
                         if isinstance(anon_s, BNode) and len([g.subject_predicates(anon_s)]) == 0)
        return set(s_ for s_ in g1.subjects() if isinstance(s_, URIRef)).union(anon_subjs)

    rval = ""

    # Step 1: Find any subjects in one graph that don't exist in the other
    g1_subjs = primary_subjects(g1)
    g2_subjs = primary_subjects(g2)
    for s in g1_subjs - g2_subjs:
        rval += "\n===== Subjects in Graph 1 but not Graph 2: "
        rval += PrettyGraph.strip_prefixes(complete_definition(s, g1))
    for s in g2_subjs - g1_subjs:
        rval += "\n===== Subjects in Graph 2 but not Graph 1: "
        rval += PrettyGraph.strip_prefixes(complete_definition(s, g2))

    # Step 2: Iterate over all of the remaining subjects comparing their contents
    for s in g1_subjs.intersection(g2_subjs):
        s_in_g1 = graph_for_subject(g1, s)
        s_in_g2 = graph_for_subject(g2, s)
        in_both, in_first, in_second = graph_diff(skolemize(s_in_g1), skolemize(s_in_g2))
        if compare_filter:
            compare_filter(in_both, in_first, in_second)
        if len(list(in_first)) or len(list(in_second)):
            rval += "\n\nSubject {} DIFFERENCE: ".format(s) + '=' * 30
            if len(in_first):
                rval += "\n\t----> First: \n" + '\n'.join(dump_nt_sorted(in_first))
            if len(in_second):
                rval += "\n\t----> Second: \n" + '\n'.join(dump_nt_sorted(in_second))
            rval += '-' * 40
    return rval

Response: Compare two graphs g1 and g2 and returns a list of differences as printable lines or blank if everything matches.


Instruction:
Can you generate the documentation for the following Python 3 function
def proc_file(infile: str, outfile: str, opts: Namespace) -> bool:
    g = fhir_json_to_rdf(infile, opts.uribase, opts.graph, add_ontology_header=not opts.noontology,
                         do_continuations=not opts.nocontinuation, replace_narrative_text=bool(opts.nonarrative),
                         metavoc=opts.fhir_metavoc)

    # If we aren't carrying graph in opts, we're doing a file by file transformation
    if g:
        if not opts.graph:
            serialize_graph(g, outfile, opts)
        return True
    else:
        print("{} : Not a FHIR collection or resource".format(infile))
        return False

Response:
Process infile.
    :param infile: input file to be processed
    :param outfile: target output file.
    :param opts:
    :return:


Instruction:
Write a Python 3 script to
determine whether to process a file based on the input file name and directory.

Response:
def file_filter(ifn: str, indir: str, opts: Namespace) -> bool:
    """
    Determine whether to process ifn.  We con't process:
        1) Anything in a directory having a path element that begins with "_"
        2) Really, really big files
        3) Temporary lists of know errors
    :param ifn: input file name
    :param indir: input directory
    :param opts: argparse options
    :return: True if to be processed, false if to be skipped
    """
    # If it looks like we're processing a URL as an input file, skip the suffix check
    if '://' in ifn:
        return True

    if not ifn.endswith('.json'):
        return False

    if indir and (indir.startswith("_") or "/_" in indir or any(dn in indir for dn in opts.skipdirs)):
        return False

    if opts.skipfns and any(sfn in ifn for sfn in opts.skipfns):
        return False

    infile = os.path.join(indir, ifn)
    if not opts.infile and opts.maxsize and os.path.getsize(infile) > (opts.maxsize * 1000):
        return False

    return True


Instruction:
Can you tell what is the following Python 3 function doing
def fhirtordf(argv: List[str], default_exit: bool = True) -> bool:
    dlp = dirlistproc.DirectoryListProcessor(argv,
                                             description="Convert FHIR JSON into RDF",
                                             infile_suffix=".json",
                                             outfile_suffix=".ttl",
                                             addargs=addargs,
                                             noexit=not default_exit)

    if not dlp.successful_parse:
        return False

    # Version
    if dlp.opts.version:
        print("FHIR to RDF Conversion Tool -- Version {}".format(__version__))

    # We either have to have an input file or an input directory
    if not dlp.opts.infile and not dlp.opts.indir:
        if not dlp.opts.version:
            dlp.parser.error("Either an input file or an input directory must be supplied")
        return dlp.opts.version

    # Create the output directory if needed
    if dlp.opts.outdir and not os.path.exists(dlp.opts.outdir):
        os.makedirs(dlp.opts.outdir)

    # If we are going to a single output file or stdout, gather all the input
    dlp.opts.graph = Graph() if (not dlp.opts.outfile and not dlp.opts.outdir) or\
                                (dlp.opts.outfile and len(dlp.opts.outfile) == 1) else None
    dlp.opts.fhir_metavoc = load_fhir_ontology(dlp.opts)

    # If it looks like we're processing a URL as an input file, skip the suffix check
    if dlp.opts.infile and len(dlp.opts.infile) == 1 and not dlp.opts.indir and "://" in dlp.opts.infile[0]:
        dlp.infile_suffix = ""
    dlp.outfile_suffix = '.' + suffix_for(dlp.opts.format)
    nfiles, nsuccess = dlp.run(proc=proc_file, file_filter_2=file_filter)
    if nfiles:
        if dlp.opts.graph:
            serialize_graph(dlp.opts.graph, dlp.opts.outfile[0] if dlp.opts.outfile else None, dlp.opts)
        return nsuccess > 0
    return False

Response: Entry point for FHIR to RDF conversion tool


Instruction:
Create a Python 3 function for
parsing a resource URI into a tuple of namespace type and resource.

Response:
def parse_fhir_resource_uri(uri: Union[URIRef, str]) -> FHIR_RESOURCE:
    """
    Use the FHIR Regular Expression for Resource URI's to determine the namespace and type
    of a given URI.  As an example, "http://hl7.org/fhir/Patient/p123" maps to the tuple
    ``('Patient', 'http://hl7.org/fhir')

    :param uri:  URI to parse
    :return: FHIR_RESOURCE (namespace, type, resource)
    """
    uri_str = str(uri)
    m = FHIR_RESOURCE_RE.match(uri_str)
    if m:
        return FHIR_RESOURCE(URIRef(m.group(FHIR_RE_BASE)), FHIR[m.group(FHIR_RE_RESOURCE)], m.group(FHIR_RE_ID))
    else:
        # Not in the FHIR format - we can only do namespace and name
        namespace, name = uri_str.rsplit('#', 1) if '#' in uri_str \
            else uri_str.rsplit('/', 1) if '/' in uri_str else (None, uri_str)

        return FHIR_RESOURCE(URIRef(namespace), None, name)


Instruction:
Write a Python 3 function that can
return a dummy distribution object that can be used to instrument the setup environment before calling the actual setup function.

Response:
def get_dummy_distribution():
    """
    Returns a distutils Distribution object used to instrument the setup
    environment before calling the actual setup() function.
    """

    from .setup_helpers import _module_state

    if _module_state['registered_commands'] is None:
        raise RuntimeError(
            'astropy_helpers.setup_helpers.register_commands() must be '
            'called before using '
            'astropy_helpers.setup_helpers.get_dummy_distribution()')

    # Pre-parse the Distutils command-line options and config files to if
    # the option is set.
    dist = Distribution({'script_name': os.path.basename(sys.argv[0]),
                         'script_args': sys.argv[1:]})
    dist.cmdclass.update(_module_state['registered_commands'])

    with silence():
        try:
            dist.parse_config_files()
            dist.parse_command_line()
        except (DistutilsError, AttributeError, SystemExit):
            # Let distutils handle DistutilsErrors itself AttributeErrors can
            # get raise for ./setup.py --help SystemExit can be raised if a
            # display option was used, for example
            pass

    return dist


Instruction:
Explain what the following Python 3 code does
def get_distutils_option(option, commands):

    dist = get_dummy_distribution()

    for cmd in commands:
        cmd_opts = dist.command_options.get(cmd)
        if cmd_opts is not None and option in cmd_opts:
            return cmd_opts[option][1]
    else:
        return None

Response: Returns the value of the given distutils option.


Instruction:
Write a Python 3 script to
add a custom option to a command object.

Response:
def add_command_option(command, name, doc, is_bool=False):
    """
    Add a custom option to a setup command.

    Issues a warning if the option already exists on that command.

    Parameters
    ----------
    command : str
        The name of the command as given on the command line

    name : str
        The name of the build option

    doc : str
        A short description of the option, for the `--help` message

    is_bool : bool, optional
        When `True`, the option is a boolean option and doesn't
        require an associated value.
    """

    dist = get_dummy_distribution()
    cmdcls = dist.get_command_class(command)

    if (hasattr(cmdcls, '_astropy_helpers_options') and
            name in cmdcls._astropy_helpers_options):
        return

    attr = name.replace('-', '_')

    if hasattr(cmdcls, attr):
        raise RuntimeError(
            '{0!r} already has a {1!r} class attribute, barring {2!r} from '
            'being usable as a custom option name.'.format(cmdcls, attr, name))

    for idx, cmd in enumerate(cmdcls.user_options):
        if cmd[0] == name:
            log.warn('Overriding existing {0!r} option '
                     '{1!r}'.format(command, name))
            del cmdcls.user_options[idx]
            if name in cmdcls.boolean_options:
                cmdcls.boolean_options.remove(name)
            break

    cmdcls.user_options.append((name, None, doc))

    if is_bool:
        cmdcls.boolean_options.append(name)

    # Distutils' command parsing requires that a command object have an
    # attribute with the same name as the option (with '-' replaced with '_')
    # in order for that option to be recognized as valid
    setattr(cmdcls, attr, None)

    # This caches the options added through add_command_option so that if it is
    # run multiple times in the same interpreter repeated adds are ignored
    # (this way we can still raise a RuntimeError if a custom option overrides
    # a built-in option)
    if not hasattr(cmdcls, '_astropy_helpers_options'):
        cmdcls._astropy_helpers_options = set([name])
    else:
        cmdcls._astropy_helpers_options.add(name)


Instruction:
Write a Python 3 script for
returning a set of all distutils display options in their long and short form.

Response:
def get_distutils_display_options():
    """ Returns a set of all the distutils display options in their long and
    short forms.  These are the setup.py arguments such as --name or --version
    which print the project's metadata and then exit.

    Returns
    -------
    opts : set
        The long and short form display option arguments, including the - or --
    """

    short_display_opts = set('-' + o[1] for o in Distribution.display_options
                             if o[1])
    long_display_opts = set('--' + o[0] for o in Distribution.display_options)

    # Include -h and --help which are not explicitly listed in
    # Distribution.display_options (as they are handled by optparse)
    short_display_opts.add('-h')
    long_display_opts.add('--help')

    # This isn't the greatest approach to hardcode these commands.
    # However, there doesn't seem to be a good way to determine
    # whether build *will be* run as part of the command at this
    # phase.
    display_commands = set([
        'clean', 'register', 'setopt', 'saveopts', 'egg_info',
        'alias'])

    return short_display_opts.union(long_display_opts.union(display_commands))


Instruction:
Can you tell what is the following Python 3 function doing
def ensure_sphinx_astropy_installed():
    # We've split out the Sphinx part of astropy-helpers into sphinx-astropy
    # but we want it to be auto-installed seamlessly for anyone using
    # build_docs. We check if it's already installed, and if not, we install
    # it to a local .eggs directory and add the eggs to the path (these
    # have to each be added to the path, we can't add them by simply adding
    # .eggs to the path)
    sys_path_inserts = []
    sphinx_astropy_version = None
    try:
        from sphinx_astropy import __version__ as sphinx_astropy_version  # noqa
    except ImportError:

        from setuptools import Distribution
        dist = Distribution()

        # Numpydoc 0.9.0 requires sphinx 1.6+, we can limit the version here
        # until we also makes our minimum required version Sphinx 1.6
        if SPHINX_LT_16:
            dist.fetch_build_eggs('numpydoc<0.9')

        # This egg build doesn't respect python_requires, not aware of
        # pre-releases. We know that mpl 3.1+ requires Python 3.6+, so this
        # ugly workaround takes care of it until there is a solution for
        # https://github.com/astropy/astropy-helpers/issues/462
        if LooseVersion(sys.version) < LooseVersion('3.6'):
            dist.fetch_build_eggs('matplotlib<3.1')

        eggs = dist.fetch_build_eggs('sphinx-astropy')

        # Find out the version of sphinx-astropy if possible. For some old
        # setuptools version, eggs will be None even if sphinx-astropy was
        # successfully installed.
        if eggs is not None:
            for egg in eggs:
                if egg.project_name == 'sphinx-astropy':
                    sphinx_astropy_version = egg.parsed_version.public
                    break

        eggs_path = os.path.abspath('.eggs')
        for egg in glob.glob(os.path.join(eggs_path, '*.egg')):
            sys_path_inserts.append(egg)

    return sphinx_astropy_version, sys_path_inserts

Response: Ensure that sphinx - astropy is available installing it temporarily if not.


Instruction:
Write a Python 3 function that can
return the name of the appropriate platform - specific build subdirectory directory.

Response:
def _get_platlib_dir(cmd):
    """
    Given a build command, return the name of the appropriate platform-specific
    build subdirectory directory (e.g. build/lib.linux-x86_64-2.7)
    """

    plat_specifier = '.{0}-{1}'.format(cmd.plat_name, sys.version[0:3])
    return os.path.join(cmd.build_base, 'lib' + plat_specifier)


Instruction:
Implement a function in Python 3 to
get the path to the numpy headers.

Response:
def get_numpy_include_path():
    """
    Gets the path to the numpy headers.
    """
    # We need to go through this nonsense in case setuptools
    # downloaded and installed Numpy for us as part of the build or
    # install, since Numpy may still think it's in "setup mode", when
    # in fact we're ready to use it to build astropy now.

    import builtins
    if hasattr(builtins, '__NUMPY_SETUP__'):
        del builtins.__NUMPY_SETUP__
    import imp
    import numpy
    imp.reload(numpy)

    try:
        numpy_include = numpy.get_include()
    except AttributeError:
        numpy_include = numpy.get_numpy_include()
    return numpy_include


Instruction:
Can you create a Python 3 function that
determines if a given file or directory is hidden.

Response:
def is_path_hidden(filepath):
    """
    Determines if a given file or directory is hidden.

    Parameters
    ----------
    filepath : str
        The path to a file or directory

    Returns
    -------
    hidden : bool
        Returns `True` if the file is hidden
    """

    name = os.path.basename(os.path.abspath(filepath))
    if isinstance(name, bytes):
        is_dotted = name.startswith(b'.')
    else:
        is_dotted = name.startswith('.')
    return is_dotted or _has_hidden_attribute(filepath)


Instruction:
Can you tell what is the following Python 3 function doing
def write_if_different(filename, data):

    assert isinstance(data, bytes)

    if os.path.exists(filename):
        with open(filename, 'rb') as fd:
            original_data = fd.read()
    else:
        original_data = None

    if original_data != data:
        with open(filename, 'wb') as fd:
            fd.write(data)

Response: Writes data to filename if the content of the file is different.


Instruction:
Can you tell what is the following Python 3 function doing
def import_file(filename, name=None):
    # Specifying a traditional dot-separated fully qualified name here
    # results in a number of "Parent module 'astropy' not found while
    # handling absolute import" warnings.  Using the same name, the
    # namespaces of the modules get merged together.  So, this
    # generates an underscore-separated name which is more likely to
    # be unique, and it doesn't really matter because the name isn't
    # used directly here anyway.
    mode = 'r'

    if name is None:
        basename = os.path.splitext(filename)[0]
        name = '_'.join(os.path.relpath(basename).split(os.sep)[1:])

    if not os.path.exists(filename):
        raise ImportError('Could not import file {0}'.format(filename))

    if import_machinery:
        loader = import_machinery.SourceFileLoader(name, filename)
        mod = loader.load_module()
    else:
        with open(filename, mode) as fd:
            mod = imp.load_module(name, fd, filename, ('.py', mode, 1))

    return mod

Response: Imports a module from a single file as if it doesn t belong to a specific package.


Instruction:
Explain what the following Python 3 code does
def extends_doc(extended_func):

    def decorator(func):
        if not (extended_func.__doc__ is None or func.__doc__ is None):
            func.__doc__ = '\n\n'.join([extended_func.__doc__.rstrip('\n'),
                                        func.__doc__.lstrip('\n')])
        return func

    return decorator

Response:
A function decorator for use when wrapping an existing function but adding additional functionality to the docstring of the original
   .


Instruction:
Write a Python 3 script to
find data files in package that match pattern.

Response:
def find_data_files(package, pattern):
    """
    Include files matching ``pattern`` inside ``package``.

    Parameters
    ----------
    package : str
        The package inside which to look for data files
    pattern : str
        Pattern (glob-style) to match for the data files (e.g. ``*.dat``).
        This supports the``**``recursive syntax. For example, ``**/*.fits``
        matches all files ending with ``.fits`` recursively. Only one
        instance of ``**`` can be included in the pattern.
    """

    return glob.glob(os.path.join(package, pattern), recursive=True)


Instruction:
Create a Python 3 function to
split a version string into major minor and micro.

Response:
def _version_split(version):
    """
    Split a version string into major, minor, and bugfix numbers.  If any of
    those numbers are missing the default is zero.  Any pre/post release
    modifiers are ignored.

    Examples
    ========
    >>> _version_split('1.2.3')
    (1, 2, 3)
    >>> _version_split('1.2')
    (1, 2, 0)
    >>> _version_split('1.2rc1')
    (1, 2, 0)
    >>> _version_split('1')
    (1, 0, 0)
    >>> _version_split('')
    (0, 0, 0)
    """

    parsed_version = pkg_resources.parse_version(version)

    if hasattr(parsed_version, 'base_version'):
        # New version parsing for setuptools >= 8.0
        if parsed_version.base_version:
            parts = [int(part)
                     for part in parsed_version.base_version.split('.')]
        else:
            parts = []
    else:
        parts = []
        for part in parsed_version:
            if part.startswith('*'):
                # Ignore any .dev, a, b, rc, etc.
                break
            parts.append(int(part))

    if len(parts) < 3:
        parts += [0] * (3 - len(parts))

    # In principle a version could have more parts (like 1.2.3.4) but we only
    # support <major>.<minor>.<micro>
    return tuple(parts[:3])


Instruction:
Can you create a Python 3 function that
generates a git header for the version. py module.

Response:
def _generate_git_header(packagename, version, githash):
    """
    Generates a header to the version.py module that includes utilities for
    probing the git repository for updates (to the current git hash, etc.)
    These utilities should only be available in development versions, and not
    in release builds.

    If this fails for any reason an empty string is returned.
    """

    loader = pkgutil.get_loader(git_helpers)
    source = loader.get_source(git_helpers.__name__) or ''
    source_lines = source.splitlines()
    if not source_lines:
        log.warn('Cannot get source code for astropy_helpers.git_helpers; '
                 'git support disabled.')
        return ''

    idx = 0
    for idx, line in enumerate(source_lines):
        if line.startswith('# BEGIN'):
            break
    git_helpers_py = '\n'.join(source_lines[idx + 1:])

    verstr = version

    new_githash = git_helpers.get_git_devstr(sha=True, show_warning=False)

    if new_githash:
        githash = new_githash

    return _FROZEN_VERSION_PY_WITH_GIT_HEADER.format(
                git_helpers=git_helpers_py, packagename=packagename,
                verstr=verstr, githash=githash)


Instruction:
Given the following Python 3 function, write the documentation
def generate_version_py(packagename=None, version=None, release=None, debug=None,
                        uses_git=None, srcdir='.'):
    """
    Generate a version.py file in the package with version information, and
    update developer version strings.

    This function should normally be called without any arguments. In this case
    the package name and version is read in from the ``setup.cfg`` file (from
    the ``name`` or ``package_name`` entry and the ``version`` entry in the
    ``[metadata]`` section).

    If the version is a developer version (of the form ``3.2.dev``), the
    version string will automatically be expanded to include a sequential
    number as a suffix (e.g. ``3.2.dev13312``), and the updated version string
    will be returned by this function.

    Based on this updated version string, a ``version.py`` file will be
    generated inside the package, containing the version string as well as more
    detailed information (for example the major, minor, and bugfix version
    numbers, a ``release`` flag indicating whether the current version is a
    stable or developer version, and so on.
    """

    if packagename is not None:
        warnings.warn('The packagename argument to generate_version_py has '
                      'been deprecated and will be removed in future. Specify '
                      'the package name in setup.cfg instead', AstropyDeprecationWarning)

    if version is not None:
        warnings.warn('The version argument to generate_version_py has '
                      'been deprecated and will be removed in future. Specify '
                      'the version number in setup.cfg instead', AstropyDeprecationWarning)

    if release is not None:
        warnings.warn('The release argument to generate_version_py has '
                      'been deprecated and will be removed in future. We now '
                      'use the presence of the "dev" string in the version to '
                      'determine whether this is a release', AstropyDeprecationWarning)

    # We use ConfigParser instead of read_configuration here because the latter
    # only reads in keys recognized by setuptools, but we need to access
    # package_name below.
    conf = ConfigParser()
    conf.read('setup.cfg')

    if conf.has_option('metadata', 'name'):
        packagename = conf.get('metadata', 'name')
    elif conf.has_option('metadata', 'package_name'):
        # The package-template used package_name instead of name for a while
        warnings.warn('Specifying the package name using the "package_name" '
                      'option in setup.cfg is deprecated - use the "name" '
                      'option instead.', AstropyDeprecationWarning)
        packagename = conf.get('metadata', 'package_name')
    elif packagename is not None:  # deprecated
        pass
    else:
        sys.stderr.write('ERROR: Could not read package name from setup.cfg\n')
        sys.exit(1)

    if conf.has_option('metadata', 'version'):
        version = conf.get('metadata', 'version')
        add_git_devstr = True
    elif version is not None:  # deprecated
        add_git_devstr = False
    else:
        sys.stderr.write('ERROR: Could not read package version from setup.cfg\n')
        sys.exit(1)

    if release is None:
        release = 'dev' not in version

    if not release and add_git_devstr:
        version += get_git_devstr(False)

    if uses_git is None:
        uses_git = not release

    # In some cases, packages have a - but this is a _ in the module. Since we
    # are only interested in the module here, we replace - by _
    packagename = packagename.replace('-', '_')

    try:
        version_module = get_pkg_version_module(packagename)

        try:
            last_generated_version = version_module._last_generated_version
        except AttributeError:
            last_generated_version = version_module.version

        try:
            last_githash = version_module._last_githash
        except AttributeError:
            last_githash = version_module.githash

        current_release = version_module.release
        current_debug = version_module.debug
    except ImportError:
        version_module = None
        last_generated_version = None
        last_githash = None
        current_release = None
        current_debug = None

    if release is None:
        # Keep whatever the current value is, if it exists
        release = bool(current_release)

    if debug is None:
        # Likewise, keep whatever the current value is, if it exists
        debug = bool(current_debug)

    package_srcdir = os.path.join(srcdir, *packagename.split('.'))
    version_py = os.path.join(package_srcdir, 'version.py')

    if (last_generated_version != version or current_release != release or
            current_debug != debug):
        if '-q' not in sys.argv and '--quiet' not in sys.argv:
            log.set_threshold(log.INFO)

        if is_distutils_display_option():
            # Always silence unnecessary log messages when display options are
            # being used
            log.set_threshold(log.WARN)

        log.info('Freezing version number to {0}'.format(version_py))

        with open(version_py, 'w') as f:
            # This overwrites the actual version.py
            f.write(_get_version_py_str(packagename, version, last_githash,
                                        release, debug, uses_git=uses_git))

    return version

Response: Generates a version. py file in the package with version information.


Instruction:
Explain what the following Python 3 code does
def get_pkg_version_module(packagename, fromlist=None):

    version = import_file(os.path.join(packagename, 'version.py'), name='version')

    if fromlist:
        return tuple(getattr(version, member) for member in fromlist)
    else:
        return version

Response: Returns the. version module generated by astropy_helpers. version_helpers. generate_version_py.


Instruction:
Write a Python 3 script to
determine if the current build is in debug mode.

Response:
def get_debug_option(packagename):
    """ Determines if the build is in debug mode.

    Returns
    -------
    debug : bool
        True if the current build was started with the debug option, False
        otherwise.

    """

    try:
        current_debug = get_pkg_version_module(packagename,
                                               fromlist=['debug'])[0]
    except (ImportError, AttributeError):
        current_debug = None

    # Only modify the debug flag if one of the build commands was explicitly
    # run (i.e. not as a sub-command of something else)
    dist = get_dummy_distribution()
    if any(cmd in dist.commands for cmd in ['build', 'build_ext']):
        debug = bool(get_distutils_build_option('debug'))
    else:
        debug = bool(current_debug)

    if current_debug is not None and current_debug != debug:
        build_ext_cmd = dist.get_command_class('build_ext')
        build_ext_cmd._force_rebuild = True

    return debug


Instruction:
Given the following Python 3 function, write the documentation
def register_commands(package=None, version=None, release=None, srcdir='.'):

    if package is not None:
        warnings.warn('The package argument to generate_version_py has '
                      'been deprecated and will be removed in future. Specify '
                      'the package name in setup.cfg instead', AstropyDeprecationWarning)

    if version is not None:
        warnings.warn('The version argument to generate_version_py has '
                      'been deprecated and will be removed in future. Specify '
                      'the version number in setup.cfg instead', AstropyDeprecationWarning)

    if release is not None:
        warnings.warn('The release argument to generate_version_py has '
                      'been deprecated and will be removed in future. We now '
                      'use the presence of the "dev" string in the version to '
                      'determine whether this is a release', AstropyDeprecationWarning)

    # We use ConfigParser instead of read_configuration here because the latter
    # only reads in keys recognized by setuptools, but we need to access
    # package_name below.
    conf = ConfigParser()
    conf.read('setup.cfg')

    if conf.has_option('metadata', 'name'):
        package = conf.get('metadata', 'name')
    elif conf.has_option('metadata', 'package_name'):
        # The package-template used package_name instead of name for a while
        warnings.warn('Specifying the package name using the "package_name" '
                      'option in setup.cfg is deprecated - use the "name" '
                      'option instead.', AstropyDeprecationWarning)
        package = conf.get('metadata', 'package_name')
    elif package is not None:  # deprecated
        pass
    else:
        sys.stderr.write('ERROR: Could not read package name from setup.cfg\n')
        sys.exit(1)

    if _module_state['registered_commands'] is not None:
        return _module_state['registered_commands']

    if _module_state['have_sphinx']:
        try:
            from .commands.build_sphinx import (AstropyBuildSphinx,
                                                AstropyBuildDocs)
        except ImportError:
            AstropyBuildSphinx = AstropyBuildDocs = FakeBuildSphinx
    else:
        AstropyBuildSphinx = AstropyBuildDocs = FakeBuildSphinx

    _module_state['registered_commands'] = registered_commands = {
        'test': generate_test_command(package),

        # Use distutils' sdist because it respects package_data.
        # setuptools/distributes sdist requires duplication of information in
        # MANIFEST.in
        'sdist': DistutilsSdist,

        'build_ext': AstropyHelpersBuildExt,
        'build_sphinx': AstropyBuildSphinx,
        'build_docs': AstropyBuildDocs
    }

    # Need to override the __name__ here so that the commandline options are
    # presented as being related to the "build" command, for example; normally
    # this wouldn't be necessary since commands also have a command_name
    # attribute, but there is a bug in distutils' help display code that it
    # uses __name__ instead of command_name. Yay distutils!
    for name, cls in registered_commands.items():
        cls.__name__ = name

    # Add a few custom options; more of these can be added by specific packages
    # later
    for option in [
            ('use-system-libraries',
             "Use system libraries whenever possible", True)]:
        add_command_option('build', *option)
        add_command_option('install', *option)

    add_command_hooks(registered_commands, srcdir=srcdir)

    return registered_commands

Response: This function generates a dictionary containing customized commands that can be passed to the cmdclass argument in setup. cfg.


Instruction:
Can you write a function in Python 3 where it
adds a list of distutils command hooks to the passed in dict.

Response:
def add_command_hooks(commands, srcdir='.'):
    """
    Look through setup_package.py modules for functions with names like
    ``pre_<command_name>_hook`` and ``post_<command_name>_hook`` where
    ``<command_name>`` is the name of a ``setup.py`` command (e.g. build_ext).

    If either hook is present this adds a wrapped version of that command to
    the passed in ``commands`` `dict`.  ``commands`` may be pre-populated with
    other custom distutils command classes that should be wrapped if there are
    hooks for them (e.g. `AstropyBuildPy`).
    """

    hook_re = re.compile(r'^(pre|post)_(.+)_hook$')

    # Distutils commands have a method of the same name, but it is not a
    # *classmethod* (which probably didn't exist when distutils was first
    # written)
    def get_command_name(cmdcls):
        if hasattr(cmdcls, 'command_name'):
            return cmdcls.command_name
        else:
            return cmdcls.__name__

    packages = find_packages(srcdir)
    dist = get_dummy_distribution()

    hooks = collections.defaultdict(dict)

    for setuppkg in iter_setup_packages(srcdir, packages):
        for name, obj in vars(setuppkg).items():
            match = hook_re.match(name)
            if not match:
                continue

            hook_type = match.group(1)
            cmd_name = match.group(2)

            if hook_type not in hooks[cmd_name]:
                hooks[cmd_name][hook_type] = []

            hooks[cmd_name][hook_type].append((setuppkg.__name__, obj))

    for cmd_name, cmd_hooks in hooks.items():
        commands[cmd_name] = generate_hooked_command(
            cmd_name, dist.get_command_class(cmd_name), cmd_hooks)


Instruction:
Can you implement a function in Python 3 that
generates a new command that runs pre - and post - command hooks for that command before and after the cmd_cls.

Response:
def generate_hooked_command(cmd_name, cmd_cls, hooks):
    """
    Returns a generated subclass of ``cmd_cls`` that runs the pre- and
    post-command hooks for that command before and after the ``cmd_cls.run``
    method.
    """

    def run(self, orig_run=cmd_cls.run):
        self.run_command_hooks('pre_hooks')
        orig_run(self)
        self.run_command_hooks('post_hooks')

    return type(cmd_name, (cmd_cls, object),
                {'run': run, 'run_command_hooks': run_command_hooks,
                 'pre_hooks': hooks.get('pre', []),
                 'post_hooks': hooks.get('post', [])})


Instruction:
Write a Python 3 function for
running all the command hooks registered for that command and phase.

Response:
def run_command_hooks(cmd_obj, hook_kind):
    """Run hooks registered for that command and phase.

    *cmd_obj* is a finalized command object; *hook_kind* is either
    'pre_hook' or 'post_hook'.
    """

    hooks = getattr(cmd_obj, hook_kind, None)

    if not hooks:
        return

    for modname, hook in hooks:
        if isinstance(hook, str):
            try:
                hook_obj = resolve_name(hook)
            except ImportError as exc:
                raise DistutilsModuleError(
                    'cannot find hook {0}: {1}'.format(hook, exc))
        else:
            hook_obj = hook

        if not callable(hook_obj):
            raise DistutilsOptionError('hook {0!r} is not callable' % hook)

        log.info('running {0} from {1} for {2} command'.format(
                 hook_kind.rstrip('s'), modname, cmd_obj.get_command_name()))

        try:
            hook_obj(cmd_obj)
        except Exception:
            log.error('{0} command hook {1} raised an exception: %s\n'.format(
                hook_obj.__name__, cmd_obj.get_command_name()))
            log.error(traceback.format_exc())
            sys.exit(1)


Instruction:
Create a Python 3 function for
updating the package_data and packagenames with the contents of the packages.

Response:
def update_package_files(srcdir, extensions, package_data, packagenames,
                         package_dirs):
    """
    This function is deprecated and maintained for backward compatibility
    with affiliated packages.  Affiliated packages should update their
    setup.py to use `get_package_info` instead.
    """

    info = get_package_info(srcdir)
    extensions.extend(info['ext_modules'])
    package_data.update(info['package_data'])
    packagenames = list(set(packagenames + info['packages']))
    package_dirs.update(info['package_dir'])


Instruction:
Explain what the following Python 3 code does
def get_package_info(srcdir='.', exclude=()):
    ext_modules = []
    packages = []
    package_dir = {}

    # Read in existing package data, and add to it below
    setup_cfg = os.path.join(srcdir, 'setup.cfg')
    if os.path.exists(setup_cfg):
        conf = read_configuration(setup_cfg)
        if 'options' in conf and 'package_data' in conf['options']:
            package_data = conf['options']['package_data']
        else:
            package_data = {}
    else:
        package_data = {}

    if exclude:
        warnings.warn(
            "Use of the exclude parameter is no longer supported since it does "
            "not work as expected. Use add_exclude_packages instead. Note that "
            "it must be called prior to any other calls from setup helpers.",
            AstropyDeprecationWarning)

    # Use the find_packages tool to locate all packages and modules
    packages = find_packages(srcdir, exclude=exclude)

    # Update package_dir if the package lies in a subdirectory
    if srcdir != '.':
        package_dir[''] = srcdir

    # For each of the setup_package.py modules, extract any
    # information that is needed to install them.  The build options
    # are extracted first, so that their values will be available in
    # subsequent calls to `get_extensions`, etc.
    for setuppkg in iter_setup_packages(srcdir, packages):
        if hasattr(setuppkg, 'get_build_options'):
            options = setuppkg.get_build_options()
            for option in options:
                add_command_option('build', *option)
        if hasattr(setuppkg, 'get_external_libraries'):
            libraries = setuppkg.get_external_libraries()
            for library in libraries:
                add_external_library(library)

    for setuppkg in iter_setup_packages(srcdir, packages):
        # get_extensions must include any Cython extensions by their .pyx
        # filename.
        if hasattr(setuppkg, 'get_extensions'):
            ext_modules.extend(setuppkg.get_extensions())
        if hasattr(setuppkg, 'get_package_data'):
            package_data.update(setuppkg.get_package_data())

    # Locate any .pyx files not already specified, and add their extensions in.
    # The default include dirs include numpy to facilitate numerical work.
    ext_modules.extend(get_cython_extensions(srcdir, packages, ext_modules,
                                             ['numpy']))

    # Now remove extensions that have the special name 'skip_cython', as they
    # exist Only to indicate that the cython extensions shouldn't be built
    for i, ext in reversed(list(enumerate(ext_modules))):
        if ext.name == 'skip_cython':
            del ext_modules[i]

    # On Microsoft compilers, we need to pass the '/MANIFEST'
    # commandline argument.  This was the default on MSVC 9.0, but is
    # now required on MSVC 10.0, but it doesn't seem to hurt to add
    # it unconditionally.
    if get_compiler_option() == 'msvc':
        for ext in ext_modules:
            ext.extra_link_args.append('/MANIFEST')

    return {
        'ext_modules': ext_modules,
        'packages': packages,
        'package_dir': package_dir,
        'package_data': package_data,
        }

Response: Returns a dictionary of keyword arguments that can be passed directly to distutils. setup. py.


Instruction:
Explain what the following Python 3 code does
def iter_setup_packages(srcdir, packages):

    for packagename in packages:
        package_parts = packagename.split('.')
        package_path = os.path.join(srcdir, *package_parts)
        setup_package = os.path.relpath(
            os.path.join(package_path, 'setup_package.py'))

        if os.path.isfile(setup_package):
            module = import_file(setup_package,
                                 name=packagename + '.setup_package')
            yield module

Response: A generator that yields all of the setup_package. py modules in the source packages.


Instruction:
Can you tell what is the following Python 3 function doing
def iter_pyx_files(package_dir, package_name):
    for dirpath, dirnames, filenames in walk_skip_hidden(package_dir):
        for fn in filenames:
            if fn.endswith('.pyx'):
                fullfn = os.path.relpath(os.path.join(dirpath, fn))
                # Package must match file name
                extmod = '.'.join([package_name, fn[:-4]])
                yield (extmod, fullfn)

        break

Response: Iterate over the. pyx files in the specified package directory.


Instruction:
Here you have a function in Python 3, explain what it does
def get_cython_extensions(srcdir, packages, prevextensions=tuple(),
                          extincludedirs=None):
    """
    Looks for Cython files and generates Extensions if needed.

    Parameters
    ----------
    srcdir : str
        Path to the root of the source directory to search.
    prevextensions : list of `~distutils.core.Extension` objects
        The extensions that are already defined.  Any .pyx files already here
        will be ignored.
    extincludedirs : list of str or None
        Directories to include as the `include_dirs` argument to the generated
        `~distutils.core.Extension` objects.

    Returns
    -------
    exts : list of `~distutils.core.Extension` objects
        The new extensions that are needed to compile all .pyx files (does not
        include any already in `prevextensions`).
    """

    # Vanilla setuptools and old versions of distribute include Cython files
    # as .c files in the sources, not .pyx, so we cannot simply look for
    # existing .pyx sources in the previous sources, but we should also check
    # for .c files with the same remaining filename. So we look for .pyx and
    # .c files, and we strip the extension.
    prevsourcepaths = []
    ext_modules = []

    for ext in prevextensions:
        for s in ext.sources:
            if s.endswith(('.pyx', '.c', '.cpp')):
                sourcepath = os.path.realpath(os.path.splitext(s)[0])
                prevsourcepaths.append(sourcepath)

    for package_name in packages:
        package_parts = package_name.split('.')
        package_path = os.path.join(srcdir, *package_parts)

        for extmod, pyxfn in iter_pyx_files(package_path, package_name):
            sourcepath = os.path.realpath(os.path.splitext(pyxfn)[0])
            if sourcepath not in prevsourcepaths:
                ext_modules.append(Extension(extmod, [pyxfn],
                                             include_dirs=extincludedirs))

    return ext_modules

Response: Returns a list of extensions that can be used to compile all. pyx files in the source directory.


Instruction:
Write a Python 3 script for
using pkg - config to update distutils. Extension arguments for pkg - config packages.

Response:
def pkg_config(packages, default_libraries, executable='pkg-config'):
    """
    Uses pkg-config to update a set of distutils Extension arguments
    to include the flags necessary to link against the given packages.

    If the pkg-config lookup fails, default_libraries is applied to
    libraries.

    Parameters
    ----------
    packages : list of str
        A list of pkg-config packages to look up.

    default_libraries : list of str
        A list of library names to use if the pkg-config lookup fails.

    Returns
    -------
    config : dict
        A dictionary containing keyword arguments to
        `distutils.Extension`.  These entries include:

        - ``include_dirs``: A list of include directories
        - ``library_dirs``: A list of library directories
        - ``libraries``: A list of libraries
        - ``define_macros``: A list of macro defines
        - ``undef_macros``: A list of macros to undefine
        - ``extra_compile_args``: A list of extra arguments to pass to
          the compiler
    """

    flag_map = {'-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries',
                '-D': 'define_macros', '-U': 'undef_macros'}
    command = "{0} --libs --cflags {1}".format(executable, ' '.join(packages)),

    result = DistutilsExtensionArgs()

    try:
        pipe = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
        output = pipe.communicate()[0].strip()
    except subprocess.CalledProcessError as e:
        lines = [
            ("{0} failed. This may cause the build to fail below."
             .format(executable)),
            "  command: {0}".format(e.cmd),
            "  returncode: {0}".format(e.returncode),
            "  output: {0}".format(e.output)
            ]
        log.warn('\n'.join(lines))
        result['libraries'].extend(default_libraries)
    else:
        if pipe.returncode != 0:
            lines = [
                "pkg-config could not lookup up package(s) {0}.".format(
                    ", ".join(packages)),
                "This may cause the build to fail below."
                ]
            log.warn('\n'.join(lines))
            result['libraries'].extend(default_libraries)
        else:
            for token in output.split():
                # It's not clear what encoding the output of
                # pkg-config will come to us in.  It will probably be
                # some combination of pure ASCII (for the compiler
                # flags) and the filesystem encoding (for any argument
                # that includes directories or filenames), but this is
                # just conjecture, as the pkg-config documentation
                # doesn't seem to address it.
                arg = token[:2].decode('ascii')
                value = token[2:].decode(sys.getfilesystemencoding())
                if arg in flag_map:
                    if arg == '-D':
                        value = tuple(value.split('=', 1))
                    result[flag_map[arg]].append(value)
                else:
                    result['extra_compile_args'].append(value)

    return result


Instruction:
Create a Python 3 function for
adding a build option for selecting the internal or system copy of a library.

Response:
def add_external_library(library):
    """
    Add a build option for selecting the internal or system copy of a library.

    Parameters
    ----------
    library : str
        The name of the library.  If the library is `foo`, the build
        option will be called `--use-system-foo`.
    """

    for command in ['build', 'build_ext', 'install']:
        add_command_option(command, str('use-system-' + library),
                           'Use the system {0} library'.format(library),
                           is_bool=True)


Instruction:
Write a Python 3 script for
returning the previously used Cython version or unknown if Cython is installed.

Response:
def should_build_with_cython(previous_cython_version, is_release):
    """
    Returns the previously used Cython version (or 'unknown' if not
    previously built) if Cython should be used to build extension modules from
    pyx files.
    """

    # Only build with Cython if, of course, Cython is installed, we're in a
    # development version (i.e. not release) or the Cython-generated source
    # files haven't been created yet (cython_version == 'unknown'). The latter
    # case can happen even when release is True if checking out a release tag
    # from the repository
    have_cython = False
    try:
        from Cython import __version__ as cython_version  # noqa
        have_cython = True
    except ImportError:
        pass

    if have_cython and (not is_release or previous_cython_version == 'unknown'):
        return cython_version
    else:
        return False


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

        # Determine the compiler we'll be using
        if self.compiler is None:
            compiler = get_default_compiler()
        else:
            compiler = self.compiler

        # Replace .pyx with C-equivalents, unless c files are missing
        for jdx, src in enumerate(extension.sources):
            base, ext = os.path.splitext(src)
            pyxfn = base + '.pyx'
            cfn = base + '.c'
            cppfn = base + '.cpp'

            if not os.path.isfile(pyxfn):
                continue

            if self._uses_cython:
                extension.sources[jdx] = pyxfn
            else:
                if os.path.isfile(cfn):
                    extension.sources[jdx] = cfn
                elif os.path.isfile(cppfn):
                    extension.sources[jdx] = cppfn
                else:
                    msg = (
                        'Could not find C/C++ file {0}.(c/cpp) for Cython '
                        'file {1} when building extension {2}. Cython '
                        'must be installed to build from a git '
                        'checkout.'.format(base, pyxfn, extension.name))
                    raise IOError(errno.ENOENT, msg, cfn)

            # Cython (at least as of 0.29.2) uses deprecated Numpy API features
            # the use of which produces a few warnings when compiling.
            # These additional flags should squelch those warnings.
            # TODO: Feel free to remove this if/when a Cython update
            # removes use of the deprecated Numpy API
            if compiler == 'unix':
                extension.extra_compile_args.extend([
                    '-Wp,-w', '-Wno-unused-function'])

Response: Check that the C ++ files associated with. pyx and. c files are present and that they are C - equivalent.


Instruction:
Can you tell what is the following Python 3 function doing
def _get_flag_value_from_var(flag, var, delim=' '):

    if sys.platform.startswith('win'):
        return None

    # Simple input validation
    if not var or not flag:
        return None
    flag_length = len(flag)
    if not flag_length:
        return None

    # Look for var in os.eviron then in get_config_var
    if var in os.environ:
        flags = os.environ[var]
    else:
        try:
            flags = get_config_var(var)
        except KeyError:
            return None

    # Extract flag from {var:value}
    if flags:
        for item in flags.split(delim):
            if item.startswith(flag):
                return item[flag_length:]

Response: Returns the value of a flag from an environment variable.


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

    compile_flags = []
    link_flags = []

    if get_compiler_option() == 'msvc':
        compile_flags.append('-openmp')
    else:

        include_path = _get_flag_value_from_var('-I', 'CFLAGS')
        if include_path:
            compile_flags.append('-I' + include_path)

        lib_path = _get_flag_value_from_var('-L', 'LDFLAGS')
        if lib_path:
            link_flags.append('-L' + lib_path)
            link_flags.append('-Wl,-rpath,' + lib_path)

        compile_flags.append('-fopenmp')
        link_flags.append('-fopenmp')

    return {'compiler_flags': compile_flags, 'linker_flags': link_flags}

Response:
Utility for returning compiler and linker flags possibly needed for
    OpenMP support.


Instruction:
Implement a Python 3 function for
checking whether OpenMP test code can be compiled and run.

Response:
def check_openmp_support(openmp_flags=None):
    """
    Check whether OpenMP test code can be compiled and run.

    Parameters
    ----------
    openmp_flags : dict, optional
        This should be a dictionary with keys ``compiler_flags`` and
        ``linker_flags`` giving the compiliation and linking flags respectively.
        These are passed as `extra_postargs` to `compile()` and
        `link_executable()` respectively. If this is not set, the flags will
        be automatically determined using environment variables.

    Returns
    -------
    result : bool
        `True` if the test passed, `False` otherwise.
    """

    ccompiler = new_compiler()
    customize_compiler(ccompiler)

    if not openmp_flags:
        # customize_compiler() extracts info from os.environ. If certain keys
        # exist it uses these plus those from sysconfig.get_config_vars().
        # If the key is missing in os.environ it is not extracted from
        # sysconfig.get_config_var(). E.g. 'LDFLAGS' get left out, preventing
        # clang from finding libomp.dylib because -L<path> is not passed to
        # linker. Call get_openmp_flags() to get flags missed by
        # customize_compiler().
        openmp_flags = get_openmp_flags()

    compile_flags = openmp_flags.get('compiler_flags')
    link_flags = openmp_flags.get('linker_flags')

    # Pass -coverage flag to linker.
    # https://github.com/astropy/astropy-helpers/pull/374
    if '-coverage' in compile_flags and '-coverage' not in link_flags:
        link_flags.append('-coverage')

    tmp_dir = tempfile.mkdtemp()
    start_dir = os.path.abspath('.')

    try:
        os.chdir(tmp_dir)

        # Write test program
        with open('test_openmp.c', 'w') as f:
            f.write(CCODE)

        os.mkdir('objects')

        # Compile, test program
        ccompiler.compile(['test_openmp.c'], output_dir='objects',
                          extra_postargs=compile_flags)

        # Link test program
        objects = glob.glob(os.path.join('objects', '*' + ccompiler.obj_extension))
        ccompiler.link_executable(objects, 'test_openmp',
                                  extra_postargs=link_flags)

        # Run test program
        output = subprocess.check_output('./test_openmp')
        output = output.decode(sys.stdout.encoding or 'utf-8').splitlines()

        if 'nthreads=' in output[0]:
            nthreads = int(output[0].strip().split('=')[1])
            if len(output) == nthreads:
                is_openmp_supported = True
            else:
                log.warn("Unexpected number of lines from output of test OpenMP "
                         "program (output was {0})".format(output))
                is_openmp_supported = False
        else:
            log.warn("Unexpected output from test OpenMP "
                     "program (output was {0})".format(output))
            is_openmp_supported = False
    except (CompileError, LinkError, subprocess.CalledProcessError):
        is_openmp_supported = False

    finally:
        os.chdir(start_dir)

    return is_openmp_supported


Instruction:
How would you explain what the following Python 3 function does
def is_openmp_supported():
    log_threshold = log.set_threshold(log.FATAL)
    ret = check_openmp_support()
    log.set_threshold(log_threshold)
    return ret

Response: Determines whether the build compiler has OpenMP support.


Instruction:
Can you create a Python 3 function that
adds OpenMP compilation flags if supported.

Response:
def add_openmp_flags_if_available(extension):
    """
    Add OpenMP compilation flags, if supported (if not a warning will be
    printed to the console and no flags will be added.)

    Returns `True` if the flags were added, `False` otherwise.
    """

    if _ASTROPY_DISABLE_SETUP_WITH_OPENMP_:
        log.info("OpenMP support has been explicitly disabled.")
        return False

    openmp_flags = get_openmp_flags()
    using_openmp = check_openmp_support(openmp_flags=openmp_flags)

    if using_openmp:
        compile_flags = openmp_flags.get('compiler_flags')
        link_flags = openmp_flags.get('linker_flags')
        log.info("Compiling Cython/C/C++ extension with OpenMP support")
        extension.extra_compile_args.extend(compile_flags)
        extension.extra_link_args.extend(link_flags)
    else:
        log.warn("Cannot compile Cython/C/C++ extension with OpenMP, reverting "
                 "to non-parallel code")

    return using_openmp


Instruction:
Can you generate a brief explanation for the following Python 3 code
def generate_openmp_enabled_py(packagename, srcdir='.', disable_openmp=None):

    if packagename.lower() == 'astropy':
        packagetitle = 'Astropy'
    else:
        packagetitle = packagename

    epoch = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
    timestamp = datetime.datetime.utcfromtimestamp(epoch)

    if disable_openmp is not None:
        import builtins
        builtins._ASTROPY_DISABLE_SETUP_WITH_OPENMP_ = disable_openmp
    if _ASTROPY_DISABLE_SETUP_WITH_OPENMP_:
        log.info("OpenMP support has been explicitly disabled.")
    openmp_support = False if _ASTROPY_DISABLE_SETUP_WITH_OPENMP_ else is_openmp_supported()

    src = _IS_OPENMP_ENABLED_SRC.format(packagetitle=packagetitle,
                                        timestamp=timestamp,
                                        return_bool=openmp_support)

    package_srcdir = os.path.join(srcdir, *packagename.split('.'))
    is_openmp_enabled_py = os.path.join(package_srcdir, 'openmp_enabled.py')
    with open(is_openmp_enabled_py, 'w') as f:
        f.write(src)

Response: Generate a new package. openmp_enabled. is_openmp_enabled. py file.


Instruction:
Create a Python 3 function for
getting the value of a key from a pandas DataFrame.

Response:
def GetValue(self, row, col):
        """
        Find the matching value from pandas DataFrame,
        return it.
        """
        if len(self.dataframe):
            return str(self.dataframe.iloc[row, col])
        return ''


Instruction:
Here you have a function in Python 3, explain what it does
def SetValue(self, row, col, value):
        self.dataframe.iloc[row, col] = value

Response: Set the value in the pandas DataFrame


Instruction:
Can you tell what is the following Python 3 function doing
def SetColumnValues(self, col, value):
        try:
            self.dataframe.iloc[:, col] = value
        except ValueError:
            self.dataframe.loc[:, col] = value

Response: Set all values in a column to a list - like value.


Instruction:
Write a Python 3 script for
getting col label from dataframe

Response:
def GetColLabelValue(self, col):
        """
        Get col label from dataframe
        """
        if len(self.dataframe):
            return self.dataframe.columns[col]
        return ''


Instruction:
Create a Python 3 function for
setting col label value in dataframe

Response:
def SetColLabelValue(self, col, value):
        """
        Set col label value in dataframe
        """
        if len(self.dataframe):
            col_name = str(self.dataframe.columns[col])
            self.dataframe.rename(columns={col_name: str(value)}, inplace=True)
        return None


Instruction:
Write a Python 3 function for
setting to always have vertical scrollbar. Have horizontal scrollbar unless grid has very few rows. Older versions of wxPython will choke on this, in which case nothing happens.

Response:
def set_scrollbars(self):
        """
        Set to always have vertical scrollbar.
        Have horizontal scrollbar unless grid has very few rows.
        Older versions of wxPython will choke on this,
        in which case nothing happens.
        """
        try:
            if len(self.row_labels) < 5:
                show_horizontal = wx.SHOW_SB_NEVER
            else:
                show_horizontal = wx.SHOW_SB_DEFAULT
            self.ShowScrollbars(show_horizontal, wx.SHOW_SB_DEFAULT)
        except AttributeError:
            pass


Instruction:
Write a Python 3 function for
adding items and update existing items in grid

Response:
def add_items(self, dataframe, hide_cols=()):
        """
        Add items and/or update existing items in grid
        """
        # replace "None" values with ""
        dataframe = dataframe.fillna("")
        # remove any columns that shouldn't be shown
        for col in hide_cols:
            if col in dataframe.columns:
                del dataframe[col]
        # add more rows
        self.AppendRows(len(dataframe))
        columns = dataframe.columns
        row_num = -1
        # fill in all rows with appropriate values
        for ind, row in dataframe.iterrows():
            row_num += 1
            for col_num, col in enumerate(columns):
                value = row[col]
                self.SetCellValue(row_num, col_num, str(value))
                # set citation default value
                if col == 'citations':
                    citation = row['citations']
                    if (citation is None) or (citation is np.nan):
                            self.SetCellValue(row_num, col_num, 'This study')
                    else:
                        if 'This study' not in citation:
                            if len(citation):
                                citation += ':'
                            citation += 'This study'
                            self.SetCellValue(row_num, col_num, citation)
        self.row_labels.extend(dataframe.index)


Instruction:
Given the following Python 3 function, write the documentation
def save_items(self, rows=None, verbose=False):
        if rows:
            rows = rows
        else:
            rows = list(range(self.GetNumberRows()))
        cols = list(range(self.GetNumberCols()))
        data = {}
        for row in rows:
            data[row] = {}
            for col in cols:
                col_name = self.GetColLabelValue(col)
                if verbose:
                    print(col_name, ":", self.GetCellValue(row, col))
                data[row][col_name] = self.GetCellValue(row, col)
        return data

Response: Save the items of the current table in a dictionary.


Instruction:
Write a Python 3 function for
setting self. changes to true when user edits the grid

Response:
def on_edit_grid(self, event):
        """sets self.changes to true when user edits the grid.
        provides down and up key functionality for exiting the editor"""
        if not self.changes:
            self.changes = {event.Row}
        else:
            self.changes.add(event.Row)
        #self.changes = True
        try:
            editor = event.GetControl()
            editor.Bind(wx.EVT_KEY_DOWN, self.onEditorKey)
        except AttributeError:
            # if it's a EVT_GRID_EDITOR_SHOWN, it doesn't have the GetControl method
            pass


Instruction:
Can you tell what is the following Python 3 function doing
def do_paste(self, event):
        # find where the user has clicked
        col_ind = self.GetGridCursorCol()
        row_ind = self.GetGridCursorRow()
        # read in clipboard text
        text_df = pd.read_clipboard(header=None, sep='\t').fillna('')
        # add extra rows if need to accomadate clipboard text
        row_length_diff = len(text_df) - (len(self.row_labels) - row_ind)
        if row_length_diff > 0:
            for n in range(row_length_diff):
                self.add_row()
        # ignore excess columns if present
        col_length_diff = len(text_df.columns) - (len(self.col_labels) - col_ind)
        if col_length_diff > 0:
            text_df = text_df.iloc[:, :-col_length_diff].copy()
        # go through copied text and parse it into the grid rows
        for label, row_data in text_df.iterrows():
            col_range = list(range(col_ind, col_ind + len(row_data)))
            if len(row_data) > 1:
                cols = list(zip(col_range, row_data.index))
                for column in cols:
                    value = row_data[column[1]]
                    this_col = column[0]
                    self.SetCellValue(row_ind, this_col, str(value))
            else:
                value = row_data[0]
                self.SetCellValue(row_ind, col_ind, str(value))
            row_ind += 1
        # could instead use wxPython clipboard here
        # see old git history for that
        self.size_grid()
        event.Skip()

Response: Read clipboard into dataframe and parse it into grid rows and columns


Instruction:
Make a summary of the following Python 3 code
def update_changes_after_row_delete(self, row_num):
        if row_num in self.changes.copy():
            self.changes.remove(row_num)
        updated_rows = []
        for changed_row in self.changes:
            if changed_row == -1:
                updated_rows.append(-1)
            if changed_row > row_num:
                updated_rows.append(changed_row - 1)
            if changed_row < row_num:
                updated_rows.append(changed_row)
        self.changes = set(updated_rows)

Response: Update self. changes so that the row numbers for deleted rows are still correct.


Instruction:
How would you explain what the following Python 3 function does
def paint_invalid_cell(self, row, col, color='MEDIUM VIOLET RED',
                           skip_cell=False):
        """
        Take row, column, and turn it color
        """
        self.SetColLabelRenderer(col, MyColLabelRenderer('#1101e0'))
        # SetCellRenderer doesn't work with table-based grid (HugeGrid class)
        if not skip_cell:
            self.SetCellRenderer(row, col, MyCustomRenderer(color))

Response: This method is used to paint an invalid cell in the table.


Instruction:
Can you create a Python 3 function that
adds a column to the table dataframe and returns the index of the new column.

Response:
def add_col(self, label):
        """
        Update table dataframe, and append a new column

        Parameters
        ----------
        label : str

        Returns
        ---------
        last_col: int
            index column number of added col
        """
        self.table.dataframe[label] = ''
        self.AppendCols(1, updateLabels=False)
        last_col = self.table.GetNumberCols() - 1
        self.SetColLabelValue(last_col, label)
        self.col_labels.append(label)
        self.size_grid()
        return last_col


Instruction:
Can you write a function in Python 3 where it
removes a column from the table dataframe and resize grid to display correctly

Response:
def remove_col(self, col_num):
        """
        update table dataframe, and remove a column.
        resize grid to display correctly
        """
        label_value = self.GetColLabelValue(col_num).strip('**').strip('^^')
        self.col_labels.remove(label_value)
        del self.table.dataframe[label_value]
        result = self.DeleteCols(pos=col_num, numCols=1, updateLabels=True)
        self.size_grid()
        return result


Instruction:
Can you create a Python 3 function that
names is the main function for the high level function plotdi_a. py

Response:
def main():
    """
    NAME
       plotdi_a.py

    DESCRIPTION
       plots equal area projection  from dec inc data and fisher mean, cone of confidence

    INPUT FORMAT
       takes dec, inc, alpha95 as first three columns in space delimited file

    SYNTAX
       plotdi_a.py [-i][-f FILE]

    OPTIONS
        -f FILE to read file name from command line
        -fmt [png,jpg,eps,pdf,svg] set plot file format ['svg' is default]
        -sav save plot and quit

    """
    fmt,plot='svg',0
    if len(sys.argv) > 0:
        if '-h' in sys.argv: # check if help is needed
            print(main.__doc__)
            sys.exit() # graceful quit
        if '-fmt' in sys.argv:
            ind=sys.argv.index('-fmt')
            fmt=sys.argv[ind+1]
        if '-sav' in sys.argv:plot=1
        if '-f' in sys.argv:
            ind=sys.argv.index('-f')
            file=sys.argv[ind+1]
            f=open(file,'r')
            data=f.readlines()
        else:
            data=sys.stdin.readlines() # read in data from standard input
    DIs,Pars=[],[]
    for line in data:   # read in the data from standard input
        pars=[]
        rec=line.split() # split each line on space to get records
        DIs.append([float(rec[0]),float(rec[1])])
        pars.append(float(rec[0]))
        pars.append(float(rec[1]))
        pars.append(float(rec[2]))
        pars.append(float(rec[0]))
        isign=abs(float(rec[1])) / float(rec[1])
        pars.append(float(rec[1])-isign*90.) #Beta inc
        pars.append(float(rec[2])) # gamma
        pars.append(float(rec[0])+90.) # Beta dec
        pars.append(0.) #Beta inc
        Pars.append(pars)
#
    EQ={'eq':1} # make plot dictionary
    pmagplotlib.plot_init(EQ['eq'],5,5)
    title='Equal area projection'
    pmagplotlib.plot_eq(EQ['eq'],DIs,title)# plot directions
    for k in range(len(Pars)):
        pmagplotlib.plot_ell(EQ['eq'],Pars[k],'b',0,1) # plot ellipses
    files={}
    for key in list(EQ.keys()):
        files[key]=key+'.'+fmt
    titles={}
    titles['eq']='Equal Area Plot'
    if pmagplotlib.isServer:
        black     = '#000000'
        purple    = '#800080'
        EQ = pmagplotlib.add_borders(EQ,titles,black,purple)
        pmagplotlib.save_plots(EQ,files)
    elif plot==0:
        pmagplotlib.draw_figs(EQ)
        ans=input(" S[a]ve to save plot, [q]uit, Return to continue:  ")
        if ans=="q": sys.exit()
        if ans=="a":
            pmagplotlib.save_plots(EQ,files)
    else:
        pmagplotlib.save_plots(EQ,files)


Instruction:
How would you explain what the following Python 3 function does
def main():
    D,I=0.,90.
    outfile=""
    infile=""
    if '-h' in sys.argv:
        print(main.__doc__)
        sys.exit()
    if '-f' in sys.argv:
        ind=sys.argv.index('-f')
        infile=sys.argv[ind+1]
        data=numpy.loadtxt(infile)
    else:
        data=numpy.loadtxt(sys.stdin,dtype=numpy.float)
    if '-F' in sys.argv:
        ind=sys.argv.index('-F')
        outfile=sys.argv[ind+1]
        out=open(outfile,'w')
    if '-D' in sys.argv:
        ind=sys.argv.index('-D')
        D=float(sys.argv[ind+1])
    if '-I' in sys.argv:
        ind=sys.argv.index('-I')
        I=float(sys.argv[ind+1])
    if len(data.shape)>1: # 2-D array
        N=data.shape[0]
        DipDir,Dip=numpy.ones(N,dtype=numpy.float).transpose()*(D-180.),numpy.ones(N,dtype=numpy.float).transpose()*(90.-I)
        data=data.transpose()
        data=numpy.array([data[0],data[1],DipDir ,Dip]).transpose()
        drot,irot=pmag.dotilt_V(data)
        drot=(drot-180.)%360.  #
        for k in range(N):
             if outfile=="":
                print('%7.1f %7.1f ' % (drot[k],irot[k]))
             else:
                out.write('%7.1f %7.1f\n' % (drot[k],irot[k]))
    else:
        d,i=pmag.dotilt(data[0],data[1],(D-180.),90.-I)
        if outfile=="":
            print('%7.1f %7.1f ' % ((d-180.)%360.,i))
        else:
            out.write('%7.1f %7.1f\n' % ((d-180.)%360.,i))

Response:
NAME
        di_rot.py

    DESCRIPTION
        rotates set of directions to new coordinate system

    SYNTAX
        di_rot.py [command line options]

    OPTIONS
        -h prints help message and quits
        -f specify input file, default is standard input
        -F specify output file, default is standard output
        -D D specify  Dec of new coordinate system, default is 0
        -I I specify  Inc of new coordinate system, default is 90
    INTPUT/OUTPUT
        dec  inc   [space delimited]