Instruction:
Can you tell what is the following Python 3 function doing
def build_job_configs(self, args):
        job_configs = {}

        components = Component.build_from_yamlfile(args['comp'])
        NAME_FACTORY.update_base_dict(args['data'])

        models = load_yaml(args['models'])

        for modelkey in models:
            manifest = os.path.join('analysis', 'model_%s' % modelkey,
                                    'srcmap_manifest_%s.yaml' % modelkey)
            for comp in components:
                key = comp.make_key('{ebin_name}_{evtype_name}')
                fullkey = "%s_%s" % (modelkey, key)
                outfile = NAME_FACTORY.merged_srcmaps(modelkey=modelkey,
                                                      component=key,
                                                      coordsys=comp.coordsys,
                                                      mktime='none',
                                                      irf_ver=NAME_FACTORY.irf_ver())
                logfile = make_nfs_path(outfile.replace('.fits', '.log'))
                job_configs[fullkey] = dict(input=manifest,
                                            compname=key,
                                            logfile=logfile)
        return job_configs

Response: Hook to build job configurations from the input data and the output files of the components.


Instruction:
Make a summary of the following Python 3 code
def _map_arguments(self, input_dict):
        data = input_dict.get('data')
        comp = input_dict.get('comp')
        library = input_dict.get('library')
        models = input_dict.get('models')
        hpx_order = input_dict.get('hpx_order_fitting')
        dry_run = input_dict.get('dry_run', False)

        self._set_link('init-model', InitModel,
                       comp=comp, data=data,
                       library=library,
                       models=models,
                       hpx_order=hpx_order,
                       dry_run=dry_run)

        self._set_link('assemble-model', AssembleModel_SG,
                       comp=comp, data=data,
                       models=models)

Response:
Map from the top - level arguments to the arguments provided to
        the indiviudal links


Instruction:
Write a Python 3 function that can
hook to build job configurations based on the arguments passed in.

Response:
def build_job_configs(self, args):
        """Hook to build job configurations
        """
        job_configs = {}

        ttype = args['ttype']
        (targets_yaml, sim) = NAME_FACTORY.resolve_targetfile(args)
        if sim is not None:
            raise ValueError("Found 'sim' argument on AnalyzeROI_SG config.")
        if targets_yaml is None:
            return job_configs

        config_yaml = 'config.yaml'
        config_override = args.get('config')
        if is_not_null(config_override):
            config_yaml = config_override

        targets = load_yaml(targets_yaml)
        base_config = dict(roi_baseline=args['roi_baseline'],
                           make_plots=args['make_plots'])

        for target_name in targets.keys():
            name_keys = dict(target_type=ttype,
                             target_name=target_name,
                             fullpath=True)
            target_dir = NAME_FACTORY.targetdir(**name_keys)
            config_path = os.path.join(target_dir, config_yaml)
            logfile = make_nfs_path(os.path.join(
                target_dir, "%s_%s.log" % (self.linkname, target_name)))
            job_config = base_config.copy()
            job_config.update(dict(config=config_path,
                                   logfile=logfile))
            job_configs[target_name] = job_config

        return job_configs


Instruction:
How would you implement a function in Python 3 that
receives data from the device.

Response:
def read_data(self, size):
        """Receive data from the device.

        If the read fails for any reason, an :obj:`IOError` exception
        is raised.

        :param size: the number of bytes to read.

        :type size: int

        :return: the data received.

        :rtype: list(int)

        """
        result = list()
        data = ctypes.create_string_buffer(8)
        while size > 0:
            length = min(size, 8)
            n = hidapi.hid_read_timeout(self.device, data, length, 100)
            if n <= 0:
                raise IOError(
                    'pywws.device_ctypes_hidapi.USBDevice.read_data failed')
            for i in range(n):
                result.append(ord(data[i]))
            size -= n
        return result


Instruction:
Here you have a function in Python 3, explain what it does
def write_data(self, buf):
        data = ''.join(map(chr, buf))
        size = len(data)
        if hidapi.hid_write(self.device, ctypes.c_char_p(data), size) != size:
            raise IOError(
                'pywws.device_ctypes_hidapi.USBDevice.write_data failed')
        return True

Response: Send data to the device.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def zambretti_code(params, hourly_data):
    north = literal_eval(params.get('Zambretti', 'north', 'True'))
    baro_upper = float(params.get('Zambretti', 'baro upper', '1050.0'))
    baro_lower = float(params.get('Zambretti', 'baro lower', '950.0'))
    if not hourly_data['rel_pressure']:
        return ''
    if hourly_data['wind_ave'] is None or hourly_data['wind_ave'] < 0.3:
        wind = None
    else:
        wind = hourly_data['wind_dir']
    if hourly_data['pressure_trend'] is None:
        trend = 0.0
    else:
        trend = hourly_data['pressure_trend'] / 3.0
    # normalise pressure
    pressure = 950.0 + (
        (1050.0 - 950.0) * (hourly_data['rel_pressure'] - baro_lower) /
        (baro_upper - baro_lower))
    # adjust pressure for wind direction
    if wind is not None:
        if not isinstance(wind, int):
            wind = int(wind + 0.5) % 16
        if not north:
            # southern hemisphere, so add 180 degrees
            wind = (wind + 8) % 16
        pressure += (  5.2,  4.2,  3.2,  1.05, -1.1, -3.15, -5.2, -8.35,
                     -11.5, -9.4, -7.3, -5.25, -3.2, -1.15,  0.9,  3.05)[wind]
    # compute base forecast from pressure and trend (hPa / hour)
    summer = north == (hourly_data['idx'].month >= 4 and
                       hourly_data['idx'].month <= 9)
    if trend >= 0.1:
        # rising pressure
        if summer:
            pressure += 3.2
        F = 0.1740 * (1031.40 - pressure)
        LUT = ('A', 'B', 'B', 'C', 'F', 'G', 'I', 'J', 'L', 'M', 'M', 'Q', 'T',
               'Y')
    elif trend <= -0.1:
        # falling pressure
        if summer:
            pressure -= 3.2
        F = 0.1553 * (1029.95 - pressure)
        LUT = ('B', 'D', 'H', 'O', 'R', 'U', 'V', 'X', 'X', 'Z')
    else:
        # steady
        F = 0.2314 * (1030.81 - pressure)
        LUT = ('A', 'B', 'B', 'B', 'E', 'K', 'N', 'N', 'P', 'P', 'S', 'W', 'W',
               'X', 'X', 'X', 'Z')
    # clip to range of lookup table
    F = min(max(int(F + 0.5), 0), len(LUT) - 1)
    # convert to letter code
    return LUT[F]

Response: Simple implementation of Zambretti forecaster algorithm.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read_block(self, address):
        buf = [
            self.ReadCommand,
            address // 256,
            address % 256,
            self.EndMark,
            self.ReadCommand,
            address // 256,
            address % 256,
            self.EndMark,
            ]
        if not self.dev.write_data(buf):
            return None
        return self.dev.read_data(32)

Response: Read 32 bytes from the weather station.


Instruction:
Implement a function in Python 3 to
write a single byte to the weather station.

Response:
def write_byte(self, address, data):
        """Write a single byte to the weather station.

        :param address: address to write to.

        :type address: int

        :param data: the value to write.

        :type data: int

        :return: success status.

        :rtype: bool

        """
        buf = [
            self.WriteCommandWord,
            address // 256,
            address % 256,
            self.EndMark,
            self.WriteCommandWord,
            data,
            0,
            self.EndMark,
            ]
        if not self.dev.write_data(buf):
            return False
        buf = self.dev.read_data(8)
        if buf is None:
            return False
        for byte in buf:
            if byte != 0xA5:
                return False
        return True


Instruction:
Can you create a Python 3 function that
gets next circular buffer data pointer.

Response:
def inc_ptr(self, ptr):
        """Get next circular buffer data pointer."""
        result = ptr + self.reading_len[self.ws_type]
        if result >= 0x10000:
            result = self.data_start
        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def dec_ptr(self, ptr):
        result = ptr - self.reading_len[self.ws_type]
        if result < self.data_start:
            result = 0x10000 - self.reading_len[self.ws_type]
        return result

Response: Get previous circular buffer data pointer.


Instruction:
Here you have a function in Python 3, explain what it does
def get_raw_data(self, ptr, unbuffered=False):
        if unbuffered:
            self._data_pos = None
        # round down ptr to a 'block boundary'
        idx = ptr - (ptr % 0x20)
        ptr -= idx
        count = self.reading_len[self.ws_type]
        if self._data_pos == idx:
            # cache contains useful data
            result = self._data_block[ptr:ptr + count]
            if len(result) >= count:
                return result
        else:
            result = list()
        if ptr + count > 0x20:
            # need part of next block, which may be in cache
            if self._data_pos != idx + 0x20:
                self._data_pos = idx + 0x20
                self._data_block = self._read_block(self._data_pos)
            result += self._data_block[0:ptr + count - 0x20]
            if len(result) >= count:
                return result
        # read current block
        self._data_pos = idx
        self._data_block = self._read_block(self._data_pos)
        result = self._data_block[ptr:ptr + count] + result
        return result

Response: Get raw data from circular buffer.


Instruction:
Given the following Python 3 function, write the documentation
def get_data(self, ptr, unbuffered=False):
        result = _decode(self.get_raw_data(ptr, unbuffered),
                         self._reading_format[self.ws_type])
        return result

Response: Get decoded data from circular buffer.


Instruction:
Given the following Python 3 function, write the documentation
def current_pos(self):
        new_ptr = _decode(
            self._read_fixed_block(0x0020), self.lo_fix_format['current_pos'])
        if new_ptr == self._current_ptr:
            return self._current_ptr
        if self._current_ptr and new_ptr != self.inc_ptr(self._current_ptr):
            logger.error(
                'unexpected ptr change %06x -> %06x', self._current_ptr, new_ptr)
        self._current_ptr = new_ptr
        return self._current_ptr

Response: Get the current circular buffer location where current data is being written.


Instruction:
Can you tell what is the following Python 3 function doing
def get_raw_fixed_block(self, unbuffered=False):
        if unbuffered or not self._fixed_block:
            self._fixed_block = self._read_fixed_block()
        return self._fixed_block

Response: Get the raw fixed block of settings and min / max data.


Instruction:
Can you write a function in Python 3 where it
gets the decoded fixed block of settings and min / max data.

Response:
def get_fixed_block(self, keys=[], unbuffered=False):
        """Get the decoded "fixed block" of settings and min/max data.

        A subset of the entire block can be selected by keys."""
        if unbuffered or not self._fixed_block:
            self._fixed_block = self._read_fixed_block()
        format = self.fixed_format
        # navigate down list of keys to get to wanted data
        for key in keys:
            format = format[key]
        return _decode(self._fixed_block, format)


Instruction:
Given the following Python 3 function, write the documentation
def write_data(self, data):
        # send data
        for ptr, value in data:
            self._write_byte(ptr, value)
        # set 'data changed'
        self._write_byte(self.fixed_format['data_changed'][0], 0xAA)
        # wait for station to clear 'data changed'
        while True:
            ack = _decode(
                self._read_fixed_block(0x0020), self.fixed_format['data_changed'])
            if ack == 0:
                break
            logger.debug('write_data waiting for ack')
            time.sleep(6)

Response: Write a set of single bytes to the weather station.


Instruction:
Can you generate the documentation for the following Python 3 function
def _find_device(self, idVendor, idProduct):
        for bus in usb.busses():
            for device in bus.devices:
                if (device.idVendor == idVendor and
                    device.idProduct == idProduct):
                    return device
        return None

Response: Find a USB device by vendor and product id.


Instruction:
Can you implement a function in Python 3 that
receives data from the device.

Response:
def read_data(self, size):
        """Receive data from the device.

        If the read fails for any reason, an :obj:`IOError` exception
        is raised.

        :param size: the number of bytes to read.

        :type size: int

        :return: the data received.

        :rtype: list(int)

        """
        result = self.devh.interruptRead(0x81, size, 1200)
        if result is None or len(result) < size:
            raise IOError('pywws.device_libusb.USBDevice.read_data failed')
        return list(result)


Instruction:
Write a Python 3 function that can
send data to the device.

Response:
def write_data(self, buf):
        """Send data to the device.

        If the write fails for any reason, an :obj:`IOError` exception
        is raised.

        :param buf: the data to send.

        :type buf: list(int)

        :return: success status.

        :rtype: bool

        """
        result = self.devh.controlMsg(
            usb.ENDPOINT_OUT + usb.TYPE_CLASS + usb.RECIP_INTERFACE,
            usb.REQ_SET_CONFIGURATION, buf, value=0x200, timeout=50)
        if result != len(buf):
            raise IOError('pywws.device_libusb.USBDevice.write_data failed')
        return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _adapt_WSDateTime(dt):
    try:
        ts = int(
            (dt.replace(tzinfo=pytz.utc)
            - datetime(1970,1,1,tzinfo=pytz.utc)
            ).total_seconds()
        )
    except (OverflowError,OSError):
        if dt < datetime.now():
            ts = 0
        else:
            ts = 2**63-1
    return ts

Response: Adapt the datetime like input into a unix timestamp.


Instruction:
Create a Python 3 function for
returning the predicate portion of the SQL query string that is used to select the items from the database.

Response:
def _predicate(self, i):
        """Given a valid datetime or slace, return the predicate portion
        of the SQL query, a boolean indicating whether multiple items are
        expected from the result, and a dictionary of parameters for the query
        """
        if isinstance(i, slice):
            if i.step is not None:
                raise TypeError("Slice step not permitted")
            if ( (i.start is not None and not isinstance(i.start, datetime))
                or (i.stop is not None and not isinstance(i.stop, datetime))
            ):
                raise TypeError(
                    "Slice indices must be {} or None".format(datetime)
                )
            if i.start is not None and i.stop is not None:
                if i.start > i.stop:
                    raise ValueError(
                        "Start index is greater than the End index"
                    )
                else:
                    # Substitution of the key coloumn, but the
                    # parameters themselves will be substituted by sqlite3
                    predicate = "WHERE {} BETWEEN :start AND :stop".format(
                        self._keycol
                    )
            elif i.start is not None:
                # i.stop will also be None
                predicate = "WHERE {} >= :start".format(self._keycol)
            elif i.stop is not None:
                # i.start will also be None
                predicate = "WHERE {} <= :stop".format(self._keycol)
            else:
                # both are None, so equivelent to wanting everything
                predicate = ""
            multi = True
            pred = {"start": i.start, "stop": i.stop}
        elif isinstance(i, datetime):
            # Substitution of the key coloumn, but the
            # parameters themselves will be substituted by sqlite3
            predicate = "WHERE {} = :key".format(self._keycol)
            multi = False
            pred = {"key": i}
        else:
            # not a slice or a datetime object
            raise TypeError("List indices must be {}".format(datetime))
        # predicate is the end of the query string.
        # multi is a boolean indicating whether the result should be iterable
        # or not. pred is a dict of the parameters for substitution
        return (predicate, multi, pred)


Instruction:
Here you have a function in Python 3, explain what it does
def update(self, i):
        key_list = self.key_list
        keynone = {key:None for key in key_list}
        # Generator which fills in missing data from the original iterator
        def datagen(i):
            for datum in i:
                tmp = keynone.copy()
                tmp.update(datum)
                yield tmp
        with self._connection as con:
            con.executemany(
                """INSERT OR REPLACE INTO {table} ({keylist})
                VALUES (:{vallist});
                """.format(table=self.table,
                    keylist=", ".join(self.key_list),
                    vallist=", :".join(self.key_list)
                ), datagen(i))

Response: Update the dictionary D with the contents of i.


Instruction:
Here you have a function in Python 3, explain what it does
def before(self, i):
        if not isinstance(i, datetime):
            raise TypeError("'{}' is not a datetime object".format(i))
        else:
            result = self._connection.execute(
                """SELECT {selkeycol} FROM {table} WHERE
                {keycol} < :key ORDER BY {keycol} DESC LIMIT 1;
                """.format(
                    selkeycol=self.selkeycol,
                    table=self.table,
                    keycol=self._keycol
                ), {"key":i}).fetchone()
        return result[self._keycol] if result is not None else None

Response:
Return datetime of newest existing data record whose datetime
        is < idx.


Instruction:
Write a Python 3 script for
clearing all items from the database.

Response:
def clear(self):
        """S.clear() -> None -- remove all items from S"""
        with self._connection as con:
            con.execute("DELETE FROM {};".format(self.table))


Instruction:
Given the following Python 3 function, write the documentation
def set_locale(lang):
    # get the default locale
    lc, encoding = locale.getdefaultlocale()
    try:
        if '.' in lang:
            locale.setlocale(locale.LC_ALL, lang)
        else:
            locale.setlocale(locale.LC_ALL, (lang, encoding))
    except locale.Error:
        return False
    return True

Response: Set the locale used by a language code.


Instruction:
Can you create a Python 3 function that
sets the translation used by pywws modules.

Response:
def set_translation(lang):
    """Set the translation used by (some) pywws modules.

    This sets the translation object ``pywws.localisation.translation``
    to use a particular language.

    The ``lang`` parameter can be any string of the form ``en``,
    ``en_GB`` or ``en_GB.UTF-8``. Anything after a ``.`` character is
    ignored. In the case of a string such as ``en_GB``, the routine
    will search for an ``en_GB`` language file before searching for an
    ``en`` one.

    :param lang: language code.
    :type lang: string
    :return: success status.
    :rtype: bool

    """
    global translation
    # make list of possible languages, in order of preference
    langs = list()
    if lang:
        if '.' in lang:
            lang = lang.split('.')[0]
        langs += [lang, lang[:2]]
    # get translation object
    path = pkg_resources.resource_filename('pywws', 'lang')
    codeset = locale.getpreferredencoding()
    if codeset == 'ASCII':
        codeset = 'UTF-8'
    try:
        translation = gettext.translation(
            'pywws', path, languages=langs, codeset=codeset)
        # Python 3 translations don't have a ugettext method
        if not hasattr(translation, 'ugettext'):
            translation.ugettext = translation.gettext
    except IOError:
        return False
    return True


Instruction:
Can you create a Python 3 function that
sets the locale and translation for a pywws program.

Response:
def set_application_language(params):
    """Set the locale and translation for a pywws program.

    This function reads the language from the configuration file, then
    calls :func:`set_locale` and :func:`set_translation`.

    :param params: a :class:`pywws.storage.params` object.

    :type params: object

    """
    lang = params.get('config', 'language', None)
    if lang:
        set_locale(lang)
        set_translation(lang)


Instruction:
Explain what the following Python 3 code does
def read_data(self, size):
        result = list()
        while size > 0:
            count = min(size, 8)
            buf = self.hid.read(count)
            if len(buf) < count:
                raise IOError(
                    'pywws.device_cython_hidapi.USBDevice.read_data failed')
            result += buf
            size -= count
        return result

Response: Receive data from the device.


Instruction:
Can you create a Python 3 function that
sends data to the device.

Response:
def write_data(self, buf):
        """Send data to the device.

        :param buf: the data to send.

        :type buf: list(int)

        :return: success status.

        :rtype: bool

        """
        if self.hid.write(buf) != len(buf):
            raise IOError(
                'pywws.device_cython_hidapi.USBDevice.write_data failed')
        return True


Instruction:
Can you create a Python 3 function that
converts any timestamp to local time.

Response:
def to_local(self, dt):
        """Convert any timestamp to local time (with tzinfo)."""
        if dt.tzinfo is None:
            dt = dt.replace(tzinfo=self.utc)
        return dt.astimezone(self.local)


Instruction:
Create a Python 3 function for
converting any timestamp to UTC ( with tzinfo.

Response:
def to_utc(self, dt):
        """Convert any timestamp to UTC (with tzinfo)."""
        if dt.tzinfo is None:
            return dt.replace(tzinfo=self.utc)
        return dt.astimezone(self.utc)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_naive(self, dt):
        if dt.tzinfo is None:
            return dt
        return dt.astimezone(self.utc).replace(tzinfo=None)

Response: Convert any timestamp to pywws ( utc no tzinfo.


Instruction:
Can you tell what is the following Python 3 function doing
def local_replace(self, dt, use_dst=True, _recurse=False, **kwds):
        local_time = dt + self.standard_offset
        if use_dst:
            dst_offset = self.dst(local_time)
            if dst_offset:
                local_time += dst_offset
                adjusted_time = local_time.replace(**kwds)
                if adjusted_time > local_time and not _recurse:
                    return self.local_replace(
                        dt - DAY, use_dst=use_dst, _recurse=True, **kwds)
                adjusted_time -= dst_offset
                if self.dst(adjusted_time):
                    return adjusted_time - self.standard_offset
        adjusted_time = local_time.replace(**kwds)
        if use_dst:
            dst_offset = self.dst(adjusted_time)
            adjusted_time -= dst_offset
        if adjusted_time > local_time and not _recurse:
            return self.local_replace(
                dt - DAY, use_dst=use_dst, _recurse=True, **kwds)
        return adjusted_time - self.standard_offset

Response:
Return the pywws timestamp for the most recent
Recognizer local time before the datetime dt with datetime replace
        applied.


Instruction:
Make a summary of the following Python 3 code
def get(self, section, option, default=None):
        with self._lock:
            if not self._config.has_option(section, option):
                if default is not None:
                    self._set(section, option, default)
                return default
            return self._config.get(section, option)

Response: Get a parameter value and return a string.


Instruction:
Here you have a function in Python 3, explain what it does
def set(self, section, option, value):
        with self._lock:
            self._set(section, option, value)

Response: Set option in section to string value.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def unset(self, section, option):
        with self._lock:
            if not self._config.has_section(section):
                return
            if self._config.has_option(section, option):
                self._config.remove_option(section, option)
                self._dirty = True
            if not self._config.options(section):
                self._config.remove_section(section)
                self._dirty = True

Response: Removes an option from a section.


Instruction:
Can you generate the documentation for the following Python 3 function
def check_params(self, *keys):

        for key in keys:
            if not self.params[key]:
                raise RuntimeError('"{}" not set in weather.ini'.format(key))

Response: Ensure user has set required values in weather. ini.


Instruction:
How would you explain what the following Python 3 function does
def monitor(i):
    count = 0
    for x in i:
        count+=1
        if count % 10000 == 0:
            logger.info("%d records so far, current record is %s",
                count, x["idx"])
        yield x

Response:
Given an iterator yields data from it
    but prints progress every 10 000 records


Instruction:
Can you generate the documentation for the following Python 3 function
def calibrate_data(params, raw_data, calib_data):
    start = calib_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = raw_data.after(start + SECOND)
    if start is None:
        return start
    del calib_data[start:]
    calibrator = Calib(params, raw_data)
    def calibgen(inputdata):
        """Internal generator function"""
        count = 0
        for data in inputdata:
            idx = data['idx']
            count += 1
            if count % 10000 == 0:
                logger.info("calib: %s", idx.isoformat(' '))
            elif count % 500 == 0:
                logger.debug("calib: %s", idx.isoformat(' '))
            for key in ('rain', 'abs_pressure', 'temp_in'):
                if data[key] is None:
                    logger.error('Ignoring invalid data at %s', idx.isoformat(' '))
                    break
            else:
                yield calibrator.calib(data)
    calib_data.update(calibgen(raw_data[start:]))
    return start

Response: Calibrate raw data using a user - supplied function.


Instruction:
Explain what the following Python 3 code does
def generate_hourly(calib_data, hourly_data, process_from):
    start = hourly_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = calib_data.after(start + SECOND)
    if process_from:
        if start:
            start = min(start, process_from)
        else:
            start = process_from
    if start is None:
        return start
    # set start of hour in local time (not all time offsets are integer hours)
    start += timezone.standard_offset
    start = start.replace(minute=0, second=0)
    start -= timezone.standard_offset
    del hourly_data[start:]
    # preload pressure history, and find last valid rain
    prev = None
    pressure_history = deque()
    last_rain = None
    for data in calib_data[start - HOURx3:start]:
        if data['rel_pressure']:
            pressure_history.append((data['idx'], data['rel_pressure']))
        if data['rain'] is not None:
            last_rain = data['rain']
        prev = data
    # iterate over data in one hour chunks
    stop = calib_data.before(datetime.max)
    acc = HourAcc(last_rain)
    def hourlygen(inputdata, prev):
        """Internal generator function"""
        hour_start = start
        count = 0
        while hour_start <= stop:
            count += 1
            if count % 1008 == 0:
                logger.info("hourly: %s", hour_start.isoformat(' '))
            elif count % 24 == 0:
                logger.debug("hourly: %s", hour_start.isoformat(' '))
            hour_end = hour_start + HOUR
            acc.reset()
            for data in inputdata[hour_start:hour_end]:
                if data['rel_pressure']:
                    pressure_history.append((data['idx'], data['rel_pressure']))
                if prev:
                    err = data['idx'] - prev['idx']
                    if abs(err - timedelta(minutes=data['delay'])) > TIME_ERR:
                        logger.info('unexpected data interval %s %s',
                                    data['idx'].isoformat(' '), str(err))
                acc.add_raw(data)
                prev = data
            new_data = acc.result()
            if new_data and (new_data['idx'] - hour_start) >= timedelta(minutes=9):
                # compute pressure trend
                new_data['pressure_trend'] = None
                if new_data['rel_pressure']:
                    target = new_data['idx'] - HOURx3
                    while (len(pressure_history) >= 2 and
                           abs(pressure_history[0][0] - target) >
                           abs(pressure_history[1][0] - target)):
                        pressure_history.popleft()
                    if (pressure_history and
                            abs(pressure_history[0][0] - target) < HOUR):
                        new_data['pressure_trend'] = (
                            new_data['rel_pressure'] - pressure_history[0][1])
                # store new hourly data
                yield new_data
            hour_start = hour_end
    hourly_data.update(hourlygen(calib_data, prev))
    return start

Response: Generate hourly summaries from calibrated data.


Instruction:
Write a Python 3 script to
generate daily summaries from calibrated and hourly data.

Response:
def generate_daily(day_end_hour, use_dst,
                   calib_data, hourly_data, daily_data, process_from):
    """Generate daily summaries from calibrated and hourly data."""
    start = daily_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = calib_data.after(start + SECOND)
    if process_from:
        if start:
            start = min(start, process_from)
        else:
            start = process_from
    if start is None:
        return start
    # round to start of this day, in local time
    start = timezone.local_replace(
        start, use_dst=use_dst, hour=day_end_hour, minute=0, second=0)
    del daily_data[start:]
    stop = calib_data.before(datetime.max)
    acc = DayAcc()
    def dailygen(inputdata):
        """Internal generator function"""
        day_start = start
        count = 0
        while day_start <= stop:
            count += 1
            if count % 30 == 0:
                logger.info("daily: %s", day_start.isoformat(' '))
            else:
                logger.debug("daily: %s", day_start.isoformat(' '))
            day_end = day_start + DAY
            if use_dst:
                # day might be 23 or 25 hours long
                day_end = timezone.local_replace(
                    day_end + HOURx3, use_dst=use_dst, hour=day_end_hour)
            acc.reset()
            for data in inputdata[day_start:day_end]:
                acc.add_raw(data)
            for data in hourly_data[day_start:day_end]:
                acc.add_hourly(data)
            new_data = acc.result()
            if new_data:
                new_data['start'] = day_start
                yield new_data
            day_start = day_end
    daily_data.update(dailygen(calib_data))
    return start


Instruction:
Here you have a function in Python 3, explain what it does
def generate_monthly(rain_day_threshold, day_end_hour, use_dst,
                     daily_data, monthly_data, process_from):
    """Generate monthly summaries from daily data."""
    start = monthly_data.before(datetime.max)
    if start is None:
        start = datetime.min
    start = daily_data.after(start + SECOND)
    if process_from:
        if start:
            start = min(start, process_from)
        else:
            start = process_from
    if start is None:
        return start
    # set start to start of first day of month (local time)
    start = timezone.local_replace(
        start, use_dst=use_dst, day=1, hour=day_end_hour, minute=0, second=0)
    if day_end_hour >= 12:
        # month actually starts on the last day of previous month
        start -= DAY
    del monthly_data[start:]
    stop = daily_data.before(datetime.max)
    if stop is None:
        return None
    acc = MonthAcc(rain_day_threshold)
    def monthlygen(inputdata):
        """Internal generator function"""
        month_start = start
        count = 0
        while month_start <= stop:
            count += 1
            if count % 12 == 0:
                logger.info("monthly: %s", month_start.isoformat(' '))
            else:
                logger.debug("monthly: %s", month_start.isoformat(' '))
            month_end = month_start + WEEK
            if month_end.month < 12:
                month_end = month_end.replace(month=month_end.month+1)
            else:
                month_end = month_end.replace(month=1, year=month_end.year+1)
            month_end = month_end - WEEK
            if use_dst:
                # month might straddle summer time start or end
                month_end = timezone.local_replace(
                    month_end + HOURx3, use_dst=use_dst, hour=day_end_hour)
            acc.reset()
            for data in inputdata[month_start:month_end]:
                acc.add_daily(data)
            new_data = acc.result()
            if new_data:
                new_data['start'] = month_start
                yield new_data
            month_start = month_end
    monthly_data.update(monthlygen(daily_data))
    return start

Response: Generate monthly summaries from daily data.


Instruction:
Here you have a function in Python 3, explain what it does
def process_data(context):
    logger.info('Generating summary data')
    # get time of last record
    last_raw = context.raw_data.before(datetime.max)
    if last_raw is None:
        raise IOError('No data found. Check data directory parameter.')
    # get daytime end hour (in local time)
    day_end_hour, use_dst = get_day_end_hour(context.params)
    # get other config
    rain_day_threshold = float(
        context.params.get('config', 'rain day threshold', '0.2'))
    # calibrate raw data
    start = calibrate_data(context.params, context.raw_data, context.calib_data)
    # generate hourly data
    start = generate_hourly(context.calib_data, context.hourly_data, start)
    # generate daily data
    start = generate_daily(day_end_hour, use_dst,
                           context.calib_data, context.hourly_data, context.daily_data, start)
    # generate monthly data
    generate_monthly(rain_day_threshold, day_end_hour, use_dst,
                     context.daily_data, context.monthly_data, start)
    return 0

Response: Generate summaries from raw weather station data.


Instruction:
Write a Python 3 function for
receiving data from the device.

Response:
def read_data(self, size):
        """Receive data from the device.

        If the read fails for any reason, an :obj:`IOError` exception
        is raised.

        :param size: the number of bytes to read.

        :type size: int

        :return: the data received.

        :rtype: list(int)

        """
        result = self.dev.bulkRead(0x81, size, timeout=1200)
        if not result or len(result) < size:
            raise IOError('pywws.device_libusb1.USBDevice.read_data failed')
        # Python2 libusb1 version 1.5 and earlier returns a string
        if not isinstance(result[0], int):
            result = map(ord, result)
        return list(result)


Instruction:
Write a Python 3 script to
send data to the device.

Response:
def write_data(self, buf):
        """Send data to the device.

        If the write fails for any reason, an :obj:`IOError` exception
        is raised.

        :param buf: the data to send.

        :type buf: list(int)

        :return: success status.

        :rtype: bool

        """
        if sys.version_info[0] < 3:
            str_buf = ''.join(map(chr, buf))
        else:
            str_buf = bytes(buf)
        result = self.dev.controlWrite(
            libusb1.LIBUSB_ENDPOINT_OUT | libusb1.LIBUSB_TYPE_CLASS |
            libusb1.LIBUSB_RECIPIENT_INTERFACE,
            libusb1.LIBUSB_REQUEST_SET_CONFIGURATION,
            0x200, 0, str_buf, timeout=50)
        if result != len(buf):
            raise IOError('pywws.device_libusb1.USBDevice.write_data failed')
        return True


Instruction:
Given the following Python 3 function, write the documentation
def before(self, idx):
        if not isinstance(idx, datetime):
            raise TypeError("'%s' is not %s" % (idx, datetime))
        day = min(idx.date(), self._hi_limit - DAY)
        while day >= self._lo_limit:
            if day < self._rd_cache.lo or day >= self._rd_cache.hi:
                self._load(self._rd_cache, day)
            self._rd_cache.set_ptr(idx)
            if self._rd_cache.ptr > 0:
                return self._rd_cache.data[self._rd_cache.ptr - 1]['idx']
            day = self._rd_cache.lo - DAY
        return None

Response:
Return datetime of newest existing data record whose
        datetime is < idx.


Instruction:
Can you generate the documentation for the following Python 3 function
def after(self, idx):
        if not isinstance(idx, datetime):
            raise TypeError("'%s' is not %s" % (idx, datetime))
        day = max(idx.date(), self._lo_limit)
        while day < self._hi_limit:
            if day < self._rd_cache.lo or day >= self._rd_cache.hi:
                self._load(self._rd_cache, day)
            self._rd_cache.set_ptr(idx)
            if self._rd_cache.ptr < len(self._rd_cache.data):
                return self._rd_cache.data[self._rd_cache.ptr]['idx']
            day = self._rd_cache.hi
        return None

Response:
Return datetime of oldest existing data record whose
        datetime is < idx.


Instruction:
How would you explain what the following Python 3 function does
def nearest(self, idx):
        hi = self.after(idx)
        lo = self.before(idx)
        if hi is None:
            return lo
        if lo is None:
            return hi
        if abs(hi - idx) < abs(lo - idx):
            return hi
        return lo

Response: Return datetime of record whose datetime is nearest idx.


Instruction:
Write a Python 3 script to
clear all data from the data store permanently

Response:
def clear(self):
        """Clears all data from the data store permanently"""
        for root, dirs, files in os.walk(self._root_dir, topdown=False):
            for file in files:
                os.unlink(os.path.join(root, file))
            os.rmdir(root)
        # Get the root dir back and re-initialise to start again
        root_dir = os.path.abspath(
            os.path.join(self._root_dir, os.pardir))
        self.__init__(root_dir)


Instruction:
Can you create a Python 3 function that
receives data from the device.

Response:
def read_data(self, size):
        """Receive data from the device.

        If the read fails for any reason, an :obj:`IOError` exception
        is raised.

        :param size: the number of bytes to read.

        :type size: int

        :return: the data received.

        :rtype: list(int)

        """
        result = self.dev.read(0x81, size, timeout=1200)
        if not result or len(result) < size:
            raise IOError('pywws.device_pyusb1.USBDevice.read_data failed')
        return list(result)


Instruction:
Write a Python 3 function for
sending data to the device.

Response:
def write_data(self, buf):
        """Send data to the device.

        If the write fails for any reason, an :obj:`IOError` exception
        is raised.

        :param buf: the data to send.

        :type buf: list(int)

        :return: success status.

        :rtype: bool

        """
        bmRequestType = usb.util.build_request_type(
            usb.util.ENDPOINT_OUT,
            usb.util.CTRL_TYPE_CLASS,
            usb.util.CTRL_RECIPIENT_INTERFACE
            )
        result = self.dev.ctrl_transfer(
            bmRequestType=bmRequestType,
            bRequest=usb.REQ_SET_CONFIGURATION,
            data_or_wLength=buf,
            wValue=0x200,
            timeout=50)
        if result != len(buf):
            raise IOError('pywws.device_pyusb1.USBDevice.write_data failed')
        return True


Instruction:
Can you generate the documentation for the following Python 3 function
def pressure_trend_text(trend):
    _ = pywws.localisation.translation.ugettext
    if trend > 6.0:
        return _(u'rising very rapidly')
    elif trend > 3.5:
        return _(u'rising quickly')
    elif trend > 1.5:
        return _(u'rising')
    elif trend >= 0.1:
        return _(u'rising slowly')
    elif trend < -6.0:
        return _(u'falling very rapidly')
    elif trend < -3.5:
        return _(u'falling quickly')
    elif trend < -1.5:
        return _(u'falling')
    elif trend <= -0.1:
        return _(u'falling slowly')
    return _(u'steady')

Response:
Convert pressure trend to a string as used by the UK met
    office.


Instruction:
Explain what the following Python 3 code does
def winddir_average(data, threshold, min_count, decay=1.0):
    wind_filter = pywws.process.WindFilter()
    count = 0
    for item in data:
        wind_filter.add(item)
        if item['wind_dir'] is not None:
            count += 1
    if count < min_count:
        return None
    speed, direction = wind_filter.result()
    if speed is None or speed < threshold:
        return None
    return direction * 22.5

Response: Compute the average wind direction for a slice of data.


Instruction:
Given the following Python 3 function, write the documentation
def winddir_text(pts):
    "Convert wind direction from 0..15 to compass point text"
    global _winddir_text_array
    if pts is None:
        return None
    if not isinstance(pts, int):
        pts = int(pts + 0.5) % 16
    if not _winddir_text_array:
        _ = pywws.localisation.translation.ugettext
        _winddir_text_array = (
            _(u'N'), _(u'NNE'), _(u'NE'), _(u'ENE'),
            _(u'E'), _(u'ESE'), _(u'SE'), _(u'SSE'),
            _(u'S'), _(u'SSW'), _(u'SW'), _(u'WSW'),
            _(u'W'), _(u'WNW'), _(u'NW'), _(u'NNW'),
            )
    return _winddir_text_array[pts]

Response: Convert wind direction from 0.. 15 to compass point text


Instruction:
Here you have a function in Python 3, explain what it does
def wind_bft(ms):
    "Convert wind from metres per second to Beaufort scale"
    if ms is None:
        return None
    for bft in range(len(_bft_threshold)):
        if ms < _bft_threshold[bft]:
            return bft
    return len(_bft_threshold)

Response: Convert wind from metres per second to Beaufort scale


Instruction:
How would you explain what the following Python 3 function does
def dew_point(temp, hum):
    if temp is None or hum is None:
        return None
    a = 17.27
    b = 237.7
    gamma = ((a * temp) / (b + temp)) + math.log(float(hum) / 100.0)
    return (b * gamma) / (a - gamma)

Response:
Compute dew point using formula from
    http://en. wikidata. org.


Instruction:
Can you tell what is the following Python 3 function doing
def cadhumidex(temp, humidity):
    "Calculate Humidity Index as per Canadian Weather Standards"
    if temp is None or humidity is None:
        return None
    # Formulas are adapted to not use e^(...) with no appreciable
    # change in accuracy (0.0227%)
    saturation_pressure = (6.112 * (10.0**(7.5 * temp / (237.7 + temp))) *
                           float(humidity) / 100.0)
    return temp + (0.555 * (saturation_pressure - 10.0))

Response: Calculate Humidity Index as per Canadian Weather Standards


Instruction:
How would you explain what the following Python 3 function does
def usaheatindex(temp, humidity, dew=None):
    if temp is None or humidity is None:
        return None
    if dew is None:
        dew = dew_point(temp, humidity)
    if temp < 26.7 or humidity < 40 or dew < 12.0:
        return temp
    T = (temp * 1.8) + 32.0
    R = humidity
    c_1 = -42.379
    c_2 = 2.04901523
    c_3 = 10.14333127
    c_4 = -0.22475541
    c_5 = -0.00683783
    c_6 = -0.05481717
    c_7 = 0.00122874
    c_8 = 0.00085282
    c_9 = -0.00000199
    return ((c_1 + (c_2 * T) + (c_3 * R) + (c_4 * T * R) + (c_5 * (T**2)) +
             (c_6 * (R**2)) + (c_7 * (T**2) * R) + (c_8 * T * (R**2)) +
             (c_9 * (T**2) * (R**2))) - 32.0) / 1.8

Response: Calculate the Heat Index as per USA National Weather Service Standards


Instruction:
Explain what the following Python 3 code does
def wind_chill(temp, wind):
    if temp is None or wind is None:
        return None
    wind_kph = wind * 3.6
    if wind_kph <= 4.8 or temp > 10.0:
        return temp
    return min(13.12 + (temp * 0.6215) +
               (((0.3965 * temp) - 11.37) * (wind_kph ** 0.16)),
               temp)

Response:
Compute wind chill using formula from
    http://en. wikidata. org.


Instruction:
Can you write a function in Python 3 where it
computes apparent temperature using formula from http://www. omgov. au / info / thermal_stress /

Response:
def apparent_temp(temp, rh, wind):
    """Compute apparent temperature (real feel), using formula from
    http://www.bom.gov.au/info/thermal_stress/

    """
    if temp is None or rh is None or wind is None:
        return None
    vap_press = (float(rh) / 100.0) * 6.105 * math.exp(
        17.27 * temp / (237.7 + temp))
    return temp + (0.33 * vap_press) - (0.70 * wind) - 4.00


Instruction:
Can you implement a function in Python 3 that
calculates cumulus cloud base in metres using formula from https://en. wikidata. org.

Response:
def cloud_base(temp, hum):
    """Calculate cumulus cloud base in metres, using formula from
    https://en.wikipedia.org/wiki/Cloud_base or
    https://de.wikipedia.org/wiki/Kondensationsniveau#Konvektionskondensationsniveau
    """
    if temp is None or hum is None:
        return None
    dew_pt = dew_point(temp, hum)
    spread = float(temp) - dew_pt
    return spread * 125.0


Instruction:
Can you generate the documentation for the following Python 3 function
def upload(training_dir, algorithm_id=None, writeup=None, api_key=None, ignore_open_monitors=False):

    if not ignore_open_monitors:
        open_monitors = monitoring._open_monitors()
        if len(open_monitors) > 0:
            envs = [m.env.spec.id if m.env.spec else '(unknown)' for m in open_monitors]
            raise error.Error("Still have an open monitor on {}. You must run 'env.monitor.close()' before uploading.".format(', '.join(envs)))

    env_info, training_episode_batch, training_video = upload_training_data(training_dir, api_key=api_key)
    env_id = env_info['env_id']
    training_episode_batch_id = training_video_id = None
    if training_episode_batch:
        training_episode_batch_id = training_episode_batch.id
    if training_video:
        training_video_id = training_video.id

    if logger.level <= logging.INFO:
        if training_episode_batch_id is not None and training_video_id is not None:
            logger.info('[%s] Creating evaluation object from %s with learning curve and training video', env_id, training_dir)
        elif training_episode_batch_id is not None:
            logger.info('[%s] Creating evaluation object from %s with learning curve', env_id, training_dir)
        elif training_video_id is not None:
            logger.info('[%s] Creating evaluation object from %s with training video', env_id, training_dir)
        else:
            raise error.Error("[%s] You didn't have any recorded training data in {}. Once you've used 'env.monitor.start(training_dir)' to start recording, you need to actually run some rollouts. Please join the community chat on https://gym.openai.com if you have any issues.".format(env_id, training_dir))

    evaluation = resource.Evaluation.create(
        training_episode_batch=training_episode_batch_id,
        training_video=training_video_id,
        env=env_info['env_id'],
        algorithm={
            'id': algorithm_id,
        },
        writeup=writeup,
        gym_version=env_info['gym_version'],
        api_key=api_key,
# >>>>>>>>> START changes >>>>>>>>>>>>>>>>>>>>>>>>
        env_info=env_info,
# <<<<<<<<< END changes <<<<<<<<<<<<<<<<<<<<<<<<<<
    )

    logger.info(

    """
****************************************************
You successfully uploaded your evaluation on %s to
OpenAI Gym! You can find it at:

    %s

****************************************************
    """.rstrip(), env_id, evaluation.web_url())

    return evaluation

Response: Uploads the training data to the environment and returns the training data.


Instruction:
Here you have a function in Python 3, explain what it does
def load_user_envs(self):
        installed_packages = self._list_packages()

        # Tagging core envs
        gym_package = 'gym ({})'.format(installed_packages['gym']) if 'gym' in installed_packages else 'gym'
        core_specs = registry.all()
        for spec in core_specs:
            spec.source = 'OpenAI Gym Core Package'
            spec.package = gym_package

        # Loading user envs
        if not os.path.isfile(self.cache_path):
            return
        with open(self.cache_path) as cache:
            for line in cache:
                user_package, registered_envs = self._load_package(line.rstrip('\n'), installed_packages)
                if logger.level <= logging.DEBUG:
                    logger.debug('Installed %d user environments from package "%s"', len(registered_envs), user_package['name'])
        if self.cache_needs_update:
            self._update_cache()
        if len(self.env_ids) > 0:
            logger.info('Found and registered %d user environments.', len(self.env_ids))

Response: Loads downloaded user environments from filesystem cache on import gym


Instruction:
Can you create a Python 3 function that
downloads and register a user environment from a git repository

Response:
def pull(self, source=''):
        """
        Downloads and registers a user environment from a git repository
        Args:
            source: the source where to download the envname (expected 'github.com/user/repo[@branch]')

        Note: the user environment will be registered as (username/EnvName-vVersion)
        """
        # Checking syntax
        branch_parts = source.split('@')
        if len(branch_parts) == 2:
            branch = branch_parts[1]
            source = branch_parts[0]
            git_url = 'https://{}.git@{}'.format(source, branch)
        else:
            git_url = 'https://{}.git'.format(source)

        # Validating params
        source_parts = source.split('/')
        if len(source_parts) != 3 or source_parts[0].lower() != 'github.com':
            logger.warn(""" Invalid Syntax - source must be in the format 'github.com/username/repository[@branch]'

Syntax: gym.pull('github.com/username/repository')

where username is a GitHub username, repository is the name of a GitHub repository.""")
            return

        username = source_parts[1]
        modified_packages = []

        # Installing pip package
        logger.info('Installing pip package from "%s"', git_url)
        packages_before = self._list_packages()
        return_code = self._run_cmd('{} install --upgrade git+{}'.format(pip_exec, git_url))
        if return_code != 0:        # Failed - pip will display the error message
            return

        # Detecting new and upgraded packages
        packages_after = self._list_packages()
        for package_name in packages_after:
            package_version = packages_after[package_name]
            if package_name not in packages_before:
                logger.info('Installed new package: "%s (%s)"', package_name, package_version)
                modified_packages.append(package_name)

            elif LooseVersion(packages_before[package_name]) < LooseVersion(package_version):
                logger.info('Upgraded package "%s" from "%s" to "%s"',
                            package_name, packages_before[package_name], package_version)
                modified_packages.append(package_name)

            elif LooseVersion(packages_before[package_name]) > LooseVersion(package_version):
                logger.warn('Package "%s" downgraded from "%s" to "%s". Are you sure that is what you want?',
                            package_name, packages_before[package_name], package_version)
                modified_packages.append(package_name)

        # Package conflict - check if already installed from a different source
        for package_name in modified_packages:
            if package_name in self.user_packages and source != self.user_packages[package_name]['source']:
                logger.warn('Package conflict - The package "%s" from "%s" was already installed from "%s". '
                            'Uninstalling both packages. Please reinstall the one you want.',
                            package_name, source, self.user_packages[package_name]['source'])
                self._deregister_envs_from_source(source)
                self._deregister_envs_from_source(self.user_packages[package_name]['source'])
                self._run_cmd('{} uninstall -y {}'.format(pip_exec, package_name))
                del self.user_packages[package_name]
                self._update_cache()
                return

        # Detecting if already up-to-date
        if len(modified_packages) == 0:
            logger.warn('The user environments for "%s" are already up-to-date (no new version detected).', source)
            return

        # De-register envs with same source
        self._deregister_envs_from_source(source)

        # Loading new packages
        new_envs = set([])
        uninstall_packages = []
        for package_name in modified_packages:
            json_line = json.dumps({'name': package_name, 'version': packages_after[package_name], 'source': source})
            user_package, registered_envs = self._load_package(json_line, packages_after)
            for new_env in registered_envs:
                if not new_env.lower().startswith('{}/'.format(username.lower())):
                    if len(uninstall_packages) == 0:    # We don't need to repeat the message multiple times
                        logger.warn('This package does not respect the naming convention and will be uninstalled to avoid conflicts. '
                                    'Expected user environment to start with "{}/", but got "{}" instead.'.format(username, new_env))
                    uninstall_packages.append(package_name)
            new_envs = new_envs | registered_envs

        # Removing packages and deregistering envs if they don't respect naming convention
        if len(uninstall_packages) > 0:
            self._deregister_envs_from_source(source)
            for package_name in uninstall_packages:
                self._run_cmd('{} uninstall -y {}'.format(pip_exec, package_name))
            return

        # Updating cache
        self._update_cache()

        # Displaying results
        logger.info('--------------------------------------------------')
        if len(new_envs) > 0:
            for env in sorted(new_envs, key=lambda s: s.lower()):
                logger.info('Successfully registered the environment: "%s"', env)
        else:
            logger.info('No environments have been registered. The following packages were modified: %s', ','.join(modified_packages))
        return


Instruction:
Can you generate the documentation for the following Python 3 function
def _load_package(self, json_line, installed_packages):
        if len(json_line) == 0:
            return {}, set([])

        valid_json = False
        try:
            user_package = json.loads(json_line)
            valid_json = True
        except ValueError:
            user_package = {}

        package_name = user_package['name'] if 'name' in user_package else None
        module_name = package_name.replace('-', '_') if package_name is not None else ''
        envs_before = set(registry.list())

        if not valid_json or package_name is None:
            self.cache_needs_update = True
            logger.warn('Unable to load user environments. Try deleting your cache '
                        'file "%s" if this problem persists. \n\nLine: %s', self.cache_path, json_line)
            return {}, set([])
        elif package_name not in installed_packages:
            self.cache_needs_update = True
            logger.warn('The package "%s" does not seem to be installed anymore. User environments from this '
                        'package will not be registered, and the package will no longer be loaded on `import gym`', package_name)
        elif module_name in sys.modules:
            self.cache_needs_update = True
            try:
                reload_module(sys.modules[module_name])
            except ImportError:
                if 'gym' in package_name:   # To avoid uninstalling failing dependencies
                    logger.warn('Unable to reload the module "%s" from package "%s" (%s). This is usually caused by a '
                                'invalid pip package. The package will be uninstalled and no longer be loaded on `import gym`.\n',
                                module_name, package_name, installed_packages[package_name])
                    traceback.print_exc(file=sys.stdout)
                    sys.stdout.write('\n')
                    self._run_cmd('{} uninstall -y {}'.format(pip_exec, package_name))
        else:
            try:
                __import__(module_name)
            except ImportError:
                if 'gym' in package_name:   # To avoid uninstalling failing dependencies
                    self.cache_needs_update = True
                    logger.warn('Unable to import the module "%s" from package "%s" (%s). This is usually caused by a '
                                'invalid pip package. The package will be uninstalled and no longer be loaded on `import gym`.\n',
                                module_name, package_name, installed_packages[package_name])
                    traceback.print_exc(file=sys.stdout)
                    sys.stdout.write('\n')
                    self._run_cmd('{} uninstall -y {}'.format(pip_exec, package_name))

        envs_after = set(registry.list())
        registered_envs = envs_after - envs_before
        if len(registered_envs) > 0:
            self.user_packages[package_name] = user_package
        for new_env in registered_envs:
            new_spec = registry.spec(new_env)
            new_spec.source = user_package['source']
            new_spec.package = '{} ({})'.format(user_package['name'], user_package['version'])
            self.env_ids.add(new_env.lower())
        return user_package, registered_envs

Response: Load a user package from a JSON file and return the user_package name version source list of environments registered when the package was loaded.


Instruction:
Explain what the following Python 3 code does
def make(self):
        if self._entry_point is None:
            raise error.Error('Attempting to make deprecated env {}. (HINT: is there a newer registered version of this env?)'.format(self.id))

        cls = load(self._entry_point)
        env = cls(**self._kwargs)

        # Make the enviroment aware of which spec it came from.
        env.spec = self
        env = env.build(extra_wrappers=self._wrappers)

        return env

Response: Instantiates an instance of the environment with appropriate kwargs


Instruction:
Create a Python 3 function for
parsing a path into hosts and path tuple.

Response:
def _parse_path(self, path):
        """Return (hosts, path) tuple"""
        # Support specifying another host via hdfs://host:port/path syntax
        # We ignore the scheme and piece together the query and fragment
        # Note that HDFS URIs are not URL encoded, so a '?' or a '#' in the URI is part of the
        # path
        parts = urlsplit(path, allow_fragments=False)
        if not parts.path.startswith('/'):
            raise ValueError("Path must be absolute, was given {}".format(path))
        if parts.scheme not in ('', 'hdfs', 'hftp', 'webhdfs'):
            warnings.warn("Unexpected scheme {}".format(parts.scheme))
        assert not parts.fragment
        path = parts.path
        if parts.query:
            path += '?' + parts.query
        if parts.netloc:
            hosts = self._parse_hosts(parts.netloc)
        else:
            hosts = self.hosts
        return hosts, path


Instruction:
Explain what the following Python 3 code does
def _record_last_active(self, host):
        if host in self.hosts:  # this check is for when user passes a host at request time
            # Keep this thread safe: set hosts atomically and update it before the timestamp
            self.hosts = [host] + [h for h in self.hosts if h != host]
            self._last_time_recorded_active = time.time()

Response: Record the last active entry in our hosts list.


Instruction:
Can you implement a function in Python 3 that
makes a request against the NameNodes WebHDFS server.

Response:
def _request(self, method, path, op, expected_status=httplib.OK, **kwargs):
        """Make a WebHDFS request against the NameNodes

        This function handles NameNode failover and error checking.
        All kwargs are passed as query params to the WebHDFS server.
        """
        hosts, path = self._parse_path(path)
        _transform_user_name_key(kwargs)
        kwargs.setdefault('user.name', self.user_name)

        formatted_args = ' '.join('{}={}'.format(*t) for t in kwargs.items())
        _logger.info("%s %s %s %s", op, path, formatted_args, ','.join(hosts))
        kwargs['op'] = op
        for i in range(self.max_tries):
            log_level = logging.DEBUG if i < self.max_tries - 1 else logging.WARNING
            for host in hosts:
                try:
                    response = self._requests_session.request(
                        method,
                        'http://{}{}{}'.format(host, WEBHDFS_PATH, url_quote(path.encode('utf-8'))),
                        params=kwargs, timeout=self.timeout, allow_redirects=False,
                        **self._requests_kwargs
                    )
                except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
                    _logger.log(log_level, "Failed to reach to %s (attempt %d/%d)",
                                host, i + 1, self.max_tries, exc_info=True)
                    continue
                try:
                    _check_response(response, expected_status)
                except (HdfsRetriableException, HdfsStandbyException):
                    _logger.log(log_level, "%s is in startup or standby mode (attempt %d/%d)",
                                host, i + 1, self.max_tries, exc_info=True)
                    continue
                # Note: standby NN can still return basic validation errors, so non-StandbyException
                # does not necessarily mean we have the active NN.
                self._record_last_active(host)
                return response
            if i != self.max_tries - 1:
                time.sleep(self.retry_delay)
        raise HdfsNoServerException("Could not use any of the given hosts")


Instruction:
Here you have a function in Python 3, explain what it does
def create(self, path, data, **kwargs):
        metadata_response = self._put(
            path, 'CREATE', expected_status=httplib.TEMPORARY_REDIRECT, **kwargs)
        assert not metadata_response.content
        data_response = self._requests_session.put(
            metadata_response.headers['location'], data=data, **self._requests_kwargs)
        _check_response(data_response, expected_status=httplib.CREATED)
        assert not data_response.content

Response: Create a file at the given path.


Instruction:
How would you explain what the following Python 3 function does
def append(self, path, data, **kwargs):
        metadata_response = self._post(
            path, 'APPEND', expected_status=httplib.TEMPORARY_REDIRECT, **kwargs)
        data_response = self._requests_session.post(
            metadata_response.headers['location'], data=data, **self._requests_kwargs)
        _check_response(data_response)
        assert not data_response.content

Response: Append to the given file.


Instruction:
Implement a function in Python 3 to
concatenate existing files together.

Response:
def concat(self, target, sources, **kwargs):
        """Concat existing files together.

        For preconditions, see
        https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/filesystem/filesystem.html#void_concatPath_p_Path_sources

        :param target: the path to the target destination.
        :param sources: the paths to the sources to use for the concatenation.
        :type sources: list
        """
        if isinstance(sources, basestring):
            raise ValueError("sources should be a list")
        if any(',' in s for s in sources):
            raise NotImplementedError("WebHDFS does not support commas in concat")
        response = self._post(target, 'CONCAT', sources=','.join(sources), **kwargs)
        assert not response.content


Instruction:
Explain what the following Python 3 code does
def open(self, path, **kwargs):
        metadata_response = self._get(
            path, 'OPEN', expected_status=httplib.TEMPORARY_REDIRECT, **kwargs)
        data_response = self._requests_session.get(
            metadata_response.headers['location'], stream=True, **self._requests_kwargs)
        _check_response(data_response)
        return data_response.raw

Response: Open a file - like object for reading the given HDFS path.


Instruction:
Given the following Python 3 function, write the documentation
def create_symlink(self, link, destination, **kwargs):
        response = self._put(link, 'CREATESYMLINK', destination=destination, **kwargs)
        assert not response.content

Response: Create a symbolic link at link pointing to destination.


Instruction:
Explain what the following Python 3 code does
def rename(self, path, destination, **kwargs):
        return _json(self._put(path, 'RENAME', destination=destination, **kwargs))['boolean']

Response: Renames Path src to Path dst.


Instruction:
Can you write a function in Python 3 where it
returns a FileStatus object that represents the path.

Response:
def get_file_status(self, path, **kwargs):
        """Return a :py:class:`FileStatus` object that represents the path."""
        return FileStatus(**_json(self._get(path, 'GETFILESTATUS', **kwargs))['FileStatus'])


Instruction:
Implement a Python 3 function for
listing the statuses of the files and directories in the given path.

Response:
def list_status(self, path, **kwargs):
        """List the statuses of the files/directories in the given path if the path is a directory.

        :rtype: ``list`` of :py:class:`FileStatus` objects
        """
        return [
            FileStatus(**item) for item in
            _json(self._get(path, 'LISTSTATUS', **kwargs))['FileStatuses']['FileStatus']
        ]


Instruction:
Explain what the following Python 3 code does
def get_content_summary(self, path, **kwargs):
        return ContentSummary(
            **_json(self._get(path, 'GETCONTENTSUMMARY', **kwargs))['ContentSummary'])

Response: Return the ContentSummary of a given Path.


Instruction:
Write a Python 3 function that can
get the checksum of a file.

Response:
def get_file_checksum(self, path, **kwargs):
        """Get the checksum of a file.

        :rtype: :py:class:`FileChecksum`
        """
        metadata_response = self._get(
            path, 'GETFILECHECKSUM', expected_status=httplib.TEMPORARY_REDIRECT, **kwargs)
        assert not metadata_response.content
        data_response = self._requests_session.get(
            metadata_response.headers['location'], **self._requests_kwargs)
        _check_response(data_response)
        return FileChecksum(**_json(data_response)['FileChecksum'])


Instruction:
How would you explain what the following Python 3 function does
def set_permission(self, path, **kwargs):
        response = self._put(path, 'SETPERMISSION', **kwargs)
        assert not response.content

Response: Set the permission of a file or directory.


Instruction:
Implement a function in Python 3 to
set the owner of a file or directory.

Response:
def set_owner(self, path, **kwargs):
        """Set owner of a path (i.e. a file or a directory).

        The parameters owner and group cannot both be null.

        :param owner: user
        :param group: group
        """
        response = self._put(path, 'SETOWNER', **kwargs)
        assert not response.content


Instruction:
Write a Python 3 function that can
set the modification time and accesstime of a file.

Response:
def set_times(self, path, **kwargs):
        """Set access time of a file.

        :param modificationtime: Set the modification time of this file. The number of milliseconds
            since Jan 1, 1970.
        :type modificationtime: long
        :param accesstime: Set the access time of this file. The number of milliseconds since Jan 1
            1970.
        :type accesstime: long
        """
        response = self._put(path, 'SETTIMES', **kwargs)
        assert not response.content


Instruction:
Can you write a function in Python 3 where it
sets an xattr of a file or directory.

Response:
def set_xattr(self, path, xattr_name, xattr_value, flag, **kwargs):
        """Set an xattr of a file or directory.

        :param xattr_name: The name must be prefixed with the namespace followed by ``.``. For
            example, ``user.attr``.
        :param flag: ``CREATE`` or ``REPLACE``
        """
        kwargs['xattr.name'] = xattr_name
        kwargs['xattr.value'] = xattr_value
        response = self._put(path, 'SETXATTR', flag=flag, **kwargs)
        assert not response.content


Instruction:
Implement a function in Python 3 to
remove an xattr of a file or directory.

Response:
def remove_xattr(self, path, xattr_name, **kwargs):
        """Remove an xattr of a file or directory."""
        kwargs['xattr.name'] = xattr_name
        response = self._put(path, 'REMOVEXATTR', **kwargs)
        assert not response.content


Instruction:
Write a Python 3 function that can
get one or more xattr values for a file or directory.

Response:
def get_xattrs(self, path, xattr_name=None, encoding='text', **kwargs):
        """Get one or more xattr values for a file or directory.

        :param xattr_name: ``str`` to get one attribute, ``list`` to get multiple attributes,
            ``None`` to get all attributes.
        :param encoding: ``text`` | ``hex`` | ``base64``, defaults to ``text``

        :returns: Dictionary mapping xattr name to value. With text encoding, the value will be a
            unicode string. With hex or base64 encoding, the value will be a byte array.
        :rtype: dict
        """
        kwargs['xattr.name'] = xattr_name
        json = _json(self._get(path, 'GETXATTRS', encoding=encoding, **kwargs))['XAttrs']
        # Decode the result
        result = {}
        for attr in json:
            k = attr['name']
            v = attr['value']
            if v is None:
                result[k] = None
            elif encoding == 'text':
                assert attr['value'].startswith('"') and attr['value'].endswith('"')
                result[k] = v[1:-1]
            elif encoding == 'hex':
                assert attr['value'].startswith('0x')
                # older python demands bytes, so we have to ascii encode
                result[k] = binascii.unhexlify(v[2:].encode('ascii'))
            elif encoding == 'base64':
                assert attr['value'].startswith('0s')
                # older python demands bytes, so we have to ascii encode
                result[k] = base64.b64decode(v[2:].encode('ascii'))
            else:
                warnings.warn("Unexpected encoding {}".format(encoding))
                result[k] = v
        return result


Instruction:
How would you implement a function in Python 3 that
gets all of the xattr names for a file or directory.

Response:
def list_xattrs(self, path, **kwargs):
        """Get all of the xattr names for a file or directory.

        :rtype: list
        """
        return simplejson.loads(_json(self._get(path, 'LISTXATTRS', **kwargs))['XAttrNames'])


Instruction:
Here you have a function in Python 3, explain what it does
def delete_snapshot(self, path, snapshotname, **kwargs):
        response = self._delete(path, 'DELETESNAPSHOT', snapshotname=snapshotname, **kwargs)
        assert not response.content

Response: Delete a snapshot of a directory


Instruction:
Here you have a function in Python 3, explain what it does
def listdir(self, path, **kwargs):
        statuses = self.list_status(path, **kwargs)
        if len(statuses) == 1 and statuses[0].pathSuffix == '' and statuses[0].type == 'FILE':
            raise NotADirectoryError('Not a directory: {!r}'.format(path))
        return [f.pathSuffix for f in statuses]

Response: Return a list containing names of files in the given path


Instruction:
Write a Python 3 function that can
return True if the given path exists

Response:
def exists(self, path, **kwargs):
        """Return true if the given path exists"""
        try:
            self.get_file_status(path, **kwargs)
            return True
        except HdfsFileNotFoundException:
            return False


Instruction:
Write a Python 3 script for
seeing ``os.walk`` for documentation

Response:
def walk(self, top, topdown=True, onerror=None, **kwargs):
        """See ``os.walk`` for documentation"""
        try:
            listing = self.list_status(top, **kwargs)
        except HdfsException as e:
            if onerror is not None:
                onerror(e)
            return

        dirnames, filenames = [], []
        for f in listing:
            if f.type == 'DIRECTORY':
                dirnames.append(f.pathSuffix)
            elif f.type == 'FILE':
                filenames.append(f.pathSuffix)
            else:  # pragma: no cover
                raise AssertionError("Unexpected type {}".format(f.type))

        if topdown:
            yield top, dirnames, filenames
        for name in dirnames:
            new_path = posixpath.join(top, name)
            for x in self.walk(new_path, topdown, onerror, **kwargs):
                yield x
        if not topdown:
            yield top, dirnames, filenames


Instruction:
Write a Python 3 script for
copying a single file from the local file system to dest.

Response:
def copy_from_local(self, localsrc, dest, **kwargs):
        """Copy a single file from the local file system to ``dest``

        Takes all arguments that :py:meth:`create` takes.
        """
        with io.open(localsrc, 'rb') as f:
            self.create(dest, f, **kwargs)


Instruction:
How would you explain what the following Python 3 function does
def copy_to_local(self, src, localdest, **kwargs):
        with self.open(src, **kwargs) as fsrc:
            with io.open(localdest, 'wb') as fdst:
                shutil.copyfileobj(fsrc, fdst)

Response:
Copy a single file from src to local file system

        Takes all arguments that open takes.


Instruction:
How would you explain what the following Python 3 function does
def get_active_namenode(self, max_staleness=None):
        if (max_staleness is None or
                self._last_time_recorded_active is None or
                self._last_time_recorded_active < time.time() - max_staleness):
            # Make a cheap request and rely on the reordering in self._record_last_active
            self.get_file_status('/')
        return self.hosts[0]

Response: Return the address of the currently active NameNode.


Instruction:
Here you have a function in Python 3, explain what it does
def next(self):
        if not self.__has_more():
            raise StopIteration()
        else:
            return javabridge.get_env().get_string(self.__next())

Response: Reads the next instance of the set of attributes.


Instruction:
How would you explain what the following Python 3 function does
def main():

    parser = argparse.ArgumentParser(
        description='Executes an associator from the command-line. Calls JVM start/stop automatically.')
    parser.add_argument("-j", metavar="classpath", dest="classpath", help="additional classpath, jars/directories")
    parser.add_argument("-X", metavar="heap", dest="heap", help="max heap size for jvm, e.g., 512m")
    parser.add_argument("-t", metavar="train", dest="train", required=True, help="training set file")
    parser.add_argument("associator", help="associator classname, e.g., weka.associations.Apriori")
    parser.add_argument("option", nargs=argparse.REMAINDER, help="additional associator options")
    parsed = parser.parse_args()
    jars = []
    if parsed.classpath is not None:
        jars = parsed.classpath.split(os.pathsep)

    jvm.start(jars, max_heap_size=parsed.heap, packages=True)

    logger.debug("Commandline: " + join_options(sys.argv[1:]))

    try:
        associator = Associator(classname=parsed.associator)
        if len(parsed.option) > 0:
            associator.options = parsed.option
        loader = converters.loader_for_file(parsed.train)
        data = loader.load_file(parsed.train)
        associator.build_associations(data)
        print(str(associator))
    except Exception, e:
        print(e)
    finally:
        jvm.stop()

Response: Main entry point for the associator.


Instruction:
Can you implement a function in Python 3 that
returns the next rule object in the list.

Response:
def next(self):
        """
        Returns the next rule.

        :return: the next rule object
        :rtype: AssociationRule
        """
        if self.index < self.length:
            index = self.index
            self.index += 1
            return self.rules[index]
        else:
            raise StopIteration()


Instruction:
Write a Python 3 function for
building the tree for the given actor.

Response:
def _build_tree(self, actor, content):
        """
        Builds the tree for the given actor.

        :param actor: the actor to process
        :type actor: Actor
        :param content: the rows of the tree collected so far
        :type content: list
        """
        depth = actor.depth
        row = ""
        for i in xrange(depth - 1):
            row += "| "
        if depth > 0:
            row += "|-"
        name = actor.name
        if name != actor.__class__.__name__:
            name = actor.__class__.__name__ + " '" + name + "'"
        row += name
        quickinfo = actor.quickinfo
        if quickinfo is not None:
            row += " [" + quickinfo + "]"
        content.append(row)

        if isinstance(actor, ActorHandler):
            for sub in actor.actors:
                self._build_tree(sub, content)


Instruction:
Here you have a function in Python 3, explain what it does
def check_actors(self):
        actors = []
        for actor in self.owner.actors:
            if actor.skip:
                continue
            actors.append(actor)
        if len(actors) == 0:
            return
        if not self.allow_source and base.is_source(actors[0]):
            raise Exception("Actor '" + actors[0].full_name + "' is a source, but no sources allowed!")
        for i in xrange(1, len(actors)):
            if not isinstance(actors[i], InputConsumer):
                raise Exception("Actor does not accept any input: " + actors[i].full_name)

Response: Checks the actors of the owner. Raises an exception if invalid.


Instruction:
Write a Python 3 script for
saving the flow to a JSON file.

Response:
def save(cls, flow, fname):
        """
        Saves the flow to a JSON file.

        :param flow: the flow to save
        :type flow: Flow
        :param fname: the file to load
        :type fname: str
        :return: None if successful, otherwise error message
        :rtype: str
        """
        result = None
        try:
            f = open(fname, 'w')
            f.write(flow.to_json())
            f.close()
        except Exception, e:
            result = str(e)
        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def setup(self):
        result = super(BranchDirector, self).setup()
        if result is None:
            try:
                self.check_actors()
            except Exception, e:
                result = str(e)
        return result

Response:
Performs some checks.

        :return: None if successful, otherwise error message.
        :rtype: str


Instruction:
How would you explain what the following Python 3 function does
def ranked_attributes(self):
        matrix = javabridge.call(self.jobject, "rankedAttributes", "()[[D")
        if matrix is None:
            return None
        else:
            return arrays.double_matrix_to_ndarray(matrix)

Response: Returns the matrix of ranked attributes from the last run.


Instruction:
How would you explain what the following Python 3 function does
def next(self):
        if self.index < self.length:
            index = self.index
            self.index += 1
            return self.data[index]
        else:
            raise StopIteration()

Response: Returns the next element from the array.


Instruction:
Make a summary of the following Python 3 code
def options(self):
        if self.is_optionhandler:
            return types.string_array_to_list(javabridge.call(self.jobject, "getOptions", "()[Ljava/lang/String;"))
        else:
            return []

Response: Gets the currently set options as list.


Instruction:
Can you generate the documentation for the following Python 3 function
def options(self, options):
        if self.is_optionhandler:
            javabridge.call(self.jobject, "setOptions", "([Ljava/lang/String;)V", types.string_list_to_array(options))

Response: Sets the command - line options.


Instruction:
How would you code a function in Python 3 to
plot the cluster assignments against the specified attributes.

Response:
def plot_cluster_assignments(evl, data, atts=None, inst_no=False, size=10, title=None, outfile=None, wait=True):
    """
    Plots the cluster assignments against the specified attributes.

    TODO: click events http://matplotlib.org/examples/event_handling/data_browser.html

    :param evl: the cluster evaluation to obtain the cluster assignments from
    :type evl: ClusterEvaluation
    :param data: the dataset the clusterer was evaluated against
    :type data: Instances
    :param atts: the list of attribute indices to plot, None for all
    :type atts: list
    :param inst_no: whether to include a fake attribute with the instance number
    :type inst_no: bool
    :param size: the size of the circles in point
    :type size: int
    :param title: an optional title
    :type title: str
    :param outfile: the (optional) file to save the generated plot to. The extension determines the file format.
    :type outfile: str
    :param wait: whether to wait for the user to close the plot
    :type wait: bool
    """
    if not plot.matplotlib_available:
        logger.error("Matplotlib is not installed, plotting unavailable!")
        return

    fig = plt.figure()

    if data.class_index == -1:
        c = None
    else:
        c = []
        for i in xrange(data.num_instances):
            inst = data.get_instance(i)
            c.append(inst.get_value(inst.class_index))

    if atts is None:
        atts = []
        for i in xrange(data.num_attributes):
            atts.append(i)

    num_plots = len(atts)
    if inst_no:
        num_plots += 1

    clusters = evl.cluster_assignments

    for index, att in enumerate(atts):
        x = data.values(att)
        ax = fig.add_subplot(
            1, num_plots, index + 1)
        if c is None:
            ax.scatter(clusters, x, s=size, alpha=0.5)
        else:
            ax.scatter(clusters, x, c=c, s=size, alpha=0.5)
        ax.set_xlabel("Clusters")
        ax.set_title(data.attribute(att).name)
        ax.get_xaxis().set_ticks(list(set(clusters)))
        ax.grid(True)

    if inst_no:
        x = []
        for i in xrange(data.num_instances):
            x.append(i+1)
        ax = fig.add_subplot(
            1, num_plots, num_plots)
        if c is None:
            ax.scatter(clusters, x, s=size, alpha=0.5)
        else:
            ax.scatter(clusters, x, c=c, s=size, alpha=0.5)
        ax.set_xlabel("Clusters")
        ax.set_title("Instance number")
        ax.get_xaxis().set_ticks(list(set(clusters)))
        ax.grid(True)

    if title is None:
        title = data.relationname
    fig.canvas.set_window_title(title)
    plt.draw()
    if not outfile is None:
        plt.savefig(outfile)
    if wait:
        plt.show()


Instruction:
Can you write a function in Python 3 where it
writes the list of objects to disk.

Response:
def write_all(filename, jobjects):
    """
    Serializes the list of objects to disk. JavaObject instances get automatically unwrapped.

    :param filename: the file to serialize the object to
    :type filename: str
    :param jobjects: the list of objects to serialize
    :type jobjects: list
    """
    array = javabridge.get_env().make_object_array(len(jobjects), javabridge.get_env().find_class("java/lang/Object"))
    for i in xrange(len(jobjects)):
        obj = jobjects[i]
        if isinstance(obj, JavaObject):
            obj = obj.jobject
        javabridge.get_env().set_object_array_element(array, i, obj)
    javabridge.static_call(
        "Lweka/core/SerializationHelper;", "writeAll",
        "(Ljava/lang/String;[Ljava/lang/Object;)V",
        filename, array)


Instruction:
Explain what the following Python 3 code does
def main():
    parser = argparse.ArgumentParser(
        description='Executes a filter from the command-line. Calls JVM start/stop automatically.')
    parser.add_argument("-j", metavar="classpath", dest="classpath", help="additional classpath, jars/directories")
    parser.add_argument("-X", metavar="heap", dest="heap", help="max heap size for jvm, e.g., 512m")
    parser.add_argument("-i", metavar="input1", dest="input1", required=True, help="input file 1")
    parser.add_argument("-o", metavar="output1", dest="output1", required=True, help="output file 1")
    parser.add_argument("-r", metavar="input2", dest="input2", help="input file 2")
    parser.add_argument("-s", metavar="output2", dest="output2", help="output file 2")
    parser.add_argument("-c", metavar="classindex", default="-1", dest="classindex",
                        help="1-based class attribute index")
    parser.add_argument("filter", help="filter classname, e.g., weka.filters.AllFilter")
    parser.add_argument("option", nargs=argparse.REMAINDER, help="additional filter options")
    parsed = parser.parse_args()
    if parsed.input2 is None and parsed.output2 is not None:
        raise Exception("No second input file provided ('-r ...')!")

    jars = []
    if parsed.classpath is not None:
        jars = parsed.classpath.split(os.pathsep)
    params = []
    if parsed.input1 is not None:
        params.extend(["-i", parsed.input1])
    if parsed.output1 is not None:
        params.extend(["-o", parsed.output1])
    if parsed.input2 is not None:
        params.extend(["-r", parsed.input2])
    if parsed.output2 is not None:
        params.extend(["-s", parsed.output2])
    if parsed.classindex is not None:
        params.extend(["-c", parsed.classindex])

    jvm.start(jars, max_heap_size=parsed.heap, packages=True)

    logger.debug("Commandline: " + join_options(sys.argv[1:]))

    try:
        flter = Filter(parsed.filter)
        if len(parsed.option) > 0:
            flter.options = parsed.option
        loader = Loader(classname="weka.core.converters.ArffLoader")
        in1 = loader.load_file(parsed.input1)
        cls = parsed.classindex
        if str(parsed.classindex) == "first":
            cls = "0"
        if str(parsed.classindex) == "last":
            cls = str(in1.num_attributes - 1)
        in1.class_index = int(cls)
        flter.inputformat(in1)
        out1 = flter.filter(in1)
        saver = Saver(classname="weka.core.converters.ArffSaver")
        saver.save_file(out1, parsed.output1)
        if parsed.input2 is not None:
            in2 = loader.load_file(parsed.input2)
            in2.class_index = int(cls)
            out2 = flter.filter(in2)
            saver.save_file(out2, parsed.output2)
    except Exception, e:
        print(e)
    finally:
        jvm.stop()

Response:
Entry point for the
    command - line interface.


Instruction:
Make a summary of the following Python 3 code
def next(self):
        result = javabridge.call(
            self.loader.jobject, "getNextInstance",
            "(Lweka/core/Instances;)Lweka/core/Instance;", self.structure.jobject)
        if result is None:
            raise StopIteration()
        else:
            return Instance(result)

Response: Reads the next dataset row.


Instruction:
Can you tell what is the following Python 3 function doing
def plot_classifier_errors(predictions, absolute=True, max_relative_size=50, absolute_size=50, title=None,
                           outfile=None, wait=True):
    """
    Plots the classifers for the given list of predictions.

    TODO: click events http://matplotlib.org/examples/event_handling/data_browser.html

    :param predictions: the predictions to plot
    :type predictions: list
    :param absolute: whether to use absolute errors as size or relative ones
    :type absolute: bool
    :param max_relative_size: the maximum size in point in case of relative mode
    :type max_relative_size: int
    :param absolute_size: the size in point in case of absolute mode
    :type absolute_size: int
    :param title: an optional title
    :type title: str
    :param outfile: the output file, ignored if None
    :type outfile: str
    :param wait: whether to wait for the user to close the plot
    :type wait: bool
    """
    if not plot.matplotlib_available:
        logger.error("Matplotlib is not installed, plotting unavailable!")
        return
    actual = []
    predicted = []
    error = None
    cls = None
    for pred in predictions:
        actual.append(pred.actual)
        predicted.append(pred.predicted)
        if isinstance(pred, NumericPrediction):
            if error is None:
                error = []
            error.append(abs(pred.error))
        elif isinstance(pred, NominalPrediction):
            if cls is None:
                cls = []
            if pred.actual != pred.predicted:
                cls.append(1)
            else:
                cls.append(0)
    fig, ax = plt.subplots()
    if error is None and cls is None:
        ax.scatter(actual, predicted, s=absolute_size, alpha=0.5)
    elif cls is not None:
        ax.scatter(actual, predicted, c=cls, s=absolute_size, alpha=0.5)
    elif error is not None:
        if not absolute:
            min_err = min(error)
            max_err = max(error)
            factor = (max_err - min_err) / max_relative_size
            for i in xrange(len(error)):
                error[i] = error[i] / factor * max_relative_size
        ax.scatter(actual, predicted, s=error, alpha=0.5)
    ax.set_xlabel("actual")
    ax.set_ylabel("predicted")
    if title is None:
        title = "Classifier errors"
    ax.set_title(title)
    ax.plot(ax.get_xlim(), ax.get_ylim(), ls="--", c="0.3")
    ax.grid(True)
    fig.canvas.set_window_title(title)
    plt.draw()
    if outfile is not None:
        plt.savefig(outfile)
    if wait:
        plt.show()

Response: Plots the classifers for the given list of predictions.


Instruction:
Can you generate the documentation for the following Python 3 function
def main():
    parser = argparse.ArgumentParser(
        description='Performs classification/regression from the command-line. Calls JVM start/stop automatically.')
    parser.add_argument("-j", metavar="classpath", dest="classpath", help="additional classpath, jars/directories")
    parser.add_argument("-X", metavar="heap", dest="heap", help="max heap size for jvm, e.g., 512m")
    parser.add_argument("-t", metavar="train", dest="train", required=True, help="Training set file")
    parser.add_argument("-T", metavar="test", dest="test", help="Test set file")
    parser.add_argument("-c", metavar="class index", dest="classindex", help="1-based class attribute index")
    parser.add_argument("-d", metavar="outmodel", dest="outmodel", help="model output file name")
    parser.add_argument("-l", metavar="inmodel", dest="inmodel", help="model input file name")
    parser.add_argument("-x", metavar="num folds", dest="numfolds", help="number of folds for cross-validation")
    parser.add_argument("-s", metavar="seed", dest="seed", help="seed value for randomization")
    parser.add_argument("-v", action="store_true", dest="notrainstats", help="no statistics for training")
    parser.add_argument("-o", action="store_true", dest="onlystats", help="only statistics, don't output model")
    parser.add_argument("-i", action="store_true", dest="irstats", help="output information retrieval statistics")
    parser.add_argument("-k", action="store_true", dest="itstats", help="output information theoretic statistics")
    parser.add_argument("-m", metavar="costmatrix", dest="costmatrix", help="cost matrix file")
    parser.add_argument("-g", metavar="graph", dest="graph", help="output file for graph (if supported)")
    parser.add_argument("classifier", help="classifier classname, e.g., weka.classifiers.trees.J48")
    parser.add_argument("option", nargs=argparse.REMAINDER, help="additional classifier options")
    parsed = parser.parse_args()
    jars = []
    if parsed.classpath is not None:
        jars = parsed.classpath.split(os.pathsep)
    params = []
    if parsed.train is not None:
        params.extend(["-t", parsed.train])
    if parsed.test is not None:
        params.extend(["-T", parsed.test])
    if parsed.classindex is not None:
        params.extend(["-c", parsed.classindex])
    if parsed.outmodel is not None:
        params.extend(["-d", parsed.outmodel])
    if parsed.inmodel is not None:
        params.extend(["-l", parsed.inmodel])
    if parsed.numfolds is not None:
        params.extend(["-x", parsed.numfolds])
    if parsed.seed is not None:
        params.extend(["-s", parsed.seed])
    if parsed.notrainstats:
        params.append("-v")
    if parsed.onlystats:
        params.append("-o")
    if parsed.irstats:
        params.append("-i")
    if parsed.itstats:
        params.append("-k")
    if parsed.costmatrix is not None:
        params.extend(["-m", parsed.costmatrix])
    if parsed.graph is not None:
        params.extend(["-g", parsed.graph])

    jvm.start(jars, max_heap_size=parsed.heap, packages=True)

    logger.debug("Commandline: " + join_options(sys.argv[1:]))

    try:
        classifier = Classifier(classname=parsed.classifier)
        if len(parsed.option) > 0:
            classifier.options = parsed.option
        print(Evaluation.evaluate_model(classifier, params))
    except Exception, e:
        print(e)
    finally:
        jvm.stop()

Response:
Main function for the
    command - line tool.


Instruction:
How would you implement a function in Python 3 that
returns the class distributions for the Instances object.

Response:
def distributions_for_instances(self, data):
        """
        Peforms predictions, returning the class distributions.

        :param data: the Instances to get the class distributions for
        :type data: Instances
        :return: the class distribution matrix, None if not a batch predictor
        :rtype: ndarray
        """
        if self.is_batchpredictor:
            return arrays.double_matrix_to_ndarray(self.__distributions(data.jobject))
        else:
            return None


Instruction:
Create a Python 3 function to
return the internal values of this attribute from all the instance objects.

Response:
def values(self, index):
        """
        Returns the internal values of this attribute from all the instance objects.

        :return: the values as numpy array
        :rtype: list
        """
        values = []
        for i in xrange(self.num_instances):
            inst = self.get_instance(i)
            values.append(inst.get_value(index))
        return numpy.array(values)


Instruction:
Create a Python 3 function for
returning a new instance set with the same set of instances.

Response:
def append_instances(cls, inst1, inst2):
        """
        Merges the two datasets (one-after-the-other). Throws an exception if the datasets aren't compatible.

        :param inst1: the first dataset
        :type inst1: Instances
        :param inst2: the first dataset
        :type inst2: Instances
        :return: the combined dataset
        :rtype: Instances
        """
        msg = inst1.equal_headers(inst2)
        if msg is not None:
            raise Exception("Cannot appent instances: " + msg)
        result = cls.copy_instances(inst1)
        for i in xrange(inst2.num_instances):
            result.add_instance(inst2.get_instance(i))
        return result


Instruction:
Here you have a function in Python 3, explain what it does
def values(self):
        enm = javabridge.call(self.jobject, "enumerateValues", "()Ljava/util/Enumeration;")
        if enm is None:
            return None
        else:
            return types.enumeration_to_list(enm)

Response: Returns the labels strings or relation - values.