Instruction:
Can you tell what is the following Python 3 function doing
def _initialize_from_dict(self, data):
        self._json = data
        self._validate()

        for name, value in self._json.items():
            if name in self._properties:
                if '$ref' in self._properties[name]:
                    if 'decimal' in self._properties[name]['$ref']:
                        value = Decimal(value)

                # applying proper formatting when required
                if 'format' in self._properties[name]:
                    format = self._properties[name]['format']

                    if 'date-time' == format:
                        value = utils.string_to_datetime(value)
                    elif 'date' == format:
                        value = utils.string_to_date(value)
                setattr(self, name, value)

Response: Loads the serializer from a dictionary.


Instruction:
Create a Python 3 function for
initializing the object from a model.

Response:
def _initialize_from_model(self, model):
        """
        Loads a model from
        """
        for name, value in model.__dict__.items():
            if name in self._properties:
                setattr(self, name, value)


Instruction:
Implement a Python 3 function for
updating the record based on serializer values

Response:
def update(self):
        """
        Finds record and update it based in serializer values
        """
        obj = self.__model__.objects.get_for_update(id=self.id)
        for name, value in self.__dict__.items():
            if name in self._properties:
                setattr(obj, name, value)
        obj.update()
        return obj


Instruction:
Write a Python 3 script to
return a json representation of the object.

Response:
def to_json(self):
        """
        Returns a json representation
        """
        data = {}
        for k, v in self.__dict__.items():
            if not k.startswith('_'):
                # values not serializable, should be converted to strings
                if isinstance(v, datetime):
                    v = utils.datetime_to_string(v)
                elif isinstance(v, date):
                    v = utils.date_to_string(v)
                elif isinstance(v, uuid.UUID):
                    v = str(v)
                elif isinstance(v, Decimal):
                    v = str(v)
                data[k] = v
        return data


Instruction:
How would you explain what the following Python 3 function does
def OPERATING_SYSTEM(stats, info):
    info.append(('architecture', platform.machine().lower()))
    info.append(('distribution',
                 "%s;%s" % (platform.linux_distribution()[0:2])))
    info.append(('system',
                 "%s;%s" % (platform.system(), platform.release())))

Response: General information about the operating system.


Instruction:
Can you generate the documentation for the following Python 3 function
def SESSION_TIME(stats, info):
    duration = time.time() - stats.started_time
    secs = int(duration)
    msecs = int((duration - secs) * 1000)
    info.append(('session_time', '%d.%d' % (secs, msecs)))

Response: Total time elapsed from the construction of the Stats object to


Instruction:
Can you implement a function in Python 3 that
reads the configuration file and sets the status attribute of the usage statistics object.

Response:
def read_config(self):
        """Reads the configuration.

        This method can be overloaded to integrate with your application's own
        configuration mechanism. By default, a single 'status' file is read
        from the reports' directory.

        This should set `self.status` to one of the state constants, and make
        sure `self.location` points to a writable directory where the reports
        will be written.

        The possible values for `self.status` are:

        - `UNSET`: nothing has been selected and the user should be prompted
        - `ENABLED`: collect and upload reports
        - `DISABLED`: don't collect or upload anything, stop prompting
        - `ERRORED`: something is broken, and we can't do anything in this
          session (for example, the configuration directory is not writable)
        """
        if self.enabled and not os.path.isdir(self.location):
            try:
                os.makedirs(self.location, 0o700)
            except OSError:
                logger.warning("Couldn't create %s, usage statistics won't be "
                               "collected", self.location)
                self.status = Stats.ERRORED

        status_file = os.path.join(self.location, 'status')
        if self.enabled and os.path.exists(status_file):
            with open(status_file, 'r') as fp:
                status = fp.read().strip()
            if status == 'ENABLED':
                self.status = Stats.ENABLED
            elif status == 'DISABLED':
                self.status = Stats.DISABLED


Instruction:
Here you have a function in Python 3, explain what it does
def write_config(self, enabled):
        status_file = os.path.join(self.location, 'status')
        with open(status_file, 'w') as fp:
            if enabled is Stats.ENABLED:
                fp.write('ENABLED')
            elif enabled is Stats.DISABLED:
                fp.write('DISABLED')
            else:
                raise ValueError("Unknown reporting state %r" % enabled)

Response:
Writes the configuration.

        This method can be overloaded to integrate with your application's own
        configuration mechanism. By default, a single 'status' file is written
        in the reports' directory, containing either ``ENABLED`` or
        ``DISABLED``; if the file doesn't exist, `UNSET` is assumed.

        :param enabled: Either `Stats.UNSET`, `Stats.DISABLED` or
        `Stats.ENABLED`.


Instruction:
Here you have a function in Python 3, explain what it does
def enable_reporting(self):
        if self.status == Stats.ENABLED:
            return
        if not self.enableable:
            logger.critical("Can't enable reporting")
            return
        self.status = Stats.ENABLED
        self.write_config(self.status)

Response: Set the status of the current report to Stats. ENABLED.


Instruction:
Create a Python 3 function to
call this method to explicitly disable reporting. The current report will be discarded, along with the previously recorded ones that haven't been uploaded. The configuration is updated so that future runs do not record or upload reports.

Response:
def disable_reporting(self):
        """Call this method to explicitly disable reporting.

        The current report will be discarded, along with the previously
        recorded ones that haven't been uploaded. The configuration is updated
        so that future runs do not record or upload reports.
        """
        if self.status == Stats.DISABLED:
            return
        if not self.disableable:
            logger.critical("Can't disable reporting")
            return
        self.status = Stats.DISABLED
        self.write_config(self.status)
        if os.path.exists(self.location):
            old_reports = [f for f in os.listdir(self.location)
                           if f.startswith('report_')]
            for old_filename in old_reports:
                fullname = os.path.join(self.location, old_filename)
                os.remove(fullname)
            logger.info("Deleted %d pending reports", len(old_reports))


Instruction:
Can you create a Python 3 function that
records some info to the report.

Response:
def note(self, info):
        """Record some info to the report.

        :param info: Dictionary of info to record. Note that previous info
        recorded under the same keys will not be overwritten.
        """
        if self.recording:
            if self.notes is None:
                raise ValueError("This report has already been submitted")
            self.notes.extend(self._to_notes(info))


Instruction:
Create a Python 3 function for
submitting a new report.

Response:
def submit(self, info, *flags):
        """Finish recording and upload or save the report.

        This closes the `Stats` object, no further methods should be called.
        The report is either saved, uploaded or discarded, depending on
        configuration. If uploading is enabled, previous reports might be
        uploaded too. If uploading is not explicitly enabled or disabled, the
        prompt will be shown, to ask the user to enable or disable it.
        """
        if not self.recording:
            return
        env_val = os.environ.get(self.env_var, '').lower()
        if env_val not in (None, '', '1', 'on', 'enabled', 'yes', 'true'):
            self.status = Stats.DISABLED_ENV
            self.notes = None
            return

        if self.notes is None:
            raise ValueError("This report has already been submitted")

        all_info, self.notes = self.notes, None
        all_info.extend(self._to_notes(info))
        for flag in flags:
            flag(self, all_info)

        now = time.time()
        secs = int(now)
        msecs = int((now - secs) * 1000)
        all_info.insert(0, ('date', '%d.%d' % (secs, msecs)))

        if self.user_id:
            all_info.insert(1, ('user', self.user_id))

        logger.debug("Generated report:\n%r", (all_info,))

        # Current report
        def generator():
            for key, value in all_info:
                yield _encode(key) + b':' + _encode(value) + b'\n'
        filename = 'report_%d_%d.txt' % (secs, msecs)

        # Save current report and exit, unless user has opted in
        if not self.sending:
            fullname = os.path.join(self.location, filename)
            with open(fullname, 'wb') as fp:
                for l in generator():
                    fp.write(l)

            # Show prompt
            sys.stderr.write(self.prompt.prompt)
            return

        # Post previous reports
        old_reports = [f for f in os.listdir(self.location)
                       if f.startswith('report_')]
        old_reports.sort()
        old_reports = old_reports[:4]  # Only upload 5 at a time
        for old_filename in old_reports:
            fullname = os.path.join(self.location, old_filename)
            try:
                with open(fullname, 'rb') as fp:
                    # `data=fp` would make requests stream, which is currently
                    # not a good idea (WSGI chokes on it)
                    r = requests.post(self.drop_point, data=fp.read(),
                                      timeout=1, verify=self.ssl_verify)
                    r.raise_for_status()
            except Exception as e:
                logger.warning("Couldn't upload %s: %s", old_filename, str(e))
                break
            else:
                logger.info("Submitted report %s", old_filename)
                os.remove(fullname)

        # Post current report
        try:
            # `data=generator()` would make requests stream, which is currently
            # not a good idea (WSGI chokes on it)
            r = requests.post(self.drop_point, data=b''.join(generator()),
                              timeout=1, verify=self.ssl_verify)
        except requests.RequestException as e:
            logger.warning("Couldn't upload report: %s", str(e))
            fullname = os.path.join(self.location, filename)
            with open(fullname, 'wb') as fp:
                for l in generator():
                    fp.write(l)
        else:
            try:
                r.raise_for_status()
                logger.info("Submitted report")
            except requests.RequestException as e:
                logger.warning("Server rejected report: %s", str(e))


Instruction:
How would you implement a function in Python 3 that
stores the report on disk.

Response:
def store(report, address):
    """Stores the report on disk.
    """
    now = time.time()
    secs = int(now)
    msecs = int((now - secs) * 1000)
    submitted_date = filename = None  # avoids warnings
    while True:
        submitted_date = '%d.%03d' % (secs, msecs)
        filename = 'report_%s.txt' % submitted_date
        filename = os.path.join(DESTINATION, filename)
        if not os.path.exists(filename):
            break
        msecs += 1

    lines = [l for l in report.split(b'\n') if l]
    for line in lines:
        if line.startswith(b'date:'):
            date = line[5:]
            if date_format.match(date):
                with open(filename, 'wb') as fp:
                    if not isinstance(address, bytes):
                        address = address.encode('ascii')
                    fp.write(b'submitted_from:' + address + b'\n')
                    fp.write(('submitted_date:%s\n' % submitted_date)
                             .encode('ascii'))
                    fp.write(report)
                return None
            else:
                return "invalid date"
    return "missing date field"


Instruction:
Can you generate a brief explanation for the following Python 3 code
def read(self, pin):
        port, pin = self.pin_to_port(pin)
        self.i2c_write([0x12 + port])
        raw = self.i2c_read(1)
        value = struct.unpack('>B', raw)[0]
        return (value & (1 << pin)) > 0

Response: Read the pin state of an input pin.


Instruction:
Implement a Python 3 function for
reading the state of a whole port.

Response:
def read_port(self, port):
        """ Read the pin state of a whole port (8 pins)

        :Example:

        >>> expander = MCP23017I2C(gw)
        >>> # Read pin A0-A7 as a int (A0 and A1 are high)
        >>> expander.read_port('A')
        3
        >>> # Read pin B0-B7 as a int (B2 is high)
        >>> expander.read_port('B')
        4

        :param port: use 'A' to read port A and 'B' for port b
        :return: An int where every bit represents the input level.
        """
        if port == 'A':
            raw = self.i2c_read_register(0x12, 1)
        elif port == 'B':
            raw = self.i2c_read_register(0x13, 1)
        return struct.unpack('>B', raw)[0]


Instruction:
Make a summary of the following Python 3 code
def write(self, pin, value):
        port, pin = self.pin_to_port(pin)
        portname = 'A'
        if port == 1:
            portname = 'B'
        self._update_register('GPIO' + portname, pin, value)
        self.sync()

Response: Set the pin state to the specified value.


Instruction:
Write a Python 3 script to
write a byte to a port in the current system.

Response:
def write_port(self, port, value):
        """ Use a whole port as a bus and write a byte to it.

        :param port: Name of the port ('A' or 'B')
        :param value: Value to write (0-255)
        """
        if port == 'A':
            self.GPIOA = value
        elif port == 'B':
            self.GPIOB = value
        else:
            raise AttributeError('Port {} does not exist, use A or B'.format(port))
        self.sync()


Instruction:
Can you implement a function in Python 3 that
uploads the changed registers to the chip This will check which register have been changed since the last sync and send them to the chip. You need to call this method if you modify one of the register attributes (mcp23017.IODIRA for example) or if you use one of the helper attributes (mcp23017.direction_A0 for example)

Response:
def sync(self):
        """ Upload the changed registers to the chip

        This will check which register have been changed since the last sync and send them to the chip.
        You need to call this method if you modify one of the register attributes (mcp23017.IODIRA for example) or
        if you use one of the helper attributes (mcp23017.direction_A0 for example)
        """
        registers = {
            0x00: 'IODIRA',
            0x01: 'IODIRB',
            0x02: 'IPOLA',
            0x03: 'IPOLB',
            0x04: 'GPINTENA',
            0x05: 'GPINTENB',
            0x0C: 'GPPUA',
            0x0D: 'GPPUB',
            0x12: 'GPIOA',
            0x13: 'GPIOB'
        }
        for reg in registers:
            name = registers[reg]
            if getattr(self, name) != getattr(self, '_' + name):
                self.i2c_write_register(reg, [getattr(self, name)])
                setattr(self, '_' + name, getattr(self, name))


Instruction:
Create a Python 3 function for
getting a list containing all 16 pins of the chip.

Response:
def get_pins(self):
        """ Get a list containing references to all 16 pins of the chip.

        :Example:

        >>> expander = MCP23017I2C(gw)
        >>> pins = expander.get_pins()
        >>> pprint.pprint(pins)
        [<GPIOPin A0 on MCP23017I2C>,
         <GPIOPin A1 on MCP23017I2C>,
         <GPIOPin A2 on MCP23017I2C>,
         <GPIOPin A3 on MCP23017I2C>,
         <GPIOPin A4 on MCP23017I2C>,
         <GPIOPin A5 on MCP23017I2C>,
         <GPIOPin A6 on MCP23017I2C>,
         <GPIOPin B0 on MCP23017I2C>,
         <GPIOPin B1 on MCP23017I2C>,
         <GPIOPin B2 on MCP23017I2C>,
         <GPIOPin B3 on MCP23017I2C>,
         <GPIOPin B4 on MCP23017I2C>,
         <GPIOPin B5 on MCP23017I2C>,
         <GPIOPin B6 on MCP23017I2C>]

        """
        result = []
        for a in range(0, 7):
            result.append(GPIOPin(self, '_action', {'pin': 'A{}'.format(a)}, name='A{}'.format(a)))
        for b in range(0, 7):
            result.append(GPIOPin(self, '_action', {'pin': 'B{}'.format(b)}, name='B{}'.format(b)))
        return result


Instruction:
Can you tell what is the following Python 3 function doing
def read(self):
        m = getattr(self.chip, self.method)
        return m(**self.arguments)

Response: Get the logic input level for the pin


Instruction:
Write a Python 3 function that can
set the logic output level for the pin.

Response:
def write(self, value):
        """ Set the logic output level for the pin.

        :type value: bool
        :param value: True for a logic high
        """
        if self.inverted:
            value = not value
        m = getattr(self.chip, self.method)
        m(value=value, **self.arguments)


Instruction:
Can you implement a function in Python 3 that
initializes application object. :param app: An instance of :class:`~flask.Flask`.

Response:
def init_app(self, app, **kwargs):
        """Initialize application object.

        :param app: An instance of :class:`~flask.Flask`.
        """
        self.init_config(app)

        # Initialize extensions
        self.menu_ext.init_app(app)
        self.menu = app.extensions['menu']
        self.breadcrumbs.init_app(app)

        # Register blueprint in order to register template and static folder.
        app.register_blueprint(Blueprint(
            'invenio_theme',
            __name__,
            template_folder='templates',
            static_folder='static',
        ))

        # Register frontpage blueprint if enabled.
        if app.config['THEME_FRONTPAGE']:
            app.register_blueprint(blueprint)

        # Initialize breadcrumbs.
        item = self.menu.submenu('breadcrumbs')
        item.register(app.config['THEME_BREADCRUMB_ROOT_ENDPOINT'], _('Home'))

        # Register errors handlers.
        app.register_error_handler(401, unauthorized)
        app.register_error_handler(403, insufficient_permissions)
        app.register_error_handler(404, page_not_found)
        app.register_error_handler(500, internal_error)

        # Save reference to self on object
        app.extensions['invenio-theme'] = self


Instruction:
How would you implement a function in Python 3 that
initializes configuration. :param app: An instance of :class:`~flask.Flask`.

Response:
def init_config(self, app):
        """Initialize configuration.

        :param app: An instance of :class:`~flask.Flask`.
        """
        _vars = ['BASE_TEMPLATE', 'COVER_TEMPLATE', 'SETTINGS_TEMPLATE']

        # Sets RequireJS config and SASS binary as well if not already set.
        for k in dir(config):
            if k.startswith('THEME_') or k in [
                    'REQUIREJS_CONFIG', 'SASS_BIN'] + _vars:
                app.config.setdefault(k, getattr(config, k))

        # Set THEME_<name>_TEMPLATE from <name>_TEMPLATE variables if not
        # already set.
        for varname in _vars:
            theme_varname = 'THEME_{}'.format(varname)
            if app.config[theme_varname] is None:
                app.config[theme_varname] = app.config[varname]

        app.config.setdefault(
            'ADMIN_BASE_TEMPLATE', config.ADMIN_BASE_TEMPLATE)


Instruction:
Implement a Python 3 function for
setting the measurement range for the accel and gyro MEMS. Higher range means less resolution.

Response:
def set_range(self, accel=1, gyro=1):
        """Set the measurement range for the accel and gyro MEMS. Higher range means less resolution.

        :param accel: a RANGE_ACCEL_* constant
        :param gyro: a RANGE_GYRO_* constant

        :Example:

        .. code-block:: python

            sensor = MPU6050I2C(gateway_class_instance)
            sensor.set_range(
                accel=MPU6050I2C.RANGE_ACCEL_2G,
                gyro=MPU6050I2C.RANGE_GYRO_250DEG
                )

        """
        self.i2c_write_register(0x1c, accel)
        self.i2c_write_register(0x1b, gyro)
        self.accel_range = accel
        self.gyro_range = gyro


Instruction:
Implement a function in Python 3 to
set the aux i2c bus in bypass mode thus connecting it to the main i2c bus.

Response:
def set_slave_bus_bypass(self, enable):
        """Put the aux i2c bus on the MPU-6050 in bypass mode, thus connecting it to the main i2c bus directly

        Dont forget to use wakeup() or else the slave bus is unavailable
        :param enable:
        :return:
        """
        current = self.i2c_read_register(0x37, 1)[0]
        if enable:
            current |= 0b00000010
        else:
            current &= 0b11111101
        self.i2c_write_register(0x37, current)


Instruction:
Write a Python 3 script for
reading the value for the internal temperature sensor.

Response:
def temperature(self):
        """Read the value for the internal temperature sensor.

        :returns: Temperature in degree celcius as float

        :Example:

        >>> sensor = MPU6050I2C(gw)
        >>> sensor.wakeup()
        >>> sensor.temperature()
        49.38
        """
        if not self.awake:
            raise Exception("MPU6050 is in sleep mode, use wakeup()")

        raw = self.i2c_read_register(0x41, 2)
        raw = struct.unpack('>h', raw)[0]
        return round((raw / 340) + 36.53, 2)


Instruction:
How would you code a function in Python 3 to
return the acceleration in G s as a tuple

Response:
def acceleration(self):
        """Return the acceleration in G's

        :returns: Acceleration for every axis as a tuple

        :Example:

        >>> sensor = MPU6050I2C(gw)
        >>> sensor.wakeup()
        >>> sensor.acceleration()
        (0.6279296875, 0.87890625, 1.1298828125)
        """
        if not self.awake:
            raise Exception("MPU6050 is in sleep mode, use wakeup()")

        raw = self.i2c_read_register(0x3B, 6)
        x, y, z = struct.unpack('>HHH', raw)
        scales = {
            self.RANGE_ACCEL_2G: 16384,
            self.RANGE_ACCEL_4G: 8192,
            self.RANGE_ACCEL_8G: 4096,
            self.RANGE_ACCEL_16G: 2048
        }
        scale = scales[self.accel_range]
        return x / scale, y / scale, z / scale


Instruction:
Can you generate a brief explanation for the following Python 3 code
def angular_rate(self):
        if not self.awake:
            raise Exception("MPU6050 is in sleep mode, use wakeup()")

        raw = self.i2c_read_register(0x43, 6)
        x, y, z = struct.unpack('>HHH', raw)
        scales = {
            self.RANGE_GYRO_250DEG: 16384,
            self.RANGE_GYRO_500DEG: 8192,
            self.RANGE_GYRO_1000DEG: 4096,
            self.RANGE_GYRO_2000DEG: 2048
        }
        scale = scales[self.gyro_range]
        return x / scale, y / scale, z / scale

Response: Return the angular rate for every axis in degree / second.


Instruction:
Can you tell what is the following Python 3 function doing
def _get_contents(self):
        return [
            str(value) if is_lazy_string(value) else value
            for value in super(LazyNpmBundle, self)._get_contents()
        ]

Response: Create strings from lazy strings.


Instruction:
Can you write a function in Python 3 where it
loads factory calibration data from device.

Response:
def load_calibration(self):
        """Load factory calibration data from device."""
        registers = self.i2c_read_register(0xAA, 22)
        (
            self.cal['AC1'],
            self.cal['AC2'],
            self.cal['AC3'],
            self.cal['AC4'],
            self.cal['AC5'],
            self.cal['AC6'],
            self.cal['B1'],
            self.cal['B2'],
            self.cal['MB'],
            self.cal['MC'],
            self.cal['MD']
        ) = struct.unpack('>hhhHHHhhhhh', registers)


Instruction:
Here you have a function in Python 3, explain what it does
def temperature(self):
        ut = self.get_raw_temp()
        x1 = ((ut - self.cal['AC6']) * self.cal['AC5']) >> 15
        x2 = (self.cal['MC'] << 11) // (x1 + self.cal['MD'])
        b5 = x1 + x2
        return ((b5 + 8) >> 4) / 10

Response: Get the temperature from the sensor.


Instruction:
Given the following Python 3 function, write the documentation
def pressure(self):
        ut = self.get_raw_temp()
        up = self.get_raw_pressure()
        x1 = ((ut - self.cal['AC6']) * self.cal['AC5']) >> 15
        x2 = (self.cal['MC'] << 11) // (x1 + self.cal['MD'])
        b5 = x1 + x2
        b6 = b5 - 4000
        x1 = (self.cal['B2'] * (b6 * b6) >> 12) >> 11
        x2 = (self.cal['AC2'] * b6) >> 11
        x3 = x1 + x2
        b3 = (((self.cal['AC1'] * 4 + x3) << self.mode) + 2) // 4
        x1 = (self.cal['AC3'] * b6) >> 13
        x2 = (self.cal['B1'] * ((b6 * b6) >> 12)) >> 16
        x3 = ((x1 + x2) + 2) >> 2
        b4 = (self.cal['AC4'] * (x3 + 32768)) >> 15
        b7 = (up - b3) * (50000 >> self.mode)
        if b7 < 0x80000000:
            p = (b7 * 2) // b4
        else:
            p = (b7 // b4) * 2
        x1 = (p >> 8) * (p >> 8)
        x1 = (x1 * 3038) >> 16
        x2 = (-7357 * p) >> 16
        p += (x1 + x2 + 3791) >> 4
        return p

Response: Get barometric pressure in milibar as a int


Instruction:
Explain what the following Python 3 code does
def switch_mode(self, new_mode):
        packet = bytearray()
        packet.append(new_mode)
        self.device.write(packet)
        possible_responses = {
            self.MODE_I2C: b'I2C1',
            self.MODE_RAW: b'BBIO1',
            self.MODE_SPI: b'API1',
            self.MODE_UART: b'ART1',
            self.MODE_ONEWIRE: b'1W01'
        }
        expected = possible_responses[new_mode]
        response = self.device.read(4)
        if response != expected:
            raise Exception('Could not switch mode')
        self.mode = new_mode
        self.set_peripheral()
        if self.i2c_speed:
            self._set_i2c_speed(self.i2c_speed)

Response: Explicitly switch the Bus Pirate mode.


Instruction:
Given the following Python 3 function, write the documentation
def set_peripheral(self, power=None, pullup=None, aux=None, chip_select=None):
        if power is not None:
            self.power = power
        if pullup is not None:
            self.pullup = pullup
        if aux is not None:
            self.aux = aux
        if chip_select is not None:
            self.chip_select = chip_select
        # Set peripheral status
        peripheral_byte = 64
        if self.chip_select:
            peripheral_byte |= 0x01
        if self.aux:
            peripheral_byte |= 0x02
        if self.pullup:
            peripheral_byte |= 0x04
        if self.power:
            peripheral_byte |= 0x08

        self.device.write(bytearray([peripheral_byte]))
        response = self.device.read(1)
        if response != b"\x01":
            raise Exception("Setting peripheral failed. Received: {}".format(repr(response)))

Response: Set the state of the internal peripheral state at runtime.


Instruction:
Here you have a function in Python 3, explain what it does
def _set_i2c_speed(self, i2c_speed):
        lower_bits_mapping = {
            '400kHz': 3,
            '100kHz': 2,
            '50kHz': 1,
            '5kHz': 0,
        }
        if i2c_speed not in lower_bits_mapping:
            raise ValueError('Invalid i2c_speed')
        speed_byte = 0b01100000 | lower_bits_mapping[i2c_speed]
        self.device.write(bytearray([speed_byte]))
        response = self.device.read(1)
        if response != b"\x01":
            raise Exception("Changing I2C speed failed. Received: {}".format(repr(response)))

Response: Set the I2C speed of the ISO.


Instruction:
Can you tell what is the following Python 3 function doing
def set_resolution(self, resolution=1090):
        options = {
            1370: 0,
            1090: 1,
            820: 2,
            660: 3,
            440: 4,
            390: 5,
            330: 6,
            230: 7
        }

        if resolution not in options.keys():
            raise Exception('Resolution of {} steps is not supported'.format(resolution))

        self.resolution = resolution

        config_b = 0
        config_b &= options[resolution] << 5
        self.i2c_write_register(0x01, config_b)

Response: Sets the resolution of the current log entry.


Instruction:
Write a Python 3 script for
getting the magnetometer values as gauss for each axis as a tuple x y z

Response:
def gauss(self):
        """
        Get the magnetometer values as gauss for each axis as a tuple (x,y,z)

        :example:

        >>> sensor = HMC5883L(gw)
        >>> sensor.gauss()
        (16.56, 21.2888, 26.017599999999998)
        """
        raw = self.raw()
        factors = {
            1370: 0.73,
            1090: 0.92,
            820: 1.22,
            660: 1.52,
            440: 2.27,
            390: 2.56,
            330: 3.03,
            230: 4.35
        }
        factor = factors[self.resolution] / 100
        return raw[0] * factor, raw[1] * factor, raw[2] * factor


Instruction:
Can you implement a function in Python 3 that
gets the temperature in degree celcius

Response:
def temperature(self):
        """ Get the temperature in degree celcius
        """
        result = self.i2c_read(2)
        value = struct.unpack('>H', result)[0]

        if value < 32768:
            return value / 256.0
        else:
            return (value - 65536) / 256.0


Instruction:
Can you implement a function in Python 3 that
displays a single character on the display

Response:
def write(self, char):
        """ Display a single character on the display

        :type char: str or int
        :param char: Character to display
        """
        char = str(char).lower()
        self.segments.write(self.font[char])


Instruction:
Here you have a function in Python 3, explain what it does
def get_html_theme_path():
    return os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

Response: Get the absolute path of the directory containing the theme files.


Instruction:
Implement a function in Python 3 to
create a URL for feedback on a particular page in a project.

Response:
def feedback_form_url(project, page):
    """
    Create a URL for feedback on a particular page in a project.
    """
    return FEEDBACK_FORM_FMT.format(pageid=quote("{}: {}".format(project, page)))


Instruction:
Can you create a Python 3 function that
updates the page rendering context to include feedback_form_url.

Response:
def update_context(app, pagename, templatename, context, doctree):  # pylint: disable=unused-argument
    """
    Update the page rendering context to include ``feedback_form_url``.
    """
    context['feedback_form_url'] = feedback_form_url(app.config.project, pagename)


Instruction:
Can you tell what is the following Python 3 function doing
def objectsFromPEM(pemdata):
    certificates = []
    keys = []
    blobs = [b""]
    for line in pemdata.split(b"\n"):
        if line.startswith(b'-----BEGIN'):
            if b'CERTIFICATE' in line:
                blobs = certificates
            else:
                blobs = keys
            blobs.append(b'')
        blobs[-1] += line
        blobs[-1] += b'\n'
    keys = [KeyPair.load(key, FILETYPE_PEM) for key in keys]
    certificates = [Certificate.loadPEM(certificate)
                    for certificate in certificates]
    return PEMObjects(keys=keys, certificates=certificates)

Response: Load some objects from a PEM file.


Instruction:
Here you have a function in Python 3, explain what it does
def following_key(key):
    if key.timestamp is not None:
        key.timestamp -= 1
    elif key.colVisibility is not None:
        key.colVisibility = following_array(key.colVisibility)
    elif key.colQualifier is not None:
        key.colQualifier = following_array(key.colQualifier)
    elif key.colFamily is not None:
        key.colFamily = following_array(key.colFamily)
    elif key.row is not None:
        key.row = following_array(key.row)
    return key

Response:
Returns the key immediately following the input key - based on the Java implementation found in
    org. apache. accumulo. core. data. Key function followingKey


Instruction:
Can you tell what is the following Python 3 function doing
def followingPrefix(prefix):
        prefixBytes = array('B', prefix)

        changeIndex = len(prefixBytes) - 1
        while (changeIndex >= 0 and prefixBytes[changeIndex] == 0xff ):
            changeIndex = changeIndex - 1;
        if(changeIndex < 0):
            return None
        newBytes = array('B', prefix[0:changeIndex + 1])
        newBytes[changeIndex] = newBytes[changeIndex] + 1
        return newBytes.tostring()

Response: Returns a String that sorts just after all Strings beginning with a prefix


Instruction:
How would you code a function in Python 3 to
return a Range that covers all rows beginning with a prefix

Response:
def prefix(rowPrefix):
        """Returns a Range that covers all rows beginning with a prefix"""
        fp = Range.followingPrefix(rowPrefix)
        return Range(srow=rowPrefix, sinclude=True, erow=fp, einclude=False)


Instruction:
Write a Python 3 script for
adding mutations to a table and flush the batch writer.

Response:
def add_mutations_and_flush(self, table, muts):
        """
        Add mutations to a table without the need to create and manage a batch writer.
        """
        if not isinstance(muts, list) and not isinstance(muts, tuple):
            muts = [muts]
        cells = {}
        for mut in muts:
            cells.setdefault(mut.row, []).extend(mut.updates)
        self.client.updateAndFlush(self.login, table, cells)


Instruction:
Write a Python 3 script for
removing a constraint from the specified table

Response:
def remove_constraint(self, table, constraint):
        """
        :param table: table name
        :param constraint: the constraint number as returned by list_constraints
        """
        self.client.removeConstraint(self.login, table, constraint)


Instruction:
Write a Python 3 script for
sending login to the server and receive the response.

Response:
def login(self, principal, loginProperties):
    """
    Parameters:
     - principal
     - loginProperties
    """
    self.send_login(principal, loginProperties)
    return self.recv_login()


Instruction:
Explain what the following Python 3 code does
def addConstraint(self, login, tableName, constraintClassName):
    self.send_addConstraint(login, tableName, constraintClassName)
    return self.recv_addConstraint()

Response: Adds a constraint to the table with the specified login.


Instruction:
How would you explain what the following Python 3 function does
def addSplits(self, login, tableName, splits):
    self.send_addSplits(login, tableName, splits)
    self.recv_addSplits()

Response: Adds a list of splits to the table with login login.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clearLocatorCache(self, login, tableName):
    self.send_clearLocatorCache(login, tableName)
    self.recv_clearLocatorCache()

Response: Clear the locator cache for a given login and tableName.


Instruction:
Can you generate the documentation for the following Python 3 function
def cloneTable(self, login, tableName, newTableName, flush, propertiesToSet, propertiesToExclude):
    self.send_cloneTable(login, tableName, newTableName, flush, propertiesToSet, propertiesToExclude)
    self.recv_cloneTable()

Response: This method is used to clone a table.


Instruction:
How would you explain what the following Python 3 function does
def cancelCompaction(self, login, tableName):
    self.send_cancelCompaction(login, tableName)
    self.recv_cancelCompaction()

Response: Cancels compaction of a table.


Instruction:
Explain what the following Python 3 code does
def deleteTable(self, login, tableName):
    self.send_deleteTable(login, tableName)
    self.recv_deleteTable()

Response: Delete a table from the server.


Instruction:
Make a summary of the following Python 3 code
def deleteRows(self, login, tableName, startRow, endRow):
    self.send_deleteRows(login, tableName, startRow, endRow)
    self.recv_deleteRows()

Response: Delete rows from a table.


Instruction:
Can you generate the documentation for the following Python 3 function
def flushTable(self, login, tableName, startRow, endRow, wait):
    self.send_flushTable(login, tableName, startRow, endRow, wait)
    self.recv_flushTable()

Response: This method is used to flush a table from the server.


Instruction:
Write a Python 3 script for
returning a list of all locality groups for a given login and tableName.

Response:
def getLocalityGroups(self, login, tableName):
    """
    Parameters:
     - login
     - tableName
    """
    self.send_getLocalityGroups(login, tableName)
    return self.recv_getLocalityGroups()


Instruction:
Here you have a function in Python 3, explain what it does
def getMaxRow(self, login, tableName, auths, startRow, startInclusive, endRow, endInclusive):
    self.send_getMaxRow(login, tableName, auths, startRow, startInclusive, endRow, endInclusive)
    return self.recv_getMaxRow()

Response: This method returns the maximum row number in a table.


Instruction:
How would you code a function in Python 3 to
return the properties of a table.

Response:
def getTableProperties(self, login, tableName):
    """
    Parameters:
     - login
     - tableName
    """
    self.send_getTableProperties(login, tableName)
    return self.recv_getTableProperties()


Instruction:
How would you implement a function in Python 3 that
returns a list of splits for a given login and tableName.

Response:
def getSplits(self, login, tableName, maxSplits):
    """
    Parameters:
     - login
     - tableName
     - maxSplits
    """
    self.send_getSplits(login, tableName, maxSplits)
    return self.recv_getSplits()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def importDirectory(self, login, tableName, importDir, failureDir, setTime):
    self.send_importDirectory(login, tableName, importDir, failureDir, setTime)
    self.recv_importDirectory()

Response: This method is used to send and receive import directory command to the server.


Instruction:
Given the following Python 3 function, write the documentation
def importTable(self, login, tableName, importDir):
    self.send_importTable(login, tableName, importDir)
    self.recv_importTable()

Response: This function is used to send and receive a table import request to the server.


Instruction:
Can you generate the documentation for the following Python 3 function
def listIterators(self, login, tableName):
    self.send_listIterators(login, tableName)
    return self.recv_listIterators()

Response: This method is used to list the iterators for a given login and tableName.


Instruction:
Can you tell what is the following Python 3 function doing
def listConstraints(self, login, tableName):
    self.send_listConstraints(login, tableName)
    return self.recv_listConstraints()

Response: This method is used to list the constraints of a table.


Instruction:
Make a summary of the following Python 3 code
def offlineTable(self, login, tableName):
    self.send_offlineTable(login, tableName)
    self.recv_offlineTable()

Response: Sends an offline table request to the server.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def onlineTable(self, login, tableName):
    self.send_onlineTable(login, tableName)
    self.recv_onlineTable()

Response: This function is used to send an online table to the server.


Instruction:
Given the following Python 3 function, write the documentation
def removeConstraint(self, login, tableName, constraint):
    self.send_removeConstraint(login, tableName, constraint)
    self.recv_removeConstraint()

Response: Removes a constraint from the table with login login.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def removeIterator(self, login, tableName, iterName, scopes):
    self.send_removeIterator(login, tableName, iterName, scopes)
    self.recv_removeIterator()

Response: Remove an iterator from the table.


Instruction:
Write a Python 3 function that can
remove a property from a table

Response:
def removeTableProperty(self, login, tableName, property):
    """
    Parameters:
     - login
     - tableName
     - property
    """
    self.send_removeTableProperty(login, tableName, property)
    self.recv_removeTableProperty()


Instruction:
Can you generate the documentation for the following Python 3 function
def renameTable(self, login, oldTableName, newTableName):
    self.send_renameTable(login, oldTableName, newTableName)
    self.recv_renameTable()

Response: This function is used to rename a table in a loginspace.


Instruction:
Explain what the following Python 3 code does
def setLocalityGroups(self, login, tableName, groups):
    self.send_setLocalityGroups(login, tableName, groups)
    self.recv_setLocalityGroups()

Response: This method is used to set the locality of the table.


Instruction:
Can you generate the documentation for the following Python 3 function
def setTableProperty(self, login, tableName, property, value):
    self.send_setTableProperty(login, tableName, property, value)
    self.recv_setTableProperty()

Response: Sets the value of a table property.


Instruction:
Create a Python 3 function to
ping a tablet server.

Response:
def pingTabletServer(self, login, tserver):
    """
    Parameters:
     - login
     - tserver
    """
    self.send_pingTabletServer(login, tserver)
    self.recv_pingTabletServer()


Instruction:
Can you tell what is the following Python 3 function doing
def getActiveScans(self, login, tserver):
    self.send_getActiveScans(login, tserver)
    return self.recv_getActiveScans()

Response: Returns the active scans for the specified login and tserver.


Instruction:
How would you explain what the following Python 3 function does
def getActiveCompactions(self, login, tserver):
    self.send_getActiveCompactions(login, tserver)
    return self.recv_getActiveCompactions()

Response: Returns the active compactions for the given login and tserver.


Instruction:
Write a Python 3 function for
removing a property from the specified login.

Response:
def removeProperty(self, login, property):
    """
    Parameters:
     - login
     - property
    """
    self.send_removeProperty(login, property)
    self.recv_removeProperty()


Instruction:
Can you tell what is the following Python 3 function doing
def setProperty(self, login, property, value):
    self.send_setProperty(login, property, value)
    self.recv_setProperty()

Response: Set the value of a property on a node.


Instruction:
Given the following Python 3 function, write the documentation
def authenticateUser(self, login, user, properties):
    self.send_authenticateUser(login, user, properties)
    return self.recv_authenticateUser()

Response:
Parameters:
     - login
     - user
     - properties


Instruction:
How would you code a function in Python 3 to
change the authorizations for a user.

Response:
def changeUserAuthorizations(self, login, user, authorizations):
    """
    Parameters:
     - login
     - user
     - authorizations
    """
    self.send_changeUserAuthorizations(login, user, authorizations)
    self.recv_changeUserAuthorizations()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def changeLocalUserPassword(self, login, user, password):
    self.send_changeLocalUserPassword(login, user, password)
    self.recv_changeLocalUserPassword()

Response: Change the password of a local user.


Instruction:
Here you have a function in Python 3, explain what it does
def dropLocalUser(self, login, user):
    self.send_dropLocalUser(login, user)
    self.recv_dropLocalUser()

Response: Drop a local user from the specified login.


Instruction:
Explain what the following Python 3 code does
def getUserAuthorizations(self, login, user):
    self.send_getUserAuthorizations(login, user)
    return self.recv_getUserAuthorizations()

Response: Returns a list of authorizations for a user.


Instruction:
Can you create a Python 3 function that
grants system permission to a user

Response:
def grantSystemPermission(self, login, user, perm):
    """
    Parameters:
     - login
     - user
     - perm
    """
    self.send_grantSystemPermission(login, user, perm)
    self.recv_grantSystemPermission()


Instruction:
Write a Python 3 function for
granting a permission to a table.

Response:
def grantTablePermission(self, login, user, table, perm):
    """
    Parameters:
     - login
     - user
     - table
     - perm
    """
    self.send_grantTablePermission(login, user, table, perm)
    self.recv_grantTablePermission()


Instruction:
Create a Python 3 function to
return true if the user has permission for login.

Response:
def hasSystemPermission(self, login, user, perm):
    """
    Parameters:
     - login
     - user
     - perm
    """
    self.send_hasSystemPermission(login, user, perm)
    return self.recv_hasSystemPermission()


Instruction:
Given the following Python 3 function, write the documentation
def hasTablePermission(self, login, user, table, perm):
    self.send_hasTablePermission(login, user, table, perm)
    return self.recv_hasTablePermission()

Response: Returns True if the user has permission for the table perm.


Instruction:
Can you tell what is the following Python 3 function doing
def revokeSystemPermission(self, login, user, perm):
    self.send_revokeSystemPermission(login, user, perm)
    self.recv_revokeSystemPermission()

Response: revokes a system permission for a given login user


Instruction:
Write a Python 3 function that can
revoke a permission from a table

Response:
def revokeTablePermission(self, login, user, table, perm):
    """
    Parameters:
     - login
     - user
     - table
     - perm
    """
    self.send_revokeTablePermission(login, user, table, perm)
    self.recv_revokeTablePermission()


Instruction:
Explain what the following Python 3 code does
def createBatchScanner(self, login, tableName, options):
    self.send_createBatchScanner(login, tableName, options)
    return self.recv_createBatchScanner()

Response: Create a batch scanner for the specified login and tableName.


Instruction:
Can you write a function in Python 3 where it
creates a new scanner for the specified login and tableName.

Response:
def createScanner(self, login, tableName, options):
    """
    Parameters:
     - login
     - tableName
     - options
    """
    self.send_createScanner(login, tableName, options)
    return self.recv_createScanner()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def nextK(self, scanner, k):
    self.send_nextK(scanner, k)
    return self.recv_nextK()

Response: This method sends the next k - element to the specified scanner and returns the k - element.


Instruction:
Explain what the following Python 3 code does
def getFollowing(self, key, part):
    self.send_getFollowing(key, part)
    return self.recv_getFollowing()

Response: Returns the next following element in a node.


Instruction:
Implement a function in Python 3 to
turn the relay on.

Response:
def set_relay_on(self):
        """Turn the relay on."""
        if not self.get_relay_state():
            try:
                request = requests.get(
                    '{}/relay'.format(self.resource), params={'state': '1'},
                    timeout=self.timeout)
                if request.status_code == 200:
                    self.data['relay'] = True
            except requests.exceptions.ConnectionError:
                raise exceptions.MyStromConnectionError()


Instruction:
Can you implement a function in Python 3 that
turns the relay off.

Response:
def set_relay_off(self):
        """Turn the relay off."""
        if self.get_relay_state():
            try:
                request = requests.get(
                    '{}/relay'.format(self.resource), params={'state': '0'},
                    timeout=self.timeout)
                if request.status_code == 200:
                    self.data['relay'] = False
            except requests.exceptions.ConnectionError:
                raise exceptions.MyStromConnectionError()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_relay_state(self):
        self.get_status()
        try:
            self.state = self.data['relay']
        except TypeError:
            self.state = False

        return bool(self.state)

Response: Get the relay state.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_consumption(self):
        self.get_status()
        try:
            self.consumption = self.data['power']
        except TypeError:
            self.consumption = 0

        return self.consumption

Response: Get current power consumption in mWh.


Instruction:
Create a Python 3 function for
getting current temperature in celsius.

Response:
def get_temperature(self):
        """Get current temperature in celsius."""
        try:
            request = requests.get(
                '{}/temp'.format(self.resource), timeout=self.timeout, allow_redirects=False)
            self.temperature = request.json()['compensated']
            return self.temperature
        except requests.exceptions.ConnectionError:
            raise exceptions.MyStromConnectionError()
        except ValueError:
            raise exceptions.MyStromNotVersionTwoSwitch()


Instruction:
Explain what the following Python 3 code does
def eval_fitness(self, chromosome):
        score = 0

        number = self.translator.translate_gene(chromosome.genes[0])

        for factor in self.factors:
            if number % factor == 0:
                score += 1
            else:
                score -= 1

        # check for optimal solution
        if score == len(self.factors):
            min_product = functools.reduce(lambda a,b: a * b, self.factors)

            if number + min_product > self.max_encoded_val:
                print("Found best solution:", number)
                self.found_best = True

        # scale number of factors achieved/missed by ratio of
        # the solution number to the maximum possible integer
        # represented by binary strings of the given length
        return score * number / self.max_encoded_val

Response: Convert a 1 - gene chromosome into an integer and calculate its fitness value by checking it against each required factor and checking it against each required factor.


Instruction:
Make a summary of the following Python 3 code
def get_status(self):
        try:
            request = requests.get(
                '{}/{}/'.format(self.resource, URI), timeout=self.timeout)
            raw_data = request.json()
            # Doesn't always work !!!!!
            #self._mac = next(iter(self.raw_data))
            self.data = raw_data[self._mac]
            return self.data
        except (requests.exceptions.ConnectionError, ValueError):
            raise exceptions.MyStromConnectionError()

Response: Get the details from the bulb.


Instruction:
Create a Python 3 function to
get the relay state.

Response:
def get_bulb_state(self):
        """Get the relay state."""
        self.get_status()
        try:
            self.state = self.data['on']
        except TypeError:
            self.state = False

        return bool(self.state)


Instruction:
Can you tell what is the following Python 3 function doing
def get_firmware(self):
        self.get_status()
        try:
            self.firmware = self.data['fw_version']
        except TypeError:
            self.firmware = 'Unknown'

        return self.firmware

Response: Get the current firmware version.


Instruction:
Write a Python 3 script for
getting the transition time in ms.

Response:
def get_transition_time(self):
        """Get the transition time in ms."""
        self.get_status()
        try:
            self.transition_time = self.data['ramp']
        except TypeError:
            self.transition_time = 0

        return self.transition_time


Instruction:
Explain what the following Python 3 code does
def set_color_hsv(self, hue, saturation, value):
        try:
            data = "action=on&color={};{};{}".format(hue, saturation, value)
            request = requests.post(
                '{}/{}/{}'.format(self.resource, URI, self._mac),
                data=data, timeout=self.timeout)
            if request.status_code == 200:
                self.data['on'] = True
        except requests.exceptions.ConnectionError:
            raise exceptions.MyStromConnectionError()

Response: Turn the bulb on with the given values as HSV.


Instruction:
Write a Python 3 function for
turning the bulb on and create a rainbow.

Response:
def set_rainbow(self, duration):
        """Turn the bulb on and create a rainbow."""
        for i in range(0, 359):
            self.set_color_hsv(i, 100, 100)
            time.sleep(duration/359)


Instruction:
How would you implement a function in Python 3 that
turns the bulb on and create a sunrise.

Response:
def set_sunrise(self, duration):
        """Turn the bulb on and create a sunrise."""
        self.set_transition_time(duration/100)
        for i in range(0, duration):
            try:
                data = "action=on&color=3;{}".format(i)
                request = requests.post(
                    '{}/{}/{}'.format(self.resource, URI, self._mac),
                    data=data, timeout=self.timeout)
                if request.status_code == 200:
                    self.data['on'] = True
            except requests.exceptions.ConnectionError:
                raise exceptions.MyStromConnectionError()
            time.sleep(duration/100)


Instruction:
Create a Python 3 function for
turning the bulb on flashing with two colors.

Response:
def set_flashing(self, duration, hsv1, hsv2):
        """Turn the bulb on, flashing with two colors."""
        self.set_transition_time(100)
        for step in range(0, int(duration/2)):
            self.set_color_hsv(hsv1[0], hsv1[1], hsv1[2])
            time.sleep(1)
            self.set_color_hsv(hsv2[0], hsv2[1], hsv2[2])
            time.sleep(1)


Instruction:
Can you create a Python 3 function that
turns the bulb off.

Response:
def set_off(self):
        """Turn the bulb off."""
        try:
            request = requests.post(
                '{}/{}/{}/'.format(self.resource, URI, self._mac),
                data={'action': 'off'}, timeout=self.timeout)
            if request.status_code == 200:
                pass
        except requests.exceptions.ConnectionError:
            raise exceptions.MyStromConnectionError()


Instruction:
Given the following Python 3 function, write the documentation
def translate_chromosome(self, chromosome):
        assert isinstance(chromosome, Chromosome)
        return [self.translate_gene(g) for g in chromosome]

Response: Translate all the genes in a chromosome into translation products


Instruction:
Given the following Python 3 function, write the documentation
def translate_gene(self, gene):
        if self.signed:
            sign = 1 if gene.dna[0] == '0' else -1
            base_start_idx = 1
        else:
            sign = 1
            base_start_idx = 0

        base = sign * int(gene.dna[base_start_idx:base_start_idx + self.significand_length], base=2)

        exponent_sign = 1 if gene.dna[1 + self.significand_length] == '0' else -1
        exponent = exponent_sign * int(gene.dna[self.significand_length + 2:], base=2)

        return float(base * 10 ** exponent)

Response: Translate a gene with binary DNA into a base - 10 floating point real number.


Instruction:
Write a Python 3 script for
loading all the client libraries for the given apis.

Response:
def load_clients(self, path=None, apis=[]):
        """Generate client libraries for the given apis, without starting an
        api server"""

        if not path:
            raise Exception("Missing path to api swagger files")

        if type(apis) is not list:
            raise Exception("'apis' should be a list of api names")

        if len(apis) == 0:
            raise Exception("'apis' is an empty list - Expected at least one api name")

        for api_name in apis:
            api_path = os.path.join(path, '%s.yaml' % api_name)
            if not os.path.isfile(api_path):
                raise Exception("Cannot find swagger specification at %s" % api_path)
            log.info("Loading api %s from %s" % (api_name, api_path))
            ApiPool.add(
                api_name,
                yaml_path=api_path,
                timeout=self.timeout,
                error_callback=self.error_callback,
                formats=self.formats,
                do_persist=False,
                local=False,
            )

        return self


Instruction:
Implement a function in Python 3 to
load all apis under the given path.

Response:
def load_apis(self, path, ignore=[], include_crash_api=False):
        """Load all swagger files found at the given path, except those whose
        names are in the 'ignore' list"""

        if not path:
            raise Exception("Missing path to api swagger files")

        if type(ignore) is not list:
            raise Exception("'ignore' should be a list of api names")

        # Always ignore pym-config.yaml
        ignore.append('pym-config')

        # Find all swagger apis under 'path'
        apis = {}

        log.debug("Searching path %s" % path)
        for root, dirs, files in os.walk(path):
            for f in files:
                if f.endswith('.yaml'):
                    api_name = f.replace('.yaml', '')

                    if api_name in ignore:
                        log.info("Ignoring api %s" % api_name)
                        continue

                    apis[api_name] = os.path.join(path, f)
                    log.debug("Found api %s in %s" % (api_name, f))

        # And add pymacaron's default ping and crash apis
        for name in ['ping', 'crash']:
            yaml_path = pkg_resources.resource_filename(__name__, 'pymacaron/%s.yaml' % name)
            if not os.path.isfile(yaml_path):
                yaml_path = os.path.join(os.path.dirname(sys.modules[__name__].__file__), '%s.yaml' % name)
            apis[name] = yaml_path

        if not include_crash_api:
            del apis['crash']

        # Save found apis
        self.path_apis = path
        self.apis = apis

        return self


Instruction:
Make a summary of the following Python 3 code
def publish_apis(self, path='doc'):

        assert path

        if not self.apis:
            raise Exception("You must call .load_apis() before .publish_apis()")

        # Infer the live host url from pym-config.yaml
        proto = 'http'
        if hasattr(get_config(), 'aws_cert_arn'):
            proto = 'https'

        live_host = "%s://%s" % (proto, get_config().live_host)

        # Allow cross-origin calls
        CORS(self.app, resources={r"/%s/*" % path: {"origins": "*"}})

        # Add routes to serve api specs and redirect to petstore ui for each one
        for api_name, api_path in self.apis.items():

            api_filename = os.path.basename(api_path)
            log.info("Publishing api %s at /%s/%s" % (api_name, path, api_name))

            def redirect_to_petstore(live_host, api_filename):
                def f():
                    url = 'http://petstore.swagger.io/?url=%s/%s/%s' % (live_host, path, api_filename)
                    log.info("Redirecting to %s" % url)
                    return redirect(url, code=302)
                return f

            def serve_api_spec(api_path):
                def f():
                    with open(api_path, 'r') as f:
                        spec = f.read()
                        log.info("Serving %s" % api_path)
                        return Response(spec, mimetype='text/plain')
                return f

            self.app.add_url_rule('/%s/%s' % (path, api_name), str(uuid4()), redirect_to_petstore(live_host, api_filename))
            self.app.add_url_rule('/%s/%s' % (path, api_filename), str(uuid4()), serve_api_spec(api_path))

        return self

Response: Publish all loaded apis on under the uri path by redirecting to petstore. swagger. io


Instruction:
Can you create a Python 3 function that
starts the app server.

Response:
def start(self, serve=[]):
        """Load all apis, either as local apis served by the flask app, or as
        remote apis to be called from whithin the app's endpoints, then start
        the app server"""

        # Check arguments
        if type(serve) is str:
            serve = [serve]
        elif type(serve) is list:
            pass
        else:
            raise Exception("'serve' should be an api name or a list of api names")

        if len(serve) == 0:
            raise Exception("You must specify at least one api to serve")

        for api_name in serve:
            if api_name not in self.apis:
                raise Exception("Can't find %s.yaml (swagger file) in the api directory %s" % (api_name, self.path_apis))

        app = self.app
        app.secret_key = os.urandom(24)

        # Initialize JWT config
        conf = get_config()
        if hasattr(conf, 'jwt_secret'):
            log.info("Set JWT parameters to issuer=%s audience=%s secret=%s***" % (
                conf.jwt_issuer,
                conf.jwt_audience,
                conf.jwt_secret[0:8],
            ))

        # Always serve the ping api
        serve.append('ping')

        # Let's compress returned data when possible
        compress = Compress()
        compress.init_app(app)

        # All apis that are not served locally are not persistent
        not_persistent = []
        for api_name in self.apis.keys():
            if api_name in serve:
                pass
            else:
                not_persistent.append(api_name)

        # Now load those apis into the ApiPool
        for api_name, api_path in self.apis.items():

            host = None
            port = None

            if api_name in serve:
                # We are serving this api locally: override the host:port specified in the swagger spec
                host = self.host
                port = self.port

            do_persist = True if api_name not in not_persistent else False
            local = True if api_name in serve else False

            log.info("Loading api %s from %s (persist: %s)" % (api_name, api_path, do_persist))
            ApiPool.add(
                api_name,
                yaml_path=api_path,
                timeout=self.timeout,
                error_callback=self.error_callback,
                formats=self.formats,
                do_persist=do_persist,
                host=host,
                port=port,
                local=local,
            )

        ApiPool.merge()

        # Now spawn flask routes for all endpoints
        for api_name in self.apis.keys():
            if api_name in serve:
                log.info("Spawning api %s" % api_name)
                api = getattr(ApiPool, api_name)
                # Spawn api and wrap every endpoint in a crash handler that
                # catches replies and reports errors
                api.spawn_api(app, decorator=generate_crash_handler_decorator(self.error_decorator))

        log.debug("Argv is [%s]" % '  '.join(sys.argv))
        if 'celery' in sys.argv[0].lower():
            # This code is loading in a celery server - Don't start the actual flask app.
            log.info("Running in a Celery worker - Not starting the Flask app")
            return

        # Initialize monitoring, if any is defined
        monitor_init(app=app, config=conf)

        if os.path.basename(sys.argv[0]) == 'gunicorn':
            # Gunicorn takes care of spawning workers
            log.info("Running in Gunicorn - Not starting the Flask app")
            return

        # Debug mode is the default when not running via gunicorn
        app.debug = self.debug

        app.run(host='0.0.0.0', port=self.port)


Instruction:
Write a Python 3 script for
creating a new exception class with the given name and code and status.

Response:
def add_error(name=None, code=None, status=None):
    """Create a new Exception class"""
    if not name or not status or not code:
        raise Exception("Can't create Exception class %s: you must set both name, status and code" % name)
    myexception = type(name, (PyMacaronException, ), {"code": code, "status": status})
    globals()[name] = myexception
    if code in code_to_class:
        raise Exception("ERROR! Exception %s is already defined." % code)
    code_to_class[code] = myexception
    return myexception


Instruction:
Explain what the following Python 3 code does
def responsify(error):
    assert str(type(error).__name__) == 'Error'
    if error.error in code_to_class:
        e = code_to_class[error.error](error.error_description)
        if error.error_id:
            e.error_id = error.error_id
        if error.user_message:
            e.user_message = error.user_message
        return e.http_reply()
    elif isinstance(error, PyMacaronException):
        return error.http_reply()
    else:
        return PyMacaronException("Caught un-mapped error: %s" % error).http_reply()

Response: Take an Error model and return it as a Flask response


Instruction:
Can you generate the documentation for the following Python 3 function
def is_error(o):
    if hasattr(o, 'error') and hasattr(o, 'error_description') and hasattr(o, 'status'):
        return True
    return False

Response: True if o is an error model or a flask Response


Instruction:
Can you implement a function in Python 3 that
takes an exception caught within pymacaron_core and turn it into abravado - core Error instance

Response:
def format_error(e):
    """Take an exception caught within pymacaron_core and turn it into a
    bravado-core Error instance
    """

    if isinstance(e, PyMacaronException):
        return e.to_model()

    if isinstance(e, PyMacaronCoreException) and e.__class__.__name__ == 'ValidationError':
        return ValidationError(str(e)).to_model()

    # Turn this exception into a PyMacaron Error model
    return UnhandledServerError(str(e)).to_model()


Instruction:
Make a summary of the following Python 3 code
def raise_error(e):
    code = e.error
    if code in code_to_class:
        raise code_to_class[code](e.error_description)
    else:
        raise InternalServerError(e.error_description)

Response: Take a bravado - core Error model and raise it as an exception


Instruction:
How would you explain what the following Python 3 function does
def http_reply(self):
        data = {
            'status': self.status,
            'error': self.code.upper(),
            'error_description': str(self)
        }

        if self.error_caught:
            data['error_caught'] = pformat(self.error_caught)

        if self.error_id:
            data['error_id'] = self.error_id

        if self.user_message:
            data['user_message'] = self.user_message

        r = jsonify(data)
        r.status_code = self.status

        if str(self.status) != "200":
            log.warn("ERROR: caught error %s %s [%s]" % (self.status, self.code, str(self)))

        return r

Response: Return a Flask reply object describing this error


Instruction:
Here you have a function in Python 3, explain what it does
def to_model(self):
        e = ApiPool().current_server_api.model.Error(
            status=self.status,
            error=self.code.upper(),
            error_description=str(self),
        )
        if self.error_id:
            e.error_id = self.error_id
        if self.user_message:
            e.user_message = self.user_message
        if self.error_caught:
            e.error_caught = pformat(self.error_caught)
        return e

Response: Return a bravado - core Error instance


Instruction:
Make a summary of the following Python 3 code
def compute_fitness_cdf(chromosomes, ga):
    ga.sort(chromosomes)

    fitness = [ga.eval_fitness(c) for c in chromosomes]
    min_fit = min(fitness)
    fit_range = max(fitness) - min_fit

    if fit_range == 0:
        # all chromosomes have equal chance of being chosen
        n = len(chromosomes)
        return [i / n for i in range(1, n + 1)]

    return [(fit - min_fit) / fit_range for fit in fitness]

Response: Compute the fitness - weighted cumulative probabilities for a set of chromosomes.


Instruction:
Here you have a function in Python 3, explain what it does
def weighted_choice(seq, cdf):
    assert len(seq) == len(cdf)
    rand = random.random()

    for i, e in enumerate(seq):
        cp = cdf[i]
        assert 0 <= cp <= 1

        if rand < cp:
            return e

Response: Select a random element from a sequence given a cumulative probabilities of selection.


Instruction:
Implement a function in Python 3 to
get a list of metrics.

Response:
def _list(self, path, dim_key=None, **kwargs):
        """Get a list of metrics."""
        url_str = self.base_url + path
        if dim_key and dim_key in kwargs:
            dim_str = self.get_dimensions_url_string(kwargs[dim_key])
            kwargs[dim_key] = dim_str

        if kwargs:
            url_str += '?%s' % parse.urlencode(kwargs, True)

        body = self.client.list(
            path=url_str
        )

        return self._parse_body(body)


Instruction:
Implement a function in Python 3 to
convert an ASCII map string with rows to a list of strings 1 string per row.

Response:
def mapstr_to_list(mapstr):
    """ Convert an ASCII map string with rows to a list of strings, 1 string per row. """
    maplist = []

    with StringIO(mapstr) as infile:
        for row in infile:
            maplist.append(row.strip())

    return maplist


Instruction:
Create a Python 3 function for
returning whether a cell is within the radius of the sprinkler.

Response:
def sprinkler_reaches_cell(x, y, sx, sy, r):
    """
    Return whether a cell is within the radius of the sprinkler.

    x:  column index of cell
    y:  row index of cell
    sx:  column index of sprinkler
    sy:  row index of sprinkler
    r:  sprinkler radius
    """
    dx = sx - x
    dy = sy - y
    return math.sqrt(dx ** 2 + dy ** 2) <= r


Instruction:
Create a Python 3 function for
evaluating the fitness of a sprinkler.

Response:
def eval_fitness(self, chromosome):
        """
        Return the number of plants reached by the sprinkler.

        Returns a large penalty for sprinkler locations outside the map.
        """
        # convert DNA to represented sprinkler coordinates
        sx, sy = self.translator.translate_chromosome(chromosome)

        # check for invalid points
        penalty = 0
        if sx >= self.w:
            penalty += self.w * self.h
        if sy >= self.h:
            penalty += self.w * self.h

        if penalty > 0:
            self.fitness_cache[chromosome.dna] = -penalty
            return -penalty

        # calculate number of crop cells watered by sprinkler
        crops_watered = 0

        # check bounding box around sprinkler for crop cells
        # circle guaranteed to be within square with side length 2*r around sprinkler
        row_start_idx = max(0, sy - self.r)
        row_end_idx = min(sy + self.r + 1, self.h)
        col_start_idx = max(0, sx - self.r)
        col_end_idx = min(sx + self.r + 1, self.w)

        for y, row in enumerate(self.maplist[row_start_idx:row_end_idx], row_start_idx):
            for x, cell in enumerate(row[col_start_idx:col_end_idx], col_start_idx):
                if cell == 'x' and sprinkler_reaches_cell(x, y, sx, sy, self.r):
                    crops_watered += 1

        # reduce score by 1 if sprinkler placed on a crop cell
        if self.maplist[sy][sx] == 'x':
            crops_watered -= 1

        self.fitness_cache[chromosome.dna] = crops_watered
        return crops_watered


Instruction:
Write a Python 3 function for
returning a string of the ASCII map showing reached crop cells.

Response:
def map_sprinkler(self, sx, sy, watered_crop='^', watered_field='_', dry_field=' ', dry_crop='x'):
        """
        Return a version of the ASCII map showing reached crop cells.
        """
        # convert strings (rows) to lists of characters for easier map editing
        maplist = [list(s) for s in self.maplist]

        for y, row in enumerate(maplist):
            for x, cell in enumerate(row):
                if sprinkler_reaches_cell(x, y, sx, sy, self.r):
                    if cell == 'x':
                        cell = watered_crop
                    else:
                        cell = watered_field
                else:
                    cell = dry_crop if cell == 'x' else dry_field

                maplist[y][x] = cell

        maplist[sy][sx] = 'O'  # sprinkler

        return '\n'.join([''.join(row) for row in maplist])


Instruction:
Can you create a Python 3 function that
gets the fitness score for a given chromosome.

Response:
def get_fitness(self, chromosome):
        """ Get the fitness score for a chromosome, using the cached value if available. """
        fitness = self.fitness_cache.get(chromosome.dna)

        if fitness is None:
            fitness = self.eval_fitness(chromosome)
            self.fitness_cache[chromosome.dna] = fitness

        return fitness


Instruction:
Can you generate the documentation for the following Python 3 function
def compete(self, chromosomes):
        # update overall fitness for this run
        self.sort(chromosomes)
        min_fit = self.get_fitness(chromosomes[0])
        max_fit = self.get_fitness(chromosomes[-1])

        if min_fit < self.min_fit_ever:
            self.min_fit_ever = min_fit
        if max_fit > self.max_fit_ever:
            self.max_fit_ever = max_fit

        overall_fit_range = self.max_fit_ever - self.min_fit_ever  # "absolute" fitness range
        current_fit_range = max_fit - min_fit                      # "relative" fitness range

        # choose survivors based on relative fitness within overall fitness range
        survivors = []
        for chromosome in chromosomes:
            fit = self.get_fitness(chromosome)
            p_survival_absolute = (fit - self.min_fit_ever) / overall_fit_range if overall_fit_range != 0 else 1
            p_survival_relative = (fit - min_fit) / current_fit_range if current_fit_range != 0 else 1

            # compute weighted average survival probability
            # a portion accounts for absolute overall fitness for all chromosomes ever encountered (environment-driven)
            # the other portion accounts for relative fitness within the current population (competition-driven)
            p_survival = p_survival_absolute * self.abs_fit_weight + p_survival_relative * self.rel_fit_weight

            if random.random() < p_survival:
                survivors.append(chromosome)

        if not survivors:
            # rarely, nothing survives -- allow everyone to live
            return chromosomes

        return survivors

Response: Simulate competition and survival of the current generation.


Instruction:
Create a Python 3 function to
reproduce the population from a pool of surviving chromosomes and a set of crossover events.

Response:
def reproduce(self, survivors, p_crossover, two_point_crossover=False, target_size=None):
        """
        Reproduces the population from a pool of surviving chromosomes
        until a target population size is met. Offspring are created
        by selecting a survivor. Survivors with higher fitness have a
        greater chance to be selected for reproduction.

        Genetic crossover events may occur for each offspring created.
        Crossover mates are randomly selected from the pool of survivors.
        Crossover points are randomly selected from the length of the crossed chromosomes.
        If crossover does not occur, an offspring is an exact copy of the selected survivor.
        Crossover only affects the DNA of the offspring, not the survivors/parents.

        survivors:  pool of parent chromosomes to reproduce from
        p_crossover:  probability in [0, 1] that a crossover event will
                      occur for each offspring
        two_point_crossover (default=False):  whether 2-point crossover is used; default is 1-point
        target_size (default=original population size): target population size

        return:  list of survivors plus any offspring
        """
        assert 0 <= p_crossover <= 1

        if not target_size:
            target_size = self.orig_pop_size

        num_survivors = len(survivors)

        # compute reproduction cumulative probabilities
        # weakest member gets p=0 but can be crossed-over with
        cdf = compute_fitness_cdf(survivors, self)

        offspring = []
        while num_survivors + len(offspring) < target_size:
            # pick a survivor to reproduce
            c1 = weighted_choice(survivors, cdf).copy()

            # crossover
            if random.random() < p_crossover:
                # randomly pick a crossover mate from survivors
                # same chromosome can be c1 and c2
                c2 = random.choice(survivors).copy()
                point1 = random.randrange(0, c1.length)
                point2 = random.randrange(point1 + 1, c1.length + 1) if two_point_crossover else None
                c1.crossover(c2, point1, point2)

            offspring.append(c1)

        return survivors + offspring


Instruction:
Write a Python 3 script to
call every chromosome s mutate method.

Response:
def mutate(self, chromosomes, p_mutate):
        """
        Call every chromosome's ``mutate`` method.

        p_mutate:  probability of mutation in [0, 1]
        """
        assert 0 <= p_mutate <= 1

        for chromosome in chromosomes:
            chromosome.mutate(p_mutate)


Instruction:
How would you explain what the following Python 3 function does
def run(self, generations, p_mutate, p_crossover, elitist=True, two_point_crossover=False,
            refresh_after=None, quit_after=None):
        """
        Run a standard genetic algorithm simulation for a set number
        of generations (iterations), each consisting of the following
        ordered steps:

          1. competition/survival of the fittest (``compete`` method)
          2. reproduction (``reproduce`` method)
          3. mutation (``mutate`` method)
          4. check if the new population's fittest is fitter than the overall fittest
          4a.  if not and the ``elitist`` option is active, replace the weakest solution
               with the overall fittest

        generations:  how many generations to run
        p_mutate:  probability of mutation in [0, 1]
        p_crossover:  probability in [0, 1] that a crossover event will occur for each offspring
        elitist (default=True):  option to replace the weakest solution with the
                                 strongest if a new one is not found each generation
        two_point_crossover (default=False):  whether 2-point crossover is used
        refresh_after:  number of generations since the last upset after which to randomly generate a new population
        quit_after:  number of generations since the last upset after which to stop the run, possibly before reaching
                     ``generations`` iterations

        return:  the overall fittest solution (chromosome)
        """
        start_time = time.time()

        assert 0 <= p_mutate <= 1
        assert 0 <= p_crossover <= 1

        # these values guaranteed to be replaced in first generation
        self.min_fit_ever =  1e999999999
        self.max_fit_ever = -1e999999999

        self.generation_fittest.clear()
        self.generation_fittest_fit.clear()
        self.overall_fittest_fit.clear()
        self.new_fittest_generations.clear()

        overall_fittest = self.get_fittest()
        overall_fittest_fit = self.get_fitness(overall_fittest)
        gens_since_upset = 0

        for gen in range(1, generations + 1):
            survivors = self.compete(self.chromosomes)
            self.chromosomes = self.reproduce(survivors, p_crossover, two_point_crossover=two_point_crossover)
            self.mutate(self.chromosomes, p_mutate)

            # check for new fittest
            gen_fittest = self.get_fittest().copy()
            gen_fittest_fit = self.get_fitness(gen_fittest)

            if gen_fittest_fit > overall_fittest_fit:
                overall_fittest = gen_fittest
                overall_fittest_fit = gen_fittest_fit
                self.new_fittest_generations.append(gen)
                gens_since_upset = 0
            else:
                gens_since_upset += 1

                if elitist:
                    # no new fittest found, replace least fit with overall fittest
                    self.sort(self.chromosomes)
                    self.chromosomes[0].dna = overall_fittest.dna

            if quit_after and gens_since_upset >= quit_after:
                print("quitting on generation", gen, "after", quit_after, "generations with no upset")
                break

            if refresh_after and gens_since_upset >= refresh_after:
                # been a very long time since a new best solution -- mix things up
                print("refreshing on generation", gen)
                self.mutate(self.chromosomes, 0.5)
                gens_since_upset = 0

            self.generation_fittest[gen] = gen_fittest
            self.generation_fittest_fit[gen] = gen_fittest_fit
            self.overall_fittest_fit[gen] = overall_fittest_fit

            if self.should_terminate(overall_fittest):
                break

            self.fitness_cache.clear()

        self.run_time_s = time.time() - start_time

        return overall_fittest

Response: Run a standard genetic algorithm simulation for a set of generations


Instruction:
How would you implement a function in Python 3 that
creates 1 or more chromosomes with randomly generated DNA.

Response:
def create_random(cls, gene_length, n=1, gene_class=BinaryGene):
        """
        Create 1 or more chromosomes with randomly generated DNA.

        gene_length:  int (or sequence of ints) describing gene DNA length
        n:  number of chromosomes to create (default=1); returns a list if n>1, else a single chromosome
        gene_class:  subclass of ``ga.chromosomes.BaseGene`` to use for genes

        return:  new chromosome
        """
        assert issubclass(gene_class, BaseGene)
        chromosomes = []

        # when gene_length is scalar, convert to a list to keep subsequent code simple
        if not hasattr(gene_length, '__iter__'):
            gene_length = [gene_length]

        for _ in range(n):
            genes = [gene_class.create_random(length) for length in gene_length]
            chromosomes.append(cls(genes))

        if n == 1:
            return chromosomes[0]
        else:
            return chromosomes


Instruction:
Write a Python 3 function for
replacing this chromosome s DNA with new DNA of equal length and assign the new DNA to the genes sequentially.

Response:
def dna(self, dna):
        """
        Replace this chromosome's DNA with new DNA of equal length,
        assigning the new DNA to the chromosome's genes sequentially.

        For example, if a chromosome contains these genes...
          1. 100100
          2. 011011

          ...and the new DNA is 111111000000, the genes become:
          1. 111111
          2. 000000
        """
        assert self.length == len(dna)
        i = 0

        for gene in self.genes:
            gene.dna = dna[i:i + gene.length]

            i += gene.length


Instruction:
Can you generate a brief explanation for the following Python 3 code
def crossover(self, chromosome, point1, point2=None):
        assert self.length == chromosome.length

        if point2 is None:
            new_dna = self.dna[:point1] + chromosome.dna[point1:]
            other_new_dna = chromosome.dna[:point1] + self.dna[point1:]

            self.dna = new_dna
            chromosome.dna = other_new_dna
        else:
            assert point2 > point1
            self_substr = self.dna[point1:point2 + 1]
            other_substr = chromosome.dna[point1:point2 + 1]

            self.dna = self.dna[:point1] + other_substr + self.dna[point2 + 1:]
            chromosome.dna = chromosome.dna[:point1] + self_substr + chromosome.dna[point2 + 1:]

Response: Exchange the DNA with another chromosome at one or two common points.


Instruction:
How would you explain what the following Python 3 function does
def mutate(self, p_mutate):
        assert 0 <= p_mutate <= 1

        for gene in self.genes:
            gene.mutate(p_mutate)

Response: Mutate all genes in this chromosome for the given probability.


Instruction:
Implement a Python 3 function for
returning a new instance of this chromosome by copying its genes.

Response:
def copy(self):
        """ Return a new instance of this chromosome by copying its genes. """
        genes = [g.copy() for g in self.genes]
        return type(self)(genes)


Instruction:
Given the following Python 3 function, write the documentation
def check_genes(self):
        gene_dna_set = set([g.dna for g in self.genes])
        assert gene_dna_set == self.dna_choices_set

Response: Assert that every DNA choice is represented by exactly one gene.


Instruction:
Explain what the following Python 3 code does
def report_error(title=None, data={}, caught=None, is_fatal=False):

    # Don't report errors if NO_ERROR_REPORTING set to 1 (set by run_acceptance_tests)
    if os.environ.get('DO_REPORT_ERROR', None):
        # Force error reporting
        pass
    elif os.environ.get('NO_ERROR_REPORTING', '') == '1':
        log.info("NO_ERROR_REPORTING is set: not reporting error!")
        return
    elif 'is_ec2_instance' in data:
        if not data['is_ec2_instance']:
            # Not running on amazon: no reporting
            log.info("DATA[is_ec2_instance] is False: not reporting error!")
            return
    elif not is_ec2_instance():
        log.info("Not running on an EC2 instance: not reporting error!")
        return

    # Fill error report with tons of usefull data
    if 'user' not in data:
        populate_error_report(data)

    # Add the message
    data['title'] = title
    data['is_fatal_error'] = is_fatal

    # Add the error caught, if any:
    if caught:
        data['error_caught'] = "%s" % caught

    # Add a trace - Formatting traceback may raise a UnicodeDecodeError...
    data['stack'] = []
    try:
        data['stack'] = [l for l in traceback.format_stack()]
    except Exception:
        data['stack'] = 'Skipped trace - contained non-ascii chars'

    # inspect may raise a UnicodeDecodeError...
    fname = ''
    try:
        fname = inspect.stack()[1][3]
    except Exception as e:
        fname = 'unknown-method'

    # Format the error's title
    status, code = 'unknown_status', 'unknown_error_code'
    if 'response' in data:
        status = data['response'].get('status', status)
        code = data['response'].get('error_code', code)
        title_details = "%s %s %s" % (ApiPool().current_server_name, status, code)
    else:
        title_details = "%s %s()" % (ApiPool().current_server_name, fname)

    if is_fatal:
        title_details = 'FATAL ERROR %s' % title_details
    else:
        title_details = 'NON-FATAL ERROR %s' % title_details

    if title:
        title = "%s: %s" % (title_details, title)
    else:
        title = title_details

    global error_reporter
    log.info("Reporting crash...")

    try:
        error_reporter(title, json.dumps(data, sort_keys=True, indent=4))
    except Exception as e:
        # Don't block on replying to api caller
        log.error("Failed to send email report: %s" % str(e))

Response: Format a crash report and send it somewhere relevant.


Instruction:
Make a summary of the following Python 3 code
def populate_error_report(data):

    # Did pymacaron_core set a call_id and call_path?
    call_id, call_path = '', ''
    if hasattr(stack.top, 'call_id'):
        call_id = stack.top.call_id
    if hasattr(stack.top, 'call_path'):
        call_path = stack.top.call_path

    # Unique ID associated to all responses associated to a given
    # call to apis, across all micro-services
    data['call_id'] = call_id
    data['call_path'] = call_path

    # Are we in aws?
    data['is_ec2_instance'] = is_ec2_instance()

    # If user is authenticated, get her id
    user_data = {
        'id': '',
        'is_auth': 0,
        'ip': '',
    }

    if stack.top:
        # We are in a request context
        user_data['ip'] = request.remote_addr

        if 'X-Forwarded-For' in request.headers:
            user_data['forwarded_ip'] = request.headers.get('X-Forwarded-For', '')

        if 'User-Agent' in request.headers:
            user_data['user_agent'] = request.headers.get('User-Agent', '')

    if hasattr(stack.top, 'current_user'):
        user_data['is_auth'] = 1
        user_data['id'] = stack.top.current_user.get('sub', '')
        for k in ('name', 'email', 'is_expert', 'is_admin', 'is_support', 'is_tester', 'language'):
            v = stack.top.current_user.get(k, None)
            if v:
                user_data[k] = v

    data['user'] = user_data

    # Is the current code running as a server?
    if ApiPool().current_server_api:
        # Server info
        server = request.base_url
        server = server.replace('http://', '')
        server = server.replace('https://', '')
        server = server.split('/')[0]
        parts = server.split(':')
        fqdn = parts[0]
        port = parts[1] if len(parts) == 2 else ''

        data['server'] = {
            'fqdn': fqdn,
            'port': port,
            'api_name': ApiPool().current_server_name,
            'api_version': ApiPool().current_server_api.get_version(),
        }

        # Endpoint data
        data['endpoint'] = {
            'id': "%s %s %s" % (ApiPool().current_server_name, request.method, request.path),
            'url': request.url,
            'base_url': request.base_url,
            'path': request.path,
            'method': request.method
        }

Response: Populate the error report with the generic stats from the request


Instruction:
Can you create a Python 3 function that
returns a function that returns a function that will be called when a crash occurs.

Response:
def generate_crash_handler_decorator(error_decorator=None):
    """Return the crash_handler to pass to pymacaron_core, with optional error decoration"""

    def crash_handler(f):
        """Return a decorator that reports failed api calls via the error_reporter,
        for use on every server endpoint"""

        @wraps(f)
        def wrapper(*args, **kwargs):
            """Generate a report of this api call, and if the call failed or was too slow,
            forward this report via the error_reporter"""

            data = {}
            t0 = timenow()
            exception_string = ''

            # Call endpoint and log execution time
            try:
                res = f(*args, **kwargs)
            except Exception as e:
                # An unhandled exception occured!
                exception_string = str(e)
                exc_type, exc_value, exc_traceback = sys.exc_info()
                trace = traceback.format_exception(exc_type, exc_value, exc_traceback, 30)
                data['trace'] = trace

                # If it is a PyMacaronException, just call its http_reply()
                if hasattr(e, 'http_reply'):
                    res = e.http_reply()
                else:
                    # Otherwise, forge a Response
                    e = UnhandledServerError(exception_string)
                    log.error("UNHANDLED EXCEPTION: %s" % '\n'.join(trace))
                    res = e.http_reply()

            t1 = timenow()

            # Is the response an Error instance?
            response_type = type(res).__name__
            status_code = 200
            is_an_error = 0
            error = ''
            error_description = ''
            error_user_message = ''

            error_id = ''

            if isinstance(res, Response):
                # Got a flask.Response object
                res_data = None

                status_code = str(res.status_code)

                if str(status_code) == '200':

                    # It could be any valid json response, but it could also be an Error model
                    # that pymacaron_core handled as a status 200 because it does not know of
                    # pymacaron Errors
                    if res.content_type == 'application/json':
                        s = str(res.data)
                        if '"error":' in s and '"error_description":' in s and '"status":' in s:
                            # This looks like an error, let's decode it
                            res_data = res.get_data()
                else:
                    # Assuming it is a PyMacaronException.http_reply()
                    res_data = res.get_data()

                if res_data:
                    if type(res_data) is bytes:
                        res_data = res_data.decode("utf-8")

                    is_json = True
                    try:
                        j = json.loads(res_data)
                    except ValueError as e:
                        # This was a plain html response. Fake an error
                        is_json = False
                        j = {'error': res_data, 'status': status_code}

                    # Make sure that the response gets the same status as the PyMacaron Error it contained
                    status_code = j['status']
                    res.status_code = int(status_code)

                    # Patch Response to contain a unique id
                    if is_json:
                        if 'error_id' not in j:
                            # If the error is forwarded by multiple micro-services, we
                            # want the error_id to be set only on the original error
                            error_id = str(uuid.uuid4())
                            j['error_id'] = error_id
                            res.set_data(json.dumps(j))

                        if error_decorator:
                            # Apply error_decorator, if any defined
                            res.set_data(json.dumps(error_decorator(j)))

                    # And extract data from this error
                    error = j.get('error', 'NO_ERROR_IN_JSON')
                    error_description = j.get('error_description', res_data)
                    if error_description == '':
                        error_description = res_data

                    if not exception_string:
                        exception_string = error_description

                    error_user_message = j.get('user_message', '')
                    is_an_error = 1

            request_args = []
            if len(args):
                request_args.append(args)
            if kwargs:
                request_args.append(kwargs)

            data.update({
                # Set only on the original error, not on forwarded ones, not on
                # success responses
                'error_id': error_id,

                # Call results
                'time': {
                    'start': t0.isoformat(),
                    'end': t1.isoformat(),
                    'microsecs': (t1.timestamp() - t0.timestamp()) * 1000000,
                },

                # Response details
                'response': {
                    'type': response_type,
                    'status': str(status_code),
                    'is_error': is_an_error,
                    'error_code': error,
                    'error_description': error_description,
                    'user_message': error_user_message,
                },

                # Request details
                'request': {
                    'params': pformat(request_args),
                },
            })

            populate_error_report(data)

            # inspect may raise a UnicodeDecodeError...
            fname = function_name(f)

            #
            # Should we report this call?
            #

            # If it is an internal errors, report it
            if data['response']['status'] and int(data['response']['status']) >= 500:
                report_error(
                    title="%s(): %s" % (fname, exception_string),
                    data=data,
                    is_fatal=True
                )

            log.info("")
            log.info(" <= Done!")
            log.info("")

            return res

        return wrapper

    return crash_handler