Instruction:
Can you tell what is the following Python 3 function doing
def _extract_metric_from_map(self, _m, messages, obj_map, obj_help):
        _obj = metrics_pb2.MetricFamily()
        _obj.name = _m
        _obj.type = self.METRIC_TYPES.index(obj_map[_m])
        if _m in obj_help:
            _obj.help = obj_help[_m]
        # trick for histograms
        _newlbl = _m
        if obj_map[_m] == 'histogram':
            _newlbl = '{}_bucket'.format(_m)
        # Loop through the array of metrics ({labels, value}) built earlier
        for _metric in messages[_newlbl]:
            # in the case of quantiles and buckets, they need to be grouped by labels
            if obj_map[_m] in ['summary', 'histogram'] and len(_obj.metric) > 0:
                _label_exists = False
                _metric_minus = {k: v for k, v in list(iteritems(_metric['labels'])) if k not in ['quantile', 'le']}
                _metric_idx = 0
                for mls in _obj.metric:
                    _tmp_lbl = {idx.name: idx.value for idx in mls.label}
                    if _metric_minus == _tmp_lbl:
                        _label_exists = True
                        break
                    _metric_idx = _metric_idx + 1
                if _label_exists:
                    _g = _obj.metric[_metric_idx]
                else:
                    _g = _obj.metric.add()
            else:
                _g = _obj.metric.add()
            if obj_map[_m] == 'counter':
                _g.counter.value = float(_metric['value'])
            elif obj_map[_m] == 'gauge':
                _g.gauge.value = float(_metric['value'])
            elif obj_map[_m] == 'summary':
                if '{}_count'.format(_m) in messages:
                    _g.summary.sample_count = long(self.get_metric_value_by_labels(messages, _metric, _m, 'count'))
                if '{}_sum'.format(_m) in messages:
                    _g.summary.sample_sum = self.get_metric_value_by_labels(messages, _metric, _m, 'sum')
            # TODO: see what can be done with the untyped metrics
            elif obj_map[_m] == 'histogram':
                if '{}_count'.format(_m) in messages:
                    _g.histogram.sample_count = long(self.get_metric_value_by_labels(messages, _metric, _m, 'count'))
                if '{}_sum'.format(_m) in messages:
                    _g.histogram.sample_sum = self.get_metric_value_by_labels(messages, _metric, _m, 'sum')
            # last_metric = len(_obj.metric) - 1
            # if last_metric >= 0:
            for lbl in _metric['labels']:
                # In the string format, the quantiles are in the labels
                if lbl == 'quantile':
                    # _q = _obj.metric[last_metric].summary.quantile.add()
                    _q = _g.summary.quantile.add()
                    _q.quantile = float(_metric['labels'][lbl])
                    _q.value = float(_metric['value'])
                # The upper_bounds are stored as "le" labels on string format
                elif obj_map[_m] == 'histogram' and lbl == 'le':
                    # _q = _obj.metric[last_metric].histogram.bucket.add()
                    _q = _g.histogram.bucket.add()
                    _q.upper_bound = float(_metric['labels'][lbl])
                    _q.cumulative_count = long(float(_metric['value']))
                else:
                    # labels deduplication
                    is_in_labels = False
                    for _existing_lbl in _g.label:
                        if lbl == _existing_lbl.name:
                            is_in_labels = True
                    if not is_in_labels:
                        _l = _g.label.add()
                        _l.name = lbl
                        _l.value = _metric['labels'][lbl]
        return _obj

Response: Extracts the metric from the given object map.


Instruction:
Can you tell what is the following Python 3 function doing
def scrape_metrics(self, endpoint):
        response = self.poll(endpoint)
        try:
            # no dry run if no label joins
            if not self.label_joins:
                self._dry_run = False
            elif not self._watched_labels:
                # build the _watched_labels set
                for val in itervalues(self.label_joins):
                    self._watched_labels.add(val['label_to_match'])

            for metric in self.parse_metric_family(response):
                yield metric

            # Set dry run off
            self._dry_run = False
            # Garbage collect unused mapping and reset active labels
            for metric, mapping in list(iteritems(self._label_mapping)):
                for key in list(mapping):
                    if key not in self._active_label_mapping[metric]:
                        del self._label_mapping[metric][key]
            self._active_label_mapping = {}
        finally:
            response.close()

Response: Scrape the metrics from prometheus and return the metrics as a generator.


Instruction:
How would you implement a function in Python 3 that
processes the metrics from Prometheus and pushes them as gauges

Response:
def process(self, endpoint, **kwargs):
        """
        Polls the data from prometheus and pushes them as gauges
        `endpoint` is the metrics endpoint to use to poll metrics from Prometheus

        Note that if the instance has a 'tags' attribute, it will be pushed
        automatically as additional custom tags and added to the metrics
        """
        instance = kwargs.get('instance')
        if instance:
            kwargs['custom_tags'] = instance.get('tags', [])

        for metric in self.scrape_metrics(endpoint):
            self.process_metric(metric, **kwargs)


Instruction:
Can you tell what is the following Python 3 function doing
def process_metric(self, message, **kwargs):

        # If targeted metric, store labels
        self.store_labels(message)

        if message.name in self.ignore_metrics:
            return  # Ignore the metric

        # Filter metric to see if we can enrich with joined labels
        self.join_labels(message)

        send_histograms_buckets = kwargs.get('send_histograms_buckets', True)
        send_monotonic_counter = kwargs.get('send_monotonic_counter', False)
        custom_tags = kwargs.get('custom_tags')
        ignore_unmapped = kwargs.get('ignore_unmapped', False)

        try:
            if not self._dry_run:
                try:
                    self._submit(
                        self.metrics_mapper[message.name],
                        message,
                        send_histograms_buckets,
                        send_monotonic_counter,
                        custom_tags,
                    )
                except KeyError:
                    if not ignore_unmapped:
                        # call magic method (non-generic check)
                        handler = getattr(self, message.name)  # Lookup will throw AttributeError if not found
                        try:
                            handler(message, **kwargs)
                        except Exception as err:
                            self.log.warning("Error handling metric: {} - error: {}".format(message.name, err))
                    else:
                        # build the wildcard list if first pass
                        if self._metrics_wildcards is None:
                            self._metrics_wildcards = [x for x in self.metrics_mapper.keys() if '*' in x]
                        # try matching wildcard (generic check)
                        for wildcard in self._metrics_wildcards:
                            if fnmatchcase(message.name, wildcard):
                                self._submit(
                                    message.name, message, send_histograms_buckets, send_monotonic_counter, custom_tags
                                )

        except AttributeError as err:
            self.log.debug("Unable to handle metric: {} - error: {}".format(message.name, err))

Response: Process a metric message according to the above flow.


Instruction:
Implement a function in Python 3 to
poll the metrics from the prometheus metrics endpoint provided.

Response:
def poll(self, endpoint, pFormat=PrometheusFormat.PROTOBUF, headers=None):
        """
        Polls the metrics from the prometheus metrics endpoint provided.
        Defaults to the protobuf format, but can use the formats specified by
        the PrometheusFormat class.
        Custom headers can be added to the default headers.

        Returns a valid requests.Response, raise requests.HTTPError if the status code of the requests.Response
        isn't valid - see response.raise_for_status()

        The caller needs to close the requests.Response

        :param endpoint: string url endpoint
        :param pFormat: the preferred format defined in PrometheusFormat
        :param headers: extra headers
        :return: requests.Response
        """
        if headers is None:
            headers = {}
        if 'accept-encoding' not in headers:
            headers['accept-encoding'] = 'gzip'
        if pFormat == PrometheusFormat.PROTOBUF:
            headers['accept'] = (
                'application/vnd.google.protobuf; ' 'proto=io.prometheus.client.MetricFamily; ' 'encoding=delimited'
            )
        headers.update(self.extra_headers)
        cert = None
        if isinstance(self.ssl_cert, string_types):
            cert = self.ssl_cert
            if isinstance(self.ssl_private_key, string_types):
                cert = (self.ssl_cert, self.ssl_private_key)
        verify = True
        if isinstance(self.ssl_ca_cert, string_types):
            verify = self.ssl_ca_cert
        elif self.ssl_ca_cert is False:
            disable_warnings(InsecureRequestWarning)
            verify = False
        try:
            response = requests.get(
                endpoint, headers=headers, stream=False, timeout=self.prometheus_timeout, cert=cert, verify=verify
            )
        except requests.exceptions.SSLError:
            self.log.error("Invalid SSL settings for requesting {} endpoint".format(endpoint))
            raise
        except IOError:
            if self.health_service_check:
                self._submit_service_check(
                    "{}{}".format(self.NAMESPACE, ".prometheus.health"),
                    AgentCheck.CRITICAL,
                    tags=["endpoint:" + endpoint],
                )
            raise
        try:
            response.raise_for_status()
            if self.health_service_check:
                self._submit_service_check(
                    "{}{}".format(self.NAMESPACE, ".prometheus.health"), AgentCheck.OK, tags=["endpoint:" + endpoint]
                )
            return response
        except requests.HTTPError:
            response.close()
            if self.health_service_check:
                self._submit_service_check(
                    "{}{}".format(self.NAMESPACE, ".prometheus.health"),
                    AgentCheck.CRITICAL,
                    tags=["endpoint:" + endpoint],
                )
            raise


Instruction:
How would you code a function in Python 3 to
submit a message to Datadog.

Response:
def _submit(
        self,
        metric_name,
        message,
        send_histograms_buckets=True,
        send_monotonic_counter=False,
        custom_tags=None,
        hostname=None,
    ):
        """
        For each metric in the message, report it as a gauge with all labels as tags
        except if a labels dict is passed, in which case keys are label names we'll extract
        and corresponding values are tag names we'll use (eg: {'node': 'node'}).

        Histograms generate a set of values instead of a unique metric.
        send_histograms_buckets is used to specify if yes or no you want to
            send the buckets as tagged values when dealing with histograms.

        `custom_tags` is an array of 'tag:value' that will be added to the
        metric when sending the gauge to Datadog.
        """
        if message.type < len(self.METRIC_TYPES):
            for metric in message.metric:
                custom_hostname = self._get_hostname(hostname, metric)
                if message.type == 0:
                    val = getattr(metric, self.METRIC_TYPES[message.type]).value
                    if self._is_value_valid(val):
                        if send_monotonic_counter:
                            self._submit_monotonic_count(metric_name, val, metric, custom_tags, custom_hostname)
                        else:
                            self._submit_gauge(metric_name, val, metric, custom_tags, custom_hostname)
                    else:
                        self.log.debug("Metric value is not supported for metric {}.".format(metric_name))
                elif message.type == 4:
                    self._submit_gauges_from_histogram(
                        metric_name, metric, send_histograms_buckets, custom_tags, custom_hostname
                    )
                elif message.type == 2:
                    self._submit_gauges_from_summary(metric_name, metric, custom_tags, custom_hostname)
                else:
                    val = getattr(metric, self.METRIC_TYPES[message.type]).value
                    if self._is_value_valid(val):
                        if message.name in self.rate_metrics:
                            self._submit_rate(metric_name, val, metric, custom_tags, custom_hostname)
                        else:
                            self._submit_gauge(metric_name, val, metric, custom_tags, custom_hostname)
                    else:
                        self.log.debug("Metric value is not supported for metric {}.".format(metric_name))

        else:
            self.log.error("Metric type {} unsupported for metric {}.".format(message.type, message.name))


Instruction:
How would you explain what the following Python 3 function does
def _get_hostname(self, hostname, metric):
        if hostname is None and self.label_to_hostname is not None:
            for label in metric.label:
                if label.name == self.label_to_hostname:
                    return label.value + self.label_to_hostname_suffix

        return hostname

Response: Get the hostname from the label_to_hostname setting.


Instruction:
Can you generate the documentation for the following Python 3 function
def _submit_gauges_from_summary(self, name, metric, custom_tags=None, hostname=None):
        if custom_tags is None:
            custom_tags = []
        # summaries do not have a value attribute
        val = getattr(metric, self.METRIC_TYPES[2]).sample_count
        if self._is_value_valid(val):
            self._submit_gauge("{}.count".format(name), val, metric, custom_tags)
        else:
            self.log.debug("Metric value is not supported for metric {}.count.".format(name))
        val = getattr(metric, self.METRIC_TYPES[2]).sample_sum
        if self._is_value_valid(val):
            self._submit_gauge("{}.sum".format(name), val, metric, custom_tags)
        else:
            self.log.debug("Metric value is not supported for metric {}.sum.".format(name))
        for quantile in getattr(metric, self.METRIC_TYPES[2]).quantile:
            val = quantile.value
            limit = quantile.quantile
            if self._is_value_valid(val):
                self._submit_gauge(
                    "{}.quantile".format(name),
                    val,
                    metric,
                    custom_tags=custom_tags + ["quantile:{}".format(limit)],
                    hostname=hostname,
                )
            else:
                self.log.debug("Metric value is not supported for metric {}.quantile.".format(name))

Response: Extracts metrics from a prometheus summary metric and sends them as gauges.


Instruction:
Explain what the following Python 3 code does
def _submit_gauges_from_histogram(
        self, name, metric, send_histograms_buckets=True, custom_tags=None, hostname=None
    ):
        """
        Extracts metrics from a prometheus histogram and sends them as gauges
        """
        if custom_tags is None:
            custom_tags = []
        # histograms do not have a value attribute
        val = getattr(metric, self.METRIC_TYPES[4]).sample_count
        if self._is_value_valid(val):
            self._submit_gauge("{}.count".format(name), val, metric, custom_tags)
        else:
            self.log.debug("Metric value is not supported for metric {}.count.".format(name))
        val = getattr(metric, self.METRIC_TYPES[4]).sample_sum
        if self._is_value_valid(val):
            self._submit_gauge("{}.sum".format(name), val, metric, custom_tags)
        else:
            self.log.debug("Metric value is not supported for metric {}.sum.".format(name))
        if send_histograms_buckets:
            for bucket in getattr(metric, self.METRIC_TYPES[4]).bucket:
                val = bucket.cumulative_count
                limit = bucket.upper_bound
                if self._is_value_valid(val):
                    self._submit_gauge(
                        "{}.count".format(name),
                        val,
                        metric,
                        custom_tags=custom_tags + ["upper_bound:{}".format(limit)],
                        hostname=hostname,
                    )
                else:
                    self.log.debug("Metric value is not supported for metric {}.count.".format(name))

Response: Extracts metrics from a prometheus histogram and sends them as gauges.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_auth_info(config=None):
    gh_config = (config or {}).get('github', {})
    user = gh_config.get('user') or os.getenv('DD_GITHUB_USER')
    token = gh_config.get('token') or os.getenv('DD_GITHUB_TOKEN')
    if user and token:
        return user, token

Response: Get user and token from GitHub config


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_changelog_types(pr_payload):
    changelog_labels = []
    for name in get_pr_labels(pr_payload):
        if name.startswith(CHANGELOG_LABEL_PREFIX):
            # only add the name, e.g. for `changelog/Added` it's just `Added`
            changelog_labels.append(name.split(CHANGELOG_LABEL_PREFIX)[1])

    return changelog_labels

Response: Fetch the labels from the PR and process them.


Instruction:
Here you have a function in Python 3, explain what it does
def get_pr(pr_num, config=None, repo=DEFAULT_REPO, raw=False):
    response = requests.get(PR_ENDPOINT.format(repo, pr_num), auth=get_auth_info(config))

    if raw:
        return response
    else:
        response.raise_for_status()
        return response.json()

Response: Get the payload for the given PR number.


Instruction:
Make a summary of the following Python 3 code
def parse_pr_numbers(git_log_lines):
    prs = []
    for line in git_log_lines:
        pr_number = parse_pr_number(line)
        if pr_number:
            prs.append(pr_number)
    return prs

Response: Parse PR numbers from commit messages.


Instruction:
Here you have a function in Python 3, explain what it does
def open(self):
        with self._lock:
            if self._thread_will_exit:
                # If the background thread has read self._stopped as True
                # there is a chance that it has not yet exited. The call to
                # join should not block indefinitely because there is no
                # other work done outside the while loop in self._run.
                try:
                    self._thread.join()
                except ReferenceError:
                    # Thread terminated.
                    pass
            self._thread_will_exit = False
            self._stopped = False
        started = False
        try:
            started = self._thread and self._thread.is_alive()
        except ReferenceError:
            # Thread terminated.
            pass

        if not started:
            thread = threading.Thread(target=self._run, name=self._name)
            thread.daemon = True
            self._thread = weakref.proxy(thread)
            _register_executor(self)
            thread.start()

Response: Start. Multiple calls have no effect.


Instruction:
Create a Python 3 function for
configuring the tox environment.

Response:
def tox_configure(config):
    """
    For more info, see: https://tox.readthedocs.io/en/latest/plugins.html
    For an example, see: https://github.com/tox-dev/tox-travis
    """
    sections = config._cfg.sections

    # Cache these
    make_envconfig = None
    reader = None

    # Default to false so:
    # 1. we don't affect other projects using tox
    # 2. check migrations can happen gradually
    if str(sections.get('testenv', {}).get(STYLE_FLAG, 'false')).lower() == 'true':
        # Disable flake8 since we already include that
        config.envlist[:] = [env for env in config.envlist if not env.endswith('flake8')]

        make_envconfig = get_make_envconfig(make_envconfig)
        reader = get_reader(reader, config)

        add_style_checker(config, sections, make_envconfig, reader)
        add_style_formatter(config, sections, make_envconfig, reader)


Instruction:
Make a summary of the following Python 3 code
def _is_collect_cx_state_runnable(self, proc_location):
        if self._collect_cx_state is False:
            return False

        if proc_location != "/proc":
            self.warning("Cannot collect connection state: currently with a custom /proc path: %s" % proc_location)
            return False

        return True

Response: Determine if collect_connection_state is set and can effectively run.


Instruction:
How would you explain what the following Python 3 function does
def _check_linux(self, instance):
        proc_location = self.agentConfig.get('procfs_path', '/proc').rstrip('/')
        custom_tags = instance.get('tags', [])

        if Platform.is_containerized() and proc_location != "/proc":
            proc_location = "%s/1" % proc_location

        if self._is_collect_cx_state_runnable(proc_location):
            try:
                self.log.debug("Using `ss` to collect connection state")
                # Try using `ss` for increased performance over `netstat`
                for ip_version in ['4', '6']:
                    for protocol in ['tcp', 'udp']:
                        # Call `ss` for each IP version because there's no built-in way of distinguishing
                        # between the IP versions in the output
                        # Also calls `ss` for each protocol, because on some systems (e.g. Ubuntu 14.04), there is a
                        # bug that print `tcp` even if it's `udp`
                        output, _, _ = get_subprocess_output(
                            ["ss", "-n", "-{0}".format(protocol[0]), "-a", "-{0}".format(ip_version)], self.log
                        )
                        lines = output.splitlines()

                        # State      Recv-Q Send-Q     Local Address:Port       Peer Address:Port
                        # UNCONN     0      0              127.0.0.1:8125                  *:*
                        # ESTAB      0      0              127.0.0.1:37036         127.0.0.1:8125
                        # UNCONN     0      0        fe80::a00:27ff:fe1c:3c4:123          :::*
                        # TIME-WAIT  0      0          90.56.111.177:56867        46.105.75.4:143
                        # LISTEN     0      0       ::ffff:127.0.0.1:33217  ::ffff:127.0.0.1:7199
                        # ESTAB      0      0       ::ffff:127.0.0.1:58975  ::ffff:127.0.0.1:2181

                        metrics = self._parse_linux_cx_state(
                            lines[1:], self.tcp_states['ss'], 0, protocol=protocol, ip_version=ip_version
                        )
                        # Only send the metrics which match the loop iteration's ip version
                        for stat, metric in iteritems(self.cx_state_gauge):
                            if stat[0].endswith(ip_version) and stat[0].startswith(protocol):
                                self.gauge(metric, metrics.get(metric), tags=custom_tags)

            except OSError:
                self.log.info("`ss` not found: using `netstat` as a fallback")
                output, _, _ = get_subprocess_output(["netstat", "-n", "-u", "-t", "-a"], self.log)
                lines = output.splitlines()
                # Active Internet connections (w/o servers)
                # Proto Recv-Q Send-Q Local Address           Foreign Address         State
                # tcp        0      0 46.105.75.4:80          79.220.227.193:2032     SYN_RECV
                # tcp        0      0 46.105.75.4:143         90.56.111.177:56867     ESTABLISHED
                # tcp        0      0 46.105.75.4:50468       107.20.207.175:443      TIME_WAIT
                # tcp6       0      0 46.105.75.4:80          93.15.237.188:58038     FIN_WAIT2
                # tcp6       0      0 46.105.75.4:80          79.220.227.193:2029     ESTABLISHED
                # udp        0      0 0.0.0.0:123             0.0.0.0:*
                # udp6       0      0 :::41458                :::*

                metrics = self._parse_linux_cx_state(lines[2:], self.tcp_states['netstat'], 5)
                for metric, value in iteritems(metrics):
                    self.gauge(metric, value, tags=custom_tags)
            except SubprocessOutputEmptyError:
                self.log.exception("Error collecting connection stats.")

        proc_dev_path = "{}/net/dev".format(proc_location)
        with open(proc_dev_path, 'r') as proc:
            lines = proc.readlines()
        # Inter-|   Receive                                                 |  Transmit
        #  face |bytes     packets errs drop fifo frame compressed multicast|bytes       packets errs drop fifo colls carrier compressed # noqa: E501
        #     lo:45890956   112797   0    0    0     0          0         0    45890956   112797    0    0    0     0       0          0 # noqa: E501
        #   eth0:631947052 1042233   0   19    0   184          0      1206  1208625538  1320529    0    0    0     0       0          0 # noqa: E501
        #   eth1:       0        0   0    0    0     0          0         0           0        0    0    0    0     0       0          0 # noqa: E501
        for l in lines[2:]:
            cols = l.split(':', 1)
            x = cols[1].split()
            # Filter inactive interfaces
            if self._parse_value(x[0]) or self._parse_value(x[8]):
                iface = cols[0].strip()
                metrics = {
                    'bytes_rcvd': self._parse_value(x[0]),
                    'bytes_sent': self._parse_value(x[8]),
                    'packets_in.count': self._parse_value(x[1]),
                    'packets_in.error': self._parse_value(x[2]) + self._parse_value(x[3]),
                    'packets_out.count': self._parse_value(x[9]),
                    'packets_out.error': self._parse_value(x[10]) + self._parse_value(x[11]),
                }
                self._submit_devicemetrics(iface, metrics, custom_tags)

        netstat_data = {}
        for f in ['netstat', 'snmp']:
            proc_data_path = "{}/net/{}".format(proc_location, f)
            try:
                with open(proc_data_path, 'r') as netstat:
                    while True:
                        n_header = netstat.readline()
                        if not n_header:
                            break  # No more? Abort!
                        n_data = netstat.readline()

                        h_parts = n_header.strip().split(' ')
                        h_values = n_data.strip().split(' ')
                        ns_category = h_parts[0][:-1]
                        netstat_data[ns_category] = {}
                        # Turn the data into a dictionary
                        for idx, hpart in enumerate(h_parts[1:]):
                            netstat_data[ns_category][hpart] = h_values[idx + 1]
            except IOError:
                # On Openshift, /proc/net/snmp is only readable by root
                self.log.debug("Unable to read %s.", proc_data_path)

        nstat_metrics_names = {
            'Tcp': {
                'RetransSegs': 'system.net.tcp.retrans_segs',
                'InSegs': 'system.net.tcp.in_segs',
                'OutSegs': 'system.net.tcp.out_segs',
            },
            'TcpExt': {
                'ListenOverflows': 'system.net.tcp.listen_overflows',
                'ListenDrops': 'system.net.tcp.listen_drops',
                'TCPBacklogDrop': 'system.net.tcp.backlog_drops',
                'TCPRetransFail': 'system.net.tcp.failed_retransmits',
            },
            'Udp': {
                'InDatagrams': 'system.net.udp.in_datagrams',
                'NoPorts': 'system.net.udp.no_ports',
                'InErrors': 'system.net.udp.in_errors',
                'OutDatagrams': 'system.net.udp.out_datagrams',
                'RcvbufErrors': 'system.net.udp.rcv_buf_errors',
                'SndbufErrors': 'system.net.udp.snd_buf_errors',
                'InCsumErrors': 'system.net.udp.in_csum_errors',
            },
        }

        # Skip the first line, as it's junk
        for k in nstat_metrics_names:
            for met in nstat_metrics_names[k]:
                if met in netstat_data.get(k, {}):
                    self._submit_netmetric(
                        nstat_metrics_names[k][met], self._parse_value(netstat_data[k][met]), tags=custom_tags
                    )

        # Get the conntrack -S information
        conntrack_path = instance.get('conntrack_path')
        if conntrack_path is not None:
            self._add_conntrack_stats_metrics(conntrack_path, custom_tags)

        # Get the rest of the metric by reading the files. Metrics available since kernel 3.6
        conntrack_files_location = os.path.join(proc_location, 'sys', 'net', 'netfilter')
        # By default, only max and count are reported. However if the blacklist is set,
        # the whitelist is loosing its default value
        blacklisted_files = instance.get('blacklist_conntrack_metrics')
        whitelisted_files = instance.get('whitelist_conntrack_metrics')
        if blacklisted_files is None and whitelisted_files is None:
            whitelisted_files = ['max', 'count']

        available_files = []

        # Get the metrics to read
        try:
            for metric_file in os.listdir(conntrack_files_location):
                if (
                    os.path.isfile(os.path.join(conntrack_files_location, metric_file))
                    and 'nf_conntrack_' in metric_file
                ):
                    available_files.append(metric_file[len('nf_conntrack_') :])
        except Exception as e:
            self.log.debug("Unable to list the files in {}. {}".format(conntrack_files_location, e))

        filtered_available_files = pattern_filter(
            available_files, whitelist=whitelisted_files, blacklist=blacklisted_files
        )

        for metric_name in filtered_available_files:
            metric_file_location = os.path.join(conntrack_files_location, 'nf_conntrack_{}'.format(metric_name))
            try:
                with open(metric_file_location, 'r') as conntrack_file:
                    # Checking it's an integer
                    try:
                        value = int(conntrack_file.read().rstrip())
                        self.gauge('system.net.conntrack.{}'.format(metric_name), value, tags=custom_tags)
                    except ValueError:
                        self.log.debug("{} is not an integer".format(metric_name))
            except IOError as e:
                self.log.debug("Unable to read {}, skipping {}.".format(metric_file_location, e))

Response: Private function to check if Linux is running.


Instruction:
Write a Python 3 script for
parsing the output of conntrack - S and add the parsed metrics to the self. metrics

Response:
def _add_conntrack_stats_metrics(self, conntrack_path, tags):
        """
        Parse the output of conntrack -S
        Add the parsed metrics
        """
        try:
            output, _, _ = get_subprocess_output(["sudo", conntrack_path, "-S"], self.log)
            # conntrack -S sample:
            # cpu=0 found=27644 invalid=19060 ignore=485633411 insert=0 insert_failed=1 \
            #       drop=1 early_drop=0 error=0 search_restart=39936711
            # cpu=1 found=21960 invalid=17288 ignore=475938848 insert=0 insert_failed=1 \
            #       drop=1 early_drop=0 error=0 search_restart=36983181

            lines = output.splitlines()

            for line in lines:
                cols = line.split()
                cpu_num = cols[0].split('=')[-1]
                cpu_tag = ['cpu:{}'.format(cpu_num)]
                cols = cols[1:]

                for cell in cols:
                    metric, value = cell.split('=')
                    self.monotonic_count('system.net.conntrack.{}'.format(metric), int(value), tags=tags + cpu_tag)
        except SubprocessOutputEmptyError:
            self.log.debug("Couldn't use {} to get conntrack stats".format(conntrack_path))


Instruction:
Here you have a function in Python 3, explain what it does
def _parse_linux_cx_state(self, lines, tcp_states, state_col, protocol=None, ip_version=None):
        metrics = {}
        for _, val in iteritems(self.cx_state_gauge):
            metrics[val] = 0
        for l in lines:
            cols = l.split()
            if cols[0].startswith('tcp') or protocol == 'tcp':
                proto = "tcp{0}".format(ip_version) if ip_version else ("tcp4", "tcp6")[cols[0] == "tcp6"]
                if cols[state_col] in tcp_states:
                    metric = self.cx_state_gauge[proto, tcp_states[cols[state_col]]]
                    metrics[metric] += 1
            elif cols[0].startswith('udp') or protocol == 'udp':
                proto = "udp{0}".format(ip_version) if ip_version else ("udp4", "udp6")[cols[0] == "udp6"]
                metric = self.cx_state_gauge[proto, 'connections']
                metrics[metric] += 1

        return metrics

Response: Parse the output of the Linux CX state command.


Instruction:
Can you implement a function in Python 3 that
parses the Solaris network stat output.

Response:
def _parse_solaris_netstat(self, netstat_output):
        """
        Return a mapping of network metrics by interface. For example:
            { interface:
                {'bytes_sent': 0,
                  'bytes_rcvd': 0,
                  'bytes_rcvd': 0,
                  ...
                }
            }
        """
        # Here's an example of the netstat output:
        #
        # link:0:net0:brdcstrcv   527336
        # link:0:net0:brdcstxmt   1595
        # link:0:net0:class       net
        # link:0:net0:collisions  0
        # link:0:net0:crtime      16359935.2637943
        # link:0:net0:ierrors     0
        # link:0:net0:ifspeed     10000000000
        # link:0:net0:ipackets    682834
        # link:0:net0:ipackets64  682834
        # link:0:net0:link_duplex 0
        # link:0:net0:link_state  1
        # link:0:net0:multircv    0
        # link:0:net0:multixmt    1595
        # link:0:net0:norcvbuf    0
        # link:0:net0:noxmtbuf    0
        # link:0:net0:obytes      12820668
        # link:0:net0:obytes64    12820668
        # link:0:net0:oerrors     0
        # link:0:net0:opackets    105445
        # link:0:net0:opackets64  105445
        # link:0:net0:rbytes      113983614
        # link:0:net0:rbytes64    113983614
        # link:0:net0:snaptime    16834735.1607669
        # link:0:net0:unknowns    0
        # link:0:net0:zonename    53aa9b7e-48ba-4152-a52b-a6368c3d9e7c
        # link:0:net1:brdcstrcv   4947620
        # link:0:net1:brdcstxmt   1594
        # link:0:net1:class       net
        # link:0:net1:collisions  0
        # link:0:net1:crtime      16359935.2839167
        # link:0:net1:ierrors     0
        # link:0:net1:ifspeed     10000000000
        # link:0:net1:ipackets    4947620
        # link:0:net1:ipackets64  4947620
        # link:0:net1:link_duplex 0
        # link:0:net1:link_state  1
        # link:0:net1:multircv    0
        # link:0:net1:multixmt    1594
        # link:0:net1:norcvbuf    0
        # link:0:net1:noxmtbuf    0
        # link:0:net1:obytes      73324
        # link:0:net1:obytes64    73324
        # link:0:net1:oerrors     0
        # link:0:net1:opackets    1594
        # link:0:net1:opackets64  1594
        # link:0:net1:rbytes      304384894
        # link:0:net1:rbytes64    304384894
        # link:0:net1:snaptime    16834735.1613302
        # link:0:net1:unknowns    0
        # link:0:net1:zonename    53aa9b7e-48ba-4152-a52b-a6368c3d9e7c

        # A mapping of solaris names -> datadog names
        metric_by_solaris_name = {
            'rbytes64': 'bytes_rcvd',
            'obytes64': 'bytes_sent',
            'ipackets64': 'packets_in.count',
            'ierrors': 'packets_in.error',
            'opackets64': 'packets_out.count',
            'oerrors': 'packets_out.error',
        }

        lines = [l for l in netstat_output.splitlines() if len(l) > 0]

        metrics_by_interface = {}

        for l in lines:
            # Parse the metric & interface.
            cols = l.split()
            link, n, iface, name = cols[0].split(":")
            assert link == "link"

            # Get the datadog metric name.
            ddname = metric_by_solaris_name.get(name, None)
            if ddname is None:
                continue

            # Add it to this interface's list of metrics.
            metrics = metrics_by_interface.get(iface, {})
            metrics[ddname] = self._parse_value(cols[1])
            metrics_by_interface[iface] = metrics

        return metrics_by_interface


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _check_psutil(self, instance):
        custom_tags = instance.get('tags', [])
        if self._collect_cx_state:
            self._cx_state_psutil(tags=custom_tags)

        self._cx_counters_psutil(tags=custom_tags)

Response: Gather metrics about connections states and interfaces counters using psutil facilities.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _cx_state_psutil(self, tags=None):
        metrics = defaultdict(int)
        tags = [] if tags is None else tags
        for conn in psutil.net_connections():
            protocol = self._parse_protocol_psutil(conn)
            status = self.tcp_states['psutil'].get(conn.status)
            metric = self.cx_state_gauge.get((protocol, status))
            if metric is None:
                self.log.warning('Metric not found for: %s,%s', protocol, status)
            else:
                metrics[metric] += 1

        for metric, value in iteritems(metrics):
            self.gauge(metric, value, tags=tags)

Response: Collect metrics about connections state using psutil


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _cx_counters_psutil(self, tags=None):
        tags = [] if tags is None else tags
        for iface, counters in iteritems(psutil.net_io_counters(pernic=True)):
            metrics = {
                'bytes_rcvd': counters.bytes_recv,
                'bytes_sent': counters.bytes_sent,
                'packets_in.count': counters.packets_recv,
                'packets_in.error': counters.errin,
                'packets_out.count': counters.packets_sent,
                'packets_out.error': counters.errout,
            }
            self._submit_devicemetrics(iface, metrics, tags)

Response: Collect metrics about interfaces counters using psutil.


Instruction:
Implement a Python 3 function for
returning a string describing the protocol for the given connection

Response:
def _parse_protocol_psutil(self, conn):
        """
        Returns a string describing the protocol for the given connection
        in the form `tcp4`, 'udp4` as in `self.cx_state_gauge`
        """
        protocol = self.PSUTIL_TYPE_MAPPING.get(conn.type, '')
        family = self.PSUTIL_FAMILY_MAPPING.get(conn.family, '')
        return '{}{}'.format(protocol, family)


Instruction:
How would you code a function in Python 3 to
process the work unit or wait for sentinel to exit

Response:
def run(self):
        """Process the work unit, or wait for sentinel to exit"""
        while True:
            self.running = True
            workunit = self._workq.get()
            if is_sentinel(workunit):
                # Got sentinel
                break

            # Run the job / sequence
            workunit.process()
        self.running = False


Instruction:
Make a summary of the following Python 3 code
def imap(self, func, iterable, chunksize=1):
        collector = OrderedResultCollector(as_iterator=True)
        self._create_sequences(func, iterable, chunksize, collector)
        return iter(collector)

Response: A generator function that returns the result of the function func over the given iterable.


Instruction:
How would you explain what the following Python 3 function does
def imap_unordered(self, func, iterable, chunksize=1):
        collector = UnorderedResultCollector()
        self._create_sequences(func, iterable, chunksize, collector)
        return iter(collector)

Response:
The same as imap except that the order of the results
        is arbitrary.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def apply_async(self, func, args=(), kwds=dict(), callback=None):
        assert not self._closed  # No lock here. We assume it's atomic...
        apply_result = ApplyResult(callback=callback)
        job = Job(func, args, kwds, apply_result)
        self._workq.put(job)
        return apply_result

Response: A variant of the apply method which returns an ApplyResult object.


Instruction:
Make a summary of the following Python 3 code
def map_async(self, func, iterable, chunksize=None, callback=None):
        apply_result = ApplyResult(callback=callback)
        collector = OrderedResultCollector(apply_result, as_iterator=False)
        self._create_sequences(func, iterable, chunksize, collector)
        return apply_result

Response: A variant of the map method which returns a ApplyResult object.


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

        # Clearing the job queue
        try:
            while 1:
                self._workq.get_nowait()
        except queue.Empty:
            pass

        # Send one sentinel for each worker thread: each thread will die
        # eventually, leaving the next sentinel for the next thread
        for _ in self._workers:
            self._workq.put(SENTINEL)

Response: Stops the worker processes immediately without completing any outstanding work.


Instruction:
Here you have a function in Python 3, explain what it does
def _create_sequences(self, func, iterable, chunksize, collector=None):
        assert not self._closed  # No lock here. We assume it's atomic...
        sequences = []
        results = []
        it_ = iter(iterable)
        exit_loop = False
        while not exit_loop:
            seq = []
            for _ in range(chunksize or 1):
                try:
                    arg = next(it_)
                except StopIteration:
                    exit_loop = True
                    break
                apply_result = ApplyResult(collector)
                job = Job(func, (arg,), {}, apply_result)
                seq.append(job)
                results.append(apply_result)
            sequences.append(JobSequence(seq))

        for seq in sequences:
            self._workq.put(seq)

        return sequences

Response: Create the work unit objects to process and push them onto the work queue.


Instruction:
Create a Python 3 function to
process the result of the function.

Response:
def process(self):
        """
        Call the function with the args/kwds and tell the ApplyResult
        that its result is ready. Correctly handles the exceptions
        happening during the execution of the function
        """
        try:
            result = self._func(*self._args, **self._kwds)
        except:
            self._result._set_exception()
        else:
            self._result._set_value(result)


Instruction:
How would you implement a function in Python 3 that
returns the result when it arrives within timeout seconds.

Response:
def get(self, timeout=None):
        """
        Returns the result when it arrives. If timeout is not None and
        the result does not arrive within timeout seconds then
        TimeoutError is raised. If the remote call raised an exception
        then that exception will be reraised by get().
        """
        if not self.wait(timeout):
            raise TimeoutError("Result not available within %fs" % timeout)
        if self._success:
            return self._data
        raise self._data[0]


Instruction:
Can you generate a brief explanation for the following Python 3 code
def wait(self, timeout=None):
        self._event.wait(timeout)
        return self._event.isSet()

Response: Waits until the result is available or until timeout is reached.


Instruction:
Can you generate the documentation for the following Python 3 function
def _set_value(self, value):
        assert not self.ready()
        self._data = value
        self._success = True
        self._event.set()
        if self._collector is not None:
            self._collector.notify_ready(self)
        if self._callback is not None:
            try:
                self._callback(value)
            except:
                traceback.print_exc()

Response: Set the value of the result of this object.


Instruction:
Make a summary of the following Python 3 code
def _set_exception(self):
        assert not self.ready()
        self._data = sys.exc_info()
        self._success = False
        self._event.set()
        if self._collector is not None:
            self._collector.notify_ready(self)

Response: Sets the exception of the object to be raised by the job.


Instruction:
Implement a function in Python 3 to
return the values of the resultset at the given index

Response:
def _get_result(self, idx, timeout=None):
        """Called by the CollectorIterator object to retrieve the
        result's values one after another, in the order the results have
        become available.
        \param idx The index of the result we want, wrt collector's order
        \param timeout integer telling how long to wait (in seconds)
        for the result at index idx to be available, or None (wait
        forever)
        """
        self._cond.acquire()
        try:
            if idx >= self._expected:
                raise IndexError
            elif idx < len(self._collection):
                return self._collection[idx]
            elif idx != len(self._collection):
                # Violation of the sequence protocol
                raise IndexError()
            else:
                self._cond.wait(timeout=timeout)
                try:
                    return self._collection[idx]
                except IndexError:
                    # Still not added !
                    raise TimeoutError("Timeout while waiting for results")
        finally:
            self._cond.release()


Instruction:
Given the following Python 3 function, write the documentation
def notify_ready(self, apply_result):
        first_item = False
        self._cond.acquire()
        try:
            self._collection.append(apply_result)
            first_item = len(self._collection) == 1

            self._cond.notifyAll()
        finally:
            self._cond.release()

        if first_item and self._to_notify is not None:
            self._to_notify._set_value(iter(self))

Response:
Called by the ApplyResult object telling us that the job s result
        is ready.


Instruction:
Write a Python 3 script to
return the values one after another

Response:
def _get_result(self, idx, timeout=None):
        """Called by the CollectorIterator object to retrieve the
        result's values one after another (order defined by the
        implementation)
        \param idx The index of the result we want, wrt collector's order
        \param timeout integer telling how long to wait (in seconds)
        for the result at index idx to be available, or None (wait
        forever)
        """
        res = self._results[idx]
        res.wait(timeout)
        return res


Instruction:
Here you have a function in Python 3, explain what it does
def notify_ready(self, apply_result):
        got_first = False
        got_last = False
        self._lock.acquire()
        try:
            assert self._remaining > 0
            got_first = len(self._results) == self._remaining
            self._remaining -= 1
            got_last = self._remaining == 0
        finally:
            self._lock.release()

        if self._to_notify is not None:
            if self._as_iterator and got_first:
                self._to_notify._set_value(iter(self))
            elif not self._as_iterator and got_last:
                try:
                    lst = [r.get(0) for r in self._results]
                except:
                    self._to_notify._set_exception()
                else:
                    self._to_notify._set_value(lst)

Response:
Called by the ApplyResult object telling us that the job s result
        is ready.


Instruction:
Create a Python 3 function for
executing a command over a socket.

Response:
def command(sock, dbname, spec, slave_ok, is_mongos,
            read_preference, codec_options, check=True,
            allowable_errors=None, address=None,
            check_keys=False, listeners=None, max_bson_size=None,
            read_concern=DEFAULT_READ_CONCERN,
            parse_write_concern_error=False,
            collation=None):
    """Execute a command over the socket, or raise socket.error.

    :Parameters:
      - `sock`: a raw socket instance
      - `dbname`: name of the database on which to run the command
      - `spec`: a command document as a dict, SON, or mapping object
      - `slave_ok`: whether to set the SlaveOkay wire protocol bit
      - `is_mongos`: are we connected to a mongos?
      - `read_preference`: a read preference
      - `codec_options`: a CodecOptions instance
      - `check`: raise OperationFailure if there are errors
      - `allowable_errors`: errors to ignore if `check` is True
      - `address`: the (host, port) of `sock`
      - `check_keys`: if True, check `spec` for invalid keys
      - `listeners`: An instance of :class:`~pymongo.monitoring.EventListeners`
      - `max_bson_size`: The maximum encoded bson size for this server
      - `read_concern`: The read concern for this command.
      - `parse_write_concern_error`: Whether to parse the ``writeConcernError``
        field in the command response.
      - `collation`: The collation for this command.

    """
    name = next(iter(spec))
    ns = dbname + '.$cmd'
    flags = 4 if slave_ok else 0
    # Publish the original command document.
    orig = spec
    if is_mongos:
        spec = message._maybe_add_read_preference(spec, read_preference)
    if read_concern.level:
        spec['readConcern'] = read_concern.document
    if collation is not None:
        spec['collation'] = collation

    publish = listeners is not None and listeners.enabled_for_commands
    if publish:
        start = datetime.datetime.now()

    request_id, msg, size = message.query(flags, ns, 0, -1, spec,
                                          None, codec_options, check_keys)

    if (max_bson_size is not None
            and size > max_bson_size + message._COMMAND_OVERHEAD):
        message._raise_document_too_large(
            name, size, max_bson_size + message._COMMAND_OVERHEAD)

    if publish:
        encoding_duration = datetime.datetime.now() - start
        listeners.publish_command_start(orig, dbname, request_id, address)
        start = datetime.datetime.now()

    try:
        sock.sendall(msg)
        response = receive_message(sock, 1, request_id)
        unpacked = helpers._unpack_response(
            response, codec_options=codec_options)

        response_doc = unpacked['data'][0]
        if check:
            helpers._check_command_response(
                response_doc, None, allowable_errors,
                parse_write_concern_error=parse_write_concern_error)
    except Exception as exc:
        if publish:
            duration = (datetime.datetime.now() - start) + encoding_duration
            if isinstance(exc, (NotMasterError, OperationFailure)):
                failure = exc.details
            else:
                failure = message._convert_exception(exc)
            listeners.publish_command_failure(
                duration, failure, name, request_id, address)
        raise
    if publish:
        duration = (datetime.datetime.now() - start) + encoding_duration
        listeners.publish_command_success(
            duration, response_doc, name, request_id, address)
    return response_doc


Instruction:
How would you implement a function in Python 3 that
receives a BSON message from the socket.

Response:
def receive_message(
        sock, operation, request_id, max_message_size=MAX_MESSAGE_SIZE):
    """Receive a raw BSON message or raise socket.error."""
    header = _receive_data_on_socket(sock, 16)
    length = _UNPACK_INT(header[:4])[0]

    actual_op = _UNPACK_INT(header[12:])[0]
    if operation != actual_op:
        raise ProtocolError("Got opcode %r but expected "
                            "%r" % (actual_op, operation))
    # No request_id for exhaust cursor "getMore".
    if request_id is not None:
        response_id = _UNPACK_INT(header[8:12])[0]
        if request_id != response_id:
            raise ProtocolError("Got response id %r but expected "
                                "%r" % (response_id, request_id))
    if length <= 16:
        raise ProtocolError("Message length (%r) not longer than standard "
                            "message header size (16)" % (length,))
    if length > max_message_size:
        raise ProtocolError("Message length (%r) is larger than server max "
                            "message size (%r)" % (length, max_message_size))

    return _receive_data_on_socket(sock, length - 16)


Instruction:
How would you code a function in Python 3 to
create a connection to the server and return the socket object.

Response:
def create_connection(self):
        """See: https://github.com/python/cpython/blob/40ee9a3640d702bce127e9877c82a99ce817f0d1/Lib/socket.py#L691"""
        err = None
        try:
            for res in socket.getaddrinfo(self._server, self._port, 0, self._sock_type):
                af, socktype, proto, canonname, sa = res
                sock = None
                try:
                    sock = socket.socket(af, socktype, proto)
                    sock.settimeout(self._timeout)
                    sock.connect(sa)
                    # Break explicitly a reference cycle
                    err = None
                    return sock

                except socket.error as _:
                    err = _
                    if sock is not None:
                        sock.close()

            if err is not None:
                raise err
            else:
                raise socket.error('No valid addresses found, try checking your IPv6 connectivity')
        except socket.gaierror as e:
            err_code, message = e.args
            if err_code == socket.EAI_NODATA or err_code == socket.EAI_NONAME:
                raise socket.error('Unable to resolve host, check your DNS: {}'.format(message))

            raise


Instruction:
Here you have a function in Python 3, explain what it does
def dumps(obj, *args, **kwargs):
    json_options = kwargs.pop("json_options", DEFAULT_JSON_OPTIONS)
    return json.dumps(_json_convert(obj, json_options), *args, **kwargs)

Response: Wrapper function that wraps json. dumps.


Instruction:
Can you generate the documentation for the following Python 3 function
def loads(s, *args, **kwargs):
    json_options = kwargs.pop("json_options", DEFAULT_JSON_OPTIONS)
    if _HAS_OBJECT_PAIRS_HOOK:
        kwargs["object_pairs_hook"] = lambda pairs: object_pairs_hook(
            pairs, json_options)
    else:
        kwargs["object_hook"] = lambda obj: object_hook(obj, json_options)
    return json.loads(s, *args, **kwargs)

Response: A wrapper function that wraps json. loads.


Instruction:
Create a Python 3 function for
decoding a JSON datetime to python datetime. datetime.

Response:
def _parse_canonical_datetime(doc, json_options):
    """Decode a JSON datetime to python datetime.datetime."""
    dtm = doc["$date"]
    if len(doc) != 1:
        raise TypeError('Bad $date, extra field(s): %s' % (doc,))
    # mongoexport 2.6 and newer
    if isinstance(dtm, string_type):
        # Parse offset
        if dtm[-1] == 'Z':
            dt = dtm[:-1]
            offset = 'Z'
        elif dtm[-3] == ':':
            # (+|-)HH:MM
            dt = dtm[:-6]
            offset = dtm[-6:]
        elif dtm[-5] in ('+', '-'):
            # (+|-)HHMM
            dt = dtm[:-5]
            offset = dtm[-5:]
        elif dtm[-3] in ('+', '-'):
            # (+|-)HH
            dt = dtm[:-3]
            offset = dtm[-3:]
        else:
            dt = dtm
            offset = ''

        # Parse the optional factional seconds portion.
        dot_index = dt.rfind('.')
        microsecond = 0
        if dot_index != -1:
            microsecond = int(float(dt[dot_index:]) * 1000000)
            dt = dt[:dot_index]

        aware = datetime.datetime.strptime(
            dt, "%Y-%m-%dT%H:%M:%S").replace(microsecond=microsecond,
                                             tzinfo=utc)

        if offset and offset != 'Z':
            if len(offset) == 6:
                hours, minutes = offset[1:].split(':')
                secs = (int(hours) * 3600 + int(minutes) * 60)
            elif len(offset) == 5:
                secs = (int(offset[1:3]) * 3600 + int(offset[3:]) * 60)
            elif len(offset) == 3:
                secs = int(offset[1:3]) * 3600
            if offset[0] == "-":
                secs *= -1
            aware = aware - datetime.timedelta(seconds=secs)

        if json_options.tz_aware:
            if json_options.tzinfo:
                aware = aware.astimezone(json_options.tzinfo)
            return aware
        else:
            return aware.replace(tzinfo=None)
    return bson._millis_to_datetime(int(dtm), json_options)


Instruction:
Can you create a Python 3 function that
decodes a JSON symbol to Python string.

Response:
def _parse_canonical_symbol(doc):
    """Decode a JSON symbol to Python string."""
    symbol = doc['$symbol']
    if len(doc) != 1:
        raise TypeError('Bad $symbol, extra field(s): %s' % (doc,))
    return text_type(symbol)


Instruction:
Here you have a function in Python 3, explain what it does
def _parse_canonical_code(doc):
    for key in doc:
        if key not in ('$code', '$scope'):
            raise TypeError('Bad $code, extra field(s): %s' % (doc,))
    return Code(doc['$code'], scope=doc.get('$scope'))

Response: Decode a JSON code to bson. code. Code.


Instruction:
Write a Python 3 script for
decoding a JSON regex to bson. regex. Regex.

Response:
def _parse_canonical_regex(doc):
    """Decode a JSON regex to bson.regex.Regex."""
    regex = doc['$regularExpression']
    if len(doc) != 1:
        raise TypeError('Bad $regularExpression, extra field(s): %s' % (doc,))
    if len(regex) != 2:
        raise TypeError('Bad $regularExpression must include only "pattern"'
                        'and "options" components: %s' % (doc,))
    return Regex(regex['pattern'], regex['options'])


Instruction:
Create a Python 3 function to
decode a JSON DBRef to bson. dbref. DBRef.

Response:
def _parse_canonical_dbref(doc):
    """Decode a JSON DBRef to bson.dbref.DBRef."""
    for key in doc:
        if key.startswith('$') and key not in _DBREF_KEYS:
            # Other keys start with $, so dct cannot be parsed as a DBRef.
            return doc
    return DBRef(doc.pop('$ref'), doc.pop('$id'),
                 database=doc.pop('$db', None), **doc)


Instruction:
Given the following Python 3 function, write the documentation
def _parse_canonical_dbpointer(doc):
    dbref = doc['$dbPointer']
    if len(doc) != 1:
        raise TypeError('Bad $dbPointer, extra field(s): %s' % (doc,))
    if isinstance(dbref, DBRef):
        dbref_doc = dbref.as_doc()
        # DBPointer must not contain $db in its value.
        if dbref.database is not None:
            raise TypeError(
                'Bad $dbPointer, extra field $db: %s' % (dbref_doc,))
        if not isinstance(dbref.id, ObjectId):
            raise TypeError(
                'Bad $dbPointer, $id must be an ObjectId: %s' % (dbref_doc,))
        if len(dbref_doc) != 2:
            raise TypeError(
                'Bad $dbPointer, extra field(s) in DBRef: %s' % (dbref_doc,))
        return dbref
    else:
        raise TypeError('Bad $dbPointer, expected a DBRef: %s' % (doc,))

Response: Decode a JSON ( deprecated ) DBPointer to bson. dbref. DBRef.


Instruction:
Explain what the following Python 3 code does
def _parse_canonical_int32(doc):
    i_str = doc['$numberInt']
    if len(doc) != 1:
        raise TypeError('Bad $numberInt, extra field(s): %s' % (doc,))
    if not isinstance(i_str, string_type):
        raise TypeError('$numberInt must be string: %s' % (doc,))
    return int(i_str)

Response: Decode a JSON int32 to python int.


Instruction:
Write a Python 3 function that can
decode a JSON int64 to bson. int64. Int64.

Response:
def _parse_canonical_int64(doc):
    """Decode a JSON int64 to bson.int64.Int64."""
    l_str = doc['$numberLong']
    if len(doc) != 1:
        raise TypeError('Bad $numberLong, extra field(s): %s' % (doc,))
    return Int64(l_str)


Instruction:
Write a Python 3 script to
decode a JSON double to python float.

Response:
def _parse_canonical_double(doc):
    """Decode a JSON double to python float."""
    d_str = doc['$numberDouble']
    if len(doc) != 1:
        raise TypeError('Bad $numberDouble, extra field(s): %s' % (doc,))
    if not isinstance(d_str, string_type):
        raise TypeError('$numberDouble must be string: %s' % (doc,))
    return float(d_str)


Instruction:
Make a summary of the following Python 3 code
def _parse_canonical_decimal128(doc):
    d_str = doc['$numberDecimal']
    if len(doc) != 1:
        raise TypeError('Bad $numberDecimal, extra field(s): %s' % (doc,))
    if not isinstance(d_str, string_type):
        raise TypeError('$numberDecimal must be string: %s' % (doc,))
    return Decimal128(d_str)

Response: Decode a JSON decimal128 to bson. decimal128. Decimal128.


Instruction:
Create a Python 3 function to
decode a JSON MinKey to bson. min_key. MinKey.

Response:
def _parse_canonical_minkey(doc):
    """Decode a JSON MinKey to bson.min_key.MinKey."""
    if doc['$minKey'] is not 1:
        raise TypeError('$minKey value must be 1: %s' % (doc,))
    if len(doc) != 1:
        raise TypeError('Bad $minKey, extra field(s): %s' % (doc,))
    return MinKey()


Instruction:
How would you explain what the following Python 3 function does
def _parse_canonical_maxkey(doc):
    if doc['$maxKey'] is not 1:
        raise TypeError('$maxKey value must be 1: %s', (doc,))
    if len(doc) != 1:
        raise TypeError('Bad $minKey, extra field(s): %s' % (doc,))
    return MaxKey()

Response: Decode a JSON MaxKey to bson. max_key. MaxKey.


Instruction:
Can you implement a function in Python 3 that
parses read preference options.

Response:
def _parse_read_preference(options):
    """Parse read preference options."""
    if 'read_preference' in options:
        return options['read_preference']

    name = options.get('readpreference', 'primary')
    mode = read_pref_mode_from_name(name)
    tags = options.get('readpreferencetags')
    max_staleness = options.get('maxstalenessseconds', -1)
    return make_read_preference(mode, tags, max_staleness)


Instruction:
Create a Python 3 function for
parsing connection pool options.

Response:
def _parse_pool_options(options):
    """Parse connection pool options."""
    max_pool_size = options.get('maxpoolsize', common.MAX_POOL_SIZE)
    min_pool_size = options.get('minpoolsize', common.MIN_POOL_SIZE)
    max_idle_time_ms = options.get('maxidletimems', common.MAX_IDLE_TIME_MS)
    if max_pool_size is not None and min_pool_size > max_pool_size:
        raise ValueError("minPoolSize must be smaller or equal to maxPoolSize")
    connect_timeout = options.get('connecttimeoutms', common.CONNECT_TIMEOUT)
    socket_keepalive = options.get('socketkeepalive', True)
    socket_timeout = options.get('sockettimeoutms')
    wait_queue_timeout = options.get('waitqueuetimeoutms')
    wait_queue_multiple = options.get('waitqueuemultiple')
    event_listeners = options.get('event_listeners')
    appname = options.get('appname')
    ssl_context, ssl_match_hostname = _parse_ssl_options(options)
    return PoolOptions(max_pool_size,
                       min_pool_size,
                       max_idle_time_ms,
                       connect_timeout, socket_timeout,
                       wait_queue_timeout, wait_queue_multiple,
                       ssl_context, ssl_match_hostname, socket_keepalive,
                       _EventListeners(event_listeners),
                       appname)


Instruction:
Write a Python 3 function that can
validate all service_checks. json files.

Response:
def service_checks():
    """Validate all `service_checks.json` files."""
    root = get_root()
    echo_info("Validating all service_checks.json files...")
    failed_checks = 0
    ok_checks = 0
    for check_name in sorted(os.listdir(root)):
        service_checks_file = os.path.join(root, check_name, 'service_checks.json')

        if file_exists(service_checks_file):
            file_failed = False
            display_queue = []

            try:
                decoded = json.loads(read_file(service_checks_file).strip(), object_pairs_hook=OrderedDict)
            except JSONDecodeError as e:
                failed_checks += 1
                echo_info("{}/service_checks.json... ".format(check_name), nl=False)
                echo_failure("FAILED")
                echo_failure('  invalid json: {}'.format(e))
                continue

            unique_names = set()
            unique_checks = set()
            for service_check in decoded:
                # attributes are valid
                attrs = set(service_check)
                for attr in sorted(attrs - REQUIRED_ATTRIBUTES):
                    file_failed = True
                    display_queue.append((echo_failure, '  Attribute `{}` is invalid'.format(attr)))
                for attr in sorted(REQUIRED_ATTRIBUTES - attrs):
                    file_failed = True
                    display_queue.append((echo_failure, '  Attribute `{}` is required'.format(attr)))

                # agent_version
                agent_version = service_check.get('agent_version')
                version_parts = parse_version_parts(agent_version)
                if len(version_parts) != 3:
                    file_failed = True

                    if not agent_version:
                        output = '  required non-null string: agent_version'
                    else:
                        output = '  invalid `agent_version`: {}'.format(agent_version)

                    display_queue.append((echo_failure, output))

                # check
                check = service_check.get('check')
                if not check or not isinstance(check, string_types):
                    file_failed = True
                    display_queue.append((echo_failure, '  required non-null string: check'))
                else:
                    if check in unique_checks:
                        file_failed = True
                        display_queue.append((echo_failure, '  {} is not a unique check'.format(check)))
                    else:
                        unique_checks.add(check)

                # description
                description = service_check.get('description')
                if not description or not isinstance(description, string_types):
                    file_failed = True
                    display_queue.append((echo_failure, '  required non-null string: description'))

                # groups
                groups = service_check.get('groups')
                if groups is None or not isinstance(groups, list):
                    file_failed = True
                    display_queue.append((echo_failure, '  required list: groups'))

                # integration
                integration = service_check.get('integration')
                if integration is None or not isinstance(integration, string_types):
                    file_failed = True
                    display_queue.append((echo_failure, '  required non-null string: integration'))

                # name
                name = service_check.get('name')
                if not name or not isinstance(name, string_types):
                    file_failed = True
                    display_queue.append((echo_failure, '  required non-null string: name'))
                else:
                    if name in unique_names:
                        file_failed = True
                        display_queue.append((echo_failure, '  {} is not a unique name'.format(name)))
                    else:
                        unique_names.add(name)

                # statuses
                statuses = service_check.get('statuses')
                if not statuses or not isinstance(statuses, list):
                    file_failed = True
                    display_queue.append((echo_failure, '  required non empty list: statuses'))

            if file_failed:
                failed_checks += 1
                # Display detailed info if file invalid
                echo_info("{}/service_checks.json... ".format(check_name), nl=False)
                echo_failure("FAILED")
                for display_func, message in display_queue:
                    display_func(message)
            else:
                ok_checks += 1

    if ok_checks:
        echo_success("{} valid files".format(ok_checks))
    if failed_checks:
        echo_failure("{} invalid files".format(failed_checks))
        abort()


Instruction:
Write a Python 3 function for
converting a WMI time string into a 9 - tuple.

Response:
def to_time(wmi_time):
    """Convenience wrapper to take a WMI datetime string of the form
    yyyymmddHHMMSS.mmmmmm+UUU and return a 9-tuple containing the
    individual elements, or None where string contains placeholder
    stars.

    :param wmi_time: The WMI datetime string in `yyyymmddHHMMSS.mmmmmm+UUU` format

    :returns: A 9-tuple of (year, month, day, hours, minutes, seconds, microseconds, timezone)
    """

    def int_or_none(s, start, end):
        try:
            return int(s[start:end])
        except ValueError:
            return None

    year = int_or_none(wmi_time, 0, 4)
    month = int_or_none(wmi_time, 4, 6)
    day = int_or_none(wmi_time, 6, 8)
    hours = int_or_none(wmi_time, 8, 10)
    minutes = int_or_none(wmi_time, 10, 12)
    seconds = int_or_none(wmi_time, 12, 14)
    microseconds = int_or_none(wmi_time, 15, 21)
    timezone = wmi_time[22:]

    if timezone == "***":
        timezone = None

    return year, month, day, hours, minutes, seconds, microseconds, timezone


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _format_tag_query(self, sampler, wmi_obj, tag_query):
        try:
            link_source_property = int(wmi_obj[tag_query[0]])
            target_class = tag_query[1]
            link_target_class_property = tag_query[2]
            target_property = tag_query[3]
        except IndexError:
            self.log.error(
                u"Wrong `tag_queries` parameter format. " "Please refer to the configuration file for more information."
            )
            raise
        except TypeError:
            self.log.error(
                u"Incorrect 'link source property' in `tag_queries` parameter:"
                " `{wmi_property}` is not a property of `{wmi_class}`".format(
                    wmi_property=tag_query[0], wmi_class=sampler.class_name
                )
            )
            raise

        return (target_class, target_property, [{link_target_class_property: link_source_property}])

Response: Format `tag_query` or raise on incorrect parameters.


Instruction:
Can you write a function in Python 3 where it
designs a query based on the given WMIObject to extract a tag.

Response:
def _get_tag_query_tag(self, sampler, wmi_obj, tag_query):
        """
        Design a query based on the given WMIObject to extract a tag.

        Returns: tag or TagQueryUniquenessFailure exception.
        """
        self.log.debug(
            u"`tag_queries` parameter found."
            " wmi_object={wmi_obj} - query={tag_query}".format(wmi_obj=wmi_obj, tag_query=tag_query)
        )

        # Extract query information
        target_class, target_property, filters = self._format_tag_query(sampler, wmi_obj, tag_query)

        # Create a specific sampler
        tag_query_sampler = WMISampler(self.log, target_class, [target_property], filters=filters, **sampler.connection)

        tag_query_sampler.sample()

        # Extract tag
        self._raise_on_invalid_tag_query_result(tag_query_sampler, wmi_obj, tag_query)

        link_value = str(tag_query_sampler[0][target_property]).lower()

        tag = "{tag_name}:{tag_value}".format(tag_name=target_property.lower(), tag_value="_".join(link_value.split()))

        self.log.debug(u"Extracted `tag_queries` tag: '{tag}'".format(tag=tag))
        return tag


Instruction:
Create a Python 3 function for
extracting and tag metrics from the WMI sampler.

Response:
def _extract_metrics(self, wmi_sampler, tag_by, tag_queries, constant_tags):
        """
        Extract and tag metrics from the WMISampler.

        Raise when multiple WMIObject were returned by the sampler with no `tag_by` specified.

        Returns: List of WMIMetric
        ```
        [
            WMIMetric("freemegabytes", 19742, ["name:_total"]),
            WMIMetric("avgdiskbytesperwrite", 1536, ["name:c:"]),
        ]
        ```
        """
        if len(wmi_sampler) > 1 and not tag_by:
            raise MissingTagBy(
                u"WMI query returned multiple rows but no `tag_by` value was given."
                " class={wmi_class} - properties={wmi_properties} - filters={filters}".format(
                    wmi_class=wmi_sampler.class_name,
                    wmi_properties=wmi_sampler.property_names,
                    filters=wmi_sampler.filters,
                )
            )

        metrics = []
        tag_by = tag_by.lower()

        for wmi_obj in wmi_sampler:
            tags = list(constant_tags) if constant_tags else []

            # Tag with `tag_queries` parameter
            for query in tag_queries:
                try:
                    tags.append(self._get_tag_query_tag(wmi_sampler, wmi_obj, query))
                except TagQueryUniquenessFailure:
                    continue

            for wmi_property, wmi_value in iteritems(wmi_obj):
                # skips any property not in arguments since SWbemServices.ExecQuery will return key prop properties
                # https://msdn.microsoft.com/en-us/library/aa393866(v=vs.85).aspx
                if wmi_property.lower() not in (s.lower() for s in wmi_sampler.property_names):
                    continue
                # Tag with `tag_by` parameter
                if wmi_property == tag_by:
                    tag_value = str(wmi_value).lower()
                    if tag_queries and tag_value.find("#") > 0:
                        tag_value = tag_value[: tag_value.find("#")]

                    tags.append("{name}:{value}".format(name=tag_by, value=tag_value))
                    continue

                # No metric extraction on 'Name' property
                if wmi_property == 'name':
                    continue

                try:
                    metrics.append(WMIMetric(wmi_property, float(wmi_value), tags))
                except ValueError:
                    self.log.warning(
                        u"When extracting metrics with WMI, found a non digit value"
                        " for property '{0}'.".format(wmi_property)
                    )
                    continue
                except TypeError:
                    self.log.warning(
                        u"When extracting metrics with WMI, found a missing property" " '{0}'".format(wmi_property)
                    )
                    continue
        return metrics


Instruction:
Can you implement a function in Python 3 that
submits the given metrics to the user.

Response:
def _submit_metrics(self, metrics, metric_name_and_type_by_property):
        """
        Resolve metric names and types and submit it.
        """
        for metric in metrics:
            if (
                metric.name not in metric_name_and_type_by_property
                and metric.name.lower() not in metric_name_and_type_by_property
            ):
                # Only report the metrics that were specified in the configration
                # Ignore added properties like 'Timestamp_Sys100NS', `Frequency_Sys100NS`, etc ...
                continue

            if metric_name_and_type_by_property.get(metric.name):
                metric_name, metric_type = metric_name_and_type_by_property[metric.name]
            elif metric_name_and_type_by_property.get(metric.name.lower()):
                metric_name, metric_type = metric_name_and_type_by_property[metric.name.lower()]
            else:
                continue

            try:
                func = getattr(self, metric_type.lower())
            except AttributeError:
                raise Exception(u"Invalid metric type: {0}".format(metric_type))

            func(metric_name, metric.value, metric.tags)


Instruction:
Write a Python 3 function that can
return an index key for a given instance. Useful for caching.

Response:
def _get_instance_key(self, host, namespace, wmi_class, other=None):
        """
        Return an index key for a given instance. Useful for caching.
        """
        if other:
            return "{host}:{namespace}:{wmi_class}-{other}".format(
                host=host, namespace=namespace, wmi_class=wmi_class, other=other
            )

        return "{host}:{namespace}:{wmi_class}".format(host=host, namespace=namespace, wmi_class=wmi_class)


Instruction:
How would you implement a function in Python 3 that
gets a WMISampler instance for the given instance_key.

Response:
def _get_wmi_sampler(self, instance_key, wmi_class, properties, tag_by="", **kwargs):
        """
        Create and cache a WMISampler for the given (class, properties)
        """
        properties = list(properties) + [tag_by] if tag_by else list(properties)

        if instance_key not in self.wmi_samplers:
            wmi_sampler = WMISampler(self.log, wmi_class, properties, **kwargs)
            self.wmi_samplers[instance_key] = wmi_sampler

        return self.wmi_samplers[instance_key]


Instruction:
Create a Python 3 function to
get WMI properties for a given instance.

Response:
def _get_wmi_properties(self, instance_key, metrics, tag_queries):
        """
        Create and cache a (metric name, metric type) by WMI property map and a property list.
        """
        if instance_key not in self.wmi_props:
            metric_name_by_property = dict(
                (wmi_property.lower(), (metric_name, metric_type)) for wmi_property, metric_name, metric_type in metrics
            )
            properties = map(lambda x: x[0], metrics + tag_queries)
            self.wmi_props[instance_key] = (metric_name_by_property, properties)

        return self.wmi_props[instance_key]


Instruction:
Can you write a function in Python 3 where it
submits a metric as a rate

Response:
def _submit_rate(self, metric_name, val, metric, custom_tags=None, hostname=None):
        """
        Submit a metric as a rate, additional tags provided will be added to
        the ones from the label provided via the metrics object.

        `custom_tags` is an array of 'tag:value' that will be added to the
        metric when sending the rate to Datadog.
        """
        _tags = self._metric_tags(metric_name, val, metric, custom_tags, hostname)
        self.rate('{}.{}'.format(self.NAMESPACE, metric_name), val, _tags, hostname=hostname)


Instruction:
Can you create a Python 3 function that
submits a metric as a monotonic count.

Response:
def _submit_monotonic_count(self, metric_name, val, metric, custom_tags=None, hostname=None):
        """
        Submit a metric as a monotonic count, additional tags provided will be added to
        the ones from the label provided via the metrics object.

        `custom_tags` is an array of 'tag:value' that will be added to the
        metric when sending the monotonic count to Datadog.
        """

        _tags = self._metric_tags(metric_name, val, metric, custom_tags, hostname)
        self.monotonic_count('{}.{}'.format(self.NAMESPACE, metric_name), val, _tags, hostname=hostname)


Instruction:
Here you have a function in Python 3, explain what it does
def parse_expvar_data(self, data, tags, metrics, max_metrics, namespace):
        '''
        Report all the metrics based on the configuration in instance
        If a metric is not well configured or is not present in the payload,
        continue processing metrics but log the information to the info page
        '''
        count = 0
        for metric in metrics:
            path = metric.get(PATH)
            metric_type = metric.get(TYPE, DEFAULT_TYPE)
            metric_tags = list(metric.get(TAGS, []))
            metric_tags += tags
            alias = metric.get(ALIAS)

            if not path:
                self.warning("Metric %s has no path" % metric)
                continue

            if metric_type not in SUPPORTED_TYPES:
                self.warning("Metric type %s not supported for this check" % metric_type)
                continue

            keys = path.split("/")
            values = self.deep_get(data, keys)

            if len(values) == 0:
                self.warning("No results matching path %s" % path)
                continue

            tag_by_path = alias is not None

            for traversed_path, value in values:
                actual_path = ".".join(traversed_path)
                path_tag = ["path:%s" % actual_path] if tag_by_path else []

                metric_name = alias or self.normalize(actual_path, namespace, fix_case=True)

                try:
                    float(value)
                except ValueError:
                    self.log.warning("Unreportable value for path %s: %s" % (path, value))
                    continue

                if count >= max_metrics:
                    self.warning(
                        "Reporting more metrics than the allowed maximum. "
                        "Please contact support@datadoghq.com for more information."
                    )
                    return

                SUPPORTED_TYPES[metric_type](self, metric_name, value, metric_tags + path_tag)
                count += 1

Response: Parse the expvar data and return the corresponding instance of ExpVar class.


Instruction:
Given the following Python 3 function, write the documentation
def deep_get(self, content, keys, traversed_path=None):
        '''
        Allow to retrieve content nested inside a several layers deep dict/list

        Examples: -content: {
                            "key1": {
                                "key2" : [
                                            {
                                                "name"  : "object1",
                                                "value" : 42
                                            },
                                            {
                                                "name"  : "object2",
                                                "value" : 72
                                            }
                                          ]
                            }
                        }
                  -keys: ["key1", "key2", "1", "value"]
                    would return:
                        [(["key1", "key2", "1", "value"], 72)]
                  -keys: ["key1", "key2", "1", "*"]
                    would return:
                        [(["key1", "key2", "1", "value"], 72), (["key1", "key2", "1", "name"], "object2")]
                  -keys: ["key1", "key2", "*", "value"]
                    would return:
                        [(["key1", "key2", "1", "value"], 72), (["key1", "key2", "0", "value"], 42)]
        '''

        if traversed_path is None:
            traversed_path = []

        if keys == []:
            return [(traversed_path, content)]

        key = keys[0]
        regex = "".join(["^", key, "$"])
        try:
            key_rex = re.compile(regex)
        except Exception:
            self.warning("Cannot compile regex: %s" % regex)
            return []

        results = []
        for new_key, new_content in self.items(content):
            if key_rex.match(new_key):
                results.extend(self.deep_get(new_content, keys[1:], traversed_path + [str(new_key)]))
        return results

Response: This method recursively gets the content of a deep dict.


Instruction:
Can you create a Python 3 function that
splits a host string into host and port pair.

Response:
def partition_node(node):
    """Split a host:port string into (host, int(port)) pair."""
    host = node
    port = 27017
    idx = node.rfind(':')
    if idx != -1:
        host, port = node[:idx], int(node[idx + 1:])
    if host.startswith('['):
        host = host[1:-1]
    return host, port


Instruction:
Here you have a function in Python 3, explain what it does
def validate_boolean_or_string(option, value):
    if isinstance(value, string_type):
        if value not in ('true', 'false'):
            raise ValueError("The value of %s must be "
                             "'true' or 'false'" % (option,))
        return value == 'true'
    return validate_boolean(option, value)

Response: Validates that value is True False True or false.


Instruction:
How would you explain what the following Python 3 function does
def validate_positive_integer(option, value):
    val = validate_integer(option, value)
    if val <= 0:
        raise ValueError("The value of %s must be "
                         "a positive integer" % (option,))
    return val

Response: Validate that value is a positive integer which does not include 0.


Instruction:
How would you explain what the following Python 3 function does
def validate_non_negative_integer(option, value):
    val = validate_integer(option, value)
    if val < 0:
        raise ValueError("The value of %s must be "
                         "a non negative integer" % (option,))
    return val

Response: Validate that value is a positive integer or 0.


Instruction:
Here you have a function in Python 3, explain what it does
def validate_readable(option, value):
    if value is None:
        return value
    # First make sure its a string py3.3 open(True, 'r') succeeds
    # Used in ssl cert checking due to poor ssl module error reporting
    value = validate_string(option, value)
    open(value, 'r').close()
    return value

Response: Validates that value is file - like and readable.


Instruction:
Create a Python 3 function to
validate that value is an instance of basestring for Python 2 or str for Python 3.

Response:
def validate_string(option, value):
    """Validates that 'value' is an instance of `basestring` for Python 2
    or `str` for Python 3.
    """
    if isinstance(value, string_type):
        return value
    raise TypeError("Wrong type for %s, value must be "
                    "an instance of %s" % (option, string_type.__name__))


Instruction:
Make a summary of the following Python 3 code
def validate_positive_float(option, value):
    errmsg = "%s must be an integer or float" % (option,)
    try:
        value = float(value)
    except ValueError:
        raise ValueError(errmsg)
    except TypeError:
        raise TypeError(errmsg)

    # float('inf') doesn't work in 2.4 or 2.5 on Windows, so just cap floats at
    # one billion - this is a reasonable approximation for infinity
    if not 0 < value < 1e9:
        raise ValueError("%s must be greater than 0 and "
                         "less than one billion" % (option,))
    return value

Response:
Validates that value is a float or can be converted to one and is
       positive.


Instruction:
How would you code a function in Python 3 to
validate a timeout specified in milliseconds returning a value in floating point seconds for the case where None is an error 0 is a ConfigurationError.

Response:
def validate_timeout_or_zero(option, value):
    """Validates a timeout specified in milliseconds returning
    a value in floating point seconds for the case where None is an error
    and 0 is valid. Setting the timeout to nothing in the URI string is a
    config error.
    """
    if value is None:
        raise ConfigurationError("%s cannot be None" % (option, ))
    if value == 0 or value == "0":
        return 0
    return validate_positive_float(option, value) / 1000.0


Instruction:
Here you have a function in Python 3, explain what it does
def validate_auth_mechanism(option, value):
    # CRAM-MD5 is for server testing only. Undocumented,
    # unsupported, may be removed at any time. You have
    # been warned.
    if value not in MECHANISMS and value != 'CRAM-MD5':
        raise ValueError("%s must be in %s" % (option, tuple(MECHANISMS)))
    return value

Response: Validate the authMechanism URI option.


Instruction:
Implement a Python 3 function for
validating the uuid representation option selected in the URI.

Response:
def validate_uuid_representation(dummy, value):
    """Validate the uuid representation option selected in the URI.
    """
    try:
        return _UUID_REPRESENTATIONS[value]
    except KeyError:
        raise ValueError("%s is an invalid UUID representation. "
                         "Must be one of "
                         "%s" % (value, tuple(_UUID_REPRESENTATIONS)))


Instruction:
Can you tell what is the following Python 3 function doing
def validate_read_preference_tags(name, value):
    if not isinstance(value, list):
        value = [value]

    tag_sets = []
    for tag_set in value:
        if tag_set == '':
            tag_sets.append({})
            continue
        try:
            tag_sets.append(dict([tag.split(":")
                                  for tag in tag_set.split(",")]))
        except Exception:
            raise ValueError("%r not a valid "
                             "value for %s" % (tag_set, name))
    return tag_sets

Response: Parse readPreferenceTags if passed as a client kwarg.


Instruction:
Make a summary of the following Python 3 code
def validate_document_class(option, value):
    if not issubclass(value, (collections.MutableMapping, RawBSONDocument)):
        raise TypeError("%s must be dict, bson.son.SON, "
                        "bson.raw_bson.RawBSONDocument, or a "
                        "sublass of collections.MutableMapping" % (option,))
    return value

Response: Validate the document_class option.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def validate_is_document_type(option, value):
    if not isinstance(value, (collections.MutableMapping, RawBSONDocument)):
        raise TypeError("%s must be an instance of dict, bson.son.SON, "
                        "bson.raw_bson.RawBSONDocument, or "
                        "a type that inherits from "
                        "collections.MutableMapping" % (option,))

Response: Validate the type of method arguments that expect a MongoDB document.


Instruction:
How would you implement a function in Python 3 that
validates the appname option.

Response:
def validate_appname_or_none(option, value):
    """Validate the appname option."""
    if value is None:
        return value
    validate_string(option, value)
    # We need length in bytes, so encode utf8 first.
    if len(value.encode('utf-8')) > 128:
        raise ValueError("%s must be <= 128 bytes" % (option,))
    return value


Instruction:
Write a Python 3 function that can
validate a replacement document.

Response:
def validate_ok_for_replace(replacement):
    """Validate a replacement document."""
    validate_is_mapping("replacement", replacement)
    # Replacement can be {}
    if replacement and not isinstance(replacement, RawBSONDocument):
        first = next(iter(replacement))
        if first.startswith('$'):
            raise ValueError('replacement can not include $ operators')


Instruction:
Implement a function in Python 3 to
validate an update document.

Response:
def validate_ok_for_update(update):
    """Validate an update document."""
    validate_is_mapping("update", update)
    # Update can not be {}
    if not update:
        raise ValueError('update only works with $ operators')
    first = next(iter(update))
    if not first.startswith('$'):
        raise ValueError('update only works with $ operators')


Instruction:
Make a summary of the following Python 3 code
def validate_unicode_decode_error_handler(dummy, value):
    if value not in _UNICODE_DECODE_ERROR_HANDLERS:
        raise ValueError("%s is an invalid Unicode decode error handler. "
                         "Must be one of "
                         "%s" % (value, tuple(_UNICODE_DECODE_ERROR_HANDLERS)))
    return value

Response: Validate Unicode decode error handler option of CodecOptions.


Instruction:
Can you create a Python 3 function that
validates the tzinfo option

Response:
def validate_tzinfo(dummy, value):
    """Validate the tzinfo option
    """
    if value is not None and not isinstance(value, datetime.tzinfo):
        raise TypeError("%s must be an instance of datetime.tzinfo" % value)
    return value


Instruction:
Make a summary of the following Python 3 code
def validate_auth_option(option, value):
    lower, value = validate(option, value)
    if lower not in _AUTH_OPTIONS:
        raise ConfigurationError('Unknown '
                                 'authentication option: %s' % (option,))
    return lower, value

Response: Validate optional authentication parameters.


Instruction:
Can you write a function in Python 3 where it
validates each entry in options and raise a warning if it is not valid.

Response:
def get_validated_options(options, warn=True):
    """Validate each entry in options and raise a warning if it is not valid.
    Returns a copy of options with invalid entries removed
    """
    validated_options = {}
    for opt, value in iteritems(options):
        lower = opt.lower()
        try:
            validator = URI_VALIDATORS.get(lower, raise_config_error)
            value = validator(opt, value)
        except (ValueError, ConfigurationError) as exc:
            if warn:
                warnings.warn(str(exc))
            else:
                raise
        else:
            validated_options[lower] = value
    return validated_options


Instruction:
Can you generate the documentation for the following Python 3 function
def _parse_dict_string(self, string, key, default):
        try:
            for item in string.split(','):
                k, v = item.rsplit('=', 1)
                if k == key:
                    try:
                        return int(v)
                    except ValueError:
                        return v
            return default
        except Exception:
            self.log.exception("Cannot parse dictionary string: %s" % string)
            return default

Response: Take from a more recent redis. py parse_info