Instruction:
Make a summary of the following Python 3 code
def get_lats_from_cartesian(x__, y__, z__, thr=0.8):
    # if we are at low latitudes - small z, then get the
    # latitudes only from z. If we are at high latitudes (close to the poles)
    # then derive the latitude using x and y:

    lats = np.where(np.logical_and(np.less(z__, thr * EARTH_RADIUS),
                                   np.greater(z__, -1. * thr * EARTH_RADIUS)),
                    90 - rad2deg(arccos(z__/EARTH_RADIUS)),
                    sign(z__) *
                    (90 - rad2deg(arcsin(sqrt(x__ ** 2 + y__ ** 2)
                                         / EARTH_RADIUS))))
    return lats

Response: Get latitudes from cartesian coordinates.


Instruction:
Write a Python 3 function for
defining the lon lat tie points.

Response:
def set_tiepoints(self, lon, lat):
        """Defines the lon,lat tie points.
        """
        self.lon_tiepoint = lon
        self.lat_tiepoint = lat


Instruction:
Write a Python 3 function for
computing expansion and alignment for a single node.

Response:
def compute_expansion_alignment(satz_a, satz_b, satz_c, satz_d):
    """All angles in radians."""
    zeta_a = satz_a
    zeta_b = satz_b

    phi_a = compute_phi(zeta_a)
    phi_b = compute_phi(zeta_b)
    theta_a = compute_theta(zeta_a, phi_a)
    theta_b = compute_theta(zeta_b, phi_b)
    phi = (phi_a + phi_b) / 2
    zeta = compute_zeta(phi)
    theta = compute_theta(zeta, phi)

    c_expansion = 4 * (((theta_a + theta_b) / 2 - theta) / (theta_a - theta_b))

    sin_beta_2 = scan_width / (2 * H)

    d = ((R + H) / R * np.cos(phi) - np.cos(zeta)) * sin_beta_2
    e = np.cos(zeta) - np.sqrt(np.cos(zeta) ** 2 - d ** 2)

    c_alignment = 4 * e * np.sin(zeta) / (theta_a - theta_b)

    return c_expansion, c_alignment


Instruction:
Given the following Python 3 function, write the documentation
def lonlat2xyz(lons, lats):
    R = 6370997.0
    x_coords = R * da.cos(da.deg2rad(lats)) * da.cos(da.deg2rad(lons))
    y_coords = R * da.cos(da.deg2rad(lats)) * da.sin(da.deg2rad(lons))
    z_coords = R * da.sin(da.deg2rad(lats))
    return x_coords, y_coords, z_coords

Response: Convert lon latitudes to cartesian coordinates.


Instruction:
Explain what the following Python 3 code does
def xyz2lonlat(x__, y__, z__):
    R = 6370997.0
    lons = da.rad2deg(da.arccos(x__ / da.sqrt(x__ ** 2 + y__ ** 2))) * da.sign(y__)
    lats = da.sign(z__) * (90 - da.rad2deg(da.arcsin(da.sqrt(x__ ** 2 + y__ ** 2) / R)))

    return lons, lats

Response: Get longitudes from cartesian coordinates.


Instruction:
Can you tell what is the following Python 3 function doing
def setup_fields(attrs):
    fields = {}
    iterator = list(attrs.items())
    for key, value in iterator:
        if not isinstance(value, Field):
            continue
        fields[key] = value
        del attrs[key]
    return fields

Response: Collect all fields declared on the class and remove them from attrs


Instruction:
Given the following Python 3 function, write the documentation
def _parse_jing_line(line):
    parts = line.split(':', 4)
    filename, line, column, type_, message = [x.strip() for x in parts]
    if type_ == 'fatal':
        if message in KNOWN_FATAL_MESSAGES_MAPPING:
            message = KNOWN_FATAL_MESSAGES_MAPPING[message]
    return ErrorLine(filename, line, column, type_, message)

Response: Parse a line of jing output to a list of line column type_ and message.


Instruction:
Can you generate the documentation for the following Python 3 function
def _parse_jing_output(output):
    output = output.strip()
    values = [_parse_jing_line(l) for l in output.split('\n') if l]
    return tuple(values)

Response: Parse the jing output into a tuple of line column type and message.


Instruction:
How would you implement a function in Python 3 that
runs jing. jar using the RNG file against the given XML file.

Response:
def jing(rng_filepath, *xml_filepaths):
    """Run jing.jar using the RNG file against the given XML file."""
    cmd = ['java', '-jar']
    cmd.extend([str(JING_JAR), str(rng_filepath)])
    for xml_filepath in xml_filepaths:
        cmd.append(str(xml_filepath))
    proc = subprocess.Popen(cmd,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            close_fds=True)
    out, err = proc.communicate()
    return _parse_jing_output(out.decode('utf-8'))


Instruction:
Can you create a Python 3 function that
returns an import object from text.

Response:
def import_obj_from_str(s):
    """Returns an import object (either ImportImport or FromImport) from text.
    """
    ast_obj = ast.parse(s).body[0]
    return ast_type_to_import_type[type(ast_obj)](ast_obj)


Instruction:
How would you implement a function in Python 3 that
constructs an import object from a string.

Response:
def from_str(cls, s):
        """Construct an import object from a string."""
        ast_obj = ast.parse(s).body[0]
        if not isinstance(ast_obj, cls._expected_ast_type):
            raise AssertionError(
                'Expected ast of type {!r} but got {!r}'.format(
                    cls._expected_ast_type,
                    ast_obj
                )
            )
        return cls(ast_obj)


Instruction:
Write a Python 3 function for
sorting the list of imports into a single segment list.

Response:
def sort(imports, separate=True, import_before_from=True, **classify_kwargs):
    """Sort import objects into groups.

    :param list imports: FromImport / ImportImport objects
    :param bool separate: Whether to classify and return separate segments
        of imports based on classification.
    :param bool import_before_from: Whether to sort `import ...` imports before
        `from ...` imports.

    For example:
        from os import path
        from aspy import refactor_imports
        import sys
        import pyramid

    separate = True, import_before_from = True

        import sys
        from os import path

        import pyramid

        from aspy import refactor_imports

    separate = True, import_before_from = False

        from os import path
        import sys

        import pyramid

        from aspy import refactor_imports

    separate = False, import_before_from = True

        import pyramid
        import sys
        from aspy import refactor_imports
        from os import path

    separate = False, import_before_from = False

        from aspy import refactor_imports
        from os import path
        import pyramid
        import sys
    """
    if separate:
        def classify_func(obj):
            return classify_import(
                obj.import_statement.module, **classify_kwargs
            )
        types = ImportType.__all__
    else:
        # A little cheaty, this allows future imports to sort before others
        def classify_func(obj):
            return classify_import(
                obj.import_statement.module, **classify_kwargs
            ) == ImportType.FUTURE
        types = [True, False]

    if import_before_from:
        def sort_within(obj):
            return (CLS_TO_INDEX[type(obj)],) + obj.sort_key
    else:
        def sort_within(obj):
            return tuple(obj.sort_key)

    # Partition the imports
    imports_partitioned = collections.defaultdict(list)
    for import_obj in imports:
        imports_partitioned[classify_func(import_obj)].append(import_obj)

    # sort each of the segments
    for segment_key, val in imports_partitioned.items():
        imports_partitioned[segment_key] = sorted(val, key=sort_within)

    return tuple(
        tuple(imports_partitioned[key])
        for key in types if key in imports_partitioned
    )


Instruction:
Here you have a function in Python 3, explain what it does
def classify_import(module_name, application_directories=('.',)):
    # Only really care about the first part of the path
    base, _, _ = module_name.partition('.')
    found, module_path, is_builtin = _get_module_info(
        base, application_directories,
    )
    if base == '__future__':
        return ImportType.FUTURE
    # Relative imports: `from .foo import bar`
    elif base == '':
        return ImportType.APPLICATION
    # If imp tells us it is builtin, it is builtin
    elif is_builtin:
        return ImportType.BUILTIN
    # If the module path exists in the project directories
    elif _module_path_is_local_and_is_not_symlinked(
            module_path, application_directories,
    ):
        return ImportType.APPLICATION
    # Otherwise we assume it is a system module or a third party module
    elif (
            found and
            PACKAGES_PATH not in module_path and
            not _due_to_pythonpath(module_path)
    ):
        return ImportType.BUILTIN
    else:
        return ImportType.THIRD_PARTY

Response: Classifies an import by its package.


Instruction:
Can you implement a function in Python 3 that
gives an element - like object containing a root xml element return the common metadata properties.

Response:
def parse_metadata(elm_tree):
    """Given an element-like object (:mod:`lxml.etree`)
    lookup the metadata and return the found elements

    :param elm_tree: the root xml element
    :type elm_tree: an element-like object from :mod:`lxml.etree`
    :returns: common metadata properties
    :rtype: dict

    """
    xpath = make_cnx_xpath(elm_tree)
    role_xpath = lambda xp: tuple(xpath(xp)[0].split())  # noqa: E731

    props = {
        'id': _maybe(xpath('//md:content-id/text()')),
        'version': xpath('//md:version/text()')[0],
        'created': xpath('//md:created/text()')[0],
        'revised': xpath('//md:revised/text()')[0],
        'title': xpath('//md:title/text()')[0],
        'license_url': xpath('//md:license/@url')[0],
        'language': xpath('//md:language/text()')[0],
        'authors': role_xpath('//md:role[@type="author"]/text()'),
        'maintainers': role_xpath('//md:role[@type="maintainer"]/text()'),
        'licensors': role_xpath('//md:role[@type="licensor"]/text()'),
        'keywords': tuple(xpath('//md:keywordlist/md:keyword/text()')),
        'subjects': tuple(xpath('//md:subjectlist/md:subject/text()')),
        'abstract': _squash_to_text(
            _maybe(xpath('//md:abstract')),
            remove_namespaces=True,
        ),
        'print_style': _maybe(
            xpath('//col:param[@name="print-style"]/@value'),
        ),
        'derived_from': {
            'uri': _maybe(xpath('//md:derived-from/@url')),
            'title': _maybe(xpath('//md:derived-from/md:title/text()')),
        },
    }
    return props


Instruction:
Given the following Python 3 function, write the documentation
def validate_cnxml(*content_filepaths):
    content_filepaths = [Path(path).resolve() for path in content_filepaths]
    return jing(CNXML_JING_RNG, *content_filepaths)

Response: Validates the given CNXML file against the CNXML - JING RNG.


Instruction:
Write a Python 3 function for
validating the given COLLXML file against the collxml - jing. rng RNG.

Response:
def validate_collxml(*content_filepaths):
    """Validates the given COLLXML file against the collxml-jing.rng RNG."""
    content_filepaths = [Path(path).resolve() for path in content_filepaths]
    return jing(COLLXML_JING_RNG, *content_filepaths)


Instruction:
Can you tell what is the following Python 3 function doing
def action(args):
    log.info('loading reference package')

    r = refpkg.Refpkg(args.refpkg, create=False)

    # First check if we can do n rollbacks
    q = r.contents
    for i in range(args.n):
        if q['rollback'] is None:
            log.error('Cannot rollback {} changes; '
                      'refpkg only records {} changes.'.format(args.n, i))
            return 1
        else:
            q = q['rollback']

    for i in range(args.n):
        r.rollback()

    return 0

Response: Roll back a refpkg.


Instruction:
How would you explain what the following Python 3 function does
def set_targets(x, delta=10):
    data = [] # Keep track of targets
    for row, _ in x.iterrows():
        if row == x.shape[0] - 1: # Can't predict yet, done.
            break

        # Get closing prices
        curr_close = x.close[row]
        next_close = x.close[row + 1]
        high_close = next_close + (delta / 2) # Pos. neutral zone threshold
        low_close = next_close - (delta / 2)  # Neg. neutral zone threshold

        # Get target
        if curr_close < low_close:
            target = TARGET_CODES['bearish']
        elif curr_close > high_close:
            target = TARGET_CODES['bullish']
        else:
            target = TARGET_CODES['neutral']
        data.append(target)

    return pd.Series(data=data, dtype=np.int32, name='target')

Response: Sets the target market trend for a date - based dataset.


Instruction:
Explain what the following Python 3 code does
def eval_features(json):
    return {'close'    : json[-1]['close'],
            'sma'      : SMA.eval_from_json(json),
            'rsi'      : RSI.eval_from_json(json),
            'so'       : SO.eval_from_json(json),
            'obv'      : OBV.eval_from_json(json)}

Response: Evaluates technical analysis features from JSON data.


Instruction:
Write a Python 3 script to
convert an int target code to a target name.

Response:
def target_code_to_name(code):
    """ Converts an int target code to a target name

    Since self.TARGET_CODES is a 1:1 mapping, perform a reverse lookup
    to get the more readable name.

    Args:
        code: Value from self.TARGET_CODES

    Returns:
        String target name corresponding to the given code.
    """
    TARGET_NAMES = {v: k for k, v in TARGET_CODES.items()}
    return TARGET_NAMES[code]


Instruction:
Here you have a function in Python 3, explain what it does
def setup_model(x, y, model_type='random_forest', seed=None, **kwargs):
    assert len(x) > 1 and len(y) > 1, 'Not enough data objects to train on (minimum is at least two, you have (x: {0}) and (y: {1}))'.format(len(x), len(y))

    sets = namedtuple('Datasets', ['train', 'test'])
    x_train, x_test, y_train, y_test = train_test_split(x,
                                                        y,
                                                        random_state=seed,
                                                        shuffle=False)
    x = sets(x_train, x_test)
    y = sets(y_train, y_test)

    if model_type == 'random_forest' or model_type == 'rf':
        model = rf.RandomForest(x, y, random_state=seed, **kwargs)
    elif model_type == 'deep_neural_network' or model_type == 'dnn':
        model = dnn.DeepNeuralNetwork(x, y, **kwargs)
    else:
        raise ValueError('Invalid model type kwarg')
    return model

Response: Initializes a machine learning model for the given data set.


Instruction:
Can you write a function in Python 3 where it
gets market chart data from today to a previous date

Response:
def get_json(self):
        """ Gets market chart data from today to a previous date """
        today = dt.now()
        DIRECTION = 'last'
        epochs = date.get_end_start_epochs(today.year, today.month, today.day,
                                           DIRECTION, self.unit, self.count)
        return poloniex.chart_json(epochs['shifted'], epochs['initial'],
                                   self.period, self.symbol)[0]


Instruction:
Explain what the following Python 3 code does
def set_features(self, partition=1):
        if len(self.json) < partition + 1:
            raise ValueError('Not enough dates for the specified partition size: {0}.  Try a smaller partition.'.format(partition))

        data = []
        for offset in range(len(self.json) - partition):
            json = self.json[offset : offset + partition]
            data.append(eval_features(json))
        return pd.DataFrame(data=data, dtype=np.float32)

Response: Parses market data JSON for technical analysis indicators and sets the features of the technical analysis indicators.


Instruction:
How would you implement a function in Python 3 that
sets the long features of double the duration of the entry.

Response:
def set_long_features(self, features, columns_to_set=[], partition=2):
        """ Sets features of double the duration

        Example: Setting 14 day RSIs to longer will create add a
            feature column of a 28 day RSIs.

        Args:
            features: Pandas DataFrame instance with columns as numpy.float32 features.
            columns_to_set: List of strings of feature names to make longer
            partition: Int of how many dates to take into consideration
                when evaluating technical analysis indicators.

        Returns:
            Pandas DataFrame instance with columns as numpy.float32 features.
        """
        # Create long features DataFrame
        features_long = self.set_features(partition=2 * partition)

        # Remove features not specified by args.long
        unwanted_features = [f for f in features.columns if f not in columns_to_set]
        features_long = features_long.drop(unwanted_features, axis=1)

        # Prefix long columns with 'long_' to fix naming conflicts
        features_long.columns = ['long_{0}'.format(f) for f in features_long.columns]

        # Merge the two DataFrames
        skip = partition
        return pd.concat([features[skip:].reset_index(drop=True),
                         features_long],
                         axis=1)


Instruction:
Given the following Python 3 function, write the documentation
def feature_importances(self):
        feature_names = [feature for feature in self.features.train]
        return list(zip(feature_names, self.feature_importances_))

Response: Return list of features and their importances in classification


Instruction:
Can you generate a brief explanation for the following Python 3 code
def exitcode(self):
        if self._process is None:
            raise ProcessError(
                "Process '%s' has not been started yet" % self.name)
        return self._process.exitcode

Response: Return the exit code of the process.


Instruction:
Implement a Python 3 function for
initializing the instance attributes.

Response:
def initialize(self):
        """
        Initialize instance attributes. You can override this method in
        the subclasses.
        """
        self.main_pid = os.getpid()
        self.processes.extend(self.init_service_processes())
        self.processes.extend(self.init_tornado_workers())


Instruction:
Create a Python 3 function to
handle SIGUSR1 signal. Call function which is defined in the cfg file.

Response:
def sigusr1_handler(self, unused_signum, unused_frame):
        """
        Handle SIGUSR1 signal. Call function which is defined in the
        **settings.SIGUSR1_HANDLER**. If main process, forward the
        signal to all child processes.
        """
        for process in self.processes:
            if process.pid and os.getpid() == self.main_pid:
                try:
                    os.kill(process.pid, signal.SIGUSR1)
                except ProcessLookupError:
                    pass
        if self._sigusr1_handler_func is not None:
            self._sigusr1_handler_func(self.context)


Instruction:
Can you create a Python 3 function that
initializes the service processes.

Response:
def init_service_processes(self):
        """
        Prepare processes defined in the **settings.SERVICE_PROCESSES**.
        Return :class:`list` of the :class:`ProcessWrapper` instances.
        """
        processes = []

        for process_struct in getattr(
                self.context.config.settings, 'SERVICE_PROCESSES', ()):
            process_cls = import_object(process_struct[0])
            wait_unless_ready, timeout = process_struct[1], process_struct[2]

            self.logger.info("Init service process '%s'", process_cls.__name__)

            processes.append(
                ProcessWrapper(
                    process_cls, (self.context,),
                    wait_unless_ready=wait_unless_ready,
                    timeout=timeout
                )
            )

        return processes


Instruction:
Explain what the following Python 3 code does
def init_tornado_workers(self):
        workers = []

        for tornado_app in get_tornado_apps(self.context, debug=False):
            interface = tornado_app.settings['interface']

            if not interface.port and not interface.unix_socket:
                raise ValueError(
                    'Interface MUST listen either on TCP '
                    'or UNIX socket or both')

            name, processes, host, port, unix_socket = (
                interface.name, interface.processes,
                interface.host, interface.port, interface.unix_socket)
            if processes <= 0:
                processes = tornado.process.cpu_count()
            sockets = []
            listen_on = []

            if port:
                sockets.extend(tornado.netutil.bind_sockets(port, host))
                listen_on.append("{:s}:{:d}".format(host, port))
            if unix_socket:
                sockets.append(tornado.netutil.bind_unix_socket(unix_socket))
                listen_on.append("{:s}".format(interface.unix_socket))

            self.logger.info(
                "Init %d worker(s) for interface '%s' (%s)",
                processes, name, ", ".join(listen_on))

            for dummy_i in six.moves.range(processes):
                worker = ProcessWrapper(
                    TornadoProcess, (tornado_app, sockets),
                    wait_unless_ready=True, timeout=5.0,
                    name=name
                )
                workers.append(worker)

        return workers

Response: Prepare worker instances for all Tornado applications. Return a list of the ProcessWrapper instances.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def start_processes(self, max_restarts=-1):
        while 1:
            for process in self.processes:
                if not process:
                    # When process has not been started, start it
                    if not process.has_started:
                        process.start()
                        continue
                    # When process has stopped, start it again
                    exitcode = process.exitcode
                    if exitcode != 0:
                        # Process has been signaled or crashed
                        if exitcode > 0:
                            self.logger.error(
                                "Process '%s' with pid %d died with exitcode "
                                "%d", process.name, process.pid, exitcode
                            )
                        else:
                            self.logger.error(
                                "Process '%s' with pid %d died due to %s",
                                process.name, process.pid,
                                SIGNALS_TO_NAMES_DICT[abs(exitcode)]
                            )
                        # Max restarts has been reached, exit
                        if not max_restarts:
                            self.logger.fatal("Too many child restarts")
                            break
                        # Start process again
                        process.start()
                        # Decrement max_restarts counter
                        if max_restarts > 0:
                            max_restarts -= 1
                    else:
                        # Process has stopped without error
                        self.logger.info(
                            "Process '%s' with pid %d has stopped",
                            process.name, process.pid
                        )
                        # Start process again
                        process.start()
                        self.logger.info(
                            "Process '%s' has been started with pid %d",
                            process.name, process.pid
                        )
            else:
                time.sleep(0.25)
                continue
            break

Response: Start processes and check their status. When some processes crashes start it again.


Instruction:
Implement a Python 3 function for
reading a CSV taxonomy from handle into a Tree.

Response:
def taxtable_to_tree(handle):
    """Read a CSV taxonomy from *handle* into a Tree."""
    c = csv.reader(handle, quoting=csv.QUOTE_NONNUMERIC)
    header = next(c)
    rootdict = dict(list(zip(header, next(c))))
    t = Tree(rootdict['tax_id'], rank=rootdict[
             'rank'], tax_name=rootdict['tax_name'])
    for l in c:
        d = dict(list(zip(header, l)))
        target = t.descendents[d['parent_id']]
        target(Tree(d['tax_id'], rank=d['rank'], tax_name=d['tax_name']))
    return t


Instruction:
Can you implement a function in Python 3 that
returns a set of species tax_ids which will make those in tax_ids not lonely.

Response:
def lonely_company(taxonomy, tax_ids):
    """Return a set of species tax_ids which will makes those in *tax_ids* not lonely.

    The returned species will probably themselves be lonely.
    """
    return [taxonomy.species_below(taxonomy.sibling_of(t)) for t in tax_ids]


Instruction:
Write a Python 3 script to
return a set of non - lonely species tax_ids that will make those in tax_ids not lonely.

Response:
def solid_company(taxonomy, tax_ids):
    """Return a set of non-lonely species tax_ids that will make those in *tax_ids* not lonely."""
    res = []
    for t in tax_ids:
        res.extend(taxonomy.nary_subtree(taxonomy.sibling_of(t), 2) or [])
    return res


Instruction:
Implement a function in Python 3 to
evaluate the algorithm of the OBV .

Response:
def eval_algorithm(curr, prev):
        """ Evaluates OBV

        Args:
            curr: Dict of current volume and close
            prev: Dict of previous OBV and close

        Returns:
            Float of OBV
        """
        if curr['close'] > prev['close']:
            v = curr['volume']
        elif curr['close'] < prev['close']:
            v = curr['volume'] * -1
        else:
            v = 0
        return prev['obv'] + v


Instruction:
Can you write a function in Python 3 where it
evaluates the OBV from a JSON object.

Response:
def eval_from_json(json):
        """ Evaluates OBV from JSON (typically Poloniex API response)

        Args:
            json: List of dates where each entry is a dict of raw market data.

        Returns:
            Float of OBV
        """
        closes = poloniex.get_attribute(json, 'close')
        volumes = poloniex.get_attribute(json, 'volume')
        obv = 0
        for date in range(1, len(json)):
            curr = {'close': closes[date], 'volume': volumes[date]}
            prev = {'close': closes[date - 1], 'obv': obv}
            obv = OBV.eval_algorithm(curr, prev)
        return obv


Instruction:
Write a Python 3 script for
creating a temporary file and return its name.

Response:
def scratch_file(unlink=True, **kwargs):
    """Create a temporary file and return its name.

    Additional arguments are passed to :class:`tempfile.NamedTemporaryFile`

    At the start of the with block a secure, temporary file is created
    and its name returned.  At the end of the with block it is
    deleted.
    """
    kwargs['delete'] = False
    tf = tempfile.NamedTemporaryFile(**kwargs)
    tf.close()
    try:
        yield tf.name
    finally:
        if unlink:
            os.unlink(tf.name)


Instruction:
Create a Python 3 function to
return an open file object for a file in the reference package.

Response:
def open(self, name, *mode):
        """
        Return an open file object for a file in the reference package.
        """
        return self.file_factory(self.file_path(name), *mode)


Instruction:
Write a Python 3 function that can
return an open file object for a particular resource in this reference package.

Response:
def open_resource(self, resource, *mode):
        """
        Return an open file object for a particular named resource in this
        reference package.
        """
        return self.open(self.resource_name(resource), *mode)


Instruction:
Explain what the following Python 3 code does
def resource_name(self, resource):
        if not(resource in self.contents['files']):
            raise ValueError("No such resource %r in refpkg" % (resource,))
        return self.contents['files'][resource]

Response: Return the name of the file within the reference package for a particular named resource.


Instruction:
Implement a function in Python 3 to
return the stored MD5 sum for a particular resource.

Response:
def resource_md5(self, resource):
        """Return the stored MD5 sum for a particular named resource."""
        if not(resource in self.contents['md5']):
            raise ValueError("No such resource %r in refpkg" % (resource,))
        return self.contents['md5'][resource]


Instruction:
Implement a function in Python 3 to
set some default values in the manifest.

Response:
def _set_defaults(self):
        """
        Set some default values in the manifest.

        This method should be called after loading from disk, but before
        checking the integrity of the reference package.
        """
        self.contents.setdefault('log', [])
        self.contents.setdefault('rollback', None)
        self.contents.setdefault('rollforward', None)


Instruction:
Can you implement a function in Python 3 that
writes any changes made on Refpkg to disk.

Response:
def _sync_to_disk(self):
        """Write any changes made on Refpkg to disk.

        Other methods of Refpkg that alter the contents of the package
        will call this method themselves.  Generally you should never
        have to call it by hand.  The only exception would be if
        another program has changed the Refpkg on disk while your
        program is running and you want to force your version over it.
        Otherwise it should only be called by other methods of refpkg.
        """
        with self.open_manifest('w') as h:
            json.dump(self.contents, h, indent=4)
            h.write('\n')


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

        try:
            fobj = self.open_manifest('r')
        except IOError as e:
            if e.errno == errno.ENOENT:
                raise ValueError(
                    "couldn't find manifest file in %s" % (self.path,))
            elif e.errno == errno.ENOTDIR:
                raise ValueError("%s is not a directory" % (self.path,))
            else:
                raise
        with fobj:
            self.contents = json.load(fobj)

        self._set_defaults()
        self._check_refpkg()

Response: Read any changes made on disk to this Refpkg.


Instruction:
Create a Python 3 function for
copying a file into the reference package.

Response:
def _add_file(self, key, path):
        """Copy a file into the reference package."""
        filename = os.path.basename(path)
        base, ext = os.path.splitext(filename)
        if os.path.exists(self.file_path(filename)):
            with tempfile.NamedTemporaryFile(
                    dir=self.path, prefix=base, suffix=ext) as tf:
                filename = os.path.basename(tf.name)
        shutil.copyfile(path, self.file_path(filename))
        self.contents['files'][key] = filename


Instruction:
Can you tell what is the following Python 3 function doing
def is_invalid(self):
        # Manifest file contains the proper keys
        for k in ['metadata', 'files', 'md5']:
            if not(k in self.contents):
                return "Manifest file missing key %s" % k
            if not(isinstance(self.contents[k], dict)):
                return "Key %s in manifest did not refer to a dictionary" % k

        if not('rollback' in self.contents):
            return "Manifest file missing key rollback"
        if not(isinstance(self.contents['rollback'], dict)) and self.contents[
                "rollback"] is not None:
            return ("Key rollback in manifest did not refer to a "
                    "dictionary or None, found %s") % str(self.contents['rollback'])

        if not('rollforward' in self.contents):
            return "Manifest file missing key rollforward"

        if self.contents['rollforward'] is not None:
            if not(isinstance(self.contents['rollforward'], list)):
                return "Key rollforward was not a list, found %s" % str(
                    self.contents['rollforward'])
            elif len(self.contents['rollforward']) != 2:
                return "Key rollforward had wrong length, found %d" % \
                    len(self.contents['rollforward'])
            elif not is_string(self.contents['rollforward'][0]):
                print(type(self.contents['rollforward'][0]))
                return "Key rollforward's first entry was not a string, found %s" % \
                    str(self.contents['rollforward'][0])
            elif not(isinstance(self.contents['rollforward'][1], dict)):
                return "Key rollforward's second entry was not a dict, found %s" % \
                    str(self.contents['rollforward'][1])

        if not("log" in self.contents):
            return "Manifest file missing key 'log'"

        if not(isinstance(self.contents['log'], list)):
            return "Key 'log' in manifest did not refer to a list"

        # MD5 keys and filenames are in one to one correspondence
        if self.contents['files'].keys() != self.contents[
                'md5'].keys():
            return ("Files and MD5 sums in manifest do not "
                    "match (files: %s, MD5 sums: %s)") % \
                (list(self.contents['files'].keys()),
                 list(self.contents['md5'].keys()))

        # All files in the manifest exist and match the MD5 sums
        for key, filename in self.contents['files'].items():
            # we don't need to explicitly check for existence;
            # calculate_resource_md5 will open the file for us.
            expected_md5 = self.resource_md5(key)
            found_md5 = self.calculate_resource_md5(key)
            if found_md5 != expected_md5:
                return ("File %s referred to by key %s did "
                        "not match its MD5 sum (found: %s, expected %s)") % \
                    (filename, key, found_md5, expected_md5)
        return False

Response: Checks if this RefPkg is invalid.


Instruction:
Write a Python 3 script for
updating the metadata for the specified key. Returns the previous value of key.

Response:
def update_metadata(self, key, value):
        """Set *key* in the metadata to *value*.

        Returns the previous value of *key*, or None if the key was
        not previously set.
        """
        old_value = self.contents['metadata'].get(key)
        self.contents['metadata'][key] = value
        self._log('Updated metadata: %s=%s' % (key, value))
        return old_value


Instruction:
Create a Python 3 function to
update the file with the given key.

Response:
def update_file(self, key, new_path):
        """Insert file *new_path* into the refpkg under *key*.

        The filename of *new_path* will be preserved in the refpkg
        unless it would conflict with a previously existing file, in
        which case a suffix is appended which makes it unique.  The
        previous file, if there was one, is left in the refpkg.  If
        you wish to delete it, see the ``strip`` method.

        The full path to the previous file referred to by *key* is
        returned, or ``None`` if *key* was not previously defined in
        the refpkg.
        """
        if key in self.contents['files']:
            old_path = self.resource_path(key)
        else:
            old_path = None
        self._add_file(key, new_path)
        with open(new_path, 'rb') as f:
            md5_value = md5file(f)
        self.contents['md5'][key] = md5_value
        self._log('Updated file: %s=%s' % (key, new_path))
        if key == 'tree_stats' and old_path:
            warnings.warn('Updating tree_stats, but not phylo_model.',
                          DerivedFileNotUpdatedWarning, stacklevel=2)
        return old_path


Instruction:
Make a summary of the following Python 3 code
def reroot(self, rppr=None, pretend=False):
        with scratch_file(prefix='tree', suffix='.tre') as name:
            # Use a specific path to rppr, otherwise rely on $PATH
            subprocess.check_call([rppr or 'rppr', 'reroot',
                                   '-c', self.path, '-o', name])
            if not(pretend):
                self.update_file('tree', name)
        self._log('Rerooting refpkg')

Response: Reroot the phylogenetic tree.


Instruction:
How would you code a function in Python 3 to
parse a stats log and use it to update the phylo model.

Response:
def update_phylo_model(self, stats_type, stats_file, frequency_type=None):
        """Parse a stats log and use it to update ``phylo_model``.

        ``pplacer`` expects its input to include the deatils of the
        phylogenetic model used for creating a tree in JSON format
        under the key ``phylo_model``, but no program actually outputs
        that format.

        This function takes a log generated by RAxML or FastTree, parses it,
        and inserts an appropriate JSON file into the refpkg. The first
        parameter must be 'RAxML', 'PhyML' or 'FastTree', depending on which
        program generated the log. It may also be None to attempt to guess
        which program generated the log.

        :param stats_type: Statistics file type. One of 'RAxML', 'FastTree', 'PhyML'
        :param stats_file: path to statistics/log file
        :param frequency_type: For ``stats_type == 'PhyML'``, amino acid
         alignments only: was the alignment inferred with ``model`` or
         ``empirical`` frequencies?

        """

        if frequency_type not in (None, 'model', 'empirical'):
            raise ValueError(
                'Unknown frequency type: "{0}"'.format(frequency_type))
        if frequency_type and stats_type not in (None, 'PhyML'):
            raise ValueError('Frequency type should only be specified for '
                             'PhyML alignments.')

        if stats_type is None:
            with open(stats_file) as fobj:
                for line in fobj:
                    if line.startswith('FastTree'):
                        stats_type = 'FastTree'
                        break
                    elif (line.startswith('This is RAxML') or
                          line.startswith('You are using RAxML')):
                        stats_type = 'RAxML'
                        break
                    elif 'PhyML' in line:
                        stats_type = 'PhyML'
                        break
                else:
                    raise ValueError(
                        "couldn't guess log type for %r" % (stats_file,))

        if stats_type == 'RAxML':
            parser = utils.parse_raxml
        elif stats_type == 'FastTree':
            parser = utils.parse_fasttree
        elif stats_type == 'PhyML':
            parser = functools.partial(utils.parse_phyml,
                                       frequency_type=frequency_type)
        else:
            raise ValueError('invalid log type: %r' % (stats_type,))

        with scratch_file(prefix='phylo_model', suffix='.json') as name:
            with open(name, 'w') as phylo_model, open(stats_file) as h:
                json.dump(parser(h), phylo_model, indent=4)
            self.update_file('phylo_model', name)


Instruction:
How would you explain what the following Python 3 function does
def rollback(self):
        # This is slightly complicated because of Python's freakish
        # assignment semantics and because we don't store multiple
        # copies of the log.
        if self.contents['rollback'] is None:
            raise ValueError("No operation to roll back on refpkg")
        future_msg = self.contents['log'][0]
        rolledback_log = self.contents['log'][1:]
        rollforward = copy.deepcopy(self.contents)
        rollforward.pop('rollback')
        self.contents = self.contents['rollback']
        self.contents['log'] = rolledback_log
        self.contents['rollforward'] = [future_msg, rollforward]
        self._sync_to_disk()

Response: Revert the previous modification to the refpkg.


Instruction:
Write a Python 3 script to
restore a reverted modification to the refpkg.

Response:
def rollforward(self):
        """Restore a reverted modification to the refpkg.
        """
        if self.contents['rollforward'] is None:
            raise ValueError("No operation to roll forward on refpkg")
        new_log_message = self.contents['rollforward'][0]
        new_contents = self.contents['rollforward'][1]
        new_contents['log'] = [new_log_message] + self.contents.pop('log')
        self.contents['rollforward'] = None
        new_contents['rollback'] = copy.deepcopy(self.contents)
        new_contents['rollback'].pop('rollforward')
        self.contents = new_contents
        self._sync_to_disk()


Instruction:
Can you implement a function in Python 3 that
removes rollbacks rollforwards and all non - current files.

Response:
def strip(self):
        """Remove rollbacks, rollforwards, and all non-current files.

        When distributing a refpkg, you probably want to distribute as
        small a one as possible.  strip removes everything from the
        refpkg which is not relevant to its current state.
        """
        self._sync_from_disk()
        current_filenames = set(self.contents['files'].values())
        all_filenames = set(os.listdir(self.path))
        to_delete = all_filenames.difference(current_filenames)
        to_delete.discard('CONTENTS.json')
        for f in to_delete:
            self._delete_file(f)
        self.contents['rollback'] = None
        self.contents['rollforward'] = None
        self.contents['log'].insert(
            0, 'Stripped refpkg (removed %d files)' % len(to_delete))
        self._sync_to_disk()


Instruction:
Write a Python 3 function that can
begin a transaction to group operations on the refpkg.

Response:
def start_transaction(self):
        """Begin a transaction to group operations on the refpkg.

        All the operations until the next call to
        ``commit_transaction`` will be recorded as a single operation
        for rollback and rollforward, and recorded with a single line
        in the log.
        """
        if self.current_transaction:
            raise ValueError("There is already a transaction going")
        else:
            initial_state = copy.deepcopy(self.contents)
            self.current_transaction = {'rollback': initial_state,
                                        'log': '(Transaction left no log message)'}


Instruction:
How would you code a function in Python 3 to
commit a transaction with a given log entry.

Response:
def commit_transaction(self, log=None):
        """Commit a transaction, with *log* as the log entry."""
        self.current_transaction['rollback'].pop('log')
        self.current_transaction['rollback'].pop('rollforward')
        self.contents['log'].insert(
            0, log and log or self.current_transaction['log'])
        self.contents['rollback'] = self.current_transaction['rollback']
        self.contents['rollforward'] = None  # We can't roll forward anymore
        self.current_transaction = None
        self._sync_to_disk()


Instruction:
Can you implement a function in Python 3 that
loads the taxonomy into a sqlite3 database.

Response:
def load_db(self):
        """Load the taxonomy into a sqlite3 database.

        This will set ``self.db`` to a sqlite3 database which contains all of
        the taxonomic information in the reference package.
        """

        db = taxdb.Taxdb()
        db.create_tables()
        reader = csv.DictReader(self.open_resource('taxonomy', 'rU'))
        db.insert_from_taxtable(lambda: reader._fieldnames, reader)

        curs = db.cursor()
        reader = csv.DictReader(self.open_resource('seq_info', 'rU'))
        curs.executemany("INSERT INTO sequences VALUES (?, ?)",
                         ((row['seqname'], row['tax_id']) for row in reader))

        db.commit()
        self.db = db


Instruction:
Can you write a function in Python 3 where it
finds the most recent common ancestor of the specified tax_ids.

Response:
def most_recent_common_ancestor(self, *ts):
        """Find the MRCA of some tax_ids.

        Returns the MRCA of the specified tax_ids, or raises ``NoAncestor`` if
        no ancestor of the specified tax_ids could be found.
        """
        if len(ts) > 200:
            res = self._large_mrca(ts)
        else:
            res = self._small_mrca(ts)

        if res:
            (res,), = res
        else:
            raise NoAncestor()
        return res


Instruction:
How would you explain what the following Python 3 function does
def _large_mrca(self, ts):
        cursor = self.db.cursor()

        cursor.execute("""
            DROP TABLE IF EXISTS _mrca_temp
        """)

        cursor.execute("""
            CREATE TEMPORARY TABLE _mrca_temp(
                child TEXT PRIMARY KEY REFERENCES taxa (tax_id) NOT NULL
            )
        """)

        cursor.executemany("""
            INSERT INTO _mrca_temp
            VALUES (?)
        """, ((tid,) for tid in ts))
        cursor.execute("""
            SELECT parent
            FROM   _mrca_temp
                   JOIN parents USING (child)
                   JOIN taxa
                     ON parent = taxa.tax_id
                   JOIN ranks USING (rank)
            GROUP  BY parent
            HAVING COUNT(*) = ?
            ORDER  BY rank_order DESC
            LIMIT  1
        """, (len(ts),))

        return cursor.fetchall()

Response: Find the MRCA using a temporary table.


Instruction:
Explain what the following Python 3 code does
def _small_mrca(self, ts):
        cursor = self.db.cursor()
        qmarks = ', '.join('?' * len(ts))
        cursor.execute("""
            SELECT parent
            FROM   parents
                   JOIN taxa
                     ON parent = taxa.tax_id
                   JOIN ranks USING (rank)
            WHERE  child IN (%s)
            GROUP  BY parent
            HAVING COUNT(*) = ?
            ORDER  BY rank_order DESC
            LIMIT  1
        """ % qmarks, ts + (len(ts),))

        return cursor.fetchall()

Response: Find a small MRCA using query parameters.


Instruction:
Given the following Python 3 function, write the documentation
def file_abspath(self, resource):
        warnings.warn(
            "file_abspath is deprecated; use resource_path instead",
            DeprecationWarning, stacklevel=2)
        return self.resource_path(resource)

Response: Deprecated alias for resource_path *.


Instruction:
Can you tell what is the following Python 3 function doing
def file_name(self, resource):
        warnings.warn(
            "file_name is deprecated; use resource_name instead",
            DeprecationWarning, stacklevel=2)
        return self.resource_name(resource)

Response: Deprecated alias for resource_name.


Instruction:
Create a Python 3 function to
deprecate alias for resource_md5. Use resource_md5 instead.

Response:
def file_md5(self, resource):
        """Deprecated alias for *resource_md5*."""
        warnings.warn(
            "file_md5 is deprecated; use resource_md5 instead",
            DeprecationWarning, stacklevel=2)
        return self.resource_md5(resource)


Instruction:
Write a Python 3 script to
return the settings module of the application according to the command line argument or the SHELTER_SETTINGS_MODULE environment variable.

Response:
def get_app_settings(parser, known_args):
    """
    Return **settings** module of the application according to
    either command line argument or **SHELTER_SETTINGS_MODULE**
    environment variable.
    """
    args, dummy_remaining = parser.parse_known_args(known_args)
    settings_module_path = (
        args.settings or os.environ.get('SHELTER_SETTINGS_MODULE', ''))
    if not settings_module_path:
        return None
    return importlib.import_module(settings_module_path)


Instruction:
How would you implement a function in Python 3 that
returns a dictionary of all management commands registered in the settings.

Response:
def get_management_commands(settings):
    """
    Find registered managemend commands and return their classes
    as a :class:`dict`. Keys are names of the management command
    and values are classes of the management command.
    """
    app_commands = getattr(settings, 'MANAGEMENT_COMMANDS', ())
    commands = {}
    for name in itertools.chain(SHELTER_MANAGEMENT_COMMANDS, app_commands):
        command_obj = import_object(name)
        if not issubclass(command_obj, BaseCommand):
            raise ValueError("'%s' is not subclass of the BaseCommand" % name)
        commands[command_obj.name] = command_obj
    return commands


Instruction:
Write a Python 3 script for
returning either config class or default.

Response:
def get_config_class(settings):
    """
    According to **settings.CONFIG_CLASS** return either config class
    defined by user or default :class:`shelter.core.config.Config`.
    """
    config_cls_name = getattr(settings, 'CONFIG_CLASS', '')
    if config_cls_name:
        config_cls = import_object(config_cls_name)
    else:
        config_cls = Config
    return config_cls


Instruction:
Can you tell what is the following Python 3 function doing
def main(args=None):
    # Base command line parser. Help is not allowed because command
    # line is parsed in two stages - in the first stage is found setting
    # module of the application, in the second stage are found management
    # command's arguments.
    parser = ArgumentParser(add_help=False)
    parser.add_argument(
        '-s', '--settings',
        dest='settings', action='store', type=str, default=None,
        help=_('application settings module')
    )

    # Get settings module
    try:
        settings = get_app_settings(parser, args)
    except ImportError as exc:
        parser.error(_("Invalid application settings module: {}").format(exc))

    # Get management commands and add their arguments into command
    # line parser
    commands = get_management_commands(settings)
    subparsers = parser.add_subparsers(
        dest='action', help=_('specify action')
    )
    for command_cls in six.itervalues(commands):
        subparser = subparsers.add_parser(
            command_cls.name, help=command_cls.help)
        for command_args, kwargs in command_cls.arguments:
            subparser.add_argument(*command_args, **kwargs)

    # Get config class and add its arguments into command line parser
    if settings:
        config_cls = get_config_class(settings)
        if not issubclass(config_cls, Config):
            raise TypeError(
                "Config class must be subclass of the "
                "shelter.core.config.Config")
        for config_args, kwargs in config_cls.arguments:
            parser.add_argument(*config_args, **kwargs)
    else:
        config_cls = Config

    # Add help argument and parse command line
    parser.add_argument(
        '-h', '--help', action='help',
        help=_('show this help message and exit')
    )
    cmdline_args = parser.parse_args(args)
    if not cmdline_args.action:
        parser.error(_('No action'))

    # Run management command
    command_cls = commands[cmdline_args.action]
    if command_cls.settings_required and not settings:
        parser.error(_(
            "Settings module is not defined. You must either set "
            "'SHELTER_SETTINGS_MODULE' environment variable or "
            "'-s/--settings' command line argument."
        ))
    try:
        config = config_cls(settings, cmdline_args)
    except ImproperlyConfiguredError as exc:
        parser.error(str(exc))
    command = command_cls(config)
    try:
        command()
    except Exception:
        traceback.print_exc(file=sys.stderr)
        sys.stderr.flush()
        if multiprocessing.active_children():
            # If main process has children processes, exit immediately without
            # cleaning. It is a workaround, because parent process waits for
            # non-daemon children.
            os._exit(1)
        sys.exit(1)
    sys.exit(0)

Response: Main function for the management command line.


Instruction:
Can you tell what is the following Python 3 function doing
def as_length(self, value):
        new_vec = self.copy()
        new_vec.length = value
        return new_vec

Response: Return a new vector scaled to given length


Instruction:
Here you have a function in Python 3, explain what it does
def as_percent(self, value):
        new_vec = self.copy()
        new_vec.length = value * self.length
        return new_vec

Response: Return a new vector scaled by given decimal percent


Instruction:
Implement a Python 3 function for
dotting product with another vector

Response:
def dot(self, vec):
        """Dot product with another vector"""
        if not isinstance(vec, self.__class__):
            raise TypeError('Dot product operand must be a vector')
        return np.dot(self, vec)


Instruction:
Can you tell what is the following Python 3 function doing
def cross(self, vec):
        if not isinstance(vec, self.__class__):
            raise TypeError('Cross product operand must be a vector')
        return self.__class__(np.cross(self, vec))

Response: Cross product with another vector


Instruction:
Here you have a function in Python 3, explain what it does
def angle(self, vec, unit='rad'):
        if not isinstance(vec, self.__class__):
            raise TypeError('Angle operand must be of class {}'
                            .format(self.__class__.__name__))
        if unit not in ['deg', 'rad']:
            raise ValueError('Only units of rad or deg are supported')

        denom = self.length * vec.length
        if denom == 0:
            raise ZeroDivisionError('Cannot calculate angle between '
                                    'zero-length vector(s)')

        ang = np.arccos(self.dot(vec) / denom)
        if unit == 'deg':
            ang = ang * 180 / np.pi
        return ang

Response: Calculate the angle between two Vectors


Instruction:
Can you implement a function in Python 3 that
returns the polar angle of this vector in radians

Response:
def phi(self):
        """Polar angle / inclination of this vector in radians

        Based on sperical coordinate space
        returns angle between this vector and the positive z-azis
        range: (0 <= phi <= pi)
        """
        return np.arctan2(np.sqrt(self.x**2 + self.y**2), self.z)


Instruction:
Make a summary of the following Python 3 code
def cross(self, vec):
        if not isinstance(vec, self.__class__):
            raise TypeError('Cross product operand must be a vector')
        return Vector3(0, 0, np.asscalar(np.cross(self, vec)))

Response: Cross product with another vector


Instruction:
Given the following Python 3 function, write the documentation
def length(self):
        return np.sqrt(np.sum(self**2, axis=1)).view(np.ndarray)

Response: Array of vector lengths


Instruction:
Can you implement a function in Python 3 that
dots product with another vector.

Response:
def dot(self, vec):
        """Dot product with another vector"""
        if not isinstance(vec, self.__class__):
            raise TypeError('Dot product operand must be a VectorArray')
        if self.nV != 1 and vec.nV != 1 and self.nV != vec.nV:
            raise ValueError('Dot product operands must have the same '
                             'number of elements.')
        return np.sum((getattr(self, d)*getattr(vec, d) for d in self.dims), 1)


Instruction:
Write a Python 3 function that can
cross product with another Vector3Array.

Response:
def cross(self, vec):
        """Cross product with another Vector3Array"""
        if not isinstance(vec, Vector3Array):
            raise TypeError('Cross product operand must be a Vector3Array')
        if self.nV != 1 and vec.nV != 1 and self.nV != vec.nV:
            raise ValueError('Cross product operands must have the same '
                             'number of elements.')
        return Vector3Array(np.cross(self, vec))


Instruction:
How would you explain what the following Python 3 function does
def eval_rs(gains, losses):
        # Number of days that the data was collected through
        count = len(gains) + len(losses)

        avg_gains = stats.avg(gains, count=count) if gains else 1
        avg_losses = stats.avg(losses,count=count) if losses else 1
        if avg_losses == 0:
            return avg_gains
        else:
            return avg_gains / avg_losses

Response: Evaluates the RS variable in the RSI algorithm


Instruction:
Make a summary of the following Python 3 code
def eval_from_json(json):
        changes = poloniex.get_gains_losses(poloniex.parse_changes(json))
        return RSI.eval_algorithm(changes['gains'], changes['losses'])

Response: Evaluates RSI from JSON


Instruction:
How would you implement a function in Python 3 that
handles SIGUSR1 signal. Call function which is defined in the CONFIG_SYS_SIGUSR1_HANDLER.

Response:
def sigusr1_handler(self, unused_signum, unused_frame):
        """
        Handle SIGUSR1 signal. Call function which is defined in the
        **settings.SIGUSR1_HANDLER**.
        """
        if self._sigusr1_handler_func is not None:
            self._sigusr1_handler_func(self.context)


Instruction:
Can you tell what is the following Python 3 function doing
def sigusr2_handler(self, unused_signum, unused_frame):
        if self._sigusr1_handler_func is not None:
            self._sigusr2_handler_func(self.context)

Response: Handle SIGUSR2 signal. Call function which is defined in the CONFIG_SYS_SIGUSR2_HANDLER.


Instruction:
Write a Python 3 function for
showing information about reference packages.

Response:
def action(args):
    """
    Show information about reference packages.
    """
    log.info('loading reference package')

    pkg = refpkg.Refpkg(args.refpkg, create=False)

    with open(pkg.file_abspath('seq_info'), 'rU') as seq_info:
        seqinfo = list(csv.DictReader(seq_info))
        snames = [row['seqname'] for row in seqinfo]

    if args.seq_names:
        print('\n'.join(snames))
    elif args.tally:
        tally_taxa(pkg)
    elif args.lengths:
        print_lengths(pkg)
    else:
        print('number of sequences:', len(snames))
        print('package components\n', '\n'.join(sorted(pkg.file_keys())))


Instruction:
Write a Python 3 script for
converting a JSON to a URL by the Poloniex API representing the given JSON.

Response:
def json_to_url(json, symbol):
    """ Converts a JSON to a URL by the Poloniex API

    Args:
        json: JSON data as a list of dict dates, where the keys are
            the raw market statistics.
        symbol: String of currency pair, like a ticker symbol.

    Returns:
        String URL to Poloniex API representing the given JSON.
    """
    start = json[0]['date']
    end = json[-1]['date']
    diff = end - start

    # Get period by a ratio from calculated period to valid periods
    # Ratio closest to 1 is the period
    # Valid values: 300, 900, 1800, 7200, 14400, 86400
    periods = [300, 900, 1800, 7200, 14400, 86400]

    diffs = {}
    for p in periods:
        diffs[p] = abs(1 - (p / (diff / len(json)))) # Get ratio

    period = min(diffs, key=diffs.get) # Find closest period

    url = ('https://poloniex.com/public?command'
           '=returnChartData&currencyPair={0}&start={1}'
           '&end={2}&period={3}').format(symbol, start, end, period)
    return url


Instruction:
How would you code a function in Python 3 to
request and returns the JSON data from Poloniex API for a given market pair.

Response:
def chart_json(start, end, period, symbol):
    """ Requests chart data from Poloniex API

    Args:
        start: Int epoch date to START getting market stats from.
            Note that this epoch is FURTHER from the current date.
        end: Int epoch date to STOP getting market stats from.
            Note that this epoch is CLOSER to the current date.
        period: Int defining width of each chart candlestick in seconds.
            Valid values: 300, 900, 1800, 7200, 14400, 86400
        symbol: String of currency pair, like a ticker symbol.

    Returns:
        Tuple of (JSON data, URL to JSON).
        JSON data as a list of dict dates, where the keys are
        the raw market statistics.
        String URL to Poloniex API representing the given JSON.
    """
    url = ('https://poloniex.com/public?command'
           '=returnChartData&currencyPair={0}&start={1}'
           '&end={2}&period={3}').format(symbol, start, end, period)
    logger.debug(' HTTP Request URL:\n{0}'.format(url))
    json = requests.get(url).json()
    logger.debug(' JSON:\n{0}'.format(json))

    if 'error' in json:
        logger.error(' Invalid parameters in URL for HTTP response')
        raise SystemExit
    elif all(val == 0 for val in json[0]):
        logger.error(' Bad HTTP response.  Time unit too short?')
        raise SystemExit
    elif len(json) < 1: # time to short
        logger.error(' Not enough dates to calculate changes')
        raise SystemExit

    return json, url


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_changes(json):
    changes = []
    dates = len(json)
    for date in range(1, dates):
        last_close = json[date - 1]['close']
        now_close = json[date]['close']
        changes.append(now_close - last_close)
    logger.debug('Market Changes (from JSON):\n{0}'.format(changes))
    return changes

Response: Parses the JSON data into a list of floats of price changes between entries in the JSON.


Instruction:
Implement a Python 3 function for
categorizing changes into gains and losses.

Response:
def get_gains_losses(changes):
    """ Categorizes changes into gains and losses

    Args:
        changes: List of floats of price changes between entries in JSON.

    Returns:
        Dict of changes with keys 'gains' and 'losses'.
        All values are positive.
    """
    res = {'gains': [], 'losses': []}
    for change in changes:
        if change > 0:
            res['gains'].append(change)
        else:
            res['losses'].append(change * -1)
    logger.debug('Gains: {0}'.format(res['gains']))
    logger.debug('Losses: {0}'.format(res['losses']))
    return res


Instruction:
Create a Python 3 function to
get the values of an attribute from JSON data

Response:
def get_attribute(json, attr):
    """ Gets the values of an attribute from JSON

    Args:
        json: JSON data as a list of dict dates, where the keys are
            the raw market statistics.
        attr: String of attribute in JSON file to collect.

    Returns:
        List of values of specified attribute from JSON
    """
    res = [json[entry][attr] for entry, _ in enumerate(json)]
    logger.debug('{0}s (from JSON):\n{1}'.format(attr, res))
    return res


Instruction:
Implement a function in Python 3 to
get JSON from shifted date by Poloniex API.

Response:
def get_json_shift(year, month, day, unit, count, period, symbol):
    """ Gets JSON from shifted date by the Poloniex API

    Args:
        year: Int between 1 and 9999.
        month: Int between 1 and 12.
        day: Int between 1 and 31.
        unit: String of time period unit for count argument.
            How far back to check historical market data.
            Valid values: 'hour', 'day', 'week', 'month', 'year'
        count: Int of units.
            How far back to check historical market data.
        period: Int defining width of each chart candlestick in seconds.
        symbol: String of currency pair, like a ticker symbol.

    Returns: JSON, list of dates where each entry is a dict of raw market data.
    """
    epochs = date.get_end_start_epochs(year, month, day, 'last', unit, count)
    return chart_json(epochs['shifted'], epochs['initial'],
                      period, symbol)[0]


Instruction:
Given the following Python 3 function, write the documentation
def filter_ranks(results):
    for _, group in itertools.groupby(results, operator.itemgetter(0)):
        yield next(group)

Response: Yields all the ranks for all the results for a given tax_id.


Instruction:
Make a summary of the following Python 3 code
def eval_algorithm(closing, low, high):
        if high - low == 0: # High and low are the same, zero division error
            return 100 * (closing - low)
        else:
            return 100 * (closing - low) / (high - low)

Response: Evaluates the SO algorithm for the current closing price.


Instruction:
Given the following Python 3 function, write the documentation
def eval_from_json(json):
        close  = json[-1]['close'] # Latest closing price
        low    = min(poloniex.get_attribute(json, 'low')) # Lowest low
        high   = max(poloniex.get_attribute(json, 'high')) # Highest high
        return SO.eval_algorithm(close, low, high)

Response: Evaluates the SO from a JSON dict.


Instruction:
Write a Python 3 script to
return the average value of a list of numbers.

Response:
def avg(vals, count=None):
    """ Returns the average value

    Args:
        vals: List of numbers to calculate average from.
        count: Int of total count that vals was part of.

    Returns:
        Float average value throughout a count.
    """
    sum = 0
    for v in vals:
        sum += v
    if count is None:
        count = len(vals)
    return float(sum) / count


Instruction:
Can you implement a function in Python 3 that
creates a connection object to a database.

Response:
def db_connect(engine, schema=None, clobber=False):
    """Create a connection object to a database. Attempt to establish a
    schema. If there are existing tables, delete them if clobber is
    True and return otherwise. Returns a sqlalchemy engine object.

    """

    if schema is None:
        base = declarative_base()
    else:
        try:
            engine.execute(sqlalchemy.schema.CreateSchema(schema))
        except sqlalchemy.exc.ProgrammingError as err:
            logging.warn(err)
        base = declarative_base(metadata=MetaData(schema=schema))

    define_schema(base)

    if clobber:
        logging.info('Clobbering database tables')
        base.metadata.drop_all(bind=engine)

    logging.info('Creating database tables')
    base.metadata.create_all(bind=engine)

    return base


Instruction:
Given the following Python 3 function, write the documentation
def read_nodes(rows, source_id=1):

    ncbi_keys = ['tax_id', 'parent_id', 'rank', 'embl_code', 'division_id']
    extra_keys = ['source_id', 'is_valid']
    is_valid = True

    ncbi_cols = len(ncbi_keys)

    rank = ncbi_keys.index('rank')
    parent_id = ncbi_keys.index('parent_id')

    # assumes the first row is the root
    row = next(rows)
    row[rank] = 'root'
    # parent must be None for termination of recursive CTE for
    # calculating lineages
    row[parent_id] = None
    rows = itertools.chain([row], rows)

    yield ncbi_keys + extra_keys

    for row in rows:
        # replace whitespace in "rank" with underscore
        row[rank] = '_'.join(row[rank].split())
        # provide default values for source_id and is_valid
        yield row[:ncbi_cols] + [source_id, is_valid]

Response: Yields a generator of rows ready to insert into table nodes.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read_names(rows, source_id=1):

    ncbi_keys = ['tax_id', 'tax_name', 'unique_name', 'name_class']
    extra_keys = ['source_id', 'is_primary', 'is_classified']

    # is_classified applies to species only; we will set this value
    # later
    is_classified = None

    tax_id = ncbi_keys.index('tax_id')
    tax_name = ncbi_keys.index('tax_name')
    unique_name = ncbi_keys.index('unique_name')
    name_class = ncbi_keys.index('name_class')

    yield ncbi_keys + extra_keys

    for tid, grp in itertools.groupby(rows, itemgetter(tax_id)):
        # confirm that each tax_id has exactly one scientific name
        num_primary = 0
        for r in grp:
            is_primary = r[name_class] == 'scientific name'
            # fix primary key uniqueness violation
            if r[unique_name]:
                r[tax_name] = r[unique_name]
            num_primary += is_primary
            yield (r + [source_id, is_primary, is_classified])

        assert num_primary == 1

Response:
Return an iterator of rows ready to insert into table
    names.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def fetch_data(dest_dir='.', clobber=False, url=DATA_URL):

    dest_dir = os.path.abspath(dest_dir)
    try:
        os.mkdir(dest_dir)
    except OSError:
        pass

    fout = os.path.join(dest_dir, os.path.split(url)[-1])

    if os.access(fout, os.F_OK) and not clobber:
        downloaded = False
        logging.info(fout + ' exists; not downloading')
    else:
        downloaded = True
        logging.info('downloading {} to {}'.format(url, fout))
        request.urlretrieve(url, fout)

    return (fout, downloaded)

Response:
Download data from NCBI required to generate local taxonomy
    database.


Instruction:
Implement a function in Python 3 to
return an iterator of unique rows from a zip file.

Response:
def read_archive(archive, fname):
    """Return an iterator of unique rows from a zip archive.

    * archive - path to the zip archive.
    * fname - name of the compressed file within the archive.

    """

    # Note that deduplication here is equivalent to an upsert/ignore,
    # but avoids requirement for a database-specific implementation.

    zfile = zipfile.ZipFile(archive)
    contents = zfile.open(fname, 'r')
    fobj = io.TextIOWrapper(contents)

    seen = set()
    for line in fobj:
        line = line.rstrip('\t|\n')
        if line not in seen:
            yield line.split('\t|\t')
            seen.add(line)


Instruction:
Make a summary of the following Python 3 code
def prepend_schema(self, name):
        return '.'.join([self.schema, name]) if self.schema else name

Response: Prepend schema name to name when a schema is specified


Instruction:
How would you code a function in Python 3 to
load rows into table table.

Response:
def load_table(self, table, rows, colnames=None, limit=None):
        """Load 'rows' into table 'table'. If 'colnames' is not provided, the
        first element of 'rows' must provide column names.

        """

        conn = self.engine.raw_connection()
        cur = conn.cursor()

        colnames = colnames or next(rows)

        cmd = 'INSERT INTO {table} ({colnames}) VALUES ({placeholders})'.format(
            table=self.tables[table],
            colnames=', '.join(colnames),
            placeholders=', '.join([self.placeholder] * len(colnames)))

        cur.executemany(cmd, itertools.islice(rows, limit))
        conn.commit()


Instruction:
Create a Python 3 function for
loading data from the zip archive of the NCBI taxonomy.

Response:
def load_archive(self, archive):
        """Load data from the zip archive of the NCBI taxonomy.

        """

        # source
        self.load_table(
            'source',
            rows=[('ncbi', DATA_URL)],
            colnames=['name', 'description'],
        )

        conn = self.engine.raw_connection()
        cur = conn.cursor()
        cmd = "select id from {source} where name = 'ncbi'".format(**self.tables)
        cur.execute(cmd)
        source_id = cur.fetchone()[0]

        # ranks
        log.info('loading ranks')
        self.load_table(
            'ranks',
            rows=((rank, i) for i, rank in enumerate(RANKS)),
            colnames=['rank', 'height'],
        )

        # nodes
        logging.info('loading nodes')
        nodes_rows = read_nodes(
            read_archive(archive, 'nodes.dmp'), source_id=source_id)
        self.load_table('nodes', rows=nodes_rows)

        # names
        logging.info('loading names')
        names_rows = read_names(
            read_archive(archive, 'names.dmp'), source_id=source_id)
        self.load_table('names', rows=names_rows)

        # merged
        logging.info('loading merged')
        merged_rows = read_merged(read_archive(archive, 'merged.dmp'))
        self.load_table('merged', rows=merged_rows)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def date_to_delorean(year, month, day):
    return Delorean(datetime=dt(year, month, day), timezone='UTC')

Response: Converts a date object to a Delorean instance in UTC


Instruction:
Explain what the following Python 3 code does
def date_to_epoch(year, month, day):
    return int(date_to_delorean(year, month, day).epoch)

Response: Converts a date to epoch in UTC


Instruction:
Explain what the following Python 3 code does
def shift_epoch(delorean, direction, unit, count):
    return int(delorean._shift_date(direction, unit, count).epoch)

Response: Gets the resulting epoch after a time shift of a Delorean object.


Instruction:
Explain what the following Python 3 code does
def generate_epochs(delorean, direction, unit, count):
    for shift in range(count):
        yield int(delorean._shift_date(direction, unit, shift).epoch)

Response: Generates count epochs in UTC from a shifted Delorean instance.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_end_start_epochs(year, month, day, direction, unit, count):
    if year or month or day: # Date is specified
        if not year:
            year = 2017
        if not month:
            month = 1
        if not day:
            day = 1
        initial_delorean = date_to_delorean(year, month, day)
    else: # Date is not specified, get current date
        count += 1 # Get another date because market is still open
        initial_delorean = now_delorean()

    initial_epoch = int(initial_delorean.epoch)
    shifted_epoch = shift_epoch(initial_delorean, direction, unit, count)
    return { 'initial': initial_epoch, 'shifted': shifted_epoch }

Response: Gets epoch from a start date and epoch from a shifted date


Instruction:
Can you tell what is the following Python 3 function doing
def add_child(self, child):
        assert child != self
        child.parent = self
        child.ranks = self.ranks
        child.index = self.index
        assert child.tax_id not in self.index
        self.index[child.tax_id] = child
        self.children.add(child)

Response: Add a child to this node.


Instruction:
Write a Python 3 function that can
remove a child from this node.

Response:
def remove_child(self, child):
        """
        Remove a child from this node.
        """
        assert child in self.children
        self.children.remove(child)
        self.index.pop(child.tax_id)
        if child.parent is self:
            child.parent = None
        if child.index is self.index:
            child.index = None

        # Remove child subtree from index
        for n in child:
            if n is child:
                continue
            self.index.pop(n.tax_id)
            if n.index is self.index:
                n.index = None


Instruction:
Can you implement a function in Python 3 that
drops this node from the taxonomy.

Response:
def drop(self):
        """
        Remove this node from the taxonomy, maintaining child subtrees by
        adding them to the node's parent, and moving sequences at this node
        to the parent.

        Not valid for root node.
        """
        if self.is_root:
            raise ValueError("Cannot drop root node!")

        parent = self.parent
        for child in self.children:
            child.parent = parent
            parent.children.add(child)
        self.children = set()

        parent.sequence_ids.update(self.sequence_ids)
        self.sequence_ids = set()

        parent.remove_child(self)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def prune_unrepresented(self):
        for node in self.depth_first_iter(self_first=False):
            if (not node.children and
                    not node.sequence_ids and
                    node is not self):
                node.parent.remove_child(node)

Response: Remove nodes with sequences or children below this node.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def at_rank(self, rank):
        s = self
        while s:
            if s.rank == rank:
                return s
            s = s.parent
        raise ValueError("No node at rank {0} for {1}".format(
            rank, self.tax_id))

Response: Find the node above this node at the given rank.


Instruction:
Create a Python 3 function for
iterating over nodes below this node optionally yielding children before this node.

Response:
def depth_first_iter(self, self_first=True):
        """
        Iterate over nodes below this node, optionally yielding children before
        self.
        """
        if self_first:
            yield self
        for child in list(self.children):
            for i in child.depth_first_iter(self_first):
                yield i
        if not self_first:
            yield self


Instruction:
How would you implement a function in Python 3 that
gets the node at the end of the path described by tax_ids.

Response:
def path(self, tax_ids):
        """Get the node at the end of the path described by tax_ids."""
        assert tax_ids[0] == self.tax_id
        if len(tax_ids) == 1:
            return self

        n = tax_ids[1]
        try:
            child = next(i for i in self.children if i.tax_id == n)
        except StopIteration:
            raise ValueError(n)

        return child.path(tax_ids[1:])


Instruction:
Write a Python 3 script for
returning all nodes between this node and the root including this one.

Response:
def lineage(self):
        """
        Return all nodes between this node and the root, including this one.
        """
        if not self.parent:
            return [self]
        else:
            L = self.parent.lineage()
            L.append(self)
            return L


Instruction:
Write a Python 3 script to
write a taxtable for this node and all descendants including the lineage leading to this node.

Response:
def write_taxtable(self, out_fp, **kwargs):
        """
        Write a taxtable for this node and all descendants,
        including the lineage leading to this node.
        """
        ranks_represented = frozenset(i.rank for i in self) | \
            frozenset(i.rank for i in self.lineage())
        ranks = [i for i in self.ranks if i in ranks_represented]
        assert len(ranks_represented) == len(ranks)

        def node_record(node):
            parent_id = node.parent.tax_id if node.parent else node.tax_id
            d = {'tax_id': node.tax_id,
                 'tax_name': node.name,
                 'parent_id': parent_id,
                 'rank': node.rank}
            L = {i.rank: i.tax_id for i in node.lineage()}
            d.update(L)
            return d

        header = ['tax_id', 'parent_id', 'rank', 'tax_name'] + ranks
        w = csv.DictWriter(out_fp, header, quoting=csv.QUOTE_NONNUMERIC,
                           lineterminator='\n')
        w.writeheader()
        # All nodes leading to this one
        for i in self.lineage()[:-1]:
            w.writerow(node_record(i))
        w.writerows(node_record(i) for i in self)


Instruction:
Create a Python 3 function to
populate sequence_ids below this node from a seqinfo file object.

Response:
def populate_from_seqinfo(self, seqinfo):
        """Populate sequence_ids below this node from a seqinfo file object."""
        for row in csv.DictReader(seqinfo):
            node = self.index.get(row['tax_id'])
            if node:
                node.sequence_ids.add(row['seqname'])


Instruction:
Here you have a function in Python 3, explain what it does
def collapse(self, remove=False):
        descendants = iter(self)
        # Skip this node
        assert next(descendants) is self
        for descendant in descendants:
            self.sequence_ids.update(descendant.sequence_ids)
            descendant.sequence_ids.clear()

        if remove:
            for node in self.children:
                self.remove_child(node)

Response: Collapse all sequence_ids in the subtree below this node to this node.


Instruction:
Make a summary of the following Python 3 code
def write_seqinfo(self, out_fp, include_name=True):
        header = ['seqname', 'tax_id']

        if include_name:
            header.append('tax_name')

        w = csv.DictWriter(out_fp, header, quoting=csv.QUOTE_NONNUMERIC,
                           lineterminator='\n', extrasaction='ignore')
        w.writeheader()

        rows = ({'seqname': seq_id,
                 'tax_id': node.tax_id,
                 'tax_name': node.name}
                for node in self
                for seq_id in node.sequence_ids)

        w.writerows(rows)

Response: Write a simple seq_info file suitable for use in taxtastic.


Instruction:
Can you tell what is the following Python 3 function doing
def from_taxtable(cls, taxtable_fp):
        r = csv.reader(taxtable_fp)
        headers = next(r)
        rows = (collections.OrderedDict(list(zip(headers, i))) for i in r)

        row = next(rows)
        root = cls(rank=row['rank'], tax_id=row[
                   'tax_id'], name=row['tax_name'])
        path_root = headers.index('root')
        root.ranks = headers[path_root:]
        for row in rows:
            rank, tax_id, name = [
                row[i] for i in ('rank', 'tax_id', 'tax_name')]
            path = [_f for _f in list(row.values())[path_root:] if _f]
            parent = root.path(path[:-1])
            parent.add_child(cls(rank, tax_id, name=name))

        return root

Response: Generate a node from an open handle to a taxtable


Instruction:
Can you generate the documentation for the following Python 3 function
def from_taxdb(cls, con, root=None):
        cursor = con.cursor()
        if root is None:
            cursor.execute(
                "SELECT tax_id, rank FROM nodes WHERE tax_id = parent_id")
        else:
            cursor.execute(
                "SELECT tax_id, rank FROM nodes WHERE tax_id = ?", [root])

        tax_id, rank = cursor.fetchone()
        root = cls(rank=rank, tax_id=tax_id)

        def add_lineage(parent):
            cursor.execute("""SELECT tax_id, rank, tax_name
                    FROM nodes INNER JOIN names USING (tax_id)
                    WHERE parent_id = :1 and tax_id <> :1
                        AND names.is_primary = 1
                    """, [parent.tax_id])
            for tax_id, rank, name in cursor:
                node = cls(rank=rank, tax_id=tax_id, name=name)
                parent.add_child(node)
            for child in parent.children:
                add_lineage(child)

        add_lineage(root)
        return root

Response: Generate a TaxNode from a taxonomy database.


Instruction:
How would you code a function in Python 3 to
parse a host string and return a tuple containing the address and port.

Response:
def parse_host(host):
    """
    Parse *host* in format ``"[hostname:]port"`` and return :class:`tuple`
    ``(address, port)``.

        >>> parse_host('localhost:4444')
        ('localhost', 4444)
        >>> parse_host(':4444')
        ('', 4444)
        >>> parse_host('4444')
        ('', 4444)
        >>> parse_host('2001:db8::1428:57ab:4444')
        ('2001:db8::1428:57ab', 4444)
        >>> parse_host('localhost')
        ValueError: Invalid port number 'localhost'
    """
    parts = host.split(':')
    address = ':'.join(parts[:-1])
    try:
        port = int(parts[-1])
    except ValueError:
        port = None
    if not port or port < 1 or port > 65535:
        raise ValueError("Invalid port number '%s'" % port)
    return address, port


Instruction:
Can you generate the documentation for the following Python 3 function
def get_children(engine, parent_ids, rank='species', schema=None):

    if not parent_ids:
        return []

    nodes = schema + '.nodes' if schema else 'nodes'
    names = schema + '.names' if schema else 'names'

    cmd = ('select tax_id, tax_name, rank '
           'from {} join {} using (tax_id) '
           'where parent_id = :tax_id and is_primary').format(nodes, names)

    species = []
    for parent_id in parent_ids:
        result = engine.execute(sqlalchemy.sql.text(cmd), tax_id=parent_id)
        keys = list(result.keys())
        rows = [dict(list(zip(keys, row))) for row in result.fetchall()]
        for r in rows:
            if r['rank'] == rank and 'sp.' not in r['tax_name']:
                species.append(r)
        others = [r for r in rows if r['rank'] not in (rank, 'no_rank')]
        if others:
            _, s = get_children(engine, [r['tax_id'] for r in others])
            species.extend(s)

    return keys, species

Response: Recursively fetch children of tax_ids in parent_ids until the rank of rank of rank of rank


Instruction:
Can you generate the documentation for the following Python 3 function
def get_config_items(self):
        return (
            ('settings', self.settings),
            ('context_class', self.context_class),
            ('interfaces', self.interfaces),
            ('logging', self.logging),
            ('name', self.name),
            ('init_handler', self.init_handler),
            ('sigusr1_handler', self.sigusr1_handler),
            ('sigusr2_handler', self.sigusr2_handler),
        )

Response: Return a list of items from the current configuration.


Instruction:
Explain what the following Python 3 code does
def context_class(self):
        if 'context_class' not in self._cached_values:
            context_cls_name = getattr(self.settings, 'CONTEXT_CLASS', '')
            if context_cls_name:
                context_class = import_object(context_cls_name)
            else:
                context_class = Context
            self._cached_values['context_class'] = context_class
        return self._cached_values['context_class']

Response: Return the class object that represents the current context.


Instruction:
Create a Python 3 function for
returning a list of all interfaces in the current session.

Response:
def interfaces(self):
        """
        Interfaces as a :class:`list`of the
        :class:`shelter.core.config.Config.Interface` instances.
        """
        if 'interfaces' not in self._cached_values:
            self._cached_values['interfaces'] = []
            for name, interface in six.iteritems(self.settings.INTERFACES):
                listen = interface.get('LISTEN')
                unix_socket = interface.get('UNIX_SOCKET')
                if not listen and not unix_socket:
                    raise ValueError(
                        'Interface MUST listen either on TCP '
                        'or UNIX socket or both')
                host, port = parse_host(listen) if listen else (None, None)
                processes = int(interface.get('PROCESSES', 1))
                urls_obj_name = interface.get('URLS', '')
                if urls_obj_name:
                    urls = import_object(urls_obj_name)
                else:
                    urls = ()
                self._cached_values['interfaces'].append(
                    self.Interface(
                        name, host, port, unix_socket, processes, urls)
                )
        return self._cached_values['interfaces']


Instruction:
Here you have a function in Python 3, explain what it does
def action(args):
    log.info('loading reference package')

    pairs = [p.split('=', 1) for p in args.changes]
    if args.metadata:
        rp = refpkg.Refpkg(args.refpkg, create=False)
        rp.start_transaction()
        for key, value in pairs:
            rp.update_metadata(key, value)
        rp.commit_transaction('Updated metadata: ' +
                              ', '.join(['%s=%s' % (a, b)
                                         for a, b in pairs]))
    else:
        for key, filename in pairs:
            if not(os.path.exists(filename)):
                print("No such file: %s" % filename)
                exit(1)

        rp = refpkg.Refpkg(args.refpkg, create=False)
        rp.start_transaction()
        for key, filename in pairs:
            if key == 'tree_stats':
                with warnings.catch_warnings():
                    warnings.simplefilter(
                        "ignore", refpkg.DerivedFileNotUpdatedWarning)
                    rp.update_file(key, os.path.abspath(filename))
                # Trigger model update
                log.info('Updating phylo_model to match tree_stats')
                rp.update_phylo_model(args.stats_type, filename,
                                      args.frequency_type)
            else:
                rp.update_file(key, os.path.abspath(filename))

        rp.commit_transaction('Updates files: ' +
                              ', '.join(['%s=%s' % (a, b)
                                         for a, b in pairs]))
    return 0

Response: Updates a Refpkg with new files.


Instruction:
Given the following Python 3 function, write the documentation
def import_object(name):
    parts = name.split('.')
    if len(parts) < 2:
        raise ValueError("Invalid name '%s'" % name)
    module_name = ".".join(parts[:-1])
    obj_name = parts[-1]
    module = importlib.import_module(module_name)
    return getattr(module, obj_name)

Response: Imports object from module and returns object from it.


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_arguments(argv):
    parser = argparse.ArgumentParser(description=DESCRIPTION)
    base_parser = argparse.ArgumentParser(add_help=False)

    parser.add_argument('-V', '--version', action='version',
                        version='taxit v' + version,
                        help='Print the version number and exit')

    parser.add_argument('-v', '--verbose',
                        action='count', dest='verbosity', default=1,
                        help='Increase verbosity of screen output (eg, -v is verbose, '
                        '-vv more so)')
    parser.add_argument('-q', '--quiet',
                        action='store_const', dest='verbosity', const=0,
                        help='Suppress output')

    ##########################
    # Setup all sub-commands #
    ##########################

    subparsers = parser.add_subparsers(dest='subparser_name')

    # Begin help sub-command
    parser_help = subparsers.add_parser(
        'help', help='Detailed help for actions using `help <action>`')
    parser_help.add_argument('action', nargs=1)
    # End help sub-command

    actions = {}

    for name, mod in subcommands.itermodules(
            os.path.split(subcommands.__file__)[0]):
        # set up subcommand help text. The first line of the dosctring
        # in the module is displayed as the help text in the
        # script-level help message (`script -h`). The entire
        # docstring is displayed in the help message for the
        # individual subcommand ((`script action -h`)).
        subparser = subparsers.add_parser(
            name,
            prog='taxit {}'.format(name),
            help=mod.__doc__.lstrip().split('\n', 1)[0],
            description=mod.__doc__,
            formatter_class=RawDescriptionHelpFormatter,
            parents=[base_parser])

        mod.build_parser(subparser)
        actions[name] = mod.action

    # Determine we have called ourself (e.g. "help <action>")
    # Set arguments to display help if parameter is set
    #           *or*
    # Set arguments to perform an action with any specified options.
    arguments = parser.parse_args(argv)
    # Determine which action is in play.
    action = arguments.subparser_name

    # Support help <action> by simply having this function call itself and
    # translate the arguments into something that argparse can work with.
    if action == 'help':
        return parse_arguments([str(arguments.action[0]), '-h'])

    return actions[action], arguments

Response: Create the argument parser for the base argument parser.


Instruction:
Here you have a function in Python 3, explain what it does
def execute(self, statements, exc=IntegrityError, rasie_as=ValueError):

        Session = sessionmaker(bind=self.engine)
        session = Session()

        try:
            for statement in statements:
                session.execute(statement)
        except exc as err:
            session.rollback()
            raise rasie_as(str(err))
        else:
            session.commit()
        finally:
            session.close()

Response:
Execute a list of statements in a session and perform a rollback on
        error.


Instruction:
Create a Python 3 function to
return the node id and rank of a tax_id

Response:
def _node(self, tax_id):
        """
        Returns parent_id, rank

        FIXME: expand return rank to include custom 'below' ranks built when
               get_lineage is caled
        """
        s = select([self.nodes.c.parent_id, self.nodes.c.rank],
                   self.nodes.c.tax_id == tax_id)
        res = s.execute()
        output = res.fetchone()
        if not output:
            msg = 'value "{}" not found in nodes.tax_id'.format(tax_id)
            raise ValueError(msg)
        else:
            return output


Instruction:
Create a Python 3 function for
returning the primary taxonomic name associated with tax_id

Response:
def primary_from_id(self, tax_id):
        """
        Returns primary taxonomic name associated with tax_id
        """
        s = select([self.names.c.tax_name],
                   and_(self.names.c.tax_id == tax_id,
                        self.names.c.is_primary))
        res = s.execute()
        output = res.fetchone()

        if not output:
            msg = 'value "{}" not found in names.tax_id'.format(tax_id)
            raise ValueError(msg)
        else:
            return output[0]


Instruction:
Can you create a Python 3 function that
returns tax_id and primary tax_name corresponding to tax_name.

Response:
def primary_from_name(self, tax_name):
        """
        Return tax_id and primary tax_name corresponding to tax_name.
        """
        names = self.names

        s1 = select([names.c.tax_id, names.c.is_primary],
                    names.c.tax_name == tax_name)

        log.debug(str(s1))

        res = s1.execute().fetchone()
        if res:
            tax_id, is_primary = res
        else:
            msg = '"{}" not found in names.tax_names'.format(tax_name)
            raise ValueError(msg)

        if not is_primary:
            s2 = select([names.c.tax_name],
                        and_(names.c.tax_id == tax_id,
                             names.c.is_primary))
            tax_name = s2.execute().fetchone()[0]

        return tax_id, tax_name, bool(is_primary)


Instruction:
Implement a Python 3 function for
returning tax_id into which tax_id has been merged or tax_id of not obsolete.

Response:
def _get_merged(self, tax_id):
        """Returns tax_id into which `tax_id` has been merged or `tax_id` of
        not obsolete.

        """

        cmd = """
        SELECT COALESCE(
        (SELECT new_tax_id FROM {merged}
         WHERE old_tax_id = {x}), {x})
        """.format(x=self.placeholder, merged=self.merged)

        with self.engine.connect() as con:
            result = con.execute(cmd, (tax_id, tax_id))
            return result.fetchone()[0]


Instruction:
Given the following Python 3 function, write the documentation
def _get_lineage(self, tax_id, merge_obsolete=True):

        # Be sure we aren't working with an obsolete tax_id
        if merge_obsolete:
            tax_id = self._get_merged(tax_id)

        # Note: joining with ranks seems like a no-op, but for some
        # reason it results in a faster query using sqlite, as well as
        # an ordering from leaf --> root. Might be a better idea to
        # sort explicitly if this is the expected behavior, but it
        # seems like for the most part, the lineage is converted to a
        # dict and the order is irrelevant.
        cmd = """
        WITH RECURSIVE a AS (
         SELECT tax_id, parent_id, rank
          FROM {nodes}
          WHERE tax_id = {}
        UNION ALL
         SELECT p.tax_id, p.parent_id, p.rank
          FROM a JOIN {nodes} p ON a.parent_id = p.tax_id
        )
        SELECT a.rank, a.tax_id FROM a
        JOIN {ranks} using(rank)
        """.format(self.placeholder, nodes=self.nodes, ranks=self.ranks_table)

        # with some versions of sqlite3, an error is raised when no
        # rows are returned; with others, an empty list is returned.
        try:
            with self.engine.connect() as con:
                result = con.execute(cmd, (tax_id,))
                # reorder so that root is first
                lineage = result.fetchall()[::-1]
        except sqlalchemy.exc.ResourceClosedError:
            lineage = []

        if not lineage:
            raise ValueError('tax id "{}" not found'.format(tax_id))

        return lineage

Response: Return a list of tuples describing the lineage of a specific tax_id.