Instruction:
Write a Python 3 function that can
return a sorted list of filenames to process from a filename glob string or a list of filenames or globs.

Response:
def get_sorted_source_files(
            self,
            source_filenames_or_globs: Union[str, List[str]],
            recursive: bool = True) -> List[str]:
        """
        Returns a sorted list of filenames to process, from a filename,
        a glob string, or a list of filenames/globs.

        Args:
            source_filenames_or_globs: filename/glob, or list of them
            recursive: use :func:`glob.glob` in recursive mode?

        Returns:
            sorted list of files to process
        """
        if isinstance(source_filenames_or_globs, str):
            source_filenames_or_globs = [source_filenames_or_globs]
        final_filenames = []  # type: List[str]
        for sfg in source_filenames_or_globs:
            sfg_expanded = expanduser(sfg)
            log.debug("Looking for: {!r}", sfg_expanded)
            for filename in glob.glob(sfg_expanded, recursive=recursive):
                log.debug("Trying: {!r}", filename)
                if self.should_exclude(filename):
                    log.info("Skipping file {!r}", filename)
                    continue
                final_filenames.append(filename)
        final_filenames.sort()
        return final_filenames


Instruction:
Write a Python 3 function that can
return True if the filename matches the globtext.

Response:
def filename_matches_glob(filename: str, globtext: str) -> bool:
        """
        The ``glob.glob`` function doesn't do exclusion very well. We don't
        want to have to specify root directories for exclusion patterns. We
        don't want to have to trawl a massive set of files to find exclusion
        files. So let's implement a glob match.

        Args:
            filename: filename
            globtext: glob

        Returns:
            does the filename match the glob?

        See also:

        - https://stackoverflow.com/questions/20638040/glob-exclude-pattern

        """
        # Quick check on basename-only matching
        if fnmatch(filename, globtext):
            log.debug("{!r} matches {!r}", filename, globtext)
            return True
        bname = basename(filename)
        if fnmatch(bname, globtext):
            log.debug("{!r} matches {!r}", bname, globtext)
            return True
        # Directory matching: is actually accomplished by the code above!
        # Otherwise:
        return False


Instruction:
Explain what the following Python 3 code does
def should_exclude(self, filename) -> bool:
        for skip_glob in self.skip_globs:
            if self.filename_matches_glob(filename, skip_glob):
                return True
        return False

Response: Returns True if this file should be excluded.


Instruction:
How would you code a function in Python 3 to
return the RST filename corresponding to a source filename.

Response:
def specific_file_rst_filename(self, source_filename: str) -> str:
        """
        Gets the RST filename corresponding to a source filename.
        See the help for the constructor for more details.

        Args:
            source_filename: source filename within current project

        Returns:
            RST filename

        Note in particular: the way we structure the directories means that we
        won't get clashes between files with idential names in two different
        directories. However, we must also incorporate the original source
        filename, in particular for C++ where ``thing.h`` and ``thing.cpp``
        must not generate the same RST filename. So we just add ``.rst``.
        """
        highest_code_to_target = relative_filename_within_dir(
            source_filename, self.highest_code_dir)
        bname = basename(source_filename)
        result = join(self.autodoc_rst_root_dir,
                      dirname(highest_code_to_target),
                      bname + EXT_RST)
        log.debug("Source {!r} -> RST {!r}", source_filename, result)
        return result


Instruction:
Implement a function in Python 3 to
write both the individual RST files and the index.

Response:
def write_index_and_rst_files(self, overwrite: bool = False,
                                  mock: bool = False) -> None:
        """
        Writes both the individual RST files and the index.

        Args:
            overwrite: allow existing files to be overwritten?
            mock: pretend to write, but don't
        """
        for f in self.files_to_index:
            if isinstance(f, FileToAutodocument):
                f.write_rst(
                    prefix=self.rst_prefix,
                    suffix=self.rst_suffix,
                    heading_underline_char=self.source_rst_heading_underline_char,  # noqa
                    overwrite=overwrite,
                    mock=mock,
                )
            elif isinstance(f, AutodocIndex):
                f.write_index_and_rst_files(overwrite=overwrite, mock=mock)
            else:
                fail("Unknown thing in files_to_index: {!r}".format(f))
        self.write_index(overwrite=overwrite, mock=mock)


Instruction:
Here you have a function in Python 3, explain what it does
def index_filename_rel_other_index(self, other: str) -> str:
        return relpath(self.index_filename, start=dirname(other))

Response: Returns the filename of this index relative to the director of another index.


Instruction:
Write a Python 3 function for
returning the contents of the index RST file.

Response:
def index_content(self) -> str:
        """
        Returns the contents of the index RST file.
        """
        # Build the toctree command
        index_filename = self.index_filename
        spacer = "    "
        toctree_lines = [
            "..  toctree::",
            spacer + ":maxdepth: {}".format(self.toctree_maxdepth),
            ""
        ]
        for f in self.files_to_index:
            if isinstance(f, FileToAutodocument):
                rst_filename = spacer + f.rst_filename_rel_autodoc_index(
                    index_filename)
            elif isinstance(f, AutodocIndex):
                rst_filename = (
                    spacer + f.index_filename_rel_other_index(index_filename)
                )
            else:
                fail("Unknown thing in files_to_index: {!r}".format(f))
                rst_filename = ""  # won't get here; for the type checker
            toctree_lines.append(rst_filename)
        toctree = "\n".join(toctree_lines)

        # Create the whole file
        content = """
.. {filename}

{AUTOGENERATED_COMMENT}

{prefix}

{underlined_title}

{introductory_rst}

{toctree}

{suffix}
                """.format(
            filename=self.index_filename_rel_project_root,
            AUTOGENERATED_COMMENT=AUTOGENERATED_COMMENT,
            prefix=self.rst_prefix,
            underlined_title=rst_underline(
                self.title, underline_char=self.index_heading_underline_char),
            introductory_rst=self.introductory_rst,
            toctree=toctree,
            suffix=self.rst_suffix,
        ).strip() + "\n"
        return content


Instruction:
Write a Python 3 function that can
write the index file if permitted.

Response:
def write_index(self, overwrite: bool = False, mock: bool = False) -> None:
        """
        Writes the index file, if permitted.

        Args:
            overwrite: allow existing files to be overwritten?
            mock: pretend to write, but don't
        """
        write_if_allowed(self.index_filename, self.index_content(),
                         overwrite=overwrite, mock=mock)


Instruction:
Create a Python 3 function to
parse the hosts parameter and return the full host and authentication if exists

Response:
def parse_host(hosts):
        """
        Parsing the hosts parameter,
        * currently support only one host
        :param hosts: the hosts json to parse
        :return: the full host and the authentication if exists
        """
        hosts = EsParser._normalize_hosts(hosts)
        host = hosts[0]

        host_name = host[HostParsing.HOST]
        host_port = host[HostParsing.PORT]

        auth = None
        if HostParsing.HTTP_AUTH in host:
            http_auth = host[HostParsing.HTTP_AUTH]
            user_pass = http_auth.split(':')
            auth = (user_pass[0], user_pass[1])

        full_host = "{host}:{port}".format(host=host_name, port=host_port)
        if not host_name.startswith((HostParsing.HTTP + ':', HostParsing.HTTPS + ':')):
            scheme = HostParsing.HTTPS if host.get(HostParsing.USE_SSL) else HostParsing.HTTP
            full_host = "{scheme}://{full_host}".format(full_host=full_host, scheme=scheme)

        return full_host, auth


Instruction:
Can you create a Python 3 function that
updates the host ssl params if needed.

Response:
def _update_ssl_params(host):
        """
        Update the host ssl params (port or scheme) if needed.
        :param host:
        :return:
        """
        if host[HostParsing.HOST] \
                and EsParser._is_secure_connection_type(host):
            host[HostParsing.PORT] = EsParser.SSL_DEFAULT_PORT
            host[HostParsing.USE_SSL] = True
            parsed_url = urlparse(EsParser._fix_host_prefix(host[HostParsing.HOST]))
            host[HostParsing.HOST] = parsed_url.hostname
            host[HostParsing.SCHEME] = HostParsing.HTTPS
        return host


Instruction:
Implement a function in Python 3 to
parse a host string into a dictionary host

Response:
def _parse_string_host(host_str):
        """
        Parse host string into a dictionary host
        :param host_str:
        :return:
        """
        host_str = EsParser._fix_host_prefix(host_str)
        parsed_url = urlparse(host_str)
        host = {HostParsing.HOST: parsed_url.hostname}
        if parsed_url.port:
            host[HostParsing.PORT] = parsed_url.port
        if parsed_url.scheme == HostParsing.HTTPS:
            host[HostParsing.PORT] = parsed_url.port or EsParser.SSL_DEFAULT_PORT
            host[HostParsing.USE_SSL] = True
            host[HostParsing.SCHEME] = HostParsing.HTTPS
        elif parsed_url.scheme:
            host[HostParsing.SCHEME] = parsed_url.scheme
        if parsed_url.username or parsed_url.password:
            host[HostParsing.HTTP_AUTH] = '%s:%s' % (parsed_url.username, parsed_url.password)
        if parsed_url.path and parsed_url.path != '/':
            host[HostParsing.URL_PREFIX] = parsed_url.path
        return host


Instruction:
Can you write a function in Python 3 where it
creates a path from a list of sub paths.

Response:
def make_path(*sub_paths):
        """
        Create a path from a list of sub paths.
        :param sub_paths: a list of sub paths
        :return:
        """
        queued_params = [quote(c.encode('utf-8'), '') for c in sub_paths if c not in NULL_VALUES]
        queued_params.insert(0, '')
        return '/'.join(queued_params)


Instruction:
Explain what the following Python 3 code does
def prepare_url(hostname, path, params=None):
        url = hostname + path

        if params:
            url = url + '?' + urlencode(params)

        if not url.startswith(('http:', 'https:')):
            url = "http://" + url

        return url.encode('utf-8')

Response: Prepare Elasticsearch request url.


Instruction:
Can you tell what is the following Python 3 function doing
def basic_animation(frames=100, interval=30):
    fig = plt.figure()
    ax = plt.axes(xlim=(0, 10), ylim=(-2, 2))
    line, = ax.plot([], [], lw=2)

    x = np.linspace(0, 10, 1000)

    def init():
        line.set_data([], [])
        return line,

    def animate(i):
        y = np.cos(i * 0.02 * np.pi) * np.sin(x - i * 0.02 * np.pi)
        line.set_data(x, y)
        return line,

    return animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=frames, interval=interval)

Response: Plot a basic sine wave with oscillating amplitude


Instruction:
How would you code a function in Python 3 to
plot a 3D visualization of the dynamics of the Lorentz system.

Response:
def lorenz_animation(N_trajectories=20, rseed=1, frames=200, interval=30):
    """Plot a 3D visualization of the dynamics of the Lorenz system"""
    from scipy import integrate
    from mpl_toolkits.mplot3d import Axes3D
    from matplotlib.colors import cnames

    def lorentz_deriv(coords, t0, sigma=10., beta=8./3, rho=28.0):
        """Compute the time-derivative of a Lorentz system."""
        x, y, z = coords
        return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]

    # Choose random starting points, uniformly distributed from -15 to 15
    np.random.seed(rseed)
    x0 = -15 + 30 * np.random.random((N_trajectories, 3))

    # Solve for the trajectories
    t = np.linspace(0, 2, 500)
    x_t = np.asarray([integrate.odeint(lorentz_deriv, x0i, t)
                      for x0i in x0])

    # Set up figure & 3D axis for animation
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1], projection='3d')
    ax.axis('off')

    # choose a different color for each trajectory
    colors = plt.cm.jet(np.linspace(0, 1, N_trajectories))

    # set up lines and points
    lines = sum([ax.plot([], [], [], '-', c=c)
                 for c in colors], [])
    pts = sum([ax.plot([], [], [], 'o', c=c, ms=4)
               for c in colors], [])

    # prepare the axes limits
    ax.set_xlim((-25, 25))
    ax.set_ylim((-35, 35))
    ax.set_zlim((5, 55))

    # set point-of-view: specified by (altitude degrees, azimuth degrees)
    ax.view_init(30, 0)

    # initialization function: plot the background of each frame
    def init():
        for line, pt in zip(lines, pts):
            line.set_data([], [])
            line.set_3d_properties([])

            pt.set_data([], [])
            pt.set_3d_properties([])
        return lines + pts

    # animation function: called sequentially
    def animate(i):
        # we'll step two time-steps per frame.  This leads to nice results.
        i = (2 * i) % x_t.shape[1]

        for line, pt, xi in zip(lines, pts, x_t):
            x, y, z = xi[:i + 1].T
            line.set_data(x, y)
            line.set_3d_properties(z)

            pt.set_data(x[-1:], y[-1:])
            pt.set_3d_properties(z[-1:])

        ax.view_init(30, 0.3 * i)
        fig.canvas.draw()
        return lines + pts

    return animation.FuncAnimation(fig, animate, init_func=init,
                                   frames=frames, interval=interval)


Instruction:
Write a Python 3 function for
returning a string that represents the included frames in the given frame_list.

Response:
def _included_frames(frame_list, frame_format):
    """frame_list should be a list of filenames"""
    return INCLUDED_FRAMES.format(Nframes=len(frame_list),
                                  frame_dir=os.path.dirname(frame_list[0]),
                                  frame_format=frame_format)


Instruction:
Write a Python 3 script to
return the embedded frames in the current image.

Response:
def _embedded_frames(frame_list, frame_format):
    """frame_list should be a list of base64-encoded png files"""
    template = '  frames[{0}] = "data:image/{1};base64,{2}"\n'
    embedded = "\n"
    for i, frame_data in enumerate(frame_list):
        embedded += template.format(i, frame_format,
                                    frame_data.replace('\n', '\\\n'))
    return embedded


Instruction:
Implement a Python 3 function for
removing noise from the mean and standard deviations from the time and magnitude.

Response:
def remove_noise(time, magnitude, error, error_limit=3, std_limit=5):
    """Points within 'std_limit' standard deviations from the mean and with
    errors greater than 'error_limit' times the error mean are
    considered as noise and thus are eliminated.

    """
    data, mjd = magnitude, time

    data_len = len(mjd)
    error_mean = np.mean(error)
    error_tolerance = error_limit * (error_mean or 1)
    data_mean = np.mean(data)
    data_std = np.std(data)

    mjd_out, data_out, error_out = [], [], []
    for i in range(data_len):
        is_not_noise = (
            error[i] < error_tolerance and
            (np.absolute(data[i] - data_mean) / data_std) < std_limit)

        if is_not_noise:
            mjd_out.append(mjd[i])
            data_out.append(data[i])
            error_out.append(error[i])

    data_out = np.asarray(data_out)
    mjd_out = np.asarray(mjd_out)
    error_out = np.asarray(error_out)

    return mjd_out, data_out, error_out


Instruction:
Can you tell what is the following Python 3 function doing
def align(time, time2, magnitude, magnitude2, error, error2):

    error = np.zeros(time.shape) if error is None else error
    error2 = np.zeros(time2.shape) if error2 is None else error2

    # this asume that the first series is the short one
    sserie = pd.DataFrame({"mag": magnitude, "error": error}, index=time)
    lserie = pd.DataFrame({"mag": magnitude2, "error": error2}, index=time2)

    # if the second serie is logest then revert
    if len(time) > len(time2):
        sserie, lserie = lserie, sserie

    # make the merge
    merged = sserie.join(lserie, how="inner", rsuffix='2')

    # recreate columns
    new_time = merged.index.values
    new_mag, new_mag2 = merged.mag.values, merged.mag2.values
    new_error, new_error2 = merged.error.values, merged.error2.values

    if len(time) > len(time2):
        new_mag, new_mag2 = new_mag2, new_mag
        new_error, new_error2 = new_error2, new_error

    return new_time, new_mag, new_mag2, new_error, new_error2

Response: Synchronizes the light - curves in the two different bands.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def load_OGLE3_catalog():
    with bz2.BZ2File(CATALOG_PATH) as bz2fp, warnings.catch_warnings():
        warnings.simplefilter("ignore")
        df = pd.read_table(bz2fp, skiprows=6)
    df.rename(columns={"# ID": "ID"}, inplace=True)
    return df

Response: Return the full list of variables stars of OGLE - 3 as a DataFrame


Instruction:
Given the following Python 3 function, write the documentation
def fetch_OGLE3(ogle3_id, data_home=None,
                metadata=None, download_if_missing=True):
    """Retrieve a lighte curve from OGLE-3 database

    Parameters
    ----------
    ogle3_id : str
        The id of the source (see: ``load_OGLE3_catalog()`` for
        available sources.
    data_home : optional, default: None
        Specify another download and cache folder for the datasets. By default
        all feets data is stored in '~/feets' subfolders.
    metadata : bool | None
        If it's True, the row of the dataframe from ``load_OGLE3_catalog()``
        with the metadata of the source are added to the result.
    download_if_missing : optional, True by default
        If False, raise a IOError if the data is not locally available
        instead of trying to download the data from the source site.

    Returns
    -------

    A Data object.

    Examples
    --------

    .. code-block:: pycon

        >>> ds = fetch_OGLE3("OGLE-BLG-LPV-232377")
        >>> ds
        Data(id='OGLE-BLG-LPV-232377', ds_name='OGLE-III', bands=('I', 'V'))
        >>> ds.bands
        ('I', 'V')
        >>> ds.data.I
        LightCurve(time[100], magnitude[100], error[100])
        >>> ds.data.I.magnitude
        array([ 13.816,  13.826,  13.818,  13.812,  13.8  ,  13.827,  13.797,
                13.82 ,  13.804,  13.783,  13.823,  13.8  ,  13.84 ,  13.817,
                13.802,  13.824,  13.822,  13.81 ,  13.844,  13.848,  13.813,
                13.836,  13.83 ,  13.83 ,  13.837,  13.811,  13.814,  13.82 ,
                13.826,  13.822,  13.821,  13.817,  13.813,  13.809,  13.817,
                13.836,  13.804,  13.801,  13.813,  13.823,  13.818,  13.831,
                13.833,  13.814,  13.814,  13.812,  13.822,  13.814,  13.818,
                13.817,  13.8  ,  13.804,  13.799,  13.809,  13.815,  13.846,
                13.796,  13.791,  13.804,  13.853,  13.839,  13.816,  13.825,
                13.81 ,  13.8  ,  13.807,  13.819,  13.829,  13.844,  13.84 ,
                13.842,  13.818,  13.801,  13.804,  13.814,  13.821,  13.821,
                13.822,  13.82 ,  13.803,  13.813,  13.826,  13.855,  13.865,
                13.854,  13.828,  13.809,  13.828,  13.833,  13.829,  13.816,
                13.82 ,  13.827,  13.834,  13.811,  13.817,  13.808,  13.834,
                13.814,  13.829])

    """

    # retrieve the data dir for ogle
    store_path = _get_OGLE3_data_home(data_home)

    # the data dir for this lightcurve
    file_path = os.path.join(store_path, "{}.tar".format(ogle3_id))

    # members of the two bands of ogle3
    members = {"I": "./{}.I.dat".format(ogle3_id),
               "V": "./{}.V.dat".format(ogle3_id)}

    # the url of the lightcurve
    if download_if_missing:
        url = URL.format(ogle3_id)
        base.fetch(url, file_path)

    bands = []
    data = {}
    with tarfile.TarFile(file_path) as tfp:
        members_names = tfp.getnames()
        for band_name, member_name in members.items():
            if member_name in members_names:
                member = tfp.getmember(member_name)
                src = tfp.extractfile(member)
                lc = _check_dim(np.loadtxt(src))
                data[band_name] = {"time": lc[:, 0],
                                   "magnitude": lc[:, 1],
                                   "error": lc[:, 2]}
                bands.append(band_name)
    if metadata:
        cat = load_OGLE3_catalog()
        metadata = cat[cat.ID == ogle3_id].iloc[0].to_dict()
        del cat

    return Data(
        id=ogle3_id, metadata=metadata, ds_name="OGLE-III",
        description=DESCR, bands=bands, data=data)

Response: Retrieve a lighte curve from OGLE - 3 database.


Instruction:
Given the following Python 3 function, write the documentation
def sort_by_dependencies(exts, retry=None):
    sorted_ext, features_from_sorted = [], set()
    pending = [(e, 0) for e in exts]
    retry = len(exts) * 100 if retry is None else retry
    while pending:
        ext, cnt = pending.pop(0)

        if not isinstance(ext, Extractor) and not issubclass(ext, Extractor):
            msg = "Only Extractor instances are allowed. Found {}."
            raise TypeError(msg.format(type(ext)))

        deps = ext.get_dependencies()
        if deps.difference(features_from_sorted):
            if cnt + 1 > retry:
                msg = "Maximun retry ({}) to sort achieved from extractor {}."
                raise RuntimeError(msg.format(retry, type(ext)))
            pending.append((ext, cnt + 1))
        else:
            sorted_ext.append(ext)
            features_from_sorted.update(ext.get_features())
    return tuple(sorted_ext)

Response: Calculate the Feature Extractor Resolution Order.


Instruction:
Make a summary of the following Python 3 code
def getSignificance(wk1, wk2, nout, ofac):
  expy = exp(-wk2)
  effm = 2.0*(nout)/ofac
  sig = effm*expy
  ind = (sig > 0.01).nonzero()
  sig[ind] = 1.0-(1.0-expy[ind])**effm
  return sig

Response: returns the probability of the false alarm probabilitywk1 wk2 nout of ac


Instruction:
Write a Python 3 function for
retrieving data from an url and store it into dest.

Response:
def fetch(url, dest, force=False):
    """Retrieve data from an url and store it into dest.

    Parameters
    ----------
    url: str
        Link to the remote data
    dest: str
        Path where the file must be stored
    force: bool (default=False)
        Overwrite if the file exists

    Returns
    -------
    cached: bool
        True if the file already exists
    dest: str
        The same string of the parameter

    """

    cached = True
    if force or not os.path.exists(dest):
        cached = False
        r = requests.get(url, stream=True)
        if r.status_code == 200:
            with open(dest, 'wb') as f:
                for chunk in r.iter_content(1024):
                    f.write(chunk)
    return cached, dest


Instruction:
Write a Python 3 script to
generate a random light curve with any given random function.

Response:
def create_random(magf, magf_params, errf, errf_params,
                  timef=np.linspace, timef_params=None, size=DEFAULT_SIZE,
                  id=None, ds_name=DS_NAME, description=DESCRIPTION,
                  bands=BANDS, metadata=METADATA):
    """Generate a data with any given random function.

    Parameters
    ----------

    magf : callable
        Function to generate the magnitudes.
    magf_params : dict-like
        Parameters to feed the `magf` function.
    errf : callable
        Function to generate the magnitudes.
    errf_params : dict-like
        Parameters to feed the `errf` function.
    timef : callable, (default=numpy.linspace)
        Function to generate the times.
    timef_params : dict-like or None, (default={"start": 0., "stop": 1.})
        Parameters to feed the `timef` callable.
    size : int (default=10000)
        Number of obervation of the light curves
    id : object (default=None)
        Id of the created data.
    ds_name : str (default="feets-synthetic")
        Name of the dataset
    description : str (default="Lightcurve created with random numbers")
        Description of the data
    bands : tuple of strings (default=("B", "V"))
        The bands to be created
    metadata : dict-like or None (default=None)
        The metadata of the created data

    Returns
    -------

    data
        A Data object with a random lightcurves.

    Examples
    --------

    .. code-block:: pycon

        >>> from numpy import random
        >>>  create_random(
        ...     magf=random.normal, magf_params={"loc": 0, "scale": 1},
        ...     errf=random.normal, errf_params={"loc": 0, "scale": 0.008})
        Data(id=None, ds_name='feets-synthetic', bands=('B', 'V'))

    """
    timef_params = (
        {"start": 0., "stop": 1.}
        if timef_params is None else
        timef_params.copy())
    timef_params.update(num=size)

    magf_params = magf_params.copy()
    magf_params.update(size=size)

    errf_params = errf_params.copy()
    errf_params.update(size=size)

    data = {}
    for band in bands:
        data[band] = {
            "time": timef(**timef_params),
            "magnitude": magf(**magf_params),
            "error": errf(**errf_params)}
    return Data(
        id=id, ds_name=ds_name, description=description,
        bands=bands, metadata=metadata, data=data)


Instruction:
Create a Python 3 function for
generating a random data with magnitudes that follows a Gaussian distribution. Also their errors are gaussian.

Response:
def create_normal(mu=0., sigma=1., mu_err=0.,
                  sigma_err=1., seed=None, **kwargs):
    """Generate a data with magnitudes that follows a Gaussian
     distribution. Also their errors are gaussian.

    Parameters
    ----------

    mu : float (default=0)
        Mean of the gaussian distribution of magnitudes
    sigma : float (default=1)
        Standar deviation of the gaussian distribution of magnitude errors
    mu_err : float (default=0)
        Mean of the gaussian distribution of magnitudes
    sigma_err : float (default=1)
        Standar deviation of the gaussian distribution of magnitude errorrs
    seed : {None, int, array_like}, optional
        Random seed used to initialize the pseudo-random number generator.
        Can be any integer between 0 and 2**32 - 1 inclusive, an
        array (or other sequence) of such integers, or None (the default).
        If seed is None, then RandomState will try to read data from
        /dev/urandom (or the Windows analogue) if available or seed from
        the clock otherwise.
    kwargs : optional
        extra arguments for create_random.

    Returns
    -------

    data
        A Data object with a random lightcurves.

    Examples
    --------

    .. code-block:: pycon

        >>> ds = create_normal(0, 1, 0, .0008, seed=42)
        >>> ds
        Data(id=None, ds_name='feets-synthetic', bands=('B', 'V'))
        >>> ds.data.B
        LightCurve(time[10000], magnitude[10000], error[10000])
        >>> ds.data.B.time
        array([  0.00000000e+00,   1.00010001e-04,   2.00020002e-04, ...,
                 9.99799980e-01,   9.99899990e-01,   1.00000000e+00])

    """

    random = np.random.RandomState(seed)
    return create_random(
        magf=random.normal, magf_params={"loc": mu, "scale": sigma},
        errf=random.normal, errf_params={"loc": mu_err, "scale": sigma_err},
        **kwargs)


Instruction:
How would you implement a function in Python 3 that
generates a data object with magnitudes that follows a uniform distribution ; the error instead are gaussian.

Response:
def create_uniform(low=0., high=1., mu_err=0., sigma_err=1.,
                   seed=None, **kwargs):
    """Generate a data with magnitudes that follows a uniform
     distribution; the error instead are gaussian.

    Parameters
    ----------

    low : float, optional
        Lower boundary of the output interval.  All values generated will be
        greater than or equal to low.  The default value is 0.
    high : float, optional
        Upper boundary of the output interval.  All values generated will be
        less than high.  The default value is 1.0.
    mu_err : float (default=0)
        Mean of the gaussian distribution of magnitudes
    sigma_err : float (default=1)
        Standar deviation of the gaussian distribution of magnitude errorrs
    seed : {None, int, array_like}, optional
        Random seed used to initialize the pseudo-random number generator.
        Can be any integer between 0 and 2**32 - 1 inclusive, an
        array (or other sequence) of such integers, or None (the default).
        If seed is None, then RandomState will try to read data from
        /dev/urandom (or the Windows analogue) if available or seed from
        the clock otherwise.
    kwargs : optional
        extra arguments for create_random.

    Returns
    -------

    data
        A Data object with a random lightcurves.

    Examples
    --------

    .. code-block:: pycon

        >>> ds = synthetic.create_uniform(1, 2, 0, .0008, 42)
        >>> ds
        Data(id=None, ds_name='feets-synthetic', bands=('B', 'V'))
        >>> ds.data.B.magnitude
        array([ 1.37454012,  1.95071431,  1.73199394, ...,  1.94670792,
                1.39748799,  1.2171404 ])

    """
    random = np.random.RandomState(seed)
    return create_random(
        magf=random.uniform, magf_params={"low": low, "high": high},
        errf=random.normal, errf_params={"loc": mu_err, "scale": sigma_err},
        **kwargs)


Instruction:
How would you explain what the following Python 3 function does
def create_periodic(mu_err=0., sigma_err=1., seed=None, **kwargs):

    random = np.random.RandomState(seed)

    size = kwargs.get("size", DEFAULT_SIZE)

    times, mags, errors = [], [], []
    for b in kwargs.get("bands", BANDS):
        time = 100 * random.rand(size)
        error = random.normal(size=size, loc=mu_err, scale=sigma_err)
        mag = np.sin(2 * np.pi * time) + error * random.randn(size)
        times.append(time)
        errors.append(error)
        mags.append(mag)

    times, mags, errors = iter(times), iter(mags), iter(errors)

    return create_random(
        magf=lambda **k: next(mags), magf_params={},
        errf=lambda **k: next(errors), errf_params={},
        timef=lambda **k: next(times), timef_params={}, **kwargs)

Response: Generate a data object with magnitudes with periodic variability of variability error.


Instruction:
Implement a function in Python 3 to
compute the expected probability density function for a single periodogram.

Response:
def pdf_single(z, N, normalization, dH=1, dK=3):
    """Probability density function for Lomb-Scargle periodogram

    Compute the expected probability density function of the periodogram
    for the null hypothesis - i.e. data consisting of Gaussian noise.

    Parameters
    ----------
    z : array-like
        the periodogram value
    N : int
        the number of data points from which the periodogram was computed
    normalization : string
        The periodogram normalization. Must be one of
        ['standard', 'model', 'log', 'psd']
    dH, dK : integers (optional)
        The number of parameters in the null hypothesis and the model

    Returns
    -------
    pdf : np.ndarray
        The expected probability density function

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.
    All expressions used here are adapted from Table 1 of Baluev 2008 [1]_.

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)
    """
    if dK - dH != 2:
        raise NotImplementedError("Degrees of freedom != 2")
    Nk = N - dK

    if normalization == 'psd':
        return np.exp(-z)
    elif normalization == 'standard':
        return 0.5 * Nk * (1 - z) ** (0.5 * Nk - 1)
    elif normalization == 'model':
        return 0.5 * Nk * (1 + z) ** (-0.5 * Nk - 1)
    elif normalization == 'log':
        return 0.5 * Nk * np.exp(-0.5 * Nk * z)
    else:
        raise ValueError("normalization='{0}' is not recognized"
                         "".format(normalization))


Instruction:
Can you write a function in Python 3 where it
computes the expected cumulative distribution for the Lomb - Scargle periodogram.

Response:
def cdf_single(z, N, normalization, dH=1, dK=3):
    """Cumulative distribution for the Lomb-Scargle periodogram

    Compute the expected cumulative distribution of the periodogram
    for the null hypothesis - i.e. data consisting of Gaussian noise.

    Parameters
    ----------
    z : array-like
        the periodogram value
    N : int
        the number of data points from which the periodogram was computed
    normalization : string
        The periodogram normalization. Must be one of
        ['standard', 'model', 'log', 'psd']
    dH, dK : integers (optional)
        The number of parameters in the null hypothesis and the model

    Returns
    -------
    cdf : np.ndarray
        The expected cumulative distribution function

    Notes
    -----
    For normalization='psd', the distribution can only be computed for
    periodograms constructed with errors specified.
    All expressions used here are adapted from Table 1 of Baluev 2008 [1]_.

    References
    ----------
    .. [1] Baluev, R.V. MNRAS 385, 1279 (2008)

    """
    return 1 - fap_single(z, N, normalization=normalization, dH=dH, dK=dK)


Instruction:
Given the following Python 3 function, write the documentation
def fap_simple(Z, fmax, t, y, dy, normalization='standard'):
    N = len(t)
    T = max(t) - min(t)
    N_eff = fmax * T
    p_s = cdf_single(Z, N, normalization=normalization)
    return 1 - p_s ** N_eff

Response: False Alarm Probability based on estimated number of indep frequencies


Instruction:
Create a Python 3 function for
aliasing - free approximation to false alarm probability

Response:
def fap_baluev(Z, fmax, t, y, dy, normalization='standard'):
    """Alias-free approximation to false alarm probability

    (Eqn 6 of Baluev 2008)
    """
    cdf = cdf_single(Z, len(t), normalization)
    tau = tau_davies(Z, fmax, t, y, dy, normalization=normalization)
    return 1 - cdf * np.exp(-tau)


Instruction:
Here you have a function in Python 3, explain what it does
def false_alarm_probability(Z, fmax, t, y, dy, normalization,
                            method='baluev', method_kwds=None):
    """Approximate the False Alarm Probability

    Parameters
    ----------
    TODO

    Returns
    -------
    TODO
    """
    if method not in METHODS:
        raise ValueError("Unrecognized method: {0}".format(method))
    method = METHODS[method]
    method_kwds = method_kwds or {}

    return method(Z, fmax, t, y, dy, normalization, **method_kwds)

Response: Approximate the False Alarm Probability


Instruction:
Can you implement a function in Python 3 that
loads the MACHO lightcurve of 2 bands from the MACHO survey.

Response:
def load_MACHO(macho_id):
    """lightcurve of 2 bands (R, B) from the MACHO survey.

    Notes
    -----

    The files are gathered from the original FATS project tutorial:
    https://github.com/isadoranun/tsfeat

    """
    tarfname = "{}.tar.bz2".format(macho_id)
    tarpath = os.path.join(DATA_PATH, tarfname)

    rpath = "{}.R.mjd".format(macho_id)
    bpath = "{}.B.mjd".format(macho_id)
    with tarfile.open(tarpath, mode="r:bz2") as tf:
        rlc = np.loadtxt(tf.extractfile(rpath))
        blc = np.loadtxt(tf.extractfile(bpath))

    bands = ("R", "B")
    data = {
        "R": {
            "time": rlc[:, 0],
            "magnitude": rlc[:, 1],
            "error": rlc[:, 2]},
        "B": {
            "time": blc[:, 0],
            "magnitude": blc[:, 1],
            "error": blc[:, 2]}
    }
    descr = ("The files are gathered from the original FATS project "
             "tutorial: https://github.com/isadoranun/tsfeat")

    return Data(
        id=macho_id, metadata=None, ds_name="MACHO",
        description=descr, bands=bands, data=data)


Instruction:
How would you explain what the following Python 3 function does
def anim_to_html(anim, fps=None, embed_frames=True, default_mode='loop'):
    if fps is None and hasattr(anim, '_interval'):
        # Convert interval in ms to frames per second
        fps = 1000. / anim._interval

    plt.close(anim._fig)
    if hasattr(anim, "_html_representation"):
        return anim._html_representation
    else:
        # tempfile can't be used here: we need a filename, and this
        # fails on windows.  Instead, we use a custom filename generator
        #with tempfile.NamedTemporaryFile(suffix='.html') as f:
        with _NameOnlyTemporaryFile(suffix='.html') as f:
            anim.save(f.name,  writer=HTMLWriter(fps=fps,
                                                 embed_frames=embed_frames,
                                                 default_mode=default_mode))
            html = open(f.name).read()

        anim._html_representation = html
        return html

Response: Generate HTML representation of the animation


Instruction:
Make a summary of the following Python 3 code
def display_animation(anim, **kwargs):
    from IPython.display import HTML
    return HTML(anim_to_html(anim, **kwargs))

Response: Display the animation with an IPython HTML object


Instruction:
Write a Python 3 script for
indenting the string s with the character c n times.

Response:
def indent(s, c=" ", n=4):
    """Indent the string 's' with the character 'c', 'n' times.

    Parameters
    ----------

    s : str
        String to indent
    c : str, default space
        String to use as indentation
    n : int, default 4
        Number of chars to indent

    """
    indentation = c * n
    return "\n".join([indentation + l for l in s.splitlines()])


Instruction:
Explain what the following Python 3 code does
def generate_date_tail_boost_queries(
        field, timedeltas_and_boosts, relative_to=None):
    """
    Generate a list of RangeQueries usable to boost the scores of more
    recent documents.

    Example:

    ```
    queries = generate_date_tail_boost_queries("publish_date", {
            timedelta(days=90): 1,
            timedelta(days=30): 2,
            timedelta(days=10): 4,
    })
    s = Search(BoolQuery(must=..., should=queries))
    # ...
    ```

    Refs:
    http://elasticsearch-users.115913.n3.nabble.com/Boost-recent-documents-td2126107.html#a2126317

    :param field: field name to generate the queries against
    :param timedeltas_and_boosts:
      dictionary of timedelta instances and their boosts. Negative or
      zero boost values will not generate rangequeries.
    :type timedeltas_and_boosts: dict[timedelta, float]
    :param relative_to: Relative to this datetime (may be None for "now")
    :return: List of RangeQueries
    """
    relative_to = relative_to or datetime.datetime.now()
    times = {}
    for timedelta, boost in timedeltas_and_boosts.items():
        date = (relative_to - timedelta).date()
        times[date] = boost
    times = sorted(times.items(), key=lambda i: i[0])
    queries = []

    for (x, time) in enumerate(times):
        kwargs = {"field": field, "boost": time[1]}
        if x == 0:
            kwargs["lte"] = time[0]
        else:
            kwargs["gt"] = time[0]
            if x < len(times) - 1:
                kwargs["lte"] = times[x + 1][0]

        if kwargs["boost"] > 0:
            q = RangeQuery()
            q.add_range(**kwargs)
            queries.append(q)

    return queries

Response: Generates a list of RangeQueries usable to boost the scores of recent documents.


Instruction:
How would you explain what the following Python 3 function does
def _clean_dict(in_dict):
    out = {}
    for key, value in iteritems(in_dict):
        if isinstance(value, dict):
            value = _clean_dict(value)
        if value is None:
            continue
        out[key] = value
    return out

Response: Recursively remove None - valued items from dict.


Instruction:
Can you create a Python 3 function that
yields batches of count items from the given iterable.

Response:
def batch_iterable(iterable, count):
    """
    Yield batches of `count` items from the given iterable.

    >>> for x in batch([1, 2, 3, 4, 5, 6, 7], 3):
    >>>   print(x)
    [1, 2, 3]
    [4, 5, 6]
    [7]

    :param iterable: An iterable
    :type iterable: Iterable
    :param count: Number of items per batch. If <= 0, nothing is yielded.
    :type count: int
    :return: Iterable of lists of items
    :rtype: Iterable[list[object]]
    """
    if count <= 0:
        return
    current_batch = []
    for item in iterable:
        if len(current_batch) == count:
            yield current_batch
            current_batch = []
        current_batch.append(item)
    if current_batch:
        yield current_batch


Instruction:
Can you write a function in Python 3 where it
returns an iterator of Django model objects in Elasticsearch order optionally using the given Django queryset.

Response:
def get_objects(self, queryset=None):
        """
        Return an iterator of Django model objects in Elasticsearch order,
        optionally using the given Django queryset.  If no queryset is
        given, a default queryset (Model.objects.all) is used.

        :param queryset: Optional queryset to filter in.
        :return:
        """
        if not self:
            return
        if not queryset:
            queryset = self[0].django_model.objects.all()

        pks = [res.pk for res in self if res.django_model == queryset.model]
        object_map = dict((text_type(obj.pk), obj)
                          for obj in queryset.filter(pk__in=pks))
        result_map = dict((res.pk, res)
                          for res in self if res.pk in object_map)

        for pk in pks:
            obj = object_map.get(pk)
            if obj:
                obj._es = result_map.get(pk)
                try:
                    obj._score = obj._es._meta.score
                except AttributeError:
                    obj._score = None
                yield obj


Instruction:
Implement a Python 3 function for
resetting the target collection.

Response:
def reset(self):
        """ Reset target collection (rebuild index).
        """
        self.connection.rebuild_index(
            self.index, coll_name=self.target_coll_name)


Instruction:
Explain what the following Python 3 code does
def update(self, index, fields, tokenizer=default_tokenizer, cutoff=1):
        counts_by_uid = defaultdict(Counter)
        for word, count in self._gather_words(
                index, fields, tokenizer=tokenizer).items():
            uid = hashlib.sha1(unicodedata.normalize(
                "NFKD", word.lower()).encode("UTF-8")).hexdigest()
            counts_by_uid[uid][word] += count

        for uid, word_to_count in counts_by_uid.items():
            word = word_to_count.most_common(1)[0][0]
            count = sum(word_to_count.values())
            if count <= cutoff:
                continue
            self.connection.session.post(
                "/%s/%s/%s/_update" % (self.target_coll_name,
                                       self.target_type, uid),
                data={
                    "script": "ctx._source.count += count",
                    "params": {"count": count},
                    "upsert": {"word": word, "count": count}
                })

Response: Update the wordgatherer collection.


Instruction:
Can you generate the documentation for the following Python 3 function
def search(self, word, limit=30):
        search = Search(PrefixQuery("word", word), sort={"count": "desc"})
        for doc in self.connection.search(
                search, indexes=[self.index], count=limit):
            yield (doc["word"], doc["count"])

Response: Search for a word within the wordgatherer collection.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate_nb(nb):
    if nb['nbformat'] != 4:
        return False

    language_name = (nb.get('metadata', {})
        .get('kernelspec', {})
        .get('language', '').lower())
    return language_name == 'python'

Response: Validate that given notebook JSON is importable and that the notebook language is python


Instruction:
Can you generate the documentation for the following Python 3 function
def filter_ast(module_ast):
    def node_predicate(node):
        """
        Return true if given node is whitelisted
        """
        for an in ALLOWED_NODES:
            if isinstance(node, an):
                return True

        # Recurse through Assign node LHS targets when an id is not specified,
        # otherwise check that the id is uppercase
        if isinstance(node, ast.Assign):
            return all([node_predicate(t) for t in node.targets if not hasattr(t, 'id')]) \
                and all([t.id.isupper() for t in node.targets if hasattr(t, 'id')])

        return False

    module_ast.body = [n for n in module_ast.body if node_predicate(n)]
    return module_ast

Response: Filter a given module ast removing non - whitelisted nodes


Instruction:
Here you have a function in Python 3, explain what it does
def code_from_ipynb(nb, markdown=False):
    code = PREAMBLE
    for cell in nb['cells']:
        if cell['cell_type'] == 'code':
            # transform the input to executable Python
            code += ''.join(cell['source'])
        if cell['cell_type'] == 'markdown':
            code += '\n# ' + '# '.join(cell['source'])
        # We want a blank newline after each cell's output.
        # And the last line of source doesn't have a newline usually.
        code += '\n\n'
    return code

Response: Get the code for a given notebook


Instruction:
Can you tell what is the following Python 3 function doing
def _get_paths(self, fullname):
        real_path = os.path.join(*fullname[len(self.package_prefix):].split('.'))
        for base_path in sys.path:
            if base_path == '':
                # Empty string means process's cwd
                base_path = os.getcwd()
            path = os.path.join(base_path, real_path)
            yield path + '.ipynb'
            yield path + '.py'
            yield os.path.join(path, '__init__.ipynb')
            yield os.path.join(path, '__init__.py')

Response: Generate ordered list of paths we should look for fullname module in


Instruction:
How would you implement a function in Python 3 that
finds a module spec by name and path.

Response:
def find_spec(self, fullname, path, target=None):
        """
        Claims modules that are under ipynb.fs
        """
        if fullname.startswith(self.package_prefix):
            for path in self._get_paths(fullname):
                if os.path.exists(path):
                    return ModuleSpec(
                        name=fullname,
                        loader=self.loader_class(fullname, path),
                        origin=path,
                        is_package=(path.endswith('__init__.ipynb') or path.endswith('__init__.py')),
                    )


Instruction:
Given the following Python 3 function, write the documentation
def coroutine(func):
    @wraps(func)
    def start(*args, **kwargs):
        g = func(*args, **kwargs)
        next(g)
        return g

    return start

Response: Decorator for priming generator - based coroutines.


Instruction:
Here you have a function in Python 3, explain what it does
async def ticker(delay, to):
    for i in range(to):
        yield i
        await asyncio.sleep(delay)

Response: Yields numbers from 0 to every delay seconds.


Instruction:
Can you write a function in Python 3 where it
converts an iterable into a stream of events.

Response:
def iterable_source(iterable, target):
    """Convert an iterable into a stream of events.

    Args:
        iterable: A series of items which will be sent to the target one by one.
        target: The target coroutine or sink.

    Returns:
        An iterator over any remaining items.
    """
    it = iter(iterable)
    for item in it:
        try:
            target.send(item)
        except StopIteration:
            return prepend(item, it)
    return empty_iter()


Instruction:
How would you explain what the following Python 3 function does
def poisson_source(rate, iterable, target):
    if rate <= 0.0:
        raise ValueError("poisson_source rate {} is not positive".format(rate))

    it = iter(iterable)
    for item in it:
        duration = random.expovariate(rate)
        sleep(duration)
        try:
            target.send(item)
        except StopIteration:
            return prepend(item, it)
    return empty_iter()

Response: Send events at random times with uniform probability.


Instruction:
Can you generate the documentation for the following Python 3 function
def compose(f, *fs):
    rfs = list(chain([f], fs))
    rfs.reverse()

    def composed(*args, **kwargs):
        return reduce(
            lambda result, fn: fn(result),
            rfs[1:],
            rfs[0](*args, **kwargs))

    return composed

Response: Compose functions right to left.


Instruction:
Create a Python 3 function for
creating a scanning reducer.

Response:
def scanning(reducer, init=UNSET):
    """Create a scanning reducer."""

    reducer2 = reducer

    def scanning_transducer(reducer):
        return Scanning(reducer, reducer2, init)

    return scanning_transducer


Instruction:
Write a Python 3 script to
create a transducer which takes the first n items

Response:
def taking(n):
    """Create a transducer which takes the first n items"""

    if n < 0:
        raise ValueError("Cannot take fewer than zero ({}) items".format(n))

    def taking_transducer(reducer):
        return Taking(reducer, n)

    return taking_transducer


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

    if n < 0:
        raise ValueError("Cannot drop fewer than zero ({}) items".format(n))

    def dropping_transducer(reducer):
        return Dropping(reducer, n)

    return dropping_transducer

Response: Create a transducer which drops the first n items


Instruction:
Can you generate the documentation for the following Python 3 function
def batching(size):

    if size < 1:
        raise ValueError("batching() size must be at least 1")

    def batching_transducer(reducer):
        return Batching(reducer, size)

    return batching_transducer

Response: Create a transducer which produces non - overlapping batches.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def windowing(size, padding=UNSET, window_type=tuple):

    if size < 1:
        raise ValueError("windowing() size {} is not at least 1".format(size))

    def windowing_transducer(reducer):
        return Windowing(reducer, size, padding, window_type)

    return windowing_transducer

Response: Create a transducer which produces a moving window over items.


Instruction:
Can you tell what is the following Python 3 function doing
def first(predicate=None):

    predicate = true if predicate is None else predicate

    def first_transducer(reducer):
        return First(reducer, predicate)

    return first_transducer

Response: Create a transducer which obtains the first item then terminates.


Instruction:
How would you code a function in Python 3 to
create a transducer which obtains the last item.

Response:
def last(predicate=None):
    """Create a transducer which obtains the last item."""

    predicate = true if predicate is None else predicate

    def last_transducer(reducer):
        return Last(reducer, predicate)

    return last_transducer


Instruction:
Given the following Python 3 function, write the documentation
def element_at(index):

    if index < 0:
        raise IndexError("element_at used with illegal index {}".format(index))

    def element_at_transducer(reducer):
        return ElementAt(reducer, index)

    return element_at_transducer

Response: Create a transducer which obtains the item at the specified index.


Instruction:
Can you tell what is the following Python 3 function doing
def compile_sources(files, CompilerRunner_=None,
                    destdir=None, cwd=None,
                    keep_dir_struct=False,
                    per_file_kwargs=None,
                    **kwargs):
    """
    Compile source code files to object files.

    Parameters
    ----------
    files: iterable of path strings
        source files, if cwd is given, the paths are taken as relative.
    CompilerRunner_: CompilerRunner instance (optional)
        could be e.g. pycompilation.FortranCompilerRunner
        Will be inferred from filename extensions if missing.
    destdir: path string
        output directory, if cwd is given, the path is taken as relative
    cwd: path string
        working directory. Specify to have compiler run in other directory.
        also used as root of relative paths.
    keep_dir_struct: bool
        Reproduce directory structure in `destdir`. default: False
    per_file_kwargs: dict
        dict mapping instances in `files` to keyword arguments
    **kwargs: dict
        default keyword arguments to pass to CompilerRunner_
    """
    _per_file_kwargs = {}

    if per_file_kwargs is not None:
        for k, v in per_file_kwargs.items():
            if isinstance(k, Glob):
                for path in glob.glob(k.pathname):
                    _per_file_kwargs[path] = v
            elif isinstance(k, ArbitraryDepthGlob):
                for path in glob_at_depth(k.filename, cwd):
                    _per_file_kwargs[path] = v
            else:
                _per_file_kwargs[k] = v

    # Set up destination directory
    destdir = destdir or '.'
    if not os.path.isdir(destdir):
        if os.path.exists(destdir):
            raise IOError("{} is not a directory".format(destdir))
        else:
            make_dirs(destdir)
    if cwd is None:
        cwd = '.'
        for f in files:
            copy(f, destdir, only_update=True, dest_is_dir=True)

    # Compile files and return list of paths to the objects
    dstpaths = []
    for f in files:
        if keep_dir_struct:
            name, ext = os.path.splitext(f)
        else:
            name, ext = os.path.splitext(os.path.basename(f))
        file_kwargs = kwargs.copy()
        file_kwargs.update(_per_file_kwargs.get(f, {}))
        dstpaths.append(src2obj(
            f, CompilerRunner_, cwd=cwd,
            **file_kwargs
        ))
    return dstpaths

Response: Compile source code files to object files.


Instruction:
Here you have a function in Python 3, explain what it does
def link(obj_files, out_file=None, shared=False, CompilerRunner_=None,
         cwd=None, cplus=False, fort=False, **kwargs):
    """
    Link object files.

    Parameters
    ----------
    obj_files: iterable of path strings
    out_file: path string (optional)
        path to executable/shared library, if missing
        it will be deduced from the last item in obj_files.
    shared: bool
        Generate a shared library? default: False
    CompilerRunner_: pycompilation.CompilerRunner subclass (optional)
        If not given the `cplus` and `fort` flags will be inspected
        (fallback is the C compiler)
    cwd: path string
        root of relative paths and working directory for compiler
    cplus: bool
        C++ objects? default: False
    fort: bool
        Fortran objects? default: False
    **kwargs: dict
        keyword arguments passed onto CompilerRunner_

    Returns
    -------
    The absolute to the generated shared object / executable

    """
    if out_file is None:
        out_file, ext = os.path.splitext(os.path.basename(obj_files[-1]))
        if shared:
            out_file += sharedext

    if not CompilerRunner_:
        if fort:
            CompilerRunner_, extra_kwargs, vendor = \
                get_mixed_fort_c_linker(
                    vendor=kwargs.get('vendor', None),
                    metadir=kwargs.get('metadir', None),
                    cplus=cplus,
                    cwd=cwd,
                )
            for k, v in extra_kwargs.items():
                expand_collection_in_dict(kwargs, k, v)
        else:
            if cplus:
                CompilerRunner_ = CppCompilerRunner
            else:
                CompilerRunner_ = CCompilerRunner

    flags = kwargs.pop('flags', [])
    if shared:
        if '-shared' not in flags:
            flags.append('-shared')

        # mimic GNU linker behavior on OS X when using -shared
        # (otherwise likely Undefined symbol errors)
        dl_flag = '-undefined dynamic_lookup'
        if sys.platform == 'darwin' and dl_flag not in flags:
            flags.append(dl_flag)
    run_linker = kwargs.pop('run_linker', True)
    if not run_linker:
        raise ValueError("link(..., run_linker=False)!?")

    out_file = get_abspath(out_file, cwd=cwd)
    runner = CompilerRunner_(
        obj_files, out_file, flags,
        cwd=cwd,
        **kwargs)
    runner.run()
    return out_file

Response: Link objects in obj_files into out_file.


Instruction:
Create a Python 3 function for
linking Python extension module to the shared object file.

Response:
def link_py_so(obj_files, so_file=None, cwd=None, libraries=None,
               cplus=False, fort=False, **kwargs):
    """
    Link python extension module (shared object) for importing

    Parameters
    ----------
    obj_files: iterable of path strings
        object files to be linked
    so_file: path string
        Name (path) of shared object file to create. If
        not specified it will have the basname of the last object
        file in `obj_files` but with the extension '.so' (Unix) or
        '.dll' (Windows).
    cwd: path string
        root of relative paths and working directory of linker.
    libraries: iterable of strings
        libraries to link against, e.g. ['m']
    cplus: bool
        Any C++ objects? default: False
    fort: bool
        Any Fortran objects? default: False
    kwargs**: dict
        keyword arguments passed onto `link(...)`

    Returns
    -------
    Absolute path to the generate shared object
    """
    libraries = libraries or []

    include_dirs = kwargs.pop('include_dirs', [])
    library_dirs = kwargs.pop('library_dirs', [])

    # from distutils/command/build_ext.py:
    if sys.platform == "win32":
        warnings.warn("Windows not yet supported.")
    elif sys.platform == 'darwin':
        # Don't use the default code below
        pass
    elif sys.platform[:3] == 'aix':
        # Don't use the default code below
        pass
    else:
        # LIBDIR/INSTSONAME should always points to libpython (dynamic or static)
        pylib = os.path.join(get_config_var('LIBDIR'), get_config_var('INSTSONAME'))
        if os.path.exists(pylib):
            libraries.append(pylib)
        else:
            if get_config_var('Py_ENABLE_SHARED'):
                ABIFLAGS = get_config_var('ABIFLAGS')
                pythonlib = 'python{}.{}{}'.format(
                    sys.hexversion >> 24, (sys.hexversion >> 16) & 0xff,
                    ABIFLAGS or '')
                libraries += [pythonlib]
            else:
                pass

    flags = kwargs.pop('flags', [])
    needed_flags = ('-pthread',)
    for flag in needed_flags:
        if flag not in flags:
            flags.append(flag)

    # We want something like: gcc, ['-pthread', ...
    # compilername, flags = cc.split()[0], cc.split()[1:]

    # # Grab include_dirs
    # include_dirs += list(filter(lambda x: x.startswith('-I'), flags))
    # flags = list(filter(lambda x: not x.startswith('-I'), flags))

    # # Grab library_dirs
    # library_dirs += [x[2:] for x in filter(
    #     lambda x: x.startswith('-L'), flags)]
    # flags = list(filter(lambda x: not x.startswith('-L'), flags))

    # flags.extend(kwargs.pop('flags', []))

    return link(obj_files, shared=True, flags=flags, cwd=cwd,
                cplus=cplus, fort=fort, include_dirs=include_dirs,
                libraries=libraries, library_dirs=library_dirs, **kwargs)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def simple_cythonize(src, destdir=None, cwd=None, logger=None,
                     full_module_name=None, only_update=False,
                     **cy_kwargs):
    """
    Generates a C file from a Cython source file.

    Parameters
    ----------
    src: path string
        path to Cython source
    destdir: path string (optional)
        Path to output directory (default: '.')
    cwd: path string (optional)
        Root of relative paths (default: '.')
    logger: logging.Logger
        info level used.
    full_module_name: string
        passed to cy_compile (default: None)
    only_update: bool
        Only cythonize if source is newer. default: False
    **cy_kwargs:
        second argument passed to cy_compile.
        Generates a .cpp file if cplus=True in cy_kwargs, else a .c file.
    """
    from Cython.Compiler.Main import (
        default_options, CompilationOptions
    )
    from Cython.Compiler.Main import compile as cy_compile

    assert src.lower().endswith('.pyx') or src.lower().endswith('.py')
    cwd = cwd or '.'
    destdir = destdir or '.'

    ext = '.cpp' if cy_kwargs.get('cplus', False) else '.c'
    c_name = os.path.splitext(os.path.basename(src))[0] + ext

    dstfile = os.path.join(destdir, c_name)

    if only_update:
        if not missing_or_other_newer(dstfile, src, cwd=cwd):
            msg = '{0} newer than {1}, did not re-cythonize.'.format(
                dstfile, src)
            if logger:
                logger.info(msg)
            else:
                print(msg)
            return dstfile

    if cwd:
        ori_dir = os.getcwd()
    else:
        ori_dir = '.'
    os.chdir(cwd)
    try:
        cy_options = CompilationOptions(default_options)
        cy_options.__dict__.update(cy_kwargs)
        if logger:
            logger.info("Cythonizing {0} to {1}".format(
                src, dstfile))
        cy_result = cy_compile([src], cy_options, full_module_name=full_module_name)
        if cy_result.num_errors > 0:
            raise ValueError("Cython compilation failed.")
        if os.path.abspath(os.path.dirname(
                src)) != os.path.abspath(destdir):
            if os.path.exists(dstfile):
                os.unlink(dstfile)
            shutil.move(os.path.join(os.path.dirname(src), c_name),
                        destdir)
    finally:
        os.chdir(ori_dir)
    return dstfile

Response: A simple cythonize function that takes a source file and returns a path to the destination file.


Instruction:
Write a Python 3 function that can
return a new object file that can be used to compile a source code file.

Response:
def src2obj(srcpath, CompilerRunner_=None, objpath=None,
            only_update=False, cwd=None, out_ext=None, inc_py=False,
            **kwargs):
    """
    Compiles a source code file to an object file.
    Files ending with '.pyx' assumed to be cython files and
    are dispatched to pyx2obj.

    Parameters
    ----------
    srcpath: path string
        path to source file
    CompilerRunner_: pycompilation.CompilerRunner subclass (optional)
        Default: deduced from extension of srcpath
    objpath: path string (optional)
        path to generated object. defualt: deduced from srcpath
    only_update: bool
        only compile if source is newer than objpath. default: False
    cwd: path string (optional)
        working directory and root of relative paths. default: current dir.
    out_ext: string
        set when objpath is a dir and you want to override defaults
        ('.o'/'.obj' for Unix/Windows).
    inc_py: bool
        add Python include path to include_dirs. default: False
    **kwargs: dict
        keyword arguments passed onto CompilerRunner_ or pyx2obj
    """
    name, ext = os.path.splitext(os.path.basename(srcpath))
    if objpath is None:
        if os.path.isabs(srcpath):
            objpath = '.'
        else:
            objpath = os.path.dirname(srcpath)
            objpath = objpath or '.'  # avoid objpath == ''
    out_ext = out_ext or objext
    if os.path.isdir(objpath):
        objpath = os.path.join(objpath, name+out_ext)

    include_dirs = kwargs.pop('include_dirs', [])
    if inc_py:
        from distutils.sysconfig import get_python_inc
        py_inc_dir = get_python_inc()
        if py_inc_dir not in include_dirs:
            include_dirs.append(py_inc_dir)

    if ext.lower() == '.pyx':
        return pyx2obj(srcpath, objpath=objpath,
                       include_dirs=include_dirs, cwd=cwd,
                       only_update=only_update, **kwargs)

    if CompilerRunner_ is None:
        CompilerRunner_, std = extension_mapping[ext.lower()]
        if 'std' not in kwargs:
            kwargs['std'] = std

    # src2obj implies not running the linker...
    run_linker = kwargs.pop('run_linker', False)
    if run_linker:
        raise CompilationError("src2obj called with run_linker=True")

    if only_update:
        if not missing_or_other_newer(objpath, srcpath, cwd=cwd):
            msg = "Found {0}, did not recompile.".format(objpath)
            if kwargs.get('logger', None):
                kwargs['logger'].info(msg)
            else:
                print(msg)
            return objpath
    runner = CompilerRunner_(
        [srcpath], objpath, include_dirs=include_dirs,
        run_linker=run_linker, cwd=cwd, **kwargs)
    runner.run()
    return objpath


Instruction:
Can you tell what is the following Python 3 function doing
def pyx2obj(pyxpath, objpath=None, interm_c_dir=None, cwd=None,
            logger=None, full_module_name=None, only_update=False,
            metadir=None, include_numpy=False, include_dirs=None,
            cy_kwargs=None, gdb=False, cplus=None, **kwargs):
    """
    Convenience function

    If cwd is specified, pyxpath and dst are taken to be relative
    If only_update is set to `True` the modification time is checked
    and compilation is only run if the source is newer than the
    destination

    Parameters
    ----------
    pyxpath: path string
        path to Cython source file
    objpath: path string (optional)
        path to object file to generate
    interm_c_dir: path string (optional)
        directory to put generated C file.
    cwd: path string (optional)
        working directory and root of relative paths
    logger: logging.Logger (optional)
        passed onto `simple_cythonize` and `src2obj`
    full_module_name: string (optional)
        passed onto `simple_cythonize`
    only_update: bool (optional)
        passed onto `simple_cythonize` and `src2obj`
    metadir: path string (optional)
        passed onto src2obj
    include_numpy: bool (optional)
        Add numpy include directory to include_dirs. default: False
    include_dirs: iterable of path strings (optional)
        Passed onto src2obj and via cy_kwargs['include_path']
        to simple_cythonize.
    cy_kwargs: dict (optional)
        keyword arguments passed onto `simple_cythonize`
    gdb: bool (optional)
        convenience: cy_kwargs['gdb_debug'] is set True if gdb=True,
        default: False
    cplus: bool (optional)
        Indicate whether C++ is used. default: auto-detect using `pyx_is_cplus`
    **kwargs: dict
        keyword arguments passed onto src2obj

    Returns
    -------
    Absolute path of generated object file.

    """
    assert pyxpath.endswith('.pyx')
    cwd = cwd or '.'
    objpath = objpath or '.'
    interm_c_dir = interm_c_dir or os.path.dirname(objpath)

    abs_objpath = get_abspath(objpath, cwd=cwd)

    if os.path.isdir(abs_objpath):
        pyx_fname = os.path.basename(pyxpath)
        name, ext = os.path.splitext(pyx_fname)
        objpath = os.path.join(objpath, name+objext)

    cy_kwargs = cy_kwargs or {}
    cy_kwargs['output_dir'] = cwd
    if cplus is None:
        cplus = pyx_is_cplus(pyxpath)
    cy_kwargs['cplus'] = cplus
    if gdb:
        cy_kwargs['gdb_debug'] = True
    if include_dirs:
        cy_kwargs['include_path'] = include_dirs

    interm_c_file = simple_cythonize(
        pyxpath, destdir=interm_c_dir,
        cwd=cwd, logger=logger,
        full_module_name=full_module_name,
        only_update=only_update, **cy_kwargs)

    include_dirs = include_dirs or []
    if include_numpy:
        import numpy
        numpy_inc_dir = numpy.get_include()
        if numpy_inc_dir not in include_dirs:
            include_dirs.append(numpy_inc_dir)

    flags = kwargs.pop('flags', [])
    needed_flags = ('-fwrapv', '-pthread')
    if not cplus:
        needed_flags += ('-Wstrict-prototypes',)  # not really needed..
    for flag in needed_flags:
        if flag not in flags:
            flags.append(flag)

    options = kwargs.pop('options', [])

    if kwargs.pop('strict_aliasing', False):
        raise CompilationError("Cython req. strict aliasing to be disabled.")

    if 'pic' not in options:
        options.append('pic')
    if 'warn' not in options:
        options.append('warn')

    # Let's be explicit about standard
    if cplus:
        std = kwargs.pop('std', 'c++98')
    else:
        std = kwargs.pop('std', 'c99')

    return src2obj(
        interm_c_file,
        objpath=objpath,
        cwd=cwd,
        only_update=only_update,
        metadir=metadir,
        include_dirs=include_dirs,
        flags=flags,
        std=std,
        options=options,
        logger=logger,
        inc_py=True,
        strict_aliasing=False,
        **kwargs)

Response: This function generates a C object from a Python source file.


Instruction:
Given the following Python 3 function, write the documentation
def compile_link_import_py_ext(
        srcs, extname=None, build_dir=None, compile_kwargs=None,
        link_kwargs=None, **kwargs):
    """
    Compiles sources in `srcs` to a shared object (python extension)
    which is imported. If shared object is newer than the sources, they
    are not recompiled but instead it is imported.

    Parameters
    ----------
    srcs: string
        list of paths to sources
    extname: string
        name of extension (default: None)
        (taken from the last file in `srcs` - without extension)
    build_dir: string
        path to directory in which objects files etc. are generated
    compile_kwargs: dict
        keyword arguments passed to compile_sources
    link_kwargs: dict
        keyword arguments passed to link_py_so
    **kwargs:
        additional keyword arguments overwrites to both compile_kwargs
        and link_kwargs useful for convenience e.g. when passing logger

    Returns
    -------
    the imported module

    Examples
    --------
    >>> mod = compile_link_import_py_ext(['fft.f90', 'convolution.cpp',\
        'fft_wrapper.pyx'], only_update=True)  # doctest: +SKIP
    >>> Aprim = mod.fft(A)  # doctest: +SKIP

    """

    build_dir = build_dir or '.'
    if extname is None:
        extname = os.path.splitext(os.path.basename(srcs[-1]))[0]

    compile_kwargs = compile_kwargs or {}
    compile_kwargs.update(kwargs)

    link_kwargs = link_kwargs or {}
    link_kwargs.update(kwargs)

    try:
        mod = import_module_from_file(os.path.join(build_dir, extname), srcs)
    except ImportError:
        objs = compile_sources(list(map(get_abspath, srcs)), destdir=build_dir,
                               cwd=build_dir, **compile_kwargs)
        so = link_py_so(
            objs, cwd=build_dir, fort=any_fort(srcs), cplus=any_cplus(srcs),
            **link_kwargs)
        mod = import_module_from_file(so)
    return mod

Response: Compiles sources in srcs to a shared object and links them to a shared object.


Instruction:
Make a summary of the following Python 3 code
def compile_link_import_strings(codes, build_dir=None, **kwargs):
    build_dir = build_dir or tempfile.mkdtemp()
    if not os.path.isdir(build_dir):
        raise OSError("Non-existent directory: ", build_dir)

    source_files = []
    if kwargs.get('logger', False) is True:
        import logging
        logging.basicConfig(level=logging.DEBUG)
        kwargs['logger'] = logging.getLogger()

    only_update = kwargs.get('only_update', True)
    for name, code_ in codes:
        dest = os.path.join(build_dir, name)
        differs = True
        md5_in_mem = md5_of_string(code_.encode('utf-8')).hexdigest()
        if only_update and os.path.exists(dest):
            if os.path.exists(dest+'.md5'):
                md5_on_disk = open(dest+'.md5', 'rt').read()
            else:
                md5_on_disk = md5_of_file(dest).hexdigest()
            differs = md5_on_disk != md5_in_mem
        if not only_update or differs:
            with open(dest, 'wt') as fh:
                fh.write(code_)
                open(dest+'.md5', 'wt').write(md5_in_mem)
        source_files.append(dest)

    return compile_link_import_py_ext(
        source_files, build_dir=build_dir, **kwargs)

Response: Compiles a list of source strings into a temporary directory and dumps and links them into a temporary directory.


Instruction:
Create a Python 3 function to
switch Flask - Pymongo db to a different database

Response:
def switch_db(name):
    """ Hack to switch Flask-Pymongo db
    :param name: db name
    """
    with app.app_context():
        app.extensions['pymongo'][mongo.config_prefix] = mongo.cx, mongo.cx[name]


Instruction:
Explain what the following Python 3 code does
def get_row_generator(self, ref, cache=None):

        """Return a row generator for a reference"""
        from inspect import isgenerator
        from rowgenerators import get_generator

        g = get_generator(ref)

        if not g:
            raise GenerateError("Cant figure out how to generate rows from {} ref: {}".format(type(ref), ref))
        else:
            return g

Response: Return a row generator for a reference


Instruction:
How would you implement a function in Python 3 that
generates combinations of key value pairs for each key in properties.

Response:
def create_key_filter(properties: Dict[str, list]) -> List[Tuple]:
    """Generate combinations of key, value pairs for each key in properties.

    Examples
    --------
    properties = {'ent': ['geo_rev', 'supply_chain'], 'own', 'fi'}
    >> create_key_filter(properties)
      --> [('ent', 'geo_rev'), ('ent', 'supply_chain'), ('own', 'fi')]
    """

    combinations = (product([k], v) for k, v in properties.items())

    return chain.from_iterable(combinations)


Instruction:
Can you generate the documentation for the following Python 3 function
def create_indexer(indexes: list):

    if len(indexes) == 1:
        index = indexes[0]
        return lambda x: (x[index],)
    else:
        return itemgetter(*indexes)

Response: Create indexer function to pluck values from list.


Instruction:
Here you have a function in Python 3, explain what it does
def including(self, sequence) -> Generator:
        return (element for element in sequence
                if self.indexer(element) in self.predicates)

Response: Include the elements matching the filter set.


Instruction:
Write a Python 3 script to
exclude the elements matching the filter set.

Response:
def excluding(self, sequence) -> Generator:
        """Exclude the sequence elements matching the filter set."""
        return (element for element in sequence
                if self.indexer(element) not in self.predicates)


Instruction:
Can you tell what is the following Python 3 function doing
async def can(self, identity, permission) -> bool:
        assert isinstance(permission, (str, enum.Enum)), permission
        assert permission
        identify = await self.identity_policy.identify(identity)
        # non-registered user still may has some permissions
        access = await self.autz_policy.can(identify, permission)
        return access

Response:
Check user permissions.

        :return: ``True`` if the identity is allowed the permission, else return ``False``.


Instruction:
How would you explain what the following Python 3 function does
async def check_authorized(self, identity):
        identify = await self.identify(identity)
        if identify is None:
            raise UnauthorizedError()
        return identify

Response: Checks if the identity is authorized.


Instruction:
How would you explain what the following Python 3 function does
async def check_permission(self, identity, permission):
        await self.check_authorized(identity)
        allowed = await self.can(identity, permission)
        if not allowed:
            raise ForbiddenError()

Response: Checks if the identity has the permission.


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

    DESCRIPTION = """\
    """

    parser = argparse.ArgumentParser(description=DESCRIPTION, formatter_class=Raw)

    parser.add_argument("--email", dest="email", action='store', required=False, default=False,
                        help="An email address is required for querying Entrez databases.")

    parser.add_argument("--api", dest="api_key", action='store', required=False, default=False,
                        help="A users ENTREZ API Key. Will speed up download.")

    parser.add_argument("--query", dest="query", action='store', required=True,
                        help="Query to submit to Entrez.")

    parser.add_argument("--host", dest="host", action='store', required=False, default='localhost',
                        help="Location of an already running database.")

    parser.add_argument("--port", dest="port", action='store', type=int, required=False, default=27017,
                        help="Mongo database port.")

    parser.add_argument("--db", dest="db", action='store', required=False, default='sramongo',
                        help="Name of the database.")

    parser.add_argument("--debug", dest="debug", action='store_true', required=False,
                        help="Turn on debug output.")

    parser.add_argument("--force", dest="force", action='store_true', required=False,
                        help="Forces clearing the cache.")

    args = parser.parse_args()

    if not (args.email or args.api_key):
        logger.error('You must provide either an `--email` or `--api`.')
        sys.exit()

    return args

Response: Pulls in command line arguments.


Instruction:
How would you code a function in Python 3 to
execute benchmarks on every node specified in arguments.

Response:
def run(self, *nodes):
        """Execute benchmarks on every node specified in arguments.
        If none are given, then execute benchmarks on every nodes
        specified in the ``network.nodes`` campaign configuration.
        """
        nodes = nodes or self.nodes
        self._prelude(*nodes)

        @write_yaml_report
        def _run():
            self._build_installer()
            runner = functools.partial(run_on_host, self.campaign)
            if self.campaign.network.max_concurrent_runs > 1:
                pool = Pool(self.campaign.network.max_concurrent_runs)
                pool.map(runner, nodes)
            else:
                for node in nodes:
                    runner(node)
            return nodes

        with pushd(self.campaign_path):
            _run()


Instruction:
Write a Python 3 script for
executing benchmark on the specified node

Response:
def run(self):
        """Execute benchmark on the specified node
        """
        with self._scp_bensh_runner():
            self._execute_bensh_runner()
            path = self._retrieve_tarball()
            try:
                self._aggregate_tarball(path)
            finally:
                os.remove(path)


Instruction:
Implement a function in Python 3 to
get the node pairing attribute of the current tag.

Response:
def node_pairing(self):
        """if "node" then test current node and next one
        if "tag", then create tests for every pair of the current tag.
        """
        value = self.attributes['node_pairing']
        if value not in IMB.NODE_PAIRING:
            msg = 'Unexpected {0} value: got "{1}" but valid values are {2}'
            msg = msg.format('node_pairing', value, IMB.NODE_PAIRING)
            raise ValueError(msg)
        return value


Instruction:
Explain what the following Python 3 code does
def write_makeconfig(_path):
    http_proxy = str(CFG["gentoo"]["http_proxy"])
    ftp_proxy = str(CFG["gentoo"]["ftp_proxy"])
    rsync_proxy = str(CFG["gentoo"]["rsync_proxy"])

    path.mkfile_uchroot(local.path('/') / _path)
    with open(_path, 'w') as makeconf:
        lines = '''
PORTAGE_USERNAME=root
PORTAGE_GROUPNAME=root
CFLAGS="-O2 -pipe"
CXXFLAGS="${CFLAGS}"
FEATURES="nostrip -xattr"
CHOST="x86_64-pc-linux-gnu"
USE="bindist mmx sse sse2"
PORTDIR="/usr/portage"
DISTDIR="/mnt/distfiles"
PKGDIR="${PORTDIR}/packages"
'''

        makeconf.write(lines)

        mounts = CFG["container"]["mounts"].value
        tmp_dir = str(CFG["tmp_dir"])
        mounts.append({"src": tmp_dir, "tgt": "/mnt/distfiles"})
        CFG["container"]["mounts"] = mounts

        if http_proxy is not None:
            http_s = "http_proxy={0}".format(http_proxy)
            https_s = "https_proxy={0}".format(http_proxy)
            makeconf.write(http_s + "\n")
            makeconf.write(https_s + "\n")

        if ftp_proxy is not None:
            fp_s = "ftp_proxy={0}".format(ftp_proxy)
            makeconf.write(fp_s + "\n")

        if rsync_proxy is not None:
            rp_s = "RSYNC_PROXY={0}".format(rsync_proxy)
            makeconf.write(rp_s + "\n")

Response: Write a valid gentoo make. conf file to the given path.


Instruction:
Explain what the following Python 3 code does
def write_bashrc(_path):
    cfg_mounts = CFG["container"]["mounts"].value
    cfg_prefix = CFG["container"]["prefixes"].value

    path.mkfile_uchroot("/etc/portage/bashrc")
    mounts = uchroot.mounts("mnt", cfg_mounts)
    p_paths, p_libs = uchroot.env(cfg_prefix)
    paths, libs = uchroot.env(mounts)

    paths = paths + p_paths
    libs = libs + p_libs

    with open(_path, 'w') as bashrc:
        lines = '''
export PATH="{0}:${{PATH}}"
export LD_LIBRARY_PATH="{1}:${{LD_LIBRARY_PATH}}"
'''.format(path.list_to_path(paths), path.list_to_path(libs))

        bashrc.write(lines)

Response: Write a valid gentoo bashrc file to the path.


Instruction:
Write a Python 3 function for
writing a valid gentoo layout file to the path.

Response:
def write_layout(_path):
    """
    Write a valid gentoo layout file to :path:.

    Args:
        path - The output path of the layout.conf
    """

    path.mkdir_uchroot("/etc/portage/metadata")
    path.mkfile_uchroot("/etc/portage/metadata/layout.conf")
    with open(_path, 'w') as layoutconf:
        lines = '''masters = gentoo'''
        layoutconf.write(lines)


Instruction:
Write a Python 3 function for
writing a valid gentoo wgetrc file to the path.

Response:
def write_wgetrc(_path):
    """
    Write a valid gentoo wgetrc file to :path:.

    Args:
        path - The output path of the wgetrc
    """
    http_proxy = str(CFG["gentoo"]["http_proxy"])
    ftp_proxy = str(CFG["gentoo"]["ftp_proxy"])

    path.mkfile_uchroot("/etc/wgetrc")
    with open(_path, 'w') as wgetrc:
        if http_proxy is not None:
            http_s = "http_proxy = {0}".format(http_proxy)
            https_s = "https_proxy = {0}".format(http_proxy)
            wgetrc.write("use_proxy = on\n")
            wgetrc.write(http_s + "\n")
            wgetrc.write(https_s + "\n")

        if ftp_proxy is not None:
            fp_s = "ftp_proxy={0}".format(ftp_proxy)
            wgetrc.write(fp_s + "\n")


Instruction:
Here you have a function in Python 3, explain what it does
def setup_benchbuild():
    LOG.debug("Setting up Benchbuild...")

    venv_dir = local.path("/benchbuild")
    prefixes = CFG["container"]["prefixes"].value
    prefixes.append(venv_dir)
    CFG["container"]["prefixes"] = prefixes

    src_dir = str(CFG["source_dir"])
    have_src = src_dir is not None
    if have_src:
        __mount_source(src_dir)

    benchbuild = find_benchbuild()
    if benchbuild and not requires_update(benchbuild):
        if have_src:
            __upgrade_from_source(venv_dir, with_deps=False)
        return

    setup_virtualenv(venv_dir)
    if have_src:
        __upgrade_from_source(venv_dir)
    else:
        __upgrade_from_pip(venv_dir)

Response: Setup benchbuild inside a container.


Instruction:
How would you explain what the following Python 3 function does
def main(argv=None):
    arguments = cli_common(__doc__, argv=argv)
    es_export = ESExporter(arguments['CAMPAIGN-DIR'], arguments['--es'])
    es_export.export()
    if argv is not None:
        return es_export

Response: ben - elastic entry point


Instruction:
Create a Python 3 function to
set the name of the object.

Response:
def name(self, value):
        """
        Setter for **self.__name** attribute.

        :param value: Attribute value.
        :type value: unicode
        """

        if value is not None:
            assert type(value) is unicode, "'{0}' attribute: '{1}' type is not 'unicode'!".format("name", value)
        self.__name = value


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

        if value is not None:
            assert type(value) is bool, "'{0}' attribute: '{1}' type is not 'bool'!".format("activated", value)
            self.component_activated.emit() if value else self.component_deactivated.emit()
        self.__activated = value

Response: Setter for **self. __activated** attribute.


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

        if value is not None:
            assert type(value) is bool, "'{0}' attribute: '{1}' type is not 'bool'!".format("initialized", value)
            self.component_initialized.emit() if value else self.component_uninitialized.emit()
        self.__initialized = value

Response: Setter for **self. __initialized** attribute.


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

        if value is not None:
            assert type(value) is bool, "'{0}' attribute: '{1}' type is not 'bool'!".format("deactivatable", value)
        self.__deactivatable = value

Response:
Sets the value of the
       .


Instruction:
Can you generate a brief explanation for the following Python 3 code
def r_cts(self):
        _request = request.args.get("request", None)
        if _request is not None:
            try:
                if _request.lower() == "getcapabilities":
                    return self._get_capabilities(
                        urn=request.args.get("urn", None)
                    )
                elif _request.lower() == "getpassage":
                    return self._get_passage(
                        urn=request.args.get("urn", None)
                    )
                elif _request.lower() == "getpassageplus":
                    return self._get_passage_plus(
                        urn=request.args.get("urn", None)
                    )
                elif _request.lower() == "getlabel":
                    return self._get_label(
                        urn=request.args.get("urn", None)
                    )
                elif _request.lower() == "getfirsturn":
                    return self._get_first_urn(
                        urn=request.args.get("urn", None)
                    )
                elif _request.lower() == "getprevnexturn":
                    return self._get_prev_next(
                        urn=request.args.get("urn", None)
                    )
                elif _request.lower() == "getvalidreff":
                    return self._get_valid_reff(
                        urn=request.args.get("urn", None),
                        level=request.args.get("level", 1, type=int)
                    )
            except NautilusError as E:
                return self.cts_error(error_name=E.__class__.__name__, message=E.__doc__)
        return self.cts_error(MissingParameter.__name__, message=MissingParameter.__doc__)

Response: This method handles the main route of the CTS API.


Instruction:
Make a summary of the following Python 3 code
def cts_error(self, error_name, message=None):
        self.nautilus_extension.logger.info(
            "CTS error thrown {} for {} ({})".format(
                error_name,
                request.query_string.decode(),
                message)
        )
        return render_template(
            "cts/Error.xml",
            errorType=error_name,
            message=message
        ), 404, {"content-type": "application/xml"}

Response: Create a CTS Error Response


Instruction:
Given the following Python 3 function, write the documentation
def _get_capabilities(self, urn=None):
        r = self.resolver.getMetadata(objectId=urn)
        if len(r.parents) > 0:
            r = r.parents[-1]
        r = render_template(
            "cts/GetCapabilities.xml",
            filters="urn={}".format(urn),
            inventory=Markup(r.export(Mimetypes.XML.CTS))
        )
        return r, 200, {"content-type": "application/xml"}

Response: Provisional route for GetCapabilities request


Instruction:
Explain what the following Python 3 code does
def _get_first_urn(self, urn):
        urn = URN(urn)
        subreference = None
        textId = urn.upTo(URN.NO_PASSAGE)
        if urn.reference is not None:
            subreference = str(urn.reference)
        firstId = self.resolver.getTextualNode(textId=textId, subreference=subreference).firstId
        r = render_template(
            "cts/GetFirstUrn.xml",
            firstId=firstId,
            full_urn=textId,
            request_urn=str(urn)
        )
        return r, 200, {"content-type": "application/xml"}

Response: Provisional route for GetFirstUrn request


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

		while True:
			events = self._selector.select()
			for nevent in range(len(events) + 1):
				self._libinput.libinput_dispatch(self._li)
				hevent = self._libinput.libinput_get_event(self._li)
				if hevent:
					type_ = self._libinput.libinput_event_get_type(hevent)
					self._libinput.libinput_dispatch(self._li)
					if type_.is_pointer():
						yield PointerEvent(hevent, self._libinput)
					elif type_.is_keyboard():
						yield KeyboardEvent(hevent, self._libinput)
					elif type_.is_touch():
						yield TouchEvent(hevent, self._libinput)
					elif type_.is_gesture():
						yield GestureEvent(hevent, self._libinput)
					elif type_.is_tablet_tool():
						yield TabletToolEvent(hevent, self._libinput)
					elif type_.is_tablet_pad():
						yield TabletPadEvent(hevent, self._libinput)
					elif type_.is_switch():
						yield SwitchEvent(hevent, self._libinput)
					elif type_.is_device():
						yield DeviceNotifyEvent(hevent, self._libinput)

Response: Yields events from the internal libinput s queue.


Instruction:
Can you create a Python 3 function that
returns the type of the next available event in the internal queue.

Response:
def next_event_type(self):
		"""Return the type of the next event in the internal queue.

		This method does not pop the event off the queue and the next call
		to :attr:`events` returns that event.

		Returns:
			~libinput.constant.EventType: The event type of the next available
			event or :obj:`None` if no event is available.
		"""

		type_ = self._libinput.libinput_next_event_type(self._li)
		if type_ == 0:
			return None
		else:
			return EventType(type_)


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

		hdevice = self._libinput.libinput_path_add_device(
			self._li, path.encode())
		if hdevice:
			return Device(hdevice, self._libinput)
		return None

Response: Adds a device to a libinput context.


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

		rc = self._libinput.libinput_udev_assign_seat(self._li, seat.encode())
		assert rc == 0, 'Failed to assign {}'.format(seat)

Response: Assign a seat to this libinput context.


Instruction:
Can you tell what is the following Python 3 function doing
def context(self, outdir, log_prefix):
        try:
            self._outdir = outdir
            self._log_prefix = log_prefix
            yield
        finally:
            self._log_prefix = None
            self._outdir = None

Response: Setup instance to extract metrics from the proper run directory.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_subclass(cls, name):
        for subclass in cls.__subclasses__():
            if subclass.name == name:
                return subclass
        raise NameError("Not a valid Benchmark class: " + name)

Response: Get a Benchmark class by name


Instruction:
How would you implement a function in Python 3 that
attempts to automatically find "adjacent" shapes in a SVG layer. In a layer within a new SVG document, draw each detected connection between the center points of the corresponding shapes. Parameters ---------- svg_source : str Input SVG file as a filepath (or file-like object). shape_i_attr : str, optional Attribute of each shape SVG element that uniquely identifies the shape. layer_name : str, optional Name to use for the output layer where detected connections are drawn. .. note:: Any existing layer with the same name will be overwritten. shapes_xpath : str, optional XPath path expression to select shape nodes. By default, all ``svg:path`` and ``svg:polygon`` elements are selected. extend : float, optional Extend ``x``/``y`` coords by the specified number of absolute units from the center point of each shape. Each shape is stretched independently in the ``x`` and ``y`` direction. In each direction, a shape is considered adjacent to all other shapes that are overlapped by the extended shape. Returns ------- StringIO.StringIO File-like object containing SVG document with layer named according to :data:`layer_name` with the detected connections drawn as ``svg:line`` instances.

Response:
def auto_detect_adjacent_shapes(svg_source, shape_i_attr='id',
                                layer_name='Connections',
                                shapes_xpath='//svg:path | //svg:polygon',
                                extend=1.5):
    '''
    Attempt to automatically find "adjacent" shapes in a SVG layer.

    In a layer within a new SVG document, draw each detected connection between
    the center points of the corresponding shapes.

    Parameters
    ----------
    svg_source : str
        Input SVG file as a filepath (or file-like object).
    shape_i_attr : str, optional
        Attribute of each shape SVG element that uniquely identifies the shape.
    layer_name : str, optional
        Name to use for the output layer where detected connections are drawn.

        .. note:: Any existing layer with the same name will be overwritten.
    shapes_xpath : str, optional
        XPath path expression to select shape nodes.

        By default, all ``svg:path`` and ``svg:polygon`` elements are selected.
    extend : float, optional
        Extend ``x``/``y`` coords by the specified number of absolute units
        from the center point of each shape.

        Each shape is stretched independently in the ``x`` and ``y`` direction.
        In each direction, a shape is considered adjacent to all other shapes
        that are overlapped by the extended shape.

    Returns
    -------
    StringIO.StringIO
        File-like object containing SVG document with layer named according to
        :data:`layer_name` with the detected connections drawn as ``svg:line``
        instances.
    '''
    # Read SVG polygons into dataframe, one row per polygon vertex.
    df_shapes = svg_shapes_to_df(svg_source, xpath=shapes_xpath)
    df_shapes = compute_shape_centers(df_shapes, shape_i_attr)
    df_shape_connections = extract_adjacent_shapes(df_shapes, shape_i_attr,
                                                   extend=extend)

    # Parse input file.
    xml_root = etree.parse(svg_source)
    svg_root = xml_root.xpath('/svg:svg', namespaces=INKSCAPE_NSMAP)[0]

    # Get the center coordinate of each shape.
    df_shape_centers = (df_shapes.drop_duplicates(subset=[shape_i_attr])
                        [[shape_i_attr] + ['x_center', 'y_center']]
                        .set_index(shape_i_attr))

    # Get the center coordinate of the shapes corresponding to the two
    # endpoints of each connection.
    df_connection_centers = (df_shape_centers.loc[df_shape_connections.source]
                             .reset_index(drop=True)
                             .join(df_shape_centers.loc[df_shape_connections
                                                        .target]
                                   .reset_index(drop=True), lsuffix='_source',
                                   rsuffix='_target'))

    # Remove existing connections layer from source, in-memory XML (source file
    # remains unmodified).  A new connections layer will be added below.
    connections_xpath = '//svg:g[@inkscape:label="%s"]' % layer_name
    connections_groups = svg_root.xpath(connections_xpath,
                                        namespaces=INKSCAPE_NSMAP)

    if connections_groups:
        for g in connections_groups:
            g.getparent().remove(g)

    # Create in-memory SVG
    svg_output = \
        draw_lines_svg_layer(df_connection_centers
                             .rename(columns={'x_center_source': 'x_source',
                                              'y_center_source': 'y_source',
                                              'x_center_target': 'x_target',
                                              'y_center_target': 'y_target'}),
                             layer_name=layer_name)

    return svg_output